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;
60 __u8 buf_sensortemperature[2];
63 struct keymap_t key_map;
68 static int rkvr_major;
69 static struct cdev rkvr_cdev;
70 static unsigned int count_array[15] = {0,};
71 static unsigned long old_jiffy_array[15] = {0,};
72 static int rkvr_index;
74 static char sync_string[64];
76 struct sensor_hid_data {
78 int (*send_event)(char *raw_data, size_t raw_len, void *priv);
81 static DEFINE_MUTEX(device_list_lock);
82 static struct list_head rkvr_hid_hw_device_list = {
83 .next = &rkvr_hid_hw_device_list,
84 .prev = &rkvr_hid_hw_device_list
87 static struct rkvr_iio_hw_device *inv_hid_alloc(const char *name)
89 struct rkvr_iio_hw_device *p;
94 s = kstrdup_const(name, GFP_KERNEL);
97 p = kzalloc(sizeof(*p), GFP_KERNEL);
103 pr_err("%s error!\n", __func__);
109 static void inv_hid_free(struct rkvr_iio_hw_device *hw_device)
111 kfree_const(hw_device->name);
115 static int inv_hid_register_devcie(struct rkvr_iio_hw_device *hw_device)
118 mutex_lock(&device_list_lock);
119 if (hw_device->name && (!list_empty(&rkvr_hid_hw_device_list))) {
120 struct rkvr_iio_hw_device *p;
122 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
123 if (!strcmp(hw_device->name, p->name)) {
124 pr_err("%s already exist ,abort\n", hw_device->name);
129 list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
130 mutex_unlock(&device_list_lock);
134 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
136 struct rkvr_iio_hw_device *p = NULL;
138 mutex_lock(&device_list_lock);
139 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
140 if (!strcmp(name, p->name)) {
146 pr_info("find dev with name %s,free now\n", name);
149 mutex_unlock(&device_list_lock);
152 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
154 struct hidraw_list *list = file->private_data;
156 DECLARE_WAITQUEUE(wait, current);
158 mutex_lock(&list->read_mutex);
160 if (list->head == list->tail) {
161 add_wait_queue(&list->hidraw->wait, &wait);
162 set_current_state(TASK_INTERRUPTIBLE);
164 while (list->head == list->tail) {
165 if (signal_pending(current)) {
169 if (!list->hidraw->exist) {
173 if (file->f_flags & O_NONBLOCK) {
178 /* allow O_NONBLOCK to work well from other threads */
179 mutex_unlock(&list->read_mutex);
181 mutex_lock(&list->read_mutex);
182 set_current_state(TASK_INTERRUPTIBLE);
185 set_current_state(TASK_RUNNING);
186 remove_wait_queue(&list->hidraw->wait, &wait);
192 len = list->buffer[list->tail].len > count ?
193 count : list->buffer[list->tail].len;
195 if (list->buffer[list->tail].value) {
196 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
201 if (opens > 0 && rkvr_index < 15) {
202 if (++count_array[rkvr_index] >= 1000) {
203 unsigned long cur_jiffy = jiffies;
205 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);
206 count_array[rkvr_index] = 0;
207 old_jiffy_array[rkvr_index] = cur_jiffy;
209 if (++rkvr_index >= opens)
216 kfree(list->buffer[list->tail].value);
217 list->buffer[list->tail].value = NULL;
218 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
221 mutex_unlock(&list->read_mutex);
225 /* The first byte is expected to be a report number.
226 * This function is to be called with the minors_lock mutex held
228 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
230 unsigned int minor = iminor(file_inode(file));
231 struct hid_device *dev;
235 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
240 dev = rkvr_hidraw_table[minor]->hid;
242 if (count > HID_MAX_BUFFER_SIZE) {
243 hid_warn(dev, "rkvr - pid %d passed too large report\n",
244 task_pid_nr(current));
250 hid_warn(dev, "rkvr - pid %d passed too short report\n",
251 task_pid_nr(current));
256 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
262 if (copy_from_user(buf, buffer, count)) {
267 if ((report_type == HID_OUTPUT_REPORT) &&
268 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
269 ret = hid_hw_output_report(dev, buf, count);
271 * compatibility with old implementation of USB-HID and I2C-HID:
272 * if the device does not support receiving output reports,
273 * on an interrupt endpoint, fallback to SET_REPORT HID command.
279 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
288 /* the first byte is expected to be a report number */
289 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
293 mutex_lock(&minors_lock);
294 ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
295 mutex_unlock(&minors_lock);
299 /* This function performs a Get_Report transfer over the control endpoint
300 * per section 7.2.1 of the HID specification, version 1.1. The first byte
301 * of buffer is the report number to request, or 0x0 if the defice does not
302 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
303 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
306 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
308 unsigned int minor = iminor(file_inode(file));
309 struct hid_device *dev;
312 unsigned char report_number;
314 dev = rkvr_hidraw_table[minor]->hid;
316 if (!dev->ll_driver->raw_request) {
321 if (count > HID_MAX_BUFFER_SIZE) {
322 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
323 task_pid_nr(current));
329 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
330 task_pid_nr(current));
335 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
342 * Read the first byte from the user. This is the report number,
343 * which is passed to hid_hw_raw_request().
345 if (copy_from_user(&report_number, buffer, 1)) {
350 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
354 len = (ret < count) ? ret : count;
356 if (copy_to_user(buffer, buf, len)) {
369 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
371 struct hidraw_list *list = file->private_data;
373 poll_wait(file, &list->hidraw->wait, wait);
374 if (list->head != list->tail)
375 return POLLIN | POLLRDNORM;
376 if (!list->hidraw->exist)
377 return POLLERR | POLLHUP;
382 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
384 unsigned int minor = iminor(inode);
386 struct hidraw_list *list;
390 list = kzalloc(sizeof(*list), GFP_KERNEL);
396 mutex_lock(&minors_lock);
397 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
402 dev = rkvr_hidraw_table[minor];
404 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
410 err = hid_hw_open(dev->hid);
413 hid_hw_power(dev->hid, PM_HINT_NORMAL);
419 list->hidraw = rkvr_hidraw_table[minor];
420 mutex_init(&list->read_mutex);
421 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
422 list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
423 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
424 file->private_data = list;
429 mutex_unlock(&minors_lock);
437 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
439 struct hidraw_list *list = file->private_data;
441 return fasync_helper(fd, file, on, &list->fasync);
444 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
446 if (exists_bit) { /*hw removed**/
449 hid_hw_close(hidraw->hid);
450 wake_up_interruptible(&hidraw->wait);
457 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
458 rkvr_hidraw_table[hidraw->minor] = NULL;
461 /* close device for last reader */
462 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
463 hid_hw_close(hidraw->hid);
468 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
470 unsigned int minor = iminor(inode);
471 struct hidraw_list *list = file->private_data;
474 mutex_lock(&minors_lock);
476 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
477 list_del(&list->node);
478 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
481 rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
483 mutex_unlock(&minors_lock);
488 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
494 input_report_key(input, key_value, 1);
497 input_report_key(input, key_value, 0);
502 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
504 struct input_dev *input = hdev->hiddev;
505 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
507 if (rkvr_data->rkvr_data.key_map.key_menu_up)
508 rkvr_send_key_event(input, KEY_MENU, 0);
509 else if (rkvr_data->rkvr_data.key_map.key_menu_down)
510 rkvr_send_key_event(input, KEY_MENU, 1);
511 else if (rkvr_data->rkvr_data.key_map.key_home_up)
512 rkvr_send_key_event(input, KEY_HOME, 0);
513 else if (rkvr_data->rkvr_data.key_map.key_home_down)
514 rkvr_send_key_event(input, KEY_HOME, 1);
515 else if (rkvr_data->rkvr_data.key_map.key_power_up)
516 rkvr_send_key_event(input, KEY_POWER, 0);
517 else if (rkvr_data->rkvr_data.key_map.key_power_down)
518 rkvr_send_key_event(input, KEY_POWER, 1);
519 else if (rkvr_data->rkvr_data.key_map.key_volup_up)
520 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
521 else if (rkvr_data->rkvr_data.key_map.key_volup_down)
522 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
523 else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
524 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
525 else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
526 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
530 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
532 struct hidraw *dev = hid->hidraw;
533 struct hidraw_list *list;
536 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
537 struct sensor_hid_data *pdata = hid_get_drvdata(hid);
539 spin_lock_irqsave(&dev->list_lock, flags);
541 rkvr_keys_event(hid, data, len);
543 if (pdata && pdata->priv && pdata->send_event) {
544 pdata->send_event(rkvr_data->buf, len, pdata->priv);
545 spin_unlock_irqrestore(&dev->list_lock, flags);
547 list_for_each_entry(list, &dev->list, node) {
548 int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
550 if (new_head == list->tail)
553 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
554 if (!list->buffer[list->head].value) {
556 spin_unlock_irqrestore(&dev->list_lock, flags);
560 list->buffer[list->head].len = len;
561 list->head = new_head;
562 kill_fasync(&list->fasync, SIGIO, POLL_IN);
564 spin_unlock_irqrestore(&dev->list_lock, flags);
565 wake_up_interruptible(&dev->wait);
571 * for enable sensor data
572 ************************************
574 * first 8 bytes :random digits
575 * left bytes :encryt data
576 * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
577 ***********************************
580 static int hid_report_sync(struct device *dev, const char *data, size_t len)
582 struct hid_device *hid = container_of(dev, struct hid_device, dev);
584 unsigned char buf[64];
585 unsigned char buf2[3] = {0};
589 unsigned char report_number = HID_REPORT_ID_CRYP;
590 unsigned char report_type = HID_SYNCW_REPORT;
592 p = kmalloc(sizeof(*p) * len, GFP_KERNEL);
594 hid_err(hid, "no mem\n");
597 memcpy(p, data, len);
598 colon = strchr(p, ':');
600 hid_err(hid, "must have conlon\n");
604 if (colon - p + 1 >= len) {
605 hid_err(hid, "must have sync string after conlon\n");
611 tmp = (u64 *)(buf + 1);
612 if (kstrtoull(p, 10, tmp)) {
613 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
617 len = min((len - (colon - p)) / 2, sizeof(buf) - 9);
618 for (i = 0; i < len; i++) {
619 buf2[0] = colon[i * 2];
620 buf2[1] = colon[i * 2 + 1];
621 if (kstrtou8(buf2, 16, &buf[9 + i])) {
622 hid_err(hid, "err sync string,only hex string allowed\n");
629 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, len,
630 report_type, HID_REQ_SET_REPORT);
632 hid_err(hid, "hid_report_encrypt fail\n");
636 hid_info(hid, "hid_report_encrypt ok\n");
644 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
647 buf[0] = (1 << 7) | (len && 0x7f);
649 buf[0] = len && 0x7f;
651 memcpy(&buf[2], data, len);
656 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
658 struct hid_device *hid = container_of(dev, struct hid_device, dev);
659 unsigned char report_number = reg;
660 unsigned char report_type = HID_REGR_REPORT;
661 char buf[1 + sizeof(data) * len];
662 int readlen = 1 + sizeof(data) * len;
665 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
666 if (ret != readlen) {
667 hid_info(hid, "id_hw_raw_request fail\n");
669 memcpy(data, &buf[1], readlen);
670 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
676 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
678 struct hid_device *hid = container_of(dev, struct hid_device, dev);
679 unsigned char report_number = HID_REPORT_ID_W;
680 unsigned char report_type = HID_REGW_REPORT;
681 char buf[3 + sizeof(data)];
684 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
685 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
687 hid_info(hid, "id_hw_raw_request fail\n");
689 hid_info(hid, "id_hw_raw_request ok\n");
694 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
697 struct hidraw *devraw;
699 devraw = dev_get_drvdata(dev);
700 if (0 == strncmp(buf, "write", 5))
701 hid_report_writereg(&devraw->hid->dev, 0, 0);
702 hid_info(devraw->hid, "%s\n", buf);
707 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
712 struct hidraw *devraw;
714 devraw = dev_get_drvdata(dev);
715 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
716 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
718 count += sprintf(&buf[count], "read fail\n");
722 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
724 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
725 const char *buf, size_t count)
727 struct hidraw *devraw = dev_get_drvdata(dev);
729 return hid_report_sync(&devraw->hid->dev, buf, count);
732 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
735 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
737 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
738 unsigned char report_number = reg;
739 unsigned char report_type = HID_REGR_REPORT;
740 char buf[1 + sizeof(data) * len];
741 int readlen = 1 + sizeof(data) * len;
744 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
745 if (ret != readlen) {
746 hid_err(hid, "id_hw_raw_request fail\n");
748 memcpy(data, &buf[1], sizeof(data) * len);
754 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
756 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
757 unsigned char report_number = HID_REPORT_ID_W;
758 unsigned char report_type = HID_REGW_REPORT;
759 char buf[3 + sizeof(data)];
762 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
763 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
765 hid_info(hid, "id_hw_raw_request fail\n");
767 hid_info(hid, "id_hw_raw_request ok\n");
772 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
774 struct hid_device *hid;
777 hid = container_of(hdev->dev, struct hid_device, dev);
778 err = hid_hw_power(hid, PM_HINT_FULLON);
781 err = hid_hw_open(hid);
783 hid_hw_power(hid, PM_HINT_NORMAL);
790 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
792 struct hid_device *hid;
794 hid = container_of(hdev->dev, struct hid_device, dev);
795 hid_hw_power(hid, PM_HINT_NORMAL);
799 #if DYNAMIC_LOAD_MPU6500
800 static int register_mpu6500;
801 struct platform_device mpu6500_dev = {
806 static int rkvr_connect(struct hid_device *hid)
811 /* we accept any HID device, no matter the applications */
812 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
816 mutex_lock(&minors_lock);
817 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
818 if (rkvr_hidraw_table[minor])
820 rkvr_hidraw_table[minor] = dev;
825 mutex_unlock(&minors_lock);
830 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
831 NULL, "%s%d", "rkvr", minor);
833 if (IS_ERR(dev->dev)) {
834 rkvr_hidraw_table[minor] = NULL;
835 mutex_unlock(&minors_lock);
836 result = PTR_ERR(dev->dev);
841 dev_set_drvdata(dev->dev, dev);
843 device_create_file(dev->dev, &dev_attr_debug);
844 device_create_file(dev->dev, &dev_attr_sync);
848 struct rkvr_iio_hw_device *hw_device;
850 hw_device = inv_hid_alloc("hid-rkvr");
852 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
853 rkvr_hidraw_table[minor] = NULL;
854 mutex_unlock(&minors_lock);
855 result = PTR_ERR(dev->dev);
859 hw_device->dev = &hid->dev;
860 hw_device->open = rkvr_hid_open;
861 hw_device->close = rkvr_hid_close;
862 hw_device->read = rkvr_hid_read;
863 hw_device->write = rkvr_hid_write;
864 if (inv_hid_register_devcie(hw_device)) {
865 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
866 inv_hid_free(hw_device);
867 rkvr_hidraw_table[minor] = NULL;
868 mutex_unlock(&minors_lock);
869 result = PTR_ERR(dev->dev);
875 #if DYNAMIC_LOAD_MPU6500
876 if (!register_mpu6500) {
877 register_mpu6500 = 1;
878 hid_info(hid, "--->platform_device_register-->\n");
879 platform_device_register(&mpu6500_dev);
883 if (hid_hw_open(hid)) {
884 rkvr_hidraw_table[minor] = NULL;
885 mutex_unlock(&minors_lock);
886 result = PTR_ERR(dev->dev);
888 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
891 if (strlen(sync_string))
892 hid_report_sync(&hid->dev, sync_string, strlen(sync_string));
894 init_waitqueue_head(&dev->wait);
895 spin_lock_init(&dev->list_lock);
896 INIT_LIST_HEAD(&dev->list);
901 hid->hidraw = dev; /*struct hidraw * **/
902 mutex_unlock(&minors_lock);
907 static int rkvr_keys_remove(struct hid_device *hdev)
909 struct input_dev *input = hdev->hiddev;
911 input_unregister_device(input);
915 static unsigned int key_codes[] = {
924 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
927 struct device *dev = &hdev->dev;
928 struct input_dev *input = NULL;
931 input = devm_input_allocate_device(dev);
933 hid_err(hdev, "input_allocate_device fail\n");
936 input->name = "rkvr-keypad";
937 input->phys = "rkvr-keys/input0";
938 input->dev.parent = dev;
939 input->id.bustype = BUS_HOST;
940 input->id.vendor = 0x071b;
941 input->id.product = 0x3205;
942 input->id.version = 0x0001;
944 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
945 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
946 input_set_capability(input, EV_KEY, key_codes[i]);
949 error = input_register_device(input);
951 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
954 hdev->hiddev = input;
959 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
961 int ret = hdev->ll_driver->start(hdev);
965 ret = rkvr_connect(hdev);
967 hdev->ll_driver->stop(hdev);
972 static void rkvr_disconnect(struct hid_device *hid)
974 struct hidraw *hidraw = hid->hidraw;
976 mutex_lock(&minors_lock);
977 /* always unregistering inv_hid_device when hardware disconnect */
978 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
980 device_remove_file(hidraw->dev, &dev_attr_debug);
981 device_remove_file(hidraw->dev, &dev_attr_sync);
984 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
985 rkvr_drop_ref(hidraw, 1);
986 mutex_unlock(&minors_lock);
989 static void rkvr_hw_stop(struct hid_device *hdev)
991 rkvr_disconnect(hdev);
992 hdev->ll_driver->stop(hdev);
995 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
997 struct inode *inode = file_inode(file);
998 unsigned int minor = iminor(inode);
1001 void __user *user_arg = (void __user *)arg;
1003 mutex_lock(&minors_lock);
1004 dev = rkvr_hidraw_table[minor];
1011 case HIDIOCGRDESCSIZE:
1012 if (put_user(dev->hid->rsize, (int __user *)arg))
1020 if (get_user(len, (int __user *)arg))
1022 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1024 else if (copy_to_user(user_arg + offsetof(
1025 struct hidraw_report_descriptor,
1028 min(dev->hid->rsize, len)))
1032 case HIDIOCGRAWINFO:
1034 struct hidraw_devinfo dinfo;
1036 dinfo.bustype = dev->hid->bus;
1037 dinfo.vendor = dev->hid->vendor;
1038 dinfo.product = dev->hid->product;
1039 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1045 struct hid_device *hid = dev->hid;
1047 if (_IOC_TYPE(cmd) != 'H') {
1052 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1053 int len = _IOC_SIZE(cmd);
1055 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1058 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1059 int len = _IOC_SIZE(cmd);
1061 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1065 /* Begin Read-only ioctls. */
1066 if (_IOC_DIR(cmd) != _IOC_READ) {
1071 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1072 int len = strlen(hid->name) + 1;
1074 if (len > _IOC_SIZE(cmd))
1075 len = _IOC_SIZE(cmd);
1076 ret = copy_to_user(user_arg, hid->name, len) ?
1081 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1082 int len = strlen(hid->phys) + 1;
1084 if (len > _IOC_SIZE(cmd))
1085 len = _IOC_SIZE(cmd);
1086 ret = copy_to_user(user_arg, hid->phys, len) ?
1095 mutex_unlock(&minors_lock);
1099 static const struct file_operations rkvr_ops = {
1100 .owner = THIS_MODULE,
1101 .read = rkvr_hidraw_read,
1102 .write = rkvr_hidraw_write,
1103 .poll = rkvr_hidraw_poll,
1104 .open = rkvr_hidraw_open,
1105 .release = rkvr_hidraw_release,
1106 .unlocked_ioctl = rkvr_hidraw_ioctl,
1107 #ifdef CONFIG_COMPAT
1108 .compat_ioctl = rkvr_hidraw_ioctl,
1110 .fasync = rkvr_hidraw_fasync,
1111 .llseek = noop_llseek,
1114 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1116 sensorData.priv = priv;
1117 sensorData.send_event = callback;
1121 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1123 static int rkvr_sync_exec(const char *p, size_t c)
1129 mutex_lock(&minors_lock);
1130 pr_info("rkvr_sync_exec %s\n", p);
1131 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
1132 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist)
1134 if (hid_report_sync(&rkvr_hidraw_table[minor]->hid->dev, p, c)) {
1135 hid_err(rkvr_hidraw_table[minor]->hid, "hid_report_sync failed\n");
1144 mutex_unlock(&minors_lock);
1149 int rkvr_sensor_sync_inv(const char *p, size_t c)
1151 snprintf(sync_string, sizeof(sync_string), "%s", p);
1153 return rkvr_sync_exec(sync_string, strlen(sync_string));
1156 EXPORT_SYMBOL_GPL(rkvr_sensor_sync_inv);
1158 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1161 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1163 retval = hid_parse(hdev);
1165 hid_err(hdev, "rkvr - parse failed\n");
1168 hid_set_drvdata(hdev, &sensorData);
1169 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1170 retval = rkvr_keys_probe(hdev);
1172 hid_err(hdev, "rkvr_keys_probe failed\n");
1175 retval = rkvr_hw_start(hdev, 0);
1177 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1178 rkvr_keys_remove(hdev);
1182 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1184 hid_err(hdev, "rkvr - hid hw start failed\n");
1197 static void rkvr_remove(struct hid_device *hdev)
1199 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1201 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1202 rkvr_keys_remove(hdev);
1209 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1212 static unsigned int count;
1213 static unsigned long old_jiffy;
1215 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1217 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1218 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1219 intf->cur_altsetting->desc.bInterfaceNumber);
1223 /* print sensor poll frequency */
1224 if (++count >= 1000) {
1225 unsigned long cur_jiffy = jiffies;
1227 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1229 old_jiffy = cur_jiffy;
1232 if (hdev->hidraw || hdev->hiddev) {
1233 retval = rkvr_report_event(hdev, data, size);
1235 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1241 static const struct hid_device_id rkvr_devices[] = {
1242 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1246 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1248 static struct hid_driver rkvr_driver = {
1250 .id_table = rkvr_devices,
1251 .probe = rkvr_probe,
1252 .remove = rkvr_remove,
1253 .raw_event = rkvr_raw_event
1256 static int __init rkvr_init(void)
1261 rkvr_class = class_create(THIS_MODULE, "rkvr");
1262 if (IS_ERR(rkvr_class))
1263 return PTR_ERR(rkvr_class);
1265 retval = hid_register_driver(&rkvr_driver);
1267 pr_warn("rkvr_init - Can't register drive.\n");
1271 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1272 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1274 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1278 rkvr_major = MAJOR(dev_id);
1279 cdev_init(&rkvr_cdev, &rkvr_ops);
1280 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1284 hid_unregister_driver(&rkvr_driver);
1286 class_destroy(rkvr_class);
1291 static void __exit rkvr_exit(void)
1293 dev_t dev_id = MKDEV(rkvr_major, 0);
1295 cdev_del(&rkvr_cdev);
1297 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1299 hid_unregister_driver(&rkvr_driver);
1300 class_destroy(rkvr_class);
1303 module_init(rkvr_init);
1304 module_exit(rkvr_exit);
1306 MODULE_AUTHOR("zwp");
1307 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1308 MODULE_LICENSE("GPL v2");