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