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 static void hid_report_fill_rw(unsigned char *buf, u8 reg, u8 *data, int len, int w)
572 buf[0] = (1 << 7) | (len && 0x7f);
574 buf[0] = len && 0x7f;
576 memcpy(&buf[2], data, len);
580 #define HID_OUTPUT_READREG 3
582 static int hid_report_readreg(struct device *dev, u8 reg, u8 *data, int len)
584 struct hid_device *hid = container_of(dev, struct hid_device, dev);
585 unsigned char report_number = reg;
586 unsigned char report_type = HID_OUTPUT_READREG;
587 char buf[1 + sizeof(data) * len];
588 int readlen = 1 + sizeof(data) * len;
591 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
592 if (ret != readlen) {
593 hid_info(hid, "id_hw_raw_request fail\n");
595 memcpy(data, &buf[1], readlen);
596 hid_info(hid, "hid_report_readreg %02x %02x\n", reg, data[0]);
602 static int hid_report_writereg(struct device *dev, u8 reg, u8 data)
604 struct hid_device *hid = container_of(dev, struct hid_device, dev);
605 unsigned char report_number = 5;
606 unsigned char report_type = HID_OUTPUT_REPORT;
607 char buf[3 + sizeof(data)];
610 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
611 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
613 hid_info(hid, "id_hw_raw_request fail\n");
615 hid_info(hid, "id_hw_raw_request ok\n");
620 static ssize_t rkvr_dev_attr_debug_store(struct device *dev, struct device_attribute *attr,
621 const char *buf, size_t count)
623 struct hidraw *devraw;
624 struct hid_device *hid = container_of(dev, struct hid_device, dev);
626 devraw = dev_get_drvdata(dev);
627 if (0 == strncmp(buf, "write", 5))
628 hid_report_writereg(&devraw->hid->dev, 0, 0);
629 hid_info(hid, "%s\n", buf);
634 static ssize_t rkvr_dev_attr_debug_show(struct device *dev, struct device_attribute *attr,
639 struct hidraw *devraw;
641 devraw = dev_get_drvdata(dev);
642 if (!hid_report_readreg(&devraw->hid->dev, 0x75 | 0x80, &mpu6500_id, 1))
643 count += sprintf(&buf[count], "reg value %d\n", mpu6500_id);
645 count += sprintf(&buf[count], "read fail\n");
649 static DEVICE_ATTR(debug, 0664, rkvr_dev_attr_debug_show, rkvr_dev_attr_debug_store);
652 static int rkvr_hid_read(struct rkvr_iio_hw_device *hdev, int reg, unsigned char *data, int len)
654 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
655 unsigned char report_number = reg;
656 unsigned char report_type = HID_OUTPUT_READREG;
657 char buf[1 + sizeof(data) * len];
658 int readlen = 1 + sizeof(data) * len;
661 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, readlen, report_type, HID_REQ_GET_REPORT);
662 if (ret != readlen) {
663 hid_err(hid, "id_hw_raw_request fail\n");
665 memcpy(data, &buf[1], sizeof(data) * len);
671 static int rkvr_hid_write(struct rkvr_iio_hw_device *hdev, int reg, unsigned char data)
673 struct hid_device *hid = container_of(hdev->dev, struct hid_device, dev);
674 unsigned char report_number = 5;
675 unsigned char report_type = HID_OUTPUT_REPORT;
676 char buf[3 + sizeof(data)];
679 hid_report_fill_rw(&buf[1], reg, &data, sizeof(data), 1);
680 ret = hid_hw_raw_request(hid, report_number, (unsigned char *)buf, 4, report_type, HID_REQ_SET_REPORT);
682 hid_info(hid, "id_hw_raw_request fail\n");
684 hid_info(hid, "id_hw_raw_request ok\n");
689 static int rkvr_hid_open(struct rkvr_iio_hw_device *hdev)
691 struct hid_device *hid;
694 hid = container_of(hdev->dev, struct hid_device, dev);
695 err = hid_hw_power(hid, PM_HINT_FULLON);
698 err = hid_hw_open(hid);
700 hid_hw_power(hid, PM_HINT_NORMAL);
707 static void rkvr_hid_close(struct rkvr_iio_hw_device *hdev)
709 struct hid_device *hid;
711 hid = container_of(hdev->dev, struct hid_device, dev);
712 hid_hw_power(hid, PM_HINT_NORMAL);
716 #if DYNAMIC_LOAD_MPU6500
717 static int register_mpu6500;
718 struct platform_device mpu6500_dev = {
723 static int rkvr_connect(struct hid_device *hid)
728 /* we accept any HID device, no matter the applications */
729 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
733 mutex_lock(&minors_lock);
734 for (minor = 0; minor < RKVR_HIDRAW_MAX_DEVICES; minor++) {
735 if (rkvr_hidraw_table[minor])
737 rkvr_hidraw_table[minor] = dev;
742 mutex_unlock(&minors_lock);
747 dev->dev = device_create(rkvr_class, &hid->dev, MKDEV(rkvr_major, minor),
748 NULL, "%s%d", "rkvr", minor);
750 if (IS_ERR(dev->dev)) {
751 rkvr_hidraw_table[minor] = NULL;
752 mutex_unlock(&minors_lock);
753 result = PTR_ERR(dev->dev);
758 dev_set_drvdata(dev->dev, dev);
760 device_create_file(dev->dev, &dev_attr_debug);
764 struct rkvr_iio_hw_device *hw_device;
766 hw_device = inv_hid_alloc("hid-rkvr");
768 hid_err(hid, "inv_hid_alloc(\"hid-rkvr\") fail\n");
769 rkvr_hidraw_table[minor] = NULL;
770 mutex_unlock(&minors_lock);
771 result = PTR_ERR(dev->dev);
775 hw_device->dev = &hid->dev;
776 hw_device->open = rkvr_hid_open;
777 hw_device->close = rkvr_hid_close;
778 hw_device->read = rkvr_hid_read;
779 hw_device->write = rkvr_hid_write;
780 if (inv_hid_register_devcie(hw_device)) {
781 hid_err(hid, "inv_hid_register_devcie(\"hid-rkvr\") fail\n");
782 inv_hid_free(hw_device);
783 rkvr_hidraw_table[minor] = NULL;
784 mutex_unlock(&minors_lock);
785 result = PTR_ERR(dev->dev);
791 #if DYNAMIC_LOAD_MPU6500
792 if (!register_mpu6500) {
793 register_mpu6500 = 1;
794 hid_info(hid, "--->platform_device_register-->\n");
795 platform_device_register(&mpu6500_dev);
799 if (hid_hw_open(hid)) {
800 rkvr_hidraw_table[minor] = NULL;
801 mutex_unlock(&minors_lock);
802 result = PTR_ERR(dev->dev);
804 hid_err(hid, "rkvr_connect:hid_hw_open fail\n");
808 init_waitqueue_head(&dev->wait);
809 spin_lock_init(&dev->list_lock);
810 INIT_LIST_HEAD(&dev->list);
815 hid->hidraw = dev; /*struct hidraw * **/
816 mutex_unlock(&minors_lock);
821 static int rkvr_keys_remove(struct hid_device *hdev)
823 struct input_dev *input = hdev->hiddev;
825 input_unregister_device(input);
829 static unsigned int key_codes[] = {
838 static int __must_check rkvr_keys_probe(struct hid_device *hdev)
841 struct device *dev = &hdev->dev;
842 struct input_dev *input = NULL;
845 input = devm_input_allocate_device(dev);
847 hid_err(hdev, "input_allocate_device fail\n");
850 input->name = "rkvr-keypad";
851 input->phys = "rkvr-keys/input0";
852 input->dev.parent = dev;
853 input->id.bustype = BUS_HOST;
854 input->id.vendor = 0x071b;
855 input->id.product = 0x3205;
856 input->id.version = 0x0001;
858 for (i = 0; i < sizeof(key_codes) / sizeof(key_codes[0]); i++) {
859 hid_info(hdev, "input_set_capability %d\n", key_codes[i]);
860 input_set_capability(input, EV_KEY, key_codes[i]);
863 error = input_register_device(input);
865 hid_err(hdev, "rkvr-s: Unable to register input device, error: %d\n", error);
868 hdev->hiddev = input;
873 static inline int __must_check rkvr_hw_start(struct hid_device *hdev, unsigned int connect_mask)
875 int ret = hdev->ll_driver->start(hdev);
879 ret = rkvr_connect(hdev);
881 hdev->ll_driver->stop(hdev);
886 static void rkvr_disconnect(struct hid_device *hid)
888 struct hidraw *hidraw = hid->hidraw;
890 mutex_lock(&minors_lock);
891 /* always unregistering inv_hid_device when hardware disconnect */
892 inv_hid_unregister_and_destroy_devcie_by_name("hid-rkvr");
894 device_remove_file(hidraw->dev, &dev_attr_debug);
897 device_destroy(rkvr_class, MKDEV(rkvr_major, hidraw->minor));
898 rkvr_drop_ref(hidraw, 1);
899 mutex_unlock(&minors_lock);
902 static void rkvr_hw_stop(struct hid_device *hdev)
904 rkvr_disconnect(hdev);
905 hdev->ll_driver->stop(hdev);
908 static long rkvr_hidraw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
910 struct inode *inode = file_inode(file);
911 unsigned int minor = iminor(inode);
914 void __user *user_arg = (void __user *)arg;
916 mutex_lock(&minors_lock);
917 dev = rkvr_hidraw_table[minor];
924 case HIDIOCGRDESCSIZE:
925 if (put_user(dev->hid->rsize, (int __user *)arg))
933 if (get_user(len, (int __user *)arg))
935 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
937 else if (copy_to_user(user_arg + offsetof(
938 struct hidraw_report_descriptor,
941 min(dev->hid->rsize, len)))
947 struct hidraw_devinfo dinfo;
949 dinfo.bustype = dev->hid->bus;
950 dinfo.vendor = dev->hid->vendor;
951 dinfo.product = dev->hid->product;
952 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
958 struct hid_device *hid = dev->hid;
960 if (_IOC_TYPE(cmd) != 'H') {
965 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
966 int len = _IOC_SIZE(cmd);
968 ret = rkvr_hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
971 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
972 int len = _IOC_SIZE(cmd);
974 ret = rkvr_hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
978 /* Begin Read-only ioctls. */
979 if (_IOC_DIR(cmd) != _IOC_READ) {
984 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
985 int len = strlen(hid->name) + 1;
987 if (len > _IOC_SIZE(cmd))
988 len = _IOC_SIZE(cmd);
989 ret = copy_to_user(user_arg, hid->name, len) ?
994 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
995 int len = strlen(hid->phys) + 1;
997 if (len > _IOC_SIZE(cmd))
998 len = _IOC_SIZE(cmd);
999 ret = copy_to_user(user_arg, hid->phys, len) ?
1008 mutex_unlock(&minors_lock);
1012 static const struct file_operations rkvr_ops = {
1013 .owner = THIS_MODULE,
1014 .read = rkvr_hidraw_read,
1015 .write = rkvr_hidraw_write,
1016 .poll = rkvr_hidraw_poll,
1017 .open = rkvr_hidraw_open,
1018 .release = rkvr_hidraw_release,
1019 .unlocked_ioctl = rkvr_hidraw_ioctl,
1020 #ifdef CONFIG_COMPAT
1021 .compat_ioctl = rkvr_hidraw_ioctl,
1023 .fasync = rkvr_hidraw_fasync,
1024 .llseek = noop_llseek,
1027 int rkvr_sensor_register_callback(int (*callback)(char *, size_t, void *), void *priv)
1029 sensorData.priv = priv;
1030 sensorData.send_event = callback;
1034 EXPORT_SYMBOL_GPL(rkvr_sensor_register_callback);
1036 static int rkvr_probe(struct hid_device *hdev, const struct hid_device_id *id)
1039 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1041 retval = hid_parse(hdev);
1043 hid_err(hdev, "rkvr - parse failed\n");
1046 hid_set_drvdata(hdev, &sensorData);
1047 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1048 retval = rkvr_keys_probe(hdev);
1050 hid_err(hdev, "rkvr_keys_probe failed\n");
1053 retval = rkvr_hw_start(hdev, 0);
1055 hid_err(hdev, "rkvr - rkvr hw start failed\n");
1056 rkvr_keys_remove(hdev);
1060 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1062 hid_err(hdev, "rkvr - hid hw start failed\n");
1075 static void rkvr_remove(struct hid_device *hdev)
1077 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1079 if (intf->cur_altsetting->desc.bInterfaceNumber == RKVR_INTERFACE_USB_SENSOR_ID) {
1080 rkvr_keys_remove(hdev);
1087 static int rkvr_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1090 static unsigned int count;
1091 static unsigned long old_jiffy;
1093 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1095 if (intf->cur_altsetting->desc.bInterfaceNumber != RKVR_INTERFACE_USB_SENSOR_ID) {
1096 hid_info(hdev, "%s,ignored interface number is %d\n", __func__,
1097 intf->cur_altsetting->desc.bInterfaceNumber);
1101 /* print sensor poll frequency */
1102 if (++count >= 1000) {
1103 unsigned long cur_jiffy = jiffies;
1105 hid_info(hdev, "rkvr: %d Hz, hidrkvr %d\n", (int)(1000 * HZ / (cur_jiffy - old_jiffy)), (hdev->hidraw ? 1 : 0));
1107 old_jiffy = cur_jiffy;
1110 if (hdev->hidraw || hdev->hiddev) {
1111 retval = rkvr_report_event(hdev, data, size);
1113 hid_info(hdev, "rkvr: raw event err %d\n", retval);
1119 static const struct hid_device_id rkvr_devices[] = {
1120 { HID_USB_DEVICE(USB_VENDOR_ID_ROCKCHIP, USB_DEVICE_ID_NANOC) },
1124 MODULE_DEVICE_TABLE(hid, rkvr_devices);
1126 static struct hid_driver rkvr_driver = {
1128 .id_table = rkvr_devices,
1129 .probe = rkvr_probe,
1130 .remove = rkvr_remove,
1131 .raw_event = rkvr_raw_event
1134 static int __init rkvr_init(void)
1139 rkvr_class = class_create(THIS_MODULE, "rkvr");
1140 if (IS_ERR(rkvr_class))
1141 return PTR_ERR(rkvr_class);
1143 retval = hid_register_driver(&rkvr_driver);
1145 pr_warn("rkvr_init - Can't register drive.\n");
1149 retval = alloc_chrdev_region(&dev_id, RKVR_FIRST_MINOR,
1150 RKVR_HIDRAW_MAX_DEVICES, "rkvr");
1152 pr_warn("rkvr_init - Can't allocate chrdev region.\n");
1156 rkvr_major = MAJOR(dev_id);
1157 cdev_init(&rkvr_cdev, &rkvr_ops);
1158 cdev_add(&rkvr_cdev, dev_id, RKVR_HIDRAW_MAX_DEVICES);
1162 hid_unregister_driver(&rkvr_driver);
1164 class_destroy(rkvr_class);
1169 static void __exit rkvr_exit(void)
1171 dev_t dev_id = MKDEV(rkvr_major, 0);
1173 cdev_del(&rkvr_cdev);
1175 unregister_chrdev_region(dev_id, RKVR_HIDRAW_MAX_DEVICES);
1177 hid_unregister_driver(&rkvr_driver);
1178 class_destroy(rkvr_class);
1181 module_init(rkvr_init);
1182 module_exit(rkvr_exit);
1184 MODULE_AUTHOR("zwp");
1185 MODULE_DESCRIPTION("USB ROCKCHIP VR char device driver.");
1186 MODULE_LICENSE("GPL v2");