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 /* hid descriptor for a keyboard */
174 const struct hidg_func_descriptor my_hid_data = {
175 .subclass = 1, /* No subclass */
176 .protocol = 2, /* 1-Keyboard,2-mouse */
178 .report_desc_length = 150,
181 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
182 0x09, 0x06, /* USAGE (Keyboard) */
183 0xA1, 0x01, /* COLLECTION (Application) */
184 0x85, 0x01, /* REPORT ID (0x01) */
185 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
186 0x19, 0xE0, /* USAGE_MINIMUM (Keyboard LeftControl) */
187 0x29, 0xE7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
188 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
189 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
190 0x75, 0x01, /* REPORT_SIZE (1) */
191 0x95, 0x08, /* REPORT_COUNT (8) */
192 0x81, 0x02, /* INPUT (Data,Var,Abs) */
193 0x95, 0x01, /* REPORT_COUNT (1) */
194 0x75, 0x08, /* REPORT_SIZE (8) */
195 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
196 0x95, 0x05, /* REPORT_COUNT (5) */
197 0x75, 0x01, /* REPORT_SIZE (1) */
198 0x05, 0x08, /* USAGE_PAGE (LEDs) */
199 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
200 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
201 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
202 0x95, 0x01, /* REPORT_COUNT (1) */
203 0x75, 0x03, /* REPORT_SIZE (3) */
204 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
205 0x95, 0x06, /* REPORT_COUNT (6) */
206 0x75, 0x08, /* REPORT_SIZE (8) */
207 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
208 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
209 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
210 0x19, 0x00, /* USAGE_MINIMUM (Reserved) */
211 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
212 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
213 0xC0, /* END_COLLECTION */
215 0x05, 0x0C, /* USAGE_PAGE (consumer page) */
216 0x09, 0x01, /* USAGE (consumer control) */
217 0xA1, 0x01, /* COLLECTION (Application) */
218 0x85, 0x03, /* REPORT ID (0x03) */
219 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
220 0x26, 0xFF, 0x02, /* LOGICAL_MAXIMUM (0x2FF) */
221 0x19, 0x00, /* USAGE_MINIMUM (00) */
222 0x2A, 0xFF, 0x02, /* USAGE_MAXIMUM (0x2FF) */
223 0x75, 0x10, /* REPORT_SIZE (16) */
224 0x95, 0x01, /* REPORT_COUNT (1) */
225 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
228 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
229 0x09, 0x02, /* USAGE (Mouse) */
230 0xA1, 0x01, /* COLLECTION (Application) */
231 0x85, 0x02, /* REPORT ID (0x02) */
232 0x09, 0x01, /* USAGE (Pointer) */
234 0xA1, 0x00, /* COLLECTION (Application) */
235 0x05, 0x09, /* USAGE_PAGE (Button) */
236 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
237 0x29, 0x08, /* USAGE_MAXIMUM (Button 8) */
238 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
239 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
240 0x75, 0x01, /* REPORT_SIZE (1) */
241 0x95, 0x08, /* REPORT_COUNT (8) */
242 0x81, 0x02, /* INPUT (Data,Var,Abs) */
243 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
244 0x09, 0x30, /* USAGE (X) */
245 0x09, 0x31, /* USAGE (Y) */
246 0x16, 0x01, 0xF8, /* LOGICAL_MINIMUM (-2047) */
247 0x26, 0xFF, 0x07, /* LOGICAL_MAXIMUM (2047) */
248 0x75, 0x0C, /* REPORT_SIZE (12) */
249 0x95, 0x02, /* REPORT_COUNT (2) */
250 0x81, 0x06, /* INPUT (Data,Var,Rel) */
252 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
253 0x25, 0x7F, /* LOGICAL_MAXIMUM (127) */
254 0x75, 0x08, /* REPORT_SIZE (8) */
255 0x95, 0x01, /* REPORT_COUNT (1) */
256 0x81, 0x06, /* INPUT (Data,Var,Rel) */
257 0xC0 , /* END_COLLECTION */
259 0xC0 /* END_COLLECTION */
264 struct f_hidg *g_hidg;
266 /*-------------------------------------------------------------------------*/
269 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
270 size_t count, loff_t *ptr)
272 struct f_hidg *hidg = file->private_data;
273 char *tmp_buff = NULL;
279 if (!access_ok(VERIFY_WRITE, buffer, count))
282 spin_lock_irqsave(&hidg->spinlock, flags);
284 #define READ_COND (hidg->set_report_buff != NULL)
287 spin_unlock_irqrestore(&hidg->spinlock, flags);
288 if (file->f_flags & O_NONBLOCK)
291 if (wait_event_interruptible(hidg->read_queue, READ_COND))
294 spin_lock_irqsave(&hidg->spinlock, flags);
298 count = min_t(unsigned, count, hidg->set_report_length);
299 tmp_buff = hidg->set_report_buff;
300 hidg->set_report_buff = NULL;
302 spin_unlock_irqrestore(&hidg->spinlock, flags);
304 if (tmp_buff != NULL) {
305 /* copy to user outside spinlock */
306 count -= copy_to_user(buffer, tmp_buff, count);
314 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
316 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
318 if (req->status != 0) {
319 //ERROR(hidg->func.config->cdev,
320 // "End Point Request ERROR: %d\n", req->status);
323 hidg->write_pending = 0;
324 wake_up(&hidg->write_queue);
327 #define WRITE_COND (!hidg->write_pending)
328 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
329 size_t count, loff_t *offp)
332 struct f_hidg *hidg = file->private_data;
333 ssize_t status = -ENOMEM;
335 if (!access_ok(VERIFY_READ, buffer, count))
338 mutex_lock(&hidg->lock);
340 #define WRITE_COND (!hidg->write_pending)
343 while (!WRITE_COND) {
344 mutex_unlock(&hidg->lock);
345 if (file->f_flags & O_NONBLOCK)
348 if (wait_event_interruptible_exclusive(
349 hidg->write_queue, WRITE_COND))
352 mutex_lock(&hidg->lock);
355 count = min_t(unsigned, count, hidg->report_length);
356 status = copy_from_user(hidg->req->buf, buffer, count);
359 //ERROR(hidg->func.config->cdev,
360 // "copy_from_user error\n");
361 mutex_unlock(&hidg->lock);
365 hidg->req->status = 0;
367 hidg->req->length = count;
368 hidg->req->complete = f_hidg_req_complete;
369 hidg->req->context = hidg;
370 hidg->write_pending = 1;
372 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
374 //ERROR(hidg->func.config->cdev,
375 // "usb_ep_queue error on int endpoint %zd\n", status);
376 hidg->write_pending = 0;
377 wake_up(&hidg->write_queue);
382 mutex_unlock(&hidg->lock);
386 static void f_hid_queue_report(u8 *data, int len)
388 //this function will run in interrupt context
389 ssize_t status = -ENOMEM;
390 struct f_hidg *hidg = g_hidg;
391 //static char raw_report[8];
395 //mutex_lock(&hidg->lock);
396 memcpy(hidg->req->buf, data, len);
397 hidg->req->status = 0;
399 hidg->req->length = len;
400 //hidg->req->buf = raw_report;
401 hidg->req->complete = f_hidg_req_complete;
402 hidg->req->context = hidg;
404 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
406 printk("usb_ep_queue error on int endpoint %zd\n", status);
409 //mutex_unlock(&hidg->lock);
414 #define KBD_REPORT_ID (0x01)
415 #define MOUSE_REPORT_ID (0x02)
416 #define CONSUMER_REPORT_ID (0x03)
418 unsigned int f_hid_bypass_input_get()
423 return g_hidg->bypass_input;
425 EXPORT_SYMBOL(f_hid_bypass_input_get);
427 unsigned char kbd_idle[] = {KBD_REPORT_ID,0,0,0,0,0,0,0,0};
428 unsigned char mouse_idle[] = {MOUSE_REPORT_ID,0,0,0,0,0};
429 unsigned char consumer_idle[]= {CONSUMER_REPORT_ID,0,0};
431 static void f_hid_send_idle_report(void)
435 f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
437 f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
439 f_hid_queue_report(consumer_idle, sizeof(consumer_idle));
443 static void f_hid_bypass_input_set(u8 bypass)
447 u8 current_state = f_hid_bypass_input_get();
449 if( bypass && (!current_state))
451 g_hidg->bypass_input = 1;
453 if(!bypass && (current_state))
455 f_hid_send_idle_report();
456 g_hidg->bypass_input = 0;
467 }__attribute__ ((packed));
469 struct consumer_report {
472 }__attribute__ ((packed));
474 void f_hid_kbd_translate_report(struct hid_report *report, u8 *data)
476 if(f_hid_bypass_input_get())
479 struct kbd_report k = {0};
480 struct consumer_report c = {0};
482 struct hid_field *field;
484 k.id = KBD_REPORT_ID;//report id
485 for (i = 0; i < report->maxfield; i++){
486 field = report->field[i];
487 if(HID_MAIN_ITEM_VARIABLE & field->flags)//VARIABLE REPORT
489 for(j = 0; j < field->report_count; j++)
491 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTCTRL))
492 k.command |= field->value[j] ? 1 << 0 : 0 ;
493 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTSHIFT))
494 k.command |= field->value[j] ? 1 << 1 : 0 ;
495 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTALT))
496 k.command |= field->value[j] ? 1 << 2 : 0 ;
497 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTMETA))
498 k.command |= field->value[j] ? 1 << 3 : 0 ;
499 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTCTRL))
500 k.command |= field->value[j] ? 1 << 4 : 0 ;
501 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTSHIFT))
502 k.command |= field->value[j] ? 1 << 5 : 0 ;
503 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTALT))
504 k.command |= field->value[j] ? 1 << 6 : 0 ;
505 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTMETA))
506 k.command |= field->value[j] ? 1 << 7 : 0 ;
511 if(field->application == HID_GD_KEYBOARD)
513 for(j = 0 ; j<(min(6,field->report_count)); j++)
515 k.key_array[j] = field->value[j];
518 if(field->application == 0x000c0001)//CONSUMER PAGE
520 for(j = 0 ; j < (field->report_count); j++)
522 c.id = CONSUMER_REPORT_ID;
523 c.data = field->value[j];
524 f_hid_queue_report((u8 *)&c, sizeof(c));
530 f_hid_queue_report((u8 *)&k, sizeof(k));
533 EXPORT_SYMBOL(f_hid_kbd_translate_report);
535 struct mouse_report {
539 bool button_middle:1;
542 bool button_forward:1;
549 }__attribute__ ((packed));
552 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
555 if(f_hid_bypass_input_get())
557 struct mouse_report m = {0};
558 struct hid_field *field;
561 m.id = MOUSE_REPORT_ID;
562 for (i = 0; i < report->maxfield; i++){
563 field = report->field[i];
564 for(j=0; j<field->report_count; j++)
566 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_LEFT))
569 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_RIGHT))
572 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_MIDDLE))
575 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_SIDE))
578 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_EXTRA))
581 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_FORWARD))
584 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_BACK))
587 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_TASK))
592 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_X))
593 m.x = field->value[j];
594 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_Y))
595 m.y = field->value[j];
596 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_WHEEL))
597 m.wheel= field->value[j];
600 f_hid_queue_report((u8 *)&m, sizeof(m));
603 EXPORT_SYMBOL(f_hid_mouse_translate_report);
606 #undef MOUSE_REPORT_ID
607 #undef CONSUMER_REPORT_ID
610 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
612 struct f_hidg *hidg = file->private_data;
613 unsigned int ret = 0;
615 poll_wait(file, &hidg->read_queue, wait);
616 poll_wait(file, &hidg->write_queue, wait);
619 ret |= POLLOUT | POLLWRNORM;
622 ret |= POLLIN | POLLRDNORM;
630 static int f_hidg_release(struct inode *inode, struct file *fd)
632 fd->private_data = NULL;
636 static int f_hidg_open(struct inode *inode, struct file *fd)
638 struct f_hidg *hidg =
639 container_of(inode->i_cdev, struct f_hidg, cdev);
641 fd->private_data = hidg;
646 /*-------------------------------------------------------------------------*/
652 g_hidg->connected = 1;
655 void hidg_disconnect()
658 g_hidg->connected = 0;
661 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
663 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
665 struct f_hidg *hidg = (struct f_hidg *)req->context;
666 printk("hidg_set_report_complete ,req->status = %d len = %d\n",
667 req->status,req->actual);
668 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
672 spin_lock(&hidg->spinlock);
675 //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
678 hidg->set_report_buff = krealloc(hidg->set_report_buff,
679 req->actual, GFP_ATOMIC);
681 if (hidg->set_report_buff == NULL) {
682 spin_unlock(&hidg->spinlock);
685 hidg->set_report_length = req->actual;
686 memcpy(hidg->set_report_buff, req->buf, req->actual);
688 spin_unlock(&hidg->spinlock);
690 wake_up(&hidg->read_queue);
693 static int hidg_setup(struct usb_function *f,
694 const struct usb_ctrlrequest *ctrl)
696 struct f_hidg *hidg = func_to_hidg(f);
697 struct usb_composite_dev *cdev = f->config->cdev;
698 struct usb_request *req = cdev->req;
702 value = __le16_to_cpu(ctrl->wValue);
703 length = __le16_to_cpu(ctrl->wLength);
705 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
706 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
708 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
709 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
710 | HID_REQ_GET_REPORT):
711 VDBG(cdev, "get_report\n");
713 /* send an empty report */
714 length = min_t(unsigned, length, hidg->report_length);
715 memset(req->buf, 0x0, length);
720 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
721 | HID_REQ_GET_PROTOCOL):
722 VDBG(cdev, "get_protocol\n");
726 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
727 | HID_REQ_SET_REPORT):
728 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
730 req->complete = hidg_set_report_complete;
734 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
735 | HID_REQ_SET_PROTOCOL):
736 VDBG(cdev, "set_protocol\n");
740 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
741 | USB_REQ_GET_DESCRIPTOR):
742 switch (value >> 8) {
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 decriptor 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 int hidg_ctrlrequest(struct usb_composite_dev *cdev,
779 const struct usb_ctrlrequest *ctrl)
781 struct f_hidg *hidg = g_hidg;
782 struct usb_request *req = cdev->req;
786 value = __le16_to_cpu(ctrl->wValue);
787 length = __le16_to_cpu(ctrl->wLength);
790 printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
791 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
793 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
794 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
795 | HID_REQ_GET_REPORT):
796 VDBG(cdev, "get_report\n");
799 /* send an empty report */
800 length = min_t(unsigned, length, hidg->report_length);
801 memset(req->buf, 0x0, length);
805 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
806 | HID_REQ_GET_PROTOCOL):
807 VDBG(cdev, "get_protocol\n");
811 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
812 | HID_REQ_SET_REPORT):
813 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
815 req->complete = hidg_set_report_complete;
819 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
820 | HID_REQ_SET_PROTOCOL):
821 VDBG(cdev, "set_protocol\n");
825 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
826 | USB_REQ_GET_DESCRIPTOR):
827 switch (value >> 8) {
829 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
830 length = min_t(unsigned short, length,
831 hidg->report_desc_length);
832 memcpy(req->buf, hidg->report_desc, length);
837 VDBG(cdev, "Unknown decriptor request 0x%x\n",
845 VDBG(cdev, "Unknown request 0x%x\n",
856 req->length = length;
857 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
859 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
863 static void hidg_disable(struct usb_function *f)
865 struct f_hidg *hidg = func_to_hidg(f);
867 usb_ep_disable(hidg->in_ep);
868 hidg->in_ep->driver_data = NULL;
871 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
873 struct usb_composite_dev *cdev = f->config->cdev;
874 struct f_hidg *hidg = func_to_hidg(f);
875 const struct usb_endpoint_descriptor *ep_desc;
878 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
880 if (hidg->in_ep != NULL) {
881 /* restart endpoint */
882 if (hidg->in_ep->driver_data != NULL)
883 usb_ep_disable(hidg->in_ep);
885 ep_desc = ep_choose(f->config->cdev->gadget,
886 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
887 status = usb_ep_enable(hidg->in_ep, ep_desc);
889 //ERROR(cdev, "Enable endpoint FAILED!\n");
892 hidg->in_ep->driver_data = hidg;
898 const struct file_operations f_hidg_fops = {
899 .owner = THIS_MODULE,
901 .release = f_hidg_release,
902 .write = NULL,//f_hidg_write,disable write to /dev/hidg0
905 .llseek = noop_llseek,
908 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
910 struct usb_ep *ep_in;
911 struct f_hidg *hidg = func_to_hidg(f);
914 /* allocate instance-specific interface IDs, and patch descriptors */
915 status = usb_interface_id(c, f);
918 hidg_interface_desc.bInterfaceNumber = status;
920 /* allocate instance-specific endpoints */
922 ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
925 ep_in->driver_data = c->cdev; /* claim */
928 /* allocate out endpoint*/
929 ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
932 ep_out->driver_data = c->cdev; /* claim */
933 hidg->out_ep = ep_out;
935 printk("ep_out->name = %s\n",ep_out->name);
937 /* preallocate request and buffer */
939 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
944 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
948 /* set descriptor dynamic values */
949 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
950 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
951 // hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
952 // hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
953 // hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
954 // hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
955 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
956 hidg_desc.desc[0].wDescriptorLength =
957 cpu_to_le16(hidg->report_desc_length);
959 hidg->set_report_buff = NULL;
961 /* copy descriptors */
962 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
966 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
968 &hidg_fs_in_ep_desc);
970 if (gadget_is_dualspeed(c->cdev->gadget)) {
971 hidg_hs_in_ep_desc.bEndpointAddress =
972 hidg_fs_in_ep_desc.bEndpointAddress;
973 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
974 if (!f->hs_descriptors)
976 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
978 &hidg_hs_in_ep_desc);
980 hidg->hs_in_ep_desc = NULL;
985 mutex_init(&hidg->lock);
986 spin_lock_init(&hidg->spinlock);
987 init_waitqueue_head(&hidg->write_queue);
988 init_waitqueue_head(&hidg->read_queue);
990 /* create char device */
991 cdev_init(&hidg->cdev, &f_hidg_fops);
992 dev = MKDEV(major, hidg->minor);
993 status = cdev_add(&hidg->cdev, dev, 1);
997 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
1002 ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
1003 if (hidg->req != NULL) {
1004 kfree(hidg->req->buf);
1005 if (hidg->in_ep != NULL)
1006 usb_ep_free_request(hidg->in_ep, hidg->req);
1009 usb_free_descriptors(f->hs_descriptors);
1010 usb_free_descriptors(f->descriptors);
1015 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1017 struct f_hidg *hidg = func_to_hidg(f);
1019 f_hid_bypass_input_set(0);
1021 device_destroy(hidg_class, MKDEV(major, hidg->minor));
1022 cdev_del(&hidg->cdev);
1024 /* disable/free request and end point */
1025 usb_ep_disable(hidg->in_ep);
1026 usb_ep_dequeue(hidg->in_ep, hidg->req);
1028 kfree(hidg->req->buf);
1029 usb_ep_free_request(hidg->in_ep, hidg->req);
1031 /* free descriptors copies */
1032 usb_free_descriptors(f->hs_descriptors);
1033 usb_free_descriptors(f->descriptors);
1036 kfree(hidg->report_desc);
1037 kfree(hidg->set_report_buff);
1043 /*-------------------------------------------------------------------------*/
1046 #define CT_FUNC_HID_IDX 0
1048 static struct usb_string ct_func_string_defs[] = {
1049 [CT_FUNC_HID_IDX].s = "HID Interface",
1050 {}, /* end of list */
1053 static struct usb_gadget_strings ct_func_string_table = {
1054 .language = 0x0409, /* en-US */
1055 .strings = ct_func_string_defs,
1058 static struct usb_gadget_strings *ct_func_strings[] = {
1059 &ct_func_string_table,
1063 /*-------------------------------------------------------------------------*/
1064 /* usb_configuration */
1066 int hidg_bind_config(struct usb_configuration *c,
1067 const struct hidg_func_descriptor *fdesc, int index)
1069 struct f_hidg *hidg;
1071 if (index >= minors)
1074 /* maybe allocate device-global string IDs, and patch descriptors */
1075 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
1076 status = usb_string_id(c->cdev);
1079 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1080 hidg_interface_desc.iInterface = status;
1083 /* allocate and initialize one new instance */
1084 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1088 hidg->bypass_input = 0;
1089 hidg->minor = index;
1090 hidg->bInterfaceSubClass = fdesc->subclass;
1091 hidg->bInterfaceProtocol = fdesc->protocol;
1092 hidg->report_length = fdesc->report_length;
1093 hidg->report_desc_length = fdesc->report_desc_length;
1094 hidg->report_desc = kmemdup(fdesc->report_desc,
1095 fdesc->report_desc_length,
1097 if (!hidg->report_desc) {
1102 hidg->func.name = "hid";
1103 hidg->func.strings = ct_func_strings;
1104 hidg->func.bind = hidg_bind;
1105 hidg->func.unbind = hidg_unbind;
1106 hidg->func.set_alt = hidg_set_alt;
1107 hidg->func.disable = hidg_disable;
1108 hidg->func.setup = hidg_setup;
1110 status = usb_add_function(c, &hidg->func);
1118 int ghid_setup(struct usb_gadget *g, int count)
1123 hidg_class = class_create(THIS_MODULE, "hidg");
1125 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1134 void ghid_cleanup(void)
1137 unregister_chrdev_region(MKDEV(major, 0), minors);
1141 class_destroy(hidg_class);