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;
27 static const 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;
49 unsigned short report_desc_length;
51 unsigned short report_length;
53 * use_out_ep - if true, the OUT Endpoint (interrupt out method)
54 * will be used to receive reports from the host
55 * using functions with the "intout" suffix.
56 * Otherwise, the OUT Endpoint will not be configured
57 * and the SETUP/SET_REPORT method ("ssreport" suffix)
58 * will be used to receive reports.
63 spinlock_t read_spinlock;
64 wait_queue_head_t read_queue;
65 /* recv report - interrupt out only (use_out_ep == 1) */
66 struct list_head completed_out_req;
68 /* recv report - setup set_report only (use_out_ep == 0) */
70 unsigned int set_report_length;
73 spinlock_t write_spinlock;
75 wait_queue_head_t write_queue;
76 struct usb_request *req;
80 struct usb_function func;
83 struct usb_ep *out_ep;
86 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
88 return container_of(f, struct f_hidg, func);
91 static void hidg_release(struct device *dev)
93 struct f_hidg *hidg = container_of(dev, struct f_hidg, dev);
95 kfree(hidg->report_desc);
96 kfree(hidg->set_report_buf);
100 /*-------------------------------------------------------------------------*/
101 /* Static descriptors */
103 static struct usb_interface_descriptor hidg_interface_desc = {
104 .bLength = sizeof hidg_interface_desc,
105 .bDescriptorType = USB_DT_INTERFACE,
106 /* .bInterfaceNumber = DYNAMIC */
107 .bAlternateSetting = 0,
108 /* .bNumEndpoints = DYNAMIC (depends on use_out_ep) */
109 .bInterfaceClass = USB_CLASS_HID,
110 /* .bInterfaceSubClass = DYNAMIC */
111 /* .bInterfaceProtocol = DYNAMIC */
112 /* .iInterface = DYNAMIC */
115 static struct hid_descriptor hidg_desc = {
116 .bLength = sizeof hidg_desc,
117 .bDescriptorType = HID_DT_HID,
118 .bcdHID = cpu_to_le16(0x0101),
119 .bCountryCode = 0x00,
120 .bNumDescriptors = 0x1,
121 /*.desc[0].bDescriptorType = DYNAMIC */
122 /*.desc[0].wDescriptorLenght = DYNAMIC */
125 /* Super-Speed Support */
127 static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
128 .bLength = USB_DT_ENDPOINT_SIZE,
129 .bDescriptorType = USB_DT_ENDPOINT,
130 .bEndpointAddress = USB_DIR_IN,
131 .bmAttributes = USB_ENDPOINT_XFER_INT,
132 /*.wMaxPacketSize = DYNAMIC */
133 .bInterval = 4, /* FIXME: Add this field in the
134 * HID gadget configuration?
135 * (struct hidg_func_descriptor)
139 static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
140 .bLength = sizeof(hidg_ss_in_comp_desc),
141 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
143 /* .bMaxBurst = 0, */
144 /* .bmAttributes = 0, */
145 /* .wBytesPerInterval = DYNAMIC */
148 static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
149 .bLength = USB_DT_ENDPOINT_SIZE,
150 .bDescriptorType = USB_DT_ENDPOINT,
151 .bEndpointAddress = USB_DIR_OUT,
152 .bmAttributes = USB_ENDPOINT_XFER_INT,
153 /*.wMaxPacketSize = DYNAMIC */
154 .bInterval = 4, /* FIXME: Add this field in the
155 * HID gadget configuration?
156 * (struct hidg_func_descriptor)
160 static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
161 .bLength = sizeof(hidg_ss_out_comp_desc),
162 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
164 /* .bMaxBurst = 0, */
165 /* .bmAttributes = 0, */
166 /* .wBytesPerInterval = DYNAMIC */
169 static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = {
170 (struct usb_descriptor_header *)&hidg_interface_desc,
171 (struct usb_descriptor_header *)&hidg_desc,
172 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
173 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
174 (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
175 (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
179 static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = {
180 (struct usb_descriptor_header *)&hidg_interface_desc,
181 (struct usb_descriptor_header *)&hidg_desc,
182 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
183 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
187 /* High-Speed Support */
189 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192 .bEndpointAddress = USB_DIR_IN,
193 .bmAttributes = USB_ENDPOINT_XFER_INT,
194 /*.wMaxPacketSize = DYNAMIC */
195 .bInterval = 4, /* FIXME: Add this field in the
196 * HID gadget configuration?
197 * (struct hidg_func_descriptor)
201 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
204 .bEndpointAddress = USB_DIR_OUT,
205 .bmAttributes = USB_ENDPOINT_XFER_INT,
206 /*.wMaxPacketSize = DYNAMIC */
207 .bInterval = 4, /* FIXME: Add this field in the
208 * HID gadget configuration?
209 * (struct hidg_func_descriptor)
213 static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = {
214 (struct usb_descriptor_header *)&hidg_interface_desc,
215 (struct usb_descriptor_header *)&hidg_desc,
216 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
217 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
221 static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = {
222 (struct usb_descriptor_header *)&hidg_interface_desc,
223 (struct usb_descriptor_header *)&hidg_desc,
224 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
228 /* Full-Speed Support */
230 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233 .bEndpointAddress = USB_DIR_IN,
234 .bmAttributes = USB_ENDPOINT_XFER_INT,
235 /*.wMaxPacketSize = DYNAMIC */
236 .bInterval = 10, /* FIXME: Add this field in the
237 * HID gadget configuration?
238 * (struct hidg_func_descriptor)
242 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
243 .bLength = USB_DT_ENDPOINT_SIZE,
244 .bDescriptorType = USB_DT_ENDPOINT,
245 .bEndpointAddress = USB_DIR_OUT,
246 .bmAttributes = USB_ENDPOINT_XFER_INT,
247 /*.wMaxPacketSize = DYNAMIC */
248 .bInterval = 10, /* FIXME: Add this field in the
249 * HID gadget configuration?
250 * (struct hidg_func_descriptor)
254 static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = {
255 (struct usb_descriptor_header *)&hidg_interface_desc,
256 (struct usb_descriptor_header *)&hidg_desc,
257 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
258 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
262 static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = {
263 (struct usb_descriptor_header *)&hidg_interface_desc,
264 (struct usb_descriptor_header *)&hidg_desc,
265 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
269 /*-------------------------------------------------------------------------*/
272 #define CT_FUNC_HID_IDX 0
274 static struct usb_string ct_func_string_defs[] = {
275 [CT_FUNC_HID_IDX].s = "HID Interface",
276 {}, /* end of list */
279 static struct usb_gadget_strings ct_func_string_table = {
280 .language = 0x0409, /* en-US */
281 .strings = ct_func_string_defs,
284 static struct usb_gadget_strings *ct_func_strings[] = {
285 &ct_func_string_table,
289 /*-------------------------------------------------------------------------*/
292 static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer,
293 size_t count, loff_t *ptr)
295 struct f_hidg *hidg = file->private_data;
296 struct f_hidg_req_list *list;
297 struct usb_request *req;
304 spin_lock_irqsave(&hidg->read_spinlock, flags);
306 #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
308 /* wait for at least one buffer to complete */
309 while (!READ_COND_INTOUT) {
310 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
311 if (file->f_flags & O_NONBLOCK)
314 if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT))
317 spin_lock_irqsave(&hidg->read_spinlock, flags);
320 /* pick the first one */
321 list = list_first_entry(&hidg->completed_out_req,
322 struct f_hidg_req_list, list);
325 * Remove this from list to protect it from beign free()
326 * while host disables our function
328 list_del(&list->list);
331 count = min_t(unsigned int, count, req->actual - list->pos);
332 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
334 /* copy to user outside spinlock */
335 count -= copy_to_user(buffer, req->buf + list->pos, count);
339 * if this request is completely handled and transfered to
340 * userspace, remove its entry from the list and requeue it
341 * again. Otherwise, we will revisit it again upon the next
342 * call, taking into account its current read position.
344 if (list->pos == req->actual) {
347 req->length = hidg->report_length;
348 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
350 free_ep_req(hidg->out_ep, req);
354 spin_lock_irqsave(&hidg->read_spinlock, flags);
355 list_add(&list->list, &hidg->completed_out_req);
356 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
358 wake_up(&hidg->read_queue);
364 #define READ_COND_SSREPORT (hidg->set_report_buf != NULL)
366 static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer,
367 size_t count, loff_t *ptr)
369 struct f_hidg *hidg = file->private_data;
370 char *tmp_buf = NULL;
376 spin_lock_irqsave(&hidg->read_spinlock, flags);
378 while (!READ_COND_SSREPORT) {
379 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
380 if (file->f_flags & O_NONBLOCK)
383 if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT))
386 spin_lock_irqsave(&hidg->read_spinlock, flags);
389 count = min_t(unsigned int, count, hidg->set_report_length);
390 tmp_buf = hidg->set_report_buf;
391 hidg->set_report_buf = NULL;
393 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
395 if (tmp_buf != NULL) {
396 count -= copy_to_user(buffer, tmp_buf, count);
402 wake_up(&hidg->read_queue);
407 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
408 size_t count, loff_t *ptr)
410 struct f_hidg *hidg = file->private_data;
412 if (hidg->use_out_ep)
413 return f_hidg_intout_read(file, buffer, count, ptr);
415 return f_hidg_ssreport_read(file, buffer, count, ptr);
418 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
420 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
423 if (req->status != 0) {
424 ERROR(hidg->func.config->cdev,
425 "End Point Request ERROR: %d\n", req->status);
428 spin_lock_irqsave(&hidg->write_spinlock, flags);
429 hidg->write_pending = 0;
430 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
431 wake_up(&hidg->write_queue);
434 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
435 size_t count, loff_t *offp)
437 struct f_hidg *hidg = file->private_data;
438 struct usb_request *req;
440 ssize_t status = -ENOMEM;
442 spin_lock_irqsave(&hidg->write_spinlock, flags);
445 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
449 #define WRITE_COND (!hidg->write_pending)
452 while (!WRITE_COND) {
453 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
454 if (file->f_flags & O_NONBLOCK)
457 if (wait_event_interruptible_exclusive(
458 hidg->write_queue, WRITE_COND))
461 spin_lock_irqsave(&hidg->write_spinlock, flags);
464 hidg->write_pending = 1;
466 count = min_t(unsigned, count, hidg->report_length);
468 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
471 ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
473 goto release_write_pending;
476 status = copy_from_user(req->buf, buffer, count);
478 ERROR(hidg->func.config->cdev,
479 "copy_from_user error\n");
481 goto release_write_pending;
484 spin_lock_irqsave(&hidg->write_spinlock, flags);
486 /* when our function has been disabled by host */
488 free_ep_req(hidg->in_ep, req);
491 * Should we fail with error here?
499 req->complete = f_hidg_req_complete;
502 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
504 if (!hidg->in_ep->enabled) {
505 ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
507 goto release_write_pending;
510 status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
512 goto release_write_pending;
517 release_write_pending:
518 spin_lock_irqsave(&hidg->write_spinlock, flags);
519 hidg->write_pending = 0;
520 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
522 wake_up(&hidg->write_queue);
527 static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
529 struct f_hidg *hidg = file->private_data;
532 poll_wait(file, &hidg->read_queue, wait);
533 poll_wait(file, &hidg->write_queue, wait);
536 ret |= EPOLLOUT | EPOLLWRNORM;
538 if (hidg->use_out_ep) {
539 if (READ_COND_INTOUT)
540 ret |= EPOLLIN | EPOLLRDNORM;
542 if (READ_COND_SSREPORT)
543 ret |= EPOLLIN | EPOLLRDNORM;
550 #undef READ_COND_SSREPORT
551 #undef READ_COND_INTOUT
553 static int f_hidg_release(struct inode *inode, struct file *fd)
555 fd->private_data = NULL;
559 static int f_hidg_open(struct inode *inode, struct file *fd)
561 struct f_hidg *hidg =
562 container_of(inode->i_cdev, struct f_hidg, cdev);
564 fd->private_data = hidg;
569 /*-------------------------------------------------------------------------*/
572 static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
575 return alloc_ep_req(ep, length);
578 static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req)
580 struct f_hidg *hidg = (struct f_hidg *) req->context;
581 struct usb_composite_dev *cdev = hidg->func.config->cdev;
582 struct f_hidg_req_list *req_list;
585 switch (req->status) {
587 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
589 ERROR(cdev, "Unable to allocate mem for req_list\n");
595 spin_lock_irqsave(&hidg->read_spinlock, flags);
596 list_add_tail(&req_list->list, &hidg->completed_out_req);
597 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
599 wake_up(&hidg->read_queue);
602 ERROR(cdev, "Set report failed %d\n", req->status);
604 case -ECONNABORTED: /* hardware forced ep reset */
605 case -ECONNRESET: /* request dequeued */
606 case -ESHUTDOWN: /* disconnect from host */
608 free_ep_req(ep, req);
613 static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req)
615 struct f_hidg *hidg = (struct f_hidg *)req->context;
616 struct usb_composite_dev *cdev = hidg->func.config->cdev;
617 char *new_buf = NULL;
620 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
622 "%s FAILED: status=%d, buf=%p, actual=%d\n",
623 __func__, req->status, req->buf, req->actual);
627 spin_lock_irqsave(&hidg->read_spinlock, flags);
629 new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC);
630 if (new_buf == NULL) {
631 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
634 hidg->set_report_buf = new_buf;
636 hidg->set_report_length = req->actual;
637 memcpy(hidg->set_report_buf, req->buf, req->actual);
639 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
641 wake_up(&hidg->read_queue);
644 static int hidg_setup(struct usb_function *f,
645 const struct usb_ctrlrequest *ctrl)
647 struct f_hidg *hidg = func_to_hidg(f);
648 struct usb_composite_dev *cdev = f->config->cdev;
649 struct usb_request *req = cdev->req;
653 value = __le16_to_cpu(ctrl->wValue);
654 length = __le16_to_cpu(ctrl->wLength);
657 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
658 __func__, ctrl->bRequestType, ctrl->bRequest, value);
660 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
661 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
662 | HID_REQ_GET_REPORT):
663 VDBG(cdev, "get_report\n");
665 /* send an empty report */
666 length = min_t(unsigned, length, hidg->report_length);
667 memset(req->buf, 0x0, length);
672 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
673 | HID_REQ_GET_PROTOCOL):
674 VDBG(cdev, "get_protocol\n");
675 length = min_t(unsigned int, length, 1);
676 ((u8 *) req->buf)[0] = hidg->protocol;
680 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
682 VDBG(cdev, "get_idle\n");
683 length = min_t(unsigned int, length, 1);
684 ((u8 *) req->buf)[0] = hidg->idle;
688 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
689 | HID_REQ_SET_REPORT):
690 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
691 if (hidg->use_out_ep)
693 req->complete = hidg_ssreport_complete;
698 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
699 | HID_REQ_SET_PROTOCOL):
700 VDBG(cdev, "set_protocol\n");
701 if (value > HID_REPORT_PROTOCOL)
705 * We assume that programs implementing the Boot protocol
706 * are also compatible with the Report Protocol
708 if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
709 hidg->protocol = value;
715 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
717 VDBG(cdev, "set_idle\n");
719 hidg->idle = value >> 8;
723 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
724 | USB_REQ_GET_DESCRIPTOR):
725 switch (value >> 8) {
728 struct hid_descriptor hidg_desc_copy = hidg_desc;
730 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
731 hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
732 hidg_desc_copy.desc[0].wDescriptorLength =
733 cpu_to_le16(hidg->report_desc_length);
735 length = min_t(unsigned short, length,
736 hidg_desc_copy.bLength);
737 memcpy(req->buf, &hidg_desc_copy, length);
742 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
743 length = min_t(unsigned short, length,
744 hidg->report_desc_length);
745 memcpy(req->buf, hidg->report_desc, length);
750 VDBG(cdev, "Unknown descriptor request 0x%x\n",
758 VDBG(cdev, "Unknown request 0x%x\n",
769 req->length = length;
770 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
772 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
776 static void hidg_disable(struct usb_function *f)
778 struct f_hidg *hidg = func_to_hidg(f);
779 struct f_hidg_req_list *list, *next;
782 usb_ep_disable(hidg->in_ep);
785 usb_ep_disable(hidg->out_ep);
787 spin_lock_irqsave(&hidg->read_spinlock, flags);
788 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
789 free_ep_req(hidg->out_ep, list->req);
790 list_del(&list->list);
793 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
796 spin_lock_irqsave(&hidg->write_spinlock, flags);
797 if (!hidg->write_pending) {
798 free_ep_req(hidg->in_ep, hidg->req);
799 hidg->write_pending = 1;
803 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
806 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
808 struct usb_composite_dev *cdev = f->config->cdev;
809 struct f_hidg *hidg = func_to_hidg(f);
810 struct usb_request *req_in = NULL;
814 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
816 if (hidg->in_ep != NULL) {
817 /* restart endpoint */
818 usb_ep_disable(hidg->in_ep);
820 status = config_ep_by_speed(f->config->cdev->gadget, f,
823 ERROR(cdev, "config_ep_by_speed FAILED!\n");
826 status = usb_ep_enable(hidg->in_ep);
828 ERROR(cdev, "Enable IN endpoint FAILED!\n");
831 hidg->in_ep->driver_data = hidg;
833 req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
840 if (hidg->use_out_ep && hidg->out_ep != NULL) {
841 /* restart endpoint */
842 usb_ep_disable(hidg->out_ep);
844 status = config_ep_by_speed(f->config->cdev->gadget, f,
847 ERROR(cdev, "config_ep_by_speed FAILED!\n");
850 status = usb_ep_enable(hidg->out_ep);
852 ERROR(cdev, "Enable OUT endpoint FAILED!\n");
855 hidg->out_ep->driver_data = hidg;
858 * allocate a bunch of read buffers and queue them all at once.
860 for (i = 0; i < hidg->qlen && status == 0; i++) {
861 struct usb_request *req =
862 hidg_alloc_ep_req(hidg->out_ep,
863 hidg->report_length);
865 req->complete = hidg_intout_complete;
867 status = usb_ep_queue(hidg->out_ep, req,
870 ERROR(cdev, "%s queue req --> %d\n",
871 hidg->out_ep->name, status);
872 free_ep_req(hidg->out_ep, req);
881 if (hidg->in_ep != NULL) {
882 spin_lock_irqsave(&hidg->write_spinlock, flags);
884 hidg->write_pending = 0;
885 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
887 wake_up(&hidg->write_queue);
892 usb_ep_disable(hidg->out_ep);
895 free_ep_req(hidg->in_ep, req_in);
899 usb_ep_disable(hidg->in_ep);
905 static const struct file_operations f_hidg_fops = {
906 .owner = THIS_MODULE,
908 .release = f_hidg_release,
909 .write = f_hidg_write,
912 .llseek = noop_llseek,
915 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
918 struct f_hidg *hidg = func_to_hidg(f);
919 struct usb_string *us;
922 /* maybe allocate device-global string IDs, and patch descriptors */
923 us = usb_gstrings_attach(c->cdev, ct_func_strings,
924 ARRAY_SIZE(ct_func_string_defs));
927 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
929 /* allocate instance-specific interface IDs, and patch descriptors */
930 status = usb_interface_id(c, f);
933 hidg_interface_desc.bInterfaceNumber = status;
935 /* allocate instance-specific endpoints */
937 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
943 if (hidg->use_out_ep) {
944 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
950 /* used only if use_out_ep == 1 */
951 hidg->set_report_buf = NULL;
953 /* set descriptor dynamic values */
954 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
955 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
956 hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1;
957 hidg->protocol = HID_REPORT_PROTOCOL;
959 hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
960 hidg_ss_in_comp_desc.wBytesPerInterval =
961 cpu_to_le16(hidg->report_length);
962 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
963 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
964 hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
965 hidg_ss_out_comp_desc.wBytesPerInterval =
966 cpu_to_le16(hidg->report_length);
967 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
968 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
970 * We can use hidg_desc struct here but we should not relay
971 * that its content won't change after returning from this function.
973 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
974 hidg_desc.desc[0].wDescriptorLength =
975 cpu_to_le16(hidg->report_desc_length);
977 hidg_hs_in_ep_desc.bEndpointAddress =
978 hidg_fs_in_ep_desc.bEndpointAddress;
979 hidg_hs_out_ep_desc.bEndpointAddress =
980 hidg_fs_out_ep_desc.bEndpointAddress;
982 hidg_ss_in_ep_desc.bEndpointAddress =
983 hidg_fs_in_ep_desc.bEndpointAddress;
984 hidg_ss_out_ep_desc.bEndpointAddress =
985 hidg_fs_out_ep_desc.bEndpointAddress;
987 if (hidg->use_out_ep)
988 status = usb_assign_descriptors(f,
989 hidg_fs_descriptors_intout,
990 hidg_hs_descriptors_intout,
991 hidg_ss_descriptors_intout,
992 hidg_ss_descriptors_intout);
994 status = usb_assign_descriptors(f,
995 hidg_fs_descriptors_ssreport,
996 hidg_hs_descriptors_ssreport,
997 hidg_ss_descriptors_ssreport,
998 hidg_ss_descriptors_ssreport);
1003 spin_lock_init(&hidg->write_spinlock);
1004 hidg->write_pending = 1;
1006 spin_lock_init(&hidg->read_spinlock);
1007 init_waitqueue_head(&hidg->write_queue);
1008 init_waitqueue_head(&hidg->read_queue);
1009 INIT_LIST_HEAD(&hidg->completed_out_req);
1011 /* create char device */
1012 cdev_init(&hidg->cdev, &f_hidg_fops);
1013 status = cdev_device_add(&hidg->cdev, &hidg->dev);
1015 goto fail_free_descs;
1019 usb_free_all_descriptors(f);
1021 ERROR(f->config->cdev, "hidg_bind FAILED\n");
1022 if (hidg->req != NULL)
1023 free_ep_req(hidg->in_ep, hidg->req);
1028 static inline int hidg_get_minor(void)
1032 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
1033 if (ret >= HIDG_MINORS) {
1034 ida_simple_remove(&hidg_ida, ret);
1041 static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
1043 return container_of(to_config_group(item), struct f_hid_opts,
1047 static void hid_attr_release(struct config_item *item)
1049 struct f_hid_opts *opts = to_f_hid_opts(item);
1051 usb_put_function_instance(&opts->func_inst);
1054 static struct configfs_item_operations hidg_item_ops = {
1055 .release = hid_attr_release,
1058 #define F_HID_OPT(name, prec, limit) \
1059 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
1061 struct f_hid_opts *opts = to_f_hid_opts(item); \
1064 mutex_lock(&opts->lock); \
1065 result = sprintf(page, "%d\n", opts->name); \
1066 mutex_unlock(&opts->lock); \
1071 static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
1072 const char *page, size_t len) \
1074 struct f_hid_opts *opts = to_f_hid_opts(item); \
1078 mutex_lock(&opts->lock); \
1079 if (opts->refcnt) { \
1084 ret = kstrtou##prec(page, 0, &num); \
1088 if (num > limit) { \
1096 mutex_unlock(&opts->lock); \
1100 CONFIGFS_ATTR(f_hid_opts_, name)
1102 F_HID_OPT(subclass, 8, 255);
1103 F_HID_OPT(protocol, 8, 255);
1104 F_HID_OPT(no_out_endpoint, 8, 1);
1105 F_HID_OPT(report_length, 16, 65535);
1107 static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
1109 struct f_hid_opts *opts = to_f_hid_opts(item);
1112 mutex_lock(&opts->lock);
1113 result = opts->report_desc_length;
1114 memcpy(page, opts->report_desc, opts->report_desc_length);
1115 mutex_unlock(&opts->lock);
1120 static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
1121 const char *page, size_t len)
1123 struct f_hid_opts *opts = to_f_hid_opts(item);
1127 mutex_lock(&opts->lock);
1131 if (len > PAGE_SIZE) {
1135 d = kmemdup(page, len, GFP_KERNEL);
1140 kfree(opts->report_desc);
1141 opts->report_desc = d;
1142 opts->report_desc_length = len;
1143 opts->report_desc_alloc = true;
1146 mutex_unlock(&opts->lock);
1150 CONFIGFS_ATTR(f_hid_opts_, report_desc);
1152 static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
1154 struct f_hid_opts *opts = to_f_hid_opts(item);
1156 return sprintf(page, "%d:%d\n", major, opts->minor);
1159 CONFIGFS_ATTR_RO(f_hid_opts_, dev);
1161 static struct configfs_attribute *hid_attrs[] = {
1162 &f_hid_opts_attr_subclass,
1163 &f_hid_opts_attr_protocol,
1164 &f_hid_opts_attr_no_out_endpoint,
1165 &f_hid_opts_attr_report_length,
1166 &f_hid_opts_attr_report_desc,
1167 &f_hid_opts_attr_dev,
1171 static const struct config_item_type hid_func_type = {
1172 .ct_item_ops = &hidg_item_ops,
1173 .ct_attrs = hid_attrs,
1174 .ct_owner = THIS_MODULE,
1177 static inline void hidg_put_minor(int minor)
1179 ida_simple_remove(&hidg_ida, minor);
1182 static void hidg_free_inst(struct usb_function_instance *f)
1184 struct f_hid_opts *opts;
1186 opts = container_of(f, struct f_hid_opts, func_inst);
1188 mutex_lock(&hidg_ida_lock);
1190 hidg_put_minor(opts->minor);
1191 if (ida_is_empty(&hidg_ida))
1194 mutex_unlock(&hidg_ida_lock);
1196 if (opts->report_desc_alloc)
1197 kfree(opts->report_desc);
1202 static struct usb_function_instance *hidg_alloc_inst(void)
1204 struct f_hid_opts *opts;
1205 struct usb_function_instance *ret;
1208 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1210 return ERR_PTR(-ENOMEM);
1211 mutex_init(&opts->lock);
1212 opts->func_inst.free_func_inst = hidg_free_inst;
1213 ret = &opts->func_inst;
1215 mutex_lock(&hidg_ida_lock);
1217 if (ida_is_empty(&hidg_ida)) {
1218 status = ghid_setup(NULL, HIDG_MINORS);
1220 ret = ERR_PTR(status);
1226 opts->minor = hidg_get_minor();
1227 if (opts->minor < 0) {
1228 ret = ERR_PTR(opts->minor);
1230 if (ida_is_empty(&hidg_ida))
1234 config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1237 mutex_unlock(&hidg_ida_lock);
1241 static void hidg_free(struct usb_function *f)
1243 struct f_hidg *hidg;
1244 struct f_hid_opts *opts;
1246 hidg = func_to_hidg(f);
1247 opts = container_of(f->fi, struct f_hid_opts, func_inst);
1248 put_device(&hidg->dev);
1249 mutex_lock(&opts->lock);
1251 mutex_unlock(&opts->lock);
1254 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1256 struct f_hidg *hidg = func_to_hidg(f);
1258 cdev_device_del(&hidg->cdev, &hidg->dev);
1260 usb_free_all_descriptors(f);
1263 static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
1265 struct f_hidg *hidg;
1266 struct f_hid_opts *opts;
1269 /* allocate and initialize one new instance */
1270 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
1272 return ERR_PTR(-ENOMEM);
1274 opts = container_of(fi, struct f_hid_opts, func_inst);
1276 mutex_lock(&opts->lock);
1278 device_initialize(&hidg->dev);
1279 hidg->dev.release = hidg_release;
1280 hidg->dev.class = &hidg_class;
1281 hidg->dev.devt = MKDEV(major, opts->minor);
1282 ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1286 hidg->bInterfaceSubClass = opts->subclass;
1287 hidg->bInterfaceProtocol = opts->protocol;
1288 hidg->report_length = opts->report_length;
1289 hidg->report_desc_length = opts->report_desc_length;
1290 if (opts->report_desc) {
1291 hidg->report_desc = kmemdup(opts->report_desc,
1292 opts->report_desc_length,
1294 if (!hidg->report_desc) {
1296 goto err_put_device;
1299 hidg->use_out_ep = !opts->no_out_endpoint;
1302 mutex_unlock(&opts->lock);
1304 hidg->func.name = "hid";
1305 hidg->func.bind = hidg_bind;
1306 hidg->func.unbind = hidg_unbind;
1307 hidg->func.set_alt = hidg_set_alt;
1308 hidg->func.disable = hidg_disable;
1309 hidg->func.setup = hidg_setup;
1310 hidg->func.free_func = hidg_free;
1312 /* this could be made configurable at some point */
1318 put_device(&hidg->dev);
1320 mutex_unlock(&opts->lock);
1321 return ERR_PTR(ret);
1324 DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1325 MODULE_LICENSE("GPL");
1326 MODULE_AUTHOR("Fabien Chouteau");
1328 int ghid_setup(struct usb_gadget *g, int count)
1333 status = class_register(&hidg_class);
1337 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1339 class_unregister(&hidg_class);
1349 void ghid_cleanup(void)
1352 unregister_chrdev_region(MKDEV(major, 0), minors);
1356 class_unregister(&hidg_class);