gadget hid: fix moust button bug
[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 /* 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 */
177         .report_length          = 64,
178         .report_desc_length = 156,
179         .report_desc        = {
180         
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                       */   
214         
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)                 */ 
226         0xC0,
227         
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)                  */
233             
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)             */
254             0x09, 0x38, 
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                   */
261             
262         0xC0            /*   END_COLLECTION                       */
263
264         },
265 };
266
267 struct f_hidg *g_hidg;
268
269 /*-------------------------------------------------------------------------*/
270 /*                              Char Device                                */
271
272 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
273                         size_t count, loff_t *ptr)
274 {
275         struct f_hidg   *hidg     = file->private_data;
276         char            *tmp_buff = NULL;
277         unsigned long   flags;
278
279         if (!count)
280                 return 0;
281
282         if (!access_ok(VERIFY_WRITE, buffer, count))
283                 return -EFAULT;
284
285         spin_lock_irqsave(&hidg->spinlock, flags);
286
287 #define READ_COND (hidg->set_report_buff != NULL)
288
289         while (!READ_COND) {
290                 spin_unlock_irqrestore(&hidg->spinlock, flags);
291                 if (file->f_flags & O_NONBLOCK)
292                         return -EAGAIN;
293
294                 if (wait_event_interruptible(hidg->read_queue, READ_COND))
295                         return -ERESTARTSYS;
296
297                 spin_lock_irqsave(&hidg->spinlock, flags);
298         }
299
300
301         count = min_t(unsigned, count, hidg->set_report_length);
302         tmp_buff = hidg->set_report_buff;
303         hidg->set_report_buff = NULL;
304
305         spin_unlock_irqrestore(&hidg->spinlock, flags);
306
307         if (tmp_buff != NULL) {
308                 /* copy to user outside spinlock */
309                 count -= copy_to_user(buffer, tmp_buff, count);
310                 kfree(tmp_buff);
311         } else
312                 count = -ENOMEM;
313
314         return count;
315 }
316
317 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
318 {
319         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
320
321         if (req->status != 0) {
322                 //ERROR(hidg->func.config->cdev,
323                 //      "End Point Request ERROR: %d\n", req->status);
324         }
325
326         hidg->write_pending = 0;
327         wake_up(&hidg->write_queue);
328 }
329
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)
333 {
334 #if 0
335         struct f_hidg *hidg  = file->private_data;
336         ssize_t status = -ENOMEM;
337
338         if (!access_ok(VERIFY_READ, buffer, count))
339                 return -EFAULT;
340
341         mutex_lock(&hidg->lock);
342
343 #define WRITE_COND (!hidg->write_pending)
344
345         /* write queue */
346         while (!WRITE_COND) {
347                 mutex_unlock(&hidg->lock);
348                 if (file->f_flags & O_NONBLOCK)
349                         return -EAGAIN;
350
351                 if (wait_event_interruptible_exclusive(
352                                 hidg->write_queue, WRITE_COND))
353                         return -ERESTARTSYS;
354
355                 mutex_lock(&hidg->lock);
356         }
357
358         count  = min_t(unsigned, count, hidg->report_length);
359         status = copy_from_user(hidg->req->buf, buffer, count);
360
361         if (status != 0) {
362                 //ERROR(hidg->func.config->cdev,
363                 //      "copy_from_user error\n");
364                 mutex_unlock(&hidg->lock);
365                 return -EINVAL;
366         }
367
368         hidg->req->status   = 0;
369         hidg->req->zero     = 0;
370         hidg->req->length   = count;
371         hidg->req->complete = f_hidg_req_complete;
372         hidg->req->context  = hidg;
373         hidg->write_pending = 1;
374
375         status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
376         if (status < 0) {
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);
381         } else {
382                 status = count;
383         }
384
385         mutex_unlock(&hidg->lock);
386 #endif
387         return count;
388 }
389 static void f_hid_queue_report(u8 *data, int len)
390 {
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];
395     
396     if(hidg){
397         if(hidg->connected){
398             //mutex_lock(&hidg->lock);
399             memcpy(hidg->req->buf, data, len);
400                 hidg->req->status   = 0;
401                 hidg->req->zero     = 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;
406
407                 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
408                 if (status < 0) {
409                         printk("usb_ep_queue error on int endpoint %zd\n", status);
410                 }
411             }
412     //mutex_unlock(&hidg->lock);
413     }
414 }
415
416
417 #define KBD_REPORT_ID (0x01)
418 #define MOUSE_REPORT_ID (0x02)
419 #define CONSUMER_REPORT_ID (0x03)
420
421 unsigned int f_hid_bypass_input_get()
422 {
423     if(!g_hidg)
424         return 0;
425     else
426         return g_hidg->bypass_input;
427 }
428 EXPORT_SYMBOL(f_hid_bypass_input_get);
429
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};
433
434 static void f_hid_send_idle_report(void)
435 {
436     if(g_hidg){
437         mdelay(2);
438         f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
439         mdelay(2);
440         f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
441         mdelay(2);
442         f_hid_queue_report(consumer_idle, sizeof(consumer_idle));
443     }
444 }
445
446 static void f_hid_bypass_input_set(u8 bypass)
447 {
448     if(g_hidg){
449
450         u8 current_state = f_hid_bypass_input_get();
451
452         if( bypass && (!current_state))
453         {
454             g_hidg->bypass_input = 1;
455         }
456         if(!bypass && (current_state))
457         {
458             f_hid_send_idle_report();
459             g_hidg->bypass_input = 0;
460         }
461     }
462 }
463
464 struct kbd_report {
465     u8        id;
466     u8        command;
467     u8        reserved;
468     u8        key_array[6];
469
470 }__attribute__ ((packed));
471
472 struct consumer_report {
473     u8        id;
474     u16        data;
475 }__attribute__ ((packed));
476
477 void f_hid_kbd_translate_report(struct hid_report *report, u8 *data)
478 {
479     if(f_hid_bypass_input_get())
480     {
481         int i,j;
482         struct kbd_report  k = {0};
483         struct consumer_report c = {0};
484         
485         struct hid_field *field;
486         
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
491             {
492                 for(j = 0; j < field->report_count; j++)
493                 {
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 ;
510                 }
511             }
512             else // ARRAY REPORT 
513             {
514                 if(field->application == HID_GD_KEYBOARD)
515                 {
516                     for(j = 0 ; j<(min(6,field->report_count)); j++)
517                     {
518                         k.key_array[j] = field->value[j];
519                     }
520                 }
521                 if(field->application == 0x000c0001)//CONSUMER PAGE
522                 {
523                     for(j = 0 ; j < (field->report_count); j++)
524                     {
525                         c.id = CONSUMER_REPORT_ID;
526                         c.data = field->value[j];
527                         f_hid_queue_report((u8 *)&c, sizeof(c));
528                         return;
529                     }
530                 }
531             }
532         }     
533         f_hid_queue_report((u8 *)&k, sizeof(k));
534     }
535 }
536 EXPORT_SYMBOL(f_hid_kbd_translate_report);
537
538 struct mouse_report {
539     u8        id:8;
540     bool      button_l:1;
541     bool      button_r:1;
542     bool      button_m:1;
543     u8        reserved:5;
544     signed    x :12;
545     signed    y :12;
546     s8        wheel:8;
547 }__attribute__ ((packed));
548
549
550 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
551 {
552
553     if(f_hid_bypass_input_get())
554     {
555         struct mouse_report m = {0};
556         struct hid_field *field;
557         
558         int i,j;
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++)
563             {
564                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_LEFT))
565                     if(field->value[j])
566                         m.button_l = 1;
567                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_RIGHT))
568                     if(field->value[j])
569                         m.button_r = 1;
570                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_MIDDLE))
571                     if(field->value[j])
572                         m.button_m = 1;
573                         
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];
580             }
581         }
582         f_hid_queue_report((u8 *)&m, sizeof(m));
583     }
584 }
585 EXPORT_SYMBOL(f_hid_mouse_translate_report);
586
587 #undef KBD_REPORT_ID
588 #undef MOUSE_REPORT_ID
589 #undef CONSUMER_REPORT_ID
590
591
592 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
593 {
594         struct f_hidg   *hidg  = file->private_data;
595         unsigned int    ret = 0;
596
597         poll_wait(file, &hidg->read_queue, wait);
598         poll_wait(file, &hidg->write_queue, wait);
599
600         if (WRITE_COND)
601                 ret |= POLLOUT | POLLWRNORM;
602
603         if (READ_COND)
604                 ret |= POLLIN | POLLRDNORM;
605
606         return ret;
607 }
608
609 #undef WRITE_COND
610 #undef READ_COND
611
612 static int f_hidg_release(struct inode *inode, struct file *fd)
613 {
614         fd->private_data = NULL;
615         return 0;
616 }
617
618 static int f_hidg_open(struct inode *inode, struct file *fd)
619 {
620         struct f_hidg *hidg =
621                 container_of(inode->i_cdev, struct f_hidg, cdev);
622
623         fd->private_data = hidg;
624
625         return 0;
626 }
627
628 /*-------------------------------------------------------------------------*/
629 /*                                usb_function                             */
630
631 void hidg_connect()
632 {
633     if(g_hidg)
634         g_hidg->connected = 1; 
635 }
636
637 void hidg_disconnect()
638 {
639     if(g_hidg){
640         g_hidg->connected = 0;
641     }
642 }
643 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
644
645 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
646 {
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) {
651                 return;
652         }
653     
654         spin_lock(&hidg->spinlock);
655
656     if(!hidg->connected)
657         //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
658         hidg_connect();
659     
660         hidg->set_report_buff = krealloc(hidg->set_report_buff,
661                                          req->actual, GFP_ATOMIC);
662
663         if (hidg->set_report_buff == NULL) {
664                 spin_unlock(&hidg->spinlock);
665                 return;
666         }
667         hidg->set_report_length = req->actual;
668         memcpy(hidg->set_report_buff, req->buf, req->actual);
669     
670         spin_unlock(&hidg->spinlock);
671
672         wake_up(&hidg->read_queue);
673 }
674
675 static int hidg_setup(struct usb_function *f,
676                 const struct usb_ctrlrequest *ctrl)
677 {
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;
681         int status = 0;
682         __u16 value, length;
683
684         value   = __le16_to_cpu(ctrl->wValue);
685         length  = __le16_to_cpu(ctrl->wLength);
686
687         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
688                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
689
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");
694
695                 /* send an empty report */
696                 length = min_t(unsigned, length, hidg->report_length);
697                 memset(req->buf, 0x0, length);
698
699                 goto respond;
700                 break;
701
702         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
703                   | HID_REQ_GET_PROTOCOL):
704                 VDBG(cdev, "get_protocol\n");
705                 goto stall;
706                 break;
707
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);
711                 req->context  = hidg;
712                 req->complete = hidg_set_report_complete;
713                 goto respond;
714                 break;
715
716         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
717                   | HID_REQ_SET_PROTOCOL):
718                 VDBG(cdev, "set_protocol\n");
719                 goto stall;
720                 break;
721
722         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
723                   | USB_REQ_GET_DESCRIPTOR):
724                 switch (value >> 8) {
725                 case HID_DT_REPORT:
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);
730                         goto respond;
731                         break;
732
733                 default:
734                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
735                                  value >> 8);
736                         goto stall;
737                         break;
738                 }
739                 break;
740
741         default:
742                 VDBG(cdev, "Unknown request 0x%x\n",
743                          ctrl->bRequest);
744                 goto stall;
745                 break;
746         }
747
748 stall:
749         return -EOPNOTSUPP;
750
751 respond:
752         req->zero = 0;
753         req->length = length;
754         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
755         if (status < 0)
756                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
757         return status;
758 }
759
760 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
761                 const struct usb_ctrlrequest *ctrl)
762 {
763         struct f_hidg                   *hidg = g_hidg;
764         struct usb_request              *req  = cdev->req;
765         int status = 0;
766         __u16 value, length;
767
768         value   = __le16_to_cpu(ctrl->wValue);
769         length  = __le16_to_cpu(ctrl->wLength);
770
771     /*
772         printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
773                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
774     */
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");
779
780                 /* send an empty report */
781                 length = min_t(unsigned, length, hidg->report_length);
782                 memset(req->buf, 0x0, length);
783
784                 goto respond;
785                 break;
786
787         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
788                   | HID_REQ_GET_PROTOCOL):
789                 VDBG(cdev, "get_protocol\n");
790                 goto stall;
791                 break;
792
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);
796                 req->context  = hidg;
797                 req->complete = hidg_set_report_complete;
798                 goto respond;
799                 break;
800
801         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
802                   | HID_REQ_SET_PROTOCOL):
803                 VDBG(cdev, "set_protocol\n");
804                 goto stall;
805                 break;
806
807         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
808                   | USB_REQ_GET_DESCRIPTOR):
809                 switch (value >> 8) {
810                 case HID_DT_REPORT:
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);
815                         goto respond;
816                         break;
817
818                 default:
819                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
820                                  value >> 8);
821                         goto stall;
822                         break;
823                 }
824                 break;
825
826         default:
827                 VDBG(cdev, "Unknown request 0x%x\n",
828                          ctrl->bRequest);
829                 goto stall;
830                 break;
831         }
832
833 stall:
834         return -EOPNOTSUPP;
835
836 respond:
837         req->zero = 0;
838         req->length = length;
839         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
840         if (status < 0)
841                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
842         return status;
843 }
844
845 static void hidg_disable(struct usb_function *f)
846 {
847         struct f_hidg *hidg = func_to_hidg(f);
848
849         usb_ep_disable(hidg->in_ep);
850         hidg->in_ep->driver_data = NULL;
851 }
852
853 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
854 {
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;
858         int status = 0;
859
860         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
861
862         if (hidg->in_ep != NULL) {
863                 /* restart endpoint */
864                 if (hidg->in_ep->driver_data != NULL)
865                         usb_ep_disable(hidg->in_ep);
866
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);
870                 if (status < 0) {
871                         //ERROR(cdev, "Enable endpoint FAILED!\n");
872                         goto fail;
873                 }
874                 hidg->in_ep->driver_data = hidg;
875         }
876 fail:
877         return status;
878 }
879
880 const struct file_operations f_hidg_fops = {
881         .owner          = THIS_MODULE,
882         .open           = f_hidg_open,
883         .release        = f_hidg_release,
884         .write          = NULL,//f_hidg_write,disable write to /dev/hidg0
885         .read           = f_hidg_read,
886         .poll           = f_hidg_poll,
887         .llseek         = noop_llseek,
888 };
889
890 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
891 {
892         struct usb_ep           *ep_in;
893         struct f_hidg           *hidg = func_to_hidg(f);
894         int                     status;
895         dev_t                   dev;
896         /* allocate instance-specific interface IDs, and patch descriptors */
897         status = usb_interface_id(c, f);
898         if (status < 0)
899                 goto fail;
900         hidg_interface_desc.bInterfaceNumber = status;
901
902         /* allocate instance-specific endpoints */
903         status = -ENODEV;
904         ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
905         if (!ep_in)
906                 goto fail;
907         ep_in->driver_data = c->cdev;   /* claim */
908         hidg->in_ep = ep_in;
909 #if 0
910     /* allocate out endpoint*/
911         ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
912         if (!ep_out)
913                 goto fail;
914         ep_out->driver_data = c->cdev;  /* claim */
915         hidg->out_ep = ep_out; 
916
917         printk("ep_out->name = %s\n",ep_out->name);
918 #endif
919         /* preallocate request and buffer */
920         status = -ENOMEM;
921         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
922         if (!hidg->req)
923                 goto fail;
924
925
926         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
927         if (!hidg->req->buf)
928                 goto fail;
929
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);
940
941         hidg->set_report_buff = NULL;
942
943         /* copy descriptors */
944         f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
945         if (!f->descriptors)
946                 goto fail;
947
948         hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
949                                                 f->descriptors,
950                                                 &hidg_fs_in_ep_desc);
951
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)
957                         goto fail;
958                 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
959                                                         f->hs_descriptors,
960                                                         &hidg_hs_in_ep_desc);
961         } else {
962                 hidg->hs_in_ep_desc = NULL;
963         }
964
965     hidg->connected = 0;
966
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);
971
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);
976         if (status)
977                 goto fail;
978
979         device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
980
981         return 0;
982
983 fail:
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);
989         }
990     g_hidg = NULL;
991         usb_free_descriptors(f->hs_descriptors);
992         usb_free_descriptors(f->descriptors);
993
994         return status;
995 }
996
997 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
998 {
999         struct f_hidg *hidg = func_to_hidg(f);
1000
1001     f_hid_bypass_input_set(0);
1002
1003         device_destroy(hidg_class, MKDEV(major, hidg->minor));
1004         cdev_del(&hidg->cdev);
1005
1006         /* disable/free request and end point */
1007         usb_ep_disable(hidg->in_ep);
1008         usb_ep_dequeue(hidg->in_ep, hidg->req);
1009         if(hidg->req->buf)
1010         kfree(hidg->req->buf);
1011         usb_ep_free_request(hidg->in_ep, hidg->req);
1012
1013         /* free descriptors copies */
1014         usb_free_descriptors(f->hs_descriptors);
1015         usb_free_descriptors(f->descriptors);
1016         
1017     
1018         kfree(hidg->report_desc);
1019         kfree(hidg->set_report_buff);
1020         kfree(hidg);
1021         
1022         g_hidg = NULL;
1023 }
1024
1025 /*-------------------------------------------------------------------------*/
1026 /*                                 Strings                                 */
1027
1028 #define CT_FUNC_HID_IDX 0
1029
1030 static struct usb_string ct_func_string_defs[] = {
1031         [CT_FUNC_HID_IDX].s     = "HID Interface",
1032         {},                     /* end of list */
1033 };
1034
1035 static struct usb_gadget_strings ct_func_string_table = {
1036         .language       = 0x0409,       /* en-US */
1037         .strings        = ct_func_string_defs,
1038 };
1039
1040 static struct usb_gadget_strings *ct_func_strings[] = {
1041         &ct_func_string_table,
1042         NULL,
1043 };
1044
1045 /*-------------------------------------------------------------------------*/
1046 /*                             usb_configuration                           */
1047
1048 int hidg_bind_config(struct usb_configuration *c,
1049                             const struct hidg_func_descriptor *fdesc, int index)
1050 {
1051         struct f_hidg *hidg;
1052         int status;
1053         if (index >= minors)
1054                 return -ENOENT;
1055
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);
1059                 if (status < 0)
1060                         return status;
1061                 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1062                 hidg_interface_desc.iInterface = status;
1063         }
1064
1065         /* allocate and initialize one new instance */
1066         hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1067         if (!hidg)
1068                 return -ENOMEM;
1069         g_hidg = hidg;
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,
1078                                     GFP_KERNEL);
1079         if (!hidg->report_desc) {
1080                 kfree(hidg);
1081                 return -ENOMEM;
1082         }
1083
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;
1091     
1092         status = usb_add_function(c, &hidg->func);
1093         if (status)
1094                 kfree(hidg);
1095         else
1096         g_hidg = hidg;
1097         return status;
1098 }
1099
1100 int ghid_setup(struct usb_gadget *g, int count)
1101 {
1102         int status;
1103         dev_t dev;
1104
1105         hidg_class = class_create(THIS_MODULE, "hidg");
1106
1107         status = alloc_chrdev_region(&dev, 0, count, "hidg");
1108         if (!status) {
1109                 major = MAJOR(dev);
1110                 minors = count;
1111         }
1112
1113         return status;
1114 }
1115
1116 void ghid_cleanup(void)
1117 {
1118         if (major) {
1119                 unregister_chrdev_region(MKDEV(major, 0), minors);
1120                 major = minors = 0;
1121         }
1122
1123         class_destroy(hidg_class);
1124 }