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>
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
37 static struct class *rkvr_class;
39 static struct hidraw *rkvr_hidraw_table[RKVR_HIDRAW_MAX_DEVICES];
41 static DEFINE_MUTEX(minors_lock);
45 __u16 key_menu_down:1;
47 __u16 key_home_down:1;
49 __u16 key_power_down:1;
51 __u16 key_volup_down:1;
53 __u16 key_voldn_down:1;
57 __u16 key_up_pressed:1;
58 __u16 key_up_released:1;
59 __u16 key_down_pressed:1;
60 __u16 key_down_released:1;
61 __u16 key_left_pressed:1;
62 __u16 key_left_released:1;
63 __u16 key_right_pressed:1;
64 __u16 key_right_released:1;
65 __u16 key_enter_pressed:1;
66 __u16 key_enter_released:1;
73 __u8 buf_sensortemperature[2];
76 struct keymap_t key_map;
81 static int rkvr_major;
82 static struct cdev rkvr_cdev;
83 static unsigned int count_array[15] = {0,};
84 static unsigned long old_jiffy_array[15] = {0,};
85 static int rkvr_index;
88 struct sensor_hid_data {
90 int (*send_event)(char *raw_data, size_t raw_len, void *priv);
93 static DEFINE_MUTEX(device_list_lock);
94 static struct list_head rkvr_hid_hw_device_list = {
95 .next = &rkvr_hid_hw_device_list,
96 .prev = &rkvr_hid_hw_device_list
99 static struct rkvr_iio_hw_device *inv_hid_alloc(const char *name)
101 struct rkvr_iio_hw_device *p;
106 s = kstrdup_const(name, GFP_KERNEL);
109 p = kzalloc(sizeof(*p), GFP_KERNEL);
115 pr_err("%s error!\n", __func__);
121 static void inv_hid_free(struct rkvr_iio_hw_device *hw_device)
123 kfree_const(hw_device->name);
127 static int inv_hid_register_devcie(struct rkvr_iio_hw_device *hw_device)
130 mutex_lock(&device_list_lock);
131 if (hw_device->name && (!list_empty(&rkvr_hid_hw_device_list))) {
132 struct rkvr_iio_hw_device *p;
134 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
135 if (!strcmp(hw_device->name, p->name)) {
136 pr_err("%s already exist ,abort\n", hw_device->name);
141 list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
142 mutex_unlock(&device_list_lock);
146 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
148 struct rkvr_iio_hw_device *p = NULL;
150 mutex_lock(&device_list_lock);
151 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
152 if (!strcmp(name, p->name)) {
158 pr_info("find dev with name %s,free now\n", name);
161 mutex_unlock(&device_list_lock);
164 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
166 struct hidraw_list *list = file->private_data;
168 DECLARE_WAITQUEUE(wait, current);
170 mutex_lock(&list->read_mutex);
172 if (list->head == list->tail) {
173 add_wait_queue(&list->hidraw->wait, &wait);
174 set_current_state(TASK_INTERRUPTIBLE);
176 while (list->head == list->tail) {
177 if (signal_pending(current)) {
181 if (!list->hidraw->exist) {
185 if (file->f_flags & O_NONBLOCK) {
190 /* allow O_NONBLOCK to work well from other threads */
191 mutex_unlock(&list->read_mutex);
193 mutex_lock(&list->read_mutex);
194 set_current_state(TASK_INTERRUPTIBLE);
197 set_current_state(TASK_RUNNING);
198 remove_wait_queue(&list->hidraw->wait, &wait);
204 len = list->buffer[list->tail].len > count ?
205 count : list->buffer[list->tail].len;
207 if (list->buffer[list->tail].value) {
208 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
213 if (opens > 0 && rkvr_index < 15) {
214 if (++count_array[rkvr_index] >= 1000) {
215 unsigned long cur_jiffy = jiffies;
217 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);
218 count_array[rkvr_index] = 0;
219 old_jiffy_array[rkvr_index] = cur_jiffy;
221 if (++rkvr_index >= opens)
228 kfree(list->buffer[list->tail].value);
229 list->buffer[list->tail].value = NULL;
230 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
233 mutex_unlock(&list->read_mutex);
237 /* The first byte is expected to be a report number.
238 * This function is to be called with the minors_lock mutex held
240 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
242 unsigned int minor = iminor(file_inode(file));
243 struct hid_device *dev;
247 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
252 dev = rkvr_hidraw_table[minor]->hid;
254 if (count > HID_MAX_BUFFER_SIZE) {
255 hid_warn(dev, "rkvr - pid %d passed too large report\n",
256 task_pid_nr(current));
262 hid_warn(dev, "rkvr - pid %d passed too short report\n",
263 task_pid_nr(current));
268 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
274 if (copy_from_user(buf, buffer, count)) {
279 if ((report_type == HID_OUTPUT_REPORT) &&
280 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
281 ret = hid_hw_output_report(dev, buf, count);
283 * compatibility with old implementation of USB-HID and I2C-HID:
284 * if the device does not support receiving output reports,
285 * on an interrupt endpoint, fallback to SET_REPORT HID command.
291 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
300 /* the first byte is expected to be a report number */
301 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
305 mutex_lock(&minors_lock);
306 ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
307 mutex_unlock(&minors_lock);
311 /* This function performs a Get_Report transfer over the control endpoint
312 * per section 7.2.1 of the HID specification, version 1.1. The first byte
313 * of buffer is the report number to request, or 0x0 if the defice does not
314 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
315 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
318 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
320 unsigned int minor = iminor(file_inode(file));
321 struct hid_device *dev;
324 unsigned char report_number;
326 dev = rkvr_hidraw_table[minor]->hid;
328 if (!dev->ll_driver->raw_request) {
333 if (count > HID_MAX_BUFFER_SIZE) {
334 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
335 task_pid_nr(current));
341 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
342 task_pid_nr(current));
347 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
354 * Read the first byte from the user. This is the report number,
355 * which is passed to hid_hw_raw_request().
357 if (copy_from_user(&report_number, buffer, 1)) {
362 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
366 len = (ret < count) ? ret : count;
368 if (copy_to_user(buffer, buf, len)) {
381 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
383 struct hidraw_list *list = file->private_data;
385 poll_wait(file, &list->hidraw->wait, wait);
386 if (list->head != list->tail)
387 return POLLIN | POLLRDNORM;
388 if (!list->hidraw->exist)
389 return POLLERR | POLLHUP;
394 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
396 unsigned int minor = iminor(inode);
398 struct hidraw_list *list;
402 list = kzalloc(sizeof(*list), GFP_KERNEL);
408 mutex_lock(&minors_lock);
409 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
414 dev = rkvr_hidraw_table[minor];
416 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
422 err = hid_hw_open(dev->hid);
425 hid_hw_power(dev->hid, PM_HINT_NORMAL);
431 list->hidraw = rkvr_hidraw_table[minor];
432 mutex_init(&list->read_mutex);
433 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
434 list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
435 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
436 file->private_data = list;
441 mutex_unlock(&minors_lock);
449 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
451 struct hidraw_list *list = file->private_data;
453 return fasync_helper(fd, file, on, &list->fasync);
456 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
458 if (exists_bit) { /*hw removed**/
461 hid_hw_close(hidraw->hid);
462 wake_up_interruptible(&hidraw->wait);
469 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
470 rkvr_hidraw_table[hidraw->minor] = NULL;
473 /* close device for last reader */
474 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
475 hid_hw_close(hidraw->hid);
480 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
482 unsigned int minor = iminor(inode);
483 struct hidraw_list *list = file->private_data;
486 mutex_lock(&minors_lock);
488 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
489 list_del(&list->node);
490 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
493 rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
495 mutex_unlock(&minors_lock);
500 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
506 input_report_key(input, key_value, 1);
509 input_report_key(input, key_value, 0);
514 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
516 struct input_dev *input = hdev->hiddev;
517 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
519 if (rkvr_data->rkvr_data.key_map.key_menu_up)
520 rkvr_send_key_event(input, KEY_MENU, 0);
521 else if (rkvr_data->rkvr_data.key_map.key_menu_down)
522 rkvr_send_key_event(input, KEY_MENU, 1);
523 else if (rkvr_data->rkvr_data.key_map.key_home_up)
524 rkvr_send_key_event(input, KEY_HOME, 0);
525 else if (rkvr_data->rkvr_data.key_map.key_home_down)
526 rkvr_send_key_event(input, KEY_HOME, 1);
527 else if (rkvr_data->rkvr_data.key_map.key_power_up)
528 rkvr_send_key_event(input, KEY_POWER, 0);
529 else if (rkvr_data->rkvr_data.key_map.key_power_down)
530 rkvr_send_key_event(input, KEY_POWER, 1);
531 else if (rkvr_data->rkvr_data.key_map.key_volup_up)
532 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
533 else if (rkvr_data->rkvr_data.key_map.key_volup_down)
534 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
535 else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
536 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
537 else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
538 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
539 else if (rkvr_data->rkvr_data.key_map.key_esc_up)
540 rkvr_send_key_event(input, KEY_ESC, 0);
541 else if (rkvr_data->rkvr_data.key_map.key_esc_down)
542 rkvr_send_key_event(input, KEY_ESC, 1);
543 else if (rkvr_data->rkvr_data.key_map.key_up_pressed) {
544 rkvr_send_key_event(input, KEY_UP, 1);
545 rkvr_send_key_event(input, KEY_UP, 0);
546 } else if (rkvr_data->rkvr_data.key_map.key_down_pressed) {
547 rkvr_send_key_event(input, KEY_DOWN, 1);
548 rkvr_send_key_event(input, KEY_DOWN, 0);
549 } else if (rkvr_data->rkvr_data.key_map.key_left_pressed) {
550 rkvr_send_key_event(input, KEY_LEFT, 1);
551 rkvr_send_key_event(input, KEY_LEFT, 0);
552 } else if (rkvr_data->rkvr_data.key_map.key_right_pressed) {
553 rkvr_send_key_event(input, KEY_RIGHT, 1);
554 rkvr_send_key_event(input, KEY_RIGHT, 0);
555 } else if (rkvr_data->rkvr_data.key_map.key_enter_pressed) {
556 rkvr_send_key_event(input, KEY_ENTER, 1);
557 rkvr_send_key_event(input, KEY_ENTER, 0);
562 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
564 struct hidraw *dev = hid->hidraw;
565 struct hidraw_list *list;
568 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
569 struct sensor_hid_data *pdata = hid_get_drvdata(hid);
571 spin_lock_irqsave(&dev->list_lock, flags);
573 rkvr_keys_event(hid, data, len);
575 if (pdata && pdata->priv && pdata->send_event) {
576 pdata->send_event(rkvr_data->buf, len, pdata->priv);
577 spin_unlock_irqrestore(&dev->list_lock, flags);
579 list_for_each_entry(list, &dev->list, node) {
580 int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
582 if (new_head == list->tail)
585 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
586 if (!list->buffer[list->head].value) {
588 spin_unlock_irqrestore(&dev->list_lock, flags);
592 list->buffer[list->head].len = len;
593 list->head = new_head;
594 kill_fasync(&list->fasync, SIGIO, POLL_IN);
596 spin_unlock_irqrestore(&dev->list_lock, flags);
597 wake_up_interruptible(&dev->wait);
602 /******************************************
603 *--------------------
605 *--------------------
607 ******************************************/
608 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
610 struct hid_device *hid = container_of(dev, struct hid_device, dev);
611 unsigned char reportnum = HID_REPORT_ID_RKVR;
612 unsigned char rtype = HID_OUTPUT_REPORT;
615 ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
617 hid_err(hid, "rkvr_send_report fail\n");
621 hid_info(hid, "rkvr_send_report ok\n");
628 * for enable sensor data
629 ************************************
631 * first 8 bytes :random digits
632 * left bytes :encryt data
633 * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
634 ***********************************
637 static int hid_report_sync(struct device *dev, const char *data, size_t count)
639 struct hid_device *hid = container_of(dev, struct hid_device, dev);
641 unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
642 unsigned char buf2[3] = {0};
648 p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
650 hid_err(hid, "no mem\n");
653 memcpy(p, data, count);
654 colon = strnchr(p, count, ':');
656 hid_err(hid, "must have conlon\n");
660 if (colon - p + 1 >= count) {
661 hid_err(hid, "must have sync string after conlon\n");
667 tmp = (u64 *)(buf + 2);
668 if (kstrtoull(p, 10, tmp)) {
669 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
673 hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
674 len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
675 for (i = 0; i < len; i++) {
676 buf2[0] = colon[i * 2];
677 buf2[1] = colon[i * 2 + 1];
678 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
679 hid_err(hid, "err sync string,only hex string allowed\n");
684 len = i + sizeof(*tmp) + 2;
685 ret = rkvr_send_report(dev, (unsigned char *)buf, len);
687 hid_err(hid, "hid_report_encrypt fail\n");
691 hid_info(hid, "hid_report_encrypt ok\n");
699 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
702 buf[0] = (1 << 7) | (len && 0x7f);
704 buf[0] = len && 0x7f;
706 memcpy(&buf[2], data, len);
711 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
713 struct hid_device *hid = container_of(dev, struct hid_device, dev);
714 unsigned char report_number = reg;
715 unsigned char report_type = HID_REGR_REPORT;
716 char buf[1 + sizeof(data) * len];
717 int readlen = 1 + sizeof(data) * len;
720 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
721 if (ret != readlen) {
722 hid_info(hid, "id_hw_raw_request fail\n");
724 memcpy(data, &buf[1], readlen);
725 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
731 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
733 struct hid_device *hid = container_of(dev, struct hid_device, dev);
734 unsigned char report_number = HID_REPORT_ID_W;
735 unsigned char report_type = HID_REGW_REPORT;
736 char buf[3 + sizeof(data)];
739 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
740 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
742 hid_info(hid, "id_hw_raw_request fail\n");
744 hid_info(hid, "id_hw_raw_request ok\n");
749 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
752 struct hidraw *devraw;
754 devraw = dev_get_drvdata(dev);
755 if (0 == strncmp(buf, "write", 5))
756 hid_report_writereg(&devraw->hid->dev, 0, 0);
757 hid_info(devraw->hid, "%s\n", buf);
762 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
767 struct hidraw *devraw;
769 devraw = dev_get_drvdata(dev);
770 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
771 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
773 count += sprintf(&buf[count], "read fail\n");
777 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
779 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
780 const char *buf, size_t count)
782 struct hidraw *devraw = dev_get_drvdata(dev);
785 ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
786 return ret > 0 ? count : ret;
789 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
792 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
794 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
795 unsigned char report_number = reg;
796 unsigned char report_type = HID_REGR_REPORT;
797 char buf[1 + sizeof(data) * len];
798 int readlen = 1 + sizeof(data) * len;
801 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
802 if (ret != readlen) {
803 hid_err(hid, "id_hw_raw_request fail\n");
805 memcpy(data, &buf[1], sizeof(data) * len);
811 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
813 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
814 unsigned char report_number = HID_REPORT_ID_W;
815 unsigned char report_type = HID_REGW_REPORT;
816 char buf[3 + sizeof(data)];
819 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
820 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
822 hid_info(hid, "id_hw_raw_request fail\n");
824 hid_info(hid, "id_hw_raw_request ok\n");
829 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
831 struct hid_device *hid;
834 hid = container_of(hdev->dev, struct hid_device, dev);
835 err = hid_hw_power(hid, PM_HINT_FULLON);
838 err = hid_hw_open(hid);
840 hid_hw_power(hid, PM_HINT_NORMAL);
847 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
849 struct hid_device *hid;
851 hid = container_of(hdev->dev, struct hid_device, dev);
852 hid_hw_power(hid, PM_HINT_NORMAL);
856 #if DYNAMIC_LOAD_MPU6500
857 static int register_mpu6500;
858 struct platform_device mpu6500_dev = {
863 static int rkvr_connect(struct hid_device *hid)
868 /* we accept any HID device, no matter the applications */
869 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
873 mutex_lock(&minors_lock);
874 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
875 if (rkvr_hidraw_table[minor])
877 rkvr_hidraw_table[minor] = dev;
882 mutex_unlock(&minors_lock);
887 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
888 NULL, "%s%d", "rkvr", minor);
890 if (IS_ERR(dev->dev)) {
891 rkvr_hidraw_table[minor] = NULL;
892 mutex_unlock(&minors_lock);
893 result = PTR_ERR(dev->dev);
898 dev_set_drvdata(dev->dev, dev);
900 device_create_file(dev->dev, &dev_attr_debug);
901 device_create_file(dev->dev, &dev_attr_sync);
905 struct rkvr_iio_hw_device *hw_device;
907 hw_device = inv_hid_alloc("hid-rkvr");
909 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
910 rkvr_hidraw_table[minor] = NULL;
911 mutex_unlock(&minors_lock);
912 result = PTR_ERR(dev->dev);
916 hw_device->dev = &hid->dev;
917 hw_device->open = rkvr_hid_open;
918 hw_device->close = rkvr_hid_close;
919 hw_device->read = rkvr_hid_read;
920 hw_device->write = rkvr_hid_write;
921 if (inv_hid_register_devcie(hw_device)) {
922 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
923 inv_hid_free(hw_device);
924 rkvr_hidraw_table[minor] = NULL;
925 mutex_unlock(&minors_lock);
926 result = PTR_ERR(dev->dev);
932 #if DYNAMIC_LOAD_MPU6500
933 if (!register_mpu6500) {
934 register_mpu6500 = 1;
935 hid_info(hid, "--->platform_device_register-->\n");
936 platform_device_register(&mpu6500_dev);
940 if (hid_hw_open(hid)) {
941 rkvr_hidraw_table[minor] = NULL;
942 mutex_unlock(&minors_lock);
943 result = PTR_ERR(dev->dev);
945 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
949 init_waitqueue_head(&dev->wait);
950 spin_lock_init(&dev->list_lock);
951 INIT_LIST_HEAD(&dev->list);
956 hid->hidraw = dev; /*struct hidraw * **/
957 mutex_unlock(&minors_lock);
962 static int rkvr_keys_remove(struct hid_device *hdev)
964 struct input_dev *input = hdev->hiddev;
966 input_unregister_device(input);
970 static unsigned int key_codes[] = {
985 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
988 struct device *dev = &hdev->dev;
989 struct input_dev *input = NULL;
992 input = devm_input_allocate_device(dev);
994 hid_err(hdev, "input_allocate_device fail\n");
997 input->name = "rkvr-keypad";
998 input->phys = "rkvr-keys/input0";
999 input->dev.parent = dev;
1000 input->id.bustype = BUS_HOST;
1001 input->id.vendor = 0x071b;
1002 input->id.product = 0x3205;
1003 input->id.version = 0x0001;
1005 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
1006 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
1007 input_set_capability(input, EV_KEY, key_codes[i]);
1010 error = input_register_device(input);
1012 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
1015 hdev->hiddev = input;
1020 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1022 int ret = hdev->ll_driver->start(hdev);
1026 ret = rkvr_connect(hdev);
1028 hdev->ll_driver->stop(hdev);
1033 static void rkvr_disconnect(struct hid_device *hid)
1035 struct hidraw *hidraw = hid->hidraw;
1037 mutex_lock(&minors_lock);
1038 /* always unregistering inv_hid_device when hardware disconnect */
1039 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1041 device_remove_file(hidraw->dev, &dev_attr_debug);
1042 device_remove_file(hidraw->dev, &dev_attr_sync);
1045 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1046 rkvr_drop_ref(hidraw, 1);
1047 mutex_unlock(&minors_lock);
1050 static void rkvr_hw_stop(struct hid_device *hdev)
1052 rkvr_disconnect(hdev);
1053 hdev->ll_driver->stop(hdev);
1056 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1058 struct inode *inode = file_inode(file);
1059 unsigned int minor = iminor(inode);
1062 void __user *user_arg = (void __user *)arg;
1064 mutex_lock(&minors_lock);
1065 dev = rkvr_hidraw_table[minor];
1072 case HIDIOCGRDESCSIZE:
1073 if (put_user(dev->hid->rsize, (int __user *)arg))
1081 if (get_user(len, (int __user *)arg))
1083 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1085 else if (copy_to_user(user_arg + offsetof(
1086 struct hidraw_report_descriptor,
1089 min(dev->hid->rsize, len)))
1093 case HIDIOCGRAWINFO:
1095 struct hidraw_devinfo dinfo;
1097 dinfo.bustype = dev->hid->bus;
1098 dinfo.vendor = dev->hid->vendor;
1099 dinfo.product = dev->hid->product;
1100 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1106 struct hid_device *hid = dev->hid;
1108 if (_IOC_TYPE(cmd) != 'H') {
1113 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1114 int len = _IOC_SIZE(cmd);
1116 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1119 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1120 int len = _IOC_SIZE(cmd);
1122 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1126 if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1127 int len = _IOC_SIZE(cmd);
1130 buf = kzalloc(len + 1, GFP_KERNEL);
1135 if (copy_from_user(buf, user_arg, len)) {
1140 ret = hid_report_sync(&hid->dev, buf, len);
1145 /* Begin Read-only ioctls. */
1146 if (_IOC_DIR(cmd) != _IOC_READ) {
1151 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1152 int len = strlen(hid->name) + 1;
1154 if (len > _IOC_SIZE(cmd))
1155 len = _IOC_SIZE(cmd);
1156 ret = copy_to_user(user_arg, hid->name, len) ?
1161 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1162 int len = strlen(hid->phys) + 1;
1164 if (len > _IOC_SIZE(cmd))
1165 len = _IOC_SIZE(cmd);
1166 ret = copy_to_user(user_arg, hid->phys, len) ?
1175 mutex_unlock(&minors_lock);
1179 static const struct file_operations rkvr_ops = {
1180 .owner = THIS_MODULE,
1181 .read = rkvr_hidraw_read,
1182 .write = rkvr_hidraw_write,
1183 .poll = rkvr_hidraw_poll,
1184 .open = rkvr_hidraw_open,
1185 .release = rkvr_hidraw_release,
1186 .unlocked_ioctl = rkvr_hidraw_ioctl,
1187 #ifdef CONFIG_COMPAT
1188 .compat_ioctl = rkvr_hidraw_ioctl,
1190 .fasync = rkvr_hidraw_fasync,
1191 .llseek = noop_llseek,
1194 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1196 sensorData.priv = priv;
1197 sensorData.send_event = callback;
1201 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1203 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1206 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1208 retval = hid_parse(hdev);
1210 hid_err(hdev, "rkvr - parse failed\n");
1213 hid_set_drvdata(hdev, &sensorData);
1214 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1215 retval = rkvr_keys_probe(hdev);
1217 hid_err(hdev, "rkvr_keys_probe failed\n");
1220 retval = rkvr_hw_start(hdev, 0);
1222 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1223 rkvr_keys_remove(hdev);
1227 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1229 hid_err(hdev, "rkvr - hid hw start failed\n");
1242 static void rkvr_remove(struct hid_device *hdev)
1244 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1246 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1248 rkvr_keys_remove(hdev);
1254 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1257 static unsigned int count;
1258 static unsigned long old_jiffy;
1260 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1262 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1263 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1264 intf->cur_altsetting->desc.bInterfaceNumber);
1268 /* print sensor poll frequency */
1269 if (++count >= 1000) {
1270 unsigned long cur_jiffy = jiffies;
1272 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1274 old_jiffy = cur_jiffy;
1277 if (hdev->hidraw || hdev->hiddev) {
1278 retval = rkvr_report_event(hdev, data, size);
1280 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1286 static const struct hid_device_id rkvr_devices[] = {
1287 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1291 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1293 static struct hid_driver rkvr_driver = {
1295 .id_table = rkvr_devices,
1296 .probe = rkvr_probe,
1297 .remove = rkvr_remove,
1298 .raw_event = rkvr_raw_event
1301 static int __init rkvr_init(void)
1306 rkvr_class = class_create(THIS_MODULE, "rkvr");
1307 if (IS_ERR(rkvr_class))
1308 return PTR_ERR(rkvr_class);
1310 retval = hid_register_driver(&rkvr_driver);
1312 pr_warn("rkvr_init - Can't register drive.\n");
1316 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1317 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1319 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1323 rkvr_major = MAJOR(dev_id);
1324 cdev_init(&rkvr_cdev, &rkvr_ops);
1325 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1329 hid_unregister_driver(&rkvr_driver);
1331 class_destroy(rkvr_class);
1336 static void __exit rkvr_exit(void)
1338 dev_t dev_id = MKDEV(rkvr_major, 0);
1340 cdev_del(&rkvr_cdev);
1342 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1344 hid_unregister_driver(&rkvr_driver);
1345 class_destroy(rkvr_class);
1348 module_init(rkvr_init);
1349 module_exit(rkvr_exit);
1351 MODULE_AUTHOR("zwp");
1352 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1353 MODULE_LICENSE("GPL v2");