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