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