1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0+ */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * HID driver for UC-Logic devices not fully compliant with HID standard 4*4882a593Smuzhiyun * - tablet initialization and parameter retrieval 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copyright (c) 2018 Nikolai Kondrashov 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun /* 10*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it 11*4882a593Smuzhiyun * under the terms of the GNU General Public License as published by the Free 12*4882a593Smuzhiyun * Software Foundation; either version 2 of the License, or (at your option) 13*4882a593Smuzhiyun * any later version. 14*4882a593Smuzhiyun */ 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun #ifndef _HID_UCLOGIC_PARAMS_H 17*4882a593Smuzhiyun #define _HID_UCLOGIC_PARAMS_H 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun #include <linux/usb.h> 20*4882a593Smuzhiyun #include <linux/hid.h> 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun /* Types of pen in-range reporting */ 23*4882a593Smuzhiyun enum uclogic_params_pen_inrange { 24*4882a593Smuzhiyun /* Normal reports: zero - out of proximity, one - in proximity */ 25*4882a593Smuzhiyun UCLOGIC_PARAMS_PEN_INRANGE_NORMAL = 0, 26*4882a593Smuzhiyun /* Inverted reports: zero - in proximity, one - out of proximity */ 27*4882a593Smuzhiyun UCLOGIC_PARAMS_PEN_INRANGE_INVERTED, 28*4882a593Smuzhiyun /* No reports */ 29*4882a593Smuzhiyun UCLOGIC_PARAMS_PEN_INRANGE_NONE, 30*4882a593Smuzhiyun }; 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun /* Convert a pen in-range reporting type to a string */ 33*4882a593Smuzhiyun extern const char *uclogic_params_pen_inrange_to_str( 34*4882a593Smuzhiyun enum uclogic_params_pen_inrange inrange); 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun /* 37*4882a593Smuzhiyun * Tablet interface's pen input parameters. 38*4882a593Smuzhiyun * 39*4882a593Smuzhiyun * Must use declarative (descriptive) language, not imperative, to simplify 40*4882a593Smuzhiyun * understanding and maintain consistency. 41*4882a593Smuzhiyun * 42*4882a593Smuzhiyun * Noop (preserving functionality) when filled with zeroes. 43*4882a593Smuzhiyun */ 44*4882a593Smuzhiyun struct uclogic_params_pen { 45*4882a593Smuzhiyun /* 46*4882a593Smuzhiyun * Pointer to report descriptor describing the inputs. 47*4882a593Smuzhiyun * Allocated with kmalloc. 48*4882a593Smuzhiyun */ 49*4882a593Smuzhiyun __u8 *desc_ptr; 50*4882a593Smuzhiyun /* 51*4882a593Smuzhiyun * Size of the report descriptor. 52*4882a593Smuzhiyun * Only valid, if "desc_ptr" is not NULL. 53*4882a593Smuzhiyun */ 54*4882a593Smuzhiyun unsigned int desc_size; 55*4882a593Smuzhiyun /* Report ID, if reports should be tweaked, zero if not */ 56*4882a593Smuzhiyun unsigned int id; 57*4882a593Smuzhiyun /* Type of in-range reporting, only valid if "id" is not zero */ 58*4882a593Smuzhiyun enum uclogic_params_pen_inrange inrange; 59*4882a593Smuzhiyun /* 60*4882a593Smuzhiyun * True, if reports include fragmented high resolution coords, with 61*4882a593Smuzhiyun * high-order X and then Y bytes following the pressure field. 62*4882a593Smuzhiyun * Only valid if "id" is not zero. 63*4882a593Smuzhiyun */ 64*4882a593Smuzhiyun bool fragmented_hires; 65*4882a593Smuzhiyun }; 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun /* 68*4882a593Smuzhiyun * Parameters of frame control inputs of a tablet interface. 69*4882a593Smuzhiyun * 70*4882a593Smuzhiyun * Must use declarative (descriptive) language, not imperative, to simplify 71*4882a593Smuzhiyun * understanding and maintain consistency. 72*4882a593Smuzhiyun * 73*4882a593Smuzhiyun * Noop (preserving functionality) when filled with zeroes. 74*4882a593Smuzhiyun */ 75*4882a593Smuzhiyun struct uclogic_params_frame { 76*4882a593Smuzhiyun /* 77*4882a593Smuzhiyun * Pointer to report descriptor describing the inputs. 78*4882a593Smuzhiyun * Allocated with kmalloc. 79*4882a593Smuzhiyun */ 80*4882a593Smuzhiyun __u8 *desc_ptr; 81*4882a593Smuzhiyun /* 82*4882a593Smuzhiyun * Size of the report descriptor. 83*4882a593Smuzhiyun * Only valid, if "desc_ptr" is not NULL. 84*4882a593Smuzhiyun */ 85*4882a593Smuzhiyun unsigned int desc_size; 86*4882a593Smuzhiyun /* 87*4882a593Smuzhiyun * Report ID, if reports should be tweaked, zero if not. 88*4882a593Smuzhiyun */ 89*4882a593Smuzhiyun unsigned int id; 90*4882a593Smuzhiyun /* 91*4882a593Smuzhiyun * Number of the least-significant bit of the 2-bit state of a rotary 92*4882a593Smuzhiyun * encoder, in the report. Cannot point to a 2-bit field crossing a 93*4882a593Smuzhiyun * byte boundary. Zero if not present. Only valid if "id" is not zero. 94*4882a593Smuzhiyun */ 95*4882a593Smuzhiyun unsigned int re_lsb; 96*4882a593Smuzhiyun /* 97*4882a593Smuzhiyun * Offset of the Wacom-style device ID byte in the report, to be set 98*4882a593Smuzhiyun * to pad device ID (0xf), for compatibility with Wacom drivers. Zero 99*4882a593Smuzhiyun * if no changes to the report should be made. Only valid if "id" is 100*4882a593Smuzhiyun * not zero. 101*4882a593Smuzhiyun */ 102*4882a593Smuzhiyun unsigned int dev_id_byte; 103*4882a593Smuzhiyun }; 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun /* 106*4882a593Smuzhiyun * Tablet interface report parameters. 107*4882a593Smuzhiyun * 108*4882a593Smuzhiyun * Must use declarative (descriptive) language, not imperative, to simplify 109*4882a593Smuzhiyun * understanding and maintain consistency. 110*4882a593Smuzhiyun * 111*4882a593Smuzhiyun * When filled with zeros represents a "noop" configuration - passes all 112*4882a593Smuzhiyun * reports unchanged and lets the generic HID driver handle everything. 113*4882a593Smuzhiyun * 114*4882a593Smuzhiyun * The resulting device report descriptor is assembled from all the report 115*4882a593Smuzhiyun * descriptor parts referenced by the structure. No order of assembly should 116*4882a593Smuzhiyun * be assumed. The structure represents original device report descriptor if 117*4882a593Smuzhiyun * all the parts are NULL. 118*4882a593Smuzhiyun */ 119*4882a593Smuzhiyun struct uclogic_params { 120*4882a593Smuzhiyun /* 121*4882a593Smuzhiyun * True if the whole interface is invalid, false otherwise. 122*4882a593Smuzhiyun */ 123*4882a593Smuzhiyun bool invalid; 124*4882a593Smuzhiyun /* 125*4882a593Smuzhiyun * Pointer to the common part of the replacement report descriptor, 126*4882a593Smuzhiyun * allocated with kmalloc. NULL if no common part is needed. 127*4882a593Smuzhiyun * Only valid, if "invalid" is false. 128*4882a593Smuzhiyun */ 129*4882a593Smuzhiyun __u8 *desc_ptr; 130*4882a593Smuzhiyun /* 131*4882a593Smuzhiyun * Size of the common part of the replacement report descriptor. 132*4882a593Smuzhiyun * Only valid, if "desc_ptr" is not NULL. 133*4882a593Smuzhiyun */ 134*4882a593Smuzhiyun unsigned int desc_size; 135*4882a593Smuzhiyun /* 136*4882a593Smuzhiyun * True, if pen usage in report descriptor is invalid, when present. 137*4882a593Smuzhiyun * Only valid, if "invalid" is false. 138*4882a593Smuzhiyun */ 139*4882a593Smuzhiyun bool pen_unused; 140*4882a593Smuzhiyun /* 141*4882a593Smuzhiyun * Pen parameters and optional report descriptor part. 142*4882a593Smuzhiyun * Only valid if "pen_unused" is valid and false. 143*4882a593Smuzhiyun */ 144*4882a593Smuzhiyun struct uclogic_params_pen pen; 145*4882a593Smuzhiyun /* 146*4882a593Smuzhiyun * Frame control parameters and optional report descriptor part. 147*4882a593Smuzhiyun * Only valid, if "invalid" is false. 148*4882a593Smuzhiyun */ 149*4882a593Smuzhiyun struct uclogic_params_frame frame; 150*4882a593Smuzhiyun /* 151*4882a593Smuzhiyun * Bitmask matching frame controls "sub-report" flag in the second 152*4882a593Smuzhiyun * byte of the pen report, or zero if it's not expected. 153*4882a593Smuzhiyun * Only valid if both "pen" and "frame" are valid, and "frame.id" is 154*4882a593Smuzhiyun * not zero. 155*4882a593Smuzhiyun */ 156*4882a593Smuzhiyun __u8 pen_frame_flag; 157*4882a593Smuzhiyun }; 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun /* Initialize a tablet interface and discover its parameters */ 160*4882a593Smuzhiyun extern int uclogic_params_init(struct uclogic_params *params, 161*4882a593Smuzhiyun struct hid_device *hdev); 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun /* Tablet interface parameters *printf format string */ 164*4882a593Smuzhiyun #define UCLOGIC_PARAMS_FMT_STR \ 165*4882a593Smuzhiyun ".invalid = %s\n" \ 166*4882a593Smuzhiyun ".desc_ptr = %p\n" \ 167*4882a593Smuzhiyun ".desc_size = %u\n" \ 168*4882a593Smuzhiyun ".pen_unused = %s\n" \ 169*4882a593Smuzhiyun ".pen.desc_ptr = %p\n" \ 170*4882a593Smuzhiyun ".pen.desc_size = %u\n" \ 171*4882a593Smuzhiyun ".pen.id = %u\n" \ 172*4882a593Smuzhiyun ".pen.inrange = %s\n" \ 173*4882a593Smuzhiyun ".pen.fragmented_hires = %s\n" \ 174*4882a593Smuzhiyun ".frame.desc_ptr = %p\n" \ 175*4882a593Smuzhiyun ".frame.desc_size = %u\n" \ 176*4882a593Smuzhiyun ".frame.id = %u\n" \ 177*4882a593Smuzhiyun ".frame.re_lsb = %u\n" \ 178*4882a593Smuzhiyun ".frame.dev_id_byte = %u\n" \ 179*4882a593Smuzhiyun ".pen_frame_flag = 0x%02x\n" 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun /* Tablet interface parameters *printf format arguments */ 182*4882a593Smuzhiyun #define UCLOGIC_PARAMS_FMT_ARGS(_params) \ 183*4882a593Smuzhiyun ((_params)->invalid ? "true" : "false"), \ 184*4882a593Smuzhiyun (_params)->desc_ptr, \ 185*4882a593Smuzhiyun (_params)->desc_size, \ 186*4882a593Smuzhiyun ((_params)->pen_unused ? "true" : "false"), \ 187*4882a593Smuzhiyun (_params)->pen.desc_ptr, \ 188*4882a593Smuzhiyun (_params)->pen.desc_size, \ 189*4882a593Smuzhiyun (_params)->pen.id, \ 190*4882a593Smuzhiyun uclogic_params_pen_inrange_to_str((_params)->pen.inrange), \ 191*4882a593Smuzhiyun ((_params)->pen.fragmented_hires ? "true" : "false"), \ 192*4882a593Smuzhiyun (_params)->frame.desc_ptr, \ 193*4882a593Smuzhiyun (_params)->frame.desc_size, \ 194*4882a593Smuzhiyun (_params)->frame.id, \ 195*4882a593Smuzhiyun (_params)->frame.re_lsb, \ 196*4882a593Smuzhiyun (_params)->frame.dev_id_byte, \ 197*4882a593Smuzhiyun (_params)->pen_frame_flag 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun /* Get a replacement report descriptor for a tablet's interface. */ 200*4882a593Smuzhiyun extern int uclogic_params_get_desc(const struct uclogic_params *params, 201*4882a593Smuzhiyun __u8 **pdesc, 202*4882a593Smuzhiyun unsigned int *psize); 203*4882a593Smuzhiyun 204*4882a593Smuzhiyun /* Free resources used by tablet interface's parameters */ 205*4882a593Smuzhiyun extern void uclogic_params_cleanup(struct uclogic_params *params); 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun #endif /* _HID_UCLOGIC_PARAMS_H */ 208