blob: 61b68ca27790d65c270465af7152276d7d7be7c9 [file] [log] [blame]
Martin Rusko68e353f2013-05-28 14:25:15 +02001/*
2 * HID driver for Huion devices not fully compliant with HID standard
3 *
4 * Copyright (c) 2013 Martin Rusko
Nikolai Kondrashove917e982014-07-23 19:31:54 +03005 * Copyright (c) 2014 Nikolai Kondrashov
Martin Rusko68e353f2013-05-28 14:25:15 +02006 */
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 Kondrashovf8dd5cb2014-07-23 19:31:56 +030019#include <asm/unaligned.h>
Martin Rusko68e353f2013-05-28 14:25:15 +020020#include "usbhid/usbhid.h"
21
22#include "hid-ids.h"
23
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +030024/* Report descriptor template placeholder head */
25#define HUION_PH_HEAD 0xFE, 0xED, 0x1D
Martin Rusko68e353f2013-05-28 14:25:15 +020026
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +030027/* Report descriptor template placeholder IDs */
28enum 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 */
41static 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
Nikolai Kondrashov6498d022014-08-11 20:45:32 +030087/* Parameter indices */
88enum huion_prm {
89 HUION_PRM_X_LM = 1,
90 HUION_PRM_Y_LM = 2,
91 HUION_PRM_PRESSURE_LM = 4,
92 HUION_PRM_RESOLUTION = 5,
93 HUION_PRM_NUM
94};
95
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +030096/* Driver data */
97struct huion_drvdata {
98 __u8 *rdesc;
99 unsigned int rsize;
Martin Rusko68e353f2013-05-28 14:25:15 +0200100};
101
102static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc,
103 unsigned int *rsize)
104{
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300105 struct huion_drvdata *drvdata = hid_get_drvdata(hdev);
Martin Rusko68e353f2013-05-28 14:25:15 +0200106 switch (hdev->product) {
Nikolai Kondrashove917e982014-07-23 19:31:54 +0300107 case USB_DEVICE_ID_HUION_TABLET:
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300108 if (drvdata->rdesc != NULL) {
109 rdesc = drvdata->rdesc;
110 *rsize = drvdata->rsize;
Martin Rusko68e353f2013-05-28 14:25:15 +0200111 }
112 break;
113 }
114 return rdesc;
115}
116
117/**
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300118 * Enable fully-functional tablet mode and determine device parameters.
Martin Rusko68e353f2013-05-28 14:25:15 +0200119 *
120 * @hdev: HID device
Martin Rusko68e353f2013-05-28 14:25:15 +0200121 */
122static int huion_tablet_enable(struct hid_device *hdev)
123{
124 int rc;
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300125 struct usb_device *usb_dev = hid_to_usb_dev(hdev);
126 struct huion_drvdata *drvdata = hid_get_drvdata(hdev);
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300127 __le16 *buf = NULL;
128 size_t len;
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300129 s32 params[HUION_PH_ID_NUM];
130 s32 resolution;
131 __u8 *p;
132 s32 v;
Martin Rusko68e353f2013-05-28 14:25:15 +0200133
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300134 /*
135 * Read string descriptor containing tablet parameters. The specific
136 * string descriptor and data were discovered by sniffing the Windows
137 * driver traffic.
138 * NOTE: This enables fully-functional tablet mode.
139 */
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300140 len = HUION_PRM_NUM * sizeof(*buf);
141 buf = kmalloc(len, GFP_KERNEL);
142 if (buf == NULL) {
143 hid_err(hdev, "failed to allocate parameter buffer\n");
144 rc = -ENOMEM;
145 goto cleanup;
146 }
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300147 rc = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
148 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
149 (USB_DT_STRING << 8) + 0x64,
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300150 0x0409, buf, len,
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300151 USB_CTRL_GET_TIMEOUT);
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300152 if (rc == -EPIPE) {
153 hid_err(hdev, "device parameters not found\n");
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300154 rc = -ENODEV;
155 goto cleanup;
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300156 } else if (rc < 0) {
157 hid_err(hdev, "failed to get device parameters: %d\n", rc);
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300158 rc = -ENODEV;
159 goto cleanup;
160 } else if (rc != len) {
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300161 hid_err(hdev, "invalid device parameters\n");
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300162 rc = -ENODEV;
163 goto cleanup;
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300164 }
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300165
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300166 /* Extract device parameters */
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300167 params[HUION_PH_ID_X_LM] = le16_to_cpu(buf[HUION_PRM_X_LM]);
168 params[HUION_PH_ID_Y_LM] = le16_to_cpu(buf[HUION_PRM_Y_LM]);
169 params[HUION_PH_ID_PRESSURE_LM] =
170 le16_to_cpu(buf[HUION_PRM_PRESSURE_LM]);
171 resolution = le16_to_cpu(buf[HUION_PRM_RESOLUTION]);
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300172 if (resolution == 0) {
173 params[HUION_PH_ID_X_PM] = 0;
174 params[HUION_PH_ID_Y_PM] = 0;
175 } else {
176 params[HUION_PH_ID_X_PM] = params[HUION_PH_ID_X_LM] *
177 1000 / resolution;
178 params[HUION_PH_ID_Y_PM] = params[HUION_PH_ID_Y_LM] *
179 1000 / resolution;
180 }
181
182 /* Allocate fixed report descriptor */
183 drvdata->rdesc = devm_kmalloc(&hdev->dev,
184 sizeof(huion_tablet_rdesc_template),
185 GFP_KERNEL);
186 if (drvdata->rdesc == NULL) {
187 hid_err(hdev, "failed to allocate fixed rdesc\n");
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300188 rc = -ENOMEM;
189 goto cleanup;
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300190 }
191 drvdata->rsize = sizeof(huion_tablet_rdesc_template);
192
193 /* Format fixed report descriptor */
194 memcpy(drvdata->rdesc, huion_tablet_rdesc_template,
195 drvdata->rsize);
196 for (p = drvdata->rdesc;
197 p <= drvdata->rdesc + drvdata->rsize - 4;) {
198 if (p[0] == 0xFE && p[1] == 0xED && p[2] == 0x1D &&
199 p[3] < sizeof(params)) {
200 v = params[p[3]];
201 put_unaligned(cpu_to_le32(v), (s32 *)p);
202 p += 4;
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300203 } else {
Nikolai Kondrashov657d6dc2014-08-11 20:45:31 +0300204 p++;
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300205 }
206 }
Martin Rusko68e353f2013-05-28 14:25:15 +0200207
Nikolai Kondrashov6498d022014-08-11 20:45:32 +0300208 rc = 0;
209
210cleanup:
211 kfree(buf);
212 return rc;
Martin Rusko68e353f2013-05-28 14:25:15 +0200213}
214
215static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id)
216{
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300217 int rc;
218 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
219 struct huion_drvdata *drvdata;
Martin Rusko68e353f2013-05-28 14:25:15 +0200220
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300221 /* Allocate and assign driver data */
222 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
223 if (drvdata == NULL) {
224 hid_err(hdev, "failed to allocate driver data\n");
225 return -ENOMEM;
Martin Rusko68e353f2013-05-28 14:25:15 +0200226 }
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300227 hid_set_drvdata(hdev, drvdata);
Martin Rusko68e353f2013-05-28 14:25:15 +0200228
229 switch (id->product) {
Nikolai Kondrashove917e982014-07-23 19:31:54 +0300230 case USB_DEVICE_ID_HUION_TABLET:
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300231 /* If this is the pen interface */
232 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
233 rc = huion_tablet_enable(hdev);
234 if (rc) {
235 hid_err(hdev, "tablet enabling failed\n");
236 return rc;
237 }
Martin Rusko68e353f2013-05-28 14:25:15 +0200238 }
239 break;
240 }
241
Nikolai Kondrashovf8dd5cb2014-07-23 19:31:56 +0300242 rc = hid_parse(hdev);
243 if (rc) {
244 hid_err(hdev, "parse failed\n");
245 return rc;
246 }
247
248 rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
249 if (rc) {
250 hid_err(hdev, "hw start failed\n");
251 return rc;
252 }
253
Martin Rusko68e353f2013-05-28 14:25:15 +0200254 return 0;
Martin Rusko68e353f2013-05-28 14:25:15 +0200255}
256
257static int huion_raw_event(struct hid_device *hdev, struct hid_report *report,
258 u8 *data, int size)
259{
Nikolai Kondrashovfb853292014-07-23 19:31:55 +0300260 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
261
262 /* If this is a pen input report */
263 if (intf->cur_altsetting->desc.bInterfaceNumber == 0 &&
264 report->type == HID_INPUT_REPORT &&
265 report->id == 0x07 && size >= 2)
266 /* Invert the in-range bit */
Martin Rusko68e353f2013-05-28 14:25:15 +0200267 data[1] ^= 0x40;
268
269 return 0;
270}
271
272static const struct hid_device_id huion_devices[] = {
Nikolai Kondrashove917e982014-07-23 19:31:54 +0300273 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_TABLET) },
Nikolai Kondrashov3f1f3332014-07-23 19:31:57 +0300274 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, USB_DEVICE_ID_HUION_TABLET) },
Martin Rusko68e353f2013-05-28 14:25:15 +0200275 { }
276};
277MODULE_DEVICE_TABLE(hid, huion_devices);
278
279static struct hid_driver huion_driver = {
280 .name = "huion",
281 .id_table = huion_devices,
282 .probe = huion_probe,
283 .report_fixup = huion_report_fixup,
284 .raw_event = huion_raw_event,
285};
286module_hid_driver(huion_driver);
287
288MODULE_AUTHOR("Martin Rusko");
289MODULE_DESCRIPTION("Huion HID driver");
290MODULE_LICENSE("GPL");