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 = 156,
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, 0x03, /* USAGE_MAXIMUM (Button 3) */
238 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
239 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
240 0x75, 0x01, /* REPORT_SIZE (1) */
241 0x95, 0x03, /* REPORT_COUNT (3) */
242 0x81, 0x02, /* INPUT (Data,Var,Abs) */
243 0x75, 0x05, /* REPORT_SIZE (5) */
244 0x95, 0x01, /* REPORT_COUNT (1) */
245 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */
246 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
247 0x09, 0x30, /* USAGE (X) */
248 0x09, 0x31, /* USAGE (Y) */
249 0x16, 0x01, 0xF8, /* LOGICAL_MINIMUM (-2047) */
250 0x26, 0xFF, 0x07, /* LOGICAL_MAXIMUM (2047) */
251 0x75, 0x0C, /* REPORT_SIZE (12) */
252 0x95, 0x02, /* REPORT_COUNT (2) */
253 0x81, 0x06, /* INPUT (Data,Var,Rel) */
255 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
256 0x25, 0x7F, /* LOGICAL_MAXIMUM (127) */
257 0x75, 0x08, /* REPORT_SIZE (8) */
258 0x95, 0x01, /* REPORT_COUNT (1) */
259 0x81, 0x06, /* INPUT (Data,Var,Rel) */
260 0xC0 , /* END_COLLECTION */
262 0xC0 /* END_COLLECTION */
267 struct f_hidg *g_hidg;
269 /*-------------------------------------------------------------------------*/
272 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
273 size_t count, loff_t *ptr)
275 struct f_hidg *hidg = file->private_data;
276 char *tmp_buff = NULL;
282 if (!access_ok(VERIFY_WRITE, buffer, count))
285 spin_lock_irqsave(&hidg->spinlock, flags);
287 #define READ_COND (hidg->set_report_buff != NULL)
290 spin_unlock_irqrestore(&hidg->spinlock, flags);
291 if (file->f_flags & O_NONBLOCK)
294 if (wait_event_interruptible(hidg->read_queue, READ_COND))
297 spin_lock_irqsave(&hidg->spinlock, flags);
301 count = min_t(unsigned, count, hidg->set_report_length);
302 tmp_buff = hidg->set_report_buff;
303 hidg->set_report_buff = NULL;
305 spin_unlock_irqrestore(&hidg->spinlock, flags);
307 if (tmp_buff != NULL) {
308 /* copy to user outside spinlock */
309 count -= copy_to_user(buffer, tmp_buff, count);
317 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
319 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
321 if (req->status != 0) {
322 //ERROR(hidg->func.config->cdev,
323 // "End Point Request ERROR: %d\n", req->status);
326 hidg->write_pending = 0;
327 wake_up(&hidg->write_queue);
330 #define WRITE_COND (!hidg->write_pending)
331 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
332 size_t count, loff_t *offp)
335 struct f_hidg *hidg = file->private_data;
336 ssize_t status = -ENOMEM;
338 if (!access_ok(VERIFY_READ, buffer, count))
341 mutex_lock(&hidg->lock);
343 #define WRITE_COND (!hidg->write_pending)
346 while (!WRITE_COND) {
347 mutex_unlock(&hidg->lock);
348 if (file->f_flags & O_NONBLOCK)
351 if (wait_event_interruptible_exclusive(
352 hidg->write_queue, WRITE_COND))
355 mutex_lock(&hidg->lock);
358 count = min_t(unsigned, count, hidg->report_length);
359 status = copy_from_user(hidg->req->buf, buffer, count);
362 //ERROR(hidg->func.config->cdev,
363 // "copy_from_user error\n");
364 mutex_unlock(&hidg->lock);
368 hidg->req->status = 0;
370 hidg->req->length = count;
371 hidg->req->complete = f_hidg_req_complete;
372 hidg->req->context = hidg;
373 hidg->write_pending = 1;
375 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
377 //ERROR(hidg->func.config->cdev,
378 // "usb_ep_queue error on int endpoint %zd\n", status);
379 hidg->write_pending = 0;
380 wake_up(&hidg->write_queue);
385 mutex_unlock(&hidg->lock);
389 static void f_hid_queue_report(u8 *data, int len)
391 //this function will run in interrupt context
392 ssize_t status = -ENOMEM;
393 struct f_hidg *hidg = g_hidg;
394 //static char raw_report[8];
398 //mutex_lock(&hidg->lock);
399 memcpy(hidg->req->buf, data, len);
400 hidg->req->status = 0;
402 hidg->req->length = len;
403 //hidg->req->buf = raw_report;
404 hidg->req->complete = f_hidg_req_complete;
405 hidg->req->context = hidg;
407 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
409 printk("usb_ep_queue error on int endpoint %zd\n", status);
412 //mutex_unlock(&hidg->lock);
417 #define KBD_REPORT_ID (0x01)
418 #define MOUSE_REPORT_ID (0x02)
419 #define CONSUMER_REPORT_ID (0x03)
421 unsigned int f_hid_bypass_input_get()
426 return g_hidg->bypass_input;
428 EXPORT_SYMBOL(f_hid_bypass_input_get);
430 unsigned char kbd_idle[] = {KBD_REPORT_ID,0,0,0,0,0,0,0,0};
431 unsigned char mouse_idle[] = {MOUSE_REPORT_ID,0,0,0,0,0};
432 unsigned char consumer_idle[]= {CONSUMER_REPORT_ID,0,0};
434 static void f_hid_send_idle_report(void)
438 f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
440 f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
442 f_hid_queue_report(consumer_idle, sizeof(consumer_idle));
446 static void f_hid_bypass_input_set(u8 bypass)
450 u8 current_state = f_hid_bypass_input_get();
452 if( bypass && (!current_state))
454 g_hidg->bypass_input = 1;
456 if(!bypass && (current_state))
458 f_hid_send_idle_report();
459 g_hidg->bypass_input = 0;
470 }__attribute__ ((packed));
472 struct consumer_report {
475 }__attribute__ ((packed));
477 void f_hid_kbd_translate_report(struct hid_report *report, u8 *data)
479 if(f_hid_bypass_input_get())
482 struct kbd_report k = {0};
483 struct consumer_report c = {0};
485 struct hid_field *field;
487 k.id = KBD_REPORT_ID;//report id
488 for (i = 0; i < report->maxfield; i++){
489 field = report->field[i];
490 if(HID_MAIN_ITEM_VARIABLE & field->flags)//VARIABLE REPORT
492 for(j = 0; j < field->report_count; j++)
494 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTCTRL))
495 k.command |= field->value[j] ? 1 << 0 : 0 ;
496 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTSHIFT))
497 k.command |= field->value[j] ? 1 << 1 : 0 ;
498 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTALT))
499 k.command |= field->value[j] ? 1 << 2 : 0 ;
500 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTMETA))
501 k.command |= field->value[j] ? 1 << 3 : 0 ;
502 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTCTRL))
503 k.command |= field->value[j] ? 1 << 4 : 0 ;
504 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTSHIFT))
505 k.command |= field->value[j] ? 1 << 5 : 0 ;
506 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTALT))
507 k.command |= field->value[j] ? 1 << 6 : 0 ;
508 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTMETA))
509 k.command |= field->value[j] ? 1 << 7 : 0 ;
514 if(field->application == HID_GD_KEYBOARD)
516 for(j = 0 ; j<(min(6,field->report_count)); j++)
518 k.key_array[j] = field->value[j];
521 if(field->application == 0x000c0001)//CONSUMER PAGE
523 for(j = 0 ; j < (field->report_count); j++)
525 c.id = CONSUMER_REPORT_ID;
526 c.data = field->value[j];
527 f_hid_queue_report((u8 *)&c, sizeof(c));
533 f_hid_queue_report((u8 *)&k, sizeof(k));
536 EXPORT_SYMBOL(f_hid_kbd_translate_report);
538 struct mouse_report {
547 }__attribute__ ((packed));
550 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
553 if(f_hid_bypass_input_get())
555 struct mouse_report m = {0};
556 struct hid_field *field;
559 m.id = MOUSE_REPORT_ID;
560 for (i = 0; i < report->maxfield; i++){
561 field = report->field[i];
562 for(j=0; j<field->report_count; j++)
564 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_LEFT))
567 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_RIGHT))
570 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_MIDDLE))
574 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_X))
575 m.x = field->value[j];
576 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_Y))
577 m.y = field->value[j];
578 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_WHEEL))
579 m.wheel= field->value[j];
582 f_hid_queue_report((u8 *)&m, sizeof(m));
585 EXPORT_SYMBOL(f_hid_mouse_translate_report);
588 #undef MOUSE_REPORT_ID
589 #undef CONSUMER_REPORT_ID
592 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
594 struct f_hidg *hidg = file->private_data;
595 unsigned int ret = 0;
597 poll_wait(file, &hidg->read_queue, wait);
598 poll_wait(file, &hidg->write_queue, wait);
601 ret |= POLLOUT | POLLWRNORM;
604 ret |= POLLIN | POLLRDNORM;
612 static int f_hidg_release(struct inode *inode, struct file *fd)
614 fd->private_data = NULL;
618 static int f_hidg_open(struct inode *inode, struct file *fd)
620 struct f_hidg *hidg =
621 container_of(inode->i_cdev, struct f_hidg, cdev);
623 fd->private_data = hidg;
628 /*-------------------------------------------------------------------------*/
634 g_hidg->connected = 1;
637 void hidg_disconnect()
640 g_hidg->connected = 0;
643 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
645 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
647 struct f_hidg *hidg = (struct f_hidg *)req->context;
648 printk("hidg_set_report_complete ,req->status = %d len = %d\n",
649 req->status,req->actual);
650 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
654 spin_lock(&hidg->spinlock);
657 //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
660 hidg->set_report_buff = krealloc(hidg->set_report_buff,
661 req->actual, GFP_ATOMIC);
663 if (hidg->set_report_buff == NULL) {
664 spin_unlock(&hidg->spinlock);
667 hidg->set_report_length = req->actual;
668 memcpy(hidg->set_report_buff, req->buf, req->actual);
670 spin_unlock(&hidg->spinlock);
672 wake_up(&hidg->read_queue);
675 static int hidg_setup(struct usb_function *f,
676 const struct usb_ctrlrequest *ctrl)
678 struct f_hidg *hidg = func_to_hidg(f);
679 struct usb_composite_dev *cdev = f->config->cdev;
680 struct usb_request *req = cdev->req;
684 value = __le16_to_cpu(ctrl->wValue);
685 length = __le16_to_cpu(ctrl->wLength);
687 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
688 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
690 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
691 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
692 | HID_REQ_GET_REPORT):
693 VDBG(cdev, "get_report\n");
695 /* send an empty report */
696 length = min_t(unsigned, length, hidg->report_length);
697 memset(req->buf, 0x0, length);
702 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
703 | HID_REQ_GET_PROTOCOL):
704 VDBG(cdev, "get_protocol\n");
708 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
709 | HID_REQ_SET_REPORT):
710 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
712 req->complete = hidg_set_report_complete;
716 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
717 | HID_REQ_SET_PROTOCOL):
718 VDBG(cdev, "set_protocol\n");
722 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
723 | USB_REQ_GET_DESCRIPTOR):
724 switch (value >> 8) {
726 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
727 length = min_t(unsigned short, length,
728 hidg->report_desc_length);
729 memcpy(req->buf, hidg->report_desc, length);
734 VDBG(cdev, "Unknown decriptor request 0x%x\n",
742 VDBG(cdev, "Unknown request 0x%x\n",
753 req->length = length;
754 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
756 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
760 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
761 const struct usb_ctrlrequest *ctrl)
763 struct f_hidg *hidg = g_hidg;
764 struct usb_request *req = cdev->req;
768 value = __le16_to_cpu(ctrl->wValue);
769 length = __le16_to_cpu(ctrl->wLength);
772 printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
773 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
775 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
776 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
777 | HID_REQ_GET_REPORT):
778 VDBG(cdev, "get_report\n");
780 /* send an empty report */
781 length = min_t(unsigned, length, hidg->report_length);
782 memset(req->buf, 0x0, length);
787 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
788 | HID_REQ_GET_PROTOCOL):
789 VDBG(cdev, "get_protocol\n");
793 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
794 | HID_REQ_SET_REPORT):
795 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
797 req->complete = hidg_set_report_complete;
801 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
802 | HID_REQ_SET_PROTOCOL):
803 VDBG(cdev, "set_protocol\n");
807 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
808 | USB_REQ_GET_DESCRIPTOR):
809 switch (value >> 8) {
811 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
812 length = min_t(unsigned short, length,
813 hidg->report_desc_length);
814 memcpy(req->buf, hidg->report_desc, length);
819 VDBG(cdev, "Unknown decriptor request 0x%x\n",
827 VDBG(cdev, "Unknown request 0x%x\n",
838 req->length = length;
839 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
841 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
845 static void hidg_disable(struct usb_function *f)
847 struct f_hidg *hidg = func_to_hidg(f);
849 usb_ep_disable(hidg->in_ep);
850 hidg->in_ep->driver_data = NULL;
853 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
855 struct usb_composite_dev *cdev = f->config->cdev;
856 struct f_hidg *hidg = func_to_hidg(f);
857 const struct usb_endpoint_descriptor *ep_desc;
860 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
862 if (hidg->in_ep != NULL) {
863 /* restart endpoint */
864 if (hidg->in_ep->driver_data != NULL)
865 usb_ep_disable(hidg->in_ep);
867 ep_desc = ep_choose(f->config->cdev->gadget,
868 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
869 status = usb_ep_enable(hidg->in_ep, ep_desc);
871 //ERROR(cdev, "Enable endpoint FAILED!\n");
874 hidg->in_ep->driver_data = hidg;
880 const struct file_operations f_hidg_fops = {
881 .owner = THIS_MODULE,
883 .release = f_hidg_release,
884 .write = NULL,//f_hidg_write,disable write to /dev/hidg0
887 .llseek = noop_llseek,
890 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
892 struct usb_ep *ep_in;
893 struct f_hidg *hidg = func_to_hidg(f);
896 /* allocate instance-specific interface IDs, and patch descriptors */
897 status = usb_interface_id(c, f);
900 hidg_interface_desc.bInterfaceNumber = status;
902 /* allocate instance-specific endpoints */
904 ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
907 ep_in->driver_data = c->cdev; /* claim */
910 /* allocate out endpoint*/
911 ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
914 ep_out->driver_data = c->cdev; /* claim */
915 hidg->out_ep = ep_out;
917 printk("ep_out->name = %s\n",ep_out->name);
919 /* preallocate request and buffer */
921 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
926 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
930 /* set descriptor dynamic values */
931 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
932 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
933 // hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
934 // hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
935 // hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
936 // hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
937 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
938 hidg_desc.desc[0].wDescriptorLength =
939 cpu_to_le16(hidg->report_desc_length);
941 hidg->set_report_buff = NULL;
943 /* copy descriptors */
944 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
948 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
950 &hidg_fs_in_ep_desc);
952 if (gadget_is_dualspeed(c->cdev->gadget)) {
953 hidg_hs_in_ep_desc.bEndpointAddress =
954 hidg_fs_in_ep_desc.bEndpointAddress;
955 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
956 if (!f->hs_descriptors)
958 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
960 &hidg_hs_in_ep_desc);
962 hidg->hs_in_ep_desc = NULL;
967 mutex_init(&hidg->lock);
968 spin_lock_init(&hidg->spinlock);
969 init_waitqueue_head(&hidg->write_queue);
970 init_waitqueue_head(&hidg->read_queue);
972 /* create char device */
973 cdev_init(&hidg->cdev, &f_hidg_fops);
974 dev = MKDEV(major, hidg->minor);
975 status = cdev_add(&hidg->cdev, dev, 1);
979 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
984 ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
985 if (hidg->req != NULL) {
986 kfree(hidg->req->buf);
987 if (hidg->in_ep != NULL)
988 usb_ep_free_request(hidg->in_ep, hidg->req);
991 usb_free_descriptors(f->hs_descriptors);
992 usb_free_descriptors(f->descriptors);
997 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
999 struct f_hidg *hidg = func_to_hidg(f);
1001 f_hid_bypass_input_set(0);
1003 device_destroy(hidg_class, MKDEV(major, hidg->minor));
1004 cdev_del(&hidg->cdev);
1006 /* disable/free request and end point */
1007 usb_ep_disable(hidg->in_ep);
1008 usb_ep_dequeue(hidg->in_ep, hidg->req);
1010 kfree(hidg->req->buf);
1011 usb_ep_free_request(hidg->in_ep, hidg->req);
1013 /* free descriptors copies */
1014 usb_free_descriptors(f->hs_descriptors);
1015 usb_free_descriptors(f->descriptors);
1018 kfree(hidg->report_desc);
1019 kfree(hidg->set_report_buff);
1025 /*-------------------------------------------------------------------------*/
1028 #define CT_FUNC_HID_IDX 0
1030 static struct usb_string ct_func_string_defs[] = {
1031 [CT_FUNC_HID_IDX].s = "HID Interface",
1032 {}, /* end of list */
1035 static struct usb_gadget_strings ct_func_string_table = {
1036 .language = 0x0409, /* en-US */
1037 .strings = ct_func_string_defs,
1040 static struct usb_gadget_strings *ct_func_strings[] = {
1041 &ct_func_string_table,
1045 /*-------------------------------------------------------------------------*/
1046 /* usb_configuration */
1048 int hidg_bind_config(struct usb_configuration *c,
1049 const struct hidg_func_descriptor *fdesc, int index)
1051 struct f_hidg *hidg;
1053 if (index >= minors)
1056 /* maybe allocate device-global string IDs, and patch descriptors */
1057 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
1058 status = usb_string_id(c->cdev);
1061 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1062 hidg_interface_desc.iInterface = status;
1065 /* allocate and initialize one new instance */
1066 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1070 hidg->bypass_input = 0;
1071 hidg->minor = index;
1072 hidg->bInterfaceSubClass = fdesc->subclass;
1073 hidg->bInterfaceProtocol = fdesc->protocol;
1074 hidg->report_length = fdesc->report_length;
1075 hidg->report_desc_length = fdesc->report_desc_length;
1076 hidg->report_desc = kmemdup(fdesc->report_desc,
1077 fdesc->report_desc_length,
1079 if (!hidg->report_desc) {
1084 hidg->func.name = "hid";
1085 hidg->func.strings = ct_func_strings;
1086 hidg->func.bind = hidg_bind;
1087 hidg->func.unbind = hidg_unbind;
1088 hidg->func.set_alt = hidg_set_alt;
1089 hidg->func.disable = hidg_disable;
1090 hidg->func.setup = hidg_setup;
1092 status = usb_add_function(c, &hidg->func);
1100 int ghid_setup(struct usb_gadget *g, int count)
1105 hidg_class = class_create(THIS_MODULE, "hidg");
1107 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1116 void ghid_cleanup(void)
1119 unregister_chrdev_region(MKDEV(major, 0), minors);
1123 class_destroy(hidg_class);