Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Roccat common functions for device specific drivers |
| 4 | * |
| 5 | * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net> |
| 6 | */ |
| 7 | |
| 8 | /* |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 11 | #include <linux/hid.h> |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 12 | #include <linux/slab.h> |
Paul Gortmaker | 8f86a2c | 2011-07-03 13:39:48 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 14 | #include "hid-roccat-common.h" |
| 15 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 16 | static inline uint16_t roccat_common2_feature_report(uint8_t report_id) |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 17 | { |
| 18 | return 0x300 | report_id; |
| 19 | } |
| 20 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 21 | int roccat_common2_receive(struct usb_device *usb_dev, uint report_id, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 22 | void *data, uint size) |
| 23 | { |
| 24 | char *buf; |
| 25 | int len; |
| 26 | |
| 27 | buf = kmalloc(size, GFP_KERNEL); |
| 28 | if (buf == NULL) |
| 29 | return -ENOMEM; |
| 30 | |
| 31 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 32 | HID_REQ_GET_REPORT, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 33 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 34 | roccat_common2_feature_report(report_id), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 35 | 0, buf, size, USB_CTRL_SET_TIMEOUT); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 36 | |
| 37 | memcpy(data, buf, size); |
| 38 | kfree(buf); |
| 39 | return ((len < 0) ? len : ((len != size) ? -EIO : 0)); |
| 40 | } |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 41 | EXPORT_SYMBOL_GPL(roccat_common2_receive); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 42 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 43 | int roccat_common2_send(struct usb_device *usb_dev, uint report_id, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 44 | void const *data, uint size) |
| 45 | { |
| 46 | char *buf; |
| 47 | int len; |
| 48 | |
Thomas Meyer | 4c33a88 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 49 | buf = kmemdup(data, size, GFP_KERNEL); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 50 | if (buf == NULL) |
| 51 | return -ENOMEM; |
| 52 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 53 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 54 | HID_REQ_SET_REPORT, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 55 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 56 | roccat_common2_feature_report(report_id), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 57 | 0, buf, size, USB_CTRL_SET_TIMEOUT); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 58 | |
| 59 | kfree(buf); |
| 60 | return ((len < 0) ? len : ((len != size) ? -EIO : 0)); |
| 61 | } |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 62 | EXPORT_SYMBOL_GPL(roccat_common2_send); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 63 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 64 | enum roccat_common2_control_states { |
Stefan Achatz | 14fc429 | 2013-10-28 18:52:03 +0100 | [diff] [blame] | 65 | ROCCAT_COMMON_CONTROL_STATUS_CRITICAL = 0, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 66 | ROCCAT_COMMON_CONTROL_STATUS_OK = 1, |
| 67 | ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2, |
Stefan Achatz | 14fc429 | 2013-10-28 18:52:03 +0100 | [diff] [blame] | 68 | ROCCAT_COMMON_CONTROL_STATUS_BUSY = 3, |
| 69 | ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW = 4, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 72 | static int roccat_common2_receive_control_status(struct usb_device *usb_dev) |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 73 | { |
| 74 | int retval; |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 75 | struct roccat_common2_control control; |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 76 | |
| 77 | do { |
| 78 | msleep(50); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 79 | retval = roccat_common2_receive(usb_dev, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 80 | ROCCAT_COMMON_COMMAND_CONTROL, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 81 | &control, sizeof(struct roccat_common2_control)); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 82 | |
| 83 | if (retval) |
| 84 | return retval; |
| 85 | |
| 86 | switch (control.value) { |
| 87 | case ROCCAT_COMMON_CONTROL_STATUS_OK: |
| 88 | return 0; |
Stefan Achatz | 14fc429 | 2013-10-28 18:52:03 +0100 | [diff] [blame] | 89 | case ROCCAT_COMMON_CONTROL_STATUS_BUSY: |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 90 | msleep(500); |
| 91 | continue; |
| 92 | case ROCCAT_COMMON_CONTROL_STATUS_INVALID: |
Stefan Achatz | 14fc429 | 2013-10-28 18:52:03 +0100 | [diff] [blame] | 93 | case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL: |
| 94 | case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW: |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 95 | return -EINVAL; |
| 96 | default: |
| 97 | dev_err(&usb_dev->dev, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 98 | "roccat_common2_receive_control_status: " |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 99 | "unknown response value 0x%x\n", |
| 100 | control.value); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | } while (1); |
| 105 | } |
| 106 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 107 | int roccat_common2_send_with_status(struct usb_device *usb_dev, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 108 | uint command, void const *buf, uint size) |
| 109 | { |
| 110 | int retval; |
| 111 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 112 | retval = roccat_common2_send(usb_dev, command, buf, size); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 113 | if (retval) |
| 114 | return retval; |
| 115 | |
| 116 | msleep(100); |
| 117 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 118 | return roccat_common2_receive_control_status(usb_dev); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 119 | } |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 120 | EXPORT_SYMBOL_GPL(roccat_common2_send_with_status); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 121 | |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 122 | int roccat_common2_device_init_struct(struct usb_device *usb_dev, |
| 123 | struct roccat_common2_device *dev) |
| 124 | { |
| 125 | mutex_init(&dev->lock); |
| 126 | return 0; |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(roccat_common2_device_init_struct); |
| 129 | |
| 130 | ssize_t roccat_common2_sysfs_read(struct file *fp, struct kobject *kobj, |
| 131 | char *buf, loff_t off, size_t count, |
| 132 | size_t real_size, uint command) |
| 133 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 134 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 135 | struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev)); |
| 136 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 137 | int retval; |
| 138 | |
| 139 | if (off >= real_size) |
| 140 | return 0; |
| 141 | |
| 142 | if (off != 0 || count != real_size) |
| 143 | return -EINVAL; |
| 144 | |
| 145 | mutex_lock(&roccat_dev->lock); |
| 146 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
| 147 | mutex_unlock(&roccat_dev->lock); |
| 148 | |
| 149 | return retval ? retval : real_size; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(roccat_common2_sysfs_read); |
| 152 | |
| 153 | ssize_t roccat_common2_sysfs_write(struct file *fp, struct kobject *kobj, |
| 154 | void const *buf, loff_t off, size_t count, |
| 155 | size_t real_size, uint command) |
| 156 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 157 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | 71304f5 | 2013-10-28 18:52:05 +0100 | [diff] [blame] | 158 | struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev)); |
| 159 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 160 | int retval; |
| 161 | |
| 162 | if (off != 0 || count != real_size) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | mutex_lock(&roccat_dev->lock); |
| 166 | retval = roccat_common2_send_with_status(usb_dev, command, buf, real_size); |
| 167 | mutex_unlock(&roccat_dev->lock); |
| 168 | |
| 169 | return retval ? retval : real_size; |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(roccat_common2_sysfs_write); |
| 172 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 173 | MODULE_AUTHOR("Stefan Achatz"); |
| 174 | MODULE_DESCRIPTION("USB Roccat common driver"); |
| 175 | MODULE_LICENSE("GPL v2"); |