]>
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> | |
8211e460 | 25 | #include <linux/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 | { | |
cb7cf3da SA |
45 | struct pyra_control control; |
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 | ||
52 | control.command = PYRA_COMMAND_CONTROL; | |
53 | control.value = value; | |
54 | control.request = request; | |
55 | ||
5772f636 SA |
56 | return roccat_common_send(usb_dev, PYRA_USB_COMMAND_CONTROL, |
57 | &control, sizeof(struct pyra_control)); | |
cb7cf3da SA |
58 | } |
59 | ||
60 | static int pyra_receive_control_status(struct usb_device *usb_dev) | |
61 | { | |
5772f636 | 62 | int retval; |
cb7cf3da SA |
63 | struct pyra_control control; |
64 | ||
65 | do { | |
66 | msleep(10); | |
5772f636 SA |
67 | retval = roccat_common_receive(usb_dev, PYRA_USB_COMMAND_CONTROL, |
68 | &control, sizeof(struct pyra_control)); | |
cb7cf3da SA |
69 | |
70 | /* requested too early, try again */ | |
5772f636 | 71 | } while (retval == -EPROTO); |
cb7cf3da | 72 | |
5772f636 | 73 | if (!retval && control.command == PYRA_COMMAND_CONTROL && |
cb7cf3da SA |
74 | control.request == PYRA_CONTROL_REQUEST_STATUS && |
75 | control.value == 1) | |
5772f636 | 76 | return 0; |
cb7cf3da | 77 | else { |
4291ee30 JP |
78 | hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n", |
79 | control.request, control.value); | |
5772f636 | 80 | return retval ? retval : -EINVAL; |
cb7cf3da SA |
81 | } |
82 | } | |
83 | ||
84 | static int pyra_get_profile_settings(struct usb_device *usb_dev, | |
85 | struct pyra_profile_settings *buf, int number) | |
86 | { | |
87 | int retval; | |
cb7cf3da SA |
88 | retval = pyra_send_control(usb_dev, number, |
89 | PYRA_CONTROL_REQUEST_PROFILE_SETTINGS); | |
cb7cf3da SA |
90 | if (retval) |
91 | return retval; | |
5772f636 SA |
92 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS, |
93 | buf, sizeof(struct pyra_profile_settings)); | |
cb7cf3da SA |
94 | } |
95 | ||
96 | static int pyra_get_profile_buttons(struct usb_device *usb_dev, | |
97 | struct pyra_profile_buttons *buf, int number) | |
98 | { | |
99 | int retval; | |
cb7cf3da SA |
100 | retval = pyra_send_control(usb_dev, number, |
101 | PYRA_CONTROL_REQUEST_PROFILE_BUTTONS); | |
cb7cf3da SA |
102 | if (retval) |
103 | return retval; | |
5772f636 SA |
104 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS, |
105 | buf, sizeof(struct pyra_profile_buttons)); | |
cb7cf3da SA |
106 | } |
107 | ||
108 | static int pyra_get_settings(struct usb_device *usb_dev, | |
109 | struct pyra_settings *buf) | |
110 | { | |
5772f636 SA |
111 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_SETTINGS, |
112 | buf, sizeof(struct pyra_settings)); | |
cb7cf3da SA |
113 | } |
114 | ||
115 | static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf) | |
116 | { | |
5772f636 SA |
117 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_INFO, |
118 | buf, sizeof(struct pyra_info)); | |
119 | } | |
120 | ||
121 | static int pyra_send(struct usb_device *usb_dev, uint command, | |
122 | void const *buf, uint size) | |
123 | { | |
124 | int retval; | |
125 | retval = roccat_common_send(usb_dev, command, buf, size); | |
126 | if (retval) | |
127 | return retval; | |
128 | return pyra_receive_control_status(usb_dev); | |
cb7cf3da SA |
129 | } |
130 | ||
131 | static int pyra_set_profile_settings(struct usb_device *usb_dev, | |
132 | struct pyra_profile_settings const *settings) | |
133 | { | |
5772f636 SA |
134 | return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS, settings, |
135 | sizeof(struct pyra_profile_settings)); | |
cb7cf3da SA |
136 | } |
137 | ||
138 | static int pyra_set_profile_buttons(struct usb_device *usb_dev, | |
139 | struct pyra_profile_buttons const *buttons) | |
140 | { | |
5772f636 SA |
141 | return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS, buttons, |
142 | sizeof(struct pyra_profile_buttons)); | |
cb7cf3da SA |
143 | } |
144 | ||
145 | static int pyra_set_settings(struct usb_device *usb_dev, | |
146 | struct pyra_settings const *settings) | |
147 | { | |
5772f636 SA |
148 | int retval; |
149 | retval = roccat_common_send(usb_dev, PYRA_USB_COMMAND_SETTINGS, settings, | |
150 | sizeof(struct pyra_settings)); | |
151 | if (retval) | |
152 | return retval; | |
153 | return pyra_receive_control_status(usb_dev); | |
cb7cf3da SA |
154 | } |
155 | ||
156 | static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp, | |
157 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
14a057f8 | 158 | loff_t off, size_t count) |
cb7cf3da | 159 | { |
5012aada SA |
160 | struct device *dev = |
161 | container_of(kobj, struct device, kobj)->parent->parent; | |
cb7cf3da SA |
162 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
163 | ||
164 | if (off >= sizeof(struct pyra_profile_settings)) | |
165 | return 0; | |
166 | ||
167 | if (off + count > sizeof(struct pyra_profile_settings)) | |
168 | count = sizeof(struct pyra_profile_settings) - off; | |
169 | ||
170 | mutex_lock(&pyra->pyra_lock); | |
14a057f8 | 171 | memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off, |
cb7cf3da SA |
172 | count); |
173 | mutex_unlock(&pyra->pyra_lock); | |
174 | ||
175 | return count; | |
176 | } | |
177 | ||
cb7cf3da SA |
178 | static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp, |
179 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
14a057f8 | 180 | loff_t off, size_t count) |
cb7cf3da | 181 | { |
5012aada SA |
182 | struct device *dev = |
183 | container_of(kobj, struct device, kobj)->parent->parent; | |
cb7cf3da SA |
184 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
185 | ||
186 | if (off >= sizeof(struct pyra_profile_buttons)) | |
187 | return 0; | |
188 | ||
189 | if (off + count > sizeof(struct pyra_profile_buttons)) | |
190 | count = sizeof(struct pyra_profile_buttons) - off; | |
191 | ||
192 | mutex_lock(&pyra->pyra_lock); | |
14a057f8 | 193 | memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off, |
cb7cf3da SA |
194 | count); |
195 | mutex_unlock(&pyra->pyra_lock); | |
196 | ||
197 | return count; | |
198 | } | |
199 | ||
cb7cf3da SA |
200 | static ssize_t pyra_sysfs_write_profile_settings(struct file *fp, |
201 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
202 | loff_t off, size_t count) | |
203 | { | |
5012aada SA |
204 | struct device *dev = |
205 | container_of(kobj, struct device, kobj)->parent->parent; | |
cb7cf3da SA |
206 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
207 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
208 | int retval = 0; | |
209 | int difference; | |
210 | int profile_number; | |
211 | struct pyra_profile_settings *profile_settings; | |
212 | ||
213 | if (off != 0 || count != sizeof(struct pyra_profile_settings)) | |
214 | return -EINVAL; | |
215 | ||
216 | profile_number = ((struct pyra_profile_settings const *)buf)->number; | |
217 | profile_settings = &pyra->profile_settings[profile_number]; | |
218 | ||
219 | mutex_lock(&pyra->pyra_lock); | |
220 | difference = memcmp(buf, profile_settings, | |
221 | sizeof(struct pyra_profile_settings)); | |
222 | if (difference) { | |
223 | retval = pyra_set_profile_settings(usb_dev, | |
224 | (struct pyra_profile_settings const *)buf); | |
225 | if (!retval) | |
226 | memcpy(profile_settings, buf, | |
227 | sizeof(struct pyra_profile_settings)); | |
228 | } | |
229 | mutex_unlock(&pyra->pyra_lock); | |
230 | ||
231 | if (retval) | |
232 | return retval; | |
233 | ||
234 | return sizeof(struct pyra_profile_settings); | |
235 | } | |
236 | ||
237 | static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp, | |
238 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
239 | loff_t off, size_t count) | |
240 | { | |
5012aada SA |
241 | struct device *dev = |
242 | container_of(kobj, struct device, kobj)->parent->parent; | |
cb7cf3da SA |
243 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
244 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
245 | int retval = 0; | |
246 | int difference; | |
247 | int profile_number; | |
248 | struct pyra_profile_buttons *profile_buttons; | |
249 | ||
250 | if (off != 0 || count != sizeof(struct pyra_profile_buttons)) | |
251 | return -EINVAL; | |
252 | ||
253 | profile_number = ((struct pyra_profile_buttons const *)buf)->number; | |
254 | profile_buttons = &pyra->profile_buttons[profile_number]; | |
255 | ||
256 | mutex_lock(&pyra->pyra_lock); | |
257 | difference = memcmp(buf, profile_buttons, | |
258 | sizeof(struct pyra_profile_buttons)); | |
259 | if (difference) { | |
260 | retval = pyra_set_profile_buttons(usb_dev, | |
261 | (struct pyra_profile_buttons const *)buf); | |
262 | if (!retval) | |
263 | memcpy(profile_buttons, buf, | |
264 | sizeof(struct pyra_profile_buttons)); | |
265 | } | |
266 | mutex_unlock(&pyra->pyra_lock); | |
267 | ||
268 | if (retval) | |
269 | return retval; | |
270 | ||
271 | return sizeof(struct pyra_profile_buttons); | |
272 | } | |
273 | ||
274 | static ssize_t pyra_sysfs_read_settings(struct file *fp, | |
275 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
276 | loff_t off, size_t count) | |
277 | { | |
5012aada SA |
278 | struct device *dev = |
279 | container_of(kobj, struct device, kobj)->parent->parent; | |
cb7cf3da SA |
280 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
281 | ||
282 | if (off >= sizeof(struct pyra_settings)) | |
283 | return 0; | |
284 | ||
285 | if (off + count > sizeof(struct pyra_settings)) | |
286 | count = sizeof(struct pyra_settings) - off; | |
287 | ||
288 | mutex_lock(&pyra->pyra_lock); | |
289 | memcpy(buf, ((char const *)&pyra->settings) + off, count); | |
290 | mutex_unlock(&pyra->pyra_lock); | |
291 | ||
292 | return count; | |
293 | } | |
294 | ||
295 | static ssize_t pyra_sysfs_write_settings(struct file *fp, | |
296 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
297 | loff_t off, size_t count) | |
298 | { | |
5012aada SA |
299 | struct device *dev = |
300 | container_of(kobj, struct device, kobj)->parent->parent; | |
cb7cf3da SA |
301 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
302 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); | |
303 | int retval = 0; | |
304 | int difference; | |
305 | ||
306 | if (off != 0 || count != sizeof(struct pyra_settings)) | |
307 | return -EINVAL; | |
308 | ||
309 | mutex_lock(&pyra->pyra_lock); | |
310 | difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings)); | |
311 | if (difference) { | |
312 | retval = pyra_set_settings(usb_dev, | |
313 | (struct pyra_settings const *)buf); | |
314 | if (!retval) | |
315 | memcpy(&pyra->settings, buf, | |
316 | sizeof(struct pyra_settings)); | |
317 | } | |
318 | mutex_unlock(&pyra->pyra_lock); | |
319 | ||
320 | if (retval) | |
321 | return retval; | |
322 | ||
323 | profile_activated(pyra, pyra->settings.startup_profile); | |
324 | ||
325 | return sizeof(struct pyra_settings); | |
326 | } | |
327 | ||
328 | ||
329 | static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev, | |
330 | struct device_attribute *attr, char *buf) | |
331 | { | |
5012aada SA |
332 | struct pyra_device *pyra = |
333 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
cb7cf3da SA |
334 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi); |
335 | } | |
336 | ||
337 | static ssize_t pyra_sysfs_show_actual_profile(struct device *dev, | |
338 | struct device_attribute *attr, char *buf) | |
339 | { | |
5012aada SA |
340 | struct pyra_device *pyra = |
341 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
cb7cf3da SA |
342 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile); |
343 | } | |
344 | ||
345 | static ssize_t pyra_sysfs_show_firmware_version(struct device *dev, | |
346 | struct device_attribute *attr, char *buf) | |
347 | { | |
5012aada SA |
348 | struct pyra_device *pyra = |
349 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
cb7cf3da SA |
350 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version); |
351 | } | |
352 | ||
353 | static ssize_t pyra_sysfs_show_startup_profile(struct device *dev, | |
354 | struct device_attribute *attr, char *buf) | |
355 | { | |
5012aada SA |
356 | struct pyra_device *pyra = |
357 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); | |
cb7cf3da SA |
358 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile); |
359 | } | |
360 | ||
5012aada SA |
361 | static struct device_attribute pyra_attributes[] = { |
362 | __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL), | |
363 | __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL), | |
364 | __ATTR(firmware_version, 0440, | |
365 | pyra_sysfs_show_firmware_version, NULL), | |
366 | __ATTR(startup_profile, 0440, | |
367 | pyra_sysfs_show_startup_profile, NULL), | |
368 | __ATTR_NULL | |
cb7cf3da SA |
369 | }; |
370 | ||
5012aada SA |
371 | static struct bin_attribute pyra_bin_attributes[] = { |
372 | { | |
cb7cf3da SA |
373 | .attr = { .name = "profile_settings", .mode = 0220 }, |
374 | .size = sizeof(struct pyra_profile_settings), | |
375 | .write = pyra_sysfs_write_profile_settings | |
5012aada SA |
376 | }, |
377 | { | |
cb7cf3da SA |
378 | .attr = { .name = "profile1_settings", .mode = 0440 }, |
379 | .size = sizeof(struct pyra_profile_settings), | |
14a057f8 SA |
380 | .read = pyra_sysfs_read_profilex_settings, |
381 | .private = &profile_numbers[0] | |
5012aada SA |
382 | }, |
383 | { | |
cb7cf3da SA |
384 | .attr = { .name = "profile2_settings", .mode = 0440 }, |
385 | .size = sizeof(struct pyra_profile_settings), | |
14a057f8 SA |
386 | .read = pyra_sysfs_read_profilex_settings, |
387 | .private = &profile_numbers[1] | |
5012aada SA |
388 | }, |
389 | { | |
cb7cf3da SA |
390 | .attr = { .name = "profile3_settings", .mode = 0440 }, |
391 | .size = sizeof(struct pyra_profile_settings), | |
14a057f8 SA |
392 | .read = pyra_sysfs_read_profilex_settings, |
393 | .private = &profile_numbers[2] | |
5012aada SA |
394 | }, |
395 | { | |
cb7cf3da SA |
396 | .attr = { .name = "profile4_settings", .mode = 0440 }, |
397 | .size = sizeof(struct pyra_profile_settings), | |
14a057f8 SA |
398 | .read = pyra_sysfs_read_profilex_settings, |
399 | .private = &profile_numbers[3] | |
5012aada SA |
400 | }, |
401 | { | |
cb7cf3da SA |
402 | .attr = { .name = "profile5_settings", .mode = 0440 }, |
403 | .size = sizeof(struct pyra_profile_settings), | |
14a057f8 SA |
404 | .read = pyra_sysfs_read_profilex_settings, |
405 | .private = &profile_numbers[4] | |
5012aada SA |
406 | }, |
407 | { | |
cb7cf3da SA |
408 | .attr = { .name = "profile_buttons", .mode = 0220 }, |
409 | .size = sizeof(struct pyra_profile_buttons), | |
410 | .write = pyra_sysfs_write_profile_buttons | |
5012aada SA |
411 | }, |
412 | { | |
cb7cf3da SA |
413 | .attr = { .name = "profile1_buttons", .mode = 0440 }, |
414 | .size = sizeof(struct pyra_profile_buttons), | |
14a057f8 SA |
415 | .read = pyra_sysfs_read_profilex_buttons, |
416 | .private = &profile_numbers[0] | |
5012aada SA |
417 | }, |
418 | { | |
cb7cf3da SA |
419 | .attr = { .name = "profile2_buttons", .mode = 0440 }, |
420 | .size = sizeof(struct pyra_profile_buttons), | |
14a057f8 SA |
421 | .read = pyra_sysfs_read_profilex_buttons, |
422 | .private = &profile_numbers[1] | |
5012aada SA |
423 | }, |
424 | { | |
cb7cf3da SA |
425 | .attr = { .name = "profile3_buttons", .mode = 0440 }, |
426 | .size = sizeof(struct pyra_profile_buttons), | |
14a057f8 SA |
427 | .read = pyra_sysfs_read_profilex_buttons, |
428 | .private = &profile_numbers[2] | |
5012aada SA |
429 | }, |
430 | { | |
cb7cf3da SA |
431 | .attr = { .name = "profile4_buttons", .mode = 0440 }, |
432 | .size = sizeof(struct pyra_profile_buttons), | |
14a057f8 SA |
433 | .read = pyra_sysfs_read_profilex_buttons, |
434 | .private = &profile_numbers[3] | |
5012aada SA |
435 | }, |
436 | { | |
cb7cf3da SA |
437 | .attr = { .name = "profile5_buttons", .mode = 0440 }, |
438 | .size = sizeof(struct pyra_profile_buttons), | |
14a057f8 SA |
439 | .read = pyra_sysfs_read_profilex_buttons, |
440 | .private = &profile_numbers[4] | |
5012aada SA |
441 | }, |
442 | { | |
cb7cf3da SA |
443 | .attr = { .name = "settings", .mode = 0660 }, |
444 | .size = sizeof(struct pyra_settings), | |
445 | .read = pyra_sysfs_read_settings, | |
446 | .write = pyra_sysfs_write_settings | |
5012aada SA |
447 | }, |
448 | __ATTR_NULL | |
cb7cf3da SA |
449 | }; |
450 | ||
cb7cf3da SA |
451 | static int pyra_init_pyra_device_struct(struct usb_device *usb_dev, |
452 | struct pyra_device *pyra) | |
453 | { | |
5772f636 | 454 | struct pyra_info info; |
cb7cf3da SA |
455 | int retval, i; |
456 | ||
457 | mutex_init(&pyra->pyra_lock); | |
458 | ||
5772f636 SA |
459 | retval = pyra_get_info(usb_dev, &info); |
460 | if (retval) | |
cb7cf3da | 461 | return retval; |
5772f636 SA |
462 | |
463 | pyra->firmware_version = info.firmware_version; | |
cb7cf3da SA |
464 | |
465 | retval = pyra_get_settings(usb_dev, &pyra->settings); | |
466 | if (retval) | |
467 | return retval; | |
468 | ||
469 | for (i = 0; i < 5; ++i) { | |
470 | retval = pyra_get_profile_settings(usb_dev, | |
471 | &pyra->profile_settings[i], i); | |
472 | if (retval) | |
473 | return retval; | |
474 | ||
475 | retval = pyra_get_profile_buttons(usb_dev, | |
476 | &pyra->profile_buttons[i], i); | |
477 | if (retval) | |
478 | return retval; | |
479 | } | |
480 | ||
481 | profile_activated(pyra, pyra->settings.startup_profile); | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
486 | static int pyra_init_specials(struct hid_device *hdev) | |
487 | { | |
488 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
489 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
490 | struct pyra_device *pyra; | |
491 | int retval; | |
492 | ||
493 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
494 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
495 | ||
496 | pyra = kzalloc(sizeof(*pyra), GFP_KERNEL); | |
497 | if (!pyra) { | |
4291ee30 | 498 | hid_err(hdev, "can't alloc device descriptor\n"); |
cb7cf3da SA |
499 | return -ENOMEM; |
500 | } | |
501 | hid_set_drvdata(hdev, pyra); | |
502 | ||
503 | retval = pyra_init_pyra_device_struct(usb_dev, pyra); | |
504 | if (retval) { | |
4291ee30 | 505 | hid_err(hdev, "couldn't init struct pyra_device\n"); |
cb7cf3da SA |
506 | goto exit_free; |
507 | } | |
508 | ||
8211e460 SA |
509 | retval = roccat_connect(pyra_class, hdev, |
510 | sizeof(struct pyra_roccat_report)); | |
cb7cf3da | 511 | if (retval < 0) { |
4291ee30 | 512 | hid_err(hdev, "couldn't init char dev\n"); |
cb7cf3da SA |
513 | } else { |
514 | pyra->chrdev_minor = retval; | |
515 | pyra->roccat_claimed = 1; | |
516 | } | |
cb7cf3da SA |
517 | } else { |
518 | hid_set_drvdata(hdev, NULL); | |
519 | } | |
520 | ||
521 | return 0; | |
522 | exit_free: | |
523 | kfree(pyra); | |
524 | return retval; | |
525 | } | |
526 | ||
527 | static void pyra_remove_specials(struct hid_device *hdev) | |
528 | { | |
529 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
530 | struct pyra_device *pyra; | |
531 | ||
532 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
533 | == USB_INTERFACE_PROTOCOL_MOUSE) { | |
cb7cf3da SA |
534 | pyra = hid_get_drvdata(hdev); |
535 | if (pyra->roccat_claimed) | |
536 | roccat_disconnect(pyra->chrdev_minor); | |
537 | kfree(hid_get_drvdata(hdev)); | |
538 | } | |
539 | } | |
540 | ||
541 | static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
542 | { | |
543 | int retval; | |
544 | ||
545 | retval = hid_parse(hdev); | |
546 | if (retval) { | |
4291ee30 | 547 | hid_err(hdev, "parse failed\n"); |
cb7cf3da SA |
548 | goto exit; |
549 | } | |
550 | ||
551 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
552 | if (retval) { | |
4291ee30 | 553 | hid_err(hdev, "hw start failed\n"); |
cb7cf3da SA |
554 | goto exit; |
555 | } | |
556 | ||
557 | retval = pyra_init_specials(hdev); | |
558 | if (retval) { | |
4291ee30 | 559 | hid_err(hdev, "couldn't install mouse\n"); |
cb7cf3da SA |
560 | goto exit_stop; |
561 | } | |
562 | return 0; | |
563 | ||
564 | exit_stop: | |
565 | hid_hw_stop(hdev); | |
566 | exit: | |
567 | return retval; | |
568 | } | |
569 | ||
570 | static void pyra_remove(struct hid_device *hdev) | |
571 | { | |
572 | pyra_remove_specials(hdev); | |
573 | hid_hw_stop(hdev); | |
574 | } | |
575 | ||
576 | static void pyra_keep_values_up_to_date(struct pyra_device *pyra, | |
577 | u8 const *data) | |
578 | { | |
579 | struct pyra_mouse_event_button const *button_event; | |
580 | ||
581 | switch (data[0]) { | |
582 | case PYRA_MOUSE_REPORT_NUMBER_BUTTON: | |
583 | button_event = (struct pyra_mouse_event_button const *)data; | |
584 | switch (button_event->type) { | |
585 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: | |
586 | profile_activated(pyra, button_event->data1 - 1); | |
587 | break; | |
588 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: | |
589 | pyra->actual_cpi = button_event->data1; | |
590 | break; | |
591 | } | |
592 | break; | |
593 | } | |
594 | } | |
595 | ||
596 | static void pyra_report_to_chrdev(struct pyra_device const *pyra, | |
597 | u8 const *data) | |
598 | { | |
599 | struct pyra_roccat_report roccat_report; | |
600 | struct pyra_mouse_event_button const *button_event; | |
601 | ||
602 | if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON) | |
603 | return; | |
604 | ||
605 | button_event = (struct pyra_mouse_event_button const *)data; | |
606 | ||
607 | switch (button_event->type) { | |
608 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: | |
609 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: | |
610 | roccat_report.type = button_event->type; | |
611 | roccat_report.value = button_event->data1; | |
612 | roccat_report.key = 0; | |
613 | roccat_report_event(pyra->chrdev_minor, | |
8211e460 | 614 | (uint8_t const *)&roccat_report); |
cb7cf3da SA |
615 | break; |
616 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO: | |
617 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT: | |
618 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH: | |
619 | if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) { | |
620 | roccat_report.type = button_event->type; | |
621 | roccat_report.key = button_event->data1; | |
d2b570a5 SA |
622 | /* |
623 | * pyra reports profile numbers with range 1-5. | |
624 | * Keeping this behaviour. | |
625 | */ | |
626 | roccat_report.value = pyra->actual_profile + 1; | |
cb7cf3da | 627 | roccat_report_event(pyra->chrdev_minor, |
8211e460 | 628 | (uint8_t const *)&roccat_report); |
cb7cf3da SA |
629 | } |
630 | break; | |
631 | } | |
632 | } | |
633 | ||
634 | static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report, | |
635 | u8 *data, int size) | |
636 | { | |
637 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
638 | struct pyra_device *pyra = hid_get_drvdata(hdev); | |
639 | ||
640 | if (intf->cur_altsetting->desc.bInterfaceProtocol | |
641 | != USB_INTERFACE_PROTOCOL_MOUSE) | |
642 | return 0; | |
643 | ||
644 | pyra_keep_values_up_to_date(pyra, data); | |
645 | ||
646 | if (pyra->roccat_claimed) | |
647 | pyra_report_to_chrdev(pyra, data); | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
652 | static const struct hid_device_id pyra_devices[] = { | |
653 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, | |
654 | USB_DEVICE_ID_ROCCAT_PYRA_WIRED) }, | |
655 | /* TODO add USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS after testing */ | |
656 | { } | |
657 | }; | |
658 | ||
659 | MODULE_DEVICE_TABLE(hid, pyra_devices); | |
660 | ||
661 | static struct hid_driver pyra_driver = { | |
662 | .name = "pyra", | |
663 | .id_table = pyra_devices, | |
664 | .probe = pyra_probe, | |
665 | .remove = pyra_remove, | |
666 | .raw_event = pyra_raw_event | |
667 | }; | |
668 | ||
669 | static int __init pyra_init(void) | |
670 | { | |
5012aada SA |
671 | int retval; |
672 | ||
673 | /* class name has to be same as driver name */ | |
674 | pyra_class = class_create(THIS_MODULE, "pyra"); | |
675 | if (IS_ERR(pyra_class)) | |
676 | return PTR_ERR(pyra_class); | |
677 | pyra_class->dev_attrs = pyra_attributes; | |
678 | pyra_class->dev_bin_attrs = pyra_bin_attributes; | |
679 | ||
680 | retval = hid_register_driver(&pyra_driver); | |
681 | if (retval) | |
682 | class_destroy(pyra_class); | |
683 | return retval; | |
cb7cf3da SA |
684 | } |
685 | ||
686 | static void __exit pyra_exit(void) | |
687 | { | |
688 | hid_unregister_driver(&pyra_driver); | |
74b643da | 689 | class_destroy(pyra_class); |
cb7cf3da SA |
690 | } |
691 | ||
692 | module_init(pyra_init); | |
693 | module_exit(pyra_exit); | |
694 | ||
695 | MODULE_AUTHOR("Stefan Achatz"); | |
696 | MODULE_DESCRIPTION("USB Roccat Pyra driver"); | |
697 | MODULE_LICENSE("GPL v2"); |