2eeac6d6654aa506bcb61280379b98809d5c5793
[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 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
570 {
571         if (w)
572                 buf[0] = (1 << 7) | (len && 0x7f);
573         else
574                 buf[0] = len && 0x7f;
575         buf[1] = reg;
576         memcpy(&buf[2], data, len);
577 }
578
579 #if DEBUG_SYS
580 #define HID_OUTPUT_READREG      3
581
582 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
583 {
584         struct hid_device *hid = container_of(dev, struct hid_device, dev);
585         unsigned char report_number = reg;
586         unsigned char report_type = HID_OUTPUT_READREG;
587         char buf[1 + sizeof(data) * len];
588         int readlen = 1 + sizeof(data) * len;
589         int ret;
590
591         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
592         if (ret != readlen) {
593                 hid_info(hid, "id_hw_raw_request fail\n");
594         } else {
595                 memcpy(data, &buf[1], readlen);
596                 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
597         }
598
599         return 0;
600 }
601
602 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
603 {
604         struct hid_device *hid = container_of(dev, struct hid_device, dev);
605         unsigned char report_number = 5;
606         unsigned char report_type = HID_OUTPUT_REPORT;
607         char buf[3 + sizeof(data)];
608         int ret;
609
610         hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
611         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
612         if (ret != 4)
613                 hid_info(hid, "id_hw_raw_request fail\n");
614         else
615                 hid_info(hid, "id_hw_raw_request ok\n");
616
617         return 0;
618 }
619
620 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
621                         const char *buf, size_t count)
622 {
623         struct hidraw *devraw;
624         struct hid_device *hid = container_of(dev, struct hid_device, dev);
625
626         devraw = dev_get_drvdata(dev);
627         if (0 == strncmp(buf, "write", 5))
628                 hid_report_writereg(&devraw->hid->dev, 0, 0);
629         hid_info(hid, "%s\n", buf);
630
631         return count;
632 }
633
634 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
635                                 char *buf)
636 {
637         size_t count = 0;
638         u8 mpu6500_id = 0;
639         struct hidraw *devraw;
640
641         devraw = dev_get_drvdata(dev);
642         if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
643                 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
644         else
645                 count += sprintf(&buf[count], "read fail\n");
646
647         return count;
648 }
649 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
650 #endif
651
652 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
653 {
654         struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
655         unsigned char report_number = reg;
656         unsigned char report_type = HID_OUTPUT_READREG;
657         char buf[1 + sizeof(data) * len];
658         int readlen = 1 + sizeof(data) * len;
659         int ret;
660
661         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
662         if (ret != readlen) {
663                 hid_err(hid, "id_hw_raw_request fail\n");
664         } else {
665                 memcpy(data, &buf[1], sizeof(data) * len);
666         }
667
668         return 0;
669 }
670
671 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
672 {
673         struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
674         unsigned char report_number = 5;
675         unsigned char report_type = HID_OUTPUT_REPORT;
676         char buf[3 + sizeof(data)];
677         int ret;
678
679         hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
680         ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
681         if (ret != 4)
682                 hid_info(hid, "id_hw_raw_request fail\n");
683         else
684                 hid_info(hid, "id_hw_raw_request ok\n");
685
686         return 0;
687 }
688
689 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
690 {
691         struct hid_device *hid;
692         int err;
693
694         hid = container_of(hdev->dev, struct hid_device, dev);
695         err = hid_hw_power(hid, PM_HINT_FULLON);
696         if (err < 0)
697                 return err;
698         err = hid_hw_open(hid);
699         if (err < 0) {
700                 hid_hw_power(hid, PM_HINT_NORMAL);
701                 return err;
702         }
703
704         return 0;
705 }
706
707 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
708 {
709         struct hid_device *hid;
710
711         hid = container_of(hdev->dev, struct hid_device, dev);
712         hid_hw_power(hid, PM_HINT_NORMAL);
713         hid_hw_close(hid);
714 }
715
716 #if DYNAMIC_LOAD_MPU6500
717 static int register_mpu6500;
718 struct platform_device mpu6500_dev = {
719         .name = "mpu6500",
720 };
721 #endif
722
723 static int rkvr_connect(struct hid_device *hid)
724 {
725         int minor, result;
726         struct hidraw *dev;
727
728         /* we accept any HID device, no matter the applications */
729         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
730         if (!dev)
731                 return -ENOMEM;
732         result = -EINVAL;
733         mutex_lock(&minors_lock);
734         for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
735                 if (rkvr_hidraw_table[minor])
736                         continue;
737                 rkvr_hidraw_table[minor] = dev;
738                 result = 0;
739                 break;
740         }
741         if (result) {
742                 mutex_unlock(&minors_lock);
743                 kfree(dev);
744                 goto out;
745         }
746
747         dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
748                                         NULL, "%s%d", "rkvr", minor);
749
750         if (IS_ERR(dev->dev)) {
751                 rkvr_hidraw_table[minor] = NULL;
752                 mutex_unlock(&minors_lock);
753                 result = PTR_ERR(dev->dev);
754                 kfree(dev);
755                 goto out;
756         }
757
758         dev_set_drvdata(dev->dev, dev);
759 #if DEBUG_SYS
760         device_create_file(dev->dev, &dev_attr_debug);
761 #endif
762
763         {
764                 struct rkvr_iio_hw_device *hw_device;
765
766                 hw_device = inv_hid_alloc("hid-rkvr");
767                 if (!hw_device) {
768                         hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
769                         rkvr_hidraw_table[minor] = NULL;
770                         mutex_unlock(&minors_lock);
771                         result = PTR_ERR(dev->dev);
772                         kfree(dev);
773                         goto out;
774                 }
775                 hw_device->dev = &hid->dev;
776                 hw_device->open = rkvr_hid_open;
777                 hw_device->close = rkvr_hid_close;
778                 hw_device->read = rkvr_hid_read;
779                 hw_device->write = rkvr_hid_write;
780                 if (inv_hid_register_devcie(hw_device)) {
781                         hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
782                         inv_hid_free(hw_device);
783                         rkvr_hidraw_table[minor] = NULL;
784                         mutex_unlock(&minors_lock);
785                         result = PTR_ERR(dev->dev);
786                         kfree(dev);
787                         goto out;
788                 }
789         }
790
791 #if DYNAMIC_LOAD_MPU6500
792         if (!register_mpu6500) {
793                 register_mpu6500 = 1;
794                 hid_info(hid, "--->platform_device_register-->\n");
795                 platform_device_register(&mpu6500_dev);
796         }
797 #endif
798
799         if (hid_hw_open(hid)) {
800                 rkvr_hidraw_table[minor] = NULL;
801                 mutex_unlock(&minors_lock);
802                 result = PTR_ERR(dev->dev);
803                 kfree(dev);
804                 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
805                 goto out;
806         }
807
808         init_waitqueue_head(&dev->wait);
809         spin_lock_init(&dev->list_lock);
810         INIT_LIST_HEAD(&dev->list);
811
812         dev->hid = hid;
813         dev->minor = minor;
814         dev->exist = 1;
815         hid->hidraw = dev; /*struct hidraw * **/
816         mutex_unlock(&minors_lock);
817 out:
818         return result;
819 }
820
821 static int rkvr_keys_remove(struct hid_device *hdev)
822 {
823         struct input_dev *input = hdev->hiddev;
824
825         input_unregister_device(input);
826         return 0;
827 }
828
829 static unsigned int key_codes[] = {
830         KEY_MENU,
831         KEY_HOME,
832         KEY_POWER,
833         KEY_VOLUMEUP,
834         KEY_VOLUMEDOWN,
835         KEY_WAKEUP
836 };
837
838 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
839 {
840
841         struct device *dev = &hdev->dev;
842         struct input_dev *input = NULL;
843         int i, error = 0;
844
845         input = devm_input_allocate_device(dev);
846         if (!input) {
847                 hid_err(hdev, "input_allocate_device fail\n");
848                 return -ENOMEM;
849         }
850         input->name = "rkvr-keypad";
851         input->phys = "rkvr-keys/input0";
852         input->dev.parent = dev;
853         input->id.bustype = BUS_HOST;
854         input->id.vendor = 0x071b;
855         input->id.product = 0x3205;
856         input->id.version = 0x0001;
857
858         for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
859                 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
860                 input_set_capability(input, EV_KEY, key_codes[i]);
861         }
862
863         error = input_register_device(input);
864         if (error) {
865                 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
866                 return error;
867         }
868         hdev->hiddev = input;
869
870         return 0;
871 }
872
873 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
874 {
875         int ret = hdev->ll_driver->start(hdev);
876
877         if (ret)
878                 return ret;
879         ret = rkvr_connect(hdev);
880         if (ret)
881                 hdev->ll_driver->stop(hdev);
882
883         return ret;
884 }
885
886 static void rkvr_disconnect(struct hid_device *hid)
887 {
888         struct hidraw *hidraw = hid->hidraw;
889
890         mutex_lock(&minors_lock);
891         /* always unregistering inv_hid_device when hardware disconnect */
892         inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
893 #if DEBUG_SYS
894         device_remove_file(hidraw->dev, &dev_attr_debug);
895 #endif
896
897         device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
898         rkvr_drop_ref(hidraw, 1);
899         mutex_unlock(&minors_lock);
900 }
901
902 static void rkvr_hw_stop(struct hid_device *hdev)
903 {
904         rkvr_disconnect(hdev);
905         hdev->ll_driver->stop(hdev);
906 }
907
908 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
909 {
910         struct inode *inode = file_inode(file);
911         unsigned int minor = iminor(inode);
912         long ret = 0;
913         struct hidraw *dev;
914         void __user *user_arg = (void __user *)arg;
915
916         mutex_lock(&minors_lock);
917         dev = rkvr_hidraw_table[minor];
918         if (!dev) {
919                 ret = -ENODEV;
920                 goto out;
921         }
922
923         switch (cmd) {
924         case HIDIOCGRDESCSIZE:
925                 if (put_user(dev->hid->rsize, (int __user *)arg))
926                         ret = -EFAULT;
927                 break;
928
929         case HIDIOCGRDESC:
930                 {
931                         __u32 len;
932
933                         if (get_user(len, (int __user *)arg))
934                                 ret = -EFAULT;
935                         else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
936                                 ret = -EINVAL;
937                         else if (copy_to_user(user_arg + offsetof(
938                                 struct hidraw_report_descriptor,
939                                 value[0]),
940                                 dev->hid->rdesc,
941                                 min(dev->hid->rsize, len)))
942                                 ret = -EFAULT;
943                         break;
944                 }
945         case HIDIOCGRAWINFO:
946                 {
947                         struct hidraw_devinfo dinfo;
948
949                         dinfo.bustype = dev->hid->bus;
950                         dinfo.vendor = dev->hid->vendor;
951                         dinfo.product = dev->hid->product;
952                         if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
953                                 ret = -EFAULT;
954                         break;
955                 }
956         default:
957                 {
958                         struct hid_device *hid = dev->hid;
959
960                         if (_IOC_TYPE(cmd) != 'H') {
961                                 ret = -EINVAL;
962                                 break;
963                         }
964
965                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
966                                 int len = _IOC_SIZE(cmd);
967
968                                 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
969                                 break;
970                         }
971                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
972                                 int len = _IOC_SIZE(cmd);
973
974                                 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
975                                 break;
976                         }
977
978                         /* Begin Read-only ioctls. */
979                         if (_IOC_DIR(cmd) != _IOC_READ) {
980                                 ret = -EINVAL;
981                                 break;
982                         }
983
984                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
985                                 int len = strlen(hid->name) + 1;
986
987                                 if (len > _IOC_SIZE(cmd))
988                                         len = _IOC_SIZE(cmd);
989                                 ret = copy_to_user(user_arg, hid->name, len) ?
990                                         -EFAULT : len;
991                                 break;
992                         }
993
994                         if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
995                                 int len = strlen(hid->phys) + 1;
996
997                                 if (len > _IOC_SIZE(cmd))
998                                         len = _IOC_SIZE(cmd);
999                                 ret = copy_to_user(user_arg, hid->phys, len) ?
1000                                         -EFAULT : len;
1001                                 break;
1002                         }
1003                 }
1004
1005         ret = -ENOTTY;
1006         }
1007 out:
1008         mutex_unlock(&minors_lock);
1009         return ret;
1010 }
1011
1012 static const struct file_operations rkvr_ops = {
1013         .owner = THIS_MODULE,
1014         .read = rkvr_hidraw_read,
1015         .write = rkvr_hidraw_write,
1016         .poll = rkvr_hidraw_poll,
1017         .open = rkvr_hidraw_open,
1018         .release = rkvr_hidraw_release,
1019         .unlocked_ioctl = rkvr_hidraw_ioctl,
1020 #ifdef CONFIG_COMPAT
1021         .compat_ioctl   = rkvr_hidraw_ioctl,
1022 #endif
1023         .fasync = rkvr_hidraw_fasync,
1024         .llseek = noop_llseek,
1025 };
1026
1027 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1028 {
1029         sensorData.priv = priv;
1030         sensorData.send_event = callback;
1031
1032         return 0;
1033 }
1034 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1035
1036 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1037 {
1038         int retval;
1039         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1040
1041         retval = hid_parse(hdev);
1042         if (retval) {
1043                 hid_err(hdev, "rkvr - parse failed\n");
1044                 goto exit;
1045         }
1046         hid_set_drvdata(hdev, &sensorData);
1047         if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1048                 retval = rkvr_keys_probe(hdev);
1049                 if (retval) {
1050                         hid_err(hdev, "rkvr_keys_probe failed\n");
1051                         goto exit_stop;
1052                 }
1053                 retval = rkvr_hw_start(hdev, 0);
1054                 if (retval) {
1055                         hid_err(hdev, "rkvr - rkvr hw start failed\n");
1056                         rkvr_keys_remove(hdev);
1057                         goto exit_stop;
1058                 }
1059         } else {
1060                 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1061                 if (retval) {
1062                         hid_err(hdev, "rkvr - hid hw start failed\n");
1063                         goto exit;
1064                 }
1065         }
1066
1067         return 0;
1068
1069 exit_stop:
1070         hid_hw_stop(hdev);
1071 exit:
1072         return retval;
1073 }
1074
1075 static void rkvr_remove(struct hid_device *hdev)
1076 {
1077         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1078
1079         if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1080                 rkvr_keys_remove(hdev);
1081                 rkvr_hw_stop(hdev);
1082         } else {
1083                 hid_hw_stop(hdev);
1084         }
1085 }
1086
1087 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1088 {
1089         int retval = 0;
1090         static unsigned int count;
1091         static unsigned long old_jiffy;
1092
1093         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1094
1095         if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1096                 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1097                         intf->cur_altsetting->desc.bInterfaceNumber);
1098                 return 0;
1099         }
1100
1101         /* print sensor poll frequency */
1102         if (++count >= 1000) {
1103                 unsigned long cur_jiffy = jiffies;
1104
1105                 hid_info(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1106                 count = 0;
1107                 old_jiffy = cur_jiffy;
1108         }
1109
1110         if (hdev->hidraw || hdev->hiddev) {
1111                 retval = rkvr_report_event(hdev, data, size);
1112                 if (retval < 0)
1113                         hid_info(hdev, "rkvr: raw event err %d\n", retval);
1114         }
1115
1116         return retval;
1117 }
1118
1119 static const struct hid_device_id rkvr_devices[] = {
1120         { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1121         { }
1122 };
1123
1124 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1125
1126 static struct hid_driver rkvr_driver = {
1127         .name = "rkvr",
1128         .id_table = rkvr_devices,
1129         .probe = rkvr_probe,
1130         .remove = rkvr_remove,
1131         .raw_event = rkvr_raw_event
1132 };
1133
1134 static int __init rkvr_init(void)
1135 {
1136         int retval;
1137         dev_t dev_id;
1138
1139         rkvr_class = class_create(THIS_MODULE, "rkvr");
1140         if (IS_ERR(rkvr_class))
1141                 return PTR_ERR(rkvr_class);
1142
1143         retval = hid_register_driver(&rkvr_driver);
1144         if (retval < 0) {
1145                 pr_warn("rkvr_init - Can't register drive.\n");
1146                 goto out_class;
1147         }
1148
1149         retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1150                                         RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1151         if (retval < 0) {
1152                 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1153                 goto out_register;
1154         }
1155
1156         rkvr_major = MAJOR(dev_id);
1157         cdev_init(&rkvr_cdev, &rkvr_ops);
1158         cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1159         return 0;
1160
1161 out_register:
1162         hid_unregister_driver(&rkvr_driver);
1163 out_class:
1164         class_destroy(rkvr_class);
1165
1166         return retval;
1167 }
1168
1169 static void __exit rkvr_exit(void)
1170 {
1171         dev_t dev_id = MKDEV(rkvr_major, 0);
1172
1173         cdev_del(&rkvr_cdev);
1174
1175         unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1176
1177         hid_unregister_driver(&rkvr_driver);
1178         class_destroy(rkvr_class);
1179 }
1180
1181 module_init(rkvr_init);
1182 module_exit(rkvr_exit);
1183
1184 MODULE_AUTHOR("zwp");
1185 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1186 MODULE_LICENSE("GPL v2");