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;
45 unsigned short report_desc_length;
47 unsigned short report_length;
49 * use_out_ep - if true, the OUT Endpoint (interrupt out method)
50 * will be used to receive reports from the host
51 * using functions with the "intout" suffix.
52 * Otherwise, the OUT Endpoint will not be configured
53 * and the SETUP/SET_REPORT method ("ssreport" suffix)
54 * will be used to receive reports.
59 spinlock_t read_spinlock;
60 wait_queue_head_t read_queue;
61 /* recv report - interrupt out only (use_out_ep == 1) */
62 struct list_head completed_out_req;
64 /* recv report - setup set_report only (use_out_ep == 0) */
66 unsigned int set_report_length;
69 spinlock_t write_spinlock;
71 wait_queue_head_t write_queue;
72 struct usb_request *req;
76 struct usb_function func;
79 struct usb_ep *out_ep;
82 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
84 return container_of(f, struct f_hidg, func);
87 static void hidg_release(struct device *dev)
89 struct f_hidg *hidg = container_of(dev, struct f_hidg, dev);
91 kfree(hidg->report_desc);
92 kfree(hidg->set_report_buf);
96 /*-------------------------------------------------------------------------*/
97 /* Static descriptors */
99 static struct usb_interface_descriptor hidg_interface_desc = {
100 .bLength = sizeof hidg_interface_desc,
101 .bDescriptorType = USB_DT_INTERFACE,
102 /* .bInterfaceNumber = DYNAMIC */
103 .bAlternateSetting = 0,
104 /* .bNumEndpoints = DYNAMIC (depends on use_out_ep) */
105 .bInterfaceClass = USB_CLASS_HID,
106 /* .bInterfaceSubClass = DYNAMIC */
107 /* .bInterfaceProtocol = DYNAMIC */
108 /* .iInterface = DYNAMIC */
111 static struct hid_descriptor hidg_desc = {
112 .bLength = sizeof hidg_desc,
113 .bDescriptorType = HID_DT_HID,
114 .bcdHID = cpu_to_le16(0x0101),
115 .bCountryCode = 0x00,
116 .bNumDescriptors = 0x1,
117 /*.desc[0].bDescriptorType = DYNAMIC */
118 /*.desc[0].wDescriptorLenght = DYNAMIC */
121 /* Super-Speed Support */
123 static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
124 .bLength = USB_DT_ENDPOINT_SIZE,
125 .bDescriptorType = USB_DT_ENDPOINT,
126 .bEndpointAddress = USB_DIR_IN,
127 .bmAttributes = USB_ENDPOINT_XFER_INT,
128 /*.wMaxPacketSize = DYNAMIC */
129 .bInterval = 4, /* FIXME: Add this field in the
130 * HID gadget configuration?
131 * (struct hidg_func_descriptor)
135 static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
136 .bLength = sizeof(hidg_ss_in_comp_desc),
137 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
139 /* .bMaxBurst = 0, */
140 /* .bmAttributes = 0, */
141 /* .wBytesPerInterval = DYNAMIC */
144 static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
145 .bLength = USB_DT_ENDPOINT_SIZE,
146 .bDescriptorType = USB_DT_ENDPOINT,
147 .bEndpointAddress = USB_DIR_OUT,
148 .bmAttributes = USB_ENDPOINT_XFER_INT,
149 /*.wMaxPacketSize = DYNAMIC */
150 .bInterval = 4, /* FIXME: Add this field in the
151 * HID gadget configuration?
152 * (struct hidg_func_descriptor)
156 static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
157 .bLength = sizeof(hidg_ss_out_comp_desc),
158 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
160 /* .bMaxBurst = 0, */
161 /* .bmAttributes = 0, */
162 /* .wBytesPerInterval = DYNAMIC */
165 static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = {
166 (struct usb_descriptor_header *)&hidg_interface_desc,
167 (struct usb_descriptor_header *)&hidg_desc,
168 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
169 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
170 (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
171 (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
175 static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = {
176 (struct usb_descriptor_header *)&hidg_interface_desc,
177 (struct usb_descriptor_header *)&hidg_desc,
178 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
179 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
183 /* High-Speed Support */
185 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_INT,
190 /*.wMaxPacketSize = DYNAMIC */
191 .bInterval = 4, /* FIXME: Add this field in the
192 * HID gadget configuration?
193 * (struct hidg_func_descriptor)
197 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
198 .bLength = USB_DT_ENDPOINT_SIZE,
199 .bDescriptorType = USB_DT_ENDPOINT,
200 .bEndpointAddress = USB_DIR_OUT,
201 .bmAttributes = USB_ENDPOINT_XFER_INT,
202 /*.wMaxPacketSize = DYNAMIC */
203 .bInterval = 4, /* FIXME: Add this field in the
204 * HID gadget configuration?
205 * (struct hidg_func_descriptor)
209 static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = {
210 (struct usb_descriptor_header *)&hidg_interface_desc,
211 (struct usb_descriptor_header *)&hidg_desc,
212 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
213 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
217 static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = {
218 (struct usb_descriptor_header *)&hidg_interface_desc,
219 (struct usb_descriptor_header *)&hidg_desc,
220 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
224 /* Full-Speed Support */
226 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
227 .bLength = USB_DT_ENDPOINT_SIZE,
228 .bDescriptorType = USB_DT_ENDPOINT,
229 .bEndpointAddress = USB_DIR_IN,
230 .bmAttributes = USB_ENDPOINT_XFER_INT,
231 /*.wMaxPacketSize = DYNAMIC */
232 .bInterval = 10, /* FIXME: Add this field in the
233 * HID gadget configuration?
234 * (struct hidg_func_descriptor)
238 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
239 .bLength = USB_DT_ENDPOINT_SIZE,
240 .bDescriptorType = USB_DT_ENDPOINT,
241 .bEndpointAddress = USB_DIR_OUT,
242 .bmAttributes = USB_ENDPOINT_XFER_INT,
243 /*.wMaxPacketSize = DYNAMIC */
244 .bInterval = 10, /* FIXME: Add this field in the
245 * HID gadget configuration?
246 * (struct hidg_func_descriptor)
250 static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = {
251 (struct usb_descriptor_header *)&hidg_interface_desc,
252 (struct usb_descriptor_header *)&hidg_desc,
253 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
254 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
258 static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = {
259 (struct usb_descriptor_header *)&hidg_interface_desc,
260 (struct usb_descriptor_header *)&hidg_desc,
261 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
265 /*-------------------------------------------------------------------------*/
268 #define CT_FUNC_HID_IDX 0
270 static struct usb_string ct_func_string_defs[] = {
271 [CT_FUNC_HID_IDX].s = "HID Interface",
272 {}, /* end of list */
275 static struct usb_gadget_strings ct_func_string_table = {
276 .language = 0x0409, /* en-US */
277 .strings = ct_func_string_defs,
280 static struct usb_gadget_strings *ct_func_strings[] = {
281 &ct_func_string_table,
285 /*-------------------------------------------------------------------------*/
288 static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer,
289 size_t count, loff_t *ptr)
291 struct f_hidg *hidg = file->private_data;
292 struct f_hidg_req_list *list;
293 struct usb_request *req;
300 if (!access_ok(VERIFY_WRITE, buffer, count))
303 spin_lock_irqsave(&hidg->read_spinlock, flags);
305 #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
307 /* wait for at least one buffer to complete */
308 while (!READ_COND_INTOUT) {
309 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
310 if (file->f_flags & O_NONBLOCK)
313 if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT))
316 spin_lock_irqsave(&hidg->read_spinlock, flags);
319 /* pick the first one */
320 list = list_first_entry(&hidg->completed_out_req,
321 struct f_hidg_req_list, list);
324 * Remove this from list to protect it from beign free()
325 * while host disables our function
327 list_del(&list->list);
330 count = min_t(unsigned int, count, req->actual - list->pos);
331 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
333 /* copy to user outside spinlock */
334 count -= copy_to_user(buffer, req->buf + list->pos, count);
338 * if this request is completely handled and transfered to
339 * userspace, remove its entry from the list and requeue it
340 * again. Otherwise, we will revisit it again upon the next
341 * call, taking into account its current read position.
343 if (list->pos == req->actual) {
346 req->length = hidg->report_length;
347 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
349 free_ep_req(hidg->out_ep, req);
353 spin_lock_irqsave(&hidg->read_spinlock, flags);
354 list_add(&list->list, &hidg->completed_out_req);
355 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
357 wake_up(&hidg->read_queue);
363 #define READ_COND_SSREPORT (hidg->set_report_buf != NULL)
365 static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer,
366 size_t count, loff_t *ptr)
368 struct f_hidg *hidg = file->private_data;
369 char *tmp_buf = NULL;
375 spin_lock_irqsave(&hidg->read_spinlock, flags);
377 while (!READ_COND_SSREPORT) {
378 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
379 if (file->f_flags & O_NONBLOCK)
382 if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT))
385 spin_lock_irqsave(&hidg->read_spinlock, flags);
388 count = min_t(unsigned int, count, hidg->set_report_length);
389 tmp_buf = hidg->set_report_buf;
390 hidg->set_report_buf = NULL;
392 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
394 if (tmp_buf != NULL) {
395 count -= copy_to_user(buffer, tmp_buf, count);
401 wake_up(&hidg->read_queue);
406 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
407 size_t count, loff_t *ptr)
409 struct f_hidg *hidg = file->private_data;
411 if (hidg->use_out_ep)
412 return f_hidg_intout_read(file, buffer, count, ptr);
414 return f_hidg_ssreport_read(file, buffer, count, ptr);
417 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
419 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
422 if (req->status != 0) {
423 ERROR(hidg->func.config->cdev,
424 "End Point Request ERROR: %d\n", req->status);
427 spin_lock_irqsave(&hidg->write_spinlock, flags);
428 hidg->write_pending = 0;
429 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
430 wake_up(&hidg->write_queue);
433 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
434 size_t count, loff_t *offp)
436 struct f_hidg *hidg = file->private_data;
437 struct usb_request *req;
439 ssize_t status = -ENOMEM;
441 if (!access_ok(VERIFY_READ, buffer, count))
444 spin_lock_irqsave(&hidg->write_spinlock, flags);
447 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
451 #define WRITE_COND (!hidg->write_pending)
454 while (!WRITE_COND) {
455 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
456 if (file->f_flags & O_NONBLOCK)
459 if (wait_event_interruptible_exclusive(
460 hidg->write_queue, WRITE_COND))
463 spin_lock_irqsave(&hidg->write_spinlock, flags);
466 hidg->write_pending = 1;
468 count = min_t(unsigned, count, hidg->report_length);
470 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
473 ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
475 goto release_write_pending;
478 status = copy_from_user(req->buf, buffer, count);
480 ERROR(hidg->func.config->cdev,
481 "copy_from_user error\n");
483 goto release_write_pending;
486 spin_lock_irqsave(&hidg->write_spinlock, flags);
488 /* when our function has been disabled by host */
490 free_ep_req(hidg->in_ep, req);
493 * Should we fail with error here?
501 req->complete = f_hidg_req_complete;
504 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
506 if (!hidg->in_ep->enabled) {
507 ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
509 goto release_write_pending;
512 status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
514 goto release_write_pending;
519 release_write_pending:
520 spin_lock_irqsave(&hidg->write_spinlock, flags);
521 hidg->write_pending = 0;
522 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
524 wake_up(&hidg->write_queue);
529 static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
531 struct f_hidg *hidg = file->private_data;
534 poll_wait(file, &hidg->read_queue, wait);
535 poll_wait(file, &hidg->write_queue, wait);
538 ret |= EPOLLOUT | EPOLLWRNORM;
540 if (hidg->use_out_ep) {
541 if (READ_COND_INTOUT)
542 ret |= EPOLLIN | EPOLLRDNORM;
544 if (READ_COND_SSREPORT)
545 ret |= EPOLLIN | EPOLLRDNORM;
552 #undef READ_COND_SSREPORT
553 #undef READ_COND_INTOUT
555 static int f_hidg_release(struct inode *inode, struct file *fd)
557 fd->private_data = NULL;
561 static int f_hidg_open(struct inode *inode, struct file *fd)
563 struct f_hidg *hidg =
564 container_of(inode->i_cdev, struct f_hidg, cdev);
566 fd->private_data = hidg;
571 /*-------------------------------------------------------------------------*/
574 static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
577 return alloc_ep_req(ep, length);
580 static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req)
582 struct f_hidg *hidg = (struct f_hidg *) req->context;
583 struct usb_composite_dev *cdev = hidg->func.config->cdev;
584 struct f_hidg_req_list *req_list;
587 switch (req->status) {
589 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
591 ERROR(cdev, "Unable to allocate mem for req_list\n");
597 spin_lock_irqsave(&hidg->read_spinlock, flags);
598 list_add_tail(&req_list->list, &hidg->completed_out_req);
599 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
601 wake_up(&hidg->read_queue);
604 ERROR(cdev, "Set report failed %d\n", req->status);
606 case -ECONNABORTED: /* hardware forced ep reset */
607 case -ECONNRESET: /* request dequeued */
608 case -ESHUTDOWN: /* disconnect from host */
610 free_ep_req(ep, req);
615 static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req)
617 struct f_hidg *hidg = (struct f_hidg *)req->context;
618 struct usb_composite_dev *cdev = hidg->func.config->cdev;
619 char *new_buf = NULL;
622 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
624 "%s FAILED: status=%d, buf=%p, actual=%d\n",
625 __func__, req->status, req->buf, req->actual);
629 spin_lock_irqsave(&hidg->read_spinlock, flags);
631 new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC);
632 if (new_buf == NULL) {
633 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
636 hidg->set_report_buf = new_buf;
638 hidg->set_report_length = req->actual;
639 memcpy(hidg->set_report_buf, req->buf, req->actual);
641 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
643 wake_up(&hidg->read_queue);
646 static int hidg_setup(struct usb_function *f,
647 const struct usb_ctrlrequest *ctrl)
649 struct f_hidg *hidg = func_to_hidg(f);
650 struct usb_composite_dev *cdev = f->config->cdev;
651 struct usb_request *req = cdev->req;
655 value = __le16_to_cpu(ctrl->wValue);
656 length = __le16_to_cpu(ctrl->wLength);
659 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
660 __func__, ctrl->bRequestType, ctrl->bRequest, value);
662 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
663 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
664 | HID_REQ_GET_REPORT):
665 VDBG(cdev, "get_report\n");
667 /* send an empty report */
668 length = min_t(unsigned, length, hidg->report_length);
669 memset(req->buf, 0x0, length);
674 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
675 | HID_REQ_GET_PROTOCOL):
676 VDBG(cdev, "get_protocol\n");
677 length = min_t(unsigned int, length, 1);
678 ((u8 *) req->buf)[0] = hidg->protocol;
682 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
684 VDBG(cdev, "get_idle\n");
685 length = min_t(unsigned int, length, 1);
686 ((u8 *) req->buf)[0] = hidg->idle;
690 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
691 | HID_REQ_SET_REPORT):
692 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
693 if (hidg->use_out_ep)
695 req->complete = hidg_ssreport_complete;
700 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
701 | HID_REQ_SET_PROTOCOL):
702 VDBG(cdev, "set_protocol\n");
703 if (value > HID_REPORT_PROTOCOL)
707 * We assume that programs implementing the Boot protocol
708 * are also compatible with the Report Protocol
710 if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
711 hidg->protocol = value;
717 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
719 VDBG(cdev, "set_idle\n");
721 hidg->idle = value >> 8;
725 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
726 | USB_REQ_GET_DESCRIPTOR):
727 switch (value >> 8) {
730 struct hid_descriptor hidg_desc_copy = hidg_desc;
732 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
733 hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
734 hidg_desc_copy.desc[0].wDescriptorLength =
735 cpu_to_le16(hidg->report_desc_length);
737 length = min_t(unsigned short, length,
738 hidg_desc_copy.bLength);
739 memcpy(req->buf, &hidg_desc_copy, length);
744 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
745 length = min_t(unsigned short, length,
746 hidg->report_desc_length);
747 memcpy(req->buf, hidg->report_desc, length);
752 VDBG(cdev, "Unknown descriptor request 0x%x\n",
760 VDBG(cdev, "Unknown request 0x%x\n",
771 req->length = length;
772 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
774 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
778 static void hidg_disable(struct usb_function *f)
780 struct f_hidg *hidg = func_to_hidg(f);
781 struct f_hidg_req_list *list, *next;
784 usb_ep_disable(hidg->in_ep);
787 usb_ep_disable(hidg->out_ep);
789 spin_lock_irqsave(&hidg->read_spinlock, flags);
790 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
791 free_ep_req(hidg->out_ep, list->req);
792 list_del(&list->list);
795 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
798 spin_lock_irqsave(&hidg->write_spinlock, flags);
799 if (!hidg->write_pending) {
800 free_ep_req(hidg->in_ep, hidg->req);
801 hidg->write_pending = 1;
805 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
808 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
810 struct usb_composite_dev *cdev = f->config->cdev;
811 struct f_hidg *hidg = func_to_hidg(f);
812 struct usb_request *req_in = NULL;
816 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
818 if (hidg->in_ep != NULL) {
819 /* restart endpoint */
820 usb_ep_disable(hidg->in_ep);
822 status = config_ep_by_speed(f->config->cdev->gadget, f,
825 ERROR(cdev, "config_ep_by_speed FAILED!\n");
828 status = usb_ep_enable(hidg->in_ep);
830 ERROR(cdev, "Enable IN endpoint FAILED!\n");
833 hidg->in_ep->driver_data = hidg;
835 req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
842 if (hidg->use_out_ep && hidg->out_ep != NULL) {
843 /* restart endpoint */
844 usb_ep_disable(hidg->out_ep);
846 status = config_ep_by_speed(f->config->cdev->gadget, f,
849 ERROR(cdev, "config_ep_by_speed FAILED!\n");
852 status = usb_ep_enable(hidg->out_ep);
854 ERROR(cdev, "Enable OUT endpoint FAILED!\n");
857 hidg->out_ep->driver_data = hidg;
860 * allocate a bunch of read buffers and queue them all at once.
862 for (i = 0; i < hidg->qlen && status == 0; i++) {
863 struct usb_request *req =
864 hidg_alloc_ep_req(hidg->out_ep,
865 hidg->report_length);
867 req->complete = hidg_intout_complete;
869 status = usb_ep_queue(hidg->out_ep, req,
872 ERROR(cdev, "%s queue req --> %d\n",
873 hidg->out_ep->name, status);
874 free_ep_req(hidg->out_ep, req);
883 if (hidg->in_ep != NULL) {
884 spin_lock_irqsave(&hidg->write_spinlock, flags);
886 hidg->write_pending = 0;
887 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
889 wake_up(&hidg->write_queue);
894 usb_ep_disable(hidg->out_ep);
897 free_ep_req(hidg->in_ep, req_in);
901 usb_ep_disable(hidg->in_ep);
907 static const struct file_operations f_hidg_fops = {
908 .owner = THIS_MODULE,
910 .release = f_hidg_release,
911 .write = f_hidg_write,
914 .llseek = noop_llseek,
917 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
920 struct f_hidg *hidg = func_to_hidg(f);
921 struct usb_string *us;
924 /* maybe allocate device-global string IDs, and patch descriptors */
925 us = usb_gstrings_attach(c->cdev, ct_func_strings,
926 ARRAY_SIZE(ct_func_string_defs));
929 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
931 /* allocate instance-specific interface IDs, and patch descriptors */
932 status = usb_interface_id(c, f);
935 hidg_interface_desc.bInterfaceNumber = status;
937 /* allocate instance-specific endpoints */
939 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
945 if (hidg->use_out_ep) {
946 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
952 /* used only if use_out_ep == 1 */
953 hidg->set_report_buf = NULL;
955 /* set descriptor dynamic values */
956 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
957 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
958 hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1;
959 hidg->protocol = HID_REPORT_PROTOCOL;
961 hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
962 hidg_ss_in_comp_desc.wBytesPerInterval =
963 cpu_to_le16(hidg->report_length);
964 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
965 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
966 hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
967 hidg_ss_out_comp_desc.wBytesPerInterval =
968 cpu_to_le16(hidg->report_length);
969 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
970 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
972 * We can use hidg_desc struct here but we should not relay
973 * that its content won't change after returning from this function.
975 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
976 hidg_desc.desc[0].wDescriptorLength =
977 cpu_to_le16(hidg->report_desc_length);
979 hidg_hs_in_ep_desc.bEndpointAddress =
980 hidg_fs_in_ep_desc.bEndpointAddress;
981 hidg_hs_out_ep_desc.bEndpointAddress =
982 hidg_fs_out_ep_desc.bEndpointAddress;
984 hidg_ss_in_ep_desc.bEndpointAddress =
985 hidg_fs_in_ep_desc.bEndpointAddress;
986 hidg_ss_out_ep_desc.bEndpointAddress =
987 hidg_fs_out_ep_desc.bEndpointAddress;
989 if (hidg->use_out_ep)
990 status = usb_assign_descriptors(f,
991 hidg_fs_descriptors_intout,
992 hidg_hs_descriptors_intout,
993 hidg_ss_descriptors_intout,
994 hidg_ss_descriptors_intout);
996 status = usb_assign_descriptors(f,
997 hidg_fs_descriptors_ssreport,
998 hidg_hs_descriptors_ssreport,
999 hidg_ss_descriptors_ssreport,
1000 hidg_ss_descriptors_ssreport);
1005 spin_lock_init(&hidg->write_spinlock);
1006 hidg->write_pending = 1;
1008 spin_lock_init(&hidg->read_spinlock);
1009 init_waitqueue_head(&hidg->write_queue);
1010 init_waitqueue_head(&hidg->read_queue);
1011 INIT_LIST_HEAD(&hidg->completed_out_req);
1013 /* create char device */
1014 cdev_init(&hidg->cdev, &f_hidg_fops);
1015 status = cdev_device_add(&hidg->cdev, &hidg->dev);
1017 goto fail_free_descs;
1021 usb_free_all_descriptors(f);
1023 ERROR(f->config->cdev, "hidg_bind FAILED\n");
1024 if (hidg->req != NULL)
1025 free_ep_req(hidg->in_ep, hidg->req);
1030 static inline int hidg_get_minor(void)
1034 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
1035 if (ret >= HIDG_MINORS) {
1036 ida_simple_remove(&hidg_ida, ret);
1043 static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
1045 return container_of(to_config_group(item), struct f_hid_opts,
1049 static void hid_attr_release(struct config_item *item)
1051 struct f_hid_opts *opts = to_f_hid_opts(item);
1053 usb_put_function_instance(&opts->func_inst);
1056 static struct configfs_item_operations hidg_item_ops = {
1057 .release = hid_attr_release,
1060 #define F_HID_OPT(name, prec, limit) \
1061 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
1063 struct f_hid_opts *opts = to_f_hid_opts(item); \
1066 mutex_lock(&opts->lock); \
1067 result = sprintf(page, "%d\n", opts->name); \
1068 mutex_unlock(&opts->lock); \
1073 static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
1074 const char *page, size_t len) \
1076 struct f_hid_opts *opts = to_f_hid_opts(item); \
1080 mutex_lock(&opts->lock); \
1081 if (opts->refcnt) { \
1086 ret = kstrtou##prec(page, 0, &num); \
1090 if (num > limit) { \
1098 mutex_unlock(&opts->lock); \
1102 CONFIGFS_ATTR(f_hid_opts_, name)
1104 F_HID_OPT(subclass, 8, 255);
1105 F_HID_OPT(protocol, 8, 255);
1106 F_HID_OPT(no_out_endpoint, 8, 1);
1107 F_HID_OPT(report_length, 16, 65535);
1109 static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
1111 struct f_hid_opts *opts = to_f_hid_opts(item);
1114 mutex_lock(&opts->lock);
1115 result = opts->report_desc_length;
1116 memcpy(page, opts->report_desc, opts->report_desc_length);
1117 mutex_unlock(&opts->lock);
1122 static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
1123 const char *page, size_t len)
1125 struct f_hid_opts *opts = to_f_hid_opts(item);
1129 mutex_lock(&opts->lock);
1133 if (len > PAGE_SIZE) {
1137 d = kmemdup(page, len, GFP_KERNEL);
1142 kfree(opts->report_desc);
1143 opts->report_desc = d;
1144 opts->report_desc_length = len;
1145 opts->report_desc_alloc = true;
1148 mutex_unlock(&opts->lock);
1152 CONFIGFS_ATTR(f_hid_opts_, report_desc);
1154 static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
1156 struct f_hid_opts *opts = to_f_hid_opts(item);
1158 return sprintf(page, "%d:%d\n", major, opts->minor);
1161 CONFIGFS_ATTR_RO(f_hid_opts_, dev);
1163 static struct configfs_attribute *hid_attrs[] = {
1164 &f_hid_opts_attr_subclass,
1165 &f_hid_opts_attr_protocol,
1166 &f_hid_opts_attr_no_out_endpoint,
1167 &f_hid_opts_attr_report_length,
1168 &f_hid_opts_attr_report_desc,
1169 &f_hid_opts_attr_dev,
1173 static const struct config_item_type hid_func_type = {
1174 .ct_item_ops = &hidg_item_ops,
1175 .ct_attrs = hid_attrs,
1176 .ct_owner = THIS_MODULE,
1179 static inline void hidg_put_minor(int minor)
1181 ida_simple_remove(&hidg_ida, minor);
1184 static void hidg_free_inst(struct usb_function_instance *f)
1186 struct f_hid_opts *opts;
1188 opts = container_of(f, struct f_hid_opts, func_inst);
1190 mutex_lock(&hidg_ida_lock);
1192 hidg_put_minor(opts->minor);
1193 if (ida_is_empty(&hidg_ida))
1196 mutex_unlock(&hidg_ida_lock);
1198 if (opts->report_desc_alloc)
1199 kfree(opts->report_desc);
1204 static struct usb_function_instance *hidg_alloc_inst(void)
1206 struct f_hid_opts *opts;
1207 struct usb_function_instance *ret;
1210 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1212 return ERR_PTR(-ENOMEM);
1213 mutex_init(&opts->lock);
1214 opts->func_inst.free_func_inst = hidg_free_inst;
1215 ret = &opts->func_inst;
1217 mutex_lock(&hidg_ida_lock);
1219 if (ida_is_empty(&hidg_ida)) {
1220 status = ghid_setup(NULL, HIDG_MINORS);
1222 ret = ERR_PTR(status);
1228 opts->minor = hidg_get_minor();
1229 if (opts->minor < 0) {
1230 ret = ERR_PTR(opts->minor);
1232 if (ida_is_empty(&hidg_ida))
1236 config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1239 mutex_unlock(&hidg_ida_lock);
1243 static void hidg_free(struct usb_function *f)
1245 struct f_hidg *hidg;
1246 struct f_hid_opts *opts;
1248 hidg = func_to_hidg(f);
1249 opts = container_of(f->fi, struct f_hid_opts, func_inst);
1250 put_device(&hidg->dev);
1251 mutex_lock(&opts->lock);
1253 mutex_unlock(&opts->lock);
1256 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1258 struct f_hidg *hidg = func_to_hidg(f);
1260 cdev_device_del(&hidg->cdev, &hidg->dev);
1262 usb_free_all_descriptors(f);
1265 static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
1267 struct f_hidg *hidg;
1268 struct f_hid_opts *opts;
1271 /* allocate and initialize one new instance */
1272 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
1274 return ERR_PTR(-ENOMEM);
1276 opts = container_of(fi, struct f_hid_opts, func_inst);
1278 mutex_lock(&opts->lock);
1281 device_initialize(&hidg->dev);
1282 hidg->dev.release = hidg_release;
1283 hidg->dev.class = hidg_class;
1284 hidg->dev.devt = MKDEV(major, opts->minor);
1285 ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1288 mutex_unlock(&opts->lock);
1289 return ERR_PTR(ret);
1292 hidg->bInterfaceSubClass = opts->subclass;
1293 hidg->bInterfaceProtocol = opts->protocol;
1294 hidg->report_length = opts->report_length;
1295 hidg->report_desc_length = opts->report_desc_length;
1296 if (opts->report_desc) {
1297 hidg->report_desc = kmemdup(opts->report_desc,
1298 opts->report_desc_length,
1300 if (!hidg->report_desc) {
1301 put_device(&hidg->dev);
1303 mutex_unlock(&opts->lock);
1304 return ERR_PTR(-ENOMEM);
1307 hidg->use_out_ep = !opts->no_out_endpoint;
1309 mutex_unlock(&opts->lock);
1311 hidg->func.name = "hid";
1312 hidg->func.bind = hidg_bind;
1313 hidg->func.unbind = hidg_unbind;
1314 hidg->func.set_alt = hidg_set_alt;
1315 hidg->func.disable = hidg_disable;
1316 hidg->func.setup = hidg_setup;
1317 hidg->func.free_func = hidg_free;
1319 /* this could me made configurable at some point */
1325 DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1326 MODULE_LICENSE("GPL");
1327 MODULE_AUTHOR("Fabien Chouteau");
1329 int ghid_setup(struct usb_gadget *g, int count)
1334 hidg_class = class_create(THIS_MODULE, "hidg");
1335 if (IS_ERR(hidg_class)) {
1336 status = PTR_ERR(hidg_class);
1341 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1343 class_destroy(hidg_class);
1354 void ghid_cleanup(void)
1357 unregister_chrdev_region(MKDEV(major, 0), minors);
1361 class_destroy(hidg_class);