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;
75 struct sensor_hid_data {
77 int (*send_event)(char *raw_data, size_t raw_len, void *priv);
80 static DEFINE_MUTEX(device_list_lock);
81 static struct list_head rkvr_hid_hw_device_list = {
82 .next = &rkvr_hid_hw_device_list,
83 .prev = &rkvr_hid_hw_device_list
86 static struct rkvr_iio_hw_device *inv_hid_alloc(const char *name)
88 struct rkvr_iio_hw_device *p;
93 s = kstrdup_const(name, GFP_KERNEL);
96 p = kzalloc(sizeof(*p), GFP_KERNEL);
102 pr_err("%s error!\n", __func__);
108 static void inv_hid_free(struct rkvr_iio_hw_device *hw_device)
110 kfree_const(hw_device->name);
114 static int inv_hid_register_devcie(struct rkvr_iio_hw_device *hw_device)
117 mutex_lock(&device_list_lock);
118 if (hw_device->name && (!list_empty(&rkvr_hid_hw_device_list))) {
119 struct rkvr_iio_hw_device *p;
121 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
122 if (!strcmp(hw_device->name, p->name)) {
123 pr_err("%s already exist ,abort\n", hw_device->name);
128 list_add_tail(&hw_device->l, &rkvr_hid_hw_device_list);
129 mutex_unlock(&device_list_lock);
133 static void inv_hid_unregister_and_destroy_devcie_by_name(const char *name)
135 struct rkvr_iio_hw_device *p = NULL;
137 mutex_lock(&device_list_lock);
138 list_for_each_entry(p, &rkvr_hid_hw_device_list, l) {
139 if (!strcmp(name, p->name)) {
145 pr_info("find dev with name %s,free now\n", name);
148 mutex_unlock(&device_list_lock);
151 static ssize_t rkvr_hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
153 struct hidraw_list *list = file->private_data;
155 DECLARE_WAITQUEUE(wait, current);
157 mutex_lock(&list->read_mutex);
159 if (list->head == list->tail) {
160 add_wait_queue(&list->hidraw->wait, &wait);
161 set_current_state(TASK_INTERRUPTIBLE);
163 while (list->head == list->tail) {
164 if (signal_pending(current)) {
168 if (!list->hidraw->exist) {
172 if (file->f_flags & O_NONBLOCK) {
177 /* allow O_NONBLOCK to work well from other threads */
178 mutex_unlock(&list->read_mutex);
180 mutex_lock(&list->read_mutex);
181 set_current_state(TASK_INTERRUPTIBLE);
184 set_current_state(TASK_RUNNING);
185 remove_wait_queue(&list->hidraw->wait, &wait);
191 len = list->buffer[list->tail].len > count ?
192 count : list->buffer[list->tail].len;
194 if (list->buffer[list->tail].value) {
195 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
200 if (opens > 0 && rkvr_index < 15) {
201 if (++count_array[rkvr_index] >= 1000) {
202 unsigned long cur_jiffy = jiffies;
204 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);
205 count_array[rkvr_index] = 0;
206 old_jiffy_array[rkvr_index] = cur_jiffy;
208 if (++rkvr_index >= opens)
215 kfree(list->buffer[list->tail].value);
216 list->buffer[list->tail].value = NULL;
217 list->tail = (list->tail + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
220 mutex_unlock(&list->read_mutex);
224 /* The first byte is expected to be a report number.
225 * This function is to be called with the minors_lock mutex held
227 static ssize_t rkvr_hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
229 unsigned int minor = iminor(file_inode(file));
230 struct hid_device *dev;
234 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
239 dev = rkvr_hidraw_table[minor]->hid;
241 if (count > HID_MAX_BUFFER_SIZE) {
242 hid_warn(dev, "rkvr - pid %d passed too large report\n",
243 task_pid_nr(current));
249 hid_warn(dev, "rkvr - pid %d passed too short report\n",
250 task_pid_nr(current));
255 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
261 if (copy_from_user(buf, buffer, count)) {
266 if ((report_type == HID_OUTPUT_REPORT) &&
267 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
268 ret = hid_hw_output_report(dev, buf, count);
270 * compatibility with old implementation of USB-HID and I2C-HID:
271 * if the device does not support receiving output reports,
272 * on an interrupt endpoint, fallback to SET_REPORT HID command.
278 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
287 /* the first byte is expected to be a report number */
288 static ssize_t rkvr_hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
292 mutex_lock(&minors_lock);
293 ret = rkvr_hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
294 mutex_unlock(&minors_lock);
298 /* This function performs a Get_Report transfer over the control endpoint
299 * per section 7.2.1 of the HID specification, version 1.1. The first byte
300 * of buffer is the report number to request, or 0x0 if the defice does not
301 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
302 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
305 static ssize_t rkvr_hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
307 unsigned int minor = iminor(file_inode(file));
308 struct hid_device *dev;
311 unsigned char report_number;
313 dev = rkvr_hidraw_table[minor]->hid;
315 if (!dev->ll_driver->raw_request) {
320 if (count > HID_MAX_BUFFER_SIZE) {
321 hid_warn(dev, "rkvr - hidraw: pid %d passed too large report\n",
322 task_pid_nr(current));
328 hid_warn(dev, "rkvr - hidraw: pid %d passed too short report\n",
329 task_pid_nr(current));
334 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
341 * Read the first byte from the user. This is the report number,
342 * which is passed to hid_hw_raw_request().
344 if (copy_from_user(&report_number, buffer, 1)) {
349 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
353 len = (ret < count) ? ret : count;
355 if (copy_to_user(buffer, buf, len)) {
368 static unsigned int rkvr_hidraw_poll(struct file *file, poll_table *wait)
370 struct hidraw_list *list = file->private_data;
372 poll_wait(file, &list->hidraw->wait, wait);
373 if (list->head != list->tail)
374 return POLLIN | POLLRDNORM;
375 if (!list->hidraw->exist)
376 return POLLERR | POLLHUP;
381 static int rkvr_hidraw_open(struct inode *inode, struct file *file)
383 unsigned int minor = iminor(inode);
385 struct hidraw_list *list;
389 list = kzalloc(sizeof(*list), GFP_KERNEL);
395 mutex_lock(&minors_lock);
396 if (!rkvr_hidraw_table[minor] || !rkvr_hidraw_table[minor]->exist) {
401 dev = rkvr_hidraw_table[minor];
403 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
409 err = hid_hw_open(dev->hid);
412 hid_hw_power(dev->hid, PM_HINT_NORMAL);
418 list->hidraw = rkvr_hidraw_table[minor];
419 mutex_init(&list->read_mutex);
420 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
421 list_add_tail(&list->node, &rkvr_hidraw_table[minor]->list);
422 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
423 file->private_data = list;
428 mutex_unlock(&minors_lock);
436 static int rkvr_hidraw_fasync(int fd, struct file *file, int on)
438 struct hidraw_list *list = file->private_data;
440 return fasync_helper(fd, file, on, &list->fasync);
443 static void rkvr_drop_ref(struct hidraw *hidraw, int exists_bit)
445 if (exists_bit) { /*hw removed**/
448 hid_hw_close(hidraw->hid);
449 wake_up_interruptible(&hidraw->wait);
456 if (!hidraw->exist) { /*no opened && no hardware,delete all**/
457 rkvr_hidraw_table[hidraw->minor] = NULL;
460 /* close device for last reader */
461 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
462 hid_hw_close(hidraw->hid);
467 static int rkvr_hidraw_release(struct inode *inode, struct file *file)
469 unsigned int minor = iminor(inode);
470 struct hidraw_list *list = file->private_data;
473 mutex_lock(&minors_lock);
475 spin_lock_irqsave(&rkvr_hidraw_table[minor]->list_lock, flags);
476 list_del(&list->node);
477 spin_unlock_irqrestore(&rkvr_hidraw_table[minor]->list_lock, flags);
480 rkvr_drop_ref(rkvr_hidraw_table[minor], 0);
482 mutex_unlock(&minors_lock);
487 static void rkvr_send_key_event(struct input_dev *input, int key_value, int state)
493 input_report_key(input, key_value, 1);
496 input_report_key(input, key_value, 0);
501 static int rkvr_keys_event(struct hid_device *hdev, void *data, unsigned long len)
503 struct input_dev *input = hdev->hiddev;
504 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
506 if (rkvr_data->rkvr_data.key_map.key_menu_up)
507 rkvr_send_key_event(input, KEY_MENU, 0);
508 else if (rkvr_data->rkvr_data.key_map.key_menu_down)
509 rkvr_send_key_event(input, KEY_MENU, 1);
510 else if (rkvr_data->rkvr_data.key_map.key_home_up)
511 rkvr_send_key_event(input, KEY_HOME, 0);
512 else if (rkvr_data->rkvr_data.key_map.key_home_down)
513 rkvr_send_key_event(input, KEY_HOME, 1);
514 else if (rkvr_data->rkvr_data.key_map.key_power_up)
515 rkvr_send_key_event(input, KEY_POWER, 0);
516 else if (rkvr_data->rkvr_data.key_map.key_power_down)
517 rkvr_send_key_event(input, KEY_POWER, 1);
518 else if (rkvr_data->rkvr_data.key_map.key_volup_up)
519 rkvr_send_key_event(input, KEY_VOLUMEUP, 0);
520 else if (rkvr_data->rkvr_data.key_map.key_volup_down)
521 rkvr_send_key_event(input, KEY_VOLUMEUP, 1);
522 else if (rkvr_data->rkvr_data.key_map.key_voldn_up)
523 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 0);
524 else if (rkvr_data->rkvr_data.key_map.key_voldn_down)
525 rkvr_send_key_event(input, KEY_VOLUMEDOWN, 1);
529 static int rkvr_report_event(struct hid_device *hid, u8 *data, int len)
531 struct hidraw *dev = hid->hidraw;
532 struct hidraw_list *list;
535 union rkvr_data_t *rkvr_data = (union rkvr_data_t *)data;
536 struct sensor_hid_data *pdata = hid_get_drvdata(hid);
538 spin_lock_irqsave(&dev->list_lock, flags);
540 rkvr_keys_event(hid, data, len);
542 if (pdata && pdata->priv && pdata->send_event) {
543 pdata->send_event(rkvr_data->buf, len, pdata->priv);
544 spin_unlock_irqrestore(&dev->list_lock, flags);
546 list_for_each_entry(list, &dev->list, node) {
547 int new_head = (list->head + 1) & (RKVR_HIDRAW_BUFFER_SIZE - 1);
549 if (new_head == list->tail)
552 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
553 if (!list->buffer[list->head].value) {
555 spin_unlock_irqrestore(&dev->list_lock, flags);
559 list->buffer[list->head].len = len;
560 list->head = new_head;
561 kill_fasync(&list->fasync, SIGIO, POLL_IN);
563 spin_unlock_irqrestore(&dev->list_lock, flags);
564 wake_up_interruptible(&dev->wait);
569 /******************************************
570 *--------------------
572 *--------------------
574 ******************************************/
575 static int rkvr_send_report(struct device *dev, unsigned char *data, size_t len)
577 struct hid_device *hid = container_of(dev, struct hid_device, dev);
578 unsigned char reportnum = HID_REPORT_ID_RKVR;
579 unsigned char rtype = HID_OUTPUT_REPORT;
582 ret = hid_hw_raw_request(hid, reportnum, (unsigned char *)data, len, rtype, HID_REQ_SET_REPORT);
584 hid_err(hid, "rkvr_send_report fail\n");
588 hid_info(hid, "rkvr_send_report ok\n");
595 * for enable sensor data
596 ************************************
598 * first 8 bytes :random digits
599 * left bytes :encryt data
600 * eg:32654:3AA4618F6B455D37F06279EC2D6BC478C759443277F3E4E982203562E7ED
601 ***********************************
604 static int hid_report_sync(struct device *dev, const char *data, size_t count)
606 struct hid_device *hid = container_of(dev, struct hid_device, dev);
608 unsigned char buf[64] = {HID_REPORT_ID_RKVR, RKVR_ID_SYNC};
609 unsigned char buf2[3] = {0};
615 p = kmalloc(sizeof(*p) * count, GFP_KERNEL);
617 hid_err(hid, "no mem\n");
620 memcpy(p, data, count);
621 colon = strnchr(p, count, ':');
623 hid_err(hid, "must have conlon\n");
627 if (colon - p + 1 >= count) {
628 hid_err(hid, "must have sync string after conlon\n");
634 tmp = (u64 *)(buf + 2);
635 if (kstrtoull(p, 10, tmp)) {
636 hid_err(hid, "convert rand string fail,only decimal string allowed\n");
640 hid_info(hid, "uint64 %llu\n", *(u64 *)(buf + 2));
641 len = min((count - (colon - p)) / 2, sizeof(buf) - (sizeof(*tmp) + 2));
642 for (i = 0; i < len; i++) {
643 buf2[0] = colon[i * 2];
644 buf2[1] = colon[i * 2 + 1];
645 if (kstrtou8(buf2, 16, &buf[sizeof(*tmp) + 2 + i])) {
646 hid_err(hid, "err sync string,only hex string allowed\n");
651 len = i + sizeof(*tmp) + 2;
652 ret = rkvr_send_report(dev, (unsigned char *)buf, len);
654 hid_err(hid, "hid_report_encrypt fail\n");
658 hid_info(hid, "hid_report_encrypt ok\n");
666 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
669 buf[0] = (1 << 7) | (len && 0x7f);
671 buf[0] = len && 0x7f;
673 memcpy(&buf[2], data, len);
678 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
680 struct hid_device *hid = container_of(dev, struct hid_device, dev);
681 unsigned char report_number = reg;
682 unsigned char report_type = HID_REGR_REPORT;
683 char buf[1 + sizeof(data) * len];
684 int readlen = 1 + sizeof(data) * len;
687 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
688 if (ret != readlen) {
689 hid_info(hid, "id_hw_raw_request fail\n");
691 memcpy(data, &buf[1], readlen);
692 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
698 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
700 struct hid_device *hid = container_of(dev, struct hid_device, dev);
701 unsigned char report_number = HID_REPORT_ID_W;
702 unsigned char report_type = HID_REGW_REPORT;
703 char buf[3 + sizeof(data)];
706 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
707 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
709 hid_info(hid, "id_hw_raw_request fail\n");
711 hid_info(hid, "id_hw_raw_request ok\n");
716 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
717 const char *buf, size_t count)
719 struct hidraw *devraw;
721 devraw = dev_get_drvdata(dev);
722 if (0 == strncmp(buf, "write", 5))
723 hid_report_writereg(&devraw->hid->dev, 0, 0);
724 hid_info(devraw->hid, "%s\n", buf);
729 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
734 struct hidraw *devraw;
736 devraw = dev_get_drvdata(dev);
737 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
738 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
740 count += sprintf(&buf[count], "read fail\n");
744 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
746 static ssize_t rkvr_dev_attr_sync_store(struct device *dev, struct device_attribute *attr,
747 const char *buf, size_t count)
749 struct hidraw *devraw = dev_get_drvdata(dev);
752 ret = hid_report_sync(&devraw->hid->dev, buf, count - 1);
753 return ret > 0 ? count : ret;
756 static DEVICE_ATTR(sync, S_IWUSR, NULL, rkvr_dev_attr_sync_store);
759 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
761 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
762 unsigned char report_number = reg;
763 unsigned char report_type = HID_REGR_REPORT;
764 char buf[1 + sizeof(data) * len];
765 int readlen = 1 + sizeof(data) * len;
768 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
769 if (ret != readlen) {
770 hid_err(hid, "id_hw_raw_request fail\n");
772 memcpy(data, &buf[1], sizeof(data) * len);
778 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
780 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
781 unsigned char report_number = HID_REPORT_ID_W;
782 unsigned char report_type = HID_REGW_REPORT;
783 char buf[3 + sizeof(data)];
786 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
787 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
789 hid_info(hid, "id_hw_raw_request fail\n");
791 hid_info(hid, "id_hw_raw_request ok\n");
796 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
798 struct hid_device *hid;
801 hid = container_of(hdev->dev, struct hid_device, dev);
802 err = hid_hw_power(hid, PM_HINT_FULLON);
805 err = hid_hw_open(hid);
807 hid_hw_power(hid, PM_HINT_NORMAL);
814 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
816 struct hid_device *hid;
818 hid = container_of(hdev->dev, struct hid_device, dev);
819 hid_hw_power(hid, PM_HINT_NORMAL);
823 #if DYNAMIC_LOAD_MPU6500
824 static int register_mpu6500;
825 struct platform_device mpu6500_dev = {
830 static int rkvr_connect(struct hid_device *hid)
835 /* we accept any HID device, no matter the applications */
836 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
840 mutex_lock(&minors_lock);
841 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
842 if (rkvr_hidraw_table[minor])
844 rkvr_hidraw_table[minor] = dev;
849 mutex_unlock(&minors_lock);
854 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
855 NULL, "%s%d", "rkvr", minor);
857 if (IS_ERR(dev->dev)) {
858 rkvr_hidraw_table[minor] = NULL;
859 mutex_unlock(&minors_lock);
860 result = PTR_ERR(dev->dev);
865 dev_set_drvdata(dev->dev, dev);
867 device_create_file(dev->dev, &dev_attr_debug);
868 device_create_file(dev->dev, &dev_attr_sync);
872 struct rkvr_iio_hw_device *hw_device;
874 hw_device = inv_hid_alloc("hid-rkvr");
876 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
877 rkvr_hidraw_table[minor] = NULL;
878 mutex_unlock(&minors_lock);
879 result = PTR_ERR(dev->dev);
883 hw_device->dev = &hid->dev;
884 hw_device->open = rkvr_hid_open;
885 hw_device->close = rkvr_hid_close;
886 hw_device->read = rkvr_hid_read;
887 hw_device->write = rkvr_hid_write;
888 if (inv_hid_register_devcie(hw_device)) {
889 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
890 inv_hid_free(hw_device);
891 rkvr_hidraw_table[minor] = NULL;
892 mutex_unlock(&minors_lock);
893 result = PTR_ERR(dev->dev);
899 #if DYNAMIC_LOAD_MPU6500
900 if (!register_mpu6500) {
901 register_mpu6500 = 1;
902 hid_info(hid, "--->platform_device_register-->\n");
903 platform_device_register(&mpu6500_dev);
907 if (hid_hw_open(hid)) {
908 rkvr_hidraw_table[minor] = NULL;
909 mutex_unlock(&minors_lock);
910 result = PTR_ERR(dev->dev);
912 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
916 init_waitqueue_head(&dev->wait);
917 spin_lock_init(&dev->list_lock);
918 INIT_LIST_HEAD(&dev->list);
923 hid->hidraw = dev; /*struct hidraw * **/
924 mutex_unlock(&minors_lock);
929 static int rkvr_keys_remove(struct hid_device *hdev)
931 struct input_dev *input = hdev->hiddev;
933 input_unregister_device(input);
937 static unsigned int key_codes[] = {
946 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
949 struct device *dev = &hdev->dev;
950 struct input_dev *input = NULL;
953 input = devm_input_allocate_device(dev);
955 hid_err(hdev, "input_allocate_device fail\n");
958 input->name = "rkvr-keypad";
959 input->phys = "rkvr-keys/input0";
960 input->dev.parent = dev;
961 input->id.bustype = BUS_HOST;
962 input->id.vendor = 0x071b;
963 input->id.product = 0x3205;
964 input->id.version = 0x0001;
966 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
967 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
968 input_set_capability(input, EV_KEY, key_codes[i]);
971 error = input_register_device(input);
973 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
976 hdev->hiddev = input;
981 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
983 int ret = hdev->ll_driver->start(hdev);
987 ret = rkvr_connect(hdev);
989 hdev->ll_driver->stop(hdev);
994 static void rkvr_disconnect(struct hid_device *hid)
996 struct hidraw *hidraw = hid->hidraw;
998 mutex_lock(&minors_lock);
999 /* always unregistering inv_hid_device when hardware disconnect */
1000 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
1002 device_remove_file(hidraw->dev, &dev_attr_debug);
1003 device_remove_file(hidraw->dev, &dev_attr_sync);
1006 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
1007 rkvr_drop_ref(hidraw, 1);
1008 mutex_unlock(&minors_lock);
1011 static void rkvr_hw_stop(struct hid_device *hdev)
1013 rkvr_disconnect(hdev);
1014 hdev->ll_driver->stop(hdev);
1017 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1019 struct inode *inode = file_inode(file);
1020 unsigned int minor = iminor(inode);
1023 void __user *user_arg = (void __user *)arg;
1025 mutex_lock(&minors_lock);
1026 dev = rkvr_hidraw_table[minor];
1033 case HIDIOCGRDESCSIZE:
1034 if (put_user(dev->hid->rsize, (int __user *)arg))
1042 if (get_user(len, (int __user *)arg))
1044 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
1046 else if (copy_to_user(user_arg + offsetof(
1047 struct hidraw_report_descriptor,
1050 min(dev->hid->rsize, len)))
1054 case HIDIOCGRAWINFO:
1056 struct hidraw_devinfo dinfo;
1058 dinfo.bustype = dev->hid->bus;
1059 dinfo.vendor = dev->hid->vendor;
1060 dinfo.product = dev->hid->product;
1061 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
1067 struct hid_device *hid = dev->hid;
1069 if (_IOC_TYPE(cmd) != 'H') {
1074 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
1075 int len = _IOC_SIZE(cmd);
1077 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
1080 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
1081 int len = _IOC_SIZE(cmd);
1083 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
1087 if (_IOC_NR(cmd) == _IOC_NR(HIDRKVRHANDSHAKE(0))) {
1088 int len = _IOC_SIZE(cmd);
1091 buf = kzalloc(len + 1, GFP_KERNEL);
1096 if (copy_from_user(buf, user_arg, len)) {
1101 ret = hid_report_sync(&hid->dev, buf, len);
1106 /* Begin Read-only ioctls. */
1107 if (_IOC_DIR(cmd) != _IOC_READ) {
1112 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
1113 int len = strlen(hid->name) + 1;
1115 if (len > _IOC_SIZE(cmd))
1116 len = _IOC_SIZE(cmd);
1117 ret = copy_to_user(user_arg, hid->name, len) ?
1122 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
1123 int len = strlen(hid->phys) + 1;
1125 if (len > _IOC_SIZE(cmd))
1126 len = _IOC_SIZE(cmd);
1127 ret = copy_to_user(user_arg, hid->phys, len) ?
1136 mutex_unlock(&minors_lock);
1140 static const struct file_operations rkvr_ops = {
1141 .owner = THIS_MODULE,
1142 .read = rkvr_hidraw_read,
1143 .write = rkvr_hidraw_write,
1144 .poll = rkvr_hidraw_poll,
1145 .open = rkvr_hidraw_open,
1146 .release = rkvr_hidraw_release,
1147 .unlocked_ioctl = rkvr_hidraw_ioctl,
1148 #ifdef CONFIG_COMPAT
1149 .compat_ioctl = rkvr_hidraw_ioctl,
1151 .fasync = rkvr_hidraw_fasync,
1152 .llseek = noop_llseek,
1155 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1157 sensorData.priv = priv;
1158 sensorData.send_event = callback;
1162 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1164 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1167 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1169 retval = hid_parse(hdev);
1171 hid_err(hdev, "rkvr - parse failed\n");
1174 hid_set_drvdata(hdev, &sensorData);
1175 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1176 retval = rkvr_keys_probe(hdev);
1178 hid_err(hdev, "rkvr_keys_probe failed\n");
1181 retval = rkvr_hw_start(hdev, 0);
1183 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1184 rkvr_keys_remove(hdev);
1188 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1190 hid_err(hdev, "rkvr - hid hw start failed\n");
1203 static void rkvr_remove(struct hid_device *hdev)
1205 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1207 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1209 rkvr_keys_remove(hdev);
1215 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1218 static unsigned int count;
1219 static unsigned long old_jiffy;
1221 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1223 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1224 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1225 intf->cur_altsetting->desc.bInterfaceNumber);
1229 /* print sensor poll frequency */
1230 if (++count >= 1000) {
1231 unsigned long cur_jiffy = jiffies;
1233 hid_dbg(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1235 old_jiffy = cur_jiffy;
1238 if (hdev->hidraw || hdev->hiddev) {
1239 retval = rkvr_report_event(hdev, data, size);
1241 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1247 static const struct hid_device_id rkvr_devices[] = {
1248 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1252 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1254 static struct hid_driver rkvr_driver = {
1256 .id_table = rkvr_devices,
1257 .probe = rkvr_probe,
1258 .remove = rkvr_remove,
1259 .raw_event = rkvr_raw_event
1262 static int __init rkvr_init(void)
1267 rkvr_class = class_create(THIS_MODULE, "rkvr");
1268 if (IS_ERR(rkvr_class))
1269 return PTR_ERR(rkvr_class);
1271 retval = hid_register_driver(&rkvr_driver);
1273 pr_warn("rkvr_init - Can't register drive.\n");
1277 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1278 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1280 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1284 rkvr_major = MAJOR(dev_id);
1285 cdev_init(&rkvr_cdev, &rkvr_ops);
1286 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1290 hid_unregister_driver(&rkvr_driver);
1292 class_destroy(rkvr_class);
1297 static void __exit rkvr_exit(void)
1299 dev_t dev_id = MKDEV(rkvr_major, 0);
1301 cdev_del(&rkvr_cdev);
1303 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1305 hid_unregister_driver(&rkvr_driver);
1306 class_destroy(rkvr_class);
1309 module_init(rkvr_init);
1310 module_exit(rkvr_exit);
1312 MODULE_AUTHOR("zwp");
1313 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1314 MODULE_LICENSE("GPL v2");