hid-roccat-kone.c 23.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Roccat Kone driver for Linux
 *
 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 */

/*
 * Roccat Kone is a gamer mouse which consists of a mouse part and a keyboard
 * part. The keyboard part enables the mouse to execute stored macros with mixed
 * key- and button-events.
 *
 * TODO implement on-the-fly polling-rate change
 *      The windows driver has the ability to change the polling rate of the
 *      device on the press of a mousebutton.
 *      Is it possible to remove and reinstall the urb in raw-event- or any
 *      other handler, or to defer this action to be executed somewhere else?
 *
 * TODO is it possible to overwrite group for sysfs attributes via udev?
 */

#include <linux/device.h>
#include <linux/input.h>
#include <linux/hid.h>
#include <linux/module.h>
T
Tejun Heo 已提交
32
#include <linux/slab.h>
33
#include <linux/hid-roccat.h>
34
#include "hid-ids.h"
35
#include "hid-roccat-common.h"
36 37
#include "hid-roccat-kone.h"

38 39
static uint profile_numbers[5] = {0, 1, 2, 3, 4};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static int kone_receive(struct usb_device *usb_dev, uint usb_command,
		void *data, uint size)
{
	char *buf;
	int len;

	buf = kmalloc(size, GFP_KERNEL);
	if (buf == NULL)
		return -ENOMEM;

	len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
			HID_REQ_GET_REPORT,
			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
			usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);

	memcpy(data, buf, size);
	kfree(buf);
	return ((len < 0) ? len : ((len != size) ? -EIO : 0));
}

static int kone_send(struct usb_device *usb_dev, uint usb_command,
		void const *data, uint size)
{
	char *buf;
	int len;

	buf = kmalloc(size, GFP_KERNEL);
	if (buf == NULL)
		return -ENOMEM;

	memcpy(buf, data, size);

	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
			HID_REQ_SET_REPORT,
			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
			usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);

	kfree(buf);
	return ((len < 0) ? len : ((len != size) ? -EIO : 0));
}

81 82 83
/* kone_class is used for creating sysfs attributes via roccat char device */
static struct class *kone_class;

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static void kone_set_settings_checksum(struct kone_settings *settings)
{
	uint16_t checksum = 0;
	unsigned char *address = (unsigned char *)settings;
	int i;

	for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
		checksum += *address;
	settings->checksum = cpu_to_le16(checksum);
}

/*
 * Checks success after writing data to mouse
 * On success returns 0
 * On failure returns errno
 */
static int kone_check_write(struct usb_device *usb_dev)
{
102 103
	int retval;
	uint8_t data;
104 105 106 107 108 109 110 111

	do {
		/*
		 * Mouse needs 50 msecs until it says ok, but there are
		 * 30 more msecs needed for next write to work.
		 */
		msleep(80);

112
		retval = kone_receive(usb_dev,
113 114 115
				kone_command_confirm_write, &data, 1);
		if (retval)
			return retval;
116 117 118 119 120 121

		/*
		 * value of 3 seems to mean something like
		 * "not finished yet, but it looks good"
		 * So check again after a moment.
		 */
122
	} while (data == 3);
123

124
	if (data == 1) /* everything alright */
125
		return 0;
126 127 128 129

	/* unknown answer */
	hid_err(usb_dev, "got retval %d when checking write\n", data);
	return -EIO;
130 131 132 133 134 135 136 137 138 139
}

/*
 * Reads settings from mouse and stores it in @buf
 * On success returns 0
 * On failure returns errno
 */
static int kone_get_settings(struct usb_device *usb_dev,
		struct kone_settings *buf)
{
140
	return kone_receive(usb_dev, kone_command_settings, buf,
141
			sizeof(struct kone_settings));
142 143 144 145 146 147 148 149 150 151
}

/*
 * Writes settings from @buf to mouse
 * On success returns 0
 * On failure returns errno
 */
static int kone_set_settings(struct usb_device *usb_dev,
		struct kone_settings const *settings)
{
152
	int retval;
153
	retval = kone_send(usb_dev, kone_command_settings,
154 155 156 157
			settings, sizeof(struct kone_settings));
	if (retval)
		return retval;
	return kone_check_write(usb_dev);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

/*
 * Reads profile data from mouse and stores it in @buf
 * @number: profile number to read
 * On success returns 0
 * On failure returns errno
 */
static int kone_get_profile(struct usb_device *usb_dev,
		struct kone_profile *buf, int number)
{
	int len;

	if (number < 1 || number > 5)
		return -EINVAL;

	len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
			USB_REQ_CLEAR_FEATURE,
			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
			kone_command_profile, number, buf,
			sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);

	if (len != sizeof(struct kone_profile))
		return -EIO;

	return 0;
}

/*
 * Writes profile data to mouse.
 * @number: profile number to write
 * On success returns 0
 * On failure returns errno
 */
static int kone_set_profile(struct usb_device *usb_dev,
		struct kone_profile const *profile, int number)
{
	int len;

	if (number < 1 || number > 5)
		return -EINVAL;

	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
			USB_REQ_SET_CONFIGURATION,
			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
203
			kone_command_profile, number, (void *)profile,
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
			sizeof(struct kone_profile),
			USB_CTRL_SET_TIMEOUT);

	if (len != sizeof(struct kone_profile))
		return len;

	if (kone_check_write(usb_dev))
		return -EIO;

	return 0;
}

/*
 * Reads value of "fast-clip-weight" and stores it in @result
 * On success returns 0
 * On failure returns errno
 */
static int kone_get_weight(struct usb_device *usb_dev, int *result)
{
223 224
	int retval;
	uint8_t data;
225

226
	retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
227

228 229
	if (retval)
		return retval;
230

231
	*result = (int)data;
232 233 234 235 236 237 238 239 240 241
	return 0;
}

/*
 * Reads firmware_version of mouse and stores it in @result
 * On success returns 0
 * On failure returns errno
 */
static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
{
242 243
	int retval;
	uint16_t data;
244

245
	retval = kone_receive(usb_dev, kone_command_firmware_version,
246 247 248
			&data, 2);
	if (retval)
		return retval;
249

250
	*result = le16_to_cpu(data);
251 252 253
	return 0;
}

254
static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
255 256
		struct bin_attribute *attr, char *buf,
		loff_t off, size_t count) {
257 258
	struct device *dev =
			container_of(kobj, struct device, kobj)->parent->parent;
259 260 261 262 263 264 265 266 267
	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));

	if (off >= sizeof(struct kone_settings))
		return 0;

	if (off + count > sizeof(struct kone_settings))
		count = sizeof(struct kone_settings) - off;

	mutex_lock(&kone->kone_lock);
268
	memcpy(buf, ((char const *)&kone->settings) + off, count);
269 270 271 272 273 274 275 276 277 278
	mutex_unlock(&kone->kone_lock);

	return count;
}

/*
 * Writing settings automatically activates startup_profile.
 * This function keeps values in kone_device up to date and assumes that in
 * case of error the old data is still valid
 */
279
static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
280 281
		struct bin_attribute *attr, char *buf,
		loff_t off, size_t count) {
282 283
	struct device *dev =
			container_of(kobj, struct device, kobj)->parent->parent;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
	int retval = 0, difference;

	/* I need to get my data in one piece */
	if (off != 0 || count != sizeof(struct kone_settings))
		return -EINVAL;

	mutex_lock(&kone->kone_lock);
	difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
	if (difference) {
		retval = kone_set_settings(usb_dev,
				(struct kone_settings const *)buf);
		if (!retval)
			memcpy(&kone->settings, buf,
					sizeof(struct kone_settings));
	}
	mutex_unlock(&kone->kone_lock);

	if (retval)
		return retval;

	/*
	 * If we get here, treat settings as okay and update actual values
	 * according to startup_profile
	 */
	kone->actual_profile = kone->settings.startup_profile;
	kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;

	return sizeof(struct kone_settings);
}

316 317 318
static ssize_t kone_sysfs_read_profilex(struct file *fp,
		struct kobject *kobj, struct bin_attribute *attr,
		char *buf, loff_t off, size_t count) {
319 320
	struct device *dev =
			container_of(kobj, struct device, kobj)->parent->parent;
321 322 323 324 325 326 327 328 329
	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));

	if (off >= sizeof(struct kone_profile))
		return 0;

	if (off + count > sizeof(struct kone_profile))
		count = sizeof(struct kone_profile) - off;

	mutex_lock(&kone->kone_lock);
330
	memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
331 332 333 334 335 336
	mutex_unlock(&kone->kone_lock);

	return count;
}

/* Writes data only if different to stored data */
337 338 339
static ssize_t kone_sysfs_write_profilex(struct file *fp,
		struct kobject *kobj, struct bin_attribute *attr,
		char *buf, loff_t off, size_t count) {
340 341
	struct device *dev =
			container_of(kobj, struct device, kobj)->parent->parent;
342 343 344 345 346 347 348 349 350
	struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
	struct kone_profile *profile;
	int retval = 0, difference;

	/* I need to get my data in one piece */
	if (off != 0 || count != sizeof(struct kone_profile))
		return -EINVAL;

351
	profile = &kone->profiles[*(uint *)(attr->private)];
352 353 354 355 356

	mutex_lock(&kone->kone_lock);
	difference = memcmp(buf, profile, sizeof(struct kone_profile));
	if (difference) {
		retval = kone_set_profile(usb_dev,
357 358
				(struct kone_profile const *)buf,
				*(uint *)(attr->private) + 1);
359 360 361 362 363 364 365 366 367 368 369 370 371 372
		if (!retval)
			memcpy(profile, buf, sizeof(struct kone_profile));
	}
	mutex_unlock(&kone->kone_lock);

	if (retval)
		return retval;

	return sizeof(struct kone_profile);
}

static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
		struct device_attribute *attr, char *buf)
{
373 374
	struct kone_device *kone =
			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
375 376 377 378 379 380
	return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
}

static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
		struct device_attribute *attr, char *buf)
{
381 382
	struct kone_device *kone =
			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
383 384 385 386 387 388 389
	return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
}

/* weight is read each time, since we don't get informed when it's changed */
static ssize_t kone_sysfs_show_weight(struct device *dev,
		struct device_attribute *attr, char *buf)
{
390 391
	struct kone_device *kone;
	struct usb_device *usb_dev;
392 393 394
	int weight = 0;
	int retval;

395 396 397 398
	dev = dev->parent->parent;
	kone = hid_get_drvdata(dev_get_drvdata(dev));
	usb_dev = interface_to_usbdev(to_usb_interface(dev));

399 400 401 402 403 404 405 406 407 408 409 410
	mutex_lock(&kone->kone_lock);
	retval = kone_get_weight(usb_dev, &weight);
	mutex_unlock(&kone->kone_lock);

	if (retval)
		return retval;
	return snprintf(buf, PAGE_SIZE, "%d\n", weight);
}

static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
		struct device_attribute *attr, char *buf)
{
411 412
	struct kone_device *kone =
			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
413 414 415 416 417 418
	return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
}

static ssize_t kone_sysfs_show_tcu(struct device *dev,
		struct device_attribute *attr, char *buf)
{
419 420
	struct kone_device *kone =
			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
421 422 423 424 425
	return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
}

static int kone_tcu_command(struct usb_device *usb_dev, int number)
{
426 427
	unsigned char value;
	value = number;
428
	return kone_send(usb_dev, kone_command_calibrate, &value, 1);
429 430 431 432 433 434 435 436 437
}

/*
 * Calibrating the tcu is the only action that changes settings data inside the
 * mouse, so this data needs to be reread
 */
static ssize_t kone_sysfs_set_tcu(struct device *dev,
		struct device_attribute *attr, char const *buf, size_t size)
{
438 439
	struct kone_device *kone;
	struct usb_device *usb_dev;
440 441 442
	int retval;
	unsigned long state;

443 444 445 446
	dev = dev->parent->parent;
	kone = hid_get_drvdata(dev_get_drvdata(dev));
	usb_dev = interface_to_usbdev(to_usb_interface(dev));

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	retval = strict_strtoul(buf, 10, &state);
	if (retval)
		return retval;

	if (state != 0 && state != 1)
		return -EINVAL;

	mutex_lock(&kone->kone_lock);

	if (state == 1) { /* state activate */
		retval = kone_tcu_command(usb_dev, 1);
		if (retval)
			goto exit_unlock;
		retval = kone_tcu_command(usb_dev, 2);
		if (retval)
			goto exit_unlock;
		ssleep(5); /* tcu needs this time for calibration */
		retval = kone_tcu_command(usb_dev, 3);
		if (retval)
			goto exit_unlock;
		retval = kone_tcu_command(usb_dev, 0);
		if (retval)
			goto exit_unlock;
		retval = kone_tcu_command(usb_dev, 4);
		if (retval)
			goto exit_unlock;
		/*
		 * Kone needs this time to settle things.
		 * Reading settings too early will result in invalid data.
		 * Roccat's driver waits 1 sec, maybe this time could be
		 * shortened.
		 */
		ssleep(1);
	}

	/* calibration changes values in settings, so reread */
	retval = kone_get_settings(usb_dev, &kone->settings);
	if (retval)
		goto exit_no_settings;

	/* only write settings back if activation state is different */
	if (kone->settings.tcu != state) {
		kone->settings.tcu = state;
		kone_set_settings_checksum(&kone->settings);

		retval = kone_set_settings(usb_dev, &kone->settings);
		if (retval) {
494
			hid_err(usb_dev, "couldn't set tcu state\n");
495 496 497 498 499 500 501 502 503 504 505 506 507
			/*
			 * try to reread valid settings into buffer overwriting
			 * first error code
			 */
			retval = kone_get_settings(usb_dev, &kone->settings);
			if (retval)
				goto exit_no_settings;
			goto exit_unlock;
		}
	}

	retval = size;
exit_no_settings:
508
	hid_err(usb_dev, "couldn't read settings\n");
509 510 511 512 513 514 515 516
exit_unlock:
	mutex_unlock(&kone->kone_lock);
	return retval;
}

static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
		struct device_attribute *attr, char *buf)
{
517 518
	struct kone_device *kone =
			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
519 520 521 522 523 524
	return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
}

static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
		struct device_attribute *attr, char const *buf, size_t size)
{
525 526
	struct kone_device *kone;
	struct usb_device *usb_dev;
527 528 529
	int retval;
	unsigned long new_startup_profile;

530 531 532 533
	dev = dev->parent->parent;
	kone = hid_get_drvdata(dev_get_drvdata(dev));
	usb_dev = interface_to_usbdev(to_usb_interface(dev));

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	retval = strict_strtoul(buf, 10, &new_startup_profile);
	if (retval)
		return retval;

	if (new_startup_profile  < 1 || new_startup_profile > 5)
		return -EINVAL;

	mutex_lock(&kone->kone_lock);

	kone->settings.startup_profile = new_startup_profile;
	kone_set_settings_checksum(&kone->settings);

	retval = kone_set_settings(usb_dev, &kone->settings);

	mutex_unlock(&kone->kone_lock);

	if (retval)
		return retval;

	/* changing the startup profile immediately activates this profile */
	kone->actual_profile = new_startup_profile;
	kone->actual_dpi = kone->profiles[kone->actual_profile - 1].startup_dpi;

	return size;
}

560 561 562 563 564 565 566 567
static struct device_attribute kone_attributes[] = {
	/*
	 * Read actual dpi settings.
	 * Returns raw value for further processing. Refer to enum
	 * kone_polling_rates to get real value.
	 */
	__ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL),
	__ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL),
568

569 570 571 572 573 574 575 576
	/*
	 * The mouse can be equipped with one of four supplied weights from 5
	 * to 20 grams which are recognized and its value can be read out.
	 * This returns the raw value reported by the mouse for easy evaluation
	 * by software. Refer to enum kone_weights to get corresponding real
	 * weight.
	 */
	__ATTR(weight, 0440, kone_sysfs_show_weight, NULL),
577

578 579 580 581 582 583 584 585
	/*
	 * Prints firmware version stored in mouse as integer.
	 * The raw value reported by the mouse is returned for easy evaluation,
	 * to get the real version number the decimal point has to be shifted 2
	 * positions to the left. E.g. a value of 138 means 1.38.
	 */
	__ATTR(firmware_version, 0440,
			kone_sysfs_show_firmware_version, NULL),
586

587 588 589 590 591 592
	/*
	 * Prints state of Tracking Control Unit as number where 0 = off and
	 * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
	 * activates the tcu
	 */
	__ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
593

594 595 596 597 598
	/* Prints and takes the number of the profile the mouse starts with */
	__ATTR(startup_profile, 0660,
			kone_sysfs_show_startup_profile,
			kone_sysfs_set_startup_profile),
	__ATTR_NULL
599 600
};

601 602 603 604 605 606 607 608 609 610
static struct bin_attribute kone_bin_attributes[] = {
	{
		.attr = { .name = "settings", .mode = 0660 },
		.size = sizeof(struct kone_settings),
		.read = kone_sysfs_read_settings,
		.write = kone_sysfs_write_settings
	},
	{
		.attr = { .name = "profile1", .mode = 0660 },
		.size = sizeof(struct kone_profile),
611 612 613
		.read = kone_sysfs_read_profilex,
		.write = kone_sysfs_write_profilex,
		.private = &profile_numbers[0]
614 615 616 617
	},
	{
		.attr = { .name = "profile2", .mode = 0660 },
		.size = sizeof(struct kone_profile),
618 619 620
		.read = kone_sysfs_read_profilex,
		.write = kone_sysfs_write_profilex,
		.private = &profile_numbers[1]
621 622 623 624
	},
	{
		.attr = { .name = "profile3", .mode = 0660 },
		.size = sizeof(struct kone_profile),
625 626 627
		.read = kone_sysfs_read_profilex,
		.write = kone_sysfs_write_profilex,
		.private = &profile_numbers[2]
628 629 630 631
	},
	{
		.attr = { .name = "profile4", .mode = 0660 },
		.size = sizeof(struct kone_profile),
632 633 634
		.read = kone_sysfs_read_profilex,
		.write = kone_sysfs_write_profilex,
		.private = &profile_numbers[3]
635 636 637 638
	},
	{
		.attr = { .name = "profile5", .mode = 0660 },
		.size = sizeof(struct kone_profile),
639 640 641
		.read = kone_sysfs_read_profilex,
		.write = kone_sysfs_write_profilex,
		.private = &profile_numbers[4]
642 643
	},
	__ATTR_NULL
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
};

static int kone_init_kone_device_struct(struct usb_device *usb_dev,
		struct kone_device *kone)
{
	uint i;
	int retval;

	mutex_init(&kone->kone_lock);

	for (i = 0; i < 5; ++i) {
		retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
		if (retval)
			return retval;
	}

	retval = kone_get_settings(usb_dev, &kone->settings);
	if (retval)
		return retval;

	retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
	if (retval)
		return retval;

	kone->actual_profile = kone->settings.startup_profile;
	kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;

	return 0;
}

/*
 * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
 * mousepart if usb_hid is compiled into the kernel and kone is compiled as
 * module.
 * Secial behaviour is bound only to mousepart since only mouseevents contain
 * additional notifications.
 */
static int kone_init_specials(struct hid_device *hdev)
{
	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
	struct usb_device *usb_dev = interface_to_usbdev(intf);
	struct kone_device *kone;
	int retval;

	if (intf->cur_altsetting->desc.bInterfaceProtocol
			== USB_INTERFACE_PROTOCOL_MOUSE) {

		kone = kzalloc(sizeof(*kone), GFP_KERNEL);
		if (!kone) {
693
			hid_err(hdev, "can't alloc device descriptor\n");
694 695 696 697 698 699
			return -ENOMEM;
		}
		hid_set_drvdata(hdev, kone);

		retval = kone_init_kone_device_struct(usb_dev, kone);
		if (retval) {
700
			hid_err(hdev, "couldn't init struct kone_device\n");
701 702
			goto exit_free;
		}
703

704 705
		retval = roccat_connect(kone_class, hdev,
				sizeof(struct kone_roccat_report));
706
		if (retval < 0) {
707
			hid_err(hdev, "couldn't init char dev\n");
708 709 710 711 712
			/* be tolerant about not getting chrdev */
		} else {
			kone->roccat_claimed = 1;
			kone->chrdev_minor = retval;
		}
713 714 715 716 717 718 719 720 721 722 723 724 725
	} else {
		hid_set_drvdata(hdev, NULL);
	}

	return 0;
exit_free:
	kfree(kone);
	return retval;
}

static void kone_remove_specials(struct hid_device *hdev)
{
	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
726
	struct kone_device *kone;
727 728 729

	if (intf->cur_altsetting->desc.bInterfaceProtocol
			== USB_INTERFACE_PROTOCOL_MOUSE) {
730 731 732
		kone = hid_get_drvdata(hdev);
		if (kone->roccat_claimed)
			roccat_disconnect(kone->chrdev_minor);
733 734 735 736 737 738 739 740 741 742
		kfree(hid_get_drvdata(hdev));
	}
}

static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
	int retval;

	retval = hid_parse(hdev);
	if (retval) {
743
		hid_err(hdev, "parse failed\n");
744 745 746 747 748
		goto exit;
	}

	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
	if (retval) {
749
		hid_err(hdev, "hw start failed\n");
750 751 752 753 754
		goto exit;
	}

	retval = kone_init_specials(hdev);
	if (retval) {
755
		hid_err(hdev, "couldn't install mouse\n");
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
		goto exit_stop;
	}

	return 0;

exit_stop:
	hid_hw_stop(hdev);
exit:
	return retval;
}

static void kone_remove(struct hid_device *hdev)
{
	kone_remove_specials(hdev);
	hid_hw_stop(hdev);
}

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
/* handle special events and keep actual profile and dpi values up to date */
static void kone_keep_values_up_to_date(struct kone_device *kone,
		struct kone_mouse_event const *event)
{
	switch (event->event) {
	case kone_mouse_event_switch_profile:
	case kone_mouse_event_osd_profile:
		kone->actual_profile = event->value;
		kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
				startup_dpi;
		break;
	case kone_mouse_event_switch_dpi:
	case kone_mouse_event_osd_dpi:
		kone->actual_dpi = event->value;
		break;
	}
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804
static void kone_report_to_chrdev(struct kone_device const *kone,
		struct kone_mouse_event const *event)
{
	struct kone_roccat_report roccat_report;

	switch (event->event) {
	case kone_mouse_event_switch_profile:
	case kone_mouse_event_switch_dpi:
	case kone_mouse_event_osd_profile:
	case kone_mouse_event_osd_dpi:
		roccat_report.event = event->event;
		roccat_report.value = event->value;
		roccat_report.key = 0;
		roccat_report_event(kone->chrdev_minor,
805
				(uint8_t *)&roccat_report);
806 807 808 809 810 811 812
		break;
	case kone_mouse_event_call_overlong_macro:
		if (event->value == kone_keystroke_action_press) {
			roccat_report.event = kone_mouse_event_call_overlong_macro;
			roccat_report.value = kone->actual_profile;
			roccat_report.key = event->macro_key;
			roccat_report_event(kone->chrdev_minor,
813
					(uint8_t *)&roccat_report);
814 815 816 817 818 819
		}
		break;
	}

}

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
/*
 * Is called for keyboard- and mousepart.
 * Only mousepart gets informations about special events in its extended event
 * structure.
 */
static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
		u8 *data, int size)
{
	struct kone_device *kone = hid_get_drvdata(hdev);
	struct kone_mouse_event *event = (struct kone_mouse_event *)data;

	/* keyboard events are always processed by default handler */
	if (size != sizeof(struct kone_mouse_event))
		return 0;

835 836 837
	if (kone == NULL)
		return 0;

838
	/*
839 840
	 * Firmware 1.38 introduced new behaviour for tilt and special buttons.
	 * Pressed button is reported in each movement event.
841 842
	 * Workaround sends only one event per press.
	 */
843 844 845
	if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
		memcpy(&kone->last_mouse_event, event,
				sizeof(struct kone_mouse_event));
846
	else
847
		memset(&event->tilt, 0, 5);
848

849
	kone_keep_values_up_to_date(kone, event);
850

851 852 853
	if (kone->roccat_claimed)
		kone_report_to_chrdev(kone, event);

854
	return 0; /* always do further processing */
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
}

static const struct hid_device_id kone_devices[] = {
	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
	{ }
};

MODULE_DEVICE_TABLE(hid, kone_devices);

static struct hid_driver kone_driver = {
		.name = "kone",
		.id_table = kone_devices,
		.probe = kone_probe,
		.remove = kone_remove,
		.raw_event = kone_raw_event
};

872
static int __init kone_init(void)
873
{
874 875 876 877 878 879 880 881 882 883 884 885 886
	int retval;

	/* class name has to be same as driver name */
	kone_class = class_create(THIS_MODULE, "kone");
	if (IS_ERR(kone_class))
		return PTR_ERR(kone_class);
	kone_class->dev_attrs = kone_attributes;
	kone_class->dev_bin_attrs = kone_bin_attributes;

	retval = hid_register_driver(&kone_driver);
	if (retval)
		class_destroy(kone_class);
	return retval;
887 888
}

889
static void __exit kone_exit(void)
890 891
{
	hid_unregister_driver(&kone_driver);
892
	class_destroy(kone_class);
893 894 895 896 897
}

module_init(kone_init);
module_exit(kone_exit);

898 899 900
MODULE_AUTHOR("Stefan Achatz");
MODULE_DESCRIPTION("USB Roccat Kone driver");
MODULE_LICENSE("GPL v2");