gadget hid: update to support multimedia key-board
[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     u8        button:8;
541     signed    x :12;
542     signed    y :12;
543     s8        wheel:8;
544 }__attribute__ ((packed));
545
546
547 void f_hid_mouse_translate_report(struct hid_report *report, u8 *data)
548 {
549
550     if(f_hid_bypass_input_get())
551     {
552         struct mouse_report m = {0};
553         struct hid_field *field;
554         
555         int i,j;
556         m.id     = MOUSE_REPORT_ID;
557         for (i = 0; i < report->maxfield; i++){
558             field = report->field[i];
559             for(j=0; j<field->report_count; j++)
560             {
561                 if((field->usage[j].type == EV_KEY) && (field->usage[j].code == BTN_MOUSE))
562                     if(field->value[j])
563                         m.button |= 1 << j;
564                 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_X))
565                     m.x = field->value[j];
566                 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_Y))
567                     m.y = field->value[j];
568                 if((field->usage[j].type == EV_REL) && (field->usage[j].code == REL_WHEEL))
569                     m.wheel= field->value[j];
570             }
571         }
572         f_hid_queue_report((u8 *)&m, sizeof(m));
573     }
574 }
575 EXPORT_SYMBOL(f_hid_mouse_translate_report);
576
577 #undef KBD_REPORT_ID
578 #undef MOUSE_REPORT_ID
579 #undef CONSUMER_REPORT_ID
580
581
582 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
583 {
584         struct f_hidg   *hidg  = file->private_data;
585         unsigned int    ret = 0;
586
587         poll_wait(file, &hidg->read_queue, wait);
588         poll_wait(file, &hidg->write_queue, wait);
589
590         if (WRITE_COND)
591                 ret |= POLLOUT | POLLWRNORM;
592
593         if (READ_COND)
594                 ret |= POLLIN | POLLRDNORM;
595
596         return ret;
597 }
598
599 #undef WRITE_COND
600 #undef READ_COND
601
602 static int f_hidg_release(struct inode *inode, struct file *fd)
603 {
604         fd->private_data = NULL;
605         return 0;
606 }
607
608 static int f_hidg_open(struct inode *inode, struct file *fd)
609 {
610         struct f_hidg *hidg =
611                 container_of(inode->i_cdev, struct f_hidg, cdev);
612
613         fd->private_data = hidg;
614
615         return 0;
616 }
617
618 /*-------------------------------------------------------------------------*/
619 /*                                usb_function                             */
620
621 void hidg_connect()
622 {
623     if(g_hidg)
624         g_hidg->connected = 1; 
625 }
626
627 void hidg_disconnect()
628 {
629     if(g_hidg){
630         g_hidg->connected = 0;
631     }
632 }
633 DECLARE_DELAYED_WORK(hidg_cnt, hidg_connect);
634
635 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
636 {
637         struct f_hidg *hidg = (struct f_hidg *)req->context;
638     printk("hidg_set_report_complete ,req->status = %d len = %d\n",
639     req->status,req->actual);
640         if (req->status != 0 || req->buf == NULL || req->actual == 0) {
641                 return;
642         }
643     
644         spin_lock(&hidg->spinlock);
645
646     if(!hidg->connected)
647         //schedule_delayed_work(&hidg_cnt, msecs_to_jiffies(200));
648         hidg_connect();
649     
650         hidg->set_report_buff = krealloc(hidg->set_report_buff,
651                                          req->actual, GFP_ATOMIC);
652
653         if (hidg->set_report_buff == NULL) {
654                 spin_unlock(&hidg->spinlock);
655                 return;
656         }
657         hidg->set_report_length = req->actual;
658         memcpy(hidg->set_report_buff, req->buf, req->actual);
659     
660         spin_unlock(&hidg->spinlock);
661
662         wake_up(&hidg->read_queue);
663 }
664
665 static int hidg_setup(struct usb_function *f,
666                 const struct usb_ctrlrequest *ctrl)
667 {
668         struct f_hidg                   *hidg = func_to_hidg(f);
669         struct usb_composite_dev        *cdev = f->config->cdev;
670         struct usb_request              *req  = cdev->req;
671         int status = 0;
672         __u16 value, length;
673
674         value   = __le16_to_cpu(ctrl->wValue);
675         length  = __le16_to_cpu(ctrl->wLength);
676
677         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
678                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
679
680         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
681         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
682                   | HID_REQ_GET_REPORT):
683                 VDBG(cdev, "get_report\n");
684
685                 /* send an empty report */
686                 length = min_t(unsigned, length, hidg->report_length);
687                 memset(req->buf, 0x0, length);
688
689                 goto respond;
690                 break;
691
692         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
693                   | HID_REQ_GET_PROTOCOL):
694                 VDBG(cdev, "get_protocol\n");
695                 goto stall;
696                 break;
697
698         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
699                   | HID_REQ_SET_REPORT):
700                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
701                 req->context  = hidg;
702                 req->complete = hidg_set_report_complete;
703                 goto respond;
704                 break;
705
706         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
707                   | HID_REQ_SET_PROTOCOL):
708                 VDBG(cdev, "set_protocol\n");
709                 goto stall;
710                 break;
711
712         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
713                   | USB_REQ_GET_DESCRIPTOR):
714                 switch (value >> 8) {
715                 case HID_DT_REPORT:
716                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
717                         length = min_t(unsigned short, length,
718                                                    hidg->report_desc_length);
719                         memcpy(req->buf, hidg->report_desc, length);
720                         goto respond;
721                         break;
722
723                 default:
724                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
725                                  value >> 8);
726                         goto stall;
727                         break;
728                 }
729                 break;
730
731         default:
732                 VDBG(cdev, "Unknown request 0x%x\n",
733                          ctrl->bRequest);
734                 goto stall;
735                 break;
736         }
737
738 stall:
739         return -EOPNOTSUPP;
740
741 respond:
742         req->zero = 0;
743         req->length = length;
744         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
745         if (status < 0)
746                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
747         return status;
748 }
749
750 static int hidg_ctrlrequest(struct usb_composite_dev *cdev,
751                 const struct usb_ctrlrequest *ctrl)
752 {
753         struct f_hidg                   *hidg = g_hidg;
754         struct usb_request              *req  = cdev->req;
755         int status = 0;
756         __u16 value, length;
757
758         value   = __le16_to_cpu(ctrl->wValue);
759         length  = __le16_to_cpu(ctrl->wLength);
760
761     /*
762         printk("hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
763                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
764     */
765         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
766         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
767                   | HID_REQ_GET_REPORT):
768                 VDBG(cdev, "get_report\n");
769
770                 /* send an empty report */
771                 length = min_t(unsigned, length, hidg->report_length);
772                 memset(req->buf, 0x0, length);
773
774                 goto respond;
775                 break;
776
777         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
778                   | HID_REQ_GET_PROTOCOL):
779                 VDBG(cdev, "get_protocol\n");
780                 goto stall;
781                 break;
782
783         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
784                   | HID_REQ_SET_REPORT):
785                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
786                 req->context  = hidg;
787                 req->complete = hidg_set_report_complete;
788                 goto respond;
789                 break;
790
791         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
792                   | HID_REQ_SET_PROTOCOL):
793                 VDBG(cdev, "set_protocol\n");
794                 goto stall;
795                 break;
796
797         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
798                   | USB_REQ_GET_DESCRIPTOR):
799                 switch (value >> 8) {
800                 case HID_DT_REPORT:
801                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
802                         length = min_t(unsigned short, length,
803                                                    hidg->report_desc_length);
804                         memcpy(req->buf, hidg->report_desc, length);
805                         goto respond;
806                         break;
807
808                 default:
809                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
810                                  value >> 8);
811                         goto stall;
812                         break;
813                 }
814                 break;
815
816         default:
817                 VDBG(cdev, "Unknown request 0x%x\n",
818                          ctrl->bRequest);
819                 goto stall;
820                 break;
821         }
822
823 stall:
824         return -EOPNOTSUPP;
825
826 respond:
827         req->zero = 0;
828         req->length = length;
829         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
830         if (status < 0)
831                 ;//ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
832         return status;
833 }
834
835 static void hidg_disable(struct usb_function *f)
836 {
837         struct f_hidg *hidg = func_to_hidg(f);
838
839         usb_ep_disable(hidg->in_ep);
840         hidg->in_ep->driver_data = NULL;
841 }
842
843 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
844 {
845         struct usb_composite_dev                *cdev = f->config->cdev;
846         struct f_hidg                           *hidg = func_to_hidg(f);
847         const struct usb_endpoint_descriptor    *ep_desc;
848         int status = 0;
849
850         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
851
852         if (hidg->in_ep != NULL) {
853                 /* restart endpoint */
854                 if (hidg->in_ep->driver_data != NULL)
855                         usb_ep_disable(hidg->in_ep);
856
857                 ep_desc = ep_choose(f->config->cdev->gadget,
858                                 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
859                 status = usb_ep_enable(hidg->in_ep, ep_desc);
860                 if (status < 0) {
861                         //ERROR(cdev, "Enable endpoint FAILED!\n");
862                         goto fail;
863                 }
864                 hidg->in_ep->driver_data = hidg;
865         }
866 fail:
867         return status;
868 }
869
870 const struct file_operations f_hidg_fops = {
871         .owner          = THIS_MODULE,
872         .open           = f_hidg_open,
873         .release        = f_hidg_release,
874         .write          = NULL,//f_hidg_write,disable write to /dev/hidg0
875         .read           = f_hidg_read,
876         .poll           = f_hidg_poll,
877         .llseek         = noop_llseek,
878 };
879
880 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
881 {
882         struct usb_ep           *ep_in;
883         struct f_hidg           *hidg = func_to_hidg(f);
884         int                     status;
885         dev_t                   dev;
886         /* allocate instance-specific interface IDs, and patch descriptors */
887         status = usb_interface_id(c, f);
888         if (status < 0)
889                 goto fail;
890         hidg_interface_desc.bInterfaceNumber = status;
891
892         /* allocate instance-specific endpoints */
893         status = -ENODEV;
894         ep_in = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
895         if (!ep_in)
896                 goto fail;
897         ep_in->driver_data = c->cdev;   /* claim */
898         hidg->in_ep = ep_in;
899 #if 0
900     /* allocate out endpoint*/
901         ep_out = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
902         if (!ep_out)
903                 goto fail;
904         ep_out->driver_data = c->cdev;  /* claim */
905         hidg->out_ep = ep_out; 
906
907         printk("ep_out->name = %s\n",ep_out->name);
908 #endif
909         /* preallocate request and buffer */
910         status = -ENOMEM;
911         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
912         if (!hidg->req)
913                 goto fail;
914
915
916         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
917         if (!hidg->req->buf)
918                 goto fail;
919
920         /* set descriptor dynamic values */
921         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
922         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
923 //      hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
924 //      hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
925 //    hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
926 //      hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
927         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
928         hidg_desc.desc[0].wDescriptorLength =
929                 cpu_to_le16(hidg->report_desc_length);
930
931         hidg->set_report_buff = NULL;
932
933         /* copy descriptors */
934         f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
935         if (!f->descriptors)
936                 goto fail;
937
938         hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
939                                                 f->descriptors,
940                                                 &hidg_fs_in_ep_desc);
941
942         if (gadget_is_dualspeed(c->cdev->gadget)) {
943                 hidg_hs_in_ep_desc.bEndpointAddress =
944                         hidg_fs_in_ep_desc.bEndpointAddress;
945                 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
946                 if (!f->hs_descriptors)
947                         goto fail;
948                 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
949                                                         f->hs_descriptors,
950                                                         &hidg_hs_in_ep_desc);
951         } else {
952                 hidg->hs_in_ep_desc = NULL;
953         }
954
955     hidg->connected = 0;
956
957         mutex_init(&hidg->lock);
958         spin_lock_init(&hidg->spinlock);
959         init_waitqueue_head(&hidg->write_queue);
960         init_waitqueue_head(&hidg->read_queue);
961
962         /* create char device */
963         cdev_init(&hidg->cdev, &f_hidg_fops);
964         dev = MKDEV(major, hidg->minor);
965         status = cdev_add(&hidg->cdev, dev, 1);
966         if (status)
967                 goto fail;
968
969         device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
970
971         return 0;
972
973 fail:
974         ;//ERROR(f->config->cdev, "hidg_bind FAILED\n");
975         if (hidg->req != NULL) {
976                 kfree(hidg->req->buf);
977                 if (hidg->in_ep != NULL)
978                         usb_ep_free_request(hidg->in_ep, hidg->req);
979         }
980     g_hidg = NULL;
981         usb_free_descriptors(f->hs_descriptors);
982         usb_free_descriptors(f->descriptors);
983
984         return status;
985 }
986
987 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
988 {
989         struct f_hidg *hidg = func_to_hidg(f);
990
991     f_hid_bypass_input_set(0);
992
993         device_destroy(hidg_class, MKDEV(major, hidg->minor));
994         cdev_del(&hidg->cdev);
995
996         /* disable/free request and end point */
997         usb_ep_disable(hidg->in_ep);
998         usb_ep_dequeue(hidg->in_ep, hidg->req);
999         if(hidg->req->buf)
1000         kfree(hidg->req->buf);
1001         usb_ep_free_request(hidg->in_ep, hidg->req);
1002
1003         /* free descriptors copies */
1004         usb_free_descriptors(f->hs_descriptors);
1005         usb_free_descriptors(f->descriptors);
1006         
1007     
1008         kfree(hidg->report_desc);
1009         kfree(hidg->set_report_buff);
1010         kfree(hidg);
1011         
1012         g_hidg = NULL;
1013 }
1014
1015 /*-------------------------------------------------------------------------*/
1016 /*                                 Strings                                 */
1017
1018 #define CT_FUNC_HID_IDX 0
1019
1020 static struct usb_string ct_func_string_defs[] = {
1021         [CT_FUNC_HID_IDX].s     = "HID Interface",
1022         {},                     /* end of list */
1023 };
1024
1025 static struct usb_gadget_strings ct_func_string_table = {
1026         .language       = 0x0409,       /* en-US */
1027         .strings        = ct_func_string_defs,
1028 };
1029
1030 static struct usb_gadget_strings *ct_func_strings[] = {
1031         &ct_func_string_table,
1032         NULL,
1033 };
1034
1035 /*-------------------------------------------------------------------------*/
1036 /*                             usb_configuration                           */
1037
1038 int hidg_bind_config(struct usb_configuration *c,
1039                             const struct hidg_func_descriptor *fdesc, int index)
1040 {
1041         struct f_hidg *hidg;
1042         int status;
1043         if (index >= minors)
1044                 return -ENOENT;
1045
1046         /* maybe allocate device-global string IDs, and patch descriptors */
1047         if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
1048                 status = usb_string_id(c->cdev);
1049                 if (status < 0)
1050                         return status;
1051                 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
1052                 hidg_interface_desc.iInterface = status;
1053         }
1054
1055         /* allocate and initialize one new instance */
1056         hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
1057         if (!hidg)
1058                 return -ENOMEM;
1059         g_hidg = hidg;
1060         hidg->bypass_input = 0;
1061         hidg->minor = index;
1062         hidg->bInterfaceSubClass = fdesc->subclass;
1063         hidg->bInterfaceProtocol = fdesc->protocol;
1064         hidg->report_length = fdesc->report_length;
1065         hidg->report_desc_length = fdesc->report_desc_length;
1066         hidg->report_desc = kmemdup(fdesc->report_desc,
1067                                     fdesc->report_desc_length,
1068                                     GFP_KERNEL);
1069         if (!hidg->report_desc) {
1070                 kfree(hidg);
1071                 return -ENOMEM;
1072         }
1073
1074         hidg->func.name    = "hid";
1075         hidg->func.strings = ct_func_strings;
1076         hidg->func.bind    = hidg_bind;
1077         hidg->func.unbind  = hidg_unbind;
1078         hidg->func.set_alt = hidg_set_alt;
1079         hidg->func.disable = hidg_disable;
1080         hidg->func.setup   = hidg_setup;
1081     
1082         status = usb_add_function(c, &hidg->func);
1083         if (status)
1084                 kfree(hidg);
1085         else
1086         g_hidg = hidg;
1087         return status;
1088 }
1089
1090 int ghid_setup(struct usb_gadget *g, int count)
1091 {
1092         int status;
1093         dev_t dev;
1094
1095         hidg_class = class_create(THIS_MODULE, "hidg");
1096
1097         status = alloc_chrdev_region(&dev, 0, count, "hidg");
1098         if (!status) {
1099                 major = MAJOR(dev);
1100                 minors = count;
1101         }
1102
1103         return status;
1104 }
1105
1106 void ghid_cleanup(void)
1107 {
1108         if (major) {
1109                 unregister_chrdev_region(MKDEV(major, 0), minors);
1110                 major = minors = 0;
1111         }
1112
1113         class_destroy(hidg_class);
1114 }