28 #include <linux/device.h>
29 #include <linux/input.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
38 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
40 static void kone_profile_activated(
struct kone_device *kone,
uint new_profile)
46 static void kone_profile_report(
struct kone_device *kone,
uint new_profile)
68 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
72 return ((len < 0) ? len : ((len != size) ? -
EIO : 0));
75 static int kone_send(
struct usb_device *usb_dev,
uint usb_command,
76 void const *data,
uint size)
88 usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
91 return ((len < 0) ? len : ((len != size) ? -
EIO : 0));
95 static struct class *kone_class;
97 static void kone_set_settings_checksum(
struct kone_settings *settings)
100 unsigned char *
address = (
unsigned char *)settings;
104 checksum += *address;
113 static int kone_check_write(
struct usb_device *usb_dev)
125 retval = kone_receive(usb_dev,
141 dev_err(&usb_dev->dev,
"got retval %d when checking write\n", data);
150 static int kone_get_settings(
struct usb_device *usb_dev,
162 static int kone_set_settings(
struct usb_device *usb_dev,
170 return kone_check_write(usb_dev);
179 static int kone_get_profile(
struct usb_device *usb_dev,
184 if (number < 1 || number > 5)
205 static int kone_set_profile(
struct usb_device *usb_dev,
210 if (number < 1 || number > 5)
218 USB_CTRL_SET_TIMEOUT);
223 if (kone_check_write(usb_dev))
234 static int kone_get_weight(
struct usb_device *usb_dev,
int *
result)
253 static int kone_get_firmware_version(
struct usb_device *usb_dev,
int *result)
269 loff_t off,
size_t count) {
294 loff_t off,
size_t count) {
298 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
299 int retval = 0, difference, old_profile;
308 retval = kone_set_settings(usb_dev,
315 old_profile = kone->
settings.startup_profile;
318 kone_profile_activated(kone, kone->
settings.startup_profile);
320 if (kone->
settings.startup_profile != old_profile)
321 kone_profile_report(kone, kone->
settings.startup_profile);
328 static ssize_t kone_sysfs_read_profilex(
struct file *fp,
330 char *buf, loff_t off,
size_t count) {
349 static ssize_t kone_sysfs_write_profilex(
struct file *fp,
351 char *buf, loff_t off,
size_t count) {
355 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
357 int retval = 0, difference;
368 retval = kone_set_profile(usb_dev,
382 static ssize_t kone_sysfs_show_actual_profile(
struct device *dev,
390 static ssize_t kone_sysfs_show_actual_dpi(
struct device *dev,
403 struct usb_device *usb_dev;
407 dev = dev->
parent->parent;
409 usb_dev = interface_to_usbdev(to_usb_interface(dev));
412 retval = kone_get_weight(usb_dev, &weight);
420 static ssize_t kone_sysfs_show_firmware_version(
struct device *dev,
436 static int kone_tcu_command(
struct usb_device *usb_dev,
int number)
451 struct usb_device *usb_dev;
455 dev = dev->
parent->parent;
457 usb_dev = interface_to_usbdev(to_usb_interface(dev));
463 if (state != 0 && state != 1)
469 retval = kone_tcu_command(usb_dev, 1);
472 retval = kone_tcu_command(usb_dev, 2);
476 retval = kone_tcu_command(usb_dev, 3);
479 retval = kone_tcu_command(usb_dev, 0);
482 retval = kone_tcu_command(usb_dev, 4);
495 retval = kone_get_settings(usb_dev, &kone->
settings);
497 goto exit_no_settings;
502 kone_set_settings_checksum(&kone->
settings);
504 retval = kone_set_settings(usb_dev, &kone->
settings);
506 dev_err(&usb_dev->dev,
"couldn't set tcu state\n");
511 retval = kone_get_settings(usb_dev, &kone->
settings);
513 goto exit_no_settings;
517 kone_profile_activated(kone, kone->
settings.startup_profile);
522 dev_err(&usb_dev->dev,
"couldn't read settings\n");
528 static ssize_t kone_sysfs_show_startup_profile(
struct device *dev,
536 static ssize_t kone_sysfs_set_startup_profile(
struct device *dev,
540 struct usb_device *usb_dev;
542 unsigned long new_startup_profile;
544 dev = dev->
parent->parent;
546 usb_dev = interface_to_usbdev(to_usb_interface(dev));
552 if (new_startup_profile < 1 || new_startup_profile > 5)
557 kone->
settings.startup_profile = new_startup_profile;
558 kone_set_settings_checksum(&kone->
settings);
560 retval = kone_set_settings(usb_dev, &kone->
settings);
567 kone_profile_activated(kone, new_startup_profile);
568 kone_profile_report(kone, new_startup_profile);
580 __ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi,
NULL),
590 __ATTR(weight, 0440, kone_sysfs_show_weight,
NULL),
599 kone_sysfs_show_firmware_version,
NULL),
606 __ATTR(
tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu),
610 kone_sysfs_show_startup_profile,
611 kone_sysfs_set_startup_profile),
617 .attr = { .name =
"settings", .mode = 0660 },
619 .
read = kone_sysfs_read_settings,
620 .write = kone_sysfs_write_settings
623 .attr = { .name =
"profile1", .mode = 0660 },
625 .
read = kone_sysfs_read_profilex,
626 .write = kone_sysfs_write_profilex,
627 .private = &profile_numbers[0]
630 .attr = { .name =
"profile2", .mode = 0660 },
632 .
read = kone_sysfs_read_profilex,
633 .write = kone_sysfs_write_profilex,
634 .private = &profile_numbers[1]
637 .attr = { .name =
"profile3", .mode = 0660 },
639 .
read = kone_sysfs_read_profilex,
640 .write = kone_sysfs_write_profilex,
641 .private = &profile_numbers[2]
644 .attr = { .name =
"profile4", .mode = 0660 },
646 .
read = kone_sysfs_read_profilex,
647 .write = kone_sysfs_write_profilex,
648 .private = &profile_numbers[3]
651 .attr = { .name =
"profile5", .mode = 0660 },
653 .
read = kone_sysfs_read_profilex,
654 .write = kone_sysfs_write_profilex,
655 .private = &profile_numbers[4]
660 static int kone_init_kone_device_struct(
struct usb_device *usb_dev,
668 for (i = 0; i < 5; ++
i) {
669 retval = kone_get_profile(usb_dev, &kone->
profiles[i], i + 1);
674 retval = kone_get_settings(usb_dev, &kone->
settings);
682 kone_profile_activated(kone, kone->
settings.startup_profile);
694 static int kone_init_specials(
struct hid_device *hdev)
697 struct usb_device *usb_dev = interface_to_usbdev(intf);
701 if (intf->cur_altsetting->desc.bInterfaceProtocol
706 hid_err(hdev,
"can't alloc device descriptor\n");
709 hid_set_drvdata(hdev, kone);
711 retval = kone_init_kone_device_struct(usb_dev, kone);
713 hid_err(hdev,
"couldn't init struct kone_device\n");
720 hid_err(hdev,
"couldn't init char dev\n");
727 hid_set_drvdata(hdev,
NULL);
736 static void kone_remove_specials(
struct hid_device *hdev)
741 if (intf->cur_altsetting->desc.bInterfaceProtocol
743 kone = hid_get_drvdata(hdev);
746 kfree(hid_get_drvdata(hdev));
754 retval = hid_parse(hdev);
756 hid_err(hdev,
"parse failed\n");
762 hid_err(hdev,
"hw start failed\n");
766 retval = kone_init_specials(hdev);
768 hid_err(hdev,
"couldn't install mouse\n");
780 static void kone_remove(
struct hid_device *hdev)
782 kone_remove_specials(hdev);
787 static void kone_keep_values_up_to_date(
struct kone_device *kone,
790 switch (event->
event) {
804 static void kone_report_to_chrdev(
struct kone_device const *kone,
809 switch (event->
event) {
862 kone_keep_values_up_to_date(kone, event);
865 kone_report_to_chrdev(kone, event);
879 .id_table = kone_devices,
881 .remove = kone_remove,
882 .raw_event = kone_raw_event
885 static int __init kone_init(
void)
891 if (IS_ERR(kone_class))
892 return PTR_ERR(kone_class);
902 static void __exit kone_exit(
void)