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>
32 static int major, minors;
33 static struct class *hidg_class;
35 /*-------------------------------------------------------------------------*/
36 /* HID gadget struct */
40 unsigned char bInterfaceSubClass;
41 unsigned char bInterfaceProtocol;
42 unsigned short report_desc_length;
44 unsigned short report_length;
47 char *set_report_buff;
48 unsigned short set_report_length;
50 wait_queue_head_t read_queue;
51 struct usb_request *req_out;
56 wait_queue_head_t write_queue;
57 struct usb_request *req;
61 struct usb_function func;
63 struct usb_endpoint_descriptor *fs_in_ep_desc;
64 struct usb_endpoint_descriptor *hs_in_ep_desc;
65 struct usb_endpoint_descriptor *fs_out_ep_desc;
66 struct usb_endpoint_descriptor *hs_out_ep_desc;
68 struct usb_composite_dev *u_cdev;
71 /* 4 1 for boot protocol , 0 for report protocol */
77 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
79 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 */
241 struct f_hidg *g_hidg;
243 /*-------------------------------------------------------------------------*/
246 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
247 size_t count, loff_t *ptr)
249 struct f_hidg *hidg = file->private_data;
250 char *tmp_buff = NULL;
256 if (!access_ok(VERIFY_WRITE, buffer, count))
259 spin_lock_irqsave(&hidg->spinlock, flags);
261 #define READ_COND (hidg->set_report_buff != NULL)
264 spin_unlock_irqrestore(&hidg->spinlock, flags);
265 if (file->f_flags & O_NONBLOCK)
268 if (wait_event_interruptible(hidg->read_queue, READ_COND))
271 spin_lock_irqsave(&hidg->spinlock, flags);
274 count = min_t(unsigned, count, hidg->set_report_length);
275 tmp_buff = hidg->set_report_buff;
276 hidg->set_report_buff = NULL;
278 spin_unlock_irqrestore(&hidg->spinlock, flags);
280 if (tmp_buff != NULL) {
281 /* copy to user outside spinlock */
282 count -= copy_to_user(buffer, tmp_buff, count);
290 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
292 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
294 if (req->status != 0) {
298 wake_up(&hidg->write_queue);
301 #define WRITE_COND (!hidg->write_pending)
302 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
303 size_t count, loff_t *offp)
306 struct f_hidg *hidg = file->private_data;
307 ssize_t status = -ENOMEM;
309 if (!access_ok(VERIFY_READ, buffer, count))
312 mutex_lock(&hidg->lock);
314 #define WRITE_COND (!hidg->write_pending)
317 while (!WRITE_COND) {
318 mutex_unlock(&hidg->lock);
319 if (file->f_flags & O_NONBLOCK)
322 if (wait_event_interruptible_exclusive
323 (hidg->write_queue, WRITE_COND))
326 mutex_lock(&hidg->lock);
329 count = min_t(unsigned, count, hidg->report_length);
330 status = copy_from_user(hidg->req->buf, buffer, count);
333 /* ERROR(hidg->func.config->cdev, */
334 /* "copy_from_user error\n"); */
335 mutex_unlock(&hidg->lock);
339 hidg->req->status = 0;
341 hidg->req->length = count;
342 hidg->req->complete = f_hidg_req_complete;
343 hidg->req->context = hidg;
344 hidg->write_pending = 1;
346 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
348 /* ERROR(hidg->func.config->cdev,*/
349 /* "usb_ep_queue error on int endpoint %zd\n", status); */
350 hidg->write_pending = 0;
351 wake_up(&hidg->write_queue);
356 mutex_unlock(&hidg->lock);
361 static void f_hid_queue_report(u8 *data, int len)
363 /* this function will run in interrupt context */
364 ssize_t status = -ENOMEM;
365 struct f_hidg *hidg = g_hidg;
366 /* static char raw_report[8]; */
369 if (hidg->connected) {
370 /* mutex_lock(&hidg->lock); */
371 memcpy(hidg->req->buf, data, len);
372 hidg->req->status = 0;
374 hidg->req->length = len;
375 /* hidg->req->buf = raw_report; */
376 hidg->req->complete = f_hidg_req_complete;
377 hidg->req->context = hidg;
380 usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
382 /* printk("usb_ep_queue error on int endpoint %zd\n",
386 /* mutex_unlock(&hidg->lock); */
390 #define KBD_REPORT_ID (0x01)
391 #define MOUSE_REPORT_ID (0x02)
392 #define CONSUMER_REPORT_ID (0x03)
394 unsigned int f_hid_bypass_input_get()
402 EXPORT_SYMBOL(f_hid_bypass_input_get);
404 unsigned char kbd_idle[] = { KBD_REPORT_ID, 0, 0, 0, 0, 0, 0, 0, 0 };
405 unsigned char mouse_idle[] = { MOUSE_REPORT_ID, 0, 0, 0, 0, 0 };
406 unsigned char consumer_idle[] = { CONSUMER_REPORT_ID, 0, 0 };
408 static void f_hid_send_idle_report(void)
412 f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
414 f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
416 f_hid_queue_report(consumer_idle, sizeof(consumer_idle));
420 static void f_hid_bypass_input_set(u8 bypass)
424 u8 current_state = f_hid_bypass_input_get();
426 if (bypass && (!current_state)) {
429 if (!bypass && (current_state)) {
430 f_hid_send_idle_report();
438 g_hidg->u_cdev->gadget->ops->wakeup(g_hidg->u_cdev->gadget);
446 } __attribute__ ((packed));
448 struct consumer_report {
451 } __attribute__ ((packed));
453 void f_hid_kbd_translate_report(struct hid_report *report, u8 *data)
455 if (f_hid_bypass_input_get()) {
457 struct kbd_report k = { 0 };
458 struct consumer_report c = { 0 };
460 struct hid_field *field;
462 k.id = KBD_REPORT_ID; /* report id */
463 for (i = 0; i < report->maxfield; i++) {
464 field = report->field[i];
465 /* VARIABLE REPORT */
466 if (HID_MAIN_ITEM_VARIABLE & field->flags) {
467 for (j = 0; j < field->report_count; j++) {
468 if ((field->usage[j].type == EV_KEY)
469 && (field->usage[j].code ==
472 field->value[j] ? 1 << 0 :
474 if ((field->usage[j].type == EV_KEY)
475 && (field->usage[j].code ==
478 field->value[j] ? 1 << 1 :
480 if ((field->usage[j].type == EV_KEY)
481 && (field->usage[j].code ==
484 field->value[j] ? 1 << 2 :
486 if ((field->usage[j].type == EV_KEY)
487 && (field->usage[j].code ==
490 field->value[j] ? 1 << 3 :
492 if ((field->usage[j].type == EV_KEY)
493 && (field->usage[j].code ==
496 field->value[j] ? 1 << 4 :
498 if ((field->usage[j].type == EV_KEY)
499 && (field->usage[j].code ==
502 field->value[j] ? 1 << 5 :
504 if ((field->usage[j].type == EV_KEY)
505 && (field->usage[j].code ==
508 field->value[j] ? 1 << 6 :
510 if ((field->usage[j].type == EV_KEY)
511 && (field->usage[j].code ==
514 field->value[j] ? 1 << 7 :
519 if (field->application == HID_GD_KEYBOARD) {
521 j < (min(6, field->report_count));
527 if (field->application == 0x000c0001) {
529 for (j = 0; j < (field->report_count);
531 c.id = CONSUMER_REPORT_ID;
532 c.data = field->value[j];
533 f_hid_queue_report((u8 *)&c,
540 if (g_hidg->boot_protocol)
541 f_hid_queue_report((u8 *)&k + 1, sizeof(k) - 1);
544 f_hid_queue_report((u8 *)&k, sizeof(k));
549 EXPORT_SYMBOL(f_hid_kbd_translate_report);
551 struct mouse_report {
555 bool button_middle:1;
558 bool button_forward:1;
565 } __attribute__ ((packed));
567 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
570 if (f_hid_bypass_input_get() && !g_hidg->boot_protocol) {
571 struct mouse_report m = { 0 };
572 struct hid_field *field;
575 m.id = MOUSE_REPORT_ID;
576 for (i = 0; i < report->maxfield; i++) {
577 field = report->field[i];
578 for (j = 0; j < field->report_count; j++) {
579 if ((field->usage[j].type == EV_KEY)
580 && (field->usage[j].code == BTN_LEFT))
583 if ((field->usage[j].type == EV_KEY)
584 && (field->usage[j].code == BTN_RIGHT))
587 if ((field->usage[j].type == EV_KEY)
588 && (field->usage[j].code == BTN_MIDDLE))
591 if ((field->usage[j].type == EV_KEY)
592 && (field->usage[j].code == BTN_SIDE))
595 if ((field->usage[j].type == EV_KEY)
596 && (field->usage[j].code == BTN_EXTRA))
599 if ((field->usage[j].type == EV_KEY)
600 && (field->usage[j].code == BTN_FORWARD))
602 m.button_forward = 1;
603 if ((field->usage[j].type == EV_KEY)
604 && (field->usage[j].code == BTN_BACK))
607 if ((field->usage[j].type == EV_KEY)
608 && (field->usage[j].code == BTN_TASK))
612 if ((field->usage[j].type == EV_REL)
613 && (field->usage[j].code == REL_X))
614 m.x = field->value[j];
615 if ((field->usage[j].type == EV_REL)
616 && (field->usage[j].code == REL_Y))
617 m.y = field->value[j];
618 if ((field->usage[j].type == EV_REL)
619 && (field->usage[j].code == REL_WHEEL))
620 m.wheel = field->value[j];
623 if (m.button_right || m.button_left)
625 f_hid_queue_report((u8 *)&m, sizeof(m));
629 EXPORT_SYMBOL(f_hid_mouse_translate_report);
632 #undef MOUSE_REPORT_ID
633 #undef CONSUMER_REPORT_ID
635 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
643 static int f_hidg_release(struct inode *inode, struct file *fd)
645 fd->private_data = NULL;
649 static int f_hidg_open(struct inode *inode, struct file *fd)
651 struct f_hidg *hidg = container_of(inode->i_cdev, struct f_hidg, cdev);
653 fd->private_data = hidg;
658 /*-------------------------------------------------------------------------*/
664 g_hidg->connected = 1;
667 void hidg_disconnect()
670 g_hidg->connected = 0;
674 EXPORT_SYMBOL(hidg_disconnect);
675 int hidg_start(struct usb_composite_dev *cdev);
677 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
679 struct f_hidg *hidg = (struct f_hidg *)req->context;
680 printk("hidg_set_report_complete ,req->status = %d len = %d\n",
681 req->status, req->actual);
682 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
686 spin_lock(&hidg->spinlock);
688 if (!hidg->connected)
691 hidg->set_report_buff = krealloc(hidg->set_report_buff,
692 req->actual, GFP_ATOMIC);
694 if (hidg->set_report_buff == NULL) {
695 spin_unlock(&hidg->spinlock);
698 hidg->set_report_length = req->actual;
699 memcpy(hidg->set_report_buff, req->buf, req->actual);
701 spin_unlock(&hidg->spinlock);
703 wake_up(&hidg->read_queue);
706 static int hidg_setup(struct usb_function *f,
707 const struct usb_ctrlrequest *ctrl)
709 struct f_hidg *hidg = func_to_hidg(f);
710 struct usb_composite_dev *cdev = f->config->cdev;
711 struct usb_request *req = cdev->req;
715 value = __le16_to_cpu(ctrl->wValue);
716 length = __le16_to_cpu(ctrl->wLength);
718 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
719 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
721 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
722 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_GET_REPORT):
726 /* send an empty report */
727 length = min_t(unsigned, length, hidg->report_length);
728 memset(req->buf, 0x0, length);
733 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_GET_PROTOCOL):
739 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_SET_REPORT):
740 VDBG(cdev, "set_report | wLenght=%d\n",
743 req->complete = hidg_set_report_complete;
747 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_SET_PROTOCOL):
753 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 | USB_REQ_GET_DESCRIPTOR):
754 switch (value >> 8) {
756 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
757 length = min_t(unsigned short, length,
758 hidg->report_desc_length);
759 memcpy(req->buf, hidg->report_desc, length);
764 VDBG(cdev, "Unknown decriptor request 0x%x\n",
772 VDBG(cdev, "Unknown request 0x%x\n", ctrl->bRequest);
782 req->length = length;
783 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
784 /* if (status < 0) ; */
785 /* ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);*/
789 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
790 const struct usb_ctrlrequest *ctrl)
792 struct f_hidg *hidg = g_hidg;
793 struct usb_request *req = cdev->req;
797 value = __le16_to_cpu(ctrl->wValue);
798 length = __le16_to_cpu(ctrl->wLength);
801 printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
802 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
804 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
805 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_GET_REPORT):
808 return -EOPNOTSUPP; /* this command bypass to rndis */
809 /* send an empty report */
810 length = min_t(unsigned, length, hidg->report_length);
811 memset(req->buf, 0x0, length);
815 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_GET_PROTOCOL):
821 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_SET_REPORT):
822 VDBG(cdev, "set_report | wLenght=%d\n",
825 req->complete = hidg_set_report_complete;
829 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_SET_IDLE):
830 VDBG(cdev, "set_report | wLenght=%d\n",
833 req->complete = hidg_set_report_complete;
837 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | HID_REQ_SET_PROTOCOL):
841 req->complete = hidg_set_report_complete;
842 hidg->boot_protocol = 1;
847 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 | USB_REQ_GET_DESCRIPTOR):
848 switch (value >> 8) {
850 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
851 length = min_t(unsigned short, length,
852 hidg->report_desc_length);
853 memcpy(req->buf, hidg->report_desc, length);
854 hidg->boot_protocol = 0;
859 VDBG(cdev, "Unknown decriptor request 0x%x\n",
867 VDBG(cdev, "Unknown request 0x%x\n", ctrl->bRequest);
877 req->length = length;
878 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
879 /* if (status < 0) ; */
880 /* ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);*/
884 static void hidg_disable(struct usb_function *f)
886 struct f_hidg *hidg = func_to_hidg(f);
888 usb_ep_disable(hidg->in_ep);
889 hidg->in_ep->driver_data = NULL;
892 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
894 struct usb_composite_dev *cdev = f->config->cdev;
895 struct f_hidg *hidg = func_to_hidg(f);
896 const struct usb_endpoint_descriptor *ep_desc;
898 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
900 printk("^^^^^hidg_set_alt\n");
902 if (hidg->in_ep != NULL) {
903 /* restart endpoint */
904 if (hidg->in_ep->driver_data != NULL)
905 usb_ep_disable(hidg->in_ep);
907 ep_desc = ep_choose(f->config->cdev->gadget,
908 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
909 status = usb_ep_enable(hidg->in_ep, ep_desc);
911 /* ERROR(cdev, "Enable endpoint FAILED!\n"); */
914 hidg->in_ep->driver_data = hidg;
920 int hidg_start(struct usb_composite_dev *cdev)
922 printk("^^^^^hidg_start\n");
923 struct f_hidg *hidg = g_hidg;
924 const struct usb_endpoint_descriptor *ep_desc;
927 if (hidg->in_ep != NULL) {
928 /* restart endpoint */
929 if (hidg->in_ep->driver_data != NULL)
930 usb_ep_disable(hidg->in_ep);
932 ep_desc = ep_choose(cdev->gadget,
933 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
934 status = usb_ep_enable(hidg->in_ep, ep_desc);
936 printk("Enable endpoint FAILED!\n");
939 hidg->in_ep->driver_data = hidg;
945 const struct file_operations f_hidg_fops = {
946 .owner = THIS_MODULE,
948 .release = f_hidg_release,
949 .write = NULL, /* f_hidg_write,disable write to /dev/hidg0 */
951 .poll = NULL, /* f_hidg_poll, */
952 .llseek = noop_llseek,
955 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
958 struct usb_ep *ep_in;
959 struct f_hidg *hidg = func_to_hidg(f);
962 /* allocate instance-specific interface IDs, and patch descriptors */
963 status = usb_interface_id(c, f);
966 hidg_interface_desc.bInterfaceNumber = status;
968 /* allocate instance-specific endpoints */
970 ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
973 ep_in->driver_data = c->cdev; /* claim */
976 /* preallocate request and buffer */
978 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
982 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
986 /* set descriptor dynamic values */
987 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
988 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
989 /* hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); */
990 /* hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); */
991 /* hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); */
992 /* hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); */
993 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
994 hidg_desc.desc[0].wDescriptorLength =
995 cpu_to_le16(hidg->report_desc_length);
997 hidg->set_report_buff = NULL;
999 /* copy descriptors */
1000 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
1001 if (!f->descriptors)
1004 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
1006 &hidg_fs_in_ep_desc);
1008 if (gadget_is_dualspeed(c->cdev->gadget)) {
1009 hidg_hs_in_ep_desc.bEndpointAddress =
1010 hidg_fs_in_ep_desc.bEndpointAddress;
1011 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
1012 if (!f->hs_descriptors)
1014 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
1016 &hidg_hs_in_ep_desc);
1018 hidg->hs_in_ep_desc = NULL;
1021 hidg->connected = 0;
1023 mutex_init(&hidg->lock);
1024 spin_lock_init(&hidg->spinlock);
1025 init_waitqueue_head(&hidg->write_queue);
1026 init_waitqueue_head(&hidg->read_queue);
1028 /* create char device */
1029 cdev_init(&hidg->cdev, &f_hidg_fops);
1030 dev = MKDEV(major, hidg->minor);
1031 status = cdev_add(&hidg->cdev, dev, 1);
1035 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
1040 /* ERROR(f->config->cdev, "hidg_bind FAILED\n"); */
1041 if (hidg->req != NULL) {
1042 kfree(hidg->req->buf);
1043 if (hidg->in_ep != NULL)
1044 usb_ep_free_request(hidg->in_ep, hidg->req);
1047 usb_free_descriptors(f->hs_descriptors);
1048 usb_free_descriptors(f->descriptors);
1053 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1055 struct f_hidg *hidg = func_to_hidg(f);
1058 device_destroy(hidg_class, MKDEV(major, hidg->minor));
1059 cdev_del(&hidg->cdev);
1061 /* disable/free request and end point */
1062 usb_ep_disable(hidg->in_ep);
1063 usb_ep_dequeue(hidg->in_ep, hidg->req);
1065 kfree(hidg->req->buf);
1066 usb_ep_free_request(hidg->in_ep, hidg->req);
1068 /* free descriptors copies */
1069 usb_free_descriptors(f->hs_descriptors);
1070 usb_free_descriptors(f->descriptors);
1072 kfree(hidg->report_desc);
1073 kfree(hidg->set_report_buff);
1079 /*-------------------------------------------------------------------------*/
1082 #define CT_FUNC_HID_IDX 0
1084 static struct usb_string ct_func_string_defs[] = {
1085 [CT_FUNC_HID_IDX].s = "HID Interface",
1086 {}, /* end of list */
1089 static struct usb_gadget_strings ct_func_string_table = {
1090 .language = 0x0409, /* en-US */
1091 .strings = ct_func_string_defs,
1094 static struct usb_gadget_strings *ct_func_strings[] = {
1095 &ct_func_string_table,
1099 /*-------------------------------------------------------------------------*/
1100 /* usb_configuration */
1102 int hidg_bind_config(struct usb_configuration *c,
1103 const struct hidg_func_descriptor *fdesc, int index)
1105 struct f_hidg *hidg;
1107 if (index >= minors)
1110 /* maybe allocate device-global string IDs, and patch descriptors */
1111 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
1112 status = usb_string_id(c->cdev);
1115 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1116 hidg_interface_desc.iInterface = status;
1119 /* allocate and initialize one new instance */
1120 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1124 hidg->boot_protocol = 1;
1125 hidg->connected = 0;
1126 hidg->minor = index;
1127 hidg->bInterfaceSubClass = fdesc->subclass;
1128 hidg->bInterfaceProtocol = fdesc->protocol;
1129 hidg->report_length = fdesc->report_length;
1130 hidg->report_desc_length = fdesc->report_desc_length;
1131 hidg->report_desc = kmemdup(fdesc->report_desc,
1132 fdesc->report_desc_length, GFP_KERNEL);
1133 if (!hidg->report_desc) {
1138 hidg->func.name = "hid";
1139 hidg->func.strings = ct_func_strings;
1140 hidg->func.bind = hidg_bind;
1141 hidg->func.unbind = hidg_unbind;
1142 hidg->func.set_alt = hidg_set_alt;
1143 hidg->func.disable = hidg_disable;
1144 hidg->func.setup = hidg_setup;
1146 status = usb_add_function(c, &hidg->func);
1151 g_hidg->u_cdev = c->cdev;
1155 int ghid_setup(struct usb_gadget *g, int count)
1160 hidg_class = class_create(THIS_MODULE, "hidg");
1162 status = alloc_chrdev_region(&dev, 0, count, "hidg");
1171 void ghid_cleanup(void)
1174 unregister_chrdev_region(MKDEV(major, 0), minors);
1178 class_destroy(hidg_class);