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