Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Huion devices not fully compliant with HID standard |
| 3 | * |
| 4 | * Copyright (c) 2013 Martin Rusko |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 5 | * Copyright (c) 2014 Nikolai Kondrashov |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/hid.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/usb.h> |
| 19 | #include "usbhid/usbhid.h" |
| 20 | |
| 21 | #include "hid-ids.h" |
| 22 | |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 23 | /* Original tablet report descriptor size */ |
| 24 | #define HUION_TABLET_RDESC_ORIG_SIZE 177 |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 25 | |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 26 | /* Fixed tablet report descriptor */ |
| 27 | static __u8 huion_tablet_rdesc_fixed[] = { |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 28 | 0x05, 0x0D, /* Usage Page (Digitizer), */ |
| 29 | 0x09, 0x02, /* Usage (Pen), */ |
| 30 | 0xA1, 0x01, /* Collection (Application), */ |
| 31 | 0x85, 0x07, /* Report ID (7), */ |
| 32 | 0x09, 0x20, /* Usage (Stylus), */ |
| 33 | 0xA0, /* Collection (Physical), */ |
| 34 | 0x14, /* Logical Minimum (0), */ |
| 35 | 0x25, 0x01, /* Logical Maximum (1), */ |
| 36 | 0x75, 0x01, /* Report Size (1), */ |
| 37 | 0x09, 0x42, /* Usage (Tip Switch), */ |
| 38 | 0x09, 0x44, /* Usage (Barrel Switch), */ |
| 39 | 0x09, 0x46, /* Usage (Tablet Pick), */ |
| 40 | 0x95, 0x03, /* Report Count (3), */ |
| 41 | 0x81, 0x02, /* Input (Variable), */ |
| 42 | 0x95, 0x03, /* Report Count (3), */ |
| 43 | 0x81, 0x03, /* Input (Constant, Variable), */ |
| 44 | 0x09, 0x32, /* Usage (In Range), */ |
| 45 | 0x95, 0x01, /* Report Count (1), */ |
| 46 | 0x81, 0x02, /* Input (Variable), */ |
| 47 | 0x95, 0x01, /* Report Count (1), */ |
| 48 | 0x81, 0x03, /* Input (Constant, Variable), */ |
| 49 | 0x75, 0x10, /* Report Size (16), */ |
| 50 | 0x95, 0x01, /* Report Count (1), */ |
| 51 | 0xA4, /* Push, */ |
| 52 | 0x05, 0x01, /* Usage Page (Desktop), */ |
| 53 | 0x65, 0x13, /* Unit (Inch), */ |
| 54 | 0x55, 0xFD, /* Unit Exponent (-3), */ |
| 55 | 0x34, /* Physical Minimum (0), */ |
| 56 | 0x09, 0x30, /* Usage (X), */ |
| 57 | 0x46, 0x40, 0x1F, /* Physical Maximum (8000), */ |
| 58 | 0x26, 0x00, 0x7D, /* Logical Maximum (32000), */ |
| 59 | 0x81, 0x02, /* Input (Variable), */ |
| 60 | 0x09, 0x31, /* Usage (Y), */ |
| 61 | 0x46, 0x88, 0x13, /* Physical Maximum (5000), */ |
| 62 | 0x26, 0x20, 0x4E, /* Logical Maximum (20000), */ |
| 63 | 0x81, 0x02, /* Input (Variable), */ |
| 64 | 0xB4, /* Pop, */ |
| 65 | 0x09, 0x30, /* Usage (Tip Pressure), */ |
| 66 | 0x26, 0xFF, 0x07, /* Logical Maximum (2047), */ |
| 67 | 0x81, 0x02, /* Input (Variable), */ |
| 68 | 0xC0, /* End Collection, */ |
| 69 | 0xC0 /* End Collection */ |
| 70 | }; |
| 71 | |
| 72 | static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 73 | unsigned int *rsize) |
| 74 | { |
| 75 | switch (hdev->product) { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 76 | case USB_DEVICE_ID_HUION_TABLET: |
| 77 | if (*rsize == HUION_TABLET_RDESC_ORIG_SIZE) { |
| 78 | rdesc = huion_tablet_rdesc_fixed; |
| 79 | *rsize = sizeof(huion_tablet_rdesc_fixed); |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 80 | } |
| 81 | break; |
| 82 | } |
| 83 | return rdesc; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Enable fully-functional tablet mode by reading special string |
| 88 | * descriptor. |
| 89 | * |
| 90 | * @hdev: HID device |
| 91 | * |
| 92 | * The specific string descriptor and data were discovered by sniffing |
| 93 | * the Windows driver traffic. |
| 94 | */ |
| 95 | static int huion_tablet_enable(struct hid_device *hdev) |
| 96 | { |
| 97 | int rc; |
| 98 | char buf[22]; |
| 99 | |
| 100 | rc = usb_string(hid_to_usb_dev(hdev), 0x64, buf, sizeof(buf)); |
| 101 | if (rc < 0) |
| 102 | return rc; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 108 | { |
| 109 | int ret; |
| 110 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 111 | |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 112 | /* Ignore interfaces 1 (mouse) and 2 (keyboard) for tablet, |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 113 | * as they are not used |
| 114 | */ |
| 115 | switch (id->product) { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 116 | case USB_DEVICE_ID_HUION_TABLET: |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 117 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0x00) |
| 118 | return -ENODEV; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | ret = hid_parse(hdev); |
| 123 | if (ret) { |
| 124 | hid_err(hdev, "parse failed\n"); |
| 125 | goto err; |
| 126 | } |
| 127 | |
| 128 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 129 | if (ret) { |
| 130 | hid_err(hdev, "hw start failed\n"); |
| 131 | goto err; |
| 132 | } |
| 133 | |
| 134 | switch (id->product) { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 135 | case USB_DEVICE_ID_HUION_TABLET: |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 136 | ret = huion_tablet_enable(hdev); |
| 137 | if (ret) { |
| 138 | hid_err(hdev, "tablet enabling failed\n"); |
| 139 | goto enabling_err; |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | enabling_err: |
| 146 | hid_hw_stop(hdev); |
| 147 | err: |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static int huion_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 152 | u8 *data, int size) |
| 153 | { |
| 154 | /* If this is a pen input report then invert the in-range bit */ |
| 155 | if (report->type == HID_INPUT_REPORT && report->id == 0x07 && size >= 2) |
| 156 | data[1] ^= 0x40; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static const struct hid_device_id huion_devices[] = { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame^] | 162 | { HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_TABLET) }, |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 163 | { } |
| 164 | }; |
| 165 | MODULE_DEVICE_TABLE(hid, huion_devices); |
| 166 | |
| 167 | static struct hid_driver huion_driver = { |
| 168 | .name = "huion", |
| 169 | .id_table = huion_devices, |
| 170 | .probe = huion_probe, |
| 171 | .report_fixup = huion_report_fixup, |
| 172 | .raw_event = huion_raw_event, |
| 173 | }; |
| 174 | module_hid_driver(huion_driver); |
| 175 | |
| 176 | MODULE_AUTHOR("Martin Rusko"); |
| 177 | MODULE_DESCRIPTION("Huion HID driver"); |
| 178 | MODULE_LICENSE("GPL"); |