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