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                 rkvr_send_key_event(input, KEY_ENTER, 1);
567                 rkvr_send_key_event(input, KEY_ENTER, 0);
568 #ifdef RK_HID_GEAR_TOUCH
569                 input_event(input, EV_MSC, MSC_SCAN, 0x90001);
570                 rkvr_send_key_event(input, 0x110, 1);
571                 input_event(input, EV_MSC, MSC_SCAN, 0x90001);
572                 rkvr_send_key_event(input, 0x110, 0);
573 #endif
574         }
575
576         if (rkvr_data->rkvr_data.key_map.psensor_on) {
577                 hid_info(hdev, "event: psensor_on\n");
578                 rkvr_send_key_event(input, KEY_POWER, 1);
579                 rkvr_send_key_event(input, KEY_POWER, 0);
580         } else if (rkvr_data->rkvr_data.key_map.psensor_off) {
581                 hid_info(hdev, "event: psensor_off\n");
582                 rkvr_send_key_event(input, KEY_POWER, 1);
583                 rkvr_send_key_event(input, KEY_POWER, 0);
584         }
585
586         return 0;
587 }
588
589 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
590 {
591         struct hidraw *dev = hid->hidraw;
592         struct hidraw_list *list;
593         int ret = 0;
594         unsigned long flags;
595         union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
596         struct sensor_hid_data *pdata = hid_get_drvdata(hid);
597
598         spin_lock_irqsave(&dev->list_lock, flags);
599         if (hid->hiddev) {
600                 rkvr_keys_event(hid, data, len);
601         }
602         if (pdata && pdata->priv && pdata->send_event) {
603                 pdata->send_event(rkvr_data->buf, len, pdata->priv);
604                 spin_unlock_irqrestore(&dev->list_lock, flags);
605         } else {
606                 list_for_each_entry(list, &dev->list, node) {
607                         int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
608
609                         if (new_head == list->tail)
610                                 continue;
611
612                         list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
613                         if (!list->buffer[list->head].value) {
614                                 ret = -ENOMEM;
615                                 spin_unlock_irqrestore(&dev->list_lock, flags);
616                                 break;
617                         }
618
619                         list->buffer[list->head].len = len;
620                         list->head = new_head;
621                         kill_fasync(&list->fasync, SIGIO, POLL_IN);
622                 }
623                 spin_unlock_irqrestore(&dev->list_lock, flags);
624                 wake_up_interruptible(&dev->wait);
625         }
626         return ret;
627 }
628
629 /******************************************
630  *--------------------
631  *| ID | BUF .....   |
632  *--------------------
633  *
634  ******************************************/
635 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
636 {
637         struct hid_device *hid = container_of(dev, struct hid_device, dev);
638         unsigned char reportnum = HID_REPORT_ID_RKVR;
639         unsigned char rtype = HID_OUTPUT_REPORT;
640         int ret = -EINVAL;
641
642         ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
643         if (ret != len) {
644                 hid_err(hid, "rkvr_send_report fail\n");
645                 ret = -EIO;
646                 goto fail;
647         }
648         hid_info(hid, "rkvr_send_report ok\n");
649         ret = 0;
650 fail:
651         return ret;
652 }
653
654 static int rkvr_recv_report(struct device *dev, u8 type, u8 *data, int len)
655 {
656         struct hid_device *hid = container_of(dev, struct hid_device, dev);
657         unsigned char report_number = type;
658         unsigned char report_type = HID_MISC_REPORT;
659         char buf[1 + sizeof(*data) * len];
660         int readlen = 1 + sizeof(*data) * len;
661         int ret;
662
663         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
664         if (ret != readlen) {
665                 hid_info(hid, "rkvr_recv_report fail\n");
666                 return -1;
667         }
668         memcpy(data, &buf[1], len);
669         hid_info(hid, "rkvr_recv_report %02x\n", type);
670
671         return 0;
672 }
673
674 /*
675  * for enable sensor data
676  ************************************
677  * buf contents ---->
678  * first 8 bytes :random digits
679  * left bytes    :encryt data
680  * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
681  ***********************************
682  */
683
684 static int hid_report_sync(struct device *dev, const char *data, size_t count)
685 {
686         struct hid_device *hid = container_of(dev, struct hid_device, dev);
687         u64 *tmp;
688         unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
689         unsigned char buf2[3] = {0};
690         char *colon;
691         int i, ret = 0;
692         char *p;
693         size_t len;
694
695         p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
696         if (!p) {
697                 hid_err(hid, "no mem\n");
698                 return -ENOMEM;
699         }
700         memcpy(p, data, count);
701         colon = strnchr(p, count, ':');
702         if (!colon) {
703                 hid_err(hid, "must have conlon\n");
704                 ret = -EINVAL;
705                 goto fail;
706         }
707         if (colon - p + 1 >= count) {
708                 hid_err(hid, "must have sync string after conlon\n");
709                 ret = -EINVAL;
710                 goto fail;
711         }
712         colon[0] = 0;
713         colon++;
714         tmp = (u64 *)(buf + 2);
715         if (kstrtoull(p, 10, tmp)) {
716                 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
717                 ret = -EINVAL;
718                 goto fail;
719         }
720         hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
721         len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
722         for (i = 0; i < len; i++) {
723                 buf2[0] = colon[i * 2];
724                 buf2[1] = colon[i * 2 + 1];
725                 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
726                         hid_err(hid, "err sync string,only hex string allowed\n");
727                         ret = -EINVAL;
728                         goto fail;
729                 }
730         }
731         len = i + sizeof(*tmp) + 2;
732         ret = rkvr_send_report(dev, (unsigned char *)buf, len);
733         if (ret) {
734                 hid_err(hid, "hid_report_encrypt fail\n");
735                 ret = -EIO;
736                 goto fail;
737         }
738         hid_info(hid, "hid_report_encrypt ok\n");
739         ret = count;
740 fail:
741         kfree(p);
742
743         return ret;
744 }
745
746 static int hid_get_capability(struct device *dev, struct hid_capability *caps)
747 {
748         struct hid_device *hid = container_of(dev, struct hid_device, dev);
749         u8 data = 0;
750
751         caps->suspend_notify = 0;
752         if (!rkvr_recv_report(dev, RKVR_ID_CAPS, &data, 1)) {
753                 hid_info(hid, "hid_get_capability %d\n", data);
754                 caps->suspend_notify = data;
755                 return 0;
756         }
757         return -1;
758 }
759
760 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
761 {
762         if (w)
763                 buf[0] = (1 << 7) | (len && 0x7f);
764         else
765                 buf[0] = len && 0x7f;
766         buf[1] = reg;
767         memcpy(&buf[2], data, len);
768 }
769
770 #if DEBUG_SYS
771
772 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
773 {
774         struct hid_device *hid = container_of(dev, struct hid_device, dev);
775         unsigned char report_number = reg;
776         unsigned char report_type = HID_REGR_REPORT;
777         char buf[1 + sizeof(data) * len];
778         int readlen = 1 + sizeof(data) * len;
779         int ret;
780
781         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
782         if (ret != readlen) {
783                 hid_info(hid, "id_hw_raw_request fail\n");
784         } else {
785                 memcpy(data, &buf[1], readlen);
786                 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
787         }
788
789         return 0;
790 }
791
792 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
793 {
794         struct hid_device *hid = container_of(dev, struct hid_device, dev);
795         unsigned char report_number = HID_REPORT_ID_W;
796         unsigned char report_type = HID_REGW_REPORT;
797         char buf[3 + sizeof(data)];
798         int ret;
799
800         hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
801         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
802         if (ret != 4)
803                 hid_info(hid, "id_hw_raw_request fail\n");
804         else
805                 hid_info(hid, "id_hw_raw_request ok\n");
806
807         return 0;
808 }
809
810 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
811                         const char *buf, size_t count)
812 {
813         struct hidraw *devraw;
814
815         devraw = dev_get_drvdata(dev);
816         if (0 == strncmp(buf, "write", 5))
817                 hid_report_writereg(&devraw->hid->dev, 0, 0);
818         hid_info(devraw->hid, "%s\n", buf);
819
820         return count;
821 }
822
823 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
824                                 char *buf)
825 {
826         size_t count = 0;
827         u8 mpu6500_id = 0;
828         struct hidraw *devraw;
829
830         devraw = dev_get_drvdata(dev);
831         if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
832                 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
833         else
834                 count += sprintf(&buf[count], "read fail\n");
835
836         return count;
837 }
838 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
839
840 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
841                         const char *buf, size_t count)
842 {
843         struct hidraw *devraw = dev_get_drvdata(dev);
844         int ret;
845
846         ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
847         return ret > 0 ? count : ret;
848 }
849
850 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
851 #endif
852
853 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
854 {
855         struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
856         unsigned char report_number = reg;
857         unsigned char report_type = HID_REGR_REPORT;
858         char buf[1 + sizeof(data) * len];
859         int readlen = 1 + sizeof(data) * len;
860         int ret;
861
862         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
863         if (ret != readlen) {
864                 hid_err(hid, "id_hw_raw_request fail\n");
865         } else {
866                 memcpy(data, &buf[1], sizeof(data) * len);
867         }
868
869         return 0;
870 }
871
872 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
873 {
874         struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
875         unsigned char report_number = HID_REPORT_ID_W;
876         unsigned char report_type = HID_REGW_REPORT;
877         char buf[3 + sizeof(data)];
878         int ret;
879
880         hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
881         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
882         if (ret != 4)
883                 hid_info(hid, "id_hw_raw_request fail\n");
884         else
885                 hid_info(hid, "id_hw_raw_request ok\n");
886
887         return 0;
888 }
889
890 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
891 {
892         struct hid_device *hid;
893         int err;
894
895         hid = container_of(hdev->dev, struct hid_device, dev);
896         err = hid_hw_power(hid, PM_HINT_FULLON);
897         if (err < 0)
898                 return err;
899         err = hid_hw_open(hid);
900         if (err < 0) {
901                 hid_hw_power(hid, PM_HINT_NORMAL);
902                 return err;
903         }
904
905         return 0;
906 }
907
908 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
909 {
910         struct hid_device *hid;
911
912         hid = container_of(hdev->dev, struct hid_device, dev);
913         hid_hw_power(hid, PM_HINT_NORMAL);
914         hid_hw_close(hid);
915 }
916
917 #if DYNAMIC_LOAD_MPU6500
918 static int register_mpu6500;
919 struct platform_device mpu6500_dev = {
920         .name = "mpu6500",
921 };
922 #endif
923
924 static int rkvr_connect(struct hid_device *hid)
925 {
926         int minor, result;
927         struct hidraw *dev;
928
929         /* we accept any HID device, no matter the applications */
930         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
931         if (!dev)
932                 return -ENOMEM;
933         result = -EINVAL;
934         mutex_lock(&minors_lock);
935         for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
936                 if (rkvr_hidraw_table[minor])
937                         continue;
938                 rkvr_hidraw_table[minor] = dev;
939                 result = 0;
940                 break;
941         }
942         if (result) {
943                 mutex_unlock(&minors_lock);
944                 kfree(dev);
945                 goto out;
946         }
947
948         dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
949                                         NULL, "%s%d", "rkvr", minor);
950
951         if (IS_ERR(dev->dev)) {
952                 rkvr_hidraw_table[minor] = NULL;
953                 mutex_unlock(&minors_lock);
954                 result = PTR_ERR(dev->dev);
955                 kfree(dev);
956                 goto out;
957         }
958
959         dev_set_drvdata(dev->dev, dev);
960 #if DEBUG_SYS
961         device_create_file(dev->dev, &dev_attr_debug);
962         device_create_file(dev->dev, &dev_attr_sync);
963 #endif
964
965         {
966                 struct rkvr_iio_hw_device *hw_device;
967
968                 hw_device = inv_hid_alloc("hid-rkvr");
969                 if (!hw_device) {
970                         hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
971                         rkvr_hidraw_table[minor] = NULL;
972                         mutex_unlock(&minors_lock);
973                         result = PTR_ERR(dev->dev);
974                         kfree(dev);
975                         goto out;
976                 }
977                 hw_device->dev = &hid->dev;
978                 hw_device->open = rkvr_hid_open;
979                 hw_device->close = rkvr_hid_close;
980                 hw_device->read = rkvr_hid_read;
981                 hw_device->write = rkvr_hid_write;
982                 if (inv_hid_register_devcie(hw_device)) {
983                         hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
984                         inv_hid_free(hw_device);
985                         rkvr_hidraw_table[minor] = NULL;
986                         mutex_unlock(&minors_lock);
987                         result = PTR_ERR(dev->dev);
988                         kfree(dev);
989                         goto out;
990                 }
991         }
992
993 #if DYNAMIC_LOAD_MPU6500
994         if (!register_mpu6500) {
995                 register_mpu6500 = 1;
996                 hid_info(hid, "--->platform_device_register-->\n");
997                 platform_device_register(&mpu6500_dev);
998         }
999 #endif
1000
1001         if (hid_hw_open(hid)) {
1002                 rkvr_hidraw_table[minor] = NULL;
1003                 mutex_unlock(&minors_lock);
1004                 result = PTR_ERR(dev->dev);
1005                 kfree(dev);
1006                 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
1007                 goto out;
1008         }
1009
1010         init_waitqueue_head(&dev->wait);
1011         spin_lock_init(&dev->list_lock);
1012         INIT_LIST_HEAD(&dev->list);
1013
1014         dev->hid = hid;
1015         dev->minor = minor;
1016         dev->exist = 1;
1017         hid->hidraw = dev; /*struct hidraw * **/
1018
1019         hid_get_capability(&hid->dev, &rkvr_hid_capability[minor]);
1020
1021         mutex_unlock(&minors_lock);
1022 out:
1023         return result;
1024 }
1025
1026 static int rkvr_keys_remove(struct hid_device *hdev)
1027 {
1028         struct input_dev *input = hdev->hiddev;
1029
1030         input_unregister_device(input);
1031         return 0;
1032 }
1033
1034 static unsigned int key_codes[] = {
1035         KEY_MENU,
1036         KEY_HOME,
1037         KEY_POWER,
1038         KEY_VOLUMEUP,
1039         KEY_VOLUMEDOWN,
1040         KEY_WAKEUP,
1041         KEY_ESC,
1042         KEY_LEFT,
1043         KEY_RIGHT,
1044         KEY_UP,
1045         KEY_DOWN,
1046         KEY_ENTER
1047 };
1048
1049 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
1050 {
1051
1052         struct device *dev = &hdev->dev;
1053         struct input_dev *input = NULL;
1054         int i, error = 0;
1055
1056         input = devm_input_allocate_device(dev);
1057         if (!input) {
1058                 hid_err(hdev, "input_allocate_device fail\n");
1059                 return -ENOMEM;
1060         }
1061         input->name = "rkvr-keypad";
1062         input->phys = "rkvr-keys/input0";
1063         input->dev.parent = dev;
1064         input->id.bustype = BUS_HOST;
1065         input->id.vendor = 0x071b;
1066         input->id.product = 0x3205;
1067         input->id.version = 0x0001;
1068
1069         for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
1070                 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
1071                 input_set_capability(input, EV_KEY, key_codes[i]);
1072         }
1073
1074 #ifdef RK_HID_GEAR_TOUCH
1075         set_bit(EV_REL, input->evbit);
1076         input_set_capability(input, EV_REL, REL_X);
1077         input_set_capability(input, EV_REL, REL_Y);
1078         input_set_capability(input, EV_MSC, MSC_SCAN);
1079         input_set_capability(input, EV_KEY, 0x110);
1080 #endif
1081
1082         error = input_register_device(input);
1083         if (error) {
1084                 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
1085                 return error;
1086         }
1087         hdev->hiddev = input;
1088
1089         return 0;
1090 }
1091
1092 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1093 {
1094         int ret = hdev->ll_driver->start(hdev);
1095
1096         if (ret)
1097                 return ret;
1098         ret = rkvr_connect(hdev);
1099         if (ret)
1100                 hdev->ll_driver->stop(hdev);
1101
1102         return ret;
1103 }
1104
1105 static void rkvr_disconnect(struct hid_device *hid)
1106 {
1107         struct hidraw *hidraw = hid->hidraw;
1108
1109         mutex_lock(&minors_lock);
1110         /* always unregistering inv_hid_device when hardware disconnect */
1111         inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1112 #if DEBUG_SYS
1113         device_remove_file(hidraw->dev, &dev_attr_debug);
1114         device_remove_file(hidraw->dev, &dev_attr_sync);
1115 #endif
1116
1117         device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1118         rkvr_drop_ref(hidraw, 1);
1119         mutex_unlock(&minors_lock);
1120 }
1121
1122 static void rkvr_hw_stop(struct hid_device *hdev)
1123 {
1124         rkvr_disconnect(hdev);
1125         hdev->ll_driver->stop(hdev);
1126 }
1127
1128 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1129 {
1130         struct inode *inode = file_inode(file);
1131         unsigned int minor = iminor(inode);
1132         long ret = 0;
1133         struct hidraw *dev;
1134         void __user *user_arg = (void __user *)arg;
1135
1136         mutex_lock(&minors_lock);
1137         dev = rkvr_hidraw_table[minor];
1138         if (!dev) {
1139                 ret = -ENODEV;
1140                 goto out;
1141         }
1142
1143         switch (cmd) {
1144         case HIDIOCGRDESCSIZE:
1145                 if (put_user(dev->hid->rsize, (int __user *)arg))
1146                         ret = -EFAULT;
1147                 break;
1148
1149         case HIDIOCGRDESC:
1150                 {
1151                         __u32 len;
1152
1153                         if (get_user(len, (int __user *)arg))
1154                                 ret = -EFAULT;
1155                         else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1156                                 ret = -EINVAL;
1157                         else if (copy_to_user(user_arg + offsetof(
1158                                 struct hidraw_report_descriptor,
1159                                 value[0]),
1160                                 dev->hid->rdesc,
1161                                 min(dev->hid->rsize, len)))
1162                                 ret = -EFAULT;
1163                         break;
1164                 }
1165         case HIDIOCGRAWINFO:
1166                 {
1167                         struct hidraw_devinfo dinfo;
1168
1169                         dinfo.bustype = dev->hid->bus;
1170                         dinfo.vendor = dev->hid->vendor;
1171                         dinfo.product = dev->hid->product;
1172                         if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1173                                 ret = -EFAULT;
1174                         break;
1175                 }
1176         default:
1177                 {
1178                         struct hid_device *hid = dev->hid;
1179
1180                         if (_IOC_TYPE(cmd) != 'H') {
1181                                 ret = -EINVAL;
1182                                 break;
1183                         }
1184
1185                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1186                                 int len = _IOC_SIZE(cmd);
1187
1188                                 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1189                                 break;
1190                         }
1191                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1192                                 int len = _IOC_SIZE(cmd);
1193
1194                                 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1195                                 break;
1196                         }
1197
1198                         if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1199                                 int len = _IOC_SIZE(cmd);
1200                                 char *buf;
1201
1202                                 buf = kzalloc(len + 1, GFP_KERNEL);
1203                                 if (!buf) {
1204                                         ret = -ENOMEM;
1205                                         break;
1206                                 }
1207                                 if (copy_from_user(buf, user_arg, len)) {
1208                                         ret = -EFAULT;
1209                                         kfree(buf);
1210                                         break;
1211                                 }
1212                                 ret = hid_report_sync(&hid->dev, buf, len);
1213                                 kfree(buf);
1214                                 break;
1215                         }
1216
1217                         /* Begin Read-only ioctls. */
1218                         if (_IOC_DIR(cmd) != _IOC_READ) {
1219                                 ret = -EINVAL;
1220                                 break;
1221                         }
1222
1223                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1224                                 int len = strlen(hid->name) + 1;
1225
1226                                 if (len > _IOC_SIZE(cmd))
1227                                         len = _IOC_SIZE(cmd);
1228                                 ret = copy_to_user(user_arg, hid->name, len) ?
1229                                         -EFAULT : len;
1230                                 break;
1231                         }
1232
1233                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1234                                 int len = strlen(hid->phys) + 1;
1235
1236                                 if (len > _IOC_SIZE(cmd))
1237                                         len = _IOC_SIZE(cmd);
1238                                 ret = copy_to_user(user_arg, hid->phys, len) ?
1239                                         -EFAULT : len;
1240                                 break;
1241                         }
1242                 }
1243
1244         ret = -ENOTTY;
1245         }
1246 out:
1247         mutex_unlock(&minors_lock);
1248         return ret;
1249 }
1250
1251 static const struct file_operations rkvr_ops = {
1252         .owner = THIS_MODULE,
1253         .read = rkvr_hidraw_read,
1254         .write = rkvr_hidraw_write,
1255         .poll = rkvr_hidraw_poll,
1256         .open = rkvr_hidraw_open,
1257         .release = rkvr_hidraw_release,
1258         .unlocked_ioctl = rkvr_hidraw_ioctl,
1259 #ifdef CONFIG_COMPAT
1260         .compat_ioctl   = rkvr_hidraw_ioctl,
1261 #endif
1262         .fasync = rkvr_hidraw_fasync,
1263         .llseek = noop_llseek,
1264 };
1265
1266 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1267 {
1268         sensorData.priv = priv;
1269         sensorData.send_event = callback;
1270
1271         return 0;
1272 }
1273 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1274
1275 static int rkvr_fb_event_notify(struct notifier_block *self,
1276                                            unsigned long action, void *data)
1277 {
1278         int i;
1279         unsigned char buf[3] = {HID_REPORT_ID_RKVR, RKVR_ID_IDLE, 0};
1280         struct hid_device *hid;
1281         struct fb_event *event = data;
1282         int blank_mode;
1283
1284         if (action != FB_EARLY_EVENT_BLANK && action != FB_EVENT_BLANK)
1285                 return NOTIFY_OK;
1286
1287         blank_mode = *((int *)event->data);
1288
1289         mutex_lock(&minors_lock);
1290         for (i = 0; i < RKVR_HIDRAW_MAX_DEVICES; i++) {
1291                 if (!rkvr_hidraw_table[i] || !rkvr_hidraw_table[i]->exist)
1292                         continue;
1293                 if (!rkvr_hid_capability[i].suspend_notify) {
1294                         continue;
1295                 }
1296                 hid = rkvr_hidraw_table[i]->hid;
1297                 if (action == FB_EARLY_EVENT_BLANK) {
1298                         switch (blank_mode) {
1299                         case FB_BLANK_UNBLANK:
1300                                 break;
1301                         default:
1302                                 rkvr_send_report(&hid->dev, buf, 3);
1303                                 break;
1304                         }
1305                 } else if (action == FB_EVENT_BLANK) {
1306                         switch (blank_mode) {
1307                         case FB_BLANK_UNBLANK:
1308                                 buf[2] = 1;
1309                                 rkvr_send_report(&hid->dev, buf, 3);
1310                                 break;
1311                         default:
1312                                 break;
1313                         }
1314                 }
1315         }
1316         mutex_unlock(&minors_lock);
1317         return NOTIFY_OK;
1318 }
1319
1320 static struct notifier_block rkvr_fb_notifier = {
1321         .notifier_call = rkvr_fb_event_notify,
1322 };
1323
1324 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1325 {
1326         int retval;
1327         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1328
1329         retval = hid_parse(hdev);
1330         if (retval) {
1331                 hid_err(hdev, "rkvr - parse failed\n");
1332                 goto exit;
1333         }
1334         hid_set_drvdata(hdev, &sensorData);
1335         if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1336                 retval = rkvr_keys_probe(hdev);
1337                 if (retval) {
1338                         hid_err(hdev, "rkvr_keys_probe failed\n");
1339                         goto exit_stop;
1340                 }
1341                 retval = rkvr_hw_start(hdev, 0);
1342                 if (retval) {
1343                         hid_err(hdev, "rkvr - rkvr hw start failed\n");
1344                         rkvr_keys_remove(hdev);
1345                         goto exit_stop;
1346                 }
1347         } else {
1348                 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1349                 if (retval) {
1350                         hid_err(hdev, "rkvr - hid hw start failed\n");
1351                         goto exit;
1352                 }
1353         }
1354
1355         return 0;
1356
1357 exit_stop:
1358         hid_hw_stop(hdev);
1359 exit:
1360         return retval;
1361 }
1362
1363 static void rkvr_remove(struct hid_device *hdev)
1364 {
1365         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1366
1367         if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1368                 rkvr_hw_stop(hdev);
1369                 rkvr_keys_remove(hdev);
1370         } else {
1371                 hid_hw_stop(hdev);
1372         }
1373 }
1374
1375 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1376 {
1377         int retval = 0;
1378         static unsigned int count;
1379         static unsigned long old_jiffy;
1380
1381         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1382
1383         if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1384                 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1385                         intf->cur_altsetting->desc.bInterfaceNumber);
1386                 return 0;
1387         }
1388
1389         /* print sensor poll frequency */
1390         if (++count >= 1000) {
1391                 unsigned long cur_jiffy = jiffies;
1392
1393                 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1394                 count = 0;
1395                 old_jiffy = cur_jiffy;
1396         }
1397
1398         if (hdev->hidraw || hdev->hiddev) {
1399                 retval = rkvr_report_event(hdev, data, size);
1400                 if (retval < 0)
1401                         hid_info(hdev, "rkvr: raw event err %d\n", retval);
1402         }
1403
1404         return retval;
1405 }
1406
1407 static const struct hid_device_id rkvr_devices[] = {
1408         { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1409         { }
1410 };
1411
1412 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1413
1414 static struct hid_driver rkvr_driver = {
1415         .name = "rkvr",
1416         .id_table = rkvr_devices,
1417         .probe = rkvr_probe,
1418         .remove = rkvr_remove,
1419         .raw_event = rkvr_raw_event
1420 };
1421
1422 static int __init rkvr_init(void)
1423 {
1424         int retval;
1425         dev_t dev_id;
1426
1427         rkvr_class = class_create(THIS_MODULE, "rkvr");
1428         if (IS_ERR(rkvr_class))
1429                 return PTR_ERR(rkvr_class);
1430
1431         retval = hid_register_driver(&rkvr_driver);
1432         if (retval < 0) {
1433                 pr_warn("rkvr_init - Can't register drive.\n");
1434                 goto out_class;
1435         }
1436
1437         retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1438                                         RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1439         if (retval < 0) {
1440                 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1441                 goto out_register;
1442         }
1443
1444         rkvr_major = MAJOR(dev_id);
1445         cdev_init(&rkvr_cdev, &rkvr_ops);
1446         cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1447
1448         retval = fb_register_client(&rkvr_fb_notifier);
1449         if (retval) {
1450                 pr_warn("rkvr_init - Can't register fb notifier\n");
1451                 goto out_chardev;
1452         }
1453         return 0;
1454 out_chardev:
1455         unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1456 out_register:
1457         hid_unregister_driver(&rkvr_driver);
1458 out_class:
1459         class_destroy(rkvr_class);
1460
1461         return retval;
1462 }
1463
1464 static void __exit rkvr_exit(void)
1465 {
1466         dev_t dev_id = MKDEV(rkvr_major, 0);
1467
1468         fb_unregister_client(&rkvr_fb_notifier);
1469         cdev_del(&rkvr_cdev);
1470
1471         unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1472
1473         hid_unregister_driver(&rkvr_driver);
1474         class_destroy(rkvr_class);
1475 }
1476
1477 module_init(rkvr_init);
1478 module_exit(rkvr_exit);
1479
1480 MODULE_AUTHOR("zwp");
1481 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1482 MODULE_LICENSE("GPL v2");