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
39 static struct class *rkvr_class;
41 static struct hidraw *rkvr_hidraw_table[RKVR_HIDRAW_MAX_DEVICES];
43 static struct hid_capability
46 } rkvr_hid_capability[RKVR_HIDRAW_MAX_DEVICES];
47 static DEFINE_MUTEX(minors_lock);
51 __u16 key_menu_down:1;
53 __u16 key_home_down:1;
55 __u16 key_power_down:1;
57 __u16 key_volup_down:1;
59 __u16 key_voldn_down:1;
63 __u16 key_up_pressed:1;
64 __u16 key_up_released:1;
65 __u16 key_down_pressed:1;
66 __u16 key_down_released:1;
67 __u16 key_left_pressed:1;
68 __u16 key_left_released:1;
69 __u16 key_right_pressed:1;
70 __u16 key_right_released:1;
71 __u16 key_enter_pressed:1;
72 __u16 key_enter_released:1;
81 __u8 buf_sensortemperature[2];
84 struct keymap_t key_map;
89 static int rkvr_major;
90 static struct cdev rkvr_cdev;
91 static unsigned int count_array[15] = {0,};
92 static unsigned long old_jiffy_array[15] = {0,};
93 static int rkvr_index;
96 struct sensor_hid_data {
98 int (*send_event)(char *raw_data, size_t raw_len, void *priv);
101 static DEFINE_MUTEX(device_list_lock);
102 static struct list_head rkvr_hid_hw_device_list = {
103 .next = &rkvr_hid_hw_device_list,
104 .prev = &rkvr_hid_hw_device_list
107 static struct rkvr_iio_hw_device *inv_hid_alloc(const char *name)
109 struct rkvr_iio_hw_device *p;
114 s = kstrdup_const(name, GFP_KERNEL);
117 p = kzalloc(sizeof(*p), GFP_KERNEL);
123 pr_err("%s error!\n", __func__);
129 static void inv_hid_free(struct rkvr_iio_hw_device *hw_device)
131 kfree_const(hw_device->name);
135 static int inv_hid_register_devcie(struct rkvr_iio_hw_device *hw_device)
138 mutex_lock(&device_list_lock);
139 if (hw_device->name && (!list_empty(&rkvr_hid_hw_device_list))) {
140 struct rkvr_iio_hw_device *p;
142 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
143 if (!strcmp(hw_device->name, p->name)) {
144 pr_err("%s already exist ,abort\n", hw_device->name);
149 list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
150 mutex_unlock(&device_list_lock);
154 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
156 struct rkvr_iio_hw_device *p = NULL;
158 mutex_lock(&device_list_lock);
159 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
160 if (!strcmp(name, p->name)) {
166 pr_info("find dev with name %s,free now\n", name);
169 mutex_unlock(&device_list_lock);
172 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
174 struct hidraw_list *list = file->private_data;
176 DECLARE_WAITQUEUE(wait, current);
178 mutex_lock(&list->read_mutex);
180 if (list->head == list->tail) {
181 add_wait_queue(&list->hidraw->wait, &wait);
182 set_current_state(TASK_INTERRUPTIBLE);
184 while (list->head == list->tail) {
185 if (signal_pending(current)) {
189 if (!list->hidraw->exist) {
193 if (file->f_flags & O_NONBLOCK) {
198 /* allow O_NONBLOCK to work well from other threads */
199 mutex_unlock(&list->read_mutex);
201 mutex_lock(&list->read_mutex);
202 set_current_state(TASK_INTERRUPTIBLE);
205 set_current_state(TASK_RUNNING);
206 remove_wait_queue(&list->hidraw->wait, &wait);
212 len = list->buffer[list->tail].len > count ?
213 count : list->buffer[list->tail].len;
215 if (list->buffer[list->tail].value) {
216 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
221 if (opens > 0 && rkvr_index < 15) {
222 if (++count_array[rkvr_index] >= 1000) {
223 unsigned long cur_jiffy = jiffies;
225 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);
226 count_array[rkvr_index] = 0;
227 old_jiffy_array[rkvr_index] = cur_jiffy;
229 if (++rkvr_index >= opens)
236 kfree(list->buffer[list->tail].value);
237 list->buffer[list->tail].value = NULL;
238 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
241 mutex_unlock(&list->read_mutex);
245 /* The first byte is expected to be a report number.
246 * This function is to be called with the minors_lock mutex held
248 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
250 unsigned int minor = iminor(file_inode(file));
251 struct hid_device *dev;
255 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
260 dev = rkvr_hidraw_table[minor]->hid;
262 if (count > HID_MAX_BUFFER_SIZE) {
263 hid_warn(dev, "rkvr - pid %d passed too large report\n",
264 task_pid_nr(current));
270 hid_warn(dev, "rkvr - pid %d passed too short report\n",
271 task_pid_nr(current));
276 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
282 if (copy_from_user(buf, buffer, count)) {
287 if ((report_type == HID_OUTPUT_REPORT) &&
288 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
289 ret = hid_hw_output_report(dev, buf, count);
291 * compatibility with old implementation of USB-HID and I2C-HID:
292 * if the device does not support receiving output reports,
293 * on an interrupt endpoint, fallback to SET_REPORT HID command.
299 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
308 /* the first byte is expected to be a report number */
309 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
313 mutex_lock(&minors_lock);
314 ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
315 mutex_unlock(&minors_lock);
319 /* This function performs a Get_Report transfer over the control endpoint
320 * per section 7.2.1 of the HID specification, version 1.1. The first byte
321 * of buffer is the report number to request, or 0x0 if the defice does not
322 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
323 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
326 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
328 unsigned int minor = iminor(file_inode(file));
329 struct hid_device *dev;
332 unsigned char report_number;
334 dev = rkvr_hidraw_table[minor]->hid;
336 if (!dev->ll_driver->raw_request) {
341 if (count > HID_MAX_BUFFER_SIZE) {
342 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
343 task_pid_nr(current));
349 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
350 task_pid_nr(current));
355 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
362 * Read the first byte from the user. This is the report number,
363 * which is passed to hid_hw_raw_request().
365 if (copy_from_user(&report_number, buffer, 1)) {
370 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
374 len = (ret < count) ? ret : count;
376 if (copy_to_user(buffer, buf, len)) {
389 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
391 struct hidraw_list *list = file->private_data;
393 poll_wait(file, &list->hidraw->wait, wait);
394 if (list->head != list->tail)
395 return POLLIN | POLLRDNORM;
396 if (!list->hidraw->exist)
397 return POLLERR | POLLHUP;
402 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
404 unsigned int minor = iminor(inode);
406 struct hidraw_list *list;
410 list = kzalloc(sizeof(*list), GFP_KERNEL);
416 mutex_lock(&minors_lock);
417 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
422 dev = rkvr_hidraw_table[minor];
424 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
430 err = hid_hw_open(dev->hid);
433 hid_hw_power(dev->hid, PM_HINT_NORMAL);
439 list->hidraw = rkvr_hidraw_table[minor];
440 mutex_init(&list->read_mutex);
441 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
442 list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
443 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
444 file->private_data = list;
449 mutex_unlock(&minors_lock);
457 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
459 struct hidraw_list *list = file->private_data;
461 return fasync_helper(fd, file, on, &list->fasync);
464 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
466 if (exists_bit) { /*hw removed**/
469 hid_hw_close(hidraw->hid);
470 wake_up_interruptible(&hidraw->wait);
477 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
478 rkvr_hidraw_table[hidraw->minor] = NULL;
481 /* close device for last reader */
482 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
483 hid_hw_close(hidraw->hid);
488 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
490 unsigned int minor = iminor(inode);
491 struct hidraw_list *list = file->private_data;
494 mutex_lock(&minors_lock);
496 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
497 list_del(&list->node);
498 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
501 rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
503 mutex_unlock(&minors_lock);
508 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
514 input_report_key(input, key_value, 1);
517 input_report_key(input, key_value, 0);
522 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
524 struct input_dev *input = hdev->hiddev;
525 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
527 if (rkvr_data->rkvr_data.key_map.key_menu_up)
528 rkvr_send_key_event(input, KEY_MENU, 0);
529 else if (rkvr_data->rkvr_data.key_map.key_menu_down)
530 rkvr_send_key_event(input, KEY_MENU, 1);
531 else if (rkvr_data->rkvr_data.key_map.key_home_up)
532 rkvr_send_key_event(input, KEY_HOME, 0);
533 else if (rkvr_data->rkvr_data.key_map.key_home_down)
534 rkvr_send_key_event(input, KEY_HOME, 1);
535 else if (rkvr_data->rkvr_data.key_map.key_power_up)
536 rkvr_send_key_event(input, KEY_POWER, 0);
537 else if (rkvr_data->rkvr_data.key_map.key_power_down)
538 rkvr_send_key_event(input, KEY_POWER, 1);
539 else if (rkvr_data->rkvr_data.key_map.key_volup_up)
540 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
541 else if (rkvr_data->rkvr_data.key_map.key_volup_down)
542 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
543 else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
544 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
545 else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
546 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
547 else if (rkvr_data->rkvr_data.key_map.key_esc_up)
548 rkvr_send_key_event(input, KEY_ESC, 0);
549 else if (rkvr_data->rkvr_data.key_map.key_esc_down)
550 rkvr_send_key_event(input, KEY_ESC, 1);
551 else if (rkvr_data->rkvr_data.key_map.key_up_pressed) {
552 rkvr_send_key_event(input, KEY_UP, 1);
553 rkvr_send_key_event(input, KEY_UP, 0);
554 } else if (rkvr_data->rkvr_data.key_map.key_down_pressed) {
555 rkvr_send_key_event(input, KEY_DOWN, 1);
556 rkvr_send_key_event(input, KEY_DOWN, 0);
557 } else if (rkvr_data->rkvr_data.key_map.key_left_pressed) {
558 rkvr_send_key_event(input, KEY_LEFT, 1);
559 rkvr_send_key_event(input, KEY_LEFT, 0);
560 } else if (rkvr_data->rkvr_data.key_map.key_right_pressed) {
561 rkvr_send_key_event(input, KEY_RIGHT, 1);
562 rkvr_send_key_event(input, KEY_RIGHT, 0);
563 } else if (rkvr_data->rkvr_data.key_map.key_enter_pressed) {
564 rkvr_send_key_event(input, KEY_ENTER, 1);
565 rkvr_send_key_event(input, KEY_ENTER, 0);
568 if (rkvr_data->rkvr_data.key_map.psensor_on) {
569 hid_info(hdev, "event: psensor_on\n");
570 rkvr_send_key_event(input, KEY_POWER, 1);
571 rkvr_send_key_event(input, KEY_POWER, 0);
572 } else if (rkvr_data->rkvr_data.key_map.psensor_off) {
573 hid_info(hdev, "event: psensor_off\n");
574 rkvr_send_key_event(input, KEY_POWER, 1);
575 rkvr_send_key_event(input, KEY_POWER, 0);
581 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
583 struct hidraw *dev = hid->hidraw;
584 struct hidraw_list *list;
587 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
588 struct sensor_hid_data *pdata = hid_get_drvdata(hid);
590 spin_lock_irqsave(&dev->list_lock, flags);
592 rkvr_keys_event(hid, data, len);
594 if (pdata && pdata->priv && pdata->send_event) {
595 pdata->send_event(rkvr_data->buf, len, pdata->priv);
596 spin_unlock_irqrestore(&dev->list_lock, flags);
598 list_for_each_entry(list, &dev->list, node) {
599 int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
601 if (new_head == list->tail)
604 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
605 if (!list->buffer[list->head].value) {
607 spin_unlock_irqrestore(&dev->list_lock, flags);
611 list->buffer[list->head].len = len;
612 list->head = new_head;
613 kill_fasync(&list->fasync, SIGIO, POLL_IN);
615 spin_unlock_irqrestore(&dev->list_lock, flags);
616 wake_up_interruptible(&dev->wait);
621 /******************************************
622 *--------------------
624 *--------------------
626 ******************************************/
627 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
629 struct hid_device *hid = container_of(dev, struct hid_device, dev);
630 unsigned char reportnum = HID_REPORT_ID_RKVR;
631 unsigned char rtype = HID_OUTPUT_REPORT;
634 ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
636 hid_err(hid, "rkvr_send_report fail\n");
640 hid_info(hid, "rkvr_send_report ok\n");
646 static int rkvr_recv_report(struct device *dev, u8 type, u8 *data, int len)
648 struct hid_device *hid = container_of(dev, struct hid_device, dev);
649 unsigned char report_number = type;
650 unsigned char report_type = HID_MISC_REPORT;
651 char buf[1 + sizeof(*data) * len];
652 int readlen = 1 + sizeof(*data) * len;
655 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
656 if (ret != readlen) {
657 hid_info(hid, "rkvr_recv_report fail\n");
660 memcpy(data, &buf[1], len);
661 hid_info(hid, "rkvr_recv_report %02x\n", type);
667 * for enable sensor data
668 ************************************
670 * first 8 bytes :random digits
671 * left bytes :encryt data
672 * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
673 ***********************************
676 static int hid_report_sync(struct device *dev, const char *data, size_t count)
678 struct hid_device *hid = container_of(dev, struct hid_device, dev);
680 unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
681 unsigned char buf2[3] = {0};
687 p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
689 hid_err(hid, "no mem\n");
692 memcpy(p, data, count);
693 colon = strnchr(p, count, ':');
695 hid_err(hid, "must have conlon\n");
699 if (colon - p + 1 >= count) {
700 hid_err(hid, "must have sync string after conlon\n");
706 tmp = (u64 *)(buf + 2);
707 if (kstrtoull(p, 10, tmp)) {
708 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
712 hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
713 len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
714 for (i = 0; i < len; i++) {
715 buf2[0] = colon[i * 2];
716 buf2[1] = colon[i * 2 + 1];
717 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
718 hid_err(hid, "err sync string,only hex string allowed\n");
723 len = i + sizeof(*tmp) + 2;
724 ret = rkvr_send_report(dev, (unsigned char *)buf, len);
726 hid_err(hid, "hid_report_encrypt fail\n");
730 hid_info(hid, "hid_report_encrypt ok\n");
738 static int hid_get_capability(struct device *dev, struct hid_capability *caps)
740 struct hid_device *hid = container_of(dev, struct hid_device, dev);
743 caps->suspend_notify = 0;
744 if (!rkvr_recv_report(dev, RKVR_ID_CAPS, &data, 1)) {
745 hid_info(hid, "hid_get_capability %d\n", data);
746 caps->suspend_notify = data;
752 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
755 buf[0] = (1 << 7) | (len && 0x7f);
757 buf[0] = len && 0x7f;
759 memcpy(&buf[2], data, len);
764 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
766 struct hid_device *hid = container_of(dev, struct hid_device, dev);
767 unsigned char report_number = reg;
768 unsigned char report_type = HID_REGR_REPORT;
769 char buf[1 + sizeof(data) * len];
770 int readlen = 1 + sizeof(data) * len;
773 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
774 if (ret != readlen) {
775 hid_info(hid, "id_hw_raw_request fail\n");
777 memcpy(data, &buf[1], readlen);
778 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
784 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
786 struct hid_device *hid = container_of(dev, struct hid_device, dev);
787 unsigned char report_number = HID_REPORT_ID_W;
788 unsigned char report_type = HID_REGW_REPORT;
789 char buf[3 + sizeof(data)];
792 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
793 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
795 hid_info(hid, "id_hw_raw_request fail\n");
797 hid_info(hid, "id_hw_raw_request ok\n");
802 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
805 struct hidraw *devraw;
807 devraw = dev_get_drvdata(dev);
808 if (0 == strncmp(buf, "write", 5))
809 hid_report_writereg(&devraw->hid->dev, 0, 0);
810 hid_info(devraw->hid, "%s\n", buf);
815 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
820 struct hidraw *devraw;
822 devraw = dev_get_drvdata(dev);
823 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
824 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
826 count += sprintf(&buf[count], "read fail\n");
830 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
832 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
833 const char *buf, size_t count)
835 struct hidraw *devraw = dev_get_drvdata(dev);
838 ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
839 return ret > 0 ? count : ret;
842 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
845 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
847 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
848 unsigned char report_number = reg;
849 unsigned char report_type = HID_REGR_REPORT;
850 char buf[1 + sizeof(data) * len];
851 int readlen = 1 + sizeof(data) * len;
854 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
855 if (ret != readlen) {
856 hid_err(hid, "id_hw_raw_request fail\n");
858 memcpy(data, &buf[1], sizeof(data) * len);
864 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
866 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
867 unsigned char report_number = HID_REPORT_ID_W;
868 unsigned char report_type = HID_REGW_REPORT;
869 char buf[3 + sizeof(data)];
872 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
873 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
875 hid_info(hid, "id_hw_raw_request fail\n");
877 hid_info(hid, "id_hw_raw_request ok\n");
882 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
884 struct hid_device *hid;
887 hid = container_of(hdev->dev, struct hid_device, dev);
888 err = hid_hw_power(hid, PM_HINT_FULLON);
891 err = hid_hw_open(hid);
893 hid_hw_power(hid, PM_HINT_NORMAL);
900 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
902 struct hid_device *hid;
904 hid = container_of(hdev->dev, struct hid_device, dev);
905 hid_hw_power(hid, PM_HINT_NORMAL);
909 #if DYNAMIC_LOAD_MPU6500
910 static int register_mpu6500;
911 struct platform_device mpu6500_dev = {
916 static int rkvr_connect(struct hid_device *hid)
921 /* we accept any HID device, no matter the applications */
922 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
926 mutex_lock(&minors_lock);
927 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
928 if (rkvr_hidraw_table[minor])
930 rkvr_hidraw_table[minor] = dev;
935 mutex_unlock(&minors_lock);
940 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
941 NULL, "%s%d", "rkvr", minor);
943 if (IS_ERR(dev->dev)) {
944 rkvr_hidraw_table[minor] = NULL;
945 mutex_unlock(&minors_lock);
946 result = PTR_ERR(dev->dev);
951 dev_set_drvdata(dev->dev, dev);
953 device_create_file(dev->dev, &dev_attr_debug);
954 device_create_file(dev->dev, &dev_attr_sync);
958 struct rkvr_iio_hw_device *hw_device;
960 hw_device = inv_hid_alloc("hid-rkvr");
962 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
963 rkvr_hidraw_table[minor] = NULL;
964 mutex_unlock(&minors_lock);
965 result = PTR_ERR(dev->dev);
969 hw_device->dev = &hid->dev;
970 hw_device->open = rkvr_hid_open;
971 hw_device->close = rkvr_hid_close;
972 hw_device->read = rkvr_hid_read;
973 hw_device->write = rkvr_hid_write;
974 if (inv_hid_register_devcie(hw_device)) {
975 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
976 inv_hid_free(hw_device);
977 rkvr_hidraw_table[minor] = NULL;
978 mutex_unlock(&minors_lock);
979 result = PTR_ERR(dev->dev);
985 #if DYNAMIC_LOAD_MPU6500
986 if (!register_mpu6500) {
987 register_mpu6500 = 1;
988 hid_info(hid, "--->platform_device_register-->\n");
989 platform_device_register(&mpu6500_dev);
993 if (hid_hw_open(hid)) {
994 rkvr_hidraw_table[minor] = NULL;
995 mutex_unlock(&minors_lock);
996 result = PTR_ERR(dev->dev);
998 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
1002 init_waitqueue_head(&dev->wait);
1003 spin_lock_init(&dev->list_lock);
1004 INIT_LIST_HEAD(&dev->list);
1009 hid->hidraw = dev; /*struct hidraw * **/
1011 hid_get_capability(&hid->dev, &rkvr_hid_capability[minor]);
1013 mutex_unlock(&minors_lock);
1018 static int rkvr_keys_remove(struct hid_device *hdev)
1020 struct input_dev *input = hdev->hiddev;
1022 input_unregister_device(input);
1026 static unsigned int key_codes[] = {
1041 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
1044 struct device *dev = &hdev->dev;
1045 struct input_dev *input = NULL;
1048 input = devm_input_allocate_device(dev);
1050 hid_err(hdev, "input_allocate_device fail\n");
1053 input->name = "rkvr-keypad";
1054 input->phys = "rkvr-keys/input0";
1055 input->dev.parent = dev;
1056 input->id.bustype = BUS_HOST;
1057 input->id.vendor = 0x071b;
1058 input->id.product = 0x3205;
1059 input->id.version = 0x0001;
1061 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
1062 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
1063 input_set_capability(input, EV_KEY, key_codes[i]);
1066 error = input_register_device(input);
1068 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
1071 hdev->hiddev = input;
1076 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1078 int ret = hdev->ll_driver->start(hdev);
1082 ret = rkvr_connect(hdev);
1084 hdev->ll_driver->stop(hdev);
1089 static void rkvr_disconnect(struct hid_device *hid)
1091 struct hidraw *hidraw = hid->hidraw;
1093 mutex_lock(&minors_lock);
1094 /* always unregistering inv_hid_device when hardware disconnect */
1095 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1097 device_remove_file(hidraw->dev, &dev_attr_debug);
1098 device_remove_file(hidraw->dev, &dev_attr_sync);
1101 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1102 rkvr_drop_ref(hidraw, 1);
1103 mutex_unlock(&minors_lock);
1106 static void rkvr_hw_stop(struct hid_device *hdev)
1108 rkvr_disconnect(hdev);
1109 hdev->ll_driver->stop(hdev);
1112 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1114 struct inode *inode = file_inode(file);
1115 unsigned int minor = iminor(inode);
1118 void __user *user_arg = (void __user *)arg;
1120 mutex_lock(&minors_lock);
1121 dev = rkvr_hidraw_table[minor];
1128 case HIDIOCGRDESCSIZE:
1129 if (put_user(dev->hid->rsize, (int __user *)arg))
1137 if (get_user(len, (int __user *)arg))
1139 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1141 else if (copy_to_user(user_arg + offsetof(
1142 struct hidraw_report_descriptor,
1145 min(dev->hid->rsize, len)))
1149 case HIDIOCGRAWINFO:
1151 struct hidraw_devinfo dinfo;
1153 dinfo.bustype = dev->hid->bus;
1154 dinfo.vendor = dev->hid->vendor;
1155 dinfo.product = dev->hid->product;
1156 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1162 struct hid_device *hid = dev->hid;
1164 if (_IOC_TYPE(cmd) != 'H') {
1169 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1170 int len = _IOC_SIZE(cmd);
1172 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1175 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1176 int len = _IOC_SIZE(cmd);
1178 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1182 if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1183 int len = _IOC_SIZE(cmd);
1186 buf = kzalloc(len + 1, GFP_KERNEL);
1191 if (copy_from_user(buf, user_arg, len)) {
1196 ret = hid_report_sync(&hid->dev, buf, len);
1201 /* Begin Read-only ioctls. */
1202 if (_IOC_DIR(cmd) != _IOC_READ) {
1207 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1208 int len = strlen(hid->name) + 1;
1210 if (len > _IOC_SIZE(cmd))
1211 len = _IOC_SIZE(cmd);
1212 ret = copy_to_user(user_arg, hid->name, len) ?
1217 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1218 int len = strlen(hid->phys) + 1;
1220 if (len > _IOC_SIZE(cmd))
1221 len = _IOC_SIZE(cmd);
1222 ret = copy_to_user(user_arg, hid->phys, len) ?
1231 mutex_unlock(&minors_lock);
1235 static const struct file_operations rkvr_ops = {
1236 .owner = THIS_MODULE,
1237 .read = rkvr_hidraw_read,
1238 .write = rkvr_hidraw_write,
1239 .poll = rkvr_hidraw_poll,
1240 .open = rkvr_hidraw_open,
1241 .release = rkvr_hidraw_release,
1242 .unlocked_ioctl = rkvr_hidraw_ioctl,
1243 #ifdef CONFIG_COMPAT
1244 .compat_ioctl = rkvr_hidraw_ioctl,
1246 .fasync = rkvr_hidraw_fasync,
1247 .llseek = noop_llseek,
1250 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1252 sensorData.priv = priv;
1253 sensorData.send_event = callback;
1257 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1259 static int rkvr_fb_event_notify(struct notifier_block *self,
1260 unsigned long action, void *data)
1263 unsigned char buf[3] = {HID_REPORT_ID_RKVR, RKVR_ID_IDLE, 0};
1264 struct hid_device *hid;
1265 struct fb_event *event = data;
1266 int blank_mode = *((int *)event->data);
1268 if (action != FB_EARLY_EVENT_BLANK && action != FB_EVENT_BLANK)
1271 mutex_lock(&minors_lock);
1272 for (i = 0; i < RKVR_HIDRAW_MAX_DEVICES; i++) {
1273 if (!rkvr_hidraw_table[i] || !rkvr_hidraw_table[i]->exist)
1275 if (!rkvr_hid_capability[i].suspend_notify) {
1278 hid = rkvr_hidraw_table[i]->hid;
1279 if (action == FB_EARLY_EVENT_BLANK) {
1280 switch (blank_mode) {
1281 case FB_BLANK_UNBLANK:
1284 rkvr_send_report(&hid->dev, buf, 3);
1287 } else if (action == FB_EVENT_BLANK) {
1288 switch (blank_mode) {
1289 case FB_BLANK_UNBLANK:
1291 rkvr_send_report(&hid->dev, buf, 3);
1298 mutex_unlock(&minors_lock);
1302 static struct notifier_block rkvr_fb_notifier = {
1303 .notifier_call = rkvr_fb_event_notify,
1306 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1309 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1311 retval = hid_parse(hdev);
1313 hid_err(hdev, "rkvr - parse failed\n");
1316 hid_set_drvdata(hdev, &sensorData);
1317 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1318 retval = rkvr_keys_probe(hdev);
1320 hid_err(hdev, "rkvr_keys_probe failed\n");
1323 retval = rkvr_hw_start(hdev, 0);
1325 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1326 rkvr_keys_remove(hdev);
1330 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1332 hid_err(hdev, "rkvr - hid hw start failed\n");
1345 static void rkvr_remove(struct hid_device *hdev)
1347 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1349 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1351 rkvr_keys_remove(hdev);
1357 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1360 static unsigned int count;
1361 static unsigned long old_jiffy;
1363 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1365 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1366 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1367 intf->cur_altsetting->desc.bInterfaceNumber);
1371 /* print sensor poll frequency */
1372 if (++count >= 1000) {
1373 unsigned long cur_jiffy = jiffies;
1375 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1377 old_jiffy = cur_jiffy;
1380 if (hdev->hidraw || hdev->hiddev) {
1381 retval = rkvr_report_event(hdev, data, size);
1383 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1389 static const struct hid_device_id rkvr_devices[] = {
1390 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1394 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1396 static struct hid_driver rkvr_driver = {
1398 .id_table = rkvr_devices,
1399 .probe = rkvr_probe,
1400 .remove = rkvr_remove,
1401 .raw_event = rkvr_raw_event
1404 static int __init rkvr_init(void)
1409 rkvr_class = class_create(THIS_MODULE, "rkvr");
1410 if (IS_ERR(rkvr_class))
1411 return PTR_ERR(rkvr_class);
1413 retval = hid_register_driver(&rkvr_driver);
1415 pr_warn("rkvr_init - Can't register drive.\n");
1419 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1420 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1422 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1426 rkvr_major = MAJOR(dev_id);
1427 cdev_init(&rkvr_cdev, &rkvr_ops);
1428 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1430 retval = fb_register_client(&rkvr_fb_notifier);
1432 pr_warn("rkvr_init - Can't register fb notifier\n");
1437 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1439 hid_unregister_driver(&rkvr_driver);
1441 class_destroy(rkvr_class);
1446 static void __exit rkvr_exit(void)
1448 dev_t dev_id = MKDEV(rkvr_major, 0);
1450 fb_unregister_client(&rkvr_fb_notifier);
1451 cdev_del(&rkvr_cdev);
1453 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1455 hid_unregister_driver(&rkvr_driver);
1456 class_destroy(rkvr_class);
1459 module_init(rkvr_init);
1460 module_exit(rkvr_exit);
1462 MODULE_AUTHOR("zwp");
1463 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1464 MODULE_LICENSE("GPL v2");