add function hidg to android gadget
[firefly-linux-kernel-4.4.55.git] / drivers / usb / gadget / f_hid_rk.c
1 /*
2  * f_hid.c -- USB HID function driver
3  *
4  * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
5  *
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.
10  *
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.
15  *
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
19  */
20
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>
31
32
33
34 static int major, minors;
35 static struct class *hidg_class;
36
37 /*-------------------------------------------------------------------------*/
38 /*                            HID gadget struct                            */
39
40 struct f_hidg {
41         /* configuration */
42         unsigned char                   bInterfaceSubClass;
43         unsigned char                   bInterfaceProtocol;
44         unsigned short                  report_desc_length;
45         char                            *report_desc;
46         unsigned short                  report_length;
47
48         /* recv report */
49         char                            *set_report_buff;
50         unsigned short                  set_report_length;
51         spinlock_t                      spinlock;
52         wait_queue_head_t               read_queue;
53         struct usb_request              *req_out;
54
55         /* send report */
56         struct mutex                    lock;
57         bool                            write_pending;
58         wait_queue_head_t               write_queue;
59         struct usb_request              *req;
60
61         int                             minor;
62         struct cdev                     cdev;
63         struct usb_function             func;
64         struct usb_ep                   *in_ep;
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;
69
70         unsigned short  bypass_input;
71         bool connected;
72 };
73
74 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
75 {
76         return container_of(f, struct f_hidg, func);
77 }
78
79 /*-------------------------------------------------------------------------*/
80 /*                           Static descriptors                            */
81
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,
87         .bNumEndpoints          = 1,
88         .bInterfaceClass        = USB_CLASS_HID,
89         /* .bInterfaceSubClass  = DYNAMIC */
90         /* .bInterfaceProtocol  = DYNAMIC */
91         /* .iInterface          = DYNAMIC */
92 };
93
94 static struct hid_descriptor hidg_desc = {
95         .bLength                        = sizeof hidg_desc,
96         .bDescriptorType                = HID_DT_HID,
97         .bcdHID                         = 0x0110, //0x0101,
98         .bCountryCode                   = 0x00,
99         .bNumDescriptors                = 0x1,
100         /*.desc[0].bDescriptorType      = DYNAMIC */
101         /*.desc[0].wDescriptorLenght    = DYNAMIC */
102 };
103
104 /* High-Speed Support */
105
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)
115                                       */
116 };
117 #if 0
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)
127                                       */
128 };
129 #endif
130
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,
136         NULL,
137 };
138
139 /* Full-Speed Support */
140
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)
150                                        */
151 };
152 #if 0
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)
162                                       */
163 };
164 #endif
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,
170         NULL,
171 };
172
173 struct f_hidg *g_hidg;
174
175 /*-------------------------------------------------------------------------*/
176 /*                              Char Device                                */
177
178 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
179                         size_t count, loff_t *ptr)
180 {
181         struct f_hidg   *hidg     = file->private_data;
182         char            *tmp_buff = NULL;
183         unsigned long   flags;
184
185         if (!count)
186                 return 0;
187
188         if (!access_ok(VERIFY_WRITE, buffer, count))
189                 return -EFAULT;
190
191         spin_lock_irqsave(&hidg->spinlock, flags);
192
193 #define READ_COND (hidg->set_report_buff != NULL)
194
195         while (!READ_COND) {
196                 spin_unlock_irqrestore(&hidg->spinlock, flags);
197                 if (file->f_flags & O_NONBLOCK)
198                         return -EAGAIN;
199
200                 if (wait_event_interruptible(hidg->read_queue, READ_COND))
201                         return -ERESTARTSYS;
202
203                 spin_lock_irqsave(&hidg->spinlock, flags);
204         }
205
206
207         count = min_t(unsigned, count, hidg->set_report_length);
208         tmp_buff = hidg->set_report_buff;
209         hidg->set_report_buff = NULL;
210
211         spin_unlock_irqrestore(&hidg->spinlock, flags);
212
213         if (tmp_buff != NULL) {
214                 /* copy to user outside spinlock */
215                 count -= copy_to_user(buffer, tmp_buff, count);
216                 kfree(tmp_buff);
217         } else
218                 count = -ENOMEM;
219
220         return count;
221 }
222
223 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
224 {
225         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
226
227         if (req->status != 0) {
228                 //ERROR(hidg->func.config->cdev,
229                 //      "End Point Request ERROR: %d\n", req->status);
230         }
231
232         hidg->write_pending = 0;
233         wake_up(&hidg->write_queue);
234 }
235
236 #define WRITE_COND (!hidg->write_pending)
237 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
238                             size_t count, loff_t *offp)
239 {
240 #if 0
241         struct f_hidg *hidg  = file->private_data;
242         ssize_t status = -ENOMEM;
243
244         if (!access_ok(VERIFY_READ, buffer, count))
245                 return -EFAULT;
246
247         mutex_lock(&hidg->lock);
248
249 #define WRITE_COND (!hidg->write_pending)
250
251         /* write queue */
252         while (!WRITE_COND) {
253                 mutex_unlock(&hidg->lock);
254                 if (file->f_flags & O_NONBLOCK)
255                         return -EAGAIN;
256
257                 if (wait_event_interruptible_exclusive(
258                                 hidg->write_queue, WRITE_COND))
259                         return -ERESTARTSYS;
260
261                 mutex_lock(&hidg->lock);
262         }
263
264         count  = min_t(unsigned, count, hidg->report_length);
265         status = copy_from_user(hidg->req->buf, buffer, count);
266
267         if (status != 0) {
268                 //ERROR(hidg->func.config->cdev,
269                 //      "copy_from_user error\n");
270                 mutex_unlock(&hidg->lock);
271                 return -EINVAL;
272         }
273
274         hidg->req->status   = 0;
275         hidg->req->zero     = 0;
276         hidg->req->length   = count;
277         hidg->req->complete = f_hidg_req_complete;
278         hidg->req->context  = hidg;
279         hidg->write_pending = 1;
280
281         status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
282         if (status < 0) {
283                 //ERROR(hidg->func.config->cdev,
284                 //      "usb_ep_queue error on int endpoint %zd\n", status);
285                 hidg->write_pending = 0;
286                 wake_up(&hidg->write_queue);
287         } else {
288                 status = count;
289         }
290
291         mutex_unlock(&hidg->lock);
292 #endif
293         return count;
294 }
295 static void f_hid_queue_report(u8 *data, int len)
296 {
297     //this function will run in interrupt context 
298     ssize_t status = -ENOMEM;
299     struct f_hidg *hidg = g_hidg;
300     //static char raw_report[8];
301     
302     if(hidg){
303         if(hidg->connected){
304             //mutex_lock(&hidg->lock);
305             memcpy(hidg->req->buf, data, len);
306                 hidg->req->status   = 0;
307                 hidg->req->zero     = 0;
308                 hidg->req->length   = len;
309                 //hidg->req->buf      = raw_report;
310                 hidg->req->complete = f_hidg_req_complete;
311                 hidg->req->context  = hidg;
312
313                 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
314                 if (status < 0) {
315                         printk("usb_ep_queue error on int endpoint %zd\n", status);
316                 }
317             }
318     //mutex_unlock(&hidg->lock);
319     }
320 }
321
322
323 #define KBD_REPORT_ID (0x01)
324 #define MOUSE_REPORT_ID (0x02)
325
326 unsigned int f_hid_bypass_input_get()
327 {
328     if(!g_hidg)
329         return 0;
330     else
331         return g_hidg->bypass_input;
332 }
333 EXPORT_SYMBOL(f_hid_bypass_input_get);
334
335 unsigned char kbd_idle[]   = {KBD_REPORT_ID,0,0,0,0,0,0,0,0};
336 unsigned char mouse_idle[] = {MOUSE_REPORT_ID,0,0,0,0,0};
337
338 static void f_hid_send_idle_report(void)
339 {
340     if(g_hidg){
341         mdelay(2);
342         f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
343         mdelay(2);
344         f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
345     }
346 }
347
348 static void f_hid_bypass_input_set(u8 bypass)
349 {
350     if(g_hidg){
351
352         u8 current_state = f_hid_bypass_input_get();
353
354         if( bypass && (!current_state))
355         {
356             g_hidg->bypass_input = 1;
357         }
358         if(!bypass && (current_state))
359         {
360             f_hid_send_idle_report();
361             g_hidg->bypass_input = 0;
362         }
363     }
364 }
365
366 struct kbd_report {
367     u8        id:8;
368     u8        raw_report[8];
369 }__attribute__ ((packed));
370
371 void f_hid_kbd_translate_report(u8 *data, int len)
372 {
373     if(f_hid_bypass_input_get())
374     {
375         struct kbd_report  k = {0};
376         k.id = KBD_REPORT_ID;//report id
377         memcpy(k.raw_report, data, len);//Byte 1 for report id
378
379         f_hid_queue_report((u8 *)&k, sizeof(k));
380     }
381 }
382 EXPORT_SYMBOL(f_hid_kbd_translate_report);
383
384 struct mouse_report {
385     u8        id:8;
386     u8        button:8;
387     signed    x :12;
388     signed    y :12;
389     s8        wheel:8;
390 }__attribute__ ((packed));
391
392
393 void f_hid_mouse_translate_report(struct hid_report *report , u8 *data)
394 {
395
396     if(f_hid_bypass_input_get())
397     {
398         struct mouse_report m = {0};
399         struct hid_field *field;
400         
401         s32 mouse_rel[2] = { 0 };//0 for x, 1 for y
402         s32 mouse_wheel  = 0;
403         u8  mouse_button = 0;//bit[2:0] is valid, [7:3] reserved
404         
405         int i,j;
406         
407         for (i = 0; i < report->maxfield; i++){
408             field = report->field[i];
409             if(field->usage->type == EV_KEY && field->usage->code == BTN_MOUSE) //mouse button
410             {
411                 for(j=0; j < field->report_count; j++)
412                 {
413                     if(field->value[j])
414                         mouse_button |= 1 << j;
415                 }
416             }
417             if(field->usage->type == EV_REL && field->usage->code == 0) //mouse rel location
418             {
419                 for(j=0; j < field->report_count ;j++)
420                 {
421                     mouse_rel[j] = field->value[j];
422                 }
423             }
424             if(field->usage->type == EV_REL && field->usage->code == REL_WHEEL) //mouse wheel
425             {
426                 mouse_wheel = field->value[0];
427             }
428
429         }
430
431         /*** generate HID report for hidg ***/
432         m.id     = MOUSE_REPORT_ID;
433         m.button = mouse_button;
434         m.x      = mouse_rel[0];
435         m.y      = mouse_rel[1];
436         m.wheel  = mouse_wheel;
437
438         f_hid_queue_report((u8 *)&m, sizeof(m));
439     }
440 }
441 EXPORT_SYMBOL(f_hid_mouse_translate_report);
442
443 #undef KBD_REPORT_ID
444 #undef MOUSE_REPORT_ID
445
446 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
447 {
448         struct f_hidg   *hidg  = file->private_data;
449         unsigned int    ret = 0;
450
451         poll_wait(file, &hidg->read_queue, wait);
452         poll_wait(file, &hidg->write_queue, wait);
453
454         if (WRITE_COND)
455                 ret |= POLLOUT | POLLWRNORM;
456
457         if (READ_COND)
458                 ret |= POLLIN | POLLRDNORM;
459
460         return ret;
461 }
462
463 #undef WRITE_COND
464 #undef READ_COND
465
466 static int f_hidg_release(struct inode *inode, struct file *fd)
467 {
468         fd->private_data = NULL;
469         return 0;
470 }
471
472 static int f_hidg_open(struct inode *inode, struct file *fd)
473 {
474         struct f_hidg *hidg =
475                 container_of(inode->i_cdev, struct f_hidg, cdev);
476
477         fd->private_data = hidg;
478
479         return 0;
480 }
481
482 /*-------------------------------------------------------------------------*/
483 /*                                usb_function                             */
484
485 void hidg_connect()
486 {
487     if(g_hidg)
488         g_hidg->connected = 1; 
489 }
490
491 void hidg_disconnect()
492 {
493     if(g_hidg){
494         g_hidg->connected = 0;
495     }
496 }
497 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
498
499 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
500 {
501         struct f_hidg *hidg = (struct f_hidg *)req->context;
502     printk("hidg_set_report_complete ,req->status = %d len = %d\n",
503     req->status,req->actual);
504         if (req->status != 0 || req->buf == NULL || req->actual == 0) {
505                 return;
506         }
507     
508         spin_lock(&hidg->spinlock);
509
510     if(!hidg->connected)
511         //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
512         hidg_connect();
513     
514         hidg->set_report_buff = krealloc(hidg->set_report_buff,
515                                          req->actual, GFP_ATOMIC);
516
517         if (hidg->set_report_buff == NULL) {
518                 spin_unlock(&hidg->spinlock);
519                 return;
520         }
521         hidg->set_report_length = req->actual;
522         memcpy(hidg->set_report_buff, req->buf, req->actual);
523     
524         spin_unlock(&hidg->spinlock);
525
526         wake_up(&hidg->read_queue);
527 }
528
529 static int hidg_setup(struct usb_function *f,
530                 const struct usb_ctrlrequest *ctrl)
531 {
532         struct f_hidg                   *hidg = func_to_hidg(f);
533         struct usb_composite_dev        *cdev = f->config->cdev;
534         struct usb_request              *req  = cdev->req;
535         int status = 0;
536         __u16 value, length;
537
538         value   = __le16_to_cpu(ctrl->wValue);
539         length  = __le16_to_cpu(ctrl->wLength);
540
541         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
542                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
543
544         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
545         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
546                   | HID_REQ_GET_REPORT):
547                 VDBG(cdev, "get_report\n");
548
549                 /* send an empty report */
550                 length = min_t(unsigned, length, hidg->report_length);
551                 memset(req->buf, 0x0, length);
552
553                 goto respond;
554                 break;
555
556         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
557                   | HID_REQ_GET_PROTOCOL):
558                 VDBG(cdev, "get_protocol\n");
559                 goto stall;
560                 break;
561
562         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
563                   | HID_REQ_SET_REPORT):
564                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
565                 req->context  = hidg;
566                 req->complete = hidg_set_report_complete;
567                 goto respond;
568                 break;
569
570         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
571                   | HID_REQ_SET_PROTOCOL):
572                 VDBG(cdev, "set_protocol\n");
573                 goto stall;
574                 break;
575
576         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
577                   | USB_REQ_GET_DESCRIPTOR):
578                 switch (value >> 8) {
579                 case HID_DT_REPORT:
580                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
581                         length = min_t(unsigned short, length,
582                                                    hidg->report_desc_length);
583                         memcpy(req->buf, hidg->report_desc, length);
584                         goto respond;
585                         break;
586
587                 default:
588                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
589                                  value >> 8);
590                         goto stall;
591                         break;
592                 }
593                 break;
594
595         default:
596                 VDBG(cdev, "Unknown request 0x%x\n",
597                          ctrl->bRequest);
598                 goto stall;
599                 break;
600         }
601
602 stall:
603         return -EOPNOTSUPP;
604
605 respond:
606         req->zero = 0;
607         req->length = length;
608         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
609         if (status < 0)
610                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
611         return status;
612 }
613
614 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
615                 const struct usb_ctrlrequest *ctrl)
616 {
617         struct f_hidg                   *hidg = g_hidg;
618         struct usb_request              *req  = cdev->req;
619         int status = 0;
620         __u16 value, length;
621
622         value   = __le16_to_cpu(ctrl->wValue);
623         length  = __le16_to_cpu(ctrl->wLength);
624
625     /*
626         printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
627                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
628     */
629         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
630         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
631                   | HID_REQ_GET_REPORT):
632                 VDBG(cdev, "get_report\n");
633
634                 /* send an empty report */
635                 length = min_t(unsigned, length, hidg->report_length);
636                 memset(req->buf, 0x0, length);
637
638                 goto respond;
639                 break;
640
641         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
642                   | HID_REQ_GET_PROTOCOL):
643                 VDBG(cdev, "get_protocol\n");
644                 goto stall;
645                 break;
646
647         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
648                   | HID_REQ_SET_REPORT):
649                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
650                 req->context  = hidg;
651                 req->complete = hidg_set_report_complete;
652                 goto respond;
653                 break;
654
655         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
656                   | HID_REQ_SET_PROTOCOL):
657                 VDBG(cdev, "set_protocol\n");
658                 goto stall;
659                 break;
660
661         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
662                   | USB_REQ_GET_DESCRIPTOR):
663                 switch (value >> 8) {
664                 case HID_DT_REPORT:
665                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
666                         length = min_t(unsigned short, length,
667                                                    hidg->report_desc_length);
668                         memcpy(req->buf, hidg->report_desc, length);
669                         goto respond;
670                         break;
671
672                 default:
673                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
674                                  value >> 8);
675                         goto stall;
676                         break;
677                 }
678                 break;
679
680         default:
681                 VDBG(cdev, "Unknown request 0x%x\n",
682                          ctrl->bRequest);
683                 goto stall;
684                 break;
685         }
686
687 stall:
688         return -EOPNOTSUPP;
689
690 respond:
691         req->zero = 0;
692         req->length = length;
693         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
694         if (status < 0)
695                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
696         return status;
697 }
698
699 static void hidg_disable(struct usb_function *f)
700 {
701         struct f_hidg *hidg = func_to_hidg(f);
702
703         usb_ep_disable(hidg->in_ep);
704         hidg->in_ep->driver_data = NULL;
705 }
706
707 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
708 {
709         struct usb_composite_dev                *cdev = f->config->cdev;
710         struct f_hidg                           *hidg = func_to_hidg(f);
711         const struct usb_endpoint_descriptor    *ep_desc;
712         int status = 0;
713
714         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
715
716         if (hidg->in_ep != NULL) {
717                 /* restart endpoint */
718                 if (hidg->in_ep->driver_data != NULL)
719                         usb_ep_disable(hidg->in_ep);
720
721                 ep_desc = ep_choose(f->config->cdev->gadget,
722                                 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
723                 status = usb_ep_enable(hidg->in_ep, ep_desc);
724                 if (status < 0) {
725                         //ERROR(cdev, "Enable endpoint FAILED!\n");
726                         goto fail;
727                 }
728                 hidg->in_ep->driver_data = hidg;
729         }
730 fail:
731         return status;
732 }
733
734 const struct file_operations f_hidg_fops = {
735         .owner          = THIS_MODULE,
736         .open           = f_hidg_open,
737         .release        = f_hidg_release,
738         .write          = NULL,//f_hidg_write,disable write to /dev/hidg0
739         .read           = f_hidg_read,
740         .poll           = f_hidg_poll,
741         .llseek         = noop_llseek,
742 };
743
744 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
745 {
746         struct usb_ep           *ep_in;
747         struct f_hidg           *hidg = func_to_hidg(f);
748         int                     status;
749         dev_t                   dev;
750         /* allocate instance-specific interface IDs, and patch descriptors */
751         status = usb_interface_id(c, f);
752         if (status < 0)
753                 goto fail;
754         hidg_interface_desc.bInterfaceNumber = status;
755
756         /* allocate instance-specific endpoints */
757         status = -ENODEV;
758         ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
759         if (!ep_in)
760                 goto fail;
761         ep_in->driver_data = c->cdev;   /* claim */
762         hidg->in_ep = ep_in;
763 #if 0
764     /* allocate out endpoint*/
765         ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
766         if (!ep_out)
767                 goto fail;
768         ep_out->driver_data = c->cdev;  /* claim */
769         hidg->out_ep = ep_out; 
770
771         printk("ep_out->name = %s\n",ep_out->name);
772 #endif
773         /* preallocate request and buffer */
774         status = -ENOMEM;
775         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
776         if (!hidg->req)
777                 goto fail;
778
779
780         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
781         if (!hidg->req->buf)
782                 goto fail;
783
784         /* set descriptor dynamic values */
785         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
786         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
787 //      hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
788 //      hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
789 //    hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
790 //      hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
791         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
792         hidg_desc.desc[0].wDescriptorLength =
793                 cpu_to_le16(hidg->report_desc_length);
794
795         hidg->set_report_buff = NULL;
796
797         /* copy descriptors */
798         f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
799         if (!f->descriptors)
800                 goto fail;
801
802         hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
803                                                 f->descriptors,
804                                                 &hidg_fs_in_ep_desc);
805
806         if (gadget_is_dualspeed(c->cdev->gadget)) {
807                 hidg_hs_in_ep_desc.bEndpointAddress =
808                         hidg_fs_in_ep_desc.bEndpointAddress;
809                 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
810                 if (!f->hs_descriptors)
811                         goto fail;
812                 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
813                                                         f->hs_descriptors,
814                                                         &hidg_hs_in_ep_desc);
815         } else {
816                 hidg->hs_in_ep_desc = NULL;
817         }
818
819     hidg->connected = 0;
820
821         mutex_init(&hidg->lock);
822         spin_lock_init(&hidg->spinlock);
823         init_waitqueue_head(&hidg->write_queue);
824         init_waitqueue_head(&hidg->read_queue);
825
826         /* create char device */
827         cdev_init(&hidg->cdev, &f_hidg_fops);
828         dev = MKDEV(major, hidg->minor);
829         status = cdev_add(&hidg->cdev, dev, 1);
830         if (status)
831                 goto fail;
832
833         device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
834
835         return 0;
836
837 fail:
838         ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
839         if (hidg->req != NULL) {
840                 kfree(hidg->req->buf);
841                 if (hidg->in_ep != NULL)
842                         usb_ep_free_request(hidg->in_ep, hidg->req);
843         }
844     g_hidg = NULL;
845         usb_free_descriptors(f->hs_descriptors);
846         usb_free_descriptors(f->descriptors);
847
848         return status;
849 }
850
851 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
852 {
853         struct f_hidg *hidg = func_to_hidg(f);
854
855     f_hid_bypass_input_set(0);
856
857         device_destroy(hidg_class, MKDEV(major, hidg->minor));
858         cdev_del(&hidg->cdev);
859
860         /* disable/free request and end point */
861         usb_ep_disable(hidg->in_ep);
862         usb_ep_dequeue(hidg->in_ep, hidg->req);
863         if(hidg->req->buf)
864         kfree(hidg->req->buf);
865         usb_ep_free_request(hidg->in_ep, hidg->req);
866
867         /* free descriptors copies */
868         usb_free_descriptors(f->hs_descriptors);
869         usb_free_descriptors(f->descriptors);
870         
871     
872         kfree(hidg->report_desc);
873         kfree(hidg->set_report_buff);
874         kfree(hidg);
875         
876         g_hidg = NULL;
877 }
878
879 /*-------------------------------------------------------------------------*/
880 /*                                 Strings                                 */
881
882 #define CT_FUNC_HID_IDX 0
883
884 static struct usb_string ct_func_string_defs[] = {
885         [CT_FUNC_HID_IDX].s     = "HID Interface",
886         {},                     /* end of list */
887 };
888
889 static struct usb_gadget_strings ct_func_string_table = {
890         .language       = 0x0409,       /* en-US */
891         .strings        = ct_func_string_defs,
892 };
893
894 static struct usb_gadget_strings *ct_func_strings[] = {
895         &ct_func_string_table,
896         NULL,
897 };
898
899 /*-------------------------------------------------------------------------*/
900 /*                             usb_configuration                           */
901
902 int hidg_bind_config(struct usb_configuration *c,
903                             const struct hidg_func_descriptor *fdesc, int index)
904 {
905         struct f_hidg *hidg;
906         int status;
907         if (index >= minors)
908                 return -ENOENT;
909
910         /* maybe allocate device-global string IDs, and patch descriptors */
911         if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
912                 status = usb_string_id(c->cdev);
913                 if (status < 0)
914                         return status;
915                 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
916                 hidg_interface_desc.iInterface = status;
917         }
918
919         /* allocate and initialize one new instance */
920         hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
921         if (!hidg)
922                 return -ENOMEM;
923         g_hidg = hidg;
924         hidg->bypass_input = 0;
925         hidg->minor = index;
926         hidg->bInterfaceSubClass = fdesc->subclass;
927         hidg->bInterfaceProtocol = fdesc->protocol;
928         hidg->report_length = fdesc->report_length;
929         hidg->report_desc_length = fdesc->report_desc_length;
930         hidg->report_desc = kmemdup(fdesc->report_desc,
931                                     fdesc->report_desc_length,
932                                     GFP_KERNEL);
933         if (!hidg->report_desc) {
934                 kfree(hidg);
935                 return -ENOMEM;
936         }
937
938         hidg->func.name    = "hid";
939         hidg->func.strings = ct_func_strings;
940         hidg->func.bind    = hidg_bind;
941         hidg->func.unbind  = hidg_unbind;
942         hidg->func.set_alt = hidg_set_alt;
943         hidg->func.disable = hidg_disable;
944         hidg->func.setup   = hidg_setup;
945     
946         status = usb_add_function(c, &hidg->func);
947         if (status)
948                 kfree(hidg);
949         else
950         g_hidg = hidg;
951         return status;
952 }
953
954 int ghid_setup(struct usb_gadget *g, int count)
955 {
956         int status;
957         dev_t dev;
958
959         hidg_class = class_create(THIS_MODULE, "hidg");
960
961         status = alloc_chrdev_region(&dev, 0, count, "hidg");
962         if (!status) {
963                 major = MAJOR(dev);
964                 minors = count;
965         }
966
967         return status;
968 }
969
970 void ghid_cleanup(void)
971 {
972         if (major) {
973                 unregister_chrdev_region(MKDEV(major, 0), minors);
974                 major = minors = 0;
975         }
976
977         class_destroy(hidg_class);
978 }