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> |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 19 | #include <asm/unaligned.h> |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 20 | #include "usbhid/usbhid.h" |
| 21 | |
| 22 | #include "hid-ids.h" |
| 23 | |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 24 | /* Report descriptor template placeholder head */ |
| 25 | #define HUION_PH_HEAD 0xFE, 0xED, 0x1D |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 26 | |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 27 | /* Report descriptor template placeholder IDs */ |
| 28 | enum huion_ph_id { |
| 29 | HUION_PH_ID_X_LM, |
| 30 | HUION_PH_ID_X_PM, |
| 31 | HUION_PH_ID_Y_LM, |
| 32 | HUION_PH_ID_Y_PM, |
| 33 | HUION_PH_ID_PRESSURE_LM, |
| 34 | HUION_PH_ID_NUM |
| 35 | }; |
| 36 | |
| 37 | /* Report descriptor template placeholder */ |
| 38 | #define HUION_PH(_ID) HUION_PH_HEAD, HUION_PH_ID_##_ID |
| 39 | |
| 40 | /* Fixed report descriptor template */ |
| 41 | static const __u8 huion_tablet_rdesc_template[] = { |
| 42 | 0x05, 0x0D, /* Usage Page (Digitizer), */ |
| 43 | 0x09, 0x02, /* Usage (Pen), */ |
| 44 | 0xA1, 0x01, /* Collection (Application), */ |
| 45 | 0x85, 0x07, /* Report ID (7), */ |
| 46 | 0x09, 0x20, /* Usage (Stylus), */ |
| 47 | 0xA0, /* Collection (Physical), */ |
| 48 | 0x14, /* Logical Minimum (0), */ |
| 49 | 0x25, 0x01, /* Logical Maximum (1), */ |
| 50 | 0x75, 0x01, /* Report Size (1), */ |
| 51 | 0x09, 0x42, /* Usage (Tip Switch), */ |
| 52 | 0x09, 0x44, /* Usage (Barrel Switch), */ |
| 53 | 0x09, 0x46, /* Usage (Tablet Pick), */ |
| 54 | 0x95, 0x03, /* Report Count (3), */ |
| 55 | 0x81, 0x02, /* Input (Variable), */ |
| 56 | 0x95, 0x03, /* Report Count (3), */ |
| 57 | 0x81, 0x03, /* Input (Constant, Variable), */ |
| 58 | 0x09, 0x32, /* Usage (In Range), */ |
| 59 | 0x95, 0x01, /* Report Count (1), */ |
| 60 | 0x81, 0x02, /* Input (Variable), */ |
| 61 | 0x95, 0x01, /* Report Count (1), */ |
| 62 | 0x81, 0x03, /* Input (Constant, Variable), */ |
| 63 | 0x75, 0x10, /* Report Size (16), */ |
| 64 | 0x95, 0x01, /* Report Count (1), */ |
| 65 | 0xA4, /* Push, */ |
| 66 | 0x05, 0x01, /* Usage Page (Desktop), */ |
| 67 | 0x65, 0x13, /* Unit (Inch), */ |
| 68 | 0x55, 0xFD, /* Unit Exponent (-3), */ |
| 69 | 0x34, /* Physical Minimum (0), */ |
| 70 | 0x09, 0x30, /* Usage (X), */ |
| 71 | 0x27, HUION_PH(X_LM), /* Logical Maximum (PLACEHOLDER), */ |
| 72 | 0x47, HUION_PH(X_PM), /* Physical Maximum (PLACEHOLDER), */ |
| 73 | 0x81, 0x02, /* Input (Variable), */ |
| 74 | 0x09, 0x31, /* Usage (Y), */ |
| 75 | 0x27, HUION_PH(Y_LM), /* Logical Maximum (PLACEHOLDER), */ |
| 76 | 0x47, HUION_PH(Y_PM), /* Physical Maximum (PLACEHOLDER), */ |
| 77 | 0x81, 0x02, /* Input (Variable), */ |
| 78 | 0xB4, /* Pop, */ |
| 79 | 0x09, 0x30, /* Usage (Tip Pressure), */ |
| 80 | 0x27, |
| 81 | HUION_PH(PRESSURE_LM), /* Logical Maximum (PLACEHOLDER), */ |
| 82 | 0x81, 0x02, /* Input (Variable), */ |
| 83 | 0xC0, /* End Collection, */ |
| 84 | 0xC0 /* End Collection */ |
| 85 | }; |
| 86 | |
| 87 | /* Driver data */ |
| 88 | struct huion_drvdata { |
| 89 | __u8 *rdesc; |
| 90 | unsigned int rsize; |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 94 | unsigned int *rsize) |
| 95 | { |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 96 | struct huion_drvdata *drvdata = hid_get_drvdata(hdev); |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 97 | switch (hdev->product) { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame] | 98 | case USB_DEVICE_ID_HUION_TABLET: |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 99 | if (drvdata->rdesc != NULL) { |
| 100 | rdesc = drvdata->rdesc; |
| 101 | *rsize = drvdata->rsize; |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 102 | } |
| 103 | break; |
| 104 | } |
| 105 | return rdesc; |
| 106 | } |
| 107 | |
| 108 | /** |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 109 | * Enable fully-functional tablet mode and determine device parameters. |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 110 | * |
| 111 | * @hdev: HID device |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 112 | */ |
| 113 | static int huion_tablet_enable(struct hid_device *hdev) |
| 114 | { |
| 115 | int rc; |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 116 | struct usb_device *usb_dev = hid_to_usb_dev(hdev); |
| 117 | struct huion_drvdata *drvdata = hid_get_drvdata(hdev); |
Nikolai Kondrashov | f20f6ec | 2014-07-29 15:50:06 +0300 | [diff] [blame] | 118 | __le16 buf[6]; |
Nikolai Kondrashov | 657d6dc | 2014-08-11 20:45:31 +0300 | [diff] [blame^] | 119 | s32 params[HUION_PH_ID_NUM]; |
| 120 | s32 resolution; |
| 121 | __u8 *p; |
| 122 | s32 v; |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 123 | |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 124 | /* |
| 125 | * Read string descriptor containing tablet parameters. The specific |
| 126 | * string descriptor and data were discovered by sniffing the Windows |
| 127 | * driver traffic. |
| 128 | * NOTE: This enables fully-functional tablet mode. |
| 129 | */ |
| 130 | rc = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), |
| 131 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 132 | (USB_DT_STRING << 8) + 0x64, |
| 133 | 0x0409, buf, sizeof(buf), |
| 134 | USB_CTRL_GET_TIMEOUT); |
Nikolai Kondrashov | 657d6dc | 2014-08-11 20:45:31 +0300 | [diff] [blame^] | 135 | if (rc == -EPIPE) { |
| 136 | hid_err(hdev, "device parameters not found\n"); |
| 137 | return -ENODEV; |
| 138 | } else if (rc < 0) { |
| 139 | hid_err(hdev, "failed to get device parameters: %d\n", rc); |
| 140 | return -ENODEV; |
| 141 | } else if (rc != sizeof(buf)) { |
| 142 | hid_err(hdev, "invalid device parameters\n"); |
| 143 | return -ENODEV; |
| 144 | } |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 145 | |
Nikolai Kondrashov | 657d6dc | 2014-08-11 20:45:31 +0300 | [diff] [blame^] | 146 | /* Extract device parameters */ |
| 147 | params[HUION_PH_ID_X_LM] = le16_to_cpu(buf[1]); |
| 148 | params[HUION_PH_ID_Y_LM] = le16_to_cpu(buf[2]); |
| 149 | params[HUION_PH_ID_PRESSURE_LM] = le16_to_cpu(buf[4]); |
| 150 | resolution = le16_to_cpu(buf[5]); |
| 151 | if (resolution == 0) { |
| 152 | params[HUION_PH_ID_X_PM] = 0; |
| 153 | params[HUION_PH_ID_Y_PM] = 0; |
| 154 | } else { |
| 155 | params[HUION_PH_ID_X_PM] = params[HUION_PH_ID_X_LM] * |
| 156 | 1000 / resolution; |
| 157 | params[HUION_PH_ID_Y_PM] = params[HUION_PH_ID_Y_LM] * |
| 158 | 1000 / resolution; |
| 159 | } |
| 160 | |
| 161 | /* Allocate fixed report descriptor */ |
| 162 | drvdata->rdesc = devm_kmalloc(&hdev->dev, |
| 163 | sizeof(huion_tablet_rdesc_template), |
| 164 | GFP_KERNEL); |
| 165 | if (drvdata->rdesc == NULL) { |
| 166 | hid_err(hdev, "failed to allocate fixed rdesc\n"); |
| 167 | return -ENOMEM; |
| 168 | } |
| 169 | drvdata->rsize = sizeof(huion_tablet_rdesc_template); |
| 170 | |
| 171 | /* Format fixed report descriptor */ |
| 172 | memcpy(drvdata->rdesc, huion_tablet_rdesc_template, |
| 173 | drvdata->rsize); |
| 174 | for (p = drvdata->rdesc; |
| 175 | p <= drvdata->rdesc + drvdata->rsize - 4;) { |
| 176 | if (p[0] == 0xFE && p[1] == 0xED && p[2] == 0x1D && |
| 177 | p[3] < sizeof(params)) { |
| 178 | v = params[p[3]]; |
| 179 | put_unaligned(cpu_to_le32(v), (s32 *)p); |
| 180 | p += 4; |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 181 | } else { |
Nikolai Kondrashov | 657d6dc | 2014-08-11 20:45:31 +0300 | [diff] [blame^] | 182 | p++; |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 183 | } |
| 184 | } |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 190 | { |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 191 | int rc; |
| 192 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 193 | struct huion_drvdata *drvdata; |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 194 | |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 195 | /* Allocate and assign driver data */ |
| 196 | drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 197 | if (drvdata == NULL) { |
| 198 | hid_err(hdev, "failed to allocate driver data\n"); |
| 199 | return -ENOMEM; |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 200 | } |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 201 | hid_set_drvdata(hdev, drvdata); |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 202 | |
| 203 | switch (id->product) { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame] | 204 | case USB_DEVICE_ID_HUION_TABLET: |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 205 | /* If this is the pen interface */ |
| 206 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) { |
| 207 | rc = huion_tablet_enable(hdev); |
| 208 | if (rc) { |
| 209 | hid_err(hdev, "tablet enabling failed\n"); |
| 210 | return rc; |
| 211 | } |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 212 | } |
| 213 | break; |
| 214 | } |
| 215 | |
Nikolai Kondrashov | f8dd5cb | 2014-07-23 19:31:56 +0300 | [diff] [blame] | 216 | rc = hid_parse(hdev); |
| 217 | if (rc) { |
| 218 | hid_err(hdev, "parse failed\n"); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 223 | if (rc) { |
| 224 | hid_err(hdev, "hw start failed\n"); |
| 225 | return rc; |
| 226 | } |
| 227 | |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 228 | return 0; |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int huion_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 232 | u8 *data, int size) |
| 233 | { |
Nikolai Kondrashov | fb85329 | 2014-07-23 19:31:55 +0300 | [diff] [blame] | 234 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 235 | |
| 236 | /* If this is a pen input report */ |
| 237 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0 && |
| 238 | report->type == HID_INPUT_REPORT && |
| 239 | report->id == 0x07 && size >= 2) |
| 240 | /* Invert the in-range bit */ |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 241 | data[1] ^= 0x40; |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static const struct hid_device_id huion_devices[] = { |
Nikolai Kondrashov | e917e98 | 2014-07-23 19:31:54 +0300 | [diff] [blame] | 247 | { HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_TABLET) }, |
Nikolai Kondrashov | 3f1f333 | 2014-07-23 19:31:57 +0300 | [diff] [blame] | 248 | { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_HUION_TABLET) }, |
Martin Rusko | 68e353f | 2013-05-28 14:25:15 +0200 | [diff] [blame] | 249 | { } |
| 250 | }; |
| 251 | MODULE_DEVICE_TABLE(hid, huion_devices); |
| 252 | |
| 253 | static struct hid_driver huion_driver = { |
| 254 | .name = "huion", |
| 255 | .id_table = huion_devices, |
| 256 | .probe = huion_probe, |
| 257 | .report_fixup = huion_report_fixup, |
| 258 | .raw_event = huion_raw_event, |
| 259 | }; |
| 260 | module_hid_driver(huion_driver); |
| 261 | |
| 262 | MODULE_AUTHOR("Martin Rusko"); |
| 263 | MODULE_DESCRIPTION("Huion HID driver"); |
| 264 | MODULE_LICENSE("GPL"); |