2 * HID driver for Huion devices not fully compliant with HID standard
4 * Copyright (c) 2013 Martin Rusko
5 * Copyright (c) 2014 Nikolai Kondrashov
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)
15 #include <linux/device.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <asm/unaligned.h>
20 #include "usbhid/usbhid.h"
24 /* Report descriptor template placeholder head */
25 #define HUION_PH_HEAD 0xFE, 0xED, 0x1D
27 /* Report descriptor template placeholder IDs */
33 HUION_PH_ID_PRESSURE_LM
,
37 /* Report descriptor template placeholder */
38 #define HUION_PH(_ID) HUION_PH_HEAD, HUION_PH_ID_##_ID
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), */
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), */
79 0x09, 0x30, /* Usage (Tip Pressure), */
81 HUION_PH(PRESSURE_LM
), /* Logical Maximum (PLACEHOLDER), */
82 0x81, 0x02, /* Input (Variable), */
83 0xC0, /* End Collection, */
84 0xC0 /* End Collection */
88 struct huion_drvdata
{
93 static __u8
*huion_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
96 struct huion_drvdata
*drvdata
= hid_get_drvdata(hdev
);
97 switch (hdev
->product
) {
98 case USB_DEVICE_ID_HUION_TABLET
:
99 if (drvdata
->rdesc
!= NULL
) {
100 rdesc
= drvdata
->rdesc
;
101 *rsize
= drvdata
->rsize
;
109 * Enable fully-functional tablet mode and determine device parameters.
113 static int huion_tablet_enable(struct hid_device
*hdev
)
116 struct usb_device
*usb_dev
= hid_to_usb_dev(hdev
);
117 struct huion_drvdata
*drvdata
= hid_get_drvdata(hdev
);
121 * Read string descriptor containing tablet parameters. The specific
122 * string descriptor and data were discovered by sniffing the Windows
124 * NOTE: This enables fully-functional tablet mode.
126 rc
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
127 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
128 (USB_DT_STRING
<< 8) + 0x64,
129 0x0409, buf
, sizeof(buf
),
130 USB_CTRL_GET_TIMEOUT
);
132 hid_warn(hdev
, "device parameters not found\n");
134 hid_warn(hdev
, "failed to get device parameters: %d\n", rc
);
135 else if (rc
!= sizeof(buf
))
136 hid_warn(hdev
, "invalid device parameters\n");
138 s32 params
[HUION_PH_ID_NUM
];
143 /* Extract device parameters */
144 params
[HUION_PH_ID_X_LM
] = le16_to_cpu(buf
[1]);
145 params
[HUION_PH_ID_Y_LM
] = le16_to_cpu(buf
[2]);
146 params
[HUION_PH_ID_PRESSURE_LM
] = le16_to_cpu(buf
[4]);
147 resolution
= le16_to_cpu(buf
[5]);
148 if (resolution
== 0) {
149 params
[HUION_PH_ID_X_PM
] = 0;
150 params
[HUION_PH_ID_Y_PM
] = 0;
152 params
[HUION_PH_ID_X_PM
] = params
[HUION_PH_ID_X_LM
] *
154 params
[HUION_PH_ID_Y_PM
] = params
[HUION_PH_ID_Y_LM
] *
158 /* Allocate fixed report descriptor */
159 drvdata
->rdesc
= devm_kmalloc(&hdev
->dev
,
160 sizeof(huion_tablet_rdesc_template
),
162 if (drvdata
->rdesc
== NULL
) {
163 hid_err(hdev
, "failed to allocate fixed rdesc\n");
166 drvdata
->rsize
= sizeof(huion_tablet_rdesc_template
);
168 /* Format fixed report descriptor */
169 memcpy(drvdata
->rdesc
, huion_tablet_rdesc_template
,
171 for (p
= drvdata
->rdesc
;
172 p
<= drvdata
->rdesc
+ drvdata
->rsize
- 4;) {
173 if (p
[0] == 0xFE && p
[1] == 0xED && p
[2] == 0x1D &&
174 p
[3] < sizeof(params
)) {
176 put_unaligned(cpu_to_le32(v
), (s32
*)p
);
187 static int huion_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
190 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
191 struct huion_drvdata
*drvdata
;
193 /* Allocate and assign driver data */
194 drvdata
= devm_kzalloc(&hdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
195 if (drvdata
== NULL
) {
196 hid_err(hdev
, "failed to allocate driver data\n");
199 hid_set_drvdata(hdev
, drvdata
);
201 switch (id
->product
) {
202 case USB_DEVICE_ID_HUION_TABLET
:
203 /* If this is the pen interface */
204 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
205 rc
= huion_tablet_enable(hdev
);
207 hid_err(hdev
, "tablet enabling failed\n");
214 rc
= hid_parse(hdev
);
216 hid_err(hdev
, "parse failed\n");
220 rc
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
222 hid_err(hdev
, "hw start failed\n");
229 static int huion_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
232 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
234 /* If this is a pen input report */
235 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0 &&
236 report
->type
== HID_INPUT_REPORT
&&
237 report
->id
== 0x07 && size
>= 2)
238 /* Invert the in-range bit */
244 static const struct hid_device_id huion_devices
[] = {
245 { HID_USB_DEVICE(USB_VENDOR_ID_HUION
, USB_DEVICE_ID_HUION_TABLET
) },
246 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC
, USB_DEVICE_ID_HUION_TABLET
) },
249 MODULE_DEVICE_TABLE(hid
, huion_devices
);
251 static struct hid_driver huion_driver
= {
253 .id_table
= huion_devices
,
254 .probe
= huion_probe
,
255 .report_fixup
= huion_report_fixup
,
256 .raw_event
= huion_raw_event
,
258 module_hid_driver(huion_driver
);
260 MODULE_AUTHOR("Martin Rusko");
261 MODULE_DESCRIPTION("Huion HID driver");
262 MODULE_LICENSE("GPL");