diff options
author | Nikolai Kondrashov <spbnick@gmail.com> | 2014-07-23 12:31:56 -0400 |
---|---|---|
committer | Jiri Kosina <jkosina@suse.cz> | 2014-07-29 05:21:25 -0400 |
commit | f8dd5cb2c6d0dae6585cc2961e1f721fb0fc5cf8 (patch) | |
tree | 1949513780ad16d99971284edb6f83ab81834853 | |
parent | fb853296d8ce3056c3668f2e51dee8533c32a9d5 (diff) |
HID: huion: Switch to generating report descriptor
Switch to generating tablet pen report descriptor from a template and
parameters retrieved from string descriptor 0x64.
Signed-off-by: Nikolai Kondrashov <spbnick@gmail.com>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
-rw-r--r-- | drivers/hid/hid-huion.c | 245 |
1 files changed, 167 insertions, 78 deletions
diff --git a/drivers/hid/hid-huion.c b/drivers/hid/hid-huion.c index 46c425bf4f0d..6c811c1fbf55 100644 --- a/drivers/hid/hid-huion.c +++ b/drivers/hid/hid-huion.c | |||
@@ -16,67 +16,89 @@ | |||
16 | #include <linux/hid.h> | 16 | #include <linux/hid.h> |
17 | #include <linux/module.h> | 17 | #include <linux/module.h> |
18 | #include <linux/usb.h> | 18 | #include <linux/usb.h> |
19 | #include <asm/unaligned.h> | ||
19 | #include "usbhid/usbhid.h" | 20 | #include "usbhid/usbhid.h" |
20 | 21 | ||
21 | #include "hid-ids.h" | 22 | #include "hid-ids.h" |
22 | 23 | ||
23 | /* Original tablet report descriptor size */ | 24 | /* Report descriptor template placeholder head */ |
24 | #define HUION_TABLET_RDESC_ORIG_SIZE 177 | 25 | #define HUION_PH_HEAD 0xFE, 0xED, 0x1D |
25 | 26 | ||
26 | /* Fixed tablet report descriptor */ | 27 | /* Report descriptor template placeholder IDs */ |
27 | static __u8 huion_tablet_rdesc_fixed[] = { | 28 | enum huion_ph_id { |
28 | 0x05, 0x0D, /* Usage Page (Digitizer), */ | 29 | HUION_PH_ID_X_LM, |
29 | 0x09, 0x02, /* Usage (Pen), */ | 30 | HUION_PH_ID_X_PM, |
30 | 0xA1, 0x01, /* Collection (Application), */ | 31 | HUION_PH_ID_Y_LM, |
31 | 0x85, 0x07, /* Report ID (7), */ | 32 | HUION_PH_ID_Y_PM, |
32 | 0x09, 0x20, /* Usage (Stylus), */ | 33 | HUION_PH_ID_PRESSURE_LM, |
33 | 0xA0, /* Collection (Physical), */ | 34 | HUION_PH_ID_NUM |
34 | 0x14, /* Logical Minimum (0), */ | 35 | }; |
35 | 0x25, 0x01, /* Logical Maximum (1), */ | 36 | |
36 | 0x75, 0x01, /* Report Size (1), */ | 37 | /* Report descriptor template placeholder */ |
37 | 0x09, 0x42, /* Usage (Tip Switch), */ | 38 | #define HUION_PH(_ID) HUION_PH_HEAD, HUION_PH_ID_##_ID |
38 | 0x09, 0x44, /* Usage (Barrel Switch), */ | 39 | |
39 | 0x09, 0x46, /* Usage (Tablet Pick), */ | 40 | /* Fixed report descriptor template */ |
40 | 0x95, 0x03, /* Report Count (3), */ | 41 | static const __u8 huion_tablet_rdesc_template[] = { |
41 | 0x81, 0x02, /* Input (Variable), */ | 42 | 0x05, 0x0D, /* Usage Page (Digitizer), */ |
42 | 0x95, 0x03, /* Report Count (3), */ | 43 | 0x09, 0x02, /* Usage (Pen), */ |
43 | 0x81, 0x03, /* Input (Constant, Variable), */ | 44 | 0xA1, 0x01, /* Collection (Application), */ |
44 | 0x09, 0x32, /* Usage (In Range), */ | 45 | 0x85, 0x07, /* Report ID (7), */ |
45 | 0x95, 0x01, /* Report Count (1), */ | 46 | 0x09, 0x20, /* Usage (Stylus), */ |
46 | 0x81, 0x02, /* Input (Variable), */ | 47 | 0xA0, /* Collection (Physical), */ |
47 | 0x95, 0x01, /* Report Count (1), */ | 48 | 0x14, /* Logical Minimum (0), */ |
48 | 0x81, 0x03, /* Input (Constant, Variable), */ | 49 | 0x25, 0x01, /* Logical Maximum (1), */ |
49 | 0x75, 0x10, /* Report Size (16), */ | 50 | 0x75, 0x01, /* Report Size (1), */ |
50 | 0x95, 0x01, /* Report Count (1), */ | 51 | 0x09, 0x42, /* Usage (Tip Switch), */ |
51 | 0xA4, /* Push, */ | 52 | 0x09, 0x44, /* Usage (Barrel Switch), */ |
52 | 0x05, 0x01, /* Usage Page (Desktop), */ | 53 | 0x09, 0x46, /* Usage (Tablet Pick), */ |
53 | 0x65, 0x13, /* Unit (Inch), */ | 54 | 0x95, 0x03, /* Report Count (3), */ |
54 | 0x55, 0xFD, /* Unit Exponent (-3), */ | 55 | 0x81, 0x02, /* Input (Variable), */ |
55 | 0x34, /* Physical Minimum (0), */ | 56 | 0x95, 0x03, /* Report Count (3), */ |
56 | 0x09, 0x30, /* Usage (X), */ | 57 | 0x81, 0x03, /* Input (Constant, Variable), */ |
57 | 0x46, 0x40, 0x1F, /* Physical Maximum (8000), */ | 58 | 0x09, 0x32, /* Usage (In Range), */ |
58 | 0x26, 0x00, 0x7D, /* Logical Maximum (32000), */ | 59 | 0x95, 0x01, /* Report Count (1), */ |
59 | 0x81, 0x02, /* Input (Variable), */ | 60 | 0x81, 0x02, /* Input (Variable), */ |
60 | 0x09, 0x31, /* Usage (Y), */ | 61 | 0x95, 0x01, /* Report Count (1), */ |
61 | 0x46, 0x88, 0x13, /* Physical Maximum (5000), */ | 62 | 0x81, 0x03, /* Input (Constant, Variable), */ |
62 | 0x26, 0x20, 0x4E, /* Logical Maximum (20000), */ | 63 | 0x75, 0x10, /* Report Size (16), */ |
63 | 0x81, 0x02, /* Input (Variable), */ | 64 | 0x95, 0x01, /* Report Count (1), */ |
64 | 0xB4, /* Pop, */ | 65 | 0xA4, /* Push, */ |
65 | 0x09, 0x30, /* Usage (Tip Pressure), */ | 66 | 0x05, 0x01, /* Usage Page (Desktop), */ |
66 | 0x26, 0xFF, 0x07, /* Logical Maximum (2047), */ | 67 | 0x65, 0x13, /* Unit (Inch), */ |
67 | 0x81, 0x02, /* Input (Variable), */ | 68 | 0x55, 0xFD, /* Unit Exponent (-3), */ |
68 | 0xC0, /* End Collection, */ | 69 | 0x34, /* Physical Minimum (0), */ |
69 | 0xC0 /* End Collection */ | 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; | ||
70 | }; | 91 | }; |
71 | 92 | ||
72 | static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc, | 93 | static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
73 | unsigned int *rsize) | 94 | unsigned int *rsize) |
74 | { | 95 | { |
96 | struct huion_drvdata *drvdata = hid_get_drvdata(hdev); | ||
75 | switch (hdev->product) { | 97 | switch (hdev->product) { |
76 | case USB_DEVICE_ID_HUION_TABLET: | 98 | case USB_DEVICE_ID_HUION_TABLET: |
77 | if (*rsize == HUION_TABLET_RDESC_ORIG_SIZE) { | 99 | if (drvdata->rdesc != NULL) { |
78 | rdesc = huion_tablet_rdesc_fixed; | 100 | rdesc = drvdata->rdesc; |
79 | *rsize = sizeof(huion_tablet_rdesc_fixed); | 101 | *rsize = drvdata->rsize; |
80 | } | 102 | } |
81 | break; | 103 | break; |
82 | } | 104 | } |
@@ -84,57 +106,124 @@ static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc, | |||
84 | } | 106 | } |
85 | 107 | ||
86 | /** | 108 | /** |
87 | * Enable fully-functional tablet mode by reading special string | 109 | * Enable fully-functional tablet mode and determine device parameters. |
88 | * descriptor. | ||
89 | * | 110 | * |
90 | * @hdev: HID device | 111 | * @hdev: HID device |
91 | * | ||
92 | * The specific string descriptor and data were discovered by sniffing | ||
93 | * the Windows driver traffic. | ||
94 | */ | 112 | */ |
95 | static int huion_tablet_enable(struct hid_device *hdev) | 113 | static int huion_tablet_enable(struct hid_device *hdev) |
96 | { | 114 | { |
97 | int rc; | 115 | int rc; |
98 | char buf[22]; | 116 | struct usb_device *usb_dev = hid_to_usb_dev(hdev); |
117 | struct huion_drvdata *drvdata = hid_get_drvdata(hdev); | ||
118 | u16 buf[6]; | ||
99 | 119 | ||
100 | rc = usb_string(hid_to_usb_dev(hdev), 0x64, buf, sizeof(buf)); | 120 | /* |
101 | if (rc < 0) | 121 | * Read string descriptor containing tablet parameters. The specific |
102 | return rc; | 122 | * string descriptor and data were discovered by sniffing the Windows |
123 | * driver traffic. | ||
124 | * NOTE: This enables fully-functional tablet mode. | ||
125 | */ | ||
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); | ||
131 | if (rc == -EPIPE) | ||
132 | hid_warn(hdev, "device parameters not found\n"); | ||
133 | else if (rc < 0) | ||
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"); | ||
137 | else { | ||
138 | s32 params[HUION_PH_ID_NUM]; | ||
139 | s32 resolution; | ||
140 | __u8 *p; | ||
141 | s32 v; | ||
142 | |||
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; | ||
151 | } else { | ||
152 | params[HUION_PH_ID_X_PM] = params[HUION_PH_ID_X_LM] * | ||
153 | 1000 / resolution; | ||
154 | params[HUION_PH_ID_Y_PM] = params[HUION_PH_ID_Y_LM] * | ||
155 | 1000 / resolution; | ||
156 | } | ||
157 | |||
158 | /* Allocate fixed report descriptor */ | ||
159 | drvdata->rdesc = devm_kmalloc(&hdev->dev, | ||
160 | sizeof(huion_tablet_rdesc_template), | ||
161 | GFP_KERNEL); | ||
162 | if (drvdata->rdesc == NULL) { | ||
163 | hid_err(hdev, "failed to allocate fixed rdesc\n"); | ||
164 | return -ENOMEM; | ||
165 | } | ||
166 | drvdata->rsize = sizeof(huion_tablet_rdesc_template); | ||
167 | |||
168 | /* Format fixed report descriptor */ | ||
169 | memcpy(drvdata->rdesc, huion_tablet_rdesc_template, | ||
170 | drvdata->rsize); | ||
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)) { | ||
175 | v = params[p[3]]; | ||
176 | put_unaligned(cpu_to_le32(v), (s32 *)p); | ||
177 | p += 4; | ||
178 | } else { | ||
179 | p++; | ||
180 | } | ||
181 | } | ||
182 | } | ||
103 | 183 | ||
104 | return 0; | 184 | return 0; |
105 | } | 185 | } |
106 | 186 | ||
107 | static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id) | 187 | static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id) |
108 | { | 188 | { |
109 | int ret; | 189 | int rc; |
110 | 190 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); | |
111 | ret = hid_parse(hdev); | 191 | struct huion_drvdata *drvdata; |
112 | if (ret) { | ||
113 | hid_err(hdev, "parse failed\n"); | ||
114 | goto err; | ||
115 | } | ||
116 | 192 | ||
117 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | 193 | /* Allocate and assign driver data */ |
118 | if (ret) { | 194 | drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); |
119 | hid_err(hdev, "hw start failed\n"); | 195 | if (drvdata == NULL) { |
120 | goto err; | 196 | hid_err(hdev, "failed to allocate driver data\n"); |
197 | return -ENOMEM; | ||
121 | } | 198 | } |
199 | hid_set_drvdata(hdev, drvdata); | ||
122 | 200 | ||
123 | switch (id->product) { | 201 | switch (id->product) { |
124 | case USB_DEVICE_ID_HUION_TABLET: | 202 | case USB_DEVICE_ID_HUION_TABLET: |
125 | ret = huion_tablet_enable(hdev); | 203 | /* If this is the pen interface */ |
126 | if (ret) { | 204 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) { |
127 | hid_err(hdev, "tablet enabling failed\n"); | 205 | rc = huion_tablet_enable(hdev); |
128 | goto enabling_err; | 206 | if (rc) { |
207 | hid_err(hdev, "tablet enabling failed\n"); | ||
208 | return rc; | ||
209 | } | ||
129 | } | 210 | } |
130 | break; | 211 | break; |
131 | } | 212 | } |
132 | 213 | ||
214 | rc = hid_parse(hdev); | ||
215 | if (rc) { | ||
216 | hid_err(hdev, "parse failed\n"); | ||
217 | return rc; | ||
218 | } | ||
219 | |||
220 | rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT); | ||
221 | if (rc) { | ||
222 | hid_err(hdev, "hw start failed\n"); | ||
223 | return rc; | ||
224 | } | ||
225 | |||
133 | return 0; | 226 | return 0; |
134 | enabling_err: | ||
135 | hid_hw_stop(hdev); | ||
136 | err: | ||
137 | return ret; | ||
138 | } | 227 | } |
139 | 228 | ||
140 | static int huion_raw_event(struct hid_device *hdev, struct hid_report *report, | 229 | static int huion_raw_event(struct hid_device *hdev, struct hid_report *report, |