Merge branch 'linux-linaro-lsk-v4.4-android' of git://git.linaro.org/kernel/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / hid / hid-rkvr.c
1 /*
2  * Rockchip VR driver for Linux
3  *
4  * Copyright (C) ROCKCHIP, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  */
11
12 /*
13  * Driver for Rockchip VR devices. Based on hidraw driver.
14  */
15
16 #include <linux/cdev.h>
17 #include <linux/poll.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 #include <linux/hidraw.h>
22 #include <linux/input.h>
23 #include <linux/platform_device.h>
24 #include <linux/notifier.h>
25 #include <linux/fb.h>
26 #include "hid-rkvr.h"
27 #include "hid-ids.h"
28
29 #define USB_TRACKER_INTERFACE_PROTOCOL  0
30 /* define rkvr interface number */
31 #define RKVR_INTERFACE_USB_AUDIO_ID 1
32 #define RKVR_INTERFACE_USB_SENSOR_ID 2
33 #define RKVR_INTERFACE_USB_AUDIO_KEY_ID 1
34 /* number of reports to buffer */
35 #define RKVR_HIDRAW_BUFFER_SIZE 64
36 #define RKVR_HIDRAW_MAX_DEVICES 8
37 #define RKVR_FIRST_MINOR 0
38 #define RK_HID_GEAR_TOUCH
39
40 static struct class *rkvr_class;
41
42 static struct hidraw *rkvr_hidraw_table[RKVR_HIDRAW_MAX_DEVICES];
43
44 static struct hid_capability
45 {
46         __u8 suspend_notify;
47 } rkvr_hid_capability[RKVR_HIDRAW_MAX_DEVICES];
48 static DEFINE_MUTEX(minors_lock);
49
50 struct keymap_t {
51         __u16 key_menu_up:1;
52         __u16 key_menu_down:1;
53         __u16 key_home_up:1;
54         __u16 key_home_down:1;
55         __u16 key_power_up:1;
56         __u16 key_power_down:1;
57         __u16 key_volup_up:1;
58         __u16 key_volup_down:1;
59         __u16 key_voldn_up:1;
60         __u16 key_voldn_down:1;
61         __u16 key_esc_up:1;
62         __u16 key_esc_down:1;
63         /*for touch panel **/
64         __u16 key_up_pressed:1;
65         __u16 key_up_released:1;
66         __u16 key_down_pressed:1;
67         __u16 key_down_released:1;
68         __u16 key_left_pressed:1;
69         __u16 key_left_released:1;
70         __u16 key_right_pressed:1;
71         __u16 key_right_released:1;
72         __u16 key_enter_pressed:1;
73         __u16 key_enter_released:1;
74         __u16 key_pressed:1;
75         __u16 psensor_on:1;
76         __u16 psensor_off:1;
77 } __packed;
78
79 union rkvr_data_t {
80         struct rkvr_data {
81                 __u8 buf_head[6];
82                 __u8 buf_sensortemperature[2];
83                 __u8 buf_sensor[40];
84                 __u8 buf_reserve[10];
85                 struct keymap_t key_map;
86         } rkvr_data;
87         __u8 buf[62];
88 } __packed;
89
90 static int rkvr_major;
91 static struct cdev rkvr_cdev;
92 static unsigned int count_array[15] = {0,};
93 static unsigned long old_jiffy_array[15] = {0,};
94 static int rkvr_index;
95 static int opens;
96
97 struct sensor_hid_data {
98         void *priv;
99         int (*send_event)(char *raw_data, size_t raw_len, void *priv);
100 } sensorData;
101
102 static DEFINE_MUTEX(device_list_lock);
103 static struct list_head rkvr_hid_hw_device_list = {
104         .next = &rkvr_hid_hw_device_list,
105         .prev = &rkvr_hid_hw_device_list
106 };
107
108 static struct rkvr_iio_hw_device *inv_hid_alloc(const char *name)
109 {
110         struct rkvr_iio_hw_device *p;
111         const char *s;
112
113         if (!name)
114                 return 0;
115         s = kstrdup_const(name, GFP_KERNEL);
116         if (!s)
117                 goto error;
118         p = kzalloc(sizeof(*p), GFP_KERNEL);
119         if (!p)
120                 goto error;
121         p->name = s;
122         return p;
123 error:
124         pr_err("%s error!\n", __func__);
125         if (s)
126                 kfree_const(s);
127         return 0;
128 }
129
130 static void inv_hid_free(struct rkvr_iio_hw_device *hw_device)
131 {
132         kfree_const(hw_device->name);
133         kfree(hw_device);
134 }
135
136 static int inv_hid_register_devcie(struct rkvr_iio_hw_device *hw_device)
137 {
138
139         mutex_lock(&device_list_lock);
140         if (hw_device->name && (!list_empty(&rkvr_hid_hw_device_list))) {
141                 struct rkvr_iio_hw_device *p;
142
143                 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
144                         if (!strcmp(hw_device->name, p->name)) {
145                                 pr_err("%s already exist ,abort\n", hw_device->name);
146                                 mutex_unlock(&device_list_lock);
147                                 return -1;
148                         }
149                 }
150         }
151         list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
152         mutex_unlock(&device_list_lock);
153         return 0;
154 }
155
156 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
157 {
158         struct rkvr_iio_hw_device *p = NULL;
159
160         mutex_lock(&device_list_lock);
161         list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
162                 if (!strcmp(name, p->name)) {
163                         list_del(&p->l);
164                         break;
165                 }
166         }
167         if (p) {
168                 pr_info("find dev with name %s,free now\n", name);
169                 inv_hid_free(p);
170         }
171         mutex_unlock(&device_list_lock);
172 }
173
174 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
175 {
176         struct hidraw_list *list = file->private_data;
177         int ret = 0, len;
178         DECLARE_WAITQUEUE(wait, current);
179
180         mutex_lock(&list->read_mutex);
181         while (ret == 0) {
182                 if (list->head == list->tail) {
183                         add_wait_queue(&list->hidraw->wait, &wait);
184                         set_current_state(TASK_INTERRUPTIBLE);
185
186                         while (list->head == list->tail) {
187                                 if (signal_pending(current)) {
188                                         ret = -ERESTARTSYS;
189                                         break;
190                                 }
191                                 if (!list->hidraw->exist) {
192                                         ret = -EIO;
193                                         break;
194                                 }
195                                 if (file->f_flags & O_NONBLOCK) {
196                                         ret = -EAGAIN;
197                                         break;
198                                 }
199
200                                 /* allow O_NONBLOCK to work well from other threads */
201                                 mutex_unlock(&list->read_mutex);
202                                 schedule();
203                                 mutex_lock(&list->read_mutex);
204                                 set_current_state(TASK_INTERRUPTIBLE);
205                         }
206
207                         set_current_state(TASK_RUNNING);
208                         remove_wait_queue(&list->hidraw->wait, &wait);
209                 }
210
211                 if (ret)
212                         goto out;
213
214                 len = list->buffer[list->tail].len > count ?
215                         count : list->buffer[list->tail].len;
216
217                 if (list->buffer[list->tail].value) {
218                         if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
219                                 ret = -EFAULT;
220                                 goto out;
221                         }
222                         ret = len;
223                         if (opens > 0 && rkvr_index < 15) {
224                                 if (++count_array[rkvr_index] >= 1000) {
225                                         unsigned long cur_jiffy = jiffies;
226
227                                         hid_dbg(list->hidraw->hid, "rkvr: %d Hz, read(%d) (%d:%s)\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy_array[rkvr_index])), rkvr_index, current->pid, current->comm);
228                                         count_array[rkvr_index] = 0;
229                                         old_jiffy_array[rkvr_index] = cur_jiffy;
230                                 }
231                                 if (++rkvr_index >= opens)
232                                         rkvr_index = 0;
233                         } else {
234                                 rkvr_index = 0;
235                         }
236                 }
237
238                 kfree(list->buffer[list->tail].value);
239                 list->buffer[list->tail].value = NULL;
240                 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
241         }
242 out:
243         mutex_unlock(&list->read_mutex);
244         return ret;
245 }
246
247 /* The first byte is expected to be a report number.
248  * This function is to be called with the minors_lock mutex held
249  */
250 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
251 {
252         unsigned int minor = iminor(file_inode(file));
253         struct hid_device *dev;
254         __u8 *buf;
255         int ret = 0;
256
257         if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
258                 ret = -ENODEV;
259                 goto out;
260         }
261
262         dev = rkvr_hidraw_table[minor]->hid;
263
264         if (count > HID_MAX_BUFFER_SIZE) {
265                 hid_warn(dev, "rkvr - pid %d passed too large report\n",
266                          task_pid_nr(current));
267                 ret = -EINVAL;
268                 goto out;
269         }
270
271         if (count < 2) {
272                 hid_warn(dev, "rkvr - pid %d passed too short report\n",
273                         task_pid_nr(current));
274                 ret = -EINVAL;
275                 goto out;
276         }
277
278         buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
279         if (!buf) {
280                 ret = -ENOMEM;
281                 goto out;
282         }
283
284         if (copy_from_user(buf, buffer, count)) {
285                 ret = -EFAULT;
286                 goto out_free;
287         }
288
289         if ((report_type == HID_OUTPUT_REPORT) &&
290                 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
291                 ret = hid_hw_output_report(dev, buf, count);
292                 /*
293                  * compatibility with old implementation of USB-HID and I2C-HID:
294                  * if the device does not support receiving output reports,
295                  * on an interrupt endpoint, fallback to SET_REPORT HID command.
296                  */
297                 if (ret != -ENOSYS)
298                         goto out_free;
299         }
300
301         ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
302                                 HID_REQ_SET_REPORT);
303
304 out_free:
305         kfree(buf);
306 out:
307         return ret;
308 }
309
310 /* the first byte is expected to be a report number */
311 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
312 {
313         ssize_t ret;
314
315         mutex_lock(&minors_lock);
316         ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
317         mutex_unlock(&minors_lock);
318         return ret;
319 }
320
321 /* This function performs a Get_Report transfer over the control endpoint
322  * per section 7.2.1 of the HID specification, version 1.1.  The first byte
323  * of buffer is the report number to request, or 0x0 if the defice does not
324  * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
325  * or HID_INPUT_REPORT.  This function is to be called with the minors_lock
326  *  mutex held.
327  */
328 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
329 {
330         unsigned int minor = iminor(file_inode(file));
331         struct hid_device *dev;
332         __u8 *buf;
333         int ret = 0, len;
334         unsigned char report_number;
335
336         dev = rkvr_hidraw_table[minor]->hid;
337
338         if (!dev->ll_driver->raw_request) {
339                 ret = -ENODEV;
340                 goto out;
341         }
342
343         if (count > HID_MAX_BUFFER_SIZE) {
344                 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
345                                 task_pid_nr(current));
346                 ret = -EINVAL;
347                 goto out;
348         }
349
350         if (count < 2) {
351                 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
352                                 task_pid_nr(current));
353                 ret = -EINVAL;
354                 goto out;
355         }
356
357         buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
358         if (!buf) {
359                 ret = -ENOMEM;
360                 goto out;
361         }
362
363         /*
364         * Read the first byte from the user. This is the report number,
365         * which is passed to hid_hw_raw_request().
366         */
367         if (copy_from_user(&report_number, buffer, 1)) {
368                 ret = -EFAULT;
369                 goto out_free;
370         }
371
372         ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
373                                  HID_REQ_GET_REPORT);
374         if (ret < 0)
375                 goto out_free;
376         len = (ret < count) ? ret : count;
377
378         if (copy_to_user(buffer, buf, len)) {
379                 ret = -EFAULT;
380                 goto out_free;
381         }
382
383         ret = len;
384
385 out_free:
386         kfree(buf);
387 out:
388         return ret;
389 }
390
391 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
392 {
393         struct hidraw_list *list = file->private_data;
394
395         poll_wait(file, &list->hidraw->wait, wait);
396         if (list->head != list->tail)
397                 return POLLIN | POLLRDNORM;
398         if (!list->hidraw->exist)
399                 return POLLERR | POLLHUP;
400
401         return 0;
402 }
403
404 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
405 {
406         unsigned int minor = iminor(inode);
407         struct hidraw *dev;
408         struct hidraw_list *list;
409         unsigned long flags;
410         int err = 0;
411
412         list = kzalloc(sizeof(*list), GFP_KERNEL);
413         if (!list) {
414                 err = -ENOMEM;
415                 goto out;
416         }
417
418         mutex_lock(&minors_lock);
419         if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
420                 err = -ENODEV;
421                 goto out_unlock;
422         }
423
424         dev = rkvr_hidraw_table[minor];
425         if (!dev->open++) {
426                 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
427                 if (err < 0) {
428                         dev->open--;
429                         goto out_unlock;
430                 }
431
432                 err = hid_hw_open(dev->hid);
433
434                 if (err < 0) {
435                         hid_hw_power(dev->hid, PM_HINT_NORMAL);
436                         dev->open--;
437                         goto out_unlock;
438                 }
439         }
440
441         list->hidraw = rkvr_hidraw_table[minor];
442         mutex_init(&list->read_mutex);
443         spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
444         list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
445         spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
446         file->private_data = list;
447
448         opens = dev->open;
449
450 out_unlock:
451         mutex_unlock(&minors_lock);
452 out:
453         if (err < 0)
454                 kfree(list);
455
456         return err;
457 }
458
459 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
460 {
461         struct hidraw_list *list = file->private_data;
462
463         return fasync_helper(fd, file, on, &list->fasync);
464 }
465
466 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
467 {
468         if (exists_bit) { /*hw removed**/
469                 hidraw->exist = 0;
470                 if (hidraw->open) {
471                         hid_hw_close(hidraw->hid);
472                         wake_up_interruptible(&hidraw->wait);
473                 }
474         } else {
475                 --hidraw->open;
476         }
477
478         if (!hidraw->open) {
479                 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
480                         rkvr_hidraw_table[hidraw->minor] = NULL;
481                         kfree(hidraw);
482                 } else {
483                         /* close device for last reader */
484                         hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
485                         hid_hw_close(hidraw->hid);
486                 }
487         }
488 }
489
490 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
491 {
492         unsigned int minor = iminor(inode);
493         struct hidraw_list *list = file->private_data;
494         unsigned long flags;
495
496         mutex_lock(&minors_lock);
497
498         spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
499         list_del(&list->node);
500         spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
501
502         kfree(list);
503         rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
504
505         mutex_unlock(&minors_lock);
506
507         return 0;
508 }
509
510 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
511 {
512         if (!input) {
513                 return;
514         }
515         if (state) {
516                 input_report_key(input, key_value, 1);
517                 input_sync(input);
518         } else {
519                 input_report_key(input, key_value, 0);
520                 input_sync(input);
521         }
522 }
523
524 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
525 {
526         struct input_dev *input = hdev->hiddev;
527         union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
528
529         if (rkvr_data->rkvr_data.key_map.key_menu_up)
530                 rkvr_send_key_event(input, KEY_MENU, 0);
531         else if (rkvr_data->rkvr_data.key_map.key_menu_down)
532                 rkvr_send_key_event(input, KEY_MENU, 1);
533         else if (rkvr_data->rkvr_data.key_map.key_home_up)
534                 rkvr_send_key_event(input, KEY_HOME, 0);
535         else if (rkvr_data->rkvr_data.key_map.key_home_down)
536                 rkvr_send_key_event(input, KEY_HOME, 1);
537         else if (rkvr_data->rkvr_data.key_map.key_power_up)
538                 rkvr_send_key_event(input, KEY_POWER, 0);
539         else if (rkvr_data->rkvr_data.key_map.key_power_down)
540                 rkvr_send_key_event(input, KEY_POWER, 1);
541         else if (rkvr_data->rkvr_data.key_map.key_volup_up)
542                 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
543         else if (rkvr_data->rkvr_data.key_map.key_volup_down)
544                 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
545         else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
546                 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
547         else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
548                 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
549         else if (rkvr_data->rkvr_data.key_map.key_esc_up)
550                 rkvr_send_key_event(input, KEY_ESC, 0);
551         else if (rkvr_data->rkvr_data.key_map.key_esc_down)
552                 rkvr_send_key_event(input, KEY_ESC, 1);
553         else if (rkvr_data->rkvr_data.key_map.key_up_pressed) {
554                 rkvr_send_key_event(input, KEY_UP, 1);
555                 rkvr_send_key_event(input, KEY_UP, 0);
556         } else if (rkvr_data->rkvr_data.key_map.key_down_pressed) {
557                 rkvr_send_key_event(input, KEY_DOWN, 1);
558                 rkvr_send_key_event(input, KEY_DOWN, 0);
559         } else if (rkvr_data->rkvr_data.key_map.key_left_pressed) {
560                 rkvr_send_key_event(input, KEY_LEFT, 1);
561                 rkvr_send_key_event(input, KEY_LEFT, 0);
562         } else if (rkvr_data->rkvr_data.key_map.key_right_pressed) {
563                 rkvr_send_key_event(input, KEY_RIGHT, 1);
564                 rkvr_send_key_event(input, KEY_RIGHT, 0);
565         } else if (rkvr_data->rkvr_data.key_map.key_enter_pressed) {
566                 input_event(input, EV_MSC, MSC_SCAN, 0x90001);
567                 rkvr_send_key_event(input, BTN_MOUSE, 1);
568                 input_event(input, EV_MSC, MSC_SCAN, 0x90001);
569                 rkvr_send_key_event(input, BTN_MOUSE, 0);
570         }
571
572         if (rkvr_data->rkvr_data.key_map.psensor_on) {
573                 hid_info(hdev, "event: psensor_on\n");
574                 rkvr_send_key_event(input, KEY_POWER, 1);
575                 rkvr_send_key_event(input, KEY_POWER, 0);
576         } else if (rkvr_data->rkvr_data.key_map.psensor_off) {
577                 hid_info(hdev, "event: psensor_off\n");
578                 rkvr_send_key_event(input, KEY_POWER, 1);
579                 rkvr_send_key_event(input, KEY_POWER, 0);
580         }
581
582         return 0;
583 }
584
585 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
586 {
587         struct hidraw *dev = hid->hidraw;
588         struct hidraw_list *list;
589         int ret = 0;
590         unsigned long flags;
591         union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
592         struct sensor_hid_data *pdata = hid_get_drvdata(hid);
593
594         spin_lock_irqsave(&dev->list_lock, flags);
595         if (hid->hiddev) {
596                 rkvr_keys_event(hid, data, len);
597         }
598         if (pdata && pdata->priv && pdata->send_event) {
599                 pdata->send_event(rkvr_data->buf, len, pdata->priv);
600                 spin_unlock_irqrestore(&dev->list_lock, flags);
601         } else {
602                 list_for_each_entry(list, &dev->list, node) {
603                         int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
604
605                         if (new_head == list->tail)
606                                 continue;
607
608                         list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
609                         if (!list->buffer[list->head].value) {
610                                 ret = -ENOMEM;
611                                 break;
612                         }
613
614                         list->buffer[list->head].len = len;
615                         list->head = new_head;
616                         kill_fasync(&list->fasync, SIGIO, POLL_IN);
617                 }
618                 spin_unlock_irqrestore(&dev->list_lock, flags);
619                 wake_up_interruptible(&dev->wait);
620         }
621         return ret;
622 }
623
624 /******************************************
625  *--------------------
626  *| ID | BUF .....   |
627  *--------------------
628  *
629  ******************************************/
630 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
631 {
632         struct hid_device *hid = container_of(dev, struct hid_device, dev);
633         unsigned char reportnum = HID_REPORT_ID_RKVR;
634         unsigned char rtype = HID_OUTPUT_REPORT;
635         int ret = -EINVAL;
636
637         ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
638         if (ret != len) {
639                 hid_err(hid, "rkvr_send_report fail\n");
640                 ret = -EIO;
641                 goto fail;
642         }
643         hid_info(hid, "rkvr_send_report ok\n");
644         ret = 0;
645 fail:
646         return ret;
647 }
648
649 static int rkvr_recv_report(struct device *dev, u8 type, u8 *data, int len)
650 {
651         struct hid_device *hid = container_of(dev, struct hid_device, dev);
652         unsigned char report_number = type;
653         unsigned char report_type = HID_MISC_REPORT;
654         char buf[1 + sizeof(*data) * len];
655         int readlen = 1 + sizeof(*data) * len;
656         int ret;
657
658         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
659         if (ret != readlen) {
660                 hid_info(hid, "rkvr_recv_report fail\n");
661                 return -1;
662         }
663         memcpy(data, &buf[1], len);
664         hid_info(hid, "rkvr_recv_report %02x\n", type);
665
666         return 0;
667 }
668
669 /*
670  * for enable sensor data
671  ************************************
672  * buf contents ---->
673  * first 8 bytes :random digits
674  * left bytes    :encryt data
675  * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
676  ***********************************
677  */
678
679 static int hid_report_sync(struct device *dev, const char *data, size_t count)
680 {
681         struct hid_device *hid = container_of(dev, struct hid_device, dev);
682         u64 *tmp;
683         unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
684         unsigned char buf2[3] = {0};
685         char *colon;
686         int i, ret = 0;
687         char *p;
688         size_t len;
689
690         p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
691         if (!p) {
692                 hid_err(hid, "no mem\n");
693                 return -ENOMEM;
694         }
695         memcpy(p, data, count);
696         colon = strnchr(p, count, ':');
697         if (!colon) {
698                 hid_err(hid, "must have conlon\n");
699                 ret = -EINVAL;
700                 goto fail;
701         }
702         if (colon - p + 1 >= count) {
703                 hid_err(hid, "must have sync string after conlon\n");
704                 ret = -EINVAL;
705                 goto fail;
706         }
707         colon[0] = 0;
708         colon++;
709         tmp = (u64 *)(buf + 2);
710         if (kstrtoull(p, 10, tmp)) {
711                 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
712                 ret = -EINVAL;
713                 goto fail;
714         }
715         hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
716         len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
717         for (i = 0; i < len; i++) {
718                 buf2[0] = colon[i * 2];
719                 buf2[1] = colon[i * 2 + 1];
720                 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
721                         hid_err(hid, "err sync string,only hex string allowed\n");
722                         ret = -EINVAL;
723                         goto fail;
724                 }
725         }
726         len = i + sizeof(*tmp) + 2;
727         ret = rkvr_send_report(dev, (unsigned char *)buf, len);
728         if (ret) {
729                 hid_err(hid, "hid_report_encrypt fail\n");
730                 ret = -EIO;
731                 goto fail;
732         }
733         hid_info(hid, "hid_report_encrypt ok\n");
734         ret = count;
735 fail:
736         kfree(p);
737
738         return ret;
739 }
740
741 static int hid_get_capability(struct device *dev, struct hid_capability *caps)
742 {
743         struct hid_device *hid = container_of(dev, struct hid_device, dev);
744         u8 data = 0;
745
746         caps->suspend_notify = 0;
747         if (!rkvr_recv_report(dev, RKVR_ID_CAPS, &data, 1)) {
748                 hid_info(hid, "hid_get_capability %d\n", data);
749                 caps->suspend_notify = data;
750                 return 0;
751         }
752         return -1;
753 }
754
755 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
756 {
757         if (w)
758                 buf[0] = (1 << 7) | (len & 0x7f);
759         else
760                 buf[0] = len & 0x7f;
761         buf[1] = reg;
762         memcpy(&buf[2], data, len);
763 }
764
765 #if DEBUG_SYS
766
767 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
768 {
769         struct hid_device *hid = container_of(dev, struct hid_device, dev);
770         unsigned char report_number = reg;
771         unsigned char report_type = HID_REGR_REPORT;
772         char buf[1 + sizeof(data) * len];
773         int readlen = 1 + sizeof(data) * len;
774         int ret;
775
776         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
777         if (ret != readlen) {
778                 hid_info(hid, "id_hw_raw_request fail\n");
779         } else {
780                 memcpy(data, &buf[1], readlen);
781                 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
782         }
783
784         return 0;
785 }
786
787 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
788 {
789         struct hid_device *hid = container_of(dev, struct hid_device, dev);
790         unsigned char report_number = HID_REPORT_ID_W;
791         unsigned char report_type = HID_REGW_REPORT;
792         char buf[3 + sizeof(data)];
793         int ret;
794
795         hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
796         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
797         if (ret != 4)
798                 hid_info(hid, "id_hw_raw_request fail\n");
799         else
800                 hid_info(hid, "id_hw_raw_request ok\n");
801
802         return 0;
803 }
804
805 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
806                         const char *buf, size_t count)
807 {
808         struct hidraw *devraw;
809
810         devraw = dev_get_drvdata(dev);
811         if (0 == strncmp(buf, "write", 5))
812                 hid_report_writereg(&devraw->hid->dev, 0, 0);
813         hid_info(devraw->hid, "%s\n", buf);
814
815         return count;
816 }
817
818 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
819                                 char *buf)
820 {
821         size_t count = 0;
822         u8 mpu6500_id = 0;
823         struct hidraw *devraw;
824
825         devraw = dev_get_drvdata(dev);
826         if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
827                 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
828         else
829                 count += sprintf(&buf[count], "read fail\n");
830
831         return count;
832 }
833 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
834
835 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
836                         const char *buf, size_t count)
837 {
838         struct hidraw *devraw = dev_get_drvdata(dev);
839         int ret;
840
841         ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
842         return ret > 0 ? count : ret;
843 }
844
845 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
846 #endif
847
848 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
849 {
850         struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
851         unsigned char report_number = reg;
852         unsigned char report_type = HID_REGR_REPORT;
853         char buf[1 + sizeof(data) * len];
854         int readlen = 1 + sizeof(data) * len;
855         int ret;
856
857         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
858         if (ret != readlen) {
859                 hid_err(hid, "id_hw_raw_request fail\n");
860         } else {
861                 memcpy(data, &buf[1], sizeof(data) * len);
862         }
863
864         return 0;
865 }
866
867 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
868 {
869         struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
870         unsigned char report_number = HID_REPORT_ID_W;
871         unsigned char report_type = HID_REGW_REPORT;
872         char buf[3 + sizeof(data)];
873         int ret;
874
875         hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
876         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
877         if (ret != 4)
878                 hid_info(hid, "id_hw_raw_request fail\n");
879         else
880                 hid_info(hid, "id_hw_raw_request ok\n");
881
882         return 0;
883 }
884
885 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
886 {
887         struct hid_device *hid;
888         int err;
889
890         hid = container_of(hdev->dev, struct hid_device, dev);
891         err = hid_hw_power(hid, PM_HINT_FULLON);
892         if (err < 0)
893                 return err;
894         err = hid_hw_open(hid);
895         if (err < 0) {
896                 hid_hw_power(hid, PM_HINT_NORMAL);
897                 return err;
898         }
899
900         return 0;
901 }
902
903 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
904 {
905         struct hid_device *hid;
906
907         hid = container_of(hdev->dev, struct hid_device, dev);
908         hid_hw_power(hid, PM_HINT_NORMAL);
909         hid_hw_close(hid);
910 }
911
912 #if DYNAMIC_LOAD_MPU6500
913 static int register_mpu6500;
914 struct platform_device mpu6500_dev = {
915         .name = "mpu6500",
916 };
917 #endif
918
919 static int rkvr_connect(struct hid_device *hid)
920 {
921         int minor, result;
922         struct hidraw *dev;
923
924         /* we accept any HID device, no matter the applications */
925         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
926         if (!dev)
927                 return -ENOMEM;
928         result = -EINVAL;
929         mutex_lock(&minors_lock);
930         for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
931                 if (rkvr_hidraw_table[minor])
932                         continue;
933                 rkvr_hidraw_table[minor] = dev;
934                 result = 0;
935                 break;
936         }
937         if (result) {
938                 mutex_unlock(&minors_lock);
939                 kfree(dev);
940                 goto out;
941         }
942
943         dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
944                                         NULL, "%s%d", "rkvr", minor);
945
946         if (IS_ERR(dev->dev)) {
947                 rkvr_hidraw_table[minor] = NULL;
948                 mutex_unlock(&minors_lock);
949                 result = PTR_ERR(dev->dev);
950                 kfree(dev);
951                 goto out;
952         }
953
954         dev_set_drvdata(dev->dev, dev);
955 #if DEBUG_SYS
956         device_create_file(dev->dev, &dev_attr_debug);
957         device_create_file(dev->dev, &dev_attr_sync);
958 #endif
959
960         {
961                 struct rkvr_iio_hw_device *hw_device;
962
963                 hw_device = inv_hid_alloc("hid-rkvr");
964                 if (!hw_device) {
965                         hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
966                         rkvr_hidraw_table[minor] = NULL;
967                         mutex_unlock(&minors_lock);
968                         result = PTR_ERR(dev->dev);
969                         kfree(dev);
970                         goto out;
971                 }
972                 hw_device->dev = &hid->dev;
973                 hw_device->open = rkvr_hid_open;
974                 hw_device->close = rkvr_hid_close;
975                 hw_device->read = rkvr_hid_read;
976                 hw_device->write = rkvr_hid_write;
977                 if (inv_hid_register_devcie(hw_device)) {
978                         hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
979                         inv_hid_free(hw_device);
980                         rkvr_hidraw_table[minor] = NULL;
981                         mutex_unlock(&minors_lock);
982                         result = PTR_ERR(dev->dev);
983                         kfree(dev);
984                         goto out;
985                 }
986         }
987
988 #if DYNAMIC_LOAD_MPU6500
989         if (!register_mpu6500) {
990                 register_mpu6500 = 1;
991                 hid_info(hid, "--->platform_device_register-->\n");
992                 platform_device_register(&mpu6500_dev);
993         }
994 #endif
995
996         if (hid_hw_open(hid)) {
997                 rkvr_hidraw_table[minor] = NULL;
998                 mutex_unlock(&minors_lock);
999                 result = PTR_ERR(dev->dev);
1000                 kfree(dev);
1001                 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
1002                 goto out;
1003         }
1004
1005         init_waitqueue_head(&dev->wait);
1006         spin_lock_init(&dev->list_lock);
1007         INIT_LIST_HEAD(&dev->list);
1008
1009         dev->hid = hid;
1010         dev->minor = minor;
1011         dev->exist = 1;
1012         hid->hidraw = dev; /*struct hidraw * **/
1013
1014         hid_get_capability(&hid->dev, &rkvr_hid_capability[minor]);
1015
1016         mutex_unlock(&minors_lock);
1017 out:
1018         return result;
1019 }
1020
1021 static int rkvr_keys_remove(struct hid_device *hdev)
1022 {
1023         struct input_dev *input = hdev->hiddev;
1024
1025         input_unregister_device(input);
1026         return 0;
1027 }
1028
1029 static unsigned int key_codes[] = {
1030         KEY_MENU,
1031         KEY_HOME,
1032         KEY_POWER,
1033         KEY_VOLUMEUP,
1034         KEY_VOLUMEDOWN,
1035         KEY_WAKEUP,
1036         KEY_ESC,
1037         KEY_LEFT,
1038         KEY_RIGHT,
1039         KEY_UP,
1040         KEY_DOWN,
1041         KEY_ENTER,
1042         BTN_MOUSE
1043 };
1044
1045 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
1046 {
1047
1048         struct device *dev = &hdev->dev;
1049         struct input_dev *input = NULL;
1050         int i, error = 0;
1051
1052         input = devm_input_allocate_device(dev);
1053         if (!input) {
1054                 hid_err(hdev, "input_allocate_device fail\n");
1055                 return -ENOMEM;
1056         }
1057         input->name = "rkvr-keypad";
1058         input->phys = "rkvr-keys/input0";
1059         input->dev.parent = dev;
1060         input->id.bustype = BUS_HOST;
1061         input->id.vendor = 0x071b;
1062         input->id.product = 0x3205;
1063         input->id.version = 0x0001;
1064
1065         for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
1066                 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
1067                 input_set_capability(input, EV_KEY, key_codes[i]);
1068         }
1069
1070 #ifdef RK_HID_GEAR_TOUCH
1071         set_bit(EV_REL, input->evbit);
1072         input_set_capability(input, EV_REL, REL_X);
1073         input_set_capability(input, EV_REL, REL_Y);
1074         input_set_capability(input, EV_MSC, MSC_SCAN);
1075         input_set_capability(input, EV_KEY, 0x110);
1076 #endif
1077
1078         error = input_register_device(input);
1079         if (error) {
1080                 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
1081                 return error;
1082         }
1083         hdev->hiddev = input;
1084
1085         return 0;
1086 }
1087
1088 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1089 {
1090         int ret = hdev->ll_driver->start(hdev);
1091
1092         if (ret)
1093                 return ret;
1094         ret = rkvr_connect(hdev);
1095         if (ret)
1096                 hdev->ll_driver->stop(hdev);
1097
1098         return ret;
1099 }
1100
1101 static void rkvr_disconnect(struct hid_device *hid)
1102 {
1103         struct hidraw *hidraw = hid->hidraw;
1104
1105         mutex_lock(&minors_lock);
1106         /* always unregistering inv_hid_device when hardware disconnect */
1107         inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1108 #if DEBUG_SYS
1109         device_remove_file(hidraw->dev, &dev_attr_debug);
1110         device_remove_file(hidraw->dev, &dev_attr_sync);
1111 #endif
1112
1113         device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1114         rkvr_drop_ref(hidraw, 1);
1115         mutex_unlock(&minors_lock);
1116 }
1117
1118 static void rkvr_hw_stop(struct hid_device *hdev)
1119 {
1120         rkvr_disconnect(hdev);
1121         hdev->ll_driver->stop(hdev);
1122 }
1123
1124 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1125 {
1126         struct inode *inode = file_inode(file);
1127         unsigned int minor = iminor(inode);
1128         long ret = 0;
1129         struct hidraw *dev;
1130         void __user *user_arg = (void __user *)arg;
1131
1132         mutex_lock(&minors_lock);
1133         dev = rkvr_hidraw_table[minor];
1134         if (!dev) {
1135                 ret = -ENODEV;
1136                 goto out;
1137         }
1138
1139         switch (cmd) {
1140         case HIDIOCGRDESCSIZE:
1141                 if (put_user(dev->hid->rsize, (int __user *)arg))
1142                         ret = -EFAULT;
1143                 break;
1144
1145         case HIDIOCGRDESC:
1146                 {
1147                         __u32 len;
1148
1149                         if (get_user(len, (int __user *)arg))
1150                                 ret = -EFAULT;
1151                         else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1152                                 ret = -EINVAL;
1153                         else if (copy_to_user(user_arg + offsetof(
1154                                 struct hidraw_report_descriptor,
1155                                 value[0]),
1156                                 dev->hid->rdesc,
1157                                 min(dev->hid->rsize, len)))
1158                                 ret = -EFAULT;
1159                         break;
1160                 }
1161         case HIDIOCGRAWINFO:
1162                 {
1163                         struct hidraw_devinfo dinfo;
1164
1165                         dinfo.bustype = dev->hid->bus;
1166                         dinfo.vendor = dev->hid->vendor;
1167                         dinfo.product = dev->hid->product;
1168                         if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1169                                 ret = -EFAULT;
1170                         break;
1171                 }
1172         default:
1173                 {
1174                         struct hid_device *hid = dev->hid;
1175
1176                         if (_IOC_TYPE(cmd) != 'H') {
1177                                 ret = -EINVAL;
1178                                 break;
1179                         }
1180
1181                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1182                                 int len = _IOC_SIZE(cmd);
1183
1184                                 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1185                                 break;
1186                         }
1187                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1188                                 int len = _IOC_SIZE(cmd);
1189
1190                                 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1191                                 break;
1192                         }
1193
1194                         if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1195                                 int len = _IOC_SIZE(cmd);
1196                                 char *buf;
1197
1198                                 buf = kzalloc(len + 1, GFP_KERNEL);
1199                                 if (!buf) {
1200                                         ret = -ENOMEM;
1201                                         break;
1202                                 }
1203                                 if (copy_from_user(buf, user_arg, len)) {
1204                                         ret = -EFAULT;
1205                                         kfree(buf);
1206                                         break;
1207                                 }
1208                                 ret = hid_report_sync(&hid->dev, buf, len);
1209                                 kfree(buf);
1210                                 break;
1211                         }
1212
1213                         /* Begin Read-only ioctls. */
1214                         if (_IOC_DIR(cmd) != _IOC_READ) {
1215                                 ret = -EINVAL;
1216                                 break;
1217                         }
1218
1219                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1220                                 int len = strlen(hid->name) + 1;
1221
1222                                 if (len > _IOC_SIZE(cmd))
1223                                         len = _IOC_SIZE(cmd);
1224                                 ret = copy_to_user(user_arg, hid->name, len) ?
1225                                         -EFAULT : len;
1226                                 break;
1227                         }
1228
1229                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1230                                 int len = strlen(hid->phys) + 1;
1231
1232                                 if (len > _IOC_SIZE(cmd))
1233                                         len = _IOC_SIZE(cmd);
1234                                 ret = copy_to_user(user_arg, hid->phys, len) ?
1235                                         -EFAULT : len;
1236                                 break;
1237                         }
1238                 }
1239
1240         ret = -ENOTTY;
1241         }
1242 out:
1243         mutex_unlock(&minors_lock);
1244         return ret;
1245 }
1246
1247 static const struct file_operations rkvr_ops = {
1248         .owner = THIS_MODULE,
1249         .read = rkvr_hidraw_read,
1250         .write = rkvr_hidraw_write,
1251         .poll = rkvr_hidraw_poll,
1252         .open = rkvr_hidraw_open,
1253         .release = rkvr_hidraw_release,
1254         .unlocked_ioctl = rkvr_hidraw_ioctl,
1255 #ifdef CONFIG_COMPAT
1256         .compat_ioctl   = rkvr_hidraw_ioctl,
1257 #endif
1258         .fasync = rkvr_hidraw_fasync,
1259         .llseek = noop_llseek,
1260 };
1261
1262 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1263 {
1264         sensorData.priv = priv;
1265         sensorData.send_event = callback;
1266
1267         return 0;
1268 }
1269 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1270
1271 static int rkvr_fb_event_notify(struct notifier_block *self,
1272                                            unsigned long action, void *data)
1273 {
1274         int i;
1275         unsigned char buf[3] = {HID_REPORT_ID_RKVR, RKVR_ID_IDLE, 0};
1276         struct hid_device *hid;
1277         struct fb_event *event = data;
1278         int blank_mode;
1279
1280         if (action != FB_EARLY_EVENT_BLANK && action != FB_EVENT_BLANK)
1281                 return NOTIFY_OK;
1282
1283         blank_mode = *((int *)event->data);
1284
1285         mutex_lock(&minors_lock);
1286         for (i = 0; i < RKVR_HIDRAW_MAX_DEVICES; i++) {
1287                 if (!rkvr_hidraw_table[i] || !rkvr_hidraw_table[i]->exist)
1288                         continue;
1289                 if (!rkvr_hid_capability[i].suspend_notify) {
1290                         continue;
1291                 }
1292                 hid = rkvr_hidraw_table[i]->hid;
1293                 if (action == FB_EARLY_EVENT_BLANK) {
1294                         switch (blank_mode) {
1295                         case FB_BLANK_UNBLANK:
1296                                 break;
1297                         default:
1298                                 rkvr_send_report(&hid->dev, buf, 3);
1299                                 break;
1300                         }
1301                 } else if (action == FB_EVENT_BLANK) {
1302                         switch (blank_mode) {
1303                         case FB_BLANK_UNBLANK:
1304                                 buf[2] = 1;
1305                                 rkvr_send_report(&hid->dev, buf, 3);
1306                                 break;
1307                         default:
1308                                 break;
1309                         }
1310                 }
1311         }
1312         mutex_unlock(&minors_lock);
1313         return NOTIFY_OK;
1314 }
1315
1316 static struct notifier_block rkvr_fb_notifier = {
1317         .notifier_call = rkvr_fb_event_notify,
1318 };
1319
1320 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1321 {
1322         int retval;
1323         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1324
1325         retval = hid_parse(hdev);
1326         if (retval) {
1327                 hid_err(hdev, "rkvr - parse failed\n");
1328                 goto exit;
1329         }
1330         hid_set_drvdata(hdev, &sensorData);
1331         if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1332                 retval = rkvr_keys_probe(hdev);
1333                 if (retval) {
1334                         hid_err(hdev, "rkvr_keys_probe failed\n");
1335                         goto exit_stop;
1336                 }
1337                 retval = rkvr_hw_start(hdev, 0);
1338                 if (retval) {
1339                         hid_err(hdev, "rkvr - rkvr hw start failed\n");
1340                         rkvr_keys_remove(hdev);
1341                         goto exit_stop;
1342                 }
1343         } else {
1344                 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1345                 if (retval) {
1346                         hid_err(hdev, "rkvr - hid hw start failed\n");
1347                         goto exit;
1348                 }
1349         }
1350
1351         return 0;
1352
1353 exit_stop:
1354         hid_hw_stop(hdev);
1355 exit:
1356         return retval;
1357 }
1358
1359 static void rkvr_remove(struct hid_device *hdev)
1360 {
1361         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1362
1363         if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1364                 rkvr_hw_stop(hdev);
1365                 rkvr_keys_remove(hdev);
1366         } else {
1367                 hid_hw_stop(hdev);
1368         }
1369 }
1370
1371 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1372 {
1373         int retval = 0;
1374         static unsigned int count;
1375         static unsigned long old_jiffy;
1376
1377         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1378
1379         if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1380                 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1381                         intf->cur_altsetting->desc.bInterfaceNumber);
1382                 return 0;
1383         }
1384
1385         /* print sensor poll frequency */
1386         if (++count >= 1000) {
1387                 unsigned long cur_jiffy = jiffies;
1388
1389                 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1390                 count = 0;
1391                 old_jiffy = cur_jiffy;
1392         }
1393
1394         if (hdev->hidraw || hdev->hiddev) {
1395                 retval = rkvr_report_event(hdev, data, size);
1396                 if (retval < 0)
1397                         hid_info(hdev, "rkvr: raw event err %d\n", retval);
1398         }
1399
1400         return retval;
1401 }
1402
1403 static const struct hid_device_id rkvr_devices[] = {
1404         { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1405         { }
1406 };
1407
1408 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1409
1410 static struct hid_driver rkvr_driver = {
1411         .name = "rkvr",
1412         .id_table = rkvr_devices,
1413         .probe = rkvr_probe,
1414         .remove = rkvr_remove,
1415         .raw_event = rkvr_raw_event
1416 };
1417
1418 static int __init rkvr_init(void)
1419 {
1420         int retval;
1421         dev_t dev_id;
1422
1423         rkvr_class = class_create(THIS_MODULE, "rkvr");
1424         if (IS_ERR(rkvr_class))
1425                 return PTR_ERR(rkvr_class);
1426
1427         retval = hid_register_driver(&rkvr_driver);
1428         if (retval < 0) {
1429                 pr_warn("rkvr_init - Can't register drive.\n");
1430                 goto out_class;
1431         }
1432
1433         retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1434                                         RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1435         if (retval < 0) {
1436                 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1437                 goto out_register;
1438         }
1439
1440         rkvr_major = MAJOR(dev_id);
1441         cdev_init(&rkvr_cdev, &rkvr_ops);
1442         cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1443
1444         retval = fb_register_client(&rkvr_fb_notifier);
1445         if (retval) {
1446                 pr_warn("rkvr_init - Can't register fb notifier\n");
1447                 goto out_chardev;
1448         }
1449         return 0;
1450 out_chardev:
1451         unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1452 out_register:
1453         hid_unregister_driver(&rkvr_driver);
1454 out_class:
1455         class_destroy(rkvr_class);
1456
1457         return retval;
1458 }
1459
1460 static void __exit rkvr_exit(void)
1461 {
1462         dev_t dev_id = MKDEV(rkvr_major, 0);
1463
1464         fb_unregister_client(&rkvr_fb_notifier);
1465         cdev_del(&rkvr_cdev);
1466
1467         unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1468
1469         hid_unregister_driver(&rkvr_driver);
1470         class_destroy(rkvr_class);
1471 }
1472
1473 module_init(rkvr_init);
1474 module_exit(rkvr_exit);
1475
1476 MODULE_AUTHOR("zwp");
1477 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1478 MODULE_LICENSE("GPL v2");