GNU Linux-libre 6.1.24-gnu
[releases.git] / drivers / hid / hid-uclogic-params.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  HID driver for UC-Logic devices not fully compliant with HID standard
4  *  - tablet initialization and parameter retrieval
5  *
6  *  Copyright (c) 2018 Nikolai Kondrashov
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  */
15
16 #ifndef _HID_UCLOGIC_PARAMS_H
17 #define _HID_UCLOGIC_PARAMS_H
18
19 #include <linux/usb.h>
20 #include <linux/hid.h>
21
22 #define UCLOGIC_MOUSE_FRAME_QUIRK       BIT(0)
23 #define UCLOGIC_BATTERY_QUIRK           BIT(1)
24
25 /* Types of pen in-range reporting */
26 enum uclogic_params_pen_inrange {
27         /* Normal reports: zero - out of proximity, one - in proximity */
28         UCLOGIC_PARAMS_PEN_INRANGE_NORMAL = 0,
29         /* Inverted reports: zero - in proximity, one - out of proximity */
30         UCLOGIC_PARAMS_PEN_INRANGE_INVERTED,
31         /* No reports */
32         UCLOGIC_PARAMS_PEN_INRANGE_NONE,
33 };
34
35 /* Types of frames */
36 enum uclogic_params_frame_type {
37         /* Frame with buttons */
38         UCLOGIC_PARAMS_FRAME_BUTTONS = 0,
39         /* Frame with buttons and a dial */
40         UCLOGIC_PARAMS_FRAME_DIAL,
41         /* Frame with buttons and a mouse (shaped as a dial + touchpad) */
42         UCLOGIC_PARAMS_FRAME_MOUSE,
43 };
44
45 /*
46  * Pen report's subreport data.
47  */
48 struct uclogic_params_pen_subreport {
49         /*
50          * The value of the second byte of the pen report indicating this
51          * subreport. If zero, the subreport should be considered invalid and
52          * not matched.
53          */
54         __u8 value;
55
56         /*
57          * The ID to be assigned to the report, if the second byte of the pen
58          * report is equal to "value". Only valid if "value" is not zero.
59          */
60         __u8 id;
61 };
62
63 /*
64  * Tablet interface's pen input parameters.
65  *
66  * Must use declarative (descriptive) language, not imperative, to simplify
67  * understanding and maintain consistency.
68  *
69  * Noop (preserving functionality) when filled with zeroes.
70  */
71 struct uclogic_params_pen {
72         /*
73          * True if pen usage is invalid for this interface and should be
74          * ignored, false otherwise.
75          */
76         bool usage_invalid;
77         /*
78          * Pointer to report descriptor part describing the pen inputs.
79          * Allocated with kmalloc. NULL if the part is not specified.
80          */
81         __u8 *desc_ptr;
82         /*
83          * Size of the report descriptor.
84          * Only valid, if "desc_ptr" is not NULL.
85          */
86         unsigned int desc_size;
87         /* Report ID, if reports should be tweaked, zero if not */
88         unsigned int id;
89         /* The list of subreports, only valid if "id" is not zero */
90         struct uclogic_params_pen_subreport subreport_list[3];
91         /* Type of in-range reporting, only valid if "id" is not zero */
92         enum uclogic_params_pen_inrange inrange;
93         /*
94          * True, if reports include fragmented high resolution coords, with
95          * high-order X and then Y bytes following the pressure field.
96          * Only valid if "id" is not zero.
97          */
98         bool fragmented_hires;
99         /*
100          * True if the pen reports tilt in bytes at offset 10 (X) and 11 (Y),
101          * and the Y tilt direction is flipped.
102          * Only valid if "id" is not zero.
103          */
104         bool tilt_y_flipped;
105 };
106
107 /*
108  * Parameters of frame control inputs of a tablet interface.
109  *
110  * Must use declarative (descriptive) language, not imperative, to simplify
111  * understanding and maintain consistency.
112  *
113  * Noop (preserving functionality) when filled with zeroes.
114  */
115 struct uclogic_params_frame {
116         /*
117          * Pointer to report descriptor part describing the frame inputs.
118          * Allocated with kmalloc. NULL if the part is not specified.
119          */
120         __u8 *desc_ptr;
121         /*
122          * Size of the report descriptor.
123          * Only valid, if "desc_ptr" is not NULL.
124          */
125         unsigned int desc_size;
126         /*
127          * Report ID, if reports should be tweaked, zero if not.
128          */
129         unsigned int id;
130         /*
131          * The suffix to add to the input device name, if not NULL.
132          */
133         const char *suffix;
134         /*
135          * Number of the least-significant bit of the 2-bit state of a rotary
136          * encoder, in the report. Cannot point to a 2-bit field crossing a
137          * byte boundary. Zero if not present. Only valid if "id" is not zero.
138          */
139         unsigned int re_lsb;
140         /*
141          * Offset of the Wacom-style device ID byte in the report, to be set
142          * to pad device ID (0xf), for compatibility with Wacom drivers. Zero
143          * if no changes to the report should be made. The ID byte will be set
144          * to zero whenever the byte pointed by "touch_byte" is zero, if
145          * the latter is valid. Only valid if "id" is not zero.
146          */
147         unsigned int dev_id_byte;
148         /*
149          * Offset of the touch ring/strip state byte, in the report.
150          * Zero if not present. If dev_id_byte is also valid and non-zero,
151          * then the device ID byte will be cleared when the byte pointed to by
152          * this offset is zero. Only valid if "id" is not zero.
153          */
154         unsigned int touch_byte;
155         /*
156          * The value to anchor the reversed touch ring/strip reports at.
157          * I.e. one, if the reports should be flipped without offset.
158          * Zero if no reversal should be done.
159          * Only valid if "touch_byte" is valid and not zero.
160          */
161         __s8 touch_flip_at;
162         /*
163          * Maximum value of the touch ring/strip report around which the value
164          * should be wrapped when flipping according to "touch_flip_at".
165          * The minimum valid value is considered to be one, with zero being
166          * out-of-proximity (finger lift) value.
167          * Only valid if "touch_flip_at" is valid and not zero.
168          */
169         __s8 touch_max;
170         /*
171          * Offset of the bitmap dial byte, in the report. Zero if not present.
172          * Only valid if "id" is not zero. A bitmap dial sends reports with a
173          * dedicated bit per direction: 1 means clockwise rotation, 2 means
174          * counterclockwise, as opposed to the normal 1 and -1.
175          */
176         unsigned int bitmap_dial_byte;
177 };
178
179 /*
180  * Tablet interface report parameters.
181  *
182  * Must use declarative (descriptive) language, not imperative, to simplify
183  * understanding and maintain consistency.
184  *
185  * When filled with zeros represents a "noop" configuration - passes all
186  * reports unchanged and lets the generic HID driver handle everything.
187  *
188  * The resulting device report descriptor is assembled from all the report
189  * descriptor parts referenced by the structure. No order of assembly should
190  * be assumed. The structure represents original device report descriptor if
191  * all the parts are NULL.
192  */
193 struct uclogic_params {
194         /*
195          * True if the whole interface is invalid, false otherwise.
196          */
197         bool invalid;
198         /*
199          * Pointer to the common part of the replacement report descriptor,
200          * allocated with kmalloc. NULL if no common part is needed.
201          * Only valid, if "invalid" is false.
202          */
203         __u8 *desc_ptr;
204         /*
205          * Size of the common part of the replacement report descriptor.
206          * Only valid, if "desc_ptr" is valid and not NULL.
207          */
208         unsigned int desc_size;
209         /*
210          * Pen parameters and optional report descriptor part.
211          * Only valid, if "invalid" is false.
212          */
213         struct uclogic_params_pen pen;
214         /*
215          * The list of frame control parameters and optional report descriptor
216          * parts. Only valid, if "invalid" is false.
217          */
218         struct uclogic_params_frame frame_list[3];
219 };
220
221 /* Driver data */
222 struct uclogic_drvdata {
223         /* Interface parameters */
224         struct uclogic_params params;
225         /* Pointer to the replacement report descriptor. NULL if none. */
226         __u8 *desc_ptr;
227         /*
228          * Size of the replacement report descriptor.
229          * Only valid if desc_ptr is not NULL
230          */
231         unsigned int desc_size;
232         /* Pen input device */
233         struct input_dev *pen_input;
234         /* In-range timer */
235         struct timer_list inrange_timer;
236         /* Last rotary encoder state, or U8_MAX for none */
237         u8 re_state;
238         /* Device quirks */
239         unsigned long quirks;
240 };
241
242 /* Initialize a tablet interface and discover its parameters */
243 extern int uclogic_params_init(struct uclogic_params *params,
244                                 struct hid_device *hdev);
245
246 /* Get a replacement report descriptor for a tablet's interface. */
247 extern int uclogic_params_get_desc(const struct uclogic_params *params,
248                                         __u8 **pdesc,
249                                         unsigned int *psize);
250
251 /* Free resources used by tablet interface's parameters */
252 extern void uclogic_params_cleanup(struct uclogic_params *params);
253
254 /* Dump tablet interface parameters with hid_dbg() */
255 extern void uclogic_params_hid_dbg(const struct hid_device *hdev,
256                                         const struct uclogic_params *params);
257
258 #endif /* _HID_UCLOGIC_PARAMS_H */