Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
cb7cf3da SA |
2 | /* |
3 | * Roccat Pyra driver for Linux | |
4 | * | |
5 | * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net> | |
6 | */ | |
7 | ||
8 | /* | |
cb7cf3da SA |
9 | */ |
10 | ||
11 | /* | |
12 | * Roccat Pyra is a mobile gamer mouse which comes in wired and wireless | |
13 | * variant. Wireless variant is not tested. | |
14 | * Userland tools can be found at http://sourceforge.net/projects/roccat | |
15 | */ | |
16 | ||
17 | #include <linux/device.h> | |
18 | #include <linux/input.h> | |
19 | #include <linux/hid.h> | |
cb7cf3da SA |
20 | #include <linux/module.h> |
21 | #include <linux/slab.h> | |
5dc0c983 | 22 | #include <linux/hid-roccat.h> |
cb7cf3da | 23 | #include "hid-ids.h" |
5772f636 | 24 | #include "hid-roccat-common.h" |
cb7cf3da SA |
25 | #include "hid-roccat-pyra.h" |
26 | ||
14a057f8 SA |
27 | static uint profile_numbers[5] = {0, 1, 2, 3, 4}; |
28 | ||
cb7cf3da SA |
29 | static void profile_activated(struct pyra_device *pyra, |
30 | unsigned int new_profile) | |
31 | { | |
606185b2 DC |
32 | if (new_profile >= ARRAY_SIZE(pyra->profile_settings)) |
33 | return; | |
cb7cf3da SA |
34 | pyra->actual_profile = new_profile; |
35 | pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi; | |
36 | } | |
37 | ||
38 | static int pyra_send_control(struct usb_device *usb_dev, int value, | |
39 | enum pyra_control_requests request) | |
40 | { | |
7392d73b | 41 | struct roccat_common2_control control; |
cb7cf3da SA |
42 | |
43 | if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS || | |
44 | request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) && | |
45 | (value < 0 || value > 4)) | |
46 | return -EINVAL; | |
47 | ||
4728f2dc | 48 | control.command = ROCCAT_COMMON_COMMAND_CONTROL; |
cb7cf3da SA |
49 | control.value = value; |
50 | control.request = request; | |
51 | ||
7392d73b SA |
52 | return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL, |
53 | &control, sizeof(struct roccat_common2_control)); | |
cb7cf3da SA |
54 | } |
55 | ||
56 | static int pyra_get_profile_settings(struct usb_device *usb_dev, | |
57 | struct pyra_profile_settings *buf, int number) | |
58 | { | |
59 | int retval; | |
cb7cf3da SA |
60 | retval = pyra_send_control(usb_dev, number, |
61 | PYRA_CONTROL_REQUEST_PROFILE_SETTINGS); | |
cb7cf3da SA |
62 | if (retval) |
63 | return retval; | |
7392d73b | 64 | return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS, |
be34380e | 65 | buf, PYRA_SIZE_PROFILE_SETTINGS); |
cb7cf3da SA |
66 | } |
67 | ||
68 | static int pyra_get_settings(struct usb_device *usb_dev, | |
69 | struct pyra_settings *buf) | |
70 | { | |
7392d73b | 71 | return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS, |
be34380e | 72 | buf, PYRA_SIZE_SETTINGS); |
cb7cf3da SA |
73 | } |
74 | ||
75 | static int pyra_set_settings(struct usb_device *usb_dev, | |
76 | struct pyra_settings const *settings) | |
77 | { | |
7392d73b | 78 | return roccat_common2_send_with_status(usb_dev, |
4728f2dc | 79 | PYRA_COMMAND_SETTINGS, settings, |
be34380e | 80 | PYRA_SIZE_SETTINGS); |
cb7cf3da SA |
81 | } |
82 | ||
be34380e SA |
83 | static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj, |
84 | char *buf, loff_t off, size_t count, | |
85 | size_t real_size, uint command) | |
cb7cf3da | 86 | { |
2cf83833 | 87 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
cb7cf3da | 88 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
be34380e SA |
89 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
90 | int retval; | |
cb7cf3da | 91 | |
be34380e | 92 | if (off >= real_size) |
cb7cf3da SA |
93 | return 0; |
94 | ||
be34380e SA |
95 | if (off != 0 || count != real_size) |
96 | return -EINVAL; | |
cb7cf3da SA |
97 | |
98 | mutex_lock(&pyra->pyra_lock); | |
be34380e | 99 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
cb7cf3da SA |
100 | mutex_unlock(&pyra->pyra_lock); |
101 | ||
be34380e SA |
102 | if (retval) |
103 | return retval; | |
104 | ||
105 | return real_size; | |
cb7cf3da SA |
106 | } |
107 | ||
be34380e SA |
108 | static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj, |
109 | void const *buf, loff_t off, size_t count, | |
110 | size_t real_size, uint command) | |
cb7cf3da | 111 | { |
2cf83833 | 112 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
cb7cf3da | 113 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
be34380e SA |
114 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
115 | int retval; | |
cb7cf3da | 116 | |
be34380e SA |
117 | if (off != 0 || count != real_size) |
118 | return -EINVAL; | |
cb7cf3da SA |
119 | |
120 | mutex_lock(&pyra->pyra_lock); | |
be34380e | 121 | retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size); |
cb7cf3da SA |
122 | mutex_unlock(&pyra->pyra_lock); |
123 | ||
be34380e SA |
124 | if (retval) |
125 | return retval; | |
126 | ||
127 | return real_size; | |
cb7cf3da SA |
128 | } |
129 | ||
be34380e SA |
130 | #define PYRA_SYSFS_W(thingy, THINGY) \ |
131 | static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \ | |
132 | struct kobject *kobj, struct bin_attribute *attr, char *buf, \ | |
133 | loff_t off, size_t count) \ | |
134 | { \ | |
135 | return pyra_sysfs_write(fp, kobj, buf, off, count, \ | |
136 | PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \ | |
137 | } | |
cb7cf3da | 138 | |
be34380e SA |
139 | #define PYRA_SYSFS_R(thingy, THINGY) \ |
140 | static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \ | |
141 | struct kobject *kobj, struct bin_attribute *attr, char *buf, \ | |
142 | loff_t off, size_t count) \ | |
143 | { \ | |
144 | return pyra_sysfs_read(fp, kobj, buf, off, count, \ | |
145 | PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \ | |
146 | } | |
cb7cf3da | 147 | |
be34380e SA |
148 | #define PYRA_SYSFS_RW(thingy, THINGY) \ |
149 | PYRA_SYSFS_W(thingy, THINGY) \ | |
150 | PYRA_SYSFS_R(thingy, THINGY) | |
cb7cf3da | 151 | |
be34380e | 152 | #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \ |
a7492451 GKH |
153 | PYRA_SYSFS_RW(thingy, THINGY); \ |
154 | static struct bin_attribute bin_attr_##thingy = { \ | |
be34380e SA |
155 | .attr = { .name = #thingy, .mode = 0660 }, \ |
156 | .size = PYRA_SIZE_ ## THINGY, \ | |
157 | .read = pyra_sysfs_read_ ## thingy, \ | |
158 | .write = pyra_sysfs_write_ ## thingy \ | |
159 | } | |
cb7cf3da | 160 | |
be34380e | 161 | #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \ |
a7492451 GKH |
162 | PYRA_SYSFS_R(thingy, THINGY); \ |
163 | static struct bin_attribute bin_attr_##thingy = { \ | |
be34380e SA |
164 | .attr = { .name = #thingy, .mode = 0440 }, \ |
165 | .size = PYRA_SIZE_ ## THINGY, \ | |
166 | .read = pyra_sysfs_read_ ## thingy, \ | |
167 | } | |
cb7cf3da | 168 | |
be34380e | 169 | #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \ |
a7492451 GKH |
170 | PYRA_SYSFS_W(thingy, THINGY); \ |
171 | static struct bin_attribute bin_attr_##thingy = { \ | |
be34380e SA |
172 | .attr = { .name = #thingy, .mode = 0220 }, \ |
173 | .size = PYRA_SIZE_ ## THINGY, \ | |
174 | .write = pyra_sysfs_write_ ## thingy \ | |
cb7cf3da SA |
175 | } |
176 | ||
a7492451 GKH |
177 | PYRA_BIN_ATTRIBUTE_W(control, CONTROL); |
178 | PYRA_BIN_ATTRIBUTE_RW(info, INFO); | |
179 | PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS); | |
180 | PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS); | |
be34380e SA |
181 | |
182 | static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp, | |
cb7cf3da SA |
183 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
184 | loff_t off, size_t count) | |
185 | { | |
2cf83833 | 186 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
cb7cf3da | 187 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
be34380e | 188 | ssize_t retval; |
cb7cf3da | 189 | |
be34380e SA |
190 | retval = pyra_send_control(usb_dev, *(uint *)(attr->private), |
191 | PYRA_CONTROL_REQUEST_PROFILE_SETTINGS); | |
cb7cf3da SA |
192 | if (retval) |
193 | return retval; | |
194 | ||
be34380e SA |
195 | return pyra_sysfs_read(fp, kobj, buf, off, count, |
196 | PYRA_SIZE_PROFILE_SETTINGS, | |
197 | PYRA_COMMAND_PROFILE_SETTINGS); | |
cb7cf3da SA |
198 | } |
199 | ||
be34380e | 200 | static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp, |
cb7cf3da SA |
201 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
202 | loff_t off, size_t count) | |
203 | { | |
2cf83833 | 204 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
be34380e SA |
205 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
206 | ssize_t retval; | |
cb7cf3da | 207 | |
be34380e SA |
208 | retval = pyra_send_control(usb_dev, *(uint *)(attr->private), |
209 | PYRA_CONTROL_REQUEST_PROFILE_BUTTONS); | |
210 | if (retval) | |
211 | return retval; | |
cb7cf3da | 212 | |
be34380e SA |
213 | return pyra_sysfs_read(fp, kobj, buf, off, count, |
214 | PYRA_SIZE_PROFILE_BUTTONS, | |
215 | PYRA_COMMAND_PROFILE_BUTTONS); | |
cb7cf3da SA |
216 | } |
217 | ||
a7492451 GKH |
218 | #define PROFILE_ATTR(number) \ |
219 | static struct bin_attribute bin_attr_profile##number##_settings = { \ | |
550dbf47 | 220 | .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \ |
a7492451 GKH |
221 | .size = PYRA_SIZE_PROFILE_SETTINGS, \ |
222 | .read = pyra_sysfs_read_profilex_settings, \ | |
223 | .private = &profile_numbers[number-1], \ | |
224 | }; \ | |
225 | static struct bin_attribute bin_attr_profile##number##_buttons = { \ | |
550dbf47 | 226 | .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \ |
a7492451 GKH |
227 | .size = PYRA_SIZE_PROFILE_BUTTONS, \ |
228 | .read = pyra_sysfs_read_profilex_buttons, \ | |
229 | .private = &profile_numbers[number-1], \ | |
230 | }; | |
231 | PROFILE_ATTR(1); | |
232 | PROFILE_ATTR(2); | |
233 | PROFILE_ATTR(3); | |
234 | PROFILE_ATTR(4); | |
235 | PROFILE_ATTR(5); | |
236 | ||
cb7cf3da SA |
237 | static ssize_t pyra_sysfs_write_settings(struct file *fp, |
238 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
239 | loff_t off, size_t count) | |
240 | { | |
2cf83833 | 241 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
cb7cf3da SA |
242 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
243 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
244 | int retval = 0; | |
dc186b66 | 245 | struct pyra_roccat_report roccat_report; |
be34380e | 246 | struct pyra_settings const *settings; |
cb7cf3da | 247 | |
be34380e | 248 | if (off != 0 || count != PYRA_SIZE_SETTINGS) |
cb7cf3da SA |
249 | return -EINVAL; |
250 | ||
be34380e | 251 | settings = (struct pyra_settings const *)buf; |
606185b2 DC |
252 | if (settings->startup_profile >= ARRAY_SIZE(pyra->profile_settings)) |
253 | return -EINVAL; | |
254 | ||
255 | mutex_lock(&pyra->pyra_lock); | |
cb7cf3da | 256 | |
be34380e SA |
257 | retval = pyra_set_settings(usb_dev, settings); |
258 | if (retval) { | |
259 | mutex_unlock(&pyra->pyra_lock); | |
260 | return retval; | |
dc186b66 | 261 | } |
be34380e SA |
262 | |
263 | profile_activated(pyra, settings->startup_profile); | |
264 | ||
265 | roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2; | |
266 | roccat_report.value = settings->startup_profile + 1; | |
267 | roccat_report.key = 0; | |
268 | roccat_report_event(pyra->chrdev_minor, | |
269 | (uint8_t const *)&roccat_report); | |
270 | ||
dc186b66 | 271 | mutex_unlock(&pyra->pyra_lock); |
be34380e | 272 | return PYRA_SIZE_SETTINGS; |
cb7cf3da SA |
273 | } |
274 | ||
a7492451 GKH |
275 | PYRA_SYSFS_R(settings, SETTINGS); |
276 | static struct bin_attribute bin_attr_settings = | |
277 | __BIN_ATTR(settings, (S_IWUSR | S_IRUGO), | |
278 | pyra_sysfs_read_settings, pyra_sysfs_write_settings, | |
279 | PYRA_SIZE_SETTINGS); | |
cb7cf3da SA |
280 | |
281 | static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev, | |
282 | struct device_attribute *attr, char *buf) | |
283 | { | |
5012aada SA |
284 | struct pyra_device *pyra = |
285 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
cb7cf3da SA |
286 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi); |
287 | } | |
46a58c44 | 288 | static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL); |
cb7cf3da SA |
289 | |
290 | static ssize_t pyra_sysfs_show_actual_profile(struct device *dev, | |
291 | struct device_attribute *attr, char *buf) | |
292 | { | |
5012aada SA |
293 | struct pyra_device *pyra = |
294 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
be34380e SA |
295 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
296 | struct pyra_settings settings; | |
297 | ||
298 | mutex_lock(&pyra->pyra_lock); | |
299 | roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS, | |
300 | &settings, PYRA_SIZE_SETTINGS); | |
301 | mutex_unlock(&pyra->pyra_lock); | |
302 | ||
303 | return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile); | |
cb7cf3da | 304 | } |
46a58c44 GKH |
305 | static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL); |
306 | static DEVICE_ATTR(startup_profile, 0440, pyra_sysfs_show_actual_profile, NULL); | |
cb7cf3da SA |
307 | |
308 | static ssize_t pyra_sysfs_show_firmware_version(struct device *dev, | |
309 | struct device_attribute *attr, char *buf) | |
310 | { | |
be34380e SA |
311 | struct pyra_device *pyra; |
312 | struct usb_device *usb_dev; | |
313 | struct pyra_info info; | |
cb7cf3da | 314 | |
be34380e SA |
315 | dev = dev->parent->parent; |
316 | pyra = hid_get_drvdata(dev_get_drvdata(dev)); | |
317 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
318 | ||
319 | mutex_lock(&pyra->pyra_lock); | |
320 | roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO, | |
321 | &info, PYRA_SIZE_INFO); | |
322 | mutex_unlock(&pyra->pyra_lock); | |
323 | ||
324 | return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version); | |
cb7cf3da | 325 | } |
46a58c44 GKH |
326 | static DEVICE_ATTR(firmware_version, 0440, pyra_sysfs_show_firmware_version, |
327 | NULL); | |
328 | ||
329 | static struct attribute *pyra_attrs[] = { | |
330 | &dev_attr_actual_cpi.attr, | |
331 | &dev_attr_actual_profile.attr, | |
332 | &dev_attr_firmware_version.attr, | |
333 | &dev_attr_startup_profile.attr, | |
334 | NULL, | |
cb7cf3da | 335 | }; |
a7492451 GKH |
336 | |
337 | static struct bin_attribute *pyra_bin_attributes[] = { | |
338 | &bin_attr_control, | |
339 | &bin_attr_info, | |
340 | &bin_attr_profile_settings, | |
341 | &bin_attr_profile_buttons, | |
342 | &bin_attr_settings, | |
343 | &bin_attr_profile1_settings, | |
344 | &bin_attr_profile2_settings, | |
345 | &bin_attr_profile3_settings, | |
346 | &bin_attr_profile4_settings, | |
347 | &bin_attr_profile5_settings, | |
348 | &bin_attr_profile1_buttons, | |
349 | &bin_attr_profile2_buttons, | |
350 | &bin_attr_profile3_buttons, | |
351 | &bin_attr_profile4_buttons, | |
352 | &bin_attr_profile5_buttons, | |
353 | NULL, | |
354 | }; | |
355 | ||
356 | static const struct attribute_group pyra_group = { | |
357 | .attrs = pyra_attrs, | |
358 | .bin_attrs = pyra_bin_attributes, | |
359 | }; | |
360 | ||
361 | static const struct attribute_group *pyra_groups[] = { | |
362 | &pyra_group, | |
363 | NULL, | |
cb7cf3da SA |
364 | }; |
365 | ||
afdf5dd3 IO |
366 | /* pyra_class is used for creating sysfs attributes via roccat char device */ |
367 | static const struct class pyra_class = { | |
368 | .name = "pyra", | |
369 | .dev_groups = pyra_groups, | |
370 | }; | |
371 | ||
cb7cf3da SA |
372 | static int pyra_init_pyra_device_struct(struct usb_device *usb_dev, |
373 | struct pyra_device *pyra) | |
374 | { | |
be34380e | 375 | struct pyra_settings settings; |
cb7cf3da SA |
376 | int retval, i; |
377 | ||
378 | mutex_init(&pyra->pyra_lock); | |
379 | ||
be34380e | 380 | retval = pyra_get_settings(usb_dev, &settings); |
cb7cf3da SA |
381 | if (retval) |
382 | return retval; | |
383 | ||
384 | for (i = 0; i < 5; ++i) { | |
385 | retval = pyra_get_profile_settings(usb_dev, | |
386 | &pyra->profile_settings[i], i); | |
387 | if (retval) | |
388 | return retval; | |
cb7cf3da SA |
389 | } |
390 | ||
be34380e | 391 | profile_activated(pyra, settings.startup_profile); |
cb7cf3da SA |
392 | |
393 | return 0; | |
394 | } | |
395 | ||
396 | static int pyra_init_specials(struct hid_device *hdev) | |
397 | { | |
398 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
399 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
400 | struct pyra_device *pyra; | |
401 | int retval; | |
402 | ||
403 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
404 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
405 | ||
406 | pyra = kzalloc(sizeof(*pyra), GFP_KERNEL); | |
407 | if (!pyra) { | |
4291ee30 | 408 | hid_err(hdev, "can't alloc device descriptor\n"); |
cb7cf3da SA |
409 | return -ENOMEM; |
410 | } | |
411 | hid_set_drvdata(hdev, pyra); | |
412 | ||
413 | retval = pyra_init_pyra_device_struct(usb_dev, pyra); | |
414 | if (retval) { | |
4291ee30 | 415 | hid_err(hdev, "couldn't init struct pyra_device\n"); |
cb7cf3da SA |
416 | goto exit_free; |
417 | } | |
418 | ||
afdf5dd3 | 419 | retval = roccat_connect(&pyra_class, hdev, |
8211e460 | 420 | sizeof(struct pyra_roccat_report)); |
cb7cf3da | 421 | if (retval < 0) { |
4291ee30 | 422 | hid_err(hdev, "couldn't init char dev\n"); |
cb7cf3da SA |
423 | } else { |
424 | pyra->chrdev_minor = retval; | |
425 | pyra->roccat_claimed = 1; | |
426 | } | |
cb7cf3da SA |
427 | } else { |
428 | hid_set_drvdata(hdev, NULL); | |
429 | } | |
430 | ||
431 | return 0; | |
432 | exit_free: | |
433 | kfree(pyra); | |
434 | return retval; | |
435 | } | |
436 | ||
437 | static void pyra_remove_specials(struct hid_device *hdev) | |
438 | { | |
439 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
440 | struct pyra_device *pyra; | |
441 | ||
442 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
443 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
cb7cf3da SA |
444 | pyra = hid_get_drvdata(hdev); |
445 | if (pyra->roccat_claimed) | |
446 | roccat_disconnect(pyra->chrdev_minor); | |
447 | kfree(hid_get_drvdata(hdev)); | |
448 | } | |
449 | } | |
450 | ||
451 | static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
452 | { | |
453 | int retval; | |
454 | ||
93020953 GKH |
455 | if (!hid_is_usb(hdev)) |
456 | return -EINVAL; | |
457 | ||
cb7cf3da SA |
458 | retval = hid_parse(hdev); |
459 | if (retval) { | |
4291ee30 | 460 | hid_err(hdev, "parse failed\n"); |
cb7cf3da SA |
461 | goto exit; |
462 | } | |
463 | ||
464 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
465 | if (retval) { | |
4291ee30 | 466 | hid_err(hdev, "hw start failed\n"); |
cb7cf3da SA |
467 | goto exit; |
468 | } | |
469 | ||
470 | retval = pyra_init_specials(hdev); | |
471 | if (retval) { | |
4291ee30 | 472 | hid_err(hdev, "couldn't install mouse\n"); |
cb7cf3da SA |
473 | goto exit_stop; |
474 | } | |
475 | return 0; | |
476 | ||
477 | exit_stop: | |
478 | hid_hw_stop(hdev); | |
479 | exit: | |
480 | return retval; | |
481 | } | |
482 | ||
483 | static void pyra_remove(struct hid_device *hdev) | |
484 | { | |
485 | pyra_remove_specials(hdev); | |
486 | hid_hw_stop(hdev); | |
487 | } | |
488 | ||
489 | static void pyra_keep_values_up_to_date(struct pyra_device *pyra, | |
490 | u8 const *data) | |
491 | { | |
492 | struct pyra_mouse_event_button const *button_event; | |
493 | ||
494 | switch (data[0]) { | |
495 | case PYRA_MOUSE_REPORT_NUMBER_BUTTON: | |
496 | button_event = (struct pyra_mouse_event_button const *)data; | |
497 | switch (button_event->type) { | |
498 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: | |
499 | profile_activated(pyra, button_event->data1 - 1); | |
500 | break; | |
501 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: | |
502 | pyra->actual_cpi = button_event->data1; | |
503 | break; | |
504 | } | |
505 | break; | |
506 | } | |
507 | } | |
508 | ||
509 | static void pyra_report_to_chrdev(struct pyra_device const *pyra, | |
510 | u8 const *data) | |
511 | { | |
512 | struct pyra_roccat_report roccat_report; | |
513 | struct pyra_mouse_event_button const *button_event; | |
514 | ||
515 | if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON) | |
516 | return; | |
517 | ||
518 | button_event = (struct pyra_mouse_event_button const *)data; | |
519 | ||
520 | switch (button_event->type) { | |
521 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: | |
522 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: | |
523 | roccat_report.type = button_event->type; | |
524 | roccat_report.value = button_event->data1; | |
525 | roccat_report.key = 0; | |
526 | roccat_report_event(pyra->chrdev_minor, | |
8211e460 | 527 | (uint8_t const *)&roccat_report); |
cb7cf3da SA |
528 | break; |
529 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO: | |
530 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT: | |
531 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH: | |
532 | if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) { | |
533 | roccat_report.type = button_event->type; | |
534 | roccat_report.key = button_event->data1; | |
d2b570a5 SA |
535 | /* |
536 | * pyra reports profile numbers with range 1-5. | |
537 | * Keeping this behaviour. | |
538 | */ | |
539 | roccat_report.value = pyra->actual_profile + 1; | |
cb7cf3da | 540 | roccat_report_event(pyra->chrdev_minor, |
8211e460 | 541 | (uint8_t const *)&roccat_report); |
cb7cf3da SA |
542 | } |
543 | break; | |
544 | } | |
545 | } | |
546 | ||
547 | static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report, | |
548 | u8 *data, int size) | |
549 | { | |
550 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
551 | struct pyra_device *pyra = hid_get_drvdata(hdev); | |
552 | ||
553 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
554 | != USB_INTERFACE_PROTOCOL_MOUSE) | |
555 | return 0; | |
556 | ||
901e64db SA |
557 | if (pyra == NULL) |
558 | return 0; | |
559 | ||
cb7cf3da SA |
560 | pyra_keep_values_up_to_date(pyra, data); |
561 | ||
562 | if (pyra->roccat_claimed) | |
563 | pyra_report_to_chrdev(pyra, data); | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
568 | static const struct hid_device_id pyra_devices[] = { | |
569 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, | |
570 | USB_DEVICE_ID_ROCCAT_PYRA_WIRED) }, | |
3fce2246 SA |
571 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, |
572 | USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) }, | |
cb7cf3da SA |
573 | { } |
574 | }; | |
575 | ||
576 | MODULE_DEVICE_TABLE(hid, pyra_devices); | |
577 | ||
578 | static struct hid_driver pyra_driver = { | |
579 | .name = "pyra", | |
580 | .id_table = pyra_devices, | |
581 | .probe = pyra_probe, | |
582 | .remove = pyra_remove, | |
583 | .raw_event = pyra_raw_event | |
584 | }; | |
585 | ||
586 | static int __init pyra_init(void) | |
587 | { | |
5012aada SA |
588 | int retval; |
589 | ||
590 | /* class name has to be same as driver name */ | |
afdf5dd3 IO |
591 | retval = class_register(&pyra_class); |
592 | if (retval) | |
593 | return retval; | |
5012aada SA |
594 | |
595 | retval = hid_register_driver(&pyra_driver); | |
596 | if (retval) | |
afdf5dd3 | 597 | class_unregister(&pyra_class); |
5012aada | 598 | return retval; |
cb7cf3da SA |
599 | } |
600 | ||
601 | static void __exit pyra_exit(void) | |
602 | { | |
603 | hid_unregister_driver(&pyra_driver); | |
afdf5dd3 | 604 | class_unregister(&pyra_class); |
cb7cf3da SA |
605 | } |
606 | ||
607 | module_init(pyra_init); | |
608 | module_exit(pyra_exit); | |
609 | ||
610 | MODULE_AUTHOR("Stefan Achatz"); | |
611 | MODULE_DESCRIPTION("USB Roccat Pyra driver"); | |
612 | MODULE_LICENSE("GPL v2"); |