2 * f_hid.c -- USB HID function driver
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/utsname.h>
23 #include <linux/module.h>
24 #include <linux/hid.h>
25 #include <linux/cdev.h>
26 #include <linux/mutex.h>
27 #include <linux/poll.h>
28 #include <linux/uaccess.h>
29 #include <linux/wait.h>
30 #include <linux/usb/g_hid.h>
34 static int major, minors;
35 static struct class *hidg_class;
37 /*-------------------------------------------------------------------------*/
38 /* HID gadget struct */
42 unsigned char bInterfaceSubClass;
43 unsigned char bInterfaceProtocol;
44 unsigned short report_desc_length;
46 unsigned short report_length;
49 char *set_report_buff;
50 unsigned short set_report_length;
52 wait_queue_head_t read_queue;
53 struct usb_request *req_out;
58 wait_queue_head_t write_queue;
59 struct usb_request *req;
63 struct usb_function func;
65 struct usb_endpoint_descriptor *fs_in_ep_desc;
66 struct usb_endpoint_descriptor *hs_in_ep_desc;
67 struct usb_endpoint_descriptor *fs_out_ep_desc;
68 struct usb_endpoint_descriptor *hs_out_ep_desc;
70 unsigned short bypass_input;
74 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
76 return container_of(f, struct f_hidg, func);
79 /*-------------------------------------------------------------------------*/
80 /* Static descriptors */
82 static struct usb_interface_descriptor hidg_interface_desc = {
83 .bLength = sizeof hidg_interface_desc,
84 .bDescriptorType = USB_DT_INTERFACE,
85 /* .bInterfaceNumber = DYNAMIC */
86 .bAlternateSetting = 0,
88 .bInterfaceClass = USB_CLASS_HID,
89 /* .bInterfaceSubClass = DYNAMIC */
90 /* .bInterfaceProtocol = DYNAMIC */
91 /* .iInterface = DYNAMIC */
94 static struct hid_descriptor hidg_desc = {
95 .bLength = sizeof hidg_desc,
96 .bDescriptorType = HID_DT_HID,
97 .bcdHID = 0x0110, //0x0101,
99 .bNumDescriptors = 0x1,
100 /*.desc[0].bDescriptorType = DYNAMIC */
101 /*.desc[0].wDescriptorLenght = DYNAMIC */
104 /* High-Speed Support */
106 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bEndpointAddress = USB_DIR_IN,
110 .bmAttributes = USB_ENDPOINT_XFER_INT,
111 .wMaxPacketSize = cpu_to_le16(64),
112 .bInterval = 4, /* FIXME: Add this field in the
113 * HID gadget configuration?
114 * (struct hidg_func_descriptor)
118 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
119 .bLength = USB_DT_ENDPOINT_SIZE,
120 .bDescriptorType = USB_DT_ENDPOINT,
121 .bEndpointAddress = USB_DIR_OUT|0x02,
122 .bmAttributes = USB_ENDPOINT_XFER_INT,
123 .wMaxPacketSize = cpu_to_le16(64),
124 .bInterval = 4, /* FIXME: Add this field in the
125 * HID gadget configuration?
126 * (struct hidg_func_descriptor)
131 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
132 (struct usb_descriptor_header *)&hidg_interface_desc,
133 (struct usb_descriptor_header *)&hidg_desc,
134 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
135 //(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
139 /* Full-Speed Support */
141 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = USB_DIR_IN,
145 .bmAttributes = USB_ENDPOINT_XFER_INT,
146 .wMaxPacketSize = cpu_to_le16(64),
147 .bInterval = 10, /* FIXME: Add this field in the
148 * HID gadget configuration?
149 * (struct hidg_func_descriptor)
153 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
154 .bLength = USB_DT_ENDPOINT_SIZE,
155 .bDescriptorType = USB_DT_ENDPOINT,
156 .bEndpointAddress = USB_DIR_OUT|0x02,
157 .bmAttributes = USB_ENDPOINT_XFER_INT,
158 .wMaxPacketSize = cpu_to_le16(64),
159 .bInterval = 10, /* FIXME: Add this field in the
160 * HID gadget configuration?
161 * (struct hidg_func_descriptor)
165 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
166 (struct usb_descriptor_header *)&hidg_interface_desc,
167 (struct usb_descriptor_header *)&hidg_desc,
168 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
169 //(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
173 struct f_hidg *g_hidg;
175 /*-------------------------------------------------------------------------*/
178 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
179 size_t count, loff_t *ptr)
181 struct f_hidg *hidg = file->private_data;
182 char *tmp_buff = NULL;
188 if (!access_ok(VERIFY_WRITE, buffer, count))
191 spin_lock_irqsave(&hidg->spinlock, flags);
193 #define READ_COND (hidg->set_report_buff != NULL)
196 spin_unlock_irqrestore(&hidg->spinlock, flags);
197 if (file->f_flags & O_NONBLOCK)
200 if (wait_event_interruptible(hidg->read_queue, READ_COND))
203 spin_lock_irqsave(&hidg->spinlock, flags);
207 count = min_t(unsigned, count, hidg->set_report_length);
208 tmp_buff = hidg->set_report_buff;
209 hidg->set_report_buff = NULL;
211 spin_unlock_irqrestore(&hidg->spinlock, flags);
213 if (tmp_buff != NULL) {
214 /* copy to user outside spinlock */
215 count -= copy_to_user(buffer, tmp_buff, count);
223 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
225 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
227 if (req->status != 0) {
228 //ERROR(hidg->func.config->cdev,
229 // "End Point Request ERROR: %d\n", req->status);
232 hidg->write_pending = 0;
233 wake_up(&hidg->write_queue);
236 #define WRITE_COND (!hidg->write_pending)
237 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
238 size_t count, loff_t *offp)
241 struct f_hidg *hidg = file->private_data;
242 ssize_t status = -ENOMEM;
244 if (!access_ok(VERIFY_READ, buffer, count))
247 mutex_lock(&hidg->lock);
249 #define WRITE_COND (!hidg->write_pending)
252 while (!WRITE_COND) {
253 mutex_unlock(&hidg->lock);
254 if (file->f_flags & O_NONBLOCK)
257 if (wait_event_interruptible_exclusive(
258 hidg->write_queue, WRITE_COND))
261 mutex_lock(&hidg->lock);
264 count = min_t(unsigned, count, hidg->report_length);
265 status = copy_from_user(hidg->req->buf, buffer, count);
268 //ERROR(hidg->func.config->cdev,
269 // "copy_from_user error\n");
270 mutex_unlock(&hidg->lock);
274 hidg->req->status = 0;
276 hidg->req->length = count;
277 hidg->req->complete = f_hidg_req_complete;
278 hidg->req->context = hidg;
279 hidg->write_pending = 1;
281 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
283 //ERROR(hidg->func.config->cdev,
284 // "usb_ep_queue error on int endpoint %zd\n", status);
285 hidg->write_pending = 0;
286 wake_up(&hidg->write_queue);
291 mutex_unlock(&hidg->lock);
295 static void f_hid_queue_report(u8 *data, int len)
297 //this function will run in interrupt context
298 ssize_t status = -ENOMEM;
299 struct f_hidg *hidg = g_hidg;
300 //static char raw_report[8];
304 //mutex_lock(&hidg->lock);
305 memcpy(hidg->req->buf, data, len);
306 hidg->req->status = 0;
308 hidg->req->length = len;
309 //hidg->req->buf = raw_report;
310 hidg->req->complete = f_hidg_req_complete;
311 hidg->req->context = hidg;
313 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
315 printk("usb_ep_queue error on int endpoint %zd\n", status);
318 //mutex_unlock(&hidg->lock);
323 #define KBD_REPORT_ID (0x01)
324 #define MOUSE_REPORT_ID (0x02)
326 unsigned int f_hid_bypass_input_get()
331 return g_hidg->bypass_input;
333 EXPORT_SYMBOL(f_hid_bypass_input_get);
335 unsigned char kbd_idle[] = {KBD_REPORT_ID,0,0,0,0,0,0,0,0};
336 unsigned char mouse_idle[] = {MOUSE_REPORT_ID,0,0,0,0,0};
338 static void f_hid_send_idle_report(void)
342 f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
344 f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
348 static void f_hid_bypass_input_set(u8 bypass)
352 u8 current_state = f_hid_bypass_input_get();
354 if( bypass && (!current_state))
356 g_hidg->bypass_input = 1;
358 if(!bypass && (current_state))
360 f_hid_send_idle_report();
361 g_hidg->bypass_input = 0;
369 }__attribute__ ((packed));
371 void f_hid_kbd_translate_report(u8 *data, int len)
373 if(f_hid_bypass_input_get())
375 struct kbd_report k = {0};
376 k.id = KBD_REPORT_ID;//report id
377 memcpy(k.raw_report, data, len);//Byte 1 for report id
379 f_hid_queue_report((u8 *)&k, sizeof(k));
382 EXPORT_SYMBOL(f_hid_kbd_translate_report);
384 struct mouse_report {
390 }__attribute__ ((packed));
393 void f_hid_mouse_translate_report(struct hid_report *report , u8 *data)
396 if(f_hid_bypass_input_get())
398 struct mouse_report m = {0};
399 struct hid_field *field;
401 s32 mouse_rel[2] = { 0 };//0 for x, 1 for y
403 u8 mouse_button = 0;//bit[2:0] is valid, [7:3] reserved
407 for (i = 0; i < report->maxfield; i++){
408 field = report->field[i];
409 if(field->usage->type == EV_KEY && field->usage->code == BTN_MOUSE) //mouse button
411 for(j=0; j < field->report_count; j++)
414 mouse_button |= 1 << j;
417 if(field->usage->type == EV_REL && field->usage->code == 0) //mouse rel location
419 for(j=0; j < field->report_count ;j++)
421 mouse_rel[j] = field->value[j];
424 if(field->usage->type == EV_REL && field->usage->code == REL_WHEEL) //mouse wheel
426 mouse_wheel = field->value[0];
431 /*** generate HID report for hidg ***/
432 m.id = MOUSE_REPORT_ID;
433 m.button = mouse_button;
436 m.wheel = mouse_wheel;
438 f_hid_queue_report((u8 *)&m, sizeof(m));
441 EXPORT_SYMBOL(f_hid_mouse_translate_report);
444 #undef MOUSE_REPORT_ID
446 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
448 struct f_hidg *hidg = file->private_data;
449 unsigned int ret = 0;
451 poll_wait(file, &hidg->read_queue, wait);
452 poll_wait(file, &hidg->write_queue, wait);
455 ret |= POLLOUT | POLLWRNORM;
458 ret |= POLLIN | POLLRDNORM;
466 static int f_hidg_release(struct inode *inode, struct file *fd)
468 fd->private_data = NULL;
472 static int f_hidg_open(struct inode *inode, struct file *fd)
474 struct f_hidg *hidg =
475 container_of(inode->i_cdev, struct f_hidg, cdev);
477 fd->private_data = hidg;
482 /*-------------------------------------------------------------------------*/
488 g_hidg->connected = 1;
491 void hidg_disconnect()
494 g_hidg->connected = 0;
497 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
499 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
501 struct f_hidg *hidg = (struct f_hidg *)req->context;
502 printk("hidg_set_report_complete ,req->status = %d len = %d\n",
503 req->status,req->actual);
504 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
508 spin_lock(&hidg->spinlock);
511 //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
514 hidg->set_report_buff = krealloc(hidg->set_report_buff,
515 req->actual, GFP_ATOMIC);
517 if (hidg->set_report_buff == NULL) {
518 spin_unlock(&hidg->spinlock);
521 hidg->set_report_length = req->actual;
522 memcpy(hidg->set_report_buff, req->buf, req->actual);
524 spin_unlock(&hidg->spinlock);
526 wake_up(&hidg->read_queue);
529 static int hidg_setup(struct usb_function *f,
530 const struct usb_ctrlrequest *ctrl)
532 struct f_hidg *hidg = func_to_hidg(f);
533 struct usb_composite_dev *cdev = f->config->cdev;
534 struct usb_request *req = cdev->req;
538 value = __le16_to_cpu(ctrl->wValue);
539 length = __le16_to_cpu(ctrl->wLength);
541 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
542 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
544 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
545 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
546 | HID_REQ_GET_REPORT):
547 VDBG(cdev, "get_report\n");
549 /* send an empty report */
550 length = min_t(unsigned, length, hidg->report_length);
551 memset(req->buf, 0x0, length);
556 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
557 | HID_REQ_GET_PROTOCOL):
558 VDBG(cdev, "get_protocol\n");
562 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
563 | HID_REQ_SET_REPORT):
564 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
566 req->complete = hidg_set_report_complete;
570 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
571 | HID_REQ_SET_PROTOCOL):
572 VDBG(cdev, "set_protocol\n");
576 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
577 | USB_REQ_GET_DESCRIPTOR):
578 switch (value >> 8) {
580 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
581 length = min_t(unsigned short, length,
582 hidg->report_desc_length);
583 memcpy(req->buf, hidg->report_desc, length);
588 VDBG(cdev, "Unknown decriptor request 0x%x\n",
596 VDBG(cdev, "Unknown request 0x%x\n",
607 req->length = length;
608 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
610 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
614 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
615 const struct usb_ctrlrequest *ctrl)
617 struct f_hidg *hidg = g_hidg;
618 struct usb_request *req = cdev->req;
622 value = __le16_to_cpu(ctrl->wValue);
623 length = __le16_to_cpu(ctrl->wLength);
626 printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
627 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
629 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
630 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
631 | HID_REQ_GET_REPORT):
632 VDBG(cdev, "get_report\n");
634 /* send an empty report */
635 length = min_t(unsigned, length, hidg->report_length);
636 memset(req->buf, 0x0, length);
641 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
642 | HID_REQ_GET_PROTOCOL):
643 VDBG(cdev, "get_protocol\n");
647 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
648 | HID_REQ_SET_REPORT):
649 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
651 req->complete = hidg_set_report_complete;
655 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
656 | HID_REQ_SET_PROTOCOL):
657 VDBG(cdev, "set_protocol\n");
661 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
662 | USB_REQ_GET_DESCRIPTOR):
663 switch (value >> 8) {
665 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
666 length = min_t(unsigned short, length,
667 hidg->report_desc_length);
668 memcpy(req->buf, hidg->report_desc, length);
673 VDBG(cdev, "Unknown decriptor request 0x%x\n",
681 VDBG(cdev, "Unknown request 0x%x\n",
692 req->length = length;
693 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
695 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
699 static void hidg_disable(struct usb_function *f)
701 struct f_hidg *hidg = func_to_hidg(f);
703 usb_ep_disable(hidg->in_ep);
704 hidg->in_ep->driver_data = NULL;
707 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
709 struct usb_composite_dev *cdev = f->config->cdev;
710 struct f_hidg *hidg = func_to_hidg(f);
711 const struct usb_endpoint_descriptor *ep_desc;
714 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
716 if (hidg->in_ep != NULL) {
717 /* restart endpoint */
718 if (hidg->in_ep->driver_data != NULL)
719 usb_ep_disable(hidg->in_ep);
721 ep_desc = ep_choose(f->config->cdev->gadget,
722 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
723 status = usb_ep_enable(hidg->in_ep, ep_desc);
725 //ERROR(cdev, "Enable endpoint FAILED!\n");
728 hidg->in_ep->driver_data = hidg;
734 const struct file_operations f_hidg_fops = {
735 .owner = THIS_MODULE,
737 .release = f_hidg_release,
738 .write = NULL,//f_hidg_write,disable write to /dev/hidg0
741 .llseek = noop_llseek,
744 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
746 struct usb_ep *ep_in;
747 struct f_hidg *hidg = func_to_hidg(f);
750 /* allocate instance-specific interface IDs, and patch descriptors */
751 status = usb_interface_id(c, f);
754 hidg_interface_desc.bInterfaceNumber = status;
756 /* allocate instance-specific endpoints */
758 ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
761 ep_in->driver_data = c->cdev; /* claim */
764 /* allocate out endpoint*/
765 ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
768 ep_out->driver_data = c->cdev; /* claim */
769 hidg->out_ep = ep_out;
771 printk("ep_out->name = %s\n",ep_out->name);
773 /* preallocate request and buffer */
775 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
780 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
784 /* set descriptor dynamic values */
785 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
786 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
787 // hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
788 // hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
789 // hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
790 // hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
791 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
792 hidg_desc.desc[0].wDescriptorLength =
793 cpu_to_le16(hidg->report_desc_length);
795 hidg->set_report_buff = NULL;
797 /* copy descriptors */
798 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
802 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
804 &hidg_fs_in_ep_desc);
806 if (gadget_is_dualspeed(c->cdev->gadget)) {
807 hidg_hs_in_ep_desc.bEndpointAddress =
808 hidg_fs_in_ep_desc.bEndpointAddress;
809 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
810 if (!f->hs_descriptors)
812 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
814 &hidg_hs_in_ep_desc);
816 hidg->hs_in_ep_desc = NULL;
821 mutex_init(&hidg->lock);
822 spin_lock_init(&hidg->spinlock);
823 init_waitqueue_head(&hidg->write_queue);
824 init_waitqueue_head(&hidg->read_queue);
826 /* create char device */
827 cdev_init(&hidg->cdev, &f_hidg_fops);
828 dev = MKDEV(major, hidg->minor);
829 status = cdev_add(&hidg->cdev, dev, 1);
833 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
838 ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
839 if (hidg->req != NULL) {
840 kfree(hidg->req->buf);
841 if (hidg->in_ep != NULL)
842 usb_ep_free_request(hidg->in_ep, hidg->req);
845 usb_free_descriptors(f->hs_descriptors);
846 usb_free_descriptors(f->descriptors);
851 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
853 struct f_hidg *hidg = func_to_hidg(f);
855 f_hid_bypass_input_set(0);
857 device_destroy(hidg_class, MKDEV(major, hidg->minor));
858 cdev_del(&hidg->cdev);
860 /* disable/free request and end point */
861 usb_ep_disable(hidg->in_ep);
862 usb_ep_dequeue(hidg->in_ep, hidg->req);
864 kfree(hidg->req->buf);
865 usb_ep_free_request(hidg->in_ep, hidg->req);
867 /* free descriptors copies */
868 usb_free_descriptors(f->hs_descriptors);
869 usb_free_descriptors(f->descriptors);
872 kfree(hidg->report_desc);
873 kfree(hidg->set_report_buff);
879 /*-------------------------------------------------------------------------*/
882 #define CT_FUNC_HID_IDX 0
884 static struct usb_string ct_func_string_defs[] = {
885 [CT_FUNC_HID_IDX].s = "HID Interface",
886 {}, /* end of list */
889 static struct usb_gadget_strings ct_func_string_table = {
890 .language = 0x0409, /* en-US */
891 .strings = ct_func_string_defs,
894 static struct usb_gadget_strings *ct_func_strings[] = {
895 &ct_func_string_table,
899 /*-------------------------------------------------------------------------*/
900 /* usb_configuration */
902 int hidg_bind_config(struct usb_configuration *c,
903 const struct hidg_func_descriptor *fdesc, int index)
910 /* maybe allocate device-global string IDs, and patch descriptors */
911 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
912 status = usb_string_id(c->cdev);
915 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
916 hidg_interface_desc.iInterface = status;
919 /* allocate and initialize one new instance */
920 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
924 hidg->bypass_input = 0;
926 hidg->bInterfaceSubClass = fdesc->subclass;
927 hidg->bInterfaceProtocol = fdesc->protocol;
928 hidg->report_length = fdesc->report_length;
929 hidg->report_desc_length = fdesc->report_desc_length;
930 hidg->report_desc = kmemdup(fdesc->report_desc,
931 fdesc->report_desc_length,
933 if (!hidg->report_desc) {
938 hidg->func.name = "hid";
939 hidg->func.strings = ct_func_strings;
940 hidg->func.bind = hidg_bind;
941 hidg->func.unbind = hidg_unbind;
942 hidg->func.set_alt = hidg_set_alt;
943 hidg->func.disable = hidg_disable;
944 hidg->func.setup = hidg_setup;
946 status = usb_add_function(c, &hidg->func);
954 int ghid_setup(struct usb_gadget *g, int count)
959 hidg_class = class_create(THIS_MODULE, "hidg");
961 status = alloc_chrdev_region(&dev, 0, count, "hidg");
970 void ghid_cleanup(void)
973 unregister_chrdev_region(MKDEV(major, 0), minors);
977 class_destroy(hidg_class);