blob: 38280c055a1929a319e32d8111908f580c420f86 [file] [log] [blame]
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +02001/*
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>
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020023#include <linux/module.h>
24#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010025#include <linux/hid-roccat.h>
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020026#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010027#include "hid-roccat-common.h"
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020028#include "hid-roccat-pyra.h"
29
Stefan Achatz14a057f2010-11-26 19:57:38 +000030static uint profile_numbers[5] = {0, 1, 2, 3, 4};
31
Stefan Achatz5012aad2010-11-26 19:57:33 +000032/* pyra_class is used for creating sysfs attributes via roccat char device */
33static struct class *pyra_class;
34
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020035static 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
42static int pyra_send_control(struct usb_device *usb_dev, int value,
43 enum pyra_control_requests request)
44{
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020045 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
Stefan Achatz5772f632011-01-30 13:38:23 +010056 return roccat_common_send(usb_dev, PYRA_USB_COMMAND_CONTROL,
57 &control, sizeof(struct pyra_control));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020058}
59
60static int pyra_receive_control_status(struct usb_device *usb_dev)
61{
Stefan Achatz5772f632011-01-30 13:38:23 +010062 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020063 struct pyra_control control;
64
65 do {
66 msleep(10);
Stefan Achatz5772f632011-01-30 13:38:23 +010067 retval = roccat_common_receive(usb_dev, PYRA_USB_COMMAND_CONTROL,
68 &control, sizeof(struct pyra_control));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020069
70 /* requested too early, try again */
Stefan Achatz5772f632011-01-30 13:38:23 +010071 } while (retval == -EPROTO);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020072
Stefan Achatz5772f632011-01-30 13:38:23 +010073 if (!retval && control.command == PYRA_COMMAND_CONTROL &&
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020074 control.request == PYRA_CONTROL_REQUEST_STATUS &&
75 control.value == 1)
Stefan Achatz5772f632011-01-30 13:38:23 +010076 return 0;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020077 else {
Joe Perches4291ee32010-12-09 19:29:03 -080078 hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
79 control.request, control.value);
Stefan Achatz5772f632011-01-30 13:38:23 +010080 return retval ? retval : -EINVAL;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020081 }
82}
83
84static int pyra_get_profile_settings(struct usb_device *usb_dev,
85 struct pyra_profile_settings *buf, int number)
86{
87 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020088 retval = pyra_send_control(usb_dev, number,
89 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020090 if (retval)
91 return retval;
Stefan Achatz5772f632011-01-30 13:38:23 +010092 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS,
93 buf, sizeof(struct pyra_profile_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020094}
95
96static int pyra_get_profile_buttons(struct usb_device *usb_dev,
97 struct pyra_profile_buttons *buf, int number)
98{
99 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200100 retval = pyra_send_control(usb_dev, number,
101 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200102 if (retval)
103 return retval;
Stefan Achatz5772f632011-01-30 13:38:23 +0100104 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS,
105 buf, sizeof(struct pyra_profile_buttons));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200106}
107
108static int pyra_get_settings(struct usb_device *usb_dev,
109 struct pyra_settings *buf)
110{
Stefan Achatz5772f632011-01-30 13:38:23 +0100111 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_SETTINGS,
112 buf, sizeof(struct pyra_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200113}
114
115static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
116{
Stefan Achatz5772f632011-01-30 13:38:23 +0100117 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_INFO,
118 buf, sizeof(struct pyra_info));
119}
120
121static 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);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200129}
130
131static int pyra_set_profile_settings(struct usb_device *usb_dev,
132 struct pyra_profile_settings const *settings)
133{
Stefan Achatz5772f632011-01-30 13:38:23 +0100134 return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS, settings,
135 sizeof(struct pyra_profile_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200136}
137
138static int pyra_set_profile_buttons(struct usb_device *usb_dev,
139 struct pyra_profile_buttons const *buttons)
140{
Stefan Achatz5772f632011-01-30 13:38:23 +0100141 return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS, buttons,
142 sizeof(struct pyra_profile_buttons));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200143}
144
145static int pyra_set_settings(struct usb_device *usb_dev,
146 struct pyra_settings const *settings)
147{
Stefan Achatz5772f632011-01-30 13:38:23 +0100148 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);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200154}
155
156static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
157 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000158 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200159{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000160 struct device *dev =
161 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200162 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);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000171 memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off,
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200172 count);
173 mutex_unlock(&pyra->pyra_lock);
174
175 return count;
176}
177
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200178static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
179 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000180 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200181{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000182 struct device *dev =
183 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200184 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);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000193 memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off,
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200194 count);
195 mutex_unlock(&pyra->pyra_lock);
196
197 return count;
198}
199
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200200static 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{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000204 struct device *dev =
205 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200206 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
237static 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{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000241 struct device *dev =
242 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200243 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
274static 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{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000278 struct device *dev =
279 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200280 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
295static 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{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000299 struct device *dev =
300 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200301 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
329static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000332 struct pyra_device *pyra =
333 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200334 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
335}
336
337static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000340 struct pyra_device *pyra =
341 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200342 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
343}
344
345static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000348 struct pyra_device *pyra =
349 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200350 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
351}
352
353static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000356 struct pyra_device *pyra =
357 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200358 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
359}
360
Stefan Achatz5012aad2010-11-26 19:57:33 +0000361static 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
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200369};
370
Stefan Achatz5012aad2010-11-26 19:57:33 +0000371static struct bin_attribute pyra_bin_attributes[] = {
372 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200373 .attr = { .name = "profile_settings", .mode = 0220 },
374 .size = sizeof(struct pyra_profile_settings),
375 .write = pyra_sysfs_write_profile_settings
Stefan Achatz5012aad2010-11-26 19:57:33 +0000376 },
377 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200378 .attr = { .name = "profile1_settings", .mode = 0440 },
379 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000380 .read = pyra_sysfs_read_profilex_settings,
381 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000382 },
383 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200384 .attr = { .name = "profile2_settings", .mode = 0440 },
385 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000386 .read = pyra_sysfs_read_profilex_settings,
387 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000388 },
389 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200390 .attr = { .name = "profile3_settings", .mode = 0440 },
391 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000392 .read = pyra_sysfs_read_profilex_settings,
393 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000394 },
395 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200396 .attr = { .name = "profile4_settings", .mode = 0440 },
397 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000398 .read = pyra_sysfs_read_profilex_settings,
399 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000400 },
401 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200402 .attr = { .name = "profile5_settings", .mode = 0440 },
403 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000404 .read = pyra_sysfs_read_profilex_settings,
405 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000406 },
407 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200408 .attr = { .name = "profile_buttons", .mode = 0220 },
409 .size = sizeof(struct pyra_profile_buttons),
410 .write = pyra_sysfs_write_profile_buttons
Stefan Achatz5012aad2010-11-26 19:57:33 +0000411 },
412 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200413 .attr = { .name = "profile1_buttons", .mode = 0440 },
414 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000415 .read = pyra_sysfs_read_profilex_buttons,
416 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000417 },
418 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200419 .attr = { .name = "profile2_buttons", .mode = 0440 },
420 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000421 .read = pyra_sysfs_read_profilex_buttons,
422 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000423 },
424 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200425 .attr = { .name = "profile3_buttons", .mode = 0440 },
426 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000427 .read = pyra_sysfs_read_profilex_buttons,
428 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000429 },
430 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200431 .attr = { .name = "profile4_buttons", .mode = 0440 },
432 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000433 .read = pyra_sysfs_read_profilex_buttons,
434 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000435 },
436 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200437 .attr = { .name = "profile5_buttons", .mode = 0440 },
438 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000439 .read = pyra_sysfs_read_profilex_buttons,
440 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000441 },
442 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200443 .attr = { .name = "settings", .mode = 0660 },
444 .size = sizeof(struct pyra_settings),
445 .read = pyra_sysfs_read_settings,
446 .write = pyra_sysfs_write_settings
Stefan Achatz5012aad2010-11-26 19:57:33 +0000447 },
448 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200449};
450
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200451static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
452 struct pyra_device *pyra)
453{
Stefan Achatz5772f632011-01-30 13:38:23 +0100454 struct pyra_info info;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200455 int retval, i;
456
457 mutex_init(&pyra->pyra_lock);
458
Stefan Achatz5772f632011-01-30 13:38:23 +0100459 retval = pyra_get_info(usb_dev, &info);
460 if (retval)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200461 return retval;
Stefan Achatz5772f632011-01-30 13:38:23 +0100462
463 pyra->firmware_version = info.firmware_version;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200464
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
486static 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800498 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200499 return -ENOMEM;
500 }
501 hid_set_drvdata(hdev, pyra);
502
503 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
504 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800505 hid_err(hdev, "couldn't init struct pyra_device\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200506 goto exit_free;
507 }
508
Stefan Achatz8211e462011-01-30 13:38:25 +0100509 retval = roccat_connect(pyra_class, hdev,
510 sizeof(struct pyra_roccat_report));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200511 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800512 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200513 } else {
514 pyra->chrdev_minor = retval;
515 pyra->roccat_claimed = 1;
516 }
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200517 } else {
518 hid_set_drvdata(hdev, NULL);
519 }
520
521 return 0;
522exit_free:
523 kfree(pyra);
524 return retval;
525}
526
527static 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) {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200534 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
541static 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800547 hid_err(hdev, "parse failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200548 goto exit;
549 }
550
551 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
552 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800553 hid_err(hdev, "hw start failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200554 goto exit;
555 }
556
557 retval = pyra_init_specials(hdev);
558 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800559 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200560 goto exit_stop;
561 }
562 return 0;
563
564exit_stop:
565 hid_hw_stop(hdev);
566exit:
567 return retval;
568}
569
570static void pyra_remove(struct hid_device *hdev)
571{
572 pyra_remove_specials(hdev);
573 hid_hw_stop(hdev);
574}
575
576static 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
596static 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,
Stefan Achatz8211e462011-01-30 13:38:25 +0100614 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200615 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;
Stefan Achatzd2b570a2010-09-01 12:42:23 +0200622 /*
623 * pyra reports profile numbers with range 1-5.
624 * Keeping this behaviour.
625 */
626 roccat_report.value = pyra->actual_profile + 1;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200627 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100628 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200629 }
630 break;
631 }
632}
633
634static 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
652static const struct hid_device_id pyra_devices[] = {
653 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
654 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
Stefan Achatz3fce2242011-03-23 18:11:36 +0100655 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
656 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200657 { }
658};
659
660MODULE_DEVICE_TABLE(hid, pyra_devices);
661
662static struct hid_driver pyra_driver = {
663 .name = "pyra",
664 .id_table = pyra_devices,
665 .probe = pyra_probe,
666 .remove = pyra_remove,
667 .raw_event = pyra_raw_event
668};
669
670static int __init pyra_init(void)
671{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000672 int retval;
673
674 /* class name has to be same as driver name */
675 pyra_class = class_create(THIS_MODULE, "pyra");
676 if (IS_ERR(pyra_class))
677 return PTR_ERR(pyra_class);
678 pyra_class->dev_attrs = pyra_attributes;
679 pyra_class->dev_bin_attrs = pyra_bin_attributes;
680
681 retval = hid_register_driver(&pyra_driver);
682 if (retval)
683 class_destroy(pyra_class);
684 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200685}
686
687static void __exit pyra_exit(void)
688{
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200689 hid_unregister_driver(&pyra_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100690 class_destroy(pyra_class);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200691}
692
693module_init(pyra_init);
694module_exit(pyra_exit);
695
696MODULE_AUTHOR("Stefan Achatz");
697MODULE_DESCRIPTION("USB Roccat Pyra driver");
698MODULE_LICENSE("GPL v2");