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