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