xref: /openbmc/linux/drivers/hid/hid-roccat-ryos.c (revision 7c7e33b7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Roccat Ryos driver for Linux
4  *
5  * Copyright (c) 2013 Stefan Achatz <erazor_de@users.sourceforge.net>
6  */
7 
8 /*
9  */
10 
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/input.h>
14 #include <linux/hid.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/hid-roccat.h>
18 #include "hid-ids.h"
19 #include "hid-roccat-common.h"
20 
21 enum {
22 	RYOS_REPORT_NUMBER_SPECIAL = 3,
23 	RYOS_USB_INTERFACE_PROTOCOL = 0,
24 };
25 
26 struct ryos_report_special {
27 	uint8_t number; /* RYOS_REPORT_NUMBER_SPECIAL */
28 	uint8_t data[4];
29 } __packed;
30 
31 static struct class *ryos_class;
32 
33 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
34 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x05, 0x03);
35 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_primary, 0x06, 0x7d);
36 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_function, 0x07, 0x5f);
37 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_macro, 0x08, 0x23);
38 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_thumbster, 0x09, 0x17);
39 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_extra, 0x0a, 0x08);
40 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_easyzone, 0x0b, 0x126);
41 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(key_mask, 0x0c, 0x06);
42 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light, 0x0d, 0x10);
43 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x0e, 0x7d2);
44 ROCCAT_COMMON2_BIN_ATTRIBUTE_R(info, 0x0f, 0x08);
45 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(reset, 0x11, 0x03);
46 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(light_control, 0x13, 0x08);
47 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x16, 0x10);
48 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(stored_lights, 0x17, 0x0566);
49 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(custom_lights, 0x18, 0x14);
50 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light_macro, 0x19, 0x07d2);
51 
52 static struct bin_attribute *ryos_bin_attrs[] = {
53 	&bin_attr_control,
54 	&bin_attr_profile,
55 	&bin_attr_keys_primary,
56 	&bin_attr_keys_function,
57 	&bin_attr_keys_macro,
58 	&bin_attr_keys_thumbster,
59 	&bin_attr_keys_extra,
60 	&bin_attr_keys_easyzone,
61 	&bin_attr_key_mask,
62 	&bin_attr_light,
63 	&bin_attr_macro,
64 	&bin_attr_info,
65 	&bin_attr_reset,
66 	&bin_attr_light_control,
67 	&bin_attr_talk,
68 	&bin_attr_stored_lights,
69 	&bin_attr_custom_lights,
70 	&bin_attr_light_macro,
71 	NULL,
72 };
73 
74 static const struct attribute_group ryos_group = {
75 	.bin_attrs = ryos_bin_attrs,
76 };
77 
78 static const struct attribute_group *ryos_groups[] = {
79 	&ryos_group,
80 	NULL,
81 };
82 
83 static int ryos_init_specials(struct hid_device *hdev)
84 {
85 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
86 	struct usb_device *usb_dev = interface_to_usbdev(intf);
87 	struct roccat_common2_device *ryos;
88 	int retval;
89 
90 	if (intf->cur_altsetting->desc.bInterfaceProtocol
91 			!= RYOS_USB_INTERFACE_PROTOCOL) {
92 		hid_set_drvdata(hdev, NULL);
93 		return 0;
94 	}
95 
96 	ryos = kzalloc(sizeof(*ryos), GFP_KERNEL);
97 	if (!ryos) {
98 		hid_err(hdev, "can't alloc device descriptor\n");
99 		return -ENOMEM;
100 	}
101 	hid_set_drvdata(hdev, ryos);
102 
103 	retval = roccat_common2_device_init_struct(usb_dev, ryos);
104 	if (retval) {
105 		hid_err(hdev, "couldn't init Ryos device\n");
106 		goto exit_free;
107 	}
108 
109 	retval = roccat_connect(ryos_class, hdev,
110 			sizeof(struct ryos_report_special));
111 	if (retval < 0) {
112 		hid_err(hdev, "couldn't init char dev\n");
113 	} else {
114 		ryos->chrdev_minor = retval;
115 		ryos->roccat_claimed = 1;
116 	}
117 
118 	return 0;
119 exit_free:
120 	kfree(ryos);
121 	return retval;
122 }
123 
124 static void ryos_remove_specials(struct hid_device *hdev)
125 {
126 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
127 	struct roccat_common2_device *ryos;
128 
129 	if (intf->cur_altsetting->desc.bInterfaceProtocol
130 			!= RYOS_USB_INTERFACE_PROTOCOL)
131 		return;
132 
133 	ryos = hid_get_drvdata(hdev);
134 	if (ryos->roccat_claimed)
135 		roccat_disconnect(ryos->chrdev_minor);
136 	kfree(ryos);
137 }
138 
139 static int ryos_probe(struct hid_device *hdev,
140 		const struct hid_device_id *id)
141 {
142 	int retval;
143 
144 	if (!hid_is_usb(hdev))
145 		return -EINVAL;
146 
147 	retval = hid_parse(hdev);
148 	if (retval) {
149 		hid_err(hdev, "parse failed\n");
150 		goto exit;
151 	}
152 
153 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
154 	if (retval) {
155 		hid_err(hdev, "hw start failed\n");
156 		goto exit;
157 	}
158 
159 	retval = ryos_init_specials(hdev);
160 	if (retval) {
161 		hid_err(hdev, "couldn't install mouse\n");
162 		goto exit_stop;
163 	}
164 
165 	return 0;
166 
167 exit_stop:
168 	hid_hw_stop(hdev);
169 exit:
170 	return retval;
171 }
172 
173 static void ryos_remove(struct hid_device *hdev)
174 {
175 	ryos_remove_specials(hdev);
176 	hid_hw_stop(hdev);
177 }
178 
179 static int ryos_raw_event(struct hid_device *hdev,
180 		struct hid_report *report, u8 *data, int size)
181 {
182 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
183 	struct roccat_common2_device *ryos = hid_get_drvdata(hdev);
184 
185 	if (intf->cur_altsetting->desc.bInterfaceProtocol
186 			!= RYOS_USB_INTERFACE_PROTOCOL)
187 		return 0;
188 
189 	if (data[0] != RYOS_REPORT_NUMBER_SPECIAL)
190 		return 0;
191 
192 	if (ryos != NULL && ryos->roccat_claimed)
193 		roccat_report_event(ryos->chrdev_minor, data);
194 
195 	return 0;
196 }
197 
198 static const struct hid_device_id ryos_devices[] = {
199 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK) },
200 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_GLOW) },
201 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_RYOS_MK_PRO) },
202 	{ }
203 };
204 
205 MODULE_DEVICE_TABLE(hid, ryos_devices);
206 
207 static struct hid_driver ryos_driver = {
208 		.name = "ryos",
209 		.id_table = ryos_devices,
210 		.probe = ryos_probe,
211 		.remove = ryos_remove,
212 		.raw_event = ryos_raw_event
213 };
214 
215 static int __init ryos_init(void)
216 {
217 	int retval;
218 
219 	ryos_class = class_create("ryos");
220 	if (IS_ERR(ryos_class))
221 		return PTR_ERR(ryos_class);
222 	ryos_class->dev_groups = ryos_groups;
223 
224 	retval = hid_register_driver(&ryos_driver);
225 	if (retval)
226 		class_destroy(ryos_class);
227 	return retval;
228 }
229 
230 static void __exit ryos_exit(void)
231 {
232 	hid_unregister_driver(&ryos_driver);
233 	class_destroy(ryos_class);
234 }
235 
236 module_init(ryos_init);
237 module_exit(ryos_exit);
238 
239 MODULE_AUTHOR("Stefan Achatz");
240 MODULE_DESCRIPTION("USB Roccat Ryos MK/Glow/Pro driver");
241 MODULE_LICENSE("GPL v2");
242