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 struct usb_composite_dev *u_cdev;
73 bool boot_protocol;//4 1 for boot protocol , 0 for report protocol
77 bool bypass_input = 0;
78 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
80 return container_of(f, struct f_hidg, func);
82 void hidg_disconnect();
84 /*-------------------------------------------------------------------------*/
85 /* Static descriptors */
87 static struct usb_interface_descriptor hidg_interface_desc = {
88 .bLength = sizeof hidg_interface_desc,
89 .bDescriptorType = USB_DT_INTERFACE,
90 /* .bInterfaceNumber = DYNAMIC */
91 .bAlternateSetting = 0,
93 .bInterfaceClass = USB_CLASS_HID,
94 /* .bInterfaceSubClass = DYNAMIC */
95 /* .bInterfaceProtocol = DYNAMIC */
96 /* .iInterface = DYNAMIC */
99 static struct hid_descriptor hidg_desc = {
100 .bLength = sizeof hidg_desc,
101 .bDescriptorType = HID_DT_HID,
102 .bcdHID = 0x0110, //0x0101,
103 .bCountryCode = 0x00,
104 .bNumDescriptors = 0x1,
105 /*.desc[0].bDescriptorType = DYNAMIC */
106 /*.desc[0].wDescriptorLenght = DYNAMIC */
109 /* High-Speed Support */
111 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
112 .bLength = USB_DT_ENDPOINT_SIZE,
113 .bDescriptorType = USB_DT_ENDPOINT,
114 .bEndpointAddress = USB_DIR_IN,
115 .bmAttributes = USB_ENDPOINT_XFER_INT,
116 .wMaxPacketSize = cpu_to_le16(64),
117 .bInterval = 4, /* FIXME: Add this field in the
118 * HID gadget configuration?
119 * (struct hidg_func_descriptor)
123 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
124 (struct usb_descriptor_header *)&hidg_interface_desc,
125 (struct usb_descriptor_header *)&hidg_desc,
126 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
130 /* Full-Speed Support */
132 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
133 .bLength = USB_DT_ENDPOINT_SIZE,
134 .bDescriptorType = USB_DT_ENDPOINT,
135 .bEndpointAddress = USB_DIR_IN,
136 .bmAttributes = USB_ENDPOINT_XFER_INT,
137 .wMaxPacketSize = cpu_to_le16(64),
138 .bInterval = 10, /* FIXME: Add this field in the
139 * HID gadget configuration?
140 * (struct hidg_func_descriptor)
144 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
145 (struct usb_descriptor_header *)&hidg_interface_desc,
146 (struct usb_descriptor_header *)&hidg_desc,
147 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
151 /* hid descriptor for a keyboard */
152 const struct hidg_func_descriptor my_hid_data = {
153 .subclass = 1, /* No subclass */
154 .protocol = 1, /* 1-Keyboard,2-mouse */
156 .report_desc_length = 150,
159 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
160 0x09, 0x06, /* USAGE (Keyboard) */
161 0xA1, 0x01, /* COLLECTION (Application) */
162 0x85, 0x01, /* REPORT ID (0x01) */
163 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
164 0x19, 0xE0, /* USAGE_MINIMUM (Keyboard LeftControl) */
165 0x29, 0xE7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
166 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
167 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
168 0x75, 0x01, /* REPORT_SIZE (1) */
169 0x95, 0x08, /* REPORT_COUNT (8) */
170 0x81, 0x02, /* INPUT (Data,Var,Abs) */
171 0x95, 0x01, /* REPORT_COUNT (1) */
172 0x75, 0x08, /* REPORT_SIZE (8) */
173 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
174 0x95, 0x05, /* REPORT_COUNT (5) */
175 0x75, 0x01, /* REPORT_SIZE (1) */
176 0x05, 0x08, /* USAGE_PAGE (LEDs) */
177 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
178 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
179 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
180 0x95, 0x01, /* REPORT_COUNT (1) */
181 0x75, 0x03, /* REPORT_SIZE (3) */
182 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
183 0x95, 0x06, /* REPORT_COUNT (6) */
184 0x75, 0x08, /* REPORT_SIZE (8) */
185 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
186 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
187 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
188 0x19, 0x00, /* USAGE_MINIMUM (Reserved) */
189 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
190 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
191 0xC0, /* END_COLLECTION */
193 0x05, 0x0C, /* USAGE_PAGE (consumer page) */
194 0x09, 0x01, /* USAGE (consumer control) */
195 0xA1, 0x01, /* COLLECTION (Application) */
196 0x85, 0x03, /* REPORT ID (0x03) */
197 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
198 0x26, 0xFF, 0x02, /* LOGICAL_MAXIMUM (0x2FF) */
199 0x19, 0x00, /* USAGE_MINIMUM (00) */
200 0x2A, 0xFF, 0x02, /* USAGE_MAXIMUM (0x2FF) */
201 0x75, 0x10, /* REPORT_SIZE (16) */
202 0x95, 0x01, /* REPORT_COUNT (1) */
203 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
206 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
207 0x09, 0x02, /* USAGE (Mouse) */
208 0xA1, 0x01, /* COLLECTION (Application) */
209 0x85, 0x02, /* REPORT ID (0x02) */
210 0x09, 0x01, /* USAGE (Pointer) */
212 0xA1, 0x00, /* COLLECTION (Application) */
213 0x05, 0x09, /* USAGE_PAGE (Button) */
214 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
215 0x29, 0x08, /* USAGE_MAXIMUM (Button 8) */
216 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
217 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
218 0x75, 0x01, /* REPORT_SIZE (1) */
219 0x95, 0x08, /* REPORT_COUNT (8) */
220 0x81, 0x02, /* INPUT (Data,Var,Abs) */
221 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
222 0x09, 0x30, /* USAGE (X) */
223 0x09, 0x31, /* USAGE (Y) */
224 0x16, 0x01, 0xF8, /* LOGICAL_MINIMUM (-2047) */
225 0x26, 0xFF, 0x07, /* LOGICAL_MAXIMUM (2047) */
226 0x75, 0x0C, /* REPORT_SIZE (12) */
227 0x95, 0x02, /* REPORT_COUNT (2) */
228 0x81, 0x06, /* INPUT (Data,Var,Rel) */
230 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
231 0x25, 0x7F, /* LOGICAL_MAXIMUM (127) */
232 0x75, 0x08, /* REPORT_SIZE (8) */
233 0x95, 0x01, /* REPORT_COUNT (1) */
234 0x81, 0x06, /* INPUT (Data,Var,Rel) */
235 0xC0 , /* END_COLLECTION */
237 0xC0 /* END_COLLECTION */
242 struct f_hidg *g_hidg;
244 /*-------------------------------------------------------------------------*/
247 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
248 size_t count, loff_t *ptr)
250 struct f_hidg *hidg = file->private_data;
251 char *tmp_buff = NULL;
257 if (!access_ok(VERIFY_WRITE, buffer, count))
260 spin_lock_irqsave(&hidg->spinlock, flags);
262 #define READ_COND (hidg->set_report_buff != NULL)
265 spin_unlock_irqrestore(&hidg->spinlock, flags);
266 if (file->f_flags & O_NONBLOCK)
269 if (wait_event_interruptible(hidg->read_queue, READ_COND))
272 spin_lock_irqsave(&hidg->spinlock, flags);
276 count = min_t(unsigned, count, hidg->set_report_length);
277 tmp_buff = hidg->set_report_buff;
278 hidg->set_report_buff = NULL;
280 spin_unlock_irqrestore(&hidg->spinlock, flags);
282 if (tmp_buff != NULL) {
283 /* copy to user outside spinlock */
284 count -= copy_to_user(buffer, tmp_buff, count);
292 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
294 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
296 if (req->status != 0) {
300 wake_up(&hidg->write_queue);
303 #define WRITE_COND (!hidg->write_pending)
304 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
305 size_t count, loff_t *offp)
308 struct f_hidg *hidg = file->private_data;
309 ssize_t status = -ENOMEM;
311 if (!access_ok(VERIFY_READ, buffer, count))
314 mutex_lock(&hidg->lock);
316 #define WRITE_COND (!hidg->write_pending)
319 while (!WRITE_COND) {
320 mutex_unlock(&hidg->lock);
321 if (file->f_flags & O_NONBLOCK)
324 if (wait_event_interruptible_exclusive(
325 hidg->write_queue, WRITE_COND))
328 mutex_lock(&hidg->lock);
331 count = min_t(unsigned, count, hidg->report_length);
332 status = copy_from_user(hidg->req->buf, buffer, count);
335 //ERROR(hidg->func.config->cdev,
336 // "copy_from_user error\n");
337 mutex_unlock(&hidg->lock);
341 hidg->req->status = 0;
343 hidg->req->length = count;
344 hidg->req->complete = f_hidg_req_complete;
345 hidg->req->context = hidg;
346 hidg->write_pending = 1;
348 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
350 //ERROR(hidg->func.config->cdev,
351 // "usb_ep_queue error on int endpoint %zd\n", status);
352 hidg->write_pending = 0;
353 wake_up(&hidg->write_queue);
358 mutex_unlock(&hidg->lock);
364 static void f_hid_queue_report(u8 *data, int len)
366 //this function will run in interrupt context
367 ssize_t status = -ENOMEM;
368 struct f_hidg *hidg = g_hidg;
369 //static char raw_report[8];
373 //mutex_lock(&hidg->lock);
374 memcpy(hidg->req->buf, data, len);
375 hidg->req->status = 0;
377 hidg->req->length = len;
378 //hidg->req->buf = raw_report;
379 hidg->req->complete = f_hidg_req_complete;
380 hidg->req->context = hidg;
382 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
384 //printk("usb_ep_queue error on int endpoint %zd\n", status);
387 //mutex_unlock(&hidg->lock);
392 #define KBD_REPORT_ID (0x01)
393 #define MOUSE_REPORT_ID (0x02)
394 #define CONSUMER_REPORT_ID (0x03)
396 unsigned int f_hid_bypass_input_get()
403 EXPORT_SYMBOL(f_hid_bypass_input_get);
405 unsigned char kbd_idle[] = {KBD_REPORT_ID,0,0,0,0,0,0,0,0};
406 unsigned char mouse_idle[] = {MOUSE_REPORT_ID,0,0,0,0,0};
407 unsigned char consumer_idle[]= {CONSUMER_REPORT_ID,0,0};
409 static void f_hid_send_idle_report(void)
413 f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
415 f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
417 f_hid_queue_report(consumer_idle, sizeof(consumer_idle));
421 static void f_hid_bypass_input_set(u8 bypass)
425 u8 current_state = f_hid_bypass_input_get();
427 if( bypass && (!current_state))
431 if(!bypass && (current_state))
433 f_hid_send_idle_report();
441 g_hidg->u_cdev->gadget->ops->wakeup(g_hidg->u_cdev->gadget);
449 }__attribute__ ((packed));
451 struct consumer_report {
454 }__attribute__ ((packed));
456 void f_hid_kbd_translate_report(struct hid_report *report, u8 *data)
458 if(f_hid_bypass_input_get())
461 struct kbd_report k = {0};
462 struct consumer_report c = {0};
464 struct hid_field *field;
466 k.id = KBD_REPORT_ID;//report id
467 for (i = 0; i < report->maxfield; i++){
468 field = report->field[i];
469 if(HID_MAIN_ITEM_VARIABLE & field->flags)//VARIABLE REPORT
471 for(j = 0; j < field->report_count; j++)
473 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTCTRL))
474 k.command |= field->value[j] ? 1 << 0 : 0 ;
475 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTSHIFT))
476 k.command |= field->value[j] ? 1 << 1 : 0 ;
477 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTALT))
478 k.command |= field->value[j] ? 1 << 2 : 0 ;
479 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTMETA))
480 k.command |= field->value[j] ? 1 << 3 : 0 ;
481 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTCTRL))
482 k.command |= field->value[j] ? 1 << 4 : 0 ;
483 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTSHIFT))
484 k.command |= field->value[j] ? 1 << 5 : 0 ;
485 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTALT))
486 k.command |= field->value[j] ? 1 << 6 : 0 ;
487 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTMETA))
488 k.command |= field->value[j] ? 1 << 7 : 0 ;
493 if(field->application == HID_GD_KEYBOARD)
495 for(j = 0 ; j<(min(6,field->report_count)); j++)
497 k.key_array[j] = field->value[j];
500 if(field->application == 0x000c0001)//CONSUMER PAGE
502 for(j = 0 ; j < (field->report_count); j++)
504 c.id = CONSUMER_REPORT_ID;
505 c.data = field->value[j];
506 f_hid_queue_report((u8 *)&c, sizeof(c));
512 if(g_hidg->boot_protocol)
513 f_hid_queue_report((u8 *)&k+1, sizeof(k)-1);
517 f_hid_queue_report((u8 *)&k, sizeof(k));
521 EXPORT_SYMBOL(f_hid_kbd_translate_report);
523 struct mouse_report {
527 bool button_middle:1;
530 bool button_forward:1;
537 }__attribute__ ((packed));
540 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
543 if(f_hid_bypass_input_get() && !g_hidg->boot_protocol)
545 struct mouse_report m = {0};
546 struct hid_field *field;
549 m.id = MOUSE_REPORT_ID;
550 for (i = 0; i < report->maxfield; i++){
551 field = report->field[i];
552 for(j=0; j<field->report_count; j++)
554 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_LEFT))
557 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_RIGHT))
560 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_MIDDLE))
563 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_SIDE))
566 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_EXTRA))
569 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_FORWARD))
572 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_BACK))
575 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_TASK))
580 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_X))
581 m.x = field->value[j];
582 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_Y))
583 m.y = field->value[j];
584 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_WHEEL))
585 m.wheel= field->value[j];
588 if(m.button_right || m.button_left)
590 f_hid_queue_report((u8 *)&m, sizeof(m));
593 EXPORT_SYMBOL(f_hid_mouse_translate_report);
596 #undef MOUSE_REPORT_ID
597 #undef CONSUMER_REPORT_ID
600 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
608 static int f_hidg_release(struct inode *inode, struct file *fd)
610 fd->private_data = NULL;
614 static int f_hidg_open(struct inode *inode, struct file *fd)
616 struct f_hidg *hidg =
617 container_of(inode->i_cdev, struct f_hidg, cdev);
619 fd->private_data = hidg;
624 /*-------------------------------------------------------------------------*/
630 g_hidg->connected = 1;
633 void hidg_disconnect()
636 g_hidg->connected = 0;
639 EXPORT_SYMBOL(hidg_disconnect);
640 int hidg_start(struct usb_composite_dev *cdev);
642 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
644 struct f_hidg *hidg = (struct f_hidg *)req->context;
645 printk("hidg_set_report_complete ,req->status = %d len = %d\n",
646 req->status,req->actual);
647 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
651 spin_lock(&hidg->spinlock);
656 hidg->set_report_buff = krealloc(hidg->set_report_buff,
657 req->actual, GFP_ATOMIC);
659 if (hidg->set_report_buff == NULL) {
660 spin_unlock(&hidg->spinlock);
663 hidg->set_report_length = req->actual;
664 memcpy(hidg->set_report_buff, req->buf, req->actual);
666 spin_unlock(&hidg->spinlock);
668 wake_up(&hidg->read_queue);
671 static int hidg_setup(struct usb_function *f,
672 const struct usb_ctrlrequest *ctrl)
674 struct f_hidg *hidg = func_to_hidg(f);
675 struct usb_composite_dev *cdev = f->config->cdev;
676 struct usb_request *req = cdev->req;
680 value = __le16_to_cpu(ctrl->wValue);
681 length = __le16_to_cpu(ctrl->wLength);
683 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
684 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
686 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
687 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
688 | HID_REQ_GET_REPORT):
689 VDBG(cdev, "get_report\n");
691 /* send an empty report */
692 length = min_t(unsigned, length, hidg->report_length);
693 memset(req->buf, 0x0, length);
698 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
699 | HID_REQ_GET_PROTOCOL):
700 VDBG(cdev, "get_protocol\n");
704 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
705 | HID_REQ_SET_REPORT):
706 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
708 req->complete = hidg_set_report_complete;
712 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
713 | HID_REQ_SET_PROTOCOL):
714 VDBG(cdev, "set_protocol\n");
718 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
719 | USB_REQ_GET_DESCRIPTOR):
720 switch (value >> 8) {
722 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
723 length = min_t(unsigned short, length,
724 hidg->report_desc_length);
725 memcpy(req->buf, hidg->report_desc, length);
730 VDBG(cdev, "Unknown decriptor request 0x%x\n",
738 VDBG(cdev, "Unknown request 0x%x\n",
749 req->length = length;
750 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
752 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
756 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
757 const struct usb_ctrlrequest *ctrl)
759 struct f_hidg *hidg = g_hidg;
760 struct usb_request *req = cdev->req;
764 value = __le16_to_cpu(ctrl->wValue);
765 length = __le16_to_cpu(ctrl->wLength);
768 printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
769 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
771 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
772 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
773 | HID_REQ_GET_REPORT):
774 VDBG(cdev, "get_report\n");
775 return -EOPNOTSUPP;//this command bypass to rndis
776 /* send an empty report */
777 length = min_t(unsigned, length, hidg->report_length);
778 memset(req->buf, 0x0, length);
782 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
783 | HID_REQ_GET_PROTOCOL):
784 VDBG(cdev, "get_protocol\n");
788 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
789 | HID_REQ_SET_REPORT):
790 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
792 req->complete = hidg_set_report_complete;
796 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
798 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
800 req->complete = hidg_set_report_complete;
804 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
805 | HID_REQ_SET_PROTOCOL):
806 VDBG(cdev, "set_protocol\n");
808 req->complete = hidg_set_report_complete;
809 hidg->boot_protocol = 1;
814 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
815 | USB_REQ_GET_DESCRIPTOR):
816 switch (value >> 8) {
818 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
819 length = min_t(unsigned short, length,
820 hidg->report_desc_length);
821 memcpy(req->buf, hidg->report_desc, length);
822 hidg->boot_protocol = 0;
827 VDBG(cdev, "Unknown decriptor request 0x%x\n",
835 VDBG(cdev, "Unknown request 0x%x\n",
846 req->length = length;
847 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
849 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
853 static void hidg_disable(struct usb_function *f)
855 struct f_hidg *hidg = func_to_hidg(f);
857 usb_ep_disable(hidg->in_ep);
858 hidg->in_ep->driver_data = NULL;
861 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
863 struct usb_composite_dev *cdev = f->config->cdev;
864 struct f_hidg *hidg = func_to_hidg(f);
865 const struct usb_endpoint_descriptor *ep_desc;
867 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
869 printk("^^^^^hidg_set_alt\n");
871 if (hidg->in_ep != NULL) {
872 /* restart endpoint */
873 if (hidg->in_ep->driver_data != NULL)
874 usb_ep_disable(hidg->in_ep);
876 ep_desc = ep_choose(f->config->cdev->gadget,
877 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
878 status = usb_ep_enable(hidg->in_ep, ep_desc);
880 //ERROR(cdev, "Enable endpoint FAILED!\n");
883 hidg->in_ep->driver_data = hidg;
889 int hidg_start(struct usb_composite_dev *cdev)
891 printk("^^^^^hidg_start\n");
892 struct f_hidg *hidg = g_hidg;
893 const struct usb_endpoint_descriptor *ep_desc;
896 if (hidg->in_ep != NULL) {
897 /* restart endpoint */
898 if (hidg->in_ep->driver_data != NULL)
899 usb_ep_disable(hidg->in_ep);
901 ep_desc = ep_choose(cdev->gadget,
902 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
903 status = usb_ep_enable(hidg->in_ep, ep_desc);
905 printk("Enable endpoint FAILED!\n");
908 hidg->in_ep->driver_data = hidg;
915 const struct file_operations f_hidg_fops = {
916 .owner = THIS_MODULE,
918 .release = f_hidg_release,
919 .write = NULL,//f_hidg_write,disable write to /dev/hidg0
921 .poll = NULL,//f_hidg_poll,
922 .llseek = noop_llseek,
925 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
928 struct usb_ep *ep_in;
929 struct f_hidg *hidg = func_to_hidg(f);
932 /* allocate instance-specific interface IDs, and patch descriptors */
933 status = usb_interface_id(c, f);
936 hidg_interface_desc.bInterfaceNumber = status;
938 /* allocate instance-specific endpoints */
940 ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
943 ep_in->driver_data = c->cdev; /* claim */
946 /* preallocate request and buffer */
948 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
953 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
957 /* set descriptor dynamic values */
958 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
959 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
960 // hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
961 // hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
962 // hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
963 // hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
964 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
965 hidg_desc.desc[0].wDescriptorLength =
966 cpu_to_le16(hidg->report_desc_length);
968 hidg->set_report_buff = NULL;
970 /* copy descriptors */
971 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
975 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
977 &hidg_fs_in_ep_desc);
979 if (gadget_is_dualspeed(c->cdev->gadget)) {
980 hidg_hs_in_ep_desc.bEndpointAddress =
981 hidg_fs_in_ep_desc.bEndpointAddress;
982 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
983 if (!f->hs_descriptors)
985 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
987 &hidg_hs_in_ep_desc);
989 hidg->hs_in_ep_desc = NULL;
994 mutex_init(&hidg->lock);
995 spin_lock_init(&hidg->spinlock);
996 init_waitqueue_head(&hidg->write_queue);
997 init_waitqueue_head(&hidg->read_queue);
999 /* create char device */
1000 cdev_init(&hidg->cdev, &f_hidg_fops);
1001 dev = MKDEV(major, hidg->minor);
1002 status = cdev_add(&hidg->cdev, dev, 1);
1006 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
1011 ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
1012 if (hidg->req != NULL) {
1013 kfree(hidg->req->buf);
1014 if (hidg->in_ep != NULL)
1015 usb_ep_free_request(hidg->in_ep, hidg->req);
1018 usb_free_descriptors(f->hs_descriptors);
1019 usb_free_descriptors(f->descriptors);
1024 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1026 struct f_hidg *hidg = func_to_hidg(f);
1029 device_destroy(hidg_class, MKDEV(major, hidg->minor));
1030 cdev_del(&hidg->cdev);
1032 /* disable/free request and end point */
1033 usb_ep_disable(hidg->in_ep);
1034 usb_ep_dequeue(hidg->in_ep, hidg->req);
1036 kfree(hidg->req->buf);
1037 usb_ep_free_request(hidg->in_ep, hidg->req);
1039 /* free descriptors copies */
1040 usb_free_descriptors(f->hs_descriptors);
1041 usb_free_descriptors(f->descriptors);
1044 kfree(hidg->report_desc);
1045 kfree(hidg->set_report_buff);
1051 /*-------------------------------------------------------------------------*/
1054 #define CT_FUNC_HID_IDX 0
1056 static struct usb_string ct_func_string_defs[] = {
1057 [CT_FUNC_HID_IDX].s = "HID Interface",
1058 {}, /* end of list */
1061 static struct usb_gadget_strings ct_func_string_table = {
1062 .language = 0x0409, /* en-US */
1063 .strings = ct_func_string_defs,
1066 static struct usb_gadget_strings *ct_func_strings[] = {
1067 &ct_func_string_table,
1071 /*-------------------------------------------------------------------------*/
1072 /* usb_configuration */
1074 int hidg_bind_config(struct usb_configuration *c,
1075 const struct hidg_func_descriptor *fdesc, int index)
1077 struct f_hidg *hidg;
1079 if (index >= minors)
1082 /* maybe allocate device-global string IDs, and patch descriptors */
1083 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
1084 status = usb_string_id(c->cdev);
1087 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1088 hidg_interface_desc.iInterface = status;
1091 /* allocate and initialize one new instance */
1092 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1096 hidg->boot_protocol = 1;
1097 hidg->connected = 0;
1098 hidg->minor = index;
1099 hidg->bInterfaceSubClass = fdesc->subclass;
1100 hidg->bInterfaceProtocol = fdesc->protocol;
1101 hidg->report_length = fdesc->report_length;
1102 hidg->report_desc_length = fdesc->report_desc_length;
1103 hidg->report_desc = kmemdup(fdesc->report_desc,
1104 fdesc->report_desc_length,
1106 if (!hidg->report_desc) {
1111 hidg->func.name = "hid";
1112 hidg->func.strings = ct_func_strings;
1113 hidg->func.bind = hidg_bind;
1114 hidg->func.unbind = hidg_unbind;
1115 hidg->func.set_alt = hidg_set_alt;
1116 hidg->func.disable = hidg_disable;
1117 hidg->func.setup = hidg_setup;
1119 status = usb_add_function(c, &hidg->func);
1124 g_hidg->u_cdev = c->cdev;
1128 int ghid_setup(struct usb_gadget *g, int count)
1133 hidg_class = class_create(THIS_MODULE, "hidg");
1135 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1144 void ghid_cleanup(void)
1147 unregister_chrdev_region(MKDEV(major, 0), minors);
1151 class_destroy(hidg_class);