Blame view

drivers/hid/hid-roccat-koneplus.c 15.4 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
2
3
4
5
6
7
8
  /*
   * Roccat Kone[+] driver for Linux
   *
   * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
   */
  
  /*
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
9
10
11
12
13
   */
  
  /*
   * Roccat Kone[+] is an updated/improved version of the Kone with more memory
   * and functionality and without the non-standard behaviours the Kone had.
8e74a2d31   Stefan Achatz   HID: roccat: add ...
14
   * KoneXTD has same capabilities but updated sensor.
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
15
16
17
18
19
   */
  
  #include <linux/device.h>
  #include <linux/input.h>
  #include <linux/hid.h>
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
20
21
  #include <linux/module.h>
  #include <linux/slab.h>
5dc0c9835   Stefan Achatz   HID: roccat: Rena...
22
  #include <linux/hid-roccat.h>
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
23
  #include "hid-ids.h"
5772f6361   Stefan Achatz   HID: roccat: Intr...
24
  #include "hid-roccat-common.h"
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #include "hid-roccat-koneplus.h"
  
  static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  
  static struct class *koneplus_class;
  
  static void koneplus_profile_activated(struct koneplus_device *koneplus,
  		uint new_profile)
  {
  	koneplus->actual_profile = new_profile;
  }
  
  static int koneplus_send_control(struct usb_device *usb_dev, uint value,
  		enum koneplus_control_requests request)
  {
7392d73be   Stefan Achatz   HID: roccat: rena...
40
  	struct roccat_common2_control control;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
41
42
43
44
45
  
  	if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
  			request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  			value > 4)
  		return -EINVAL;
4728f2dc9   Stefan Achatz   HID: roccat: move...
46
  	control.command = ROCCAT_COMMON_COMMAND_CONTROL;
5772f6361   Stefan Achatz   HID: roccat: Intr...
47
48
  	control.value = value;
  	control.request = request;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
49

7392d73be   Stefan Achatz   HID: roccat: rena...
50
  	return roccat_common2_send_with_status(usb_dev,
4728f2dc9   Stefan Achatz   HID: roccat: move...
51
  			ROCCAT_COMMON_COMMAND_CONTROL,
7392d73be   Stefan Achatz   HID: roccat: rena...
52
  			&control, sizeof(struct roccat_common2_control));
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
53
  }
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
54
55
  
  /* retval is 0-4 on success, < 0 on error */
b50f315cb   Stefan Achatz   HID: roccat: fix ...
56
  static int koneplus_get_actual_profile(struct usb_device *usb_dev)
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
57
  {
b50f315cb   Stefan Achatz   HID: roccat: fix ...
58
  	struct koneplus_actual_profile buf;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
59
  	int retval;
7392d73be   Stefan Achatz   HID: roccat: rena...
60
  	retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
f114fec54   Stefan Achatz   HID: roccat: Clea...
61
  			&buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
62

b50f315cb   Stefan Achatz   HID: roccat: fix ...
63
  	return retval ? retval : buf.actual_profile;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
64
  }
b50f315cb   Stefan Achatz   HID: roccat: fix ...
65
66
  static int koneplus_set_actual_profile(struct usb_device *usb_dev,
  		int new_profile)
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
67
  {
b50f315cb   Stefan Achatz   HID: roccat: fix ...
68
  	struct koneplus_actual_profile buf;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
69

b50f315cb   Stefan Achatz   HID: roccat: fix ...
70
  	buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
f114fec54   Stefan Achatz   HID: roccat: Clea...
71
  	buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
b50f315cb   Stefan Achatz   HID: roccat: fix ...
72
  	buf.actual_profile = new_profile;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
73

7392d73be   Stefan Achatz   HID: roccat: rena...
74
  	return roccat_common2_send_with_status(usb_dev,
4728f2dc9   Stefan Achatz   HID: roccat: move...
75
  			KONEPLUS_COMMAND_ACTUAL_PROFILE,
f114fec54   Stefan Achatz   HID: roccat: Clea...
76
  			&buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
77
78
79
80
81
82
  }
  
  static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
  		char *buf, loff_t off, size_t count,
  		size_t real_size, uint command)
  {
2cf83833f   Geliang Tang   HID: use kobj_to_...
83
  	struct device *dev = kobj_to_dev(kobj)->parent->parent;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
84
85
86
  	struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  	int retval;
fd82be6c0   Stefan Achatz   HID: roccat: Fix ...
87
88
  	if (off >= real_size)
  		return 0;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
89
90
91
92
  	if (off != 0 || count != real_size)
  		return -EINVAL;
  
  	mutex_lock(&koneplus->koneplus_lock);
7392d73be   Stefan Achatz   HID: roccat: rena...
93
  	retval = roccat_common2_receive(usb_dev, command, buf, real_size);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
94
95
96
97
98
99
100
101
102
103
104
105
  	mutex_unlock(&koneplus->koneplus_lock);
  
  	if (retval)
  		return retval;
  
  	return real_size;
  }
  
  static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
  		void const *buf, loff_t off, size_t count,
  		size_t real_size, uint command)
  {
2cf83833f   Geliang Tang   HID: use kobj_to_...
106
  	struct device *dev = kobj_to_dev(kobj)->parent->parent;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
107
108
109
110
111
112
113
114
  	struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  	int retval;
  
  	if (off != 0 || count != real_size)
  		return -EINVAL;
  
  	mutex_lock(&koneplus->koneplus_lock);
7392d73be   Stefan Achatz   HID: roccat: rena...
115
  	retval = roccat_common2_send_with_status(usb_dev, command,
4728f2dc9   Stefan Achatz   HID: roccat: move...
116
  			buf, real_size);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
117
118
119
120
121
122
123
  	mutex_unlock(&koneplus->koneplus_lock);
  
  	if (retval)
  		return retval;
  
  	return real_size;
  }
f114fec54   Stefan Achatz   HID: roccat: Clea...
124
125
126
127
128
129
130
  #define KONEPLUS_SYSFS_W(thingy, THINGY) \
  static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
  		struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  		loff_t off, size_t count) \
  { \
  	return koneplus_sysfs_write(fp, kobj, buf, off, count, \
  			KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
fabe51eb6   Stefan Achatz   HID: roccat: enab...
131
  }
f114fec54   Stefan Achatz   HID: roccat: Clea...
132
133
134
135
136
137
138
  #define KONEPLUS_SYSFS_R(thingy, THINGY) \
  static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
  		struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  		loff_t off, size_t count) \
  { \
  	return koneplus_sysfs_read(fp, kobj, buf, off, count, \
  			KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
fabe51eb6   Stefan Achatz   HID: roccat: enab...
139
  }
f114fec54   Stefan Achatz   HID: roccat: Clea...
140
141
142
  #define KONEPLUS_SYSFS_RW(thingy, THINGY) \
  KONEPLUS_SYSFS_W(thingy, THINGY) \
  KONEPLUS_SYSFS_R(thingy, THINGY)
6d1dec85a   Stefan Achatz   HID: roccat: Add ...
143

f114fec54   Stefan Achatz   HID: roccat: Clea...
144
  #define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
145
146
  KONEPLUS_SYSFS_RW(thingy, THINGY); \
  static struct bin_attribute bin_attr_##thingy = { \
f114fec54   Stefan Achatz   HID: roccat: Clea...
147
148
149
150
  	.attr = { .name = #thingy, .mode = 0660 }, \
  	.size = KONEPLUS_SIZE_ ## THINGY, \
  	.read = koneplus_sysfs_read_ ## thingy, \
  	.write = koneplus_sysfs_write_ ## thingy \
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
151
  }
f114fec54   Stefan Achatz   HID: roccat: Clea...
152
  #define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
153
154
  KONEPLUS_SYSFS_R(thingy, THINGY); \
  static struct bin_attribute bin_attr_##thingy = { \
f114fec54   Stefan Achatz   HID: roccat: Clea...
155
156
157
  	.attr = { .name = #thingy, .mode = 0440 }, \
  	.size = KONEPLUS_SIZE_ ## THINGY, \
  	.read = koneplus_sysfs_read_ ## thingy, \
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
158
  }
f114fec54   Stefan Achatz   HID: roccat: Clea...
159
  #define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
160
161
  KONEPLUS_SYSFS_W(thingy, THINGY); \
  static struct bin_attribute bin_attr_##thingy = { \
f114fec54   Stefan Achatz   HID: roccat: Clea...
162
163
164
  	.attr = { .name = #thingy, .mode = 0220 }, \
  	.size = KONEPLUS_SIZE_ ## THINGY, \
  	.write = koneplus_sysfs_write_ ## thingy \
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
165
  }
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
166
167
168
169
170
171
172
173
174
  KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
  KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
  KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
  KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
  KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
  KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
  KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
  KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
  KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
175
176
177
178
179
  
  static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
  		struct kobject *kobj, struct bin_attribute *attr, char *buf,
  		loff_t off, size_t count)
  {
2cf83833f   Geliang Tang   HID: use kobj_to_...
180
  	struct device *dev = kobj_to_dev(kobj)->parent->parent;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
181
  	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
f114fec54   Stefan Achatz   HID: roccat: Clea...
182
  	ssize_t retval;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
183

f114fec54   Stefan Achatz   HID: roccat: Clea...
184
185
  	retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
  			KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
186
187
  	if (retval)
  		return retval;
f114fec54   Stefan Achatz   HID: roccat: Clea...
188
189
190
  	return koneplus_sysfs_read(fp, kobj, buf, off, count,
  			KONEPLUS_SIZE_PROFILE_SETTINGS,
  			KONEPLUS_COMMAND_PROFILE_SETTINGS);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
191
192
193
194
195
196
  }
  
  static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
  		struct kobject *kobj, struct bin_attribute *attr, char *buf,
  		loff_t off, size_t count)
  {
2cf83833f   Geliang Tang   HID: use kobj_to_...
197
  	struct device *dev = kobj_to_dev(kobj)->parent->parent;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
198
  	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
f114fec54   Stefan Achatz   HID: roccat: Clea...
199
  	ssize_t retval;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
200

f114fec54   Stefan Achatz   HID: roccat: Clea...
201
202
  	retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
  			KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
203
204
  	if (retval)
  		return retval;
f114fec54   Stefan Achatz   HID: roccat: Clea...
205
206
207
  	return koneplus_sysfs_read(fp, kobj, buf, off, count,
  			KONEPLUS_SIZE_PROFILE_BUTTONS,
  			KONEPLUS_COMMAND_PROFILE_BUTTONS);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
208
  }
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
209
210
  #define PROFILE_ATTR(number)						\
  static struct bin_attribute bin_attr_profile##number##_settings = {	\
550dbf478   Stefan Achatz   HID: roccat: Fix ...
211
  	.attr = { .name = "profile" #number "_settings", .mode = 0440 },	\
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
212
213
214
215
216
  	.size = KONEPLUS_SIZE_PROFILE_SETTINGS,				\
  	.read = koneplus_sysfs_read_profilex_settings,			\
  	.private = &profile_numbers[number-1],				\
  };									\
  static struct bin_attribute bin_attr_profile##number##_buttons = {	\
550dbf478   Stefan Achatz   HID: roccat: Fix ...
217
  	.attr = { .name = "profile" #number "_buttons", .mode = 0440 },	\
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
218
219
220
221
222
223
224
225
226
  	.size = KONEPLUS_SIZE_PROFILE_BUTTONS,				\
  	.read = koneplus_sysfs_read_profilex_buttons,			\
  	.private = &profile_numbers[number-1],				\
  };
  PROFILE_ATTR(1);
  PROFILE_ATTR(2);
  PROFILE_ATTR(3);
  PROFILE_ATTR(4);
  PROFILE_ATTR(5);
b50f315cb   Stefan Achatz   HID: roccat: fix ...
227
  static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
228
229
230
231
  		struct device_attribute *attr, char *buf)
  {
  	struct koneplus_device *koneplus =
  			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
b50f315cb   Stefan Achatz   HID: roccat: fix ...
232
233
  	return snprintf(buf, PAGE_SIZE, "%d
  ", koneplus->actual_profile);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
234
  }
b50f315cb   Stefan Achatz   HID: roccat: fix ...
235
  static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
236
237
238
239
240
241
  		struct device_attribute *attr, char const *buf, size_t size)
  {
  	struct koneplus_device *koneplus;
  	struct usb_device *usb_dev;
  	unsigned long profile;
  	int retval;
b50f315cb   Stefan Achatz   HID: roccat: fix ...
242
  	struct koneplus_roccat_report roccat_report;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
243
244
245
246
  
  	dev = dev->parent->parent;
  	koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  	usb_dev = interface_to_usbdev(to_usb_interface(dev));
dfc450b55   Jingoo Han   HID: replace stri...
247
  	retval = kstrtoul(buf, 10, &profile);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
248
249
  	if (retval)
  		return retval;
901e64dbd   Stefan Achatz   HID: roccat: fix ...
250
251
  	if (profile > 4)
  		return -EINVAL;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
252
  	mutex_lock(&koneplus->koneplus_lock);
b50f315cb   Stefan Achatz   HID: roccat: fix ...
253
254
255
256
  
  	retval = koneplus_set_actual_profile(usb_dev, profile);
  	if (retval) {
  		mutex_unlock(&koneplus->koneplus_lock);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
257
  		return retval;
b50f315cb   Stefan Achatz   HID: roccat: fix ...
258
  	}
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
259

901e64dbd   Stefan Achatz   HID: roccat: fix ...
260
  	koneplus_profile_activated(koneplus, profile);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
261

b50f315cb   Stefan Achatz   HID: roccat: fix ...
262
263
264
265
266
267
268
269
270
271
  	roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
  	roccat_report.data1 = profile + 1;
  	roccat_report.data2 = 0;
  	roccat_report.profile = profile + 1;
  	roccat_report_event(koneplus->chrdev_minor,
  			(uint8_t const *)&roccat_report);
  
  	mutex_unlock(&koneplus->koneplus_lock);
  
  	return size;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
272
  }
46a58c44c   Greg Kroah-Hartman   HID: roccat: conv...
273
274
275
276
277
278
  static DEVICE_ATTR(actual_profile, 0660,
  		   koneplus_sysfs_show_actual_profile,
  		   koneplus_sysfs_set_actual_profile);
  static DEVICE_ATTR(startup_profile, 0660,
  		   koneplus_sysfs_show_actual_profile,
  		   koneplus_sysfs_set_actual_profile);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
279
280
281
282
  
  static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
f114fec54   Stefan Achatz   HID: roccat: Clea...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  	struct koneplus_device *koneplus;
  	struct usb_device *usb_dev;
  	struct koneplus_info info;
  
  	dev = dev->parent->parent;
  	koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  	usb_dev = interface_to_usbdev(to_usb_interface(dev));
  
  	mutex_lock(&koneplus->koneplus_lock);
  	roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
  			&info, KONEPLUS_SIZE_INFO);
  	mutex_unlock(&koneplus->koneplus_lock);
  
  	return snprintf(buf, PAGE_SIZE, "%d
  ", info.firmware_version);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
298
  }
46a58c44c   Greg Kroah-Hartman   HID: roccat: conv...
299
300
301
302
303
304
305
306
  static DEVICE_ATTR(firmware_version, 0440,
  		   koneplus_sysfs_show_firmware_version, NULL);
  
  static struct attribute *koneplus_attrs[] = {
  	&dev_attr_actual_profile.attr,
  	&dev_attr_startup_profile.attr,
  	&dev_attr_firmware_version.attr,
  	NULL,
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
307
  };
eb3156e0a   Greg Kroah-Hartman   hid: roccat-konep...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  
  static struct bin_attribute *koneplus_bin_attributes[] = {
  	&bin_attr_control,
  	&bin_attr_talk,
  	&bin_attr_macro,
  	&bin_attr_tcu_image,
  	&bin_attr_info,
  	&bin_attr_sensor,
  	&bin_attr_tcu,
  	&bin_attr_profile_settings,
  	&bin_attr_profile_buttons,
  	&bin_attr_profile1_settings,
  	&bin_attr_profile2_settings,
  	&bin_attr_profile3_settings,
  	&bin_attr_profile4_settings,
  	&bin_attr_profile5_settings,
  	&bin_attr_profile1_buttons,
  	&bin_attr_profile2_buttons,
  	&bin_attr_profile3_buttons,
  	&bin_attr_profile4_buttons,
  	&bin_attr_profile5_buttons,
  	NULL,
  };
  
  static const struct attribute_group koneplus_group = {
  	.attrs = koneplus_attrs,
  	.bin_attrs = koneplus_bin_attributes,
  };
  
  static const struct attribute_group *koneplus_groups[] = {
  	&koneplus_group,
  	NULL,
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
340
341
342
343
344
  };
  
  static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
  		struct koneplus_device *koneplus)
  {
f114fec54   Stefan Achatz   HID: roccat: Clea...
345
  	int retval;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
346
347
  
  	mutex_init(&koneplus->koneplus_lock);
b50f315cb   Stefan Achatz   HID: roccat: fix ...
348
349
350
351
  	retval = koneplus_get_actual_profile(usb_dev);
  	if (retval < 0)
  		return retval;
  	koneplus_profile_activated(koneplus, retval);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  
  	return 0;
  }
  
  static int koneplus_init_specials(struct hid_device *hdev)
  {
  	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  	struct usb_device *usb_dev = interface_to_usbdev(intf);
  	struct koneplus_device *koneplus;
  	int retval;
  
  	if (intf->cur_altsetting->desc.bInterfaceProtocol
  			== USB_INTERFACE_PROTOCOL_MOUSE) {
  
  		koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
  		if (!koneplus) {
a28764ef8   Stefan Achatz   HID: roccat: Use ...
368
369
  			hid_err(hdev, "can't alloc device descriptor
  ");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
370
371
372
373
374
375
  			return -ENOMEM;
  		}
  		hid_set_drvdata(hdev, koneplus);
  
  		retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
  		if (retval) {
a28764ef8   Stefan Achatz   HID: roccat: Use ...
376
377
  			hid_err(hdev, "couldn't init struct koneplus_device
  ");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
378
379
  			goto exit_free;
  		}
8211e4600   Stefan Achatz   HID: roccat: Add ...
380
381
  		retval = roccat_connect(koneplus_class, hdev,
  				sizeof(struct koneplus_roccat_report));
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
382
  		if (retval < 0) {
a28764ef8   Stefan Achatz   HID: roccat: Use ...
383
384
  			hid_err(hdev, "couldn't init char dev
  ");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  		} else {
  			koneplus->chrdev_minor = retval;
  			koneplus->roccat_claimed = 1;
  		}
  	} else {
  		hid_set_drvdata(hdev, NULL);
  	}
  
  	return 0;
  exit_free:
  	kfree(koneplus);
  	return retval;
  }
  
  static void koneplus_remove_specials(struct hid_device *hdev)
  {
  	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  	struct koneplus_device *koneplus;
  
  	if (intf->cur_altsetting->desc.bInterfaceProtocol
  			== USB_INTERFACE_PROTOCOL_MOUSE) {
  		koneplus = hid_get_drvdata(hdev);
  		if (koneplus->roccat_claimed)
  			roccat_disconnect(koneplus->chrdev_minor);
  		kfree(koneplus);
  	}
  }
  
  static int koneplus_probe(struct hid_device *hdev,
  		const struct hid_device_id *id)
  {
  	int retval;
  
  	retval = hid_parse(hdev);
  	if (retval) {
a28764ef8   Stefan Achatz   HID: roccat: Use ...
420
421
  		hid_err(hdev, "parse failed
  ");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
422
423
424
425
426
  		goto exit;
  	}
  
  	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  	if (retval) {
a28764ef8   Stefan Achatz   HID: roccat: Use ...
427
428
  		hid_err(hdev, "hw start failed
  ");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
429
430
431
432
433
  		goto exit;
  	}
  
  	retval = koneplus_init_specials(hdev);
  	if (retval) {
a28764ef8   Stefan Achatz   HID: roccat: Use ...
434
435
  		hid_err(hdev, "couldn't install mouse
  ");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  		goto exit_stop;
  	}
  
  	return 0;
  
  exit_stop:
  	hid_hw_stop(hdev);
  exit:
  	return retval;
  }
  
  static void koneplus_remove(struct hid_device *hdev)
  {
  	koneplus_remove_specials(hdev);
  	hid_hw_stop(hdev);
  }
  
  static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
  		u8 const *data)
  {
  	struct koneplus_mouse_report_button const *button_report;
  
  	switch (data[0]) {
  	case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
  		button_report = (struct koneplus_mouse_report_button const *)data;
  		switch (button_report->type) {
  		case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
  			koneplus_profile_activated(koneplus, button_report->data1 - 1);
  			break;
  		}
  		break;
  	}
  }
  
  static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
  		u8 const *data)
  {
  	struct koneplus_roccat_report roccat_report;
  	struct koneplus_mouse_report_button const *button_report;
  
  	if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  		return;
  
  	button_report = (struct koneplus_mouse_report_button const *)data;
  
  	if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  			button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
  			button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
  		return;
  
  	roccat_report.type = button_report->type;
  	roccat_report.data1 = button_report->data1;
  	roccat_report.data2 = button_report->data2;
  	roccat_report.profile = koneplus->actual_profile + 1;
  	roccat_report_event(koneplus->chrdev_minor,
8211e4600   Stefan Achatz   HID: roccat: Add ...
491
  			(uint8_t const *)&roccat_report);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
492
493
494
495
496
497
498
499
500
501
502
  }
  
  static int koneplus_raw_event(struct hid_device *hdev,
  		struct hid_report *report, u8 *data, int size)
  {
  	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  	struct koneplus_device *koneplus = hid_get_drvdata(hdev);
  
  	if (intf->cur_altsetting->desc.bInterfaceProtocol
  			!= USB_INTERFACE_PROTOCOL_MOUSE)
  		return 0;
901e64dbd   Stefan Achatz   HID: roccat: fix ...
503
504
  	if (koneplus == NULL)
  		return 0;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
505
506
507
508
509
510
511
512
513
514
  	koneplus_keep_values_up_to_date(koneplus, data);
  
  	if (koneplus->roccat_claimed)
  		koneplus_report_to_chrdev(koneplus, data);
  
  	return 0;
  }
  
  static const struct hid_device_id koneplus_devices[] = {
  	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
8e74a2d31   Stefan Achatz   HID: roccat: add ...
515
  	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  	{ }
  };
  
  MODULE_DEVICE_TABLE(hid, koneplus_devices);
  
  static struct hid_driver koneplus_driver = {
  		.name = "koneplus",
  		.id_table = koneplus_devices,
  		.probe = koneplus_probe,
  		.remove = koneplus_remove,
  		.raw_event = koneplus_raw_event
  };
  
  static int __init koneplus_init(void)
  {
  	int retval;
  
  	/* class name has to be same as driver name */
  	koneplus_class = class_create(THIS_MODULE, "koneplus");
  	if (IS_ERR(koneplus_class))
  		return PTR_ERR(koneplus_class);
46a58c44c   Greg Kroah-Hartman   HID: roccat: conv...
537
  	koneplus_class->dev_groups = koneplus_groups;
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
538
539
540
541
542
543
544
545
546
  
  	retval = hid_register_driver(&koneplus_driver);
  	if (retval)
  		class_destroy(koneplus_class);
  	return retval;
  }
  
  static void __exit koneplus_exit(void)
  {
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
547
  	hid_unregister_driver(&koneplus_driver);
74b643dac   Stefan Achatz   HID: roccat: Fix ...
548
  	class_destroy(koneplus_class);
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
549
550
551
552
553
554
  }
  
  module_init(koneplus_init);
  module_exit(koneplus_exit);
  
  MODULE_AUTHOR("Stefan Achatz");
8e74a2d31   Stefan Achatz   HID: roccat: add ...
555
  MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
47dbdbffe   Stefan Achatz   HID: roccat: Add ...
556
  MODULE_LICENSE("GPL v2");