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->set_report_buf);
95 /*-------------------------------------------------------------------------*/
96 /* Static descriptors */
98 static struct usb_interface_descriptor hidg_interface_desc = {
99 .bLength = sizeof hidg_interface_desc,
100 .bDescriptorType = USB_DT_INTERFACE,
101 /* .bInterfaceNumber = DYNAMIC */
102 .bAlternateSetting = 0,
103 /* .bNumEndpoints = DYNAMIC (depends on use_out_ep) */
104 .bInterfaceClass = USB_CLASS_HID,
105 /* .bInterfaceSubClass = DYNAMIC */
106 /* .bInterfaceProtocol = DYNAMIC */
107 /* .iInterface = DYNAMIC */
110 static struct hid_descriptor hidg_desc = {
111 .bLength = sizeof hidg_desc,
112 .bDescriptorType = HID_DT_HID,
113 .bcdHID = cpu_to_le16(0x0101),
114 .bCountryCode = 0x00,
115 .bNumDescriptors = 0x1,
116 /*.desc[0].bDescriptorType = DYNAMIC */
117 /*.desc[0].wDescriptorLenght = DYNAMIC */
120 /* Super-Speed Support */
122 static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
123 .bLength = USB_DT_ENDPOINT_SIZE,
124 .bDescriptorType = USB_DT_ENDPOINT,
125 .bEndpointAddress = USB_DIR_IN,
126 .bmAttributes = USB_ENDPOINT_XFER_INT,
127 /*.wMaxPacketSize = DYNAMIC */
128 .bInterval = 4, /* FIXME: Add this field in the
129 * HID gadget configuration?
130 * (struct hidg_func_descriptor)
134 static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
135 .bLength = sizeof(hidg_ss_in_comp_desc),
136 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
138 /* .bMaxBurst = 0, */
139 /* .bmAttributes = 0, */
140 /* .wBytesPerInterval = DYNAMIC */
143 static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
144 .bLength = USB_DT_ENDPOINT_SIZE,
145 .bDescriptorType = USB_DT_ENDPOINT,
146 .bEndpointAddress = USB_DIR_OUT,
147 .bmAttributes = USB_ENDPOINT_XFER_INT,
148 /*.wMaxPacketSize = DYNAMIC */
149 .bInterval = 4, /* FIXME: Add this field in the
150 * HID gadget configuration?
151 * (struct hidg_func_descriptor)
155 static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
156 .bLength = sizeof(hidg_ss_out_comp_desc),
157 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
159 /* .bMaxBurst = 0, */
160 /* .bmAttributes = 0, */
161 /* .wBytesPerInterval = DYNAMIC */
164 static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = {
165 (struct usb_descriptor_header *)&hidg_interface_desc,
166 (struct usb_descriptor_header *)&hidg_desc,
167 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
168 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
169 (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
170 (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
174 static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = {
175 (struct usb_descriptor_header *)&hidg_interface_desc,
176 (struct usb_descriptor_header *)&hidg_desc,
177 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
178 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
182 /* High-Speed Support */
184 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187 .bEndpointAddress = USB_DIR_IN,
188 .bmAttributes = USB_ENDPOINT_XFER_INT,
189 /*.wMaxPacketSize = DYNAMIC */
190 .bInterval = 4, /* FIXME: Add this field in the
191 * HID gadget configuration?
192 * (struct hidg_func_descriptor)
196 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
197 .bLength = USB_DT_ENDPOINT_SIZE,
198 .bDescriptorType = USB_DT_ENDPOINT,
199 .bEndpointAddress = USB_DIR_OUT,
200 .bmAttributes = USB_ENDPOINT_XFER_INT,
201 /*.wMaxPacketSize = DYNAMIC */
202 .bInterval = 4, /* FIXME: Add this field in the
203 * HID gadget configuration?
204 * (struct hidg_func_descriptor)
208 static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = {
209 (struct usb_descriptor_header *)&hidg_interface_desc,
210 (struct usb_descriptor_header *)&hidg_desc,
211 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
212 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
216 static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = {
217 (struct usb_descriptor_header *)&hidg_interface_desc,
218 (struct usb_descriptor_header *)&hidg_desc,
219 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
223 /* Full-Speed Support */
225 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
226 .bLength = USB_DT_ENDPOINT_SIZE,
227 .bDescriptorType = USB_DT_ENDPOINT,
228 .bEndpointAddress = USB_DIR_IN,
229 .bmAttributes = USB_ENDPOINT_XFER_INT,
230 /*.wMaxPacketSize = DYNAMIC */
231 .bInterval = 10, /* FIXME: Add this field in the
232 * HID gadget configuration?
233 * (struct hidg_func_descriptor)
237 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
238 .bLength = USB_DT_ENDPOINT_SIZE,
239 .bDescriptorType = USB_DT_ENDPOINT,
240 .bEndpointAddress = USB_DIR_OUT,
241 .bmAttributes = USB_ENDPOINT_XFER_INT,
242 /*.wMaxPacketSize = DYNAMIC */
243 .bInterval = 10, /* FIXME: Add this field in the
244 * HID gadget configuration?
245 * (struct hidg_func_descriptor)
249 static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = {
250 (struct usb_descriptor_header *)&hidg_interface_desc,
251 (struct usb_descriptor_header *)&hidg_desc,
252 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
253 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
257 static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = {
258 (struct usb_descriptor_header *)&hidg_interface_desc,
259 (struct usb_descriptor_header *)&hidg_desc,
260 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
264 /*-------------------------------------------------------------------------*/
267 #define CT_FUNC_HID_IDX 0
269 static struct usb_string ct_func_string_defs[] = {
270 [CT_FUNC_HID_IDX].s = "HID Interface",
271 {}, /* end of list */
274 static struct usb_gadget_strings ct_func_string_table = {
275 .language = 0x0409, /* en-US */
276 .strings = ct_func_string_defs,
279 static struct usb_gadget_strings *ct_func_strings[] = {
280 &ct_func_string_table,
284 /*-------------------------------------------------------------------------*/
287 static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer,
288 size_t count, loff_t *ptr)
290 struct f_hidg *hidg = file->private_data;
291 struct f_hidg_req_list *list;
292 struct usb_request *req;
299 if (!access_ok(VERIFY_WRITE, buffer, count))
302 spin_lock_irqsave(&hidg->read_spinlock, flags);
304 #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
306 /* wait for at least one buffer to complete */
307 while (!READ_COND_INTOUT) {
308 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
309 if (file->f_flags & O_NONBLOCK)
312 if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT))
315 spin_lock_irqsave(&hidg->read_spinlock, flags);
318 /* pick the first one */
319 list = list_first_entry(&hidg->completed_out_req,
320 struct f_hidg_req_list, list);
323 * Remove this from list to protect it from beign free()
324 * while host disables our function
326 list_del(&list->list);
329 count = min_t(unsigned int, count, req->actual - list->pos);
330 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
332 /* copy to user outside spinlock */
333 count -= copy_to_user(buffer, req->buf + list->pos, count);
337 * if this request is completely handled and transfered to
338 * userspace, remove its entry from the list and requeue it
339 * again. Otherwise, we will revisit it again upon the next
340 * call, taking into account its current read position.
342 if (list->pos == req->actual) {
345 req->length = hidg->report_length;
346 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
348 free_ep_req(hidg->out_ep, req);
352 spin_lock_irqsave(&hidg->read_spinlock, flags);
353 list_add(&list->list, &hidg->completed_out_req);
354 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
356 wake_up(&hidg->read_queue);
362 #define READ_COND_SSREPORT (hidg->set_report_buf != NULL)
364 static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer,
365 size_t count, loff_t *ptr)
367 struct f_hidg *hidg = file->private_data;
368 char *tmp_buf = NULL;
374 spin_lock_irqsave(&hidg->read_spinlock, flags);
376 while (!READ_COND_SSREPORT) {
377 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
378 if (file->f_flags & O_NONBLOCK)
381 if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT))
384 spin_lock_irqsave(&hidg->read_spinlock, flags);
387 count = min_t(unsigned int, count, hidg->set_report_length);
388 tmp_buf = hidg->set_report_buf;
389 hidg->set_report_buf = NULL;
391 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
393 if (tmp_buf != NULL) {
394 count -= copy_to_user(buffer, tmp_buf, count);
400 wake_up(&hidg->read_queue);
405 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
406 size_t count, loff_t *ptr)
408 struct f_hidg *hidg = file->private_data;
410 if (hidg->use_out_ep)
411 return f_hidg_intout_read(file, buffer, count, ptr);
413 return f_hidg_ssreport_read(file, buffer, count, ptr);
416 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
418 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
421 if (req->status != 0) {
422 ERROR(hidg->func.config->cdev,
423 "End Point Request ERROR: %d\n", req->status);
426 spin_lock_irqsave(&hidg->write_spinlock, flags);
427 hidg->write_pending = 0;
428 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
429 wake_up(&hidg->write_queue);
432 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
433 size_t count, loff_t *offp)
435 struct f_hidg *hidg = file->private_data;
436 struct usb_request *req;
438 ssize_t status = -ENOMEM;
440 if (!access_ok(VERIFY_READ, buffer, count))
443 spin_lock_irqsave(&hidg->write_spinlock, flags);
446 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
450 #define WRITE_COND (!hidg->write_pending)
453 while (!WRITE_COND) {
454 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
455 if (file->f_flags & O_NONBLOCK)
458 if (wait_event_interruptible_exclusive(
459 hidg->write_queue, WRITE_COND))
462 spin_lock_irqsave(&hidg->write_spinlock, flags);
465 hidg->write_pending = 1;
467 count = min_t(unsigned, count, hidg->report_length);
469 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
472 ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
474 goto release_write_pending;
477 status = copy_from_user(req->buf, buffer, count);
479 ERROR(hidg->func.config->cdev,
480 "copy_from_user error\n");
482 goto release_write_pending;
485 spin_lock_irqsave(&hidg->write_spinlock, flags);
487 /* when our function has been disabled by host */
489 free_ep_req(hidg->in_ep, req);
492 * Should we fail with error here?
500 req->complete = f_hidg_req_complete;
503 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
505 if (!hidg->in_ep->enabled) {
506 ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
508 goto release_write_pending;
511 status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
513 goto release_write_pending;
518 release_write_pending:
519 spin_lock_irqsave(&hidg->write_spinlock, flags);
520 hidg->write_pending = 0;
521 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
523 wake_up(&hidg->write_queue);
528 static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
530 struct f_hidg *hidg = file->private_data;
533 poll_wait(file, &hidg->read_queue, wait);
534 poll_wait(file, &hidg->write_queue, wait);
537 ret |= EPOLLOUT | EPOLLWRNORM;
539 if (hidg->use_out_ep) {
540 if (READ_COND_INTOUT)
541 ret |= EPOLLIN | EPOLLRDNORM;
543 if (READ_COND_SSREPORT)
544 ret |= EPOLLIN | EPOLLRDNORM;
551 #undef READ_COND_SSREPORT
552 #undef READ_COND_INTOUT
554 static int f_hidg_release(struct inode *inode, struct file *fd)
556 fd->private_data = NULL;
560 static int f_hidg_open(struct inode *inode, struct file *fd)
562 struct f_hidg *hidg =
563 container_of(inode->i_cdev, struct f_hidg, cdev);
565 fd->private_data = hidg;
570 /*-------------------------------------------------------------------------*/
573 static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
576 return alloc_ep_req(ep, length);
579 static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req)
581 struct f_hidg *hidg = (struct f_hidg *) req->context;
582 struct usb_composite_dev *cdev = hidg->func.config->cdev;
583 struct f_hidg_req_list *req_list;
586 switch (req->status) {
588 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
590 ERROR(cdev, "Unable to allocate mem for req_list\n");
596 spin_lock_irqsave(&hidg->read_spinlock, flags);
597 list_add_tail(&req_list->list, &hidg->completed_out_req);
598 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
600 wake_up(&hidg->read_queue);
603 ERROR(cdev, "Set report failed %d\n", req->status);
605 case -ECONNABORTED: /* hardware forced ep reset */
606 case -ECONNRESET: /* request dequeued */
607 case -ESHUTDOWN: /* disconnect from host */
609 free_ep_req(ep, req);
614 static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req)
616 struct f_hidg *hidg = (struct f_hidg *)req->context;
617 struct usb_composite_dev *cdev = hidg->func.config->cdev;
618 char *new_buf = NULL;
621 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
623 "%s FAILED: status=%d, buf=%p, actual=%d\n",
624 __func__, req->status, req->buf, req->actual);
628 spin_lock_irqsave(&hidg->read_spinlock, flags);
630 new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC);
631 if (new_buf == NULL) {
632 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
635 hidg->set_report_buf = new_buf;
637 hidg->set_report_length = req->actual;
638 memcpy(hidg->set_report_buf, req->buf, req->actual);
640 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
642 wake_up(&hidg->read_queue);
645 static int hidg_setup(struct usb_function *f,
646 const struct usb_ctrlrequest *ctrl)
648 struct f_hidg *hidg = func_to_hidg(f);
649 struct usb_composite_dev *cdev = f->config->cdev;
650 struct usb_request *req = cdev->req;
654 value = __le16_to_cpu(ctrl->wValue);
655 length = __le16_to_cpu(ctrl->wLength);
658 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
659 __func__, ctrl->bRequestType, ctrl->bRequest, value);
661 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
662 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
663 | HID_REQ_GET_REPORT):
664 VDBG(cdev, "get_report\n");
666 /* send an empty report */
667 length = min_t(unsigned, length, hidg->report_length);
668 memset(req->buf, 0x0, length);
673 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
674 | HID_REQ_GET_PROTOCOL):
675 VDBG(cdev, "get_protocol\n");
676 length = min_t(unsigned int, length, 1);
677 ((u8 *) req->buf)[0] = hidg->protocol;
681 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
683 VDBG(cdev, "get_idle\n");
684 length = min_t(unsigned int, length, 1);
685 ((u8 *) req->buf)[0] = hidg->idle;
689 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
690 | HID_REQ_SET_REPORT):
691 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
692 if (hidg->use_out_ep)
694 req->complete = hidg_ssreport_complete;
699 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
700 | HID_REQ_SET_PROTOCOL):
701 VDBG(cdev, "set_protocol\n");
702 if (value > HID_REPORT_PROTOCOL)
706 * We assume that programs implementing the Boot protocol
707 * are also compatible with the Report Protocol
709 if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
710 hidg->protocol = value;
716 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
718 VDBG(cdev, "set_idle\n");
720 hidg->idle = value >> 8;
724 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
725 | USB_REQ_GET_DESCRIPTOR):
726 switch (value >> 8) {
729 struct hid_descriptor hidg_desc_copy = hidg_desc;
731 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
732 hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
733 hidg_desc_copy.desc[0].wDescriptorLength =
734 cpu_to_le16(hidg->report_desc_length);
736 length = min_t(unsigned short, length,
737 hidg_desc_copy.bLength);
738 memcpy(req->buf, &hidg_desc_copy, length);
743 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
744 length = min_t(unsigned short, length,
745 hidg->report_desc_length);
746 memcpy(req->buf, hidg->report_desc, length);
751 VDBG(cdev, "Unknown descriptor request 0x%x\n",
759 VDBG(cdev, "Unknown request 0x%x\n",
770 req->length = length;
771 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
773 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
777 static void hidg_disable(struct usb_function *f)
779 struct f_hidg *hidg = func_to_hidg(f);
780 struct f_hidg_req_list *list, *next;
783 usb_ep_disable(hidg->in_ep);
786 usb_ep_disable(hidg->out_ep);
788 spin_lock_irqsave(&hidg->read_spinlock, flags);
789 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
790 free_ep_req(hidg->out_ep, list->req);
791 list_del(&list->list);
794 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
797 spin_lock_irqsave(&hidg->write_spinlock, flags);
798 if (!hidg->write_pending) {
799 free_ep_req(hidg->in_ep, hidg->req);
800 hidg->write_pending = 1;
804 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
807 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
809 struct usb_composite_dev *cdev = f->config->cdev;
810 struct f_hidg *hidg = func_to_hidg(f);
811 struct usb_request *req_in = NULL;
815 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
817 if (hidg->in_ep != NULL) {
818 /* restart endpoint */
819 usb_ep_disable(hidg->in_ep);
821 status = config_ep_by_speed(f->config->cdev->gadget, f,
824 ERROR(cdev, "config_ep_by_speed FAILED!\n");
827 status = usb_ep_enable(hidg->in_ep);
829 ERROR(cdev, "Enable IN endpoint FAILED!\n");
832 hidg->in_ep->driver_data = hidg;
834 req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
841 if (hidg->use_out_ep && hidg->out_ep != NULL) {
842 /* restart endpoint */
843 usb_ep_disable(hidg->out_ep);
845 status = config_ep_by_speed(f->config->cdev->gadget, f,
848 ERROR(cdev, "config_ep_by_speed FAILED!\n");
851 status = usb_ep_enable(hidg->out_ep);
853 ERROR(cdev, "Enable OUT endpoint FAILED!\n");
856 hidg->out_ep->driver_data = hidg;
859 * allocate a bunch of read buffers and queue them all at once.
861 for (i = 0; i < hidg->qlen && status == 0; i++) {
862 struct usb_request *req =
863 hidg_alloc_ep_req(hidg->out_ep,
864 hidg->report_length);
866 req->complete = hidg_intout_complete;
868 status = usb_ep_queue(hidg->out_ep, req,
871 ERROR(cdev, "%s queue req --> %d\n",
872 hidg->out_ep->name, status);
873 free_ep_req(hidg->out_ep, req);
882 if (hidg->in_ep != NULL) {
883 spin_lock_irqsave(&hidg->write_spinlock, flags);
885 hidg->write_pending = 0;
886 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
888 wake_up(&hidg->write_queue);
893 usb_ep_disable(hidg->out_ep);
896 free_ep_req(hidg->in_ep, req_in);
900 usb_ep_disable(hidg->in_ep);
906 static const struct file_operations f_hidg_fops = {
907 .owner = THIS_MODULE,
909 .release = f_hidg_release,
910 .write = f_hidg_write,
913 .llseek = noop_llseek,
916 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
919 struct f_hidg *hidg = func_to_hidg(f);
920 struct usb_string *us;
923 /* maybe allocate device-global string IDs, and patch descriptors */
924 us = usb_gstrings_attach(c->cdev, ct_func_strings,
925 ARRAY_SIZE(ct_func_string_defs));
928 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
930 /* allocate instance-specific interface IDs, and patch descriptors */
931 status = usb_interface_id(c, f);
934 hidg_interface_desc.bInterfaceNumber = status;
936 /* allocate instance-specific endpoints */
938 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
944 if (hidg->use_out_ep) {
945 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
951 /* used only if use_out_ep == 1 */
952 hidg->set_report_buf = NULL;
954 /* set descriptor dynamic values */
955 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
956 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
957 hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1;
958 hidg->protocol = HID_REPORT_PROTOCOL;
960 hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
961 hidg_ss_in_comp_desc.wBytesPerInterval =
962 cpu_to_le16(hidg->report_length);
963 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
964 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
965 hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
966 hidg_ss_out_comp_desc.wBytesPerInterval =
967 cpu_to_le16(hidg->report_length);
968 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
969 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
971 * We can use hidg_desc struct here but we should not relay
972 * that its content won't change after returning from this function.
974 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
975 hidg_desc.desc[0].wDescriptorLength =
976 cpu_to_le16(hidg->report_desc_length);
978 hidg_hs_in_ep_desc.bEndpointAddress =
979 hidg_fs_in_ep_desc.bEndpointAddress;
980 hidg_hs_out_ep_desc.bEndpointAddress =
981 hidg_fs_out_ep_desc.bEndpointAddress;
983 hidg_ss_in_ep_desc.bEndpointAddress =
984 hidg_fs_in_ep_desc.bEndpointAddress;
985 hidg_ss_out_ep_desc.bEndpointAddress =
986 hidg_fs_out_ep_desc.bEndpointAddress;
988 if (hidg->use_out_ep)
989 status = usb_assign_descriptors(f,
990 hidg_fs_descriptors_intout,
991 hidg_hs_descriptors_intout,
992 hidg_ss_descriptors_intout,
993 hidg_ss_descriptors_intout);
995 status = usb_assign_descriptors(f,
996 hidg_fs_descriptors_ssreport,
997 hidg_hs_descriptors_ssreport,
998 hidg_ss_descriptors_ssreport,
999 hidg_ss_descriptors_ssreport);
1004 spin_lock_init(&hidg->write_spinlock);
1005 hidg->write_pending = 1;
1007 spin_lock_init(&hidg->read_spinlock);
1008 init_waitqueue_head(&hidg->write_queue);
1009 init_waitqueue_head(&hidg->read_queue);
1010 INIT_LIST_HEAD(&hidg->completed_out_req);
1012 /* create char device */
1013 cdev_init(&hidg->cdev, &f_hidg_fops);
1014 status = cdev_device_add(&hidg->cdev, &hidg->dev);
1016 goto fail_free_descs;
1020 usb_free_all_descriptors(f);
1022 ERROR(f->config->cdev, "hidg_bind FAILED\n");
1023 if (hidg->req != NULL)
1024 free_ep_req(hidg->in_ep, hidg->req);
1029 static inline int hidg_get_minor(void)
1033 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
1034 if (ret >= HIDG_MINORS) {
1035 ida_simple_remove(&hidg_ida, ret);
1042 static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
1044 return container_of(to_config_group(item), struct f_hid_opts,
1048 static void hid_attr_release(struct config_item *item)
1050 struct f_hid_opts *opts = to_f_hid_opts(item);
1052 usb_put_function_instance(&opts->func_inst);
1055 static struct configfs_item_operations hidg_item_ops = {
1056 .release = hid_attr_release,
1059 #define F_HID_OPT(name, prec, limit) \
1060 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
1062 struct f_hid_opts *opts = to_f_hid_opts(item); \
1065 mutex_lock(&opts->lock); \
1066 result = sprintf(page, "%d\n", opts->name); \
1067 mutex_unlock(&opts->lock); \
1072 static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
1073 const char *page, size_t len) \
1075 struct f_hid_opts *opts = to_f_hid_opts(item); \
1079 mutex_lock(&opts->lock); \
1080 if (opts->refcnt) { \
1085 ret = kstrtou##prec(page, 0, &num); \
1089 if (num > limit) { \
1097 mutex_unlock(&opts->lock); \
1101 CONFIGFS_ATTR(f_hid_opts_, name)
1103 F_HID_OPT(subclass, 8, 255);
1104 F_HID_OPT(protocol, 8, 255);
1105 F_HID_OPT(no_out_endpoint, 8, 1);
1106 F_HID_OPT(report_length, 16, 65535);
1108 static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
1110 struct f_hid_opts *opts = to_f_hid_opts(item);
1113 mutex_lock(&opts->lock);
1114 result = opts->report_desc_length;
1115 memcpy(page, opts->report_desc, opts->report_desc_length);
1116 mutex_unlock(&opts->lock);
1121 static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
1122 const char *page, size_t len)
1124 struct f_hid_opts *opts = to_f_hid_opts(item);
1128 mutex_lock(&opts->lock);
1132 if (len > PAGE_SIZE) {
1136 d = kmemdup(page, len, GFP_KERNEL);
1141 kfree(opts->report_desc);
1142 opts->report_desc = d;
1143 opts->report_desc_length = len;
1144 opts->report_desc_alloc = true;
1147 mutex_unlock(&opts->lock);
1151 CONFIGFS_ATTR(f_hid_opts_, report_desc);
1153 static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
1155 struct f_hid_opts *opts = to_f_hid_opts(item);
1157 return sprintf(page, "%d:%d\n", major, opts->minor);
1160 CONFIGFS_ATTR_RO(f_hid_opts_, dev);
1162 static struct configfs_attribute *hid_attrs[] = {
1163 &f_hid_opts_attr_subclass,
1164 &f_hid_opts_attr_protocol,
1165 &f_hid_opts_attr_no_out_endpoint,
1166 &f_hid_opts_attr_report_length,
1167 &f_hid_opts_attr_report_desc,
1168 &f_hid_opts_attr_dev,
1172 static const struct config_item_type hid_func_type = {
1173 .ct_item_ops = &hidg_item_ops,
1174 .ct_attrs = hid_attrs,
1175 .ct_owner = THIS_MODULE,
1178 static inline void hidg_put_minor(int minor)
1180 ida_simple_remove(&hidg_ida, minor);
1183 static void hidg_free_inst(struct usb_function_instance *f)
1185 struct f_hid_opts *opts;
1187 opts = container_of(f, struct f_hid_opts, func_inst);
1189 mutex_lock(&hidg_ida_lock);
1191 hidg_put_minor(opts->minor);
1192 if (ida_is_empty(&hidg_ida))
1195 mutex_unlock(&hidg_ida_lock);
1197 if (opts->report_desc_alloc)
1198 kfree(opts->report_desc);
1203 static struct usb_function_instance *hidg_alloc_inst(void)
1205 struct f_hid_opts *opts;
1206 struct usb_function_instance *ret;
1209 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1211 return ERR_PTR(-ENOMEM);
1212 mutex_init(&opts->lock);
1213 opts->func_inst.free_func_inst = hidg_free_inst;
1214 ret = &opts->func_inst;
1216 mutex_lock(&hidg_ida_lock);
1218 if (ida_is_empty(&hidg_ida)) {
1219 status = ghid_setup(NULL, HIDG_MINORS);
1221 ret = ERR_PTR(status);
1227 opts->minor = hidg_get_minor();
1228 if (opts->minor < 0) {
1229 ret = ERR_PTR(opts->minor);
1231 if (ida_is_empty(&hidg_ida))
1235 config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1238 mutex_unlock(&hidg_ida_lock);
1242 static void hidg_free(struct usb_function *f)
1244 struct f_hidg *hidg;
1245 struct f_hid_opts *opts;
1247 hidg = func_to_hidg(f);
1248 opts = container_of(f->fi, struct f_hid_opts, func_inst);
1249 put_device(&hidg->dev);
1250 mutex_lock(&opts->lock);
1252 mutex_unlock(&opts->lock);
1255 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1257 struct f_hidg *hidg = func_to_hidg(f);
1259 cdev_device_del(&hidg->cdev, &hidg->dev);
1261 usb_free_all_descriptors(f);
1264 static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
1266 struct f_hidg *hidg;
1267 struct f_hid_opts *opts;
1270 /* allocate and initialize one new instance */
1271 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
1273 return ERR_PTR(-ENOMEM);
1275 opts = container_of(fi, struct f_hid_opts, func_inst);
1277 mutex_lock(&opts->lock);
1280 device_initialize(&hidg->dev);
1281 hidg->dev.release = hidg_release;
1282 hidg->dev.class = hidg_class;
1283 hidg->dev.devt = MKDEV(major, opts->minor);
1284 ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1287 mutex_unlock(&opts->lock);
1288 return ERR_PTR(ret);
1291 hidg->bInterfaceSubClass = opts->subclass;
1292 hidg->bInterfaceProtocol = opts->protocol;
1293 hidg->report_length = opts->report_length;
1294 hidg->report_desc_length = opts->report_desc_length;
1295 if (opts->report_desc) {
1296 hidg->report_desc = devm_kmemdup(&hidg->dev, opts->report_desc,
1297 opts->report_desc_length,
1299 if (!hidg->report_desc) {
1300 put_device(&hidg->dev);
1302 mutex_unlock(&opts->lock);
1303 return ERR_PTR(-ENOMEM);
1306 hidg->use_out_ep = !opts->no_out_endpoint;
1308 mutex_unlock(&opts->lock);
1310 hidg->func.name = "hid";
1311 hidg->func.bind = hidg_bind;
1312 hidg->func.unbind = hidg_unbind;
1313 hidg->func.set_alt = hidg_set_alt;
1314 hidg->func.disable = hidg_disable;
1315 hidg->func.setup = hidg_setup;
1316 hidg->func.free_func = hidg_free;
1318 /* this could me made configurable at some point */
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 hidg_class = class_create(THIS_MODULE, "hidg");
1334 if (IS_ERR(hidg_class)) {
1335 status = PTR_ERR(hidg_class);
1340 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1342 class_destroy(hidg_class);
1353 void ghid_cleanup(void)
1356 unregister_chrdev_region(MKDEV(major, 0), minors);
1360 class_destroy(hidg_class);