2 * Rockchip VR driver for Linux
4 * Copyright (C) ROCKCHIP, Inc.
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)
13 * Driver for Rockchip VR devices. Based on hidraw driver.
16 #include <linux/cdev.h>
17 #include <linux/poll.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 #include <linux/hidraw.h>
22 #include <linux/input.h>
23 #include <linux/platform_device.h>
24 #include <linux/notifier.h>
29 #define USB_TRACKER_INTERFACE_PROTOCOL 0
30 /* define rkvr interface number */
31 #define RKVR_INTERFACE_USB_AUDIO_ID 1
32 #define RKVR_INTERFACE_USB_SENSOR_ID 2
33 #define RKVR_INTERFACE_USB_AUDIO_KEY_ID 1
34 /* number of reports to buffer */
35 #define RKVR_HIDRAW_BUFFER_SIZE 64
36 #define RKVR_HIDRAW_MAX_DEVICES 8
37 #define RKVR_FIRST_MINOR 0
38 #define RK_HID_GEAR_TOUCH
40 static struct class *rkvr_class;
42 static struct hidraw *rkvr_hidraw_table[RKVR_HIDRAW_MAX_DEVICES];
44 static struct hid_capability
47 } rkvr_hid_capability[RKVR_HIDRAW_MAX_DEVICES];
48 static DEFINE_MUTEX(minors_lock);
52 __u16 key_menu_down:1;
54 __u16 key_home_down:1;
56 __u16 key_power_down:1;
58 __u16 key_volup_down:1;
60 __u16 key_voldn_down:1;
64 __u16 key_up_pressed:1;
65 __u16 key_up_released:1;
66 __u16 key_down_pressed:1;
67 __u16 key_down_released:1;
68 __u16 key_left_pressed:1;
69 __u16 key_left_released:1;
70 __u16 key_right_pressed:1;
71 __u16 key_right_released:1;
72 __u16 key_enter_pressed:1;
73 __u16 key_enter_released:1;
82 __u8 buf_sensortemperature[2];
85 struct keymap_t key_map;
90 static int rkvr_major;
91 static struct cdev rkvr_cdev;
92 static unsigned int count_array[15] = {0,};
93 static unsigned long old_jiffy_array[15] = {0,};
94 static int rkvr_index;
97 struct sensor_hid_data {
99 int (*send_event)(char *raw_data, size_t raw_len, void *priv);
102 static DEFINE_MUTEX(device_list_lock);
103 static struct list_head rkvr_hid_hw_device_list = {
104 .next = &rkvr_hid_hw_device_list,
105 .prev = &rkvr_hid_hw_device_list
108 static struct rkvr_iio_hw_device *inv_hid_alloc(const char *name)
110 struct rkvr_iio_hw_device *p;
115 s = kstrdup_const(name, GFP_KERNEL);
118 p = kzalloc(sizeof(*p), GFP_KERNEL);
124 pr_err("%s error!\n", __func__);
130 static void inv_hid_free(struct rkvr_iio_hw_device *hw_device)
132 kfree_const(hw_device->name);
136 static int inv_hid_register_devcie(struct rkvr_iio_hw_device *hw_device)
139 mutex_lock(&device_list_lock);
140 if (hw_device->name && (!list_empty(&rkvr_hid_hw_device_list))) {
141 struct rkvr_iio_hw_device *p;
143 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
144 if (!strcmp(hw_device->name, p->name)) {
145 pr_err("%s already exist ,abort\n", hw_device->name);
146 mutex_unlock(&device_list_lock);
151 list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
152 mutex_unlock(&device_list_lock);
156 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
158 struct rkvr_iio_hw_device *p = NULL;
160 mutex_lock(&device_list_lock);
161 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
162 if (!strcmp(name, p->name)) {
168 pr_info("find dev with name %s,free now\n", name);
171 mutex_unlock(&device_list_lock);
174 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
176 struct hidraw_list *list = file->private_data;
178 DECLARE_WAITQUEUE(wait, current);
180 mutex_lock(&list->read_mutex);
182 if (list->head == list->tail) {
183 add_wait_queue(&list->hidraw->wait, &wait);
184 set_current_state(TASK_INTERRUPTIBLE);
186 while (list->head == list->tail) {
187 if (signal_pending(current)) {
191 if (!list->hidraw->exist) {
195 if (file->f_flags & O_NONBLOCK) {
200 /* allow O_NONBLOCK to work well from other threads */
201 mutex_unlock(&list->read_mutex);
203 mutex_lock(&list->read_mutex);
204 set_current_state(TASK_INTERRUPTIBLE);
207 set_current_state(TASK_RUNNING);
208 remove_wait_queue(&list->hidraw->wait, &wait);
214 len = list->buffer[list->tail].len > count ?
215 count : list->buffer[list->tail].len;
217 if (list->buffer[list->tail].value) {
218 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
223 if (opens > 0 && rkvr_index < 15) {
224 if (++count_array[rkvr_index] >= 1000) {
225 unsigned long cur_jiffy = jiffies;
227 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);
228 count_array[rkvr_index] = 0;
229 old_jiffy_array[rkvr_index] = cur_jiffy;
231 if (++rkvr_index >= opens)
238 kfree(list->buffer[list->tail].value);
239 list->buffer[list->tail].value = NULL;
240 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
243 mutex_unlock(&list->read_mutex);
247 /* The first byte is expected to be a report number.
248 * This function is to be called with the minors_lock mutex held
250 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
252 unsigned int minor = iminor(file_inode(file));
253 struct hid_device *dev;
257 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
262 dev = rkvr_hidraw_table[minor]->hid;
264 if (count > HID_MAX_BUFFER_SIZE) {
265 hid_warn(dev, "rkvr - pid %d passed too large report\n",
266 task_pid_nr(current));
272 hid_warn(dev, "rkvr - pid %d passed too short report\n",
273 task_pid_nr(current));
278 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
284 if (copy_from_user(buf, buffer, count)) {
289 if ((report_type == HID_OUTPUT_REPORT) &&
290 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
291 ret = hid_hw_output_report(dev, buf, count);
293 * compatibility with old implementation of USB-HID and I2C-HID:
294 * if the device does not support receiving output reports,
295 * on an interrupt endpoint, fallback to SET_REPORT HID command.
301 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
310 /* the first byte is expected to be a report number */
311 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
315 mutex_lock(&minors_lock);
316 ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
317 mutex_unlock(&minors_lock);
321 /* This function performs a Get_Report transfer over the control endpoint
322 * per section 7.2.1 of the HID specification, version 1.1. The first byte
323 * of buffer is the report number to request, or 0x0 if the defice does not
324 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
325 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
328 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
330 unsigned int minor = iminor(file_inode(file));
331 struct hid_device *dev;
334 unsigned char report_number;
336 dev = rkvr_hidraw_table[minor]->hid;
338 if (!dev->ll_driver->raw_request) {
343 if (count > HID_MAX_BUFFER_SIZE) {
344 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
345 task_pid_nr(current));
351 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
352 task_pid_nr(current));
357 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
364 * Read the first byte from the user. This is the report number,
365 * which is passed to hid_hw_raw_request().
367 if (copy_from_user(&report_number, buffer, 1)) {
372 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
376 len = (ret < count) ? ret : count;
378 if (copy_to_user(buffer, buf, len)) {
391 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
393 struct hidraw_list *list = file->private_data;
395 poll_wait(file, &list->hidraw->wait, wait);
396 if (list->head != list->tail)
397 return POLLIN | POLLRDNORM;
398 if (!list->hidraw->exist)
399 return POLLERR | POLLHUP;
404 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
406 unsigned int minor = iminor(inode);
408 struct hidraw_list *list;
412 list = kzalloc(sizeof(*list), GFP_KERNEL);
418 mutex_lock(&minors_lock);
419 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
424 dev = rkvr_hidraw_table[minor];
426 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
432 err = hid_hw_open(dev->hid);
435 hid_hw_power(dev->hid, PM_HINT_NORMAL);
441 list->hidraw = rkvr_hidraw_table[minor];
442 mutex_init(&list->read_mutex);
443 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
444 list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
445 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
446 file->private_data = list;
451 mutex_unlock(&minors_lock);
459 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
461 struct hidraw_list *list = file->private_data;
463 return fasync_helper(fd, file, on, &list->fasync);
466 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
468 if (exists_bit) { /*hw removed**/
471 hid_hw_close(hidraw->hid);
472 wake_up_interruptible(&hidraw->wait);
479 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
480 rkvr_hidraw_table[hidraw->minor] = NULL;
483 /* close device for last reader */
484 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
485 hid_hw_close(hidraw->hid);
490 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
492 unsigned int minor = iminor(inode);
493 struct hidraw_list *list = file->private_data;
496 mutex_lock(&minors_lock);
498 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
499 list_del(&list->node);
500 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
503 rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
505 mutex_unlock(&minors_lock);
510 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
516 input_report_key(input, key_value, 1);
519 input_report_key(input, key_value, 0);
524 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
526 struct input_dev *input = hdev->hiddev;
527 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
529 if (rkvr_data->rkvr_data.key_map.key_menu_up)
530 rkvr_send_key_event(input, KEY_MENU, 0);
531 else if (rkvr_data->rkvr_data.key_map.key_menu_down)
532 rkvr_send_key_event(input, KEY_MENU, 1);
533 else if (rkvr_data->rkvr_data.key_map.key_home_up)
534 rkvr_send_key_event(input, KEY_HOME, 0);
535 else if (rkvr_data->rkvr_data.key_map.key_home_down)
536 rkvr_send_key_event(input, KEY_HOME, 1);
537 else if (rkvr_data->rkvr_data.key_map.key_power_up)
538 rkvr_send_key_event(input, KEY_POWER, 0);
539 else if (rkvr_data->rkvr_data.key_map.key_power_down)
540 rkvr_send_key_event(input, KEY_POWER, 1);
541 else if (rkvr_data->rkvr_data.key_map.key_volup_up)
542 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
543 else if (rkvr_data->rkvr_data.key_map.key_volup_down)
544 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
545 else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
546 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
547 else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
548 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
549 else if (rkvr_data->rkvr_data.key_map.key_esc_up)
550 rkvr_send_key_event(input, KEY_ESC, 0);
551 else if (rkvr_data->rkvr_data.key_map.key_esc_down)
552 rkvr_send_key_event(input, KEY_ESC, 1);
553 else if (rkvr_data->rkvr_data.key_map.key_up_pressed) {
554 rkvr_send_key_event(input, KEY_UP, 1);
555 rkvr_send_key_event(input, KEY_UP, 0);
556 } else if (rkvr_data->rkvr_data.key_map.key_down_pressed) {
557 rkvr_send_key_event(input, KEY_DOWN, 1);
558 rkvr_send_key_event(input, KEY_DOWN, 0);
559 } else if (rkvr_data->rkvr_data.key_map.key_left_pressed) {
560 rkvr_send_key_event(input, KEY_LEFT, 1);
561 rkvr_send_key_event(input, KEY_LEFT, 0);
562 } else if (rkvr_data->rkvr_data.key_map.key_right_pressed) {
563 rkvr_send_key_event(input, KEY_RIGHT, 1);
564 rkvr_send_key_event(input, KEY_RIGHT, 0);
565 } else if (rkvr_data->rkvr_data.key_map.key_enter_pressed) {
566 input_event(input, EV_MSC, MSC_SCAN, 0x90001);
567 rkvr_send_key_event(input, BTN_MOUSE, 1);
568 input_event(input, EV_MSC, MSC_SCAN, 0x90001);
569 rkvr_send_key_event(input, BTN_MOUSE, 0);
572 if (rkvr_data->rkvr_data.key_map.psensor_on) {
573 hid_info(hdev, "event: psensor_on\n");
574 rkvr_send_key_event(input, KEY_POWER, 1);
575 rkvr_send_key_event(input, KEY_POWER, 0);
576 } else if (rkvr_data->rkvr_data.key_map.psensor_off) {
577 hid_info(hdev, "event: psensor_off\n");
578 rkvr_send_key_event(input, KEY_POWER, 1);
579 rkvr_send_key_event(input, KEY_POWER, 0);
585 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
587 struct hidraw *dev = hid->hidraw;
588 struct hidraw_list *list;
591 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
592 struct sensor_hid_data *pdata = hid_get_drvdata(hid);
594 spin_lock_irqsave(&dev->list_lock, flags);
596 rkvr_keys_event(hid, data, len);
598 if (pdata && pdata->priv && pdata->send_event) {
599 pdata->send_event(rkvr_data->buf, len, pdata->priv);
600 spin_unlock_irqrestore(&dev->list_lock, flags);
602 list_for_each_entry(list, &dev->list, node) {
603 int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
605 if (new_head == list->tail)
608 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
609 if (!list->buffer[list->head].value) {
614 list->buffer[list->head].len = len;
615 list->head = new_head;
616 kill_fasync(&list->fasync, SIGIO, POLL_IN);
618 spin_unlock_irqrestore(&dev->list_lock, flags);
619 wake_up_interruptible(&dev->wait);
624 /******************************************
625 *--------------------
627 *--------------------
629 ******************************************/
630 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
632 struct hid_device *hid = container_of(dev, struct hid_device, dev);
633 unsigned char reportnum = HID_REPORT_ID_RKVR;
634 unsigned char rtype = HID_OUTPUT_REPORT;
637 ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
639 hid_err(hid, "rkvr_send_report fail\n");
643 hid_info(hid, "rkvr_send_report ok\n");
649 static int rkvr_recv_report(struct device *dev, u8 type, u8 *data, int len)
651 struct hid_device *hid = container_of(dev, struct hid_device, dev);
652 unsigned char report_number = type;
653 unsigned char report_type = HID_MISC_REPORT;
654 char buf[1 + sizeof(*data) * len];
655 int readlen = 1 + sizeof(*data) * len;
658 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
659 if (ret != readlen) {
660 hid_info(hid, "rkvr_recv_report fail\n");
663 memcpy(data, &buf[1], len);
664 hid_info(hid, "rkvr_recv_report %02x\n", type);
670 * for enable sensor data
671 ************************************
673 * first 8 bytes :random digits
674 * left bytes :encryt data
675 * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
676 ***********************************
679 static int hid_report_sync(struct device *dev, const char *data, size_t count)
681 struct hid_device *hid = container_of(dev, struct hid_device, dev);
683 unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
684 unsigned char buf2[3] = {0};
690 p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
692 hid_err(hid, "no mem\n");
695 memcpy(p, data, count);
696 colon = strnchr(p, count, ':');
698 hid_err(hid, "must have conlon\n");
702 if (colon - p + 1 >= count) {
703 hid_err(hid, "must have sync string after conlon\n");
709 tmp = (u64 *)(buf + 2);
710 if (kstrtoull(p, 10, tmp)) {
711 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
715 hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
716 len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
717 for (i = 0; i < len; i++) {
718 buf2[0] = colon[i * 2];
719 buf2[1] = colon[i * 2 + 1];
720 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
721 hid_err(hid, "err sync string,only hex string allowed\n");
726 len = i + sizeof(*tmp) + 2;
727 ret = rkvr_send_report(dev, (unsigned char *)buf, len);
729 hid_err(hid, "hid_report_encrypt fail\n");
733 hid_info(hid, "hid_report_encrypt ok\n");
741 static int hid_get_capability(struct device *dev, struct hid_capability *caps)
743 struct hid_device *hid = container_of(dev, struct hid_device, dev);
746 caps->suspend_notify = 0;
747 if (!rkvr_recv_report(dev, RKVR_ID_CAPS, &data, 1)) {
748 hid_info(hid, "hid_get_capability %d\n", data);
749 caps->suspend_notify = data;
755 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
758 buf[0] = (1 << 7) | (len & 0x7f);
762 memcpy(&buf[2], data, len);
767 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
769 struct hid_device *hid = container_of(dev, struct hid_device, dev);
770 unsigned char report_number = reg;
771 unsigned char report_type = HID_REGR_REPORT;
772 char buf[1 + sizeof(data) * len];
773 int readlen = 1 + sizeof(data) * len;
776 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
777 if (ret != readlen) {
778 hid_info(hid, "id_hw_raw_request fail\n");
780 memcpy(data, &buf[1], readlen);
781 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
787 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
789 struct hid_device *hid = container_of(dev, struct hid_device, dev);
790 unsigned char report_number = HID_REPORT_ID_W;
791 unsigned char report_type = HID_REGW_REPORT;
792 char buf[3 + sizeof(data)];
795 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
796 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
798 hid_info(hid, "id_hw_raw_request fail\n");
800 hid_info(hid, "id_hw_raw_request ok\n");
805 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
806 const char *buf, size_t count)
808 struct hidraw *devraw;
810 devraw = dev_get_drvdata(dev);
811 if (0 == strncmp(buf, "write", 5))
812 hid_report_writereg(&devraw->hid->dev, 0, 0);
813 hid_info(devraw->hid, "%s\n", buf);
818 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
823 struct hidraw *devraw;
825 devraw = dev_get_drvdata(dev);
826 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
827 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
829 count += sprintf(&buf[count], "read fail\n");
833 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
835 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
836 const char *buf, size_t count)
838 struct hidraw *devraw = dev_get_drvdata(dev);
841 ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
842 return ret > 0 ? count : ret;
845 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
848 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
850 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
851 unsigned char report_number = reg;
852 unsigned char report_type = HID_REGR_REPORT;
853 char buf[1 + sizeof(data) * len];
854 int readlen = 1 + sizeof(data) * len;
857 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
858 if (ret != readlen) {
859 hid_err(hid, "id_hw_raw_request fail\n");
861 memcpy(data, &buf[1], sizeof(data) * len);
867 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
869 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
870 unsigned char report_number = HID_REPORT_ID_W;
871 unsigned char report_type = HID_REGW_REPORT;
872 char buf[3 + sizeof(data)];
875 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
876 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
878 hid_info(hid, "id_hw_raw_request fail\n");
880 hid_info(hid, "id_hw_raw_request ok\n");
885 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
887 struct hid_device *hid;
890 hid = container_of(hdev->dev, struct hid_device, dev);
891 err = hid_hw_power(hid, PM_HINT_FULLON);
894 err = hid_hw_open(hid);
896 hid_hw_power(hid, PM_HINT_NORMAL);
903 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
905 struct hid_device *hid;
907 hid = container_of(hdev->dev, struct hid_device, dev);
908 hid_hw_power(hid, PM_HINT_NORMAL);
912 #if DYNAMIC_LOAD_MPU6500
913 static int register_mpu6500;
914 struct platform_device mpu6500_dev = {
919 static int rkvr_connect(struct hid_device *hid)
924 /* we accept any HID device, no matter the applications */
925 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
929 mutex_lock(&minors_lock);
930 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
931 if (rkvr_hidraw_table[minor])
933 rkvr_hidraw_table[minor] = dev;
938 mutex_unlock(&minors_lock);
943 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
944 NULL, "%s%d", "rkvr", minor);
946 if (IS_ERR(dev->dev)) {
947 rkvr_hidraw_table[minor] = NULL;
948 mutex_unlock(&minors_lock);
949 result = PTR_ERR(dev->dev);
954 dev_set_drvdata(dev->dev, dev);
956 device_create_file(dev->dev, &dev_attr_debug);
957 device_create_file(dev->dev, &dev_attr_sync);
961 struct rkvr_iio_hw_device *hw_device;
963 hw_device = inv_hid_alloc("hid-rkvr");
965 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
966 rkvr_hidraw_table[minor] = NULL;
967 mutex_unlock(&minors_lock);
968 result = PTR_ERR(dev->dev);
972 hw_device->dev = &hid->dev;
973 hw_device->open = rkvr_hid_open;
974 hw_device->close = rkvr_hid_close;
975 hw_device->read = rkvr_hid_read;
976 hw_device->write = rkvr_hid_write;
977 if (inv_hid_register_devcie(hw_device)) {
978 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
979 inv_hid_free(hw_device);
980 rkvr_hidraw_table[minor] = NULL;
981 mutex_unlock(&minors_lock);
982 result = PTR_ERR(dev->dev);
988 #if DYNAMIC_LOAD_MPU6500
989 if (!register_mpu6500) {
990 register_mpu6500 = 1;
991 hid_info(hid, "--->platform_device_register-->\n");
992 platform_device_register(&mpu6500_dev);
996 if (hid_hw_open(hid)) {
997 rkvr_hidraw_table[minor] = NULL;
998 mutex_unlock(&minors_lock);
999 result = PTR_ERR(dev->dev);
1001 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
1005 init_waitqueue_head(&dev->wait);
1006 spin_lock_init(&dev->list_lock);
1007 INIT_LIST_HEAD(&dev->list);
1012 hid->hidraw = dev; /*struct hidraw * **/
1014 hid_get_capability(&hid->dev, &rkvr_hid_capability[minor]);
1016 mutex_unlock(&minors_lock);
1021 static int rkvr_keys_remove(struct hid_device *hdev)
1023 struct input_dev *input = hdev->hiddev;
1025 input_unregister_device(input);
1029 static unsigned int key_codes[] = {
1045 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
1048 struct device *dev = &hdev->dev;
1049 struct input_dev *input = NULL;
1052 input = devm_input_allocate_device(dev);
1054 hid_err(hdev, "input_allocate_device fail\n");
1057 input->name = "rkvr-keypad";
1058 input->phys = "rkvr-keys/input0";
1059 input->dev.parent = dev;
1060 input->id.bustype = BUS_HOST;
1061 input->id.vendor = 0x071b;
1062 input->id.product = 0x3205;
1063 input->id.version = 0x0001;
1065 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
1066 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
1067 input_set_capability(input, EV_KEY, key_codes[i]);
1070 #ifdef RK_HID_GEAR_TOUCH
1071 set_bit(EV_REL, input->evbit);
1072 input_set_capability(input, EV_REL, REL_X);
1073 input_set_capability(input, EV_REL, REL_Y);
1074 input_set_capability(input, EV_MSC, MSC_SCAN);
1075 input_set_capability(input, EV_KEY, 0x110);
1078 error = input_register_device(input);
1080 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
1083 hdev->hiddev = input;
1088 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1090 int ret = hdev->ll_driver->start(hdev);
1094 ret = rkvr_connect(hdev);
1096 hdev->ll_driver->stop(hdev);
1101 static void rkvr_disconnect(struct hid_device *hid)
1103 struct hidraw *hidraw = hid->hidraw;
1105 mutex_lock(&minors_lock);
1106 /* always unregistering inv_hid_device when hardware disconnect */
1107 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1109 device_remove_file(hidraw->dev, &dev_attr_debug);
1110 device_remove_file(hidraw->dev, &dev_attr_sync);
1113 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1114 rkvr_drop_ref(hidraw, 1);
1115 mutex_unlock(&minors_lock);
1118 static void rkvr_hw_stop(struct hid_device *hdev)
1120 rkvr_disconnect(hdev);
1121 hdev->ll_driver->stop(hdev);
1124 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1126 struct inode *inode = file_inode(file);
1127 unsigned int minor = iminor(inode);
1130 void __user *user_arg = (void __user *)arg;
1132 mutex_lock(&minors_lock);
1133 dev = rkvr_hidraw_table[minor];
1140 case HIDIOCGRDESCSIZE:
1141 if (put_user(dev->hid->rsize, (int __user *)arg))
1149 if (get_user(len, (int __user *)arg))
1151 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1153 else if (copy_to_user(user_arg + offsetof(
1154 struct hidraw_report_descriptor,
1157 min(dev->hid->rsize, len)))
1161 case HIDIOCGRAWINFO:
1163 struct hidraw_devinfo dinfo;
1165 dinfo.bustype = dev->hid->bus;
1166 dinfo.vendor = dev->hid->vendor;
1167 dinfo.product = dev->hid->product;
1168 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1174 struct hid_device *hid = dev->hid;
1176 if (_IOC_TYPE(cmd) != 'H') {
1181 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1182 int len = _IOC_SIZE(cmd);
1184 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1187 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1188 int len = _IOC_SIZE(cmd);
1190 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1194 if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1195 int len = _IOC_SIZE(cmd);
1198 buf = kzalloc(len + 1, GFP_KERNEL);
1203 if (copy_from_user(buf, user_arg, len)) {
1208 ret = hid_report_sync(&hid->dev, buf, len);
1213 /* Begin Read-only ioctls. */
1214 if (_IOC_DIR(cmd) != _IOC_READ) {
1219 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1220 int len = strlen(hid->name) + 1;
1222 if (len > _IOC_SIZE(cmd))
1223 len = _IOC_SIZE(cmd);
1224 ret = copy_to_user(user_arg, hid->name, len) ?
1229 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1230 int len = strlen(hid->phys) + 1;
1232 if (len > _IOC_SIZE(cmd))
1233 len = _IOC_SIZE(cmd);
1234 ret = copy_to_user(user_arg, hid->phys, len) ?
1243 mutex_unlock(&minors_lock);
1247 static const struct file_operations rkvr_ops = {
1248 .owner = THIS_MODULE,
1249 .read = rkvr_hidraw_read,
1250 .write = rkvr_hidraw_write,
1251 .poll = rkvr_hidraw_poll,
1252 .open = rkvr_hidraw_open,
1253 .release = rkvr_hidraw_release,
1254 .unlocked_ioctl = rkvr_hidraw_ioctl,
1255 #ifdef CONFIG_COMPAT
1256 .compat_ioctl = rkvr_hidraw_ioctl,
1258 .fasync = rkvr_hidraw_fasync,
1259 .llseek = noop_llseek,
1262 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1264 sensorData.priv = priv;
1265 sensorData.send_event = callback;
1269 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1271 static int rkvr_fb_event_notify(struct notifier_block *self,
1272 unsigned long action, void *data)
1275 unsigned char buf[3] = {HID_REPORT_ID_RKVR, RKVR_ID_IDLE, 0};
1276 struct hid_device *hid;
1277 struct fb_event *event = data;
1280 if (action != FB_EARLY_EVENT_BLANK && action != FB_EVENT_BLANK)
1283 blank_mode = *((int *)event->data);
1285 mutex_lock(&minors_lock);
1286 for (i = 0; i < RKVR_HIDRAW_MAX_DEVICES; i++) {
1287 if (!rkvr_hidraw_table[i] || !rkvr_hidraw_table[i]->exist)
1289 if (!rkvr_hid_capability[i].suspend_notify) {
1292 hid = rkvr_hidraw_table[i]->hid;
1293 if (action == FB_EARLY_EVENT_BLANK) {
1294 switch (blank_mode) {
1295 case FB_BLANK_UNBLANK:
1298 rkvr_send_report(&hid->dev, buf, 3);
1301 } else if (action == FB_EVENT_BLANK) {
1302 switch (blank_mode) {
1303 case FB_BLANK_UNBLANK:
1305 rkvr_send_report(&hid->dev, buf, 3);
1312 mutex_unlock(&minors_lock);
1316 static struct notifier_block rkvr_fb_notifier = {
1317 .notifier_call = rkvr_fb_event_notify,
1320 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1323 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1325 retval = hid_parse(hdev);
1327 hid_err(hdev, "rkvr - parse failed\n");
1330 hid_set_drvdata(hdev, &sensorData);
1331 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1332 retval = rkvr_keys_probe(hdev);
1334 hid_err(hdev, "rkvr_keys_probe failed\n");
1337 retval = rkvr_hw_start(hdev, 0);
1339 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1340 rkvr_keys_remove(hdev);
1344 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1346 hid_err(hdev, "rkvr - hid hw start failed\n");
1359 static void rkvr_remove(struct hid_device *hdev)
1361 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1363 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1365 rkvr_keys_remove(hdev);
1371 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1374 static unsigned int count;
1375 static unsigned long old_jiffy;
1377 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1379 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1380 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1381 intf->cur_altsetting->desc.bInterfaceNumber);
1385 /* print sensor poll frequency */
1386 if (++count >= 1000) {
1387 unsigned long cur_jiffy = jiffies;
1389 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1391 old_jiffy = cur_jiffy;
1394 if (hdev->hidraw || hdev->hiddev) {
1395 retval = rkvr_report_event(hdev, data, size);
1397 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1403 static const struct hid_device_id rkvr_devices[] = {
1404 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1408 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1410 static struct hid_driver rkvr_driver = {
1412 .id_table = rkvr_devices,
1413 .probe = rkvr_probe,
1414 .remove = rkvr_remove,
1415 .raw_event = rkvr_raw_event
1418 static int __init rkvr_init(void)
1423 rkvr_class = class_create(THIS_MODULE, "rkvr");
1424 if (IS_ERR(rkvr_class))
1425 return PTR_ERR(rkvr_class);
1427 retval = hid_register_driver(&rkvr_driver);
1429 pr_warn("rkvr_init - Can't register drive.\n");
1433 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1434 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1436 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1440 rkvr_major = MAJOR(dev_id);
1441 cdev_init(&rkvr_cdev, &rkvr_ops);
1442 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1444 retval = fb_register_client(&rkvr_fb_notifier);
1446 pr_warn("rkvr_init - Can't register fb notifier\n");
1451 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1453 hid_unregister_driver(&rkvr_driver);
1455 class_destroy(rkvr_class);
1460 static void __exit rkvr_exit(void)
1462 dev_t dev_id = MKDEV(rkvr_major, 0);
1464 fb_unregister_client(&rkvr_fb_notifier);
1465 cdev_del(&rkvr_cdev);
1467 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1469 hid_unregister_driver(&rkvr_driver);
1470 class_destroy(rkvr_class);
1473 module_init(rkvr_init);
1474 module_exit(rkvr_exit);
1476 MODULE_AUTHOR("zwp");
1477 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1478 MODULE_LICENSE("GPL v2");