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);
145 mutex_unlock(&device_list_lock);
150 list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
151 mutex_unlock(&device_list_lock);
155 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
157 struct rkvr_iio_hw_device *p = NULL;
159 mutex_lock(&device_list_lock);
160 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
161 if (!strcmp(name, p->name)) {
167 pr_info("find dev with name %s,free now\n", name);
170 mutex_unlock(&device_list_lock);
173 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
175 struct hidraw_list *list = file->private_data;
177 DECLARE_WAITQUEUE(wait, current);
179 mutex_lock(&list->read_mutex);
181 if (list->head == list->tail) {
182 add_wait_queue(&list->hidraw->wait, &wait);
183 set_current_state(TASK_INTERRUPTIBLE);
185 while (list->head == list->tail) {
186 if (signal_pending(current)) {
190 if (!list->hidraw->exist) {
194 if (file->f_flags & O_NONBLOCK) {
199 /* allow O_NONBLOCK to work well from other threads */
200 mutex_unlock(&list->read_mutex);
202 mutex_lock(&list->read_mutex);
203 set_current_state(TASK_INTERRUPTIBLE);
206 set_current_state(TASK_RUNNING);
207 remove_wait_queue(&list->hidraw->wait, &wait);
213 len = list->buffer[list->tail].len > count ?
214 count : list->buffer[list->tail].len;
216 if (list->buffer[list->tail].value) {
217 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
222 if (opens > 0 && rkvr_index < 15) {
223 if (++count_array[rkvr_index] >= 1000) {
224 unsigned long cur_jiffy = jiffies;
226 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);
227 count_array[rkvr_index] = 0;
228 old_jiffy_array[rkvr_index] = cur_jiffy;
230 if (++rkvr_index >= opens)
237 kfree(list->buffer[list->tail].value);
238 list->buffer[list->tail].value = NULL;
239 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
242 mutex_unlock(&list->read_mutex);
246 /* The first byte is expected to be a report number.
247 * This function is to be called with the minors_lock mutex held
249 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
251 unsigned int minor = iminor(file_inode(file));
252 struct hid_device *dev;
256 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
261 dev = rkvr_hidraw_table[minor]->hid;
263 if (count > HID_MAX_BUFFER_SIZE) {
264 hid_warn(dev, "rkvr - pid %d passed too large report\n",
265 task_pid_nr(current));
271 hid_warn(dev, "rkvr - pid %d passed too short report\n",
272 task_pid_nr(current));
277 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
283 if (copy_from_user(buf, buffer, count)) {
288 if ((report_type == HID_OUTPUT_REPORT) &&
289 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
290 ret = hid_hw_output_report(dev, buf, count);
292 * compatibility with old implementation of USB-HID and I2C-HID:
293 * if the device does not support receiving output reports,
294 * on an interrupt endpoint, fallback to SET_REPORT HID command.
300 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
309 /* the first byte is expected to be a report number */
310 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
314 mutex_lock(&minors_lock);
315 ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
316 mutex_unlock(&minors_lock);
320 /* This function performs a Get_Report transfer over the control endpoint
321 * per section 7.2.1 of the HID specification, version 1.1. The first byte
322 * of buffer is the report number to request, or 0x0 if the defice does not
323 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
324 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
327 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
329 unsigned int minor = iminor(file_inode(file));
330 struct hid_device *dev;
333 unsigned char report_number;
335 dev = rkvr_hidraw_table[minor]->hid;
337 if (!dev->ll_driver->raw_request) {
342 if (count > HID_MAX_BUFFER_SIZE) {
343 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
344 task_pid_nr(current));
350 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
351 task_pid_nr(current));
356 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
363 * Read the first byte from the user. This is the report number,
364 * which is passed to hid_hw_raw_request().
366 if (copy_from_user(&report_number, buffer, 1)) {
371 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
375 len = (ret < count) ? ret : count;
377 if (copy_to_user(buffer, buf, len)) {
390 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
392 struct hidraw_list *list = file->private_data;
394 poll_wait(file, &list->hidraw->wait, wait);
395 if (list->head != list->tail)
396 return POLLIN | POLLRDNORM;
397 if (!list->hidraw->exist)
398 return POLLERR | POLLHUP;
403 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
405 unsigned int minor = iminor(inode);
407 struct hidraw_list *list;
411 list = kzalloc(sizeof(*list), GFP_KERNEL);
417 mutex_lock(&minors_lock);
418 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
423 dev = rkvr_hidraw_table[minor];
425 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
431 err = hid_hw_open(dev->hid);
434 hid_hw_power(dev->hid, PM_HINT_NORMAL);
440 list->hidraw = rkvr_hidraw_table[minor];
441 mutex_init(&list->read_mutex);
442 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
443 list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
444 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
445 file->private_data = list;
450 mutex_unlock(&minors_lock);
458 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
460 struct hidraw_list *list = file->private_data;
462 return fasync_helper(fd, file, on, &list->fasync);
465 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
467 if (exists_bit) { /*hw removed**/
470 hid_hw_close(hidraw->hid);
471 wake_up_interruptible(&hidraw->wait);
478 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
479 rkvr_hidraw_table[hidraw->minor] = NULL;
482 /* close device for last reader */
483 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
484 hid_hw_close(hidraw->hid);
489 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
491 unsigned int minor = iminor(inode);
492 struct hidraw_list *list = file->private_data;
495 mutex_lock(&minors_lock);
497 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
498 list_del(&list->node);
499 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
502 rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
504 mutex_unlock(&minors_lock);
509 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
515 input_report_key(input, key_value, 1);
518 input_report_key(input, key_value, 0);
523 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
525 struct input_dev *input = hdev->hiddev;
526 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
528 if (rkvr_data->rkvr_data.key_map.key_menu_up)
529 rkvr_send_key_event(input, KEY_MENU, 0);
530 else if (rkvr_data->rkvr_data.key_map.key_menu_down)
531 rkvr_send_key_event(input, KEY_MENU, 1);
532 else if (rkvr_data->rkvr_data.key_map.key_home_up)
533 rkvr_send_key_event(input, KEY_HOME, 0);
534 else if (rkvr_data->rkvr_data.key_map.key_home_down)
535 rkvr_send_key_event(input, KEY_HOME, 1);
536 else if (rkvr_data->rkvr_data.key_map.key_power_up)
537 rkvr_send_key_event(input, KEY_POWER, 0);
538 else if (rkvr_data->rkvr_data.key_map.key_power_down)
539 rkvr_send_key_event(input, KEY_POWER, 1);
540 else if (rkvr_data->rkvr_data.key_map.key_volup_up)
541 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
542 else if (rkvr_data->rkvr_data.key_map.key_volup_down)
543 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
544 else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
545 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
546 else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
547 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
548 else if (rkvr_data->rkvr_data.key_map.key_esc_up)
549 rkvr_send_key_event(input, KEY_ESC, 0);
550 else if (rkvr_data->rkvr_data.key_map.key_esc_down)
551 rkvr_send_key_event(input, KEY_ESC, 1);
552 else if (rkvr_data->rkvr_data.key_map.key_up_pressed) {
553 rkvr_send_key_event(input, KEY_UP, 1);
554 rkvr_send_key_event(input, KEY_UP, 0);
555 } else if (rkvr_data->rkvr_data.key_map.key_down_pressed) {
556 rkvr_send_key_event(input, KEY_DOWN, 1);
557 rkvr_send_key_event(input, KEY_DOWN, 0);
558 } else if (rkvr_data->rkvr_data.key_map.key_left_pressed) {
559 rkvr_send_key_event(input, KEY_LEFT, 1);
560 rkvr_send_key_event(input, KEY_LEFT, 0);
561 } else if (rkvr_data->rkvr_data.key_map.key_right_pressed) {
562 rkvr_send_key_event(input, KEY_RIGHT, 1);
563 rkvr_send_key_event(input, KEY_RIGHT, 0);
564 } else if (rkvr_data->rkvr_data.key_map.key_enter_pressed) {
565 rkvr_send_key_event(input, KEY_ENTER, 1);
566 rkvr_send_key_event(input, KEY_ENTER, 0);
569 if (rkvr_data->rkvr_data.key_map.psensor_on) {
570 hid_info(hdev, "event: psensor_on\n");
571 rkvr_send_key_event(input, KEY_POWER, 1);
572 rkvr_send_key_event(input, KEY_POWER, 0);
573 } else if (rkvr_data->rkvr_data.key_map.psensor_off) {
574 hid_info(hdev, "event: psensor_off\n");
575 rkvr_send_key_event(input, KEY_POWER, 1);
576 rkvr_send_key_event(input, KEY_POWER, 0);
582 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
584 struct hidraw *dev = hid->hidraw;
585 struct hidraw_list *list;
588 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
589 struct sensor_hid_data *pdata = hid_get_drvdata(hid);
591 spin_lock_irqsave(&dev->list_lock, flags);
593 rkvr_keys_event(hid, data, len);
595 if (pdata && pdata->priv && pdata->send_event) {
596 pdata->send_event(rkvr_data->buf, len, pdata->priv);
597 spin_unlock_irqrestore(&dev->list_lock, flags);
599 list_for_each_entry(list, &dev->list, node) {
600 int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
602 if (new_head == list->tail)
605 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
606 if (!list->buffer[list->head].value) {
608 spin_unlock_irqrestore(&dev->list_lock, flags);
612 list->buffer[list->head].len = len;
613 list->head = new_head;
614 kill_fasync(&list->fasync, SIGIO, POLL_IN);
616 spin_unlock_irqrestore(&dev->list_lock, flags);
617 wake_up_interruptible(&dev->wait);
622 /******************************************
623 *--------------------
625 *--------------------
627 ******************************************/
628 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
630 struct hid_device *hid = container_of(dev, struct hid_device, dev);
631 unsigned char reportnum = HID_REPORT_ID_RKVR;
632 unsigned char rtype = HID_OUTPUT_REPORT;
635 ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
637 hid_err(hid, "rkvr_send_report fail\n");
641 hid_info(hid, "rkvr_send_report ok\n");
647 static int rkvr_recv_report(struct device *dev, u8 type, u8 *data, int len)
649 struct hid_device *hid = container_of(dev, struct hid_device, dev);
650 unsigned char report_number = type;
651 unsigned char report_type = HID_MISC_REPORT;
652 char buf[1 + sizeof(*data) * len];
653 int readlen = 1 + sizeof(*data) * len;
656 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
657 if (ret != readlen) {
658 hid_info(hid, "rkvr_recv_report fail\n");
661 memcpy(data, &buf[1], len);
662 hid_info(hid, "rkvr_recv_report %02x\n", type);
668 * for enable sensor data
669 ************************************
671 * first 8 bytes :random digits
672 * left bytes :encryt data
673 * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
674 ***********************************
677 static int hid_report_sync(struct device *dev, const char *data, size_t count)
679 struct hid_device *hid = container_of(dev, struct hid_device, dev);
681 unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
682 unsigned char buf2[3] = {0};
688 p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
690 hid_err(hid, "no mem\n");
693 memcpy(p, data, count);
694 colon = strnchr(p, count, ':');
696 hid_err(hid, "must have conlon\n");
700 if (colon - p + 1 >= count) {
701 hid_err(hid, "must have sync string after conlon\n");
707 tmp = (u64 *)(buf + 2);
708 if (kstrtoull(p, 10, tmp)) {
709 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
713 hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
714 len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
715 for (i = 0; i < len; i++) {
716 buf2[0] = colon[i * 2];
717 buf2[1] = colon[i * 2 + 1];
718 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
719 hid_err(hid, "err sync string,only hex string allowed\n");
724 len = i + sizeof(*tmp) + 2;
725 ret = rkvr_send_report(dev, (unsigned char *)buf, len);
727 hid_err(hid, "hid_report_encrypt fail\n");
731 hid_info(hid, "hid_report_encrypt ok\n");
739 static int hid_get_capability(struct device *dev, struct hid_capability *caps)
741 struct hid_device *hid = container_of(dev, struct hid_device, dev);
744 caps->suspend_notify = 0;
745 if (!rkvr_recv_report(dev, RKVR_ID_CAPS, &data, 1)) {
746 hid_info(hid, "hid_get_capability %d\n", data);
747 caps->suspend_notify = data;
753 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
756 buf[0] = (1 << 7) | (len && 0x7f);
758 buf[0] = len && 0x7f;
760 memcpy(&buf[2], data, len);
765 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
767 struct hid_device *hid = container_of(dev, struct hid_device, dev);
768 unsigned char report_number = reg;
769 unsigned char report_type = HID_REGR_REPORT;
770 char buf[1 + sizeof(data) * len];
771 int readlen = 1 + sizeof(data) * len;
774 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
775 if (ret != readlen) {
776 hid_info(hid, "id_hw_raw_request fail\n");
778 memcpy(data, &buf[1], readlen);
779 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
785 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
787 struct hid_device *hid = container_of(dev, struct hid_device, dev);
788 unsigned char report_number = HID_REPORT_ID_W;
789 unsigned char report_type = HID_REGW_REPORT;
790 char buf[3 + sizeof(data)];
793 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
794 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
796 hid_info(hid, "id_hw_raw_request fail\n");
798 hid_info(hid, "id_hw_raw_request ok\n");
803 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
804 const char *buf, size_t count)
806 struct hidraw *devraw;
808 devraw = dev_get_drvdata(dev);
809 if (0 == strncmp(buf, "write", 5))
810 hid_report_writereg(&devraw->hid->dev, 0, 0);
811 hid_info(devraw->hid, "%s\n", buf);
816 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
821 struct hidraw *devraw;
823 devraw = dev_get_drvdata(dev);
824 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
825 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
827 count += sprintf(&buf[count], "read fail\n");
831 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
833 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
836 struct hidraw *devraw = dev_get_drvdata(dev);
839 ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
840 return ret > 0 ? count : ret;
843 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
846 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
848 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
849 unsigned char report_number = reg;
850 unsigned char report_type = HID_REGR_REPORT;
851 char buf[1 + sizeof(data) * len];
852 int readlen = 1 + sizeof(data) * len;
855 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
856 if (ret != readlen) {
857 hid_err(hid, "id_hw_raw_request fail\n");
859 memcpy(data, &buf[1], sizeof(data) * len);
865 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
867 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
868 unsigned char report_number = HID_REPORT_ID_W;
869 unsigned char report_type = HID_REGW_REPORT;
870 char buf[3 + sizeof(data)];
873 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
874 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
876 hid_info(hid, "id_hw_raw_request fail\n");
878 hid_info(hid, "id_hw_raw_request ok\n");
883 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
885 struct hid_device *hid;
888 hid = container_of(hdev->dev, struct hid_device, dev);
889 err = hid_hw_power(hid, PM_HINT_FULLON);
892 err = hid_hw_open(hid);
894 hid_hw_power(hid, PM_HINT_NORMAL);
901 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
903 struct hid_device *hid;
905 hid = container_of(hdev->dev, struct hid_device, dev);
906 hid_hw_power(hid, PM_HINT_NORMAL);
910 #if DYNAMIC_LOAD_MPU6500
911 static int register_mpu6500;
912 struct platform_device mpu6500_dev = {
917 static int rkvr_connect(struct hid_device *hid)
922 /* we accept any HID device, no matter the applications */
923 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
927 mutex_lock(&minors_lock);
928 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
929 if (rkvr_hidraw_table[minor])
931 rkvr_hidraw_table[minor] = dev;
936 mutex_unlock(&minors_lock);
941 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
942 NULL, "%s%d", "rkvr", minor);
944 if (IS_ERR(dev->dev)) {
945 rkvr_hidraw_table[minor] = NULL;
946 mutex_unlock(&minors_lock);
947 result = PTR_ERR(dev->dev);
952 dev_set_drvdata(dev->dev, dev);
954 device_create_file(dev->dev, &dev_attr_debug);
955 device_create_file(dev->dev, &dev_attr_sync);
959 struct rkvr_iio_hw_device *hw_device;
961 hw_device = inv_hid_alloc("hid-rkvr");
963 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
964 rkvr_hidraw_table[minor] = NULL;
965 mutex_unlock(&minors_lock);
966 result = PTR_ERR(dev->dev);
970 hw_device->dev = &hid->dev;
971 hw_device->open = rkvr_hid_open;
972 hw_device->close = rkvr_hid_close;
973 hw_device->read = rkvr_hid_read;
974 hw_device->write = rkvr_hid_write;
975 if (inv_hid_register_devcie(hw_device)) {
976 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
977 inv_hid_free(hw_device);
978 rkvr_hidraw_table[minor] = NULL;
979 mutex_unlock(&minors_lock);
980 result = PTR_ERR(dev->dev);
986 #if DYNAMIC_LOAD_MPU6500
987 if (!register_mpu6500) {
988 register_mpu6500 = 1;
989 hid_info(hid, "--->platform_device_register-->\n");
990 platform_device_register(&mpu6500_dev);
994 if (hid_hw_open(hid)) {
995 rkvr_hidraw_table[minor] = NULL;
996 mutex_unlock(&minors_lock);
997 result = PTR_ERR(dev->dev);
999 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
1003 init_waitqueue_head(&dev->wait);
1004 spin_lock_init(&dev->list_lock);
1005 INIT_LIST_HEAD(&dev->list);
1010 hid->hidraw = dev; /*struct hidraw * **/
1012 hid_get_capability(&hid->dev, &rkvr_hid_capability[minor]);
1014 mutex_unlock(&minors_lock);
1019 static int rkvr_keys_remove(struct hid_device *hdev)
1021 struct input_dev *input = hdev->hiddev;
1023 input_unregister_device(input);
1027 static unsigned int key_codes[] = {
1042 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
1045 struct device *dev = &hdev->dev;
1046 struct input_dev *input = NULL;
1049 input = devm_input_allocate_device(dev);
1051 hid_err(hdev, "input_allocate_device fail\n");
1054 input->name = "rkvr-keypad";
1055 input->phys = "rkvr-keys/input0";
1056 input->dev.parent = dev;
1057 input->id.bustype = BUS_HOST;
1058 input->id.vendor = 0x071b;
1059 input->id.product = 0x3205;
1060 input->id.version = 0x0001;
1062 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
1063 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
1064 input_set_capability(input, EV_KEY, key_codes[i]);
1067 error = input_register_device(input);
1069 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
1072 hdev->hiddev = input;
1077 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
1079 int ret = hdev->ll_driver->start(hdev);
1083 ret = rkvr_connect(hdev);
1085 hdev->ll_driver->stop(hdev);
1090 static void rkvr_disconnect(struct hid_device *hid)
1092 struct hidraw *hidraw = hid->hidraw;
1094 mutex_lock(&minors_lock);
1095 /* always unregistering inv_hid_device when hardware disconnect */
1096 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1098 device_remove_file(hidraw->dev, &dev_attr_debug);
1099 device_remove_file(hidraw->dev, &dev_attr_sync);
1102 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1103 rkvr_drop_ref(hidraw, 1);
1104 mutex_unlock(&minors_lock);
1107 static void rkvr_hw_stop(struct hid_device *hdev)
1109 rkvr_disconnect(hdev);
1110 hdev->ll_driver->stop(hdev);
1113 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1115 struct inode *inode = file_inode(file);
1116 unsigned int minor = iminor(inode);
1119 void __user *user_arg = (void __user *)arg;
1121 mutex_lock(&minors_lock);
1122 dev = rkvr_hidraw_table[minor];
1129 case HIDIOCGRDESCSIZE:
1130 if (put_user(dev->hid->rsize, (int __user *)arg))
1138 if (get_user(len, (int __user *)arg))
1140 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1142 else if (copy_to_user(user_arg + offsetof(
1143 struct hidraw_report_descriptor,
1146 min(dev->hid->rsize, len)))
1150 case HIDIOCGRAWINFO:
1152 struct hidraw_devinfo dinfo;
1154 dinfo.bustype = dev->hid->bus;
1155 dinfo.vendor = dev->hid->vendor;
1156 dinfo.product = dev->hid->product;
1157 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1163 struct hid_device *hid = dev->hid;
1165 if (_IOC_TYPE(cmd) != 'H') {
1170 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1171 int len = _IOC_SIZE(cmd);
1173 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1176 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1177 int len = _IOC_SIZE(cmd);
1179 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1183 if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1184 int len = _IOC_SIZE(cmd);
1187 buf = kzalloc(len + 1, GFP_KERNEL);
1192 if (copy_from_user(buf, user_arg, len)) {
1197 ret = hid_report_sync(&hid->dev, buf, len);
1202 /* Begin Read-only ioctls. */
1203 if (_IOC_DIR(cmd) != _IOC_READ) {
1208 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1209 int len = strlen(hid->name) + 1;
1211 if (len > _IOC_SIZE(cmd))
1212 len = _IOC_SIZE(cmd);
1213 ret = copy_to_user(user_arg, hid->name, len) ?
1218 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1219 int len = strlen(hid->phys) + 1;
1221 if (len > _IOC_SIZE(cmd))
1222 len = _IOC_SIZE(cmd);
1223 ret = copy_to_user(user_arg, hid->phys, len) ?
1232 mutex_unlock(&minors_lock);
1236 static const struct file_operations rkvr_ops = {
1237 .owner = THIS_MODULE,
1238 .read = rkvr_hidraw_read,
1239 .write = rkvr_hidraw_write,
1240 .poll = rkvr_hidraw_poll,
1241 .open = rkvr_hidraw_open,
1242 .release = rkvr_hidraw_release,
1243 .unlocked_ioctl = rkvr_hidraw_ioctl,
1244 #ifdef CONFIG_COMPAT
1245 .compat_ioctl = rkvr_hidraw_ioctl,
1247 .fasync = rkvr_hidraw_fasync,
1248 .llseek = noop_llseek,
1251 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1253 sensorData.priv = priv;
1254 sensorData.send_event = callback;
1258 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1260 static int rkvr_fb_event_notify(struct notifier_block *self,
1261 unsigned long action, void *data)
1264 unsigned char buf[3] = {HID_REPORT_ID_RKVR, RKVR_ID_IDLE, 0};
1265 struct hid_device *hid;
1266 struct fb_event *event = data;
1269 if (action != FB_EARLY_EVENT_BLANK && action != FB_EVENT_BLANK)
1272 blank_mode = *((int *)event->data);
1274 mutex_lock(&minors_lock);
1275 for (i = 0; i < RKVR_HIDRAW_MAX_DEVICES; i++) {
1276 if (!rkvr_hidraw_table[i] || !rkvr_hidraw_table[i]->exist)
1278 if (!rkvr_hid_capability[i].suspend_notify) {
1281 hid = rkvr_hidraw_table[i]->hid;
1282 if (action == FB_EARLY_EVENT_BLANK) {
1283 switch (blank_mode) {
1284 case FB_BLANK_UNBLANK:
1287 rkvr_send_report(&hid->dev, buf, 3);
1290 } else if (action == FB_EVENT_BLANK) {
1291 switch (blank_mode) {
1292 case FB_BLANK_UNBLANK:
1294 rkvr_send_report(&hid->dev, buf, 3);
1301 mutex_unlock(&minors_lock);
1305 static struct notifier_block rkvr_fb_notifier = {
1306 .notifier_call = rkvr_fb_event_notify,
1309 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1312 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1314 retval = hid_parse(hdev);
1316 hid_err(hdev, "rkvr - parse failed\n");
1319 hid_set_drvdata(hdev, &sensorData);
1320 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1321 retval = rkvr_keys_probe(hdev);
1323 hid_err(hdev, "rkvr_keys_probe failed\n");
1326 retval = rkvr_hw_start(hdev, 0);
1328 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1329 rkvr_keys_remove(hdev);
1333 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1335 hid_err(hdev, "rkvr - hid hw start failed\n");
1348 static void rkvr_remove(struct hid_device *hdev)
1350 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1352 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1354 rkvr_keys_remove(hdev);
1360 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1363 static unsigned int count;
1364 static unsigned long old_jiffy;
1366 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1368 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1369 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1370 intf->cur_altsetting->desc.bInterfaceNumber);
1374 /* print sensor poll frequency */
1375 if (++count >= 1000) {
1376 unsigned long cur_jiffy = jiffies;
1378 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1380 old_jiffy = cur_jiffy;
1383 if (hdev->hidraw || hdev->hiddev) {
1384 retval = rkvr_report_event(hdev, data, size);
1386 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1392 static const struct hid_device_id rkvr_devices[] = {
1393 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1397 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1399 static struct hid_driver rkvr_driver = {
1401 .id_table = rkvr_devices,
1402 .probe = rkvr_probe,
1403 .remove = rkvr_remove,
1404 .raw_event = rkvr_raw_event
1407 static int __init rkvr_init(void)
1412 rkvr_class = class_create(THIS_MODULE, "rkvr");
1413 if (IS_ERR(rkvr_class))
1414 return PTR_ERR(rkvr_class);
1416 retval = hid_register_driver(&rkvr_driver);
1418 pr_warn("rkvr_init - Can't register drive.\n");
1422 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1423 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1425 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1429 rkvr_major = MAJOR(dev_id);
1430 cdev_init(&rkvr_cdev, &rkvr_ops);
1431 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1433 retval = fb_register_client(&rkvr_fb_notifier);
1435 pr_warn("rkvr_init - Can't register fb notifier\n");
1440 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1442 hid_unregister_driver(&rkvr_driver);
1444 class_destroy(rkvr_class);
1449 static void __exit rkvr_exit(void)
1451 dev_t dev_id = MKDEV(rkvr_major, 0);
1453 fb_unregister_client(&rkvr_fb_notifier);
1454 cdev_del(&rkvr_cdev);
1456 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1458 hid_unregister_driver(&rkvr_driver);
1459 class_destroy(rkvr_class);
1462 module_init(rkvr_init);
1463 module_exit(rkvr_exit);
1465 MODULE_AUTHOR("zwp");
1466 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1467 MODULE_LICENSE("GPL v2");