android computer: usb-gadget: support rndis ctrl_request command
[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 = 150,
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, 0x08,     /*   USAGE_MAXIMUM (Button 8)         */
238             0x15, 0x00,     /*   LOGICAL_MINIMUM (0)              */
239             0x25, 0x01,     /*   LOGICAL_MAXIMUM (1)              */
240             0x75, 0x01,     /*   REPORT_SIZE (1)                  */
241             0x95, 0x08,     /*   REPORT_COUNT (8)                 */
242             0x81, 0x02,     /*   INPUT (Data,Var,Abs)             */
243             0x05, 0x01,     /*   USAGE_PAGE (Generic Desktop)     */
244             0x09, 0x30,     /*   USAGE (X)                        */
245             0x09, 0x31,     /*   USAGE (Y)                        */
246             0x16, 0x01, 0xF8, /* LOGICAL_MINIMUM (-2047)          */
247             0x26, 0xFF, 0x07, /* LOGICAL_MAXIMUM (2047)           */
248             0x75, 0x0C,     /*   REPORT_SIZE (12)                 */
249             0x95, 0x02,     /*   REPORT_COUNT (2)                 */
250             0x81, 0x06,     /*   INPUT (Data,Var,Rel)             */
251             0x09, 0x38, 
252             0x15, 0x81,     /*   LOGICAL_MINIMUM (-127)           */
253             0x25, 0x7F,     /*   LOGICAL_MAXIMUM (127)            */
254             0x75, 0x08,     /*   REPORT_SIZE (8)                  */
255             0x95, 0x01,     /*   REPORT_COUNT (1)                 */
256             0x81, 0x06,     /*   INPUT (Data,Var,Rel)             */
257             0xC0 ,          /*   END_COLLECTION                   */
258             
259         0xC0            /*   END_COLLECTION                       */
260
261         },
262 };
263
264 struct f_hidg *g_hidg;
265
266 /*-------------------------------------------------------------------------*/
267 /*                              Char Device                                */
268
269 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
270                         size_t count, loff_t *ptr)
271 {
272         struct f_hidg   *hidg     = file->private_data;
273         char            *tmp_buff = NULL;
274         unsigned long   flags;
275
276         if (!count)
277                 return 0;
278
279         if (!access_ok(VERIFY_WRITE, buffer, count))
280                 return -EFAULT;
281
282         spin_lock_irqsave(&hidg->spinlock, flags);
283
284 #define READ_COND (hidg->set_report_buff != NULL)
285
286         while (!READ_COND) {
287                 spin_unlock_irqrestore(&hidg->spinlock, flags);
288                 if (file->f_flags & O_NONBLOCK)
289                         return -EAGAIN;
290
291                 if (wait_event_interruptible(hidg->read_queue, READ_COND))
292                         return -ERESTARTSYS;
293
294                 spin_lock_irqsave(&hidg->spinlock, flags);
295         }
296
297
298         count = min_t(unsigned, count, hidg->set_report_length);
299         tmp_buff = hidg->set_report_buff;
300         hidg->set_report_buff = NULL;
301
302         spin_unlock_irqrestore(&hidg->spinlock, flags);
303
304         if (tmp_buff != NULL) {
305                 /* copy to user outside spinlock */
306                 count -= copy_to_user(buffer, tmp_buff, count);
307                 kfree(tmp_buff);
308         } else
309                 count = -ENOMEM;
310
311         return count;
312 }
313
314 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
315 {
316         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
317
318         if (req->status != 0) {
319                 //ERROR(hidg->func.config->cdev,
320                 //      "End Point Request ERROR: %d\n", req->status);
321         }
322
323         hidg->write_pending = 0;
324         wake_up(&hidg->write_queue);
325 }
326
327 #define WRITE_COND (!hidg->write_pending)
328 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
329                             size_t count, loff_t *offp)
330 {
331 #if 0
332         struct f_hidg *hidg  = file->private_data;
333         ssize_t status = -ENOMEM;
334
335         if (!access_ok(VERIFY_READ, buffer, count))
336                 return -EFAULT;
337
338         mutex_lock(&hidg->lock);
339
340 #define WRITE_COND (!hidg->write_pending)
341
342         /* write queue */
343         while (!WRITE_COND) {
344                 mutex_unlock(&hidg->lock);
345                 if (file->f_flags & O_NONBLOCK)
346                         return -EAGAIN;
347
348                 if (wait_event_interruptible_exclusive(
349                                 hidg->write_queue, WRITE_COND))
350                         return -ERESTARTSYS;
351
352                 mutex_lock(&hidg->lock);
353         }
354
355         count  = min_t(unsigned, count, hidg->report_length);
356         status = copy_from_user(hidg->req->buf, buffer, count);
357
358         if (status != 0) {
359                 //ERROR(hidg->func.config->cdev,
360                 //      "copy_from_user error\n");
361                 mutex_unlock(&hidg->lock);
362                 return -EINVAL;
363         }
364
365         hidg->req->status   = 0;
366         hidg->req->zero     = 0;
367         hidg->req->length   = count;
368         hidg->req->complete = f_hidg_req_complete;
369         hidg->req->context  = hidg;
370         hidg->write_pending = 1;
371
372         status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
373         if (status < 0) {
374                 //ERROR(hidg->func.config->cdev,
375                 //      "usb_ep_queue error on int endpoint %zd\n", status);
376                 hidg->write_pending = 0;
377                 wake_up(&hidg->write_queue);
378         } else {
379                 status = count;
380         }
381
382         mutex_unlock(&hidg->lock);
383 #endif
384         return count;
385 }
386 static void f_hid_queue_report(u8 *data, int len)
387 {
388     //this function will run in interrupt context 
389     ssize_t status = -ENOMEM;
390     struct f_hidg *hidg = g_hidg;
391     //static char raw_report[8];
392     
393     if(hidg){
394         if(hidg->connected){
395             //mutex_lock(&hidg->lock);
396             memcpy(hidg->req->buf, data, len);
397                 hidg->req->status   = 0;
398                 hidg->req->zero     = 0;
399                 hidg->req->length   = len;
400                 //hidg->req->buf      = raw_report;
401                 hidg->req->complete = f_hidg_req_complete;
402                 hidg->req->context  = hidg;
403
404                 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
405                 if (status < 0) {
406                         printk("usb_ep_queue error on int endpoint %zd\n", status);
407                 }
408             }
409     //mutex_unlock(&hidg->lock);
410     }
411 }
412
413
414 #define KBD_REPORT_ID (0x01)
415 #define MOUSE_REPORT_ID (0x02)
416 #define CONSUMER_REPORT_ID (0x03)
417
418 unsigned int f_hid_bypass_input_get()
419 {
420     if(!g_hidg)
421         return 0;
422     else
423         return g_hidg->bypass_input;
424 }
425 EXPORT_SYMBOL(f_hid_bypass_input_get);
426
427 unsigned char kbd_idle[]     = {KBD_REPORT_ID,0,0,0,0,0,0,0,0};
428 unsigned char mouse_idle[]   = {MOUSE_REPORT_ID,0,0,0,0,0};
429 unsigned char consumer_idle[]= {CONSUMER_REPORT_ID,0,0};
430
431 static void f_hid_send_idle_report(void)
432 {
433     if(g_hidg){
434         mdelay(2);
435         f_hid_queue_report(kbd_idle, sizeof(kbd_idle));
436         mdelay(2);
437         f_hid_queue_report(mouse_idle, sizeof(mouse_idle));
438         mdelay(2);
439         f_hid_queue_report(consumer_idle, sizeof(consumer_idle));
440     }
441 }
442
443 static void f_hid_bypass_input_set(u8 bypass)
444 {
445     if(g_hidg){
446
447         u8 current_state = f_hid_bypass_input_get();
448
449         if( bypass && (!current_state))
450         {
451             g_hidg->bypass_input = 1;
452         }
453         if(!bypass && (current_state))
454         {
455             f_hid_send_idle_report();
456             g_hidg->bypass_input = 0;
457         }
458     }
459 }
460
461 struct kbd_report {
462     u8        id;
463     u8        command;
464     u8        reserved;
465     u8        key_array[6];
466
467 }__attribute__ ((packed));
468
469 struct consumer_report {
470     u8        id;
471     u16        data;
472 }__attribute__ ((packed));
473
474 void f_hid_kbd_translate_report(struct hid_report *report, u8 *data)
475 {
476     if(f_hid_bypass_input_get())
477     {
478         int i,j;
479         struct kbd_report  k = {0};
480         struct consumer_report c = {0};
481         
482         struct hid_field *field;
483         
484         k.id = KBD_REPORT_ID;//report id
485         for (i = 0; i < report->maxfield; i++){
486             field = report->field[i];
487             if(HID_MAIN_ITEM_VARIABLE & field->flags)//VARIABLE REPORT
488             {
489                 for(j = 0; j < field->report_count; j++)
490                 {
491                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTCTRL)) 
492                         k.command |= field->value[j] ? 1 << 0 : 0 ;
493                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTSHIFT)) 
494                         k.command |= field->value[j] ? 1 << 1 : 0 ;
495                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTALT)) 
496                         k.command |= field->value[j] ? 1 << 2 : 0 ;
497                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_LEFTMETA))
498                         k.command |= field->value[j] ? 1 << 3 : 0 ;
499                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTCTRL))
500                         k.command |= field->value[j] ? 1 << 4 : 0 ;
501                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTSHIFT)) 
502                         k.command |= field->value[j] ? 1 << 5 : 0 ;
503                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTALT)) 
504                         k.command |= field->value[j] ? 1 << 6 : 0 ;
505                     if((field->usage[j].type == EV_KEY) && (field->usage[j].code == KEY_RIGHTMETA)) 
506                         k.command |= field->value[j] ? 1 << 7 : 0 ;
507                 }
508             }
509             else // ARRAY REPORT 
510             {
511                 if(field->application == HID_GD_KEYBOARD)
512                 {
513                     for(j = 0 ; j<(min(6,field->report_count)); j++)
514                     {
515                         k.key_array[j] = field->value[j];
516                     }
517                 }
518                 if(field->application == 0x000c0001)//CONSUMER PAGE
519                 {
520                     for(j = 0 ; j < (field->report_count); j++)
521                     {
522                         c.id = CONSUMER_REPORT_ID;
523                         c.data = field->value[j];
524                         f_hid_queue_report((u8 *)&c, sizeof(c));
525                         return;
526                     }
527                 }
528             }
529         }     
530         f_hid_queue_report((u8 *)&k, sizeof(k));
531     }
532 }
533 EXPORT_SYMBOL(f_hid_kbd_translate_report);
534
535 struct mouse_report {
536     u8        id:8;
537     bool      button_left:1;
538     bool      button_right:1;
539     bool      button_middle:1;
540     bool      button_side:1;
541     bool      button_extra:1;
542     bool      button_forward:1;
543     bool      button_back:1;
544     bool      button_task:1;
545
546     signed    x :12;
547     signed    y :12;
548     s8        wheel:8;
549 }__attribute__ ((packed));
550
551
552 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
553 {
554
555     if(f_hid_bypass_input_get())
556     {
557         struct mouse_report m = {0};
558         struct hid_field *field;
559         
560         int i,j;
561         m.id     = MOUSE_REPORT_ID;
562         for (i = 0; i < report->maxfield; i++){
563             field = report->field[i];
564             for(j=0; j<field->report_count; j++)
565             {
566                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_LEFT))
567                     if(field->value[j])
568                         m.button_left= 1;
569                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_RIGHT))
570                     if(field->value[j])
571                         m.button_right= 1;
572                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_MIDDLE))
573                     if(field->value[j])
574                         m.button_middle= 1;
575                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_SIDE))
576                     if(field->value[j])
577                         m.button_side= 1;
578                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_EXTRA))
579                     if(field->value[j])
580                         m.button_extra= 1;
581                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_FORWARD))
582                     if(field->value[j])
583                         m.button_forward= 1;
584                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_BACK))
585                     if(field->value[j])
586                         m.button_back= 1;
587                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_TASK))
588                     if(field->value[j])
589                         m.button_task= 1;
590
591                         
592                 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_X))
593                     m.x = field->value[j];
594                 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_Y))
595                     m.y = field->value[j];
596                 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_WHEEL))
597                     m.wheel= field->value[j];
598             }
599         }
600         f_hid_queue_report((u8 *)&m, sizeof(m));
601     }
602 }
603 EXPORT_SYMBOL(f_hid_mouse_translate_report);
604
605 #undef KBD_REPORT_ID
606 #undef MOUSE_REPORT_ID
607 #undef CONSUMER_REPORT_ID
608
609
610 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
611 {
612         struct f_hidg   *hidg  = file->private_data;
613         unsigned int    ret = 0;
614
615         poll_wait(file, &hidg->read_queue, wait);
616         poll_wait(file, &hidg->write_queue, wait);
617
618         if (WRITE_COND)
619                 ret |= POLLOUT | POLLWRNORM;
620
621         if (READ_COND)
622                 ret |= POLLIN | POLLRDNORM;
623
624         return ret;
625 }
626
627 #undef WRITE_COND
628 #undef READ_COND
629
630 static int f_hidg_release(struct inode *inode, struct file *fd)
631 {
632         fd->private_data = NULL;
633         return 0;
634 }
635
636 static int f_hidg_open(struct inode *inode, struct file *fd)
637 {
638         struct f_hidg *hidg =
639                 container_of(inode->i_cdev, struct f_hidg, cdev);
640
641         fd->private_data = hidg;
642
643         return 0;
644 }
645
646 /*-------------------------------------------------------------------------*/
647 /*                                usb_function                             */
648
649 void hidg_connect()
650 {
651     if(g_hidg)
652         g_hidg->connected = 1; 
653 }
654
655 void hidg_disconnect()
656 {
657     if(g_hidg){
658         g_hidg->connected = 0;
659     }
660 }
661 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
662
663 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
664 {
665         struct f_hidg *hidg = (struct f_hidg *)req->context;
666     printk("hidg_set_report_complete ,req->status = %d len = %d\n",
667     req->status,req->actual);
668         if (req->status != 0 || req->buf == NULL || req->actual == 0) {
669                 return;
670         }
671     
672         spin_lock(&hidg->spinlock);
673
674     if(!hidg->connected)
675         //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
676         hidg_connect();
677     
678         hidg->set_report_buff = krealloc(hidg->set_report_buff,
679                                          req->actual, GFP_ATOMIC);
680
681         if (hidg->set_report_buff == NULL) {
682                 spin_unlock(&hidg->spinlock);
683                 return;
684         }
685         hidg->set_report_length = req->actual;
686         memcpy(hidg->set_report_buff, req->buf, req->actual);
687     
688         spin_unlock(&hidg->spinlock);
689
690         wake_up(&hidg->read_queue);
691 }
692
693 static int hidg_setup(struct usb_function *f,
694                 const struct usb_ctrlrequest *ctrl)
695 {
696         struct f_hidg                   *hidg = func_to_hidg(f);
697         struct usb_composite_dev        *cdev = f->config->cdev;
698         struct usb_request              *req  = cdev->req;
699         int status = 0;
700         __u16 value, length;
701
702         value   = __le16_to_cpu(ctrl->wValue);
703         length  = __le16_to_cpu(ctrl->wLength);
704
705         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
706                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
707
708         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
709         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
710                   | HID_REQ_GET_REPORT):
711                 VDBG(cdev, "get_report\n");
712
713                 /* send an empty report */
714                 length = min_t(unsigned, length, hidg->report_length);
715                 memset(req->buf, 0x0, length);
716
717                 goto respond;
718                 break;
719
720         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
721                   | HID_REQ_GET_PROTOCOL):
722                 VDBG(cdev, "get_protocol\n");
723                 goto stall;
724                 break;
725
726         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
727                   | HID_REQ_SET_REPORT):
728                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
729                 req->context  = hidg;
730                 req->complete = hidg_set_report_complete;
731                 goto respond;
732                 break;
733
734         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
735                   | HID_REQ_SET_PROTOCOL):
736                 VDBG(cdev, "set_protocol\n");
737                 goto stall;
738                 break;
739
740         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
741                   | USB_REQ_GET_DESCRIPTOR):
742                 switch (value >> 8) {
743                 case HID_DT_REPORT:
744                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
745                         length = min_t(unsigned short, length,
746                                                    hidg->report_desc_length);
747                         memcpy(req->buf, hidg->report_desc, length);
748                         goto respond;
749                         break;
750
751                 default:
752                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
753                                  value >> 8);
754                         goto stall;
755                         break;
756                 }
757                 break;
758
759         default:
760                 VDBG(cdev, "Unknown request 0x%x\n",
761                          ctrl->bRequest);
762                 goto stall;
763                 break;
764         }
765
766 stall:
767         return -EOPNOTSUPP;
768
769 respond:
770         req->zero = 0;
771         req->length = length;
772         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
773         if (status < 0)
774                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
775         return status;
776 }
777
778 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
779                 const struct usb_ctrlrequest *ctrl)
780 {
781         struct f_hidg                   *hidg = g_hidg;
782         struct usb_request              *req  = cdev->req;
783         int status = 0;
784         __u16 value, length;
785
786         value   = __le16_to_cpu(ctrl->wValue);
787         length  = __le16_to_cpu(ctrl->wLength);
788
789     /*
790         printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
791                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
792     */
793         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
794         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
795                   | HID_REQ_GET_REPORT):
796                 VDBG(cdev, "get_report\n");
797         return -EOPNOTSUPP;
798
799                 /* send an empty report */
800                 length = min_t(unsigned, length, hidg->report_length);
801                 memset(req->buf, 0x0, length);
802                 goto respond;
803                 break;
804
805         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
806                   | HID_REQ_GET_PROTOCOL):
807                 VDBG(cdev, "get_protocol\n");
808                 goto stall;
809                 break;
810
811         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
812                   | HID_REQ_SET_REPORT):
813                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
814                 req->context  = hidg;
815                 req->complete = hidg_set_report_complete;
816                 goto respond;
817                 break;
818
819         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
820                   | HID_REQ_SET_PROTOCOL):
821                 VDBG(cdev, "set_protocol\n");
822                 goto stall;
823                 break;
824
825         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
826                   | USB_REQ_GET_DESCRIPTOR):
827                 switch (value >> 8) {
828                 case HID_DT_REPORT:
829                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
830                         length = min_t(unsigned short, length,
831                                                    hidg->report_desc_length);
832                         memcpy(req->buf, hidg->report_desc, length);
833                         goto respond;
834                         break;
835
836                 default:
837                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
838                                  value >> 8);
839                         goto stall;
840                         break;
841                 }
842                 break;
843
844         default:
845                 VDBG(cdev, "Unknown request 0x%x\n",
846                          ctrl->bRequest);
847                 goto stall;
848                 break;
849         }
850
851 stall:
852         return -EOPNOTSUPP;
853
854 respond:
855         req->zero = 0;
856         req->length = length;
857         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
858         if (status < 0)
859                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
860         return status;
861 }
862
863 static void hidg_disable(struct usb_function *f)
864 {
865         struct f_hidg *hidg = func_to_hidg(f);
866
867         usb_ep_disable(hidg->in_ep);
868         hidg->in_ep->driver_data = NULL;
869 }
870
871 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
872 {
873         struct usb_composite_dev                *cdev = f->config->cdev;
874         struct f_hidg                           *hidg = func_to_hidg(f);
875         const struct usb_endpoint_descriptor    *ep_desc;
876         int status = 0;
877
878         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
879
880         if (hidg->in_ep != NULL) {
881                 /* restart endpoint */
882                 if (hidg->in_ep->driver_data != NULL)
883                         usb_ep_disable(hidg->in_ep);
884
885                 ep_desc = ep_choose(f->config->cdev->gadget,
886                                 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
887                 status = usb_ep_enable(hidg->in_ep, ep_desc);
888                 if (status < 0) {
889                         //ERROR(cdev, "Enable endpoint FAILED!\n");
890                         goto fail;
891                 }
892                 hidg->in_ep->driver_data = hidg;
893         }
894 fail:
895         return status;
896 }
897
898 const struct file_operations f_hidg_fops = {
899         .owner          = THIS_MODULE,
900         .open           = f_hidg_open,
901         .release        = f_hidg_release,
902         .write          = NULL,//f_hidg_write,disable write to /dev/hidg0
903         .read           = f_hidg_read,
904         .poll           = f_hidg_poll,
905         .llseek         = noop_llseek,
906 };
907
908 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
909 {
910         struct usb_ep           *ep_in;
911         struct f_hidg           *hidg = func_to_hidg(f);
912         int                     status;
913         dev_t                   dev;
914         /* allocate instance-specific interface IDs, and patch descriptors */
915         status = usb_interface_id(c, f);
916         if (status < 0)
917                 goto fail;
918         hidg_interface_desc.bInterfaceNumber = status;
919
920         /* allocate instance-specific endpoints */
921         status = -ENODEV;
922         ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
923         if (!ep_in)
924                 goto fail;
925         ep_in->driver_data = c->cdev;   /* claim */
926         hidg->in_ep = ep_in;
927 #if 0
928     /* allocate out endpoint*/
929         ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
930         if (!ep_out)
931                 goto fail;
932         ep_out->driver_data = c->cdev;  /* claim */
933         hidg->out_ep = ep_out; 
934
935         printk("ep_out->name = %s\n",ep_out->name);
936 #endif
937         /* preallocate request and buffer */
938         status = -ENOMEM;
939         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
940         if (!hidg->req)
941                 goto fail;
942
943
944         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
945         if (!hidg->req->buf)
946                 goto fail;
947
948         /* set descriptor dynamic values */
949         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
950         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
951 //      hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
952 //      hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
953 //    hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
954 //      hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
955         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
956         hidg_desc.desc[0].wDescriptorLength =
957                 cpu_to_le16(hidg->report_desc_length);
958
959         hidg->set_report_buff = NULL;
960
961         /* copy descriptors */
962         f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
963         if (!f->descriptors)
964                 goto fail;
965
966         hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
967                                                 f->descriptors,
968                                                 &hidg_fs_in_ep_desc);
969
970         if (gadget_is_dualspeed(c->cdev->gadget)) {
971                 hidg_hs_in_ep_desc.bEndpointAddress =
972                         hidg_fs_in_ep_desc.bEndpointAddress;
973                 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
974                 if (!f->hs_descriptors)
975                         goto fail;
976                 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
977                                                         f->hs_descriptors,
978                                                         &hidg_hs_in_ep_desc);
979         } else {
980                 hidg->hs_in_ep_desc = NULL;
981         }
982
983     hidg->connected = 0;
984
985         mutex_init(&hidg->lock);
986         spin_lock_init(&hidg->spinlock);
987         init_waitqueue_head(&hidg->write_queue);
988         init_waitqueue_head(&hidg->read_queue);
989
990         /* create char device */
991         cdev_init(&hidg->cdev, &f_hidg_fops);
992         dev = MKDEV(major, hidg->minor);
993         status = cdev_add(&hidg->cdev, dev, 1);
994         if (status)
995                 goto fail;
996
997         device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
998
999         return 0;
1000
1001 fail:
1002         ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
1003         if (hidg->req != NULL) {
1004                 kfree(hidg->req->buf);
1005                 if (hidg->in_ep != NULL)
1006                         usb_ep_free_request(hidg->in_ep, hidg->req);
1007         }
1008     g_hidg = NULL;
1009         usb_free_descriptors(f->hs_descriptors);
1010         usb_free_descriptors(f->descriptors);
1011
1012         return status;
1013 }
1014
1015 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1016 {
1017         struct f_hidg *hidg = func_to_hidg(f);
1018
1019     f_hid_bypass_input_set(0);
1020
1021         device_destroy(hidg_class, MKDEV(major, hidg->minor));
1022         cdev_del(&hidg->cdev);
1023
1024         /* disable/free request and end point */
1025         usb_ep_disable(hidg->in_ep);
1026         usb_ep_dequeue(hidg->in_ep, hidg->req);
1027         if(hidg->req->buf)
1028         kfree(hidg->req->buf);
1029         usb_ep_free_request(hidg->in_ep, hidg->req);
1030
1031         /* free descriptors copies */
1032         usb_free_descriptors(f->hs_descriptors);
1033         usb_free_descriptors(f->descriptors);
1034         
1035     
1036         kfree(hidg->report_desc);
1037         kfree(hidg->set_report_buff);
1038         kfree(hidg);
1039         
1040         g_hidg = NULL;
1041 }
1042
1043 /*-------------------------------------------------------------------------*/
1044 /*                                 Strings                                 */
1045
1046 #define CT_FUNC_HID_IDX 0
1047
1048 static struct usb_string ct_func_string_defs[] = {
1049         [CT_FUNC_HID_IDX].s     = "HID Interface",
1050         {},                     /* end of list */
1051 };
1052
1053 static struct usb_gadget_strings ct_func_string_table = {
1054         .language       = 0x0409,       /* en-US */
1055         .strings        = ct_func_string_defs,
1056 };
1057
1058 static struct usb_gadget_strings *ct_func_strings[] = {
1059         &ct_func_string_table,
1060         NULL,
1061 };
1062
1063 /*-------------------------------------------------------------------------*/
1064 /*                             usb_configuration                           */
1065
1066 int hidg_bind_config(struct usb_configuration *c,
1067                             const struct hidg_func_descriptor *fdesc, int index)
1068 {
1069         struct f_hidg *hidg;
1070         int status;
1071         if (index >= minors)
1072                 return -ENOENT;
1073
1074         /* maybe allocate device-global string IDs, and patch descriptors */
1075         if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
1076                 status = usb_string_id(c->cdev);
1077                 if (status < 0)
1078                         return status;
1079                 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1080                 hidg_interface_desc.iInterface = status;
1081         }
1082
1083         /* allocate and initialize one new instance */
1084         hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1085         if (!hidg)
1086                 return -ENOMEM;
1087         g_hidg = hidg;
1088         hidg->bypass_input = 0;
1089         hidg->minor = index;
1090         hidg->bInterfaceSubClass = fdesc->subclass;
1091         hidg->bInterfaceProtocol = fdesc->protocol;
1092         hidg->report_length = fdesc->report_length;
1093         hidg->report_desc_length = fdesc->report_desc_length;
1094         hidg->report_desc = kmemdup(fdesc->report_desc,
1095                                     fdesc->report_desc_length,
1096                                     GFP_KERNEL);
1097         if (!hidg->report_desc) {
1098                 kfree(hidg);
1099                 return -ENOMEM;
1100         }
1101
1102         hidg->func.name    = "hid";
1103         hidg->func.strings = ct_func_strings;
1104         hidg->func.bind    = hidg_bind;
1105         hidg->func.unbind  = hidg_unbind;
1106         hidg->func.set_alt = hidg_set_alt;
1107         hidg->func.disable = hidg_disable;
1108         hidg->func.setup   = hidg_setup;
1109     
1110         status = usb_add_function(c, &hidg->func);
1111         if (status)
1112                 kfree(hidg);
1113         else
1114         g_hidg = hidg;
1115         return status;
1116 }
1117
1118 int ghid_setup(struct usb_gadget *g, int count)
1119 {
1120         int status;
1121         dev_t dev;
1122
1123         hidg_class = class_create(THIS_MODULE, "hidg");
1124
1125         status = alloc_chrdev_region(&dev, 0, count, "hidg");
1126         if (!status) {
1127                 major = MAJOR(dev);
1128                 minors = count;
1129         }
1130
1131         return status;
1132 }
1133
1134 void ghid_cleanup(void)
1135 {
1136         if (major) {
1137                 unregister_chrdev_region(MKDEV(major, 0), minors);
1138                 major = minors = 0;
1139         }
1140
1141         class_destroy(hidg_class);
1142 }