18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  HID driver for UC-Logic devices not fully compliant with HID standard
48c2ecf20Sopenharmony_ci *  - tablet initialization and parameter retrieval
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Copyright (c) 2018 Nikolai Kondrashov
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it
118c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the Free
128c2ecf20Sopenharmony_ci * Software Foundation; either version 2 of the License, or (at your option)
138c2ecf20Sopenharmony_ci * any later version.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#ifndef _HID_UCLOGIC_PARAMS_H
178c2ecf20Sopenharmony_ci#define _HID_UCLOGIC_PARAMS_H
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/usb.h>
208c2ecf20Sopenharmony_ci#include <linux/hid.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Types of pen in-range reporting */
238c2ecf20Sopenharmony_cienum uclogic_params_pen_inrange {
248c2ecf20Sopenharmony_ci	/* Normal reports: zero - out of proximity, one - in proximity */
258c2ecf20Sopenharmony_ci	UCLOGIC_PARAMS_PEN_INRANGE_NORMAL = 0,
268c2ecf20Sopenharmony_ci	/* Inverted reports: zero - in proximity, one - out of proximity */
278c2ecf20Sopenharmony_ci	UCLOGIC_PARAMS_PEN_INRANGE_INVERTED,
288c2ecf20Sopenharmony_ci	/* No reports */
298c2ecf20Sopenharmony_ci	UCLOGIC_PARAMS_PEN_INRANGE_NONE,
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Convert a pen in-range reporting type to a string */
338c2ecf20Sopenharmony_ciextern const char *uclogic_params_pen_inrange_to_str(
348c2ecf20Sopenharmony_ci			enum uclogic_params_pen_inrange inrange);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Tablet interface's pen input parameters.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * Must use declarative (descriptive) language, not imperative, to simplify
408c2ecf20Sopenharmony_ci * understanding and maintain consistency.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * Noop (preserving functionality) when filled with zeroes.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistruct uclogic_params_pen {
458c2ecf20Sopenharmony_ci	/*
468c2ecf20Sopenharmony_ci	 * Pointer to report descriptor describing the inputs.
478c2ecf20Sopenharmony_ci	 * Allocated with kmalloc.
488c2ecf20Sopenharmony_ci	 */
498c2ecf20Sopenharmony_ci	__u8 *desc_ptr;
508c2ecf20Sopenharmony_ci	/*
518c2ecf20Sopenharmony_ci	 * Size of the report descriptor.
528c2ecf20Sopenharmony_ci	 * Only valid, if "desc_ptr" is not NULL.
538c2ecf20Sopenharmony_ci	 */
548c2ecf20Sopenharmony_ci	unsigned int desc_size;
558c2ecf20Sopenharmony_ci	/* Report ID, if reports should be tweaked, zero if not */
568c2ecf20Sopenharmony_ci	unsigned int id;
578c2ecf20Sopenharmony_ci	/* Type of in-range reporting, only valid if "id" is not zero */
588c2ecf20Sopenharmony_ci	enum uclogic_params_pen_inrange inrange;
598c2ecf20Sopenharmony_ci	/*
608c2ecf20Sopenharmony_ci	 * True, if reports include fragmented high resolution coords, with
618c2ecf20Sopenharmony_ci	 * high-order X and then Y bytes following the pressure field.
628c2ecf20Sopenharmony_ci	 * Only valid if "id" is not zero.
638c2ecf20Sopenharmony_ci	 */
648c2ecf20Sopenharmony_ci	bool fragmented_hires;
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Parameters of frame control inputs of a tablet interface.
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * Must use declarative (descriptive) language, not imperative, to simplify
718c2ecf20Sopenharmony_ci * understanding and maintain consistency.
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * Noop (preserving functionality) when filled with zeroes.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistruct uclogic_params_frame {
768c2ecf20Sopenharmony_ci	/*
778c2ecf20Sopenharmony_ci	 * Pointer to report descriptor describing the inputs.
788c2ecf20Sopenharmony_ci	 * Allocated with kmalloc.
798c2ecf20Sopenharmony_ci	 */
808c2ecf20Sopenharmony_ci	__u8 *desc_ptr;
818c2ecf20Sopenharmony_ci	/*
828c2ecf20Sopenharmony_ci	 * Size of the report descriptor.
838c2ecf20Sopenharmony_ci	 * Only valid, if "desc_ptr" is not NULL.
848c2ecf20Sopenharmony_ci	 */
858c2ecf20Sopenharmony_ci	unsigned int desc_size;
868c2ecf20Sopenharmony_ci	/*
878c2ecf20Sopenharmony_ci	 * Report ID, if reports should be tweaked, zero if not.
888c2ecf20Sopenharmony_ci	 */
898c2ecf20Sopenharmony_ci	unsigned int id;
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * Number of the least-significant bit of the 2-bit state of a rotary
928c2ecf20Sopenharmony_ci	 * encoder, in the report. Cannot point to a 2-bit field crossing a
938c2ecf20Sopenharmony_ci	 * byte boundary. Zero if not present. Only valid if "id" is not zero.
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	unsigned int re_lsb;
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Offset of the Wacom-style device ID byte in the report, to be set
988c2ecf20Sopenharmony_ci	 * to pad device ID (0xf), for compatibility with Wacom drivers. Zero
998c2ecf20Sopenharmony_ci	 * if no changes to the report should be made. Only valid if "id" is
1008c2ecf20Sopenharmony_ci	 * not zero.
1018c2ecf20Sopenharmony_ci	 */
1028c2ecf20Sopenharmony_ci	unsigned int dev_id_byte;
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/*
1068c2ecf20Sopenharmony_ci * Tablet interface report parameters.
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * Must use declarative (descriptive) language, not imperative, to simplify
1098c2ecf20Sopenharmony_ci * understanding and maintain consistency.
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * When filled with zeros represents a "noop" configuration - passes all
1128c2ecf20Sopenharmony_ci * reports unchanged and lets the generic HID driver handle everything.
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * The resulting device report descriptor is assembled from all the report
1158c2ecf20Sopenharmony_ci * descriptor parts referenced by the structure. No order of assembly should
1168c2ecf20Sopenharmony_ci * be assumed. The structure represents original device report descriptor if
1178c2ecf20Sopenharmony_ci * all the parts are NULL.
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_cistruct uclogic_params {
1208c2ecf20Sopenharmony_ci	/*
1218c2ecf20Sopenharmony_ci	 * True if the whole interface is invalid, false otherwise.
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci	bool invalid;
1248c2ecf20Sopenharmony_ci	/*
1258c2ecf20Sopenharmony_ci	 * Pointer to the common part of the replacement report descriptor,
1268c2ecf20Sopenharmony_ci	 * allocated with kmalloc. NULL if no common part is needed.
1278c2ecf20Sopenharmony_ci	 * Only valid, if "invalid" is false.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	__u8 *desc_ptr;
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * Size of the common part of the replacement report descriptor.
1328c2ecf20Sopenharmony_ci	 * Only valid, if "desc_ptr" is not NULL.
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	unsigned int desc_size;
1358c2ecf20Sopenharmony_ci	/*
1368c2ecf20Sopenharmony_ci	 * True, if pen usage in report descriptor is invalid, when present.
1378c2ecf20Sopenharmony_ci	 * Only valid, if "invalid" is false.
1388c2ecf20Sopenharmony_ci	 */
1398c2ecf20Sopenharmony_ci	bool pen_unused;
1408c2ecf20Sopenharmony_ci	/*
1418c2ecf20Sopenharmony_ci	 * Pen parameters and optional report descriptor part.
1428c2ecf20Sopenharmony_ci	 * Only valid if "pen_unused" is valid and false.
1438c2ecf20Sopenharmony_ci	 */
1448c2ecf20Sopenharmony_ci	struct uclogic_params_pen pen;
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * Frame control parameters and optional report descriptor part.
1478c2ecf20Sopenharmony_ci	 * Only valid, if "invalid" is false.
1488c2ecf20Sopenharmony_ci	 */
1498c2ecf20Sopenharmony_ci	struct uclogic_params_frame frame;
1508c2ecf20Sopenharmony_ci	/*
1518c2ecf20Sopenharmony_ci	 * Bitmask matching frame controls "sub-report" flag in the second
1528c2ecf20Sopenharmony_ci	 * byte of the pen report, or zero if it's not expected.
1538c2ecf20Sopenharmony_ci	 * Only valid if both "pen" and "frame" are valid, and "frame.id" is
1548c2ecf20Sopenharmony_ci	 * not zero.
1558c2ecf20Sopenharmony_ci	 */
1568c2ecf20Sopenharmony_ci	__u8 pen_frame_flag;
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* Initialize a tablet interface and discover its parameters */
1608c2ecf20Sopenharmony_ciextern int uclogic_params_init(struct uclogic_params *params,
1618c2ecf20Sopenharmony_ci				struct hid_device *hdev);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* Tablet interface parameters *printf format string */
1648c2ecf20Sopenharmony_ci#define UCLOGIC_PARAMS_FMT_STR \
1658c2ecf20Sopenharmony_ci		".invalid = %s\n"                   \
1668c2ecf20Sopenharmony_ci		".desc_ptr = %p\n"                  \
1678c2ecf20Sopenharmony_ci		".desc_size = %u\n"                 \
1688c2ecf20Sopenharmony_ci		".pen_unused = %s\n"                \
1698c2ecf20Sopenharmony_ci		".pen.desc_ptr = %p\n"              \
1708c2ecf20Sopenharmony_ci		".pen.desc_size = %u\n"             \
1718c2ecf20Sopenharmony_ci		".pen.id = %u\n"                    \
1728c2ecf20Sopenharmony_ci		".pen.inrange = %s\n"               \
1738c2ecf20Sopenharmony_ci		".pen.fragmented_hires = %s\n"      \
1748c2ecf20Sopenharmony_ci		".frame.desc_ptr = %p\n"            \
1758c2ecf20Sopenharmony_ci		".frame.desc_size = %u\n"           \
1768c2ecf20Sopenharmony_ci		".frame.id = %u\n"                  \
1778c2ecf20Sopenharmony_ci		".frame.re_lsb = %u\n"              \
1788c2ecf20Sopenharmony_ci		".frame.dev_id_byte = %u\n"         \
1798c2ecf20Sopenharmony_ci		".pen_frame_flag = 0x%02x\n"
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* Tablet interface parameters *printf format arguments */
1828c2ecf20Sopenharmony_ci#define UCLOGIC_PARAMS_FMT_ARGS(_params) \
1838c2ecf20Sopenharmony_ci		((_params)->invalid ? "true" : "false"),                    \
1848c2ecf20Sopenharmony_ci		(_params)->desc_ptr,                                        \
1858c2ecf20Sopenharmony_ci		(_params)->desc_size,                                       \
1868c2ecf20Sopenharmony_ci		((_params)->pen_unused ? "true" : "false"),                 \
1878c2ecf20Sopenharmony_ci		(_params)->pen.desc_ptr,                                    \
1888c2ecf20Sopenharmony_ci		(_params)->pen.desc_size,                                   \
1898c2ecf20Sopenharmony_ci		(_params)->pen.id,                                          \
1908c2ecf20Sopenharmony_ci		uclogic_params_pen_inrange_to_str((_params)->pen.inrange),  \
1918c2ecf20Sopenharmony_ci		((_params)->pen.fragmented_hires ? "true" : "false"),       \
1928c2ecf20Sopenharmony_ci		(_params)->frame.desc_ptr,                                  \
1938c2ecf20Sopenharmony_ci		(_params)->frame.desc_size,                                 \
1948c2ecf20Sopenharmony_ci		(_params)->frame.id,                                        \
1958c2ecf20Sopenharmony_ci		(_params)->frame.re_lsb,                                    \
1968c2ecf20Sopenharmony_ci		(_params)->frame.dev_id_byte,                               \
1978c2ecf20Sopenharmony_ci		(_params)->pen_frame_flag
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/* Get a replacement report descriptor for a tablet's interface. */
2008c2ecf20Sopenharmony_ciextern int uclogic_params_get_desc(const struct uclogic_params *params,
2018c2ecf20Sopenharmony_ci					__u8 **pdesc,
2028c2ecf20Sopenharmony_ci					unsigned int *psize);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* Free resources used by tablet interface's parameters */
2058c2ecf20Sopenharmony_ciextern void uclogic_params_cleanup(struct uclogic_params *params);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci#endif /* _HID_UCLOGIC_PARAMS_H */
208