1/*
2 * Roccat Ryos driver for Linux
3 *
4 * Copyright (c) 2013 Stefan Achatz <erazor_de@users.sourceforge.net>
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 */
13
14#include <linux/types.h>
15#include <linux/device.h>
16#include <linux/input.h>
17#include <linux/hid.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/hid-roccat.h>
21#include "hid-ids.h"
22#include "hid-roccat-common.h"
23
24enum {
25	RYOS_REPORT_NUMBER_SPECIAL = 3,
26	RYOS_USB_INTERFACE_PROTOCOL = 0,
27};
28
29struct ryos_report_special {
30	uint8_t number; /* RYOS_REPORT_NUMBER_SPECIAL */
31	uint8_t data[4];
32} __packed;
33
34static struct class *ryos_class;
35
36ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x04, 0x03);
37ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x05, 0x03);
38ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_primary, 0x06, 0x7d);
39ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_function, 0x07, 0x5f);
40ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_macro, 0x08, 0x23);
41ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_thumbster, 0x09, 0x17);
42ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_extra, 0x0a, 0x08);
43ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(keys_easyzone, 0x0b, 0x126);
44ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(key_mask, 0x0c, 0x06);
45ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light, 0x0d, 0x10);
46ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x0e, 0x7d2);
47ROCCAT_COMMON2_BIN_ATTRIBUTE_R(info, 0x0f, 0x08);
48ROCCAT_COMMON2_BIN_ATTRIBUTE_W(reset, 0x11, 0x03);
49ROCCAT_COMMON2_BIN_ATTRIBUTE_W(light_control, 0x13, 0x08);
50ROCCAT_COMMON2_BIN_ATTRIBUTE_W(talk, 0x16, 0x10);
51ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(stored_lights, 0x17, 0x0566);
52ROCCAT_COMMON2_BIN_ATTRIBUTE_W(custom_lights, 0x18, 0x14);
53ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(light_macro, 0x19, 0x07d2);
54
55static struct bin_attribute *ryos_bin_attrs[] = {
56	&bin_attr_control,
57	&bin_attr_profile,
58	&bin_attr_keys_primary,
59	&bin_attr_keys_function,
60	&bin_attr_keys_macro,
61	&bin_attr_keys_thumbster,
62	&bin_attr_keys_extra,
63	&bin_attr_keys_easyzone,
64	&bin_attr_key_mask,
65	&bin_attr_light,
66	&bin_attr_macro,
67	&bin_attr_info,
68	&bin_attr_reset,
69	&bin_attr_light_control,
70	&bin_attr_talk,
71	&bin_attr_stored_lights,
72	&bin_attr_custom_lights,
73	&bin_attr_light_macro,
74	NULL,
75};
76
77static const struct attribute_group ryos_group = {
78	.bin_attrs = ryos_bin_attrs,
79};
80
81static const struct attribute_group *ryos_groups[] = {
82	&ryos_group,
83	NULL,
84};
85
86static int ryos_init_specials(struct hid_device *hdev)
87{
88	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
89	struct usb_device *usb_dev = interface_to_usbdev(intf);
90	struct roccat_common2_device *ryos;
91	int retval;
92
93	if (intf->cur_altsetting->desc.bInterfaceProtocol
94			!= RYOS_USB_INTERFACE_PROTOCOL) {
95		hid_set_drvdata(hdev, NULL);
96		return 0;
97	}
98
99	ryos = kzalloc(sizeof(*ryos), GFP_KERNEL);
100	if (!ryos) {
101		hid_err(hdev, "can't alloc device descriptor\n");
102		return -ENOMEM;
103	}
104	hid_set_drvdata(hdev, ryos);
105
106	retval = roccat_common2_device_init_struct(usb_dev, ryos);
107	if (retval) {
108		hid_err(hdev, "couldn't init Ryos device\n");
109		goto exit_free;
110	}
111
112	retval = roccat_connect(ryos_class, hdev,
113			sizeof(struct ryos_report_special));
114	if (retval < 0) {
115		hid_err(hdev, "couldn't init char dev\n");
116	} else {
117		ryos->chrdev_minor = retval;
118		ryos->roccat_claimed = 1;
119	}
120
121	return 0;
122exit_free:
123	kfree(ryos);
124	return retval;
125}
126
127static void ryos_remove_specials(struct hid_device *hdev)
128{
129	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
130	struct roccat_common2_device *ryos;
131
132	if (intf->cur_altsetting->desc.bInterfaceProtocol
133			!= RYOS_USB_INTERFACE_PROTOCOL)
134		return;
135
136	ryos = hid_get_drvdata(hdev);
137	if (ryos->roccat_claimed)
138		roccat_disconnect(ryos->chrdev_minor);
139	kfree(ryos);
140}
141
142static int ryos_probe(struct hid_device *hdev,
143		const struct hid_device_id *id)
144{
145	int retval;
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
167exit_stop:
168	hid_hw_stop(hdev);
169exit:
170	return retval;
171}
172
173static void ryos_remove(struct hid_device *hdev)
174{
175	ryos_remove_specials(hdev);
176	hid_hw_stop(hdev);
177}
178
179static 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
198static 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
205MODULE_DEVICE_TABLE(hid, ryos_devices);
206
207static 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
215static int __init ryos_init(void)
216{
217	int retval;
218
219	ryos_class = class_create(THIS_MODULE, "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
230static void __exit ryos_exit(void)
231{
232	hid_unregister_driver(&ryos_driver);
233	class_destroy(ryos_class);
234}
235
236module_init(ryos_init);
237module_exit(ryos_exit);
238
239MODULE_AUTHOR("Stefan Achatz");
240MODULE_DESCRIPTION("USB Roccat Ryos MK/Glow/Pro driver");
241MODULE_LICENSE("GPL v2");
242