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