HID: uclogic: Remove pen usage masking
[linux-block.git] / drivers / hid / hid-uclogic-params.c
CommitLineData
9614219e
NK
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#include "hid-uclogic-params.h"
17#include "hid-uclogic-rdesc.h"
18#include "usbhid/usbhid.h"
19#include "hid-ids.h"
20#include <linux/ctype.h>
21#include <asm/unaligned.h>
22
23/**
5abb5445
LJ
24 * uclogic_params_pen_inrange_to_str() - Convert a pen in-range reporting type
25 * to a string.
9614219e
NK
26 *
27 * @inrange: The in-range reporting type to convert.
28 *
29 * Returns:
30 * The string representing the type, or NULL if the type is unknown.
31 */
32const char *uclogic_params_pen_inrange_to_str(
33 enum uclogic_params_pen_inrange inrange)
34{
35 switch (inrange) {
36 case UCLOGIC_PARAMS_PEN_INRANGE_NORMAL:
37 return "normal";
38 case UCLOGIC_PARAMS_PEN_INRANGE_INVERTED:
39 return "inverted";
01309e29
NK
40 case UCLOGIC_PARAMS_PEN_INRANGE_NONE:
41 return "none";
9614219e
NK
42 default:
43 return NULL;
44 }
45}
46
47/**
48 * uclogic_params_get_str_desc - retrieve a string descriptor from a HID
49 * device interface, putting it into a kmalloc-allocated buffer as is, without
50 * character encoding conversion.
51 *
52 * @pbuf: Location for the kmalloc-allocated buffer pointer containing
53 * the retrieved descriptor. Not modified in case of error.
54 * Can be NULL to have retrieved descriptor discarded.
55 * @hdev: The HID device of the tablet interface to retrieve the string
56 * descriptor from. Cannot be NULL.
57 * @idx: Index of the string descriptor to request from the device.
58 * @len: Length of the buffer to allocate and the data to retrieve.
59 *
60 * Returns:
61 * number of bytes retrieved (<= len),
62 * -EPIPE, if the descriptor was not found, or
63 * another negative errno code in case of other error.
64 */
65static int uclogic_params_get_str_desc(__u8 **pbuf, struct hid_device *hdev,
66 __u8 idx, size_t len)
67{
68 int rc;
0a94131d 69 struct usb_device *udev;
9614219e
NK
70 __u8 *buf = NULL;
71
72 /* Check arguments */
73 if (hdev == NULL) {
74 rc = -EINVAL;
75 goto cleanup;
76 }
77
0a94131d
JE
78 udev = hid_to_usb_dev(hdev);
79
9614219e
NK
80 buf = kmalloc(len, GFP_KERNEL);
81 if (buf == NULL) {
82 rc = -ENOMEM;
83 goto cleanup;
84 }
85
86 rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
87 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
88 (USB_DT_STRING << 8) + idx,
89 0x0409, buf, len,
90 USB_CTRL_GET_TIMEOUT);
91 if (rc == -EPIPE) {
92 hid_dbg(hdev, "string descriptor #%hhu not found\n", idx);
93 goto cleanup;
94 } else if (rc < 0) {
95 hid_err(hdev,
a876e7e2 96 "failed retrieving string descriptor #%u: %d\n",
9614219e
NK
97 idx, rc);
98 goto cleanup;
99 }
100
101 if (pbuf != NULL) {
102 *pbuf = buf;
103 buf = NULL;
104 }
105
106cleanup:
107 kfree(buf);
108 return rc;
109}
110
111/**
112 * uclogic_params_pen_cleanup - free resources used by struct
113 * uclogic_params_pen (tablet interface's pen input parameters).
114 * Can be called repeatedly.
115 *
116 * @pen: Pen input parameters to cleanup. Cannot be NULL.
117 */
118static void uclogic_params_pen_cleanup(struct uclogic_params_pen *pen)
119{
120 kfree(pen->desc_ptr);
121 memset(pen, 0, sizeof(*pen));
122}
123
124/**
eecb5b84
NK
125 * uclogic_params_pen_init_v1() - initialize tablet interface pen
126 * input and retrieve its parameters from the device, using v1 protocol.
9614219e
NK
127 *
128 * @pen: Pointer to the pen parameters to initialize (to be
129 * cleaned up with uclogic_params_pen_cleanup()). Not modified in
130 * case of error, or if parameters are not found. Cannot be NULL.
131 * @pfound: Location for a flag which is set to true if the parameters
132 * were found, and to false if not (e.g. device was
133 * incompatible). Not modified in case of error. Cannot be NULL.
134 * @hdev: The HID device of the tablet interface to initialize and get
135 * parameters from. Cannot be NULL.
136 *
137 * Returns:
138 * Zero, if successful. A negative errno code on error.
139 */
eecb5b84
NK
140static int uclogic_params_pen_init_v1(struct uclogic_params_pen *pen,
141 bool *pfound,
142 struct hid_device *hdev)
9614219e
NK
143{
144 int rc;
145 bool found = false;
146 /* Buffer for (part of) the string descriptor */
147 __u8 *buf = NULL;
148 /* Minimum descriptor length required, maximum seen so far is 18 */
149 const int len = 12;
150 s32 resolution;
151 /* Pen report descriptor template parameters */
152 s32 desc_params[UCLOGIC_RDESC_PEN_PH_ID_NUM];
153 __u8 *desc_ptr = NULL;
154
155 /* Check arguments */
156 if (pen == NULL || pfound == NULL || hdev == NULL) {
157 rc = -EINVAL;
158 goto cleanup;
159 }
160
161 /*
162 * Read string descriptor containing pen input parameters.
163 * The specific string descriptor and data were discovered by sniffing
164 * the Windows driver traffic.
165 * NOTE: This enables fully-functional tablet mode.
166 */
167 rc = uclogic_params_get_str_desc(&buf, hdev, 100, len);
168 if (rc == -EPIPE) {
169 hid_dbg(hdev,
170 "string descriptor with pen parameters not found, assuming not compatible\n");
171 goto finish;
172 } else if (rc < 0) {
173 hid_err(hdev, "failed retrieving pen parameters: %d\n", rc);
174 goto cleanup;
175 } else if (rc != len) {
176 hid_dbg(hdev,
177 "string descriptor with pen parameters has invalid length (got %d, expected %d), assuming not compatible\n",
178 rc, len);
179 goto finish;
180 }
181
182 /*
183 * Fill report descriptor parameters from the string descriptor
184 */
185 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] =
186 get_unaligned_le16(buf + 2);
187 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] =
188 get_unaligned_le16(buf + 4);
189 desc_params[UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] =
190 get_unaligned_le16(buf + 8);
191 resolution = get_unaligned_le16(buf + 10);
192 if (resolution == 0) {
193 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0;
194 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0;
195 } else {
196 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] =
197 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] * 1000 /
198 resolution;
199 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] =
200 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] * 1000 /
201 resolution;
202 }
203 kfree(buf);
204 buf = NULL;
205
206 /*
207 * Generate pen report descriptor
208 */
209 desc_ptr = uclogic_rdesc_template_apply(
eecb5b84
NK
210 uclogic_rdesc_pen_v1_template_arr,
211 uclogic_rdesc_pen_v1_template_size,
9614219e
NK
212 desc_params, ARRAY_SIZE(desc_params));
213 if (desc_ptr == NULL) {
214 rc = -ENOMEM;
215 goto cleanup;
216 }
217
218 /*
219 * Fill-in the parameters
220 */
221 memset(pen, 0, sizeof(*pen));
222 pen->desc_ptr = desc_ptr;
223 desc_ptr = NULL;
eecb5b84
NK
224 pen->desc_size = uclogic_rdesc_pen_v1_template_size;
225 pen->id = UCLOGIC_RDESC_PEN_V1_ID;
9614219e
NK
226 pen->inrange = UCLOGIC_PARAMS_PEN_INRANGE_INVERTED;
227 found = true;
228finish:
229 *pfound = found;
230 rc = 0;
231cleanup:
232 kfree(desc_ptr);
233 kfree(buf);
234 return rc;
235}
236
2c3a88c6
NK
237/**
238 * uclogic_params_get_le24() - get a 24-bit little-endian number from a
239 * buffer.
240 *
241 * @p: The pointer to the number buffer.
242 *
243 * Returns:
244 * The retrieved number
245 */
246static s32 uclogic_params_get_le24(const void *p)
247{
248 const __u8 *b = p;
249 return b[0] | (b[1] << 8UL) | (b[2] << 16UL);
250}
251
252/**
253 * uclogic_params_pen_init_v2() - initialize tablet interface pen
254 * input and retrieve its parameters from the device, using v2 protocol.
255 *
256 * @pen: Pointer to the pen parameters to initialize (to be
257 * cleaned up with uclogic_params_pen_cleanup()). Not modified in
258 * case of error, or if parameters are not found. Cannot be NULL.
259 * @pfound: Location for a flag which is set to true if the parameters
260 * were found, and to false if not (e.g. device was
261 * incompatible). Not modified in case of error. Cannot be NULL.
262 * @hdev: The HID device of the tablet interface to initialize and get
263 * parameters from. Cannot be NULL.
264 *
265 * Returns:
266 * Zero, if successful. A negative errno code on error.
267 */
268static int uclogic_params_pen_init_v2(struct uclogic_params_pen *pen,
269 bool *pfound,
270 struct hid_device *hdev)
271{
272 int rc;
273 bool found = false;
274 /* Buffer for (part of) the string descriptor */
275 __u8 *buf = NULL;
276 /* Descriptor length required */
277 const int len = 18;
278 s32 resolution;
279 /* Pen report descriptor template parameters */
280 s32 desc_params[UCLOGIC_RDESC_PEN_PH_ID_NUM];
281 __u8 *desc_ptr = NULL;
282
283 /* Check arguments */
284 if (pen == NULL || pfound == NULL || hdev == NULL) {
285 rc = -EINVAL;
286 goto cleanup;
287 }
288
289 /*
290 * Read string descriptor containing pen input parameters.
291 * The specific string descriptor and data were discovered by sniffing
292 * the Windows driver traffic.
293 * NOTE: This enables fully-functional tablet mode.
294 */
295 rc = uclogic_params_get_str_desc(&buf, hdev, 200, len);
296 if (rc == -EPIPE) {
297 hid_dbg(hdev,
298 "string descriptor with pen parameters not found, assuming not compatible\n");
299 goto finish;
300 } else if (rc < 0) {
301 hid_err(hdev, "failed retrieving pen parameters: %d\n", rc);
302 goto cleanup;
303 } else if (rc != len) {
304 hid_dbg(hdev,
305 "string descriptor with pen parameters has invalid length (got %d, expected %d), assuming not compatible\n",
306 rc, len);
307 goto finish;
308 } else {
309 size_t i;
310 /*
311 * Check it's not just a catch-all UTF-16LE-encoded ASCII
312 * string (such as the model name) some tablets put into all
313 * unknown string descriptors.
314 */
315 for (i = 2;
316 i < len &&
317 (buf[i] >= 0x20 && buf[i] < 0x7f && buf[i + 1] == 0);
318 i += 2);
319 if (i >= len) {
320 hid_dbg(hdev,
321 "string descriptor with pen parameters seems to contain only text, assuming not compatible\n");
322 goto finish;
323 }
324 }
325
326 /*
327 * Fill report descriptor parameters from the string descriptor
328 */
329 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] =
330 uclogic_params_get_le24(buf + 2);
331 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] =
332 uclogic_params_get_le24(buf + 5);
333 desc_params[UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] =
334 get_unaligned_le16(buf + 8);
335 resolution = get_unaligned_le16(buf + 10);
336 if (resolution == 0) {
337 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0;
338 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0;
339 } else {
340 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_PM] =
341 desc_params[UCLOGIC_RDESC_PEN_PH_ID_X_LM] * 1000 /
342 resolution;
343 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_PM] =
344 desc_params[UCLOGIC_RDESC_PEN_PH_ID_Y_LM] * 1000 /
345 resolution;
346 }
347 kfree(buf);
348 buf = NULL;
349
350 /*
351 * Generate pen report descriptor
352 */
353 desc_ptr = uclogic_rdesc_template_apply(
354 uclogic_rdesc_pen_v2_template_arr,
355 uclogic_rdesc_pen_v2_template_size,
356 desc_params, ARRAY_SIZE(desc_params));
357 if (desc_ptr == NULL) {
358 rc = -ENOMEM;
359 goto cleanup;
360 }
361
362 /*
363 * Fill-in the parameters
364 */
365 memset(pen, 0, sizeof(*pen));
366 pen->desc_ptr = desc_ptr;
367 desc_ptr = NULL;
368 pen->desc_size = uclogic_rdesc_pen_v2_template_size;
369 pen->id = UCLOGIC_RDESC_PEN_V2_ID;
370 pen->inrange = UCLOGIC_PARAMS_PEN_INRANGE_NONE;
371 pen->fragmented_hires = true;
1324c5ac 372 pen->tilt_y_flipped = true;
2c3a88c6
NK
373 found = true;
374finish:
375 *pfound = found;
376 rc = 0;
377cleanup:
378 kfree(desc_ptr);
379 kfree(buf);
380 return rc;
381}
382
9614219e
NK
383/**
384 * uclogic_params_frame_cleanup - free resources used by struct
385 * uclogic_params_frame (tablet interface's frame controls input parameters).
386 * Can be called repeatedly.
387 *
388 * @frame: Frame controls input parameters to cleanup. Cannot be NULL.
389 */
390static void uclogic_params_frame_cleanup(struct uclogic_params_frame *frame)
391{
392 kfree(frame->desc_ptr);
393 memset(frame, 0, sizeof(*frame));
394}
395
396/**
397 * uclogic_params_frame_init_with_desc() - initialize tablet's frame control
398 * parameters with a static report descriptor.
399 *
400 * @frame: Pointer to the frame parameters to initialize (to be cleaned
401 * up with uclogic_params_frame_cleanup()). Not modified in case
402 * of error. Cannot be NULL.
403 * @desc_ptr: Report descriptor pointer. Can be NULL, if desc_size is zero.
404 * @desc_size: Report descriptor size.
405 * @id: Report ID used for frame reports, if they should be tweaked,
406 * zero if not.
407 *
408 * Returns:
409 * Zero, if successful. A negative errno code on error.
410 */
411static int uclogic_params_frame_init_with_desc(
412 struct uclogic_params_frame *frame,
413 const __u8 *desc_ptr,
414 size_t desc_size,
415 unsigned int id)
416{
417 __u8 *copy_desc_ptr;
418
419 if (frame == NULL || (desc_ptr == NULL && desc_size != 0))
420 return -EINVAL;
421
422 copy_desc_ptr = kmemdup(desc_ptr, desc_size, GFP_KERNEL);
423 if (copy_desc_ptr == NULL)
424 return -ENOMEM;
425
426 memset(frame, 0, sizeof(*frame));
427 frame->desc_ptr = copy_desc_ptr;
428 frame->desc_size = desc_size;
429 frame->id = id;
430 return 0;
431}
432
433/**
eecb5b84
NK
434 * uclogic_params_frame_init_v1_buttonpad() - initialize abstract buttonpad
435 * on a v1 tablet interface.
9614219e
NK
436 *
437 * @frame: Pointer to the frame parameters to initialize (to be cleaned
438 * up with uclogic_params_frame_cleanup()). Not modified in case
439 * of error, or if parameters are not found. Cannot be NULL.
440 * @pfound: Location for a flag which is set to true if the parameters
441 * were found, and to false if not (e.g. device was
442 * incompatible). Not modified in case of error. Cannot be NULL.
443 * @hdev: The HID device of the tablet interface to initialize and get
444 * parameters from. Cannot be NULL.
445 *
446 * Returns:
447 * Zero, if successful. A negative errno code on error.
448 */
eecb5b84 449static int uclogic_params_frame_init_v1_buttonpad(
9614219e
NK
450 struct uclogic_params_frame *frame,
451 bool *pfound,
452 struct hid_device *hdev)
453{
454 int rc;
455 bool found = false;
aa320fdb 456 struct usb_device *usb_dev;
9614219e
NK
457 char *str_buf = NULL;
458 const size_t str_len = 16;
459
460 /* Check arguments */
461 if (frame == NULL || pfound == NULL || hdev == NULL) {
462 rc = -EINVAL;
463 goto cleanup;
464 }
465
aa320fdb
JE
466 usb_dev = hid_to_usb_dev(hdev);
467
9614219e
NK
468 /*
469 * Enable generic button mode
470 */
471 str_buf = kzalloc(str_len, GFP_KERNEL);
472 if (str_buf == NULL) {
473 rc = -ENOMEM;
474 goto cleanup;
475 }
476
477 rc = usb_string(usb_dev, 123, str_buf, str_len);
478 if (rc == -EPIPE) {
479 hid_dbg(hdev,
480 "generic button -enabling string descriptor not found\n");
481 } else if (rc < 0) {
482 goto cleanup;
483 } else if (strncmp(str_buf, "HK On", rc) != 0) {
484 hid_dbg(hdev,
485 "invalid response to enabling generic buttons: \"%s\"\n",
486 str_buf);
487 } else {
488 hid_dbg(hdev, "generic buttons enabled\n");
489 rc = uclogic_params_frame_init_with_desc(
490 frame,
eecb5b84
NK
491 uclogic_rdesc_buttonpad_v1_arr,
492 uclogic_rdesc_buttonpad_v1_size,
493 UCLOGIC_RDESC_BUTTONPAD_V1_ID);
9614219e
NK
494 if (rc != 0)
495 goto cleanup;
496 found = true;
497 }
498
499 *pfound = found;
500 rc = 0;
501cleanup:
502 kfree(str_buf);
503 return rc;
504}
505
506/**
507 * uclogic_params_cleanup - free resources used by struct uclogic_params
508 * (tablet interface's parameters).
509 * Can be called repeatedly.
510 *
511 * @params: Input parameters to cleanup. Cannot be NULL.
512 */
513void uclogic_params_cleanup(struct uclogic_params *params)
514{
515 if (!params->invalid) {
516 kfree(params->desc_ptr);
606dadc1 517 uclogic_params_pen_cleanup(&params->pen);
9614219e
NK
518 uclogic_params_frame_cleanup(&params->frame);
519 memset(params, 0, sizeof(*params));
520 }
521}
522
523/**
5abb5445
LJ
524 * uclogic_params_get_desc() - Get a replacement report descriptor for a
525 * tablet's interface.
9614219e
NK
526 *
527 * @params: The parameters of a tablet interface to get report
528 * descriptor for. Cannot be NULL.
529 * @pdesc: Location for the resulting, kmalloc-allocated report
530 * descriptor pointer, or for NULL, if there's no replacement
531 * report descriptor. Not modified in case of error. Cannot be
532 * NULL.
533 * @psize: Location for the resulting report descriptor size, not set if
534 * there's no replacement report descriptor. Not modified in case
535 * of error. Cannot be NULL.
536 *
537 * Returns:
538 * Zero, if successful.
539 * -EINVAL, if invalid arguments are supplied.
540 * -ENOMEM, if failed to allocate memory.
541 */
542int uclogic_params_get_desc(const struct uclogic_params *params,
543 __u8 **pdesc,
544 unsigned int *psize)
545{
546 bool common_present;
547 bool pen_present;
548 bool frame_present;
549 unsigned int size;
550 __u8 *desc = NULL;
551
552 /* Check arguments */
553 if (params == NULL || pdesc == NULL || psize == NULL)
554 return -EINVAL;
555
556 size = 0;
557
558 common_present = (params->desc_ptr != NULL);
606dadc1 559 pen_present = (params->pen.desc_ptr != NULL);
9614219e
NK
560 frame_present = (params->frame.desc_ptr != NULL);
561
562 if (common_present)
563 size += params->desc_size;
564 if (pen_present)
565 size += params->pen.desc_size;
566 if (frame_present)
567 size += params->frame.desc_size;
568
569 if (common_present || pen_present || frame_present) {
570 __u8 *p;
571
572 desc = kmalloc(size, GFP_KERNEL);
573 if (desc == NULL)
574 return -ENOMEM;
575 p = desc;
576
577 if (common_present) {
578 memcpy(p, params->desc_ptr,
579 params->desc_size);
580 p += params->desc_size;
581 }
582 if (pen_present) {
583 memcpy(p, params->pen.desc_ptr,
584 params->pen.desc_size);
585 p += params->pen.desc_size;
586 }
587 if (frame_present) {
588 memcpy(p, params->frame.desc_ptr,
589 params->frame.desc_size);
590 p += params->frame.desc_size;
591 }
592
593 WARN_ON(p != desc + size);
594
595 *psize = size;
596 }
597
598 *pdesc = desc;
599 return 0;
600}
601
602/**
603 * uclogic_params_init_invalid() - initialize tablet interface parameters,
604 * specifying the interface is invalid.
605 *
606 * @params: Parameters to initialize (to be cleaned with
607 * uclogic_params_cleanup()). Cannot be NULL.
608 */
609static void uclogic_params_init_invalid(struct uclogic_params *params)
610{
611 params->invalid = true;
612}
613
614/**
615 * uclogic_params_init_with_opt_desc() - initialize tablet interface
616 * parameters with an optional replacement report descriptor. Only modify
617 * report descriptor, if the original report descriptor matches the expected
618 * size.
619 *
620 * @params: Parameters to initialize (to be cleaned with
621 * uclogic_params_cleanup()). Not modified in case of
622 * error. Cannot be NULL.
623 * @hdev: The HID device of the tablet interface create the
624 * parameters for. Cannot be NULL.
625 * @orig_desc_size: Expected size of the original report descriptor to
626 * be replaced.
627 * @desc_ptr: Pointer to the replacement report descriptor.
628 * Can be NULL, if desc_size is zero.
629 * @desc_size: Size of the replacement report descriptor.
630 *
631 * Returns:
632 * Zero, if successful. -EINVAL if an invalid argument was passed.
633 * -ENOMEM, if failed to allocate memory.
634 */
635static int uclogic_params_init_with_opt_desc(struct uclogic_params *params,
636 struct hid_device *hdev,
637 unsigned int orig_desc_size,
638 __u8 *desc_ptr,
639 unsigned int desc_size)
640{
641 __u8 *desc_copy_ptr = NULL;
642 unsigned int desc_copy_size;
643 int rc;
644
645 /* Check arguments */
646 if (params == NULL || hdev == NULL ||
647 (desc_ptr == NULL && desc_size != 0)) {
648 rc = -EINVAL;
649 goto cleanup;
650 }
651
652 /* Replace report descriptor, if it matches */
653 if (hdev->dev_rsize == orig_desc_size) {
654 hid_dbg(hdev,
655 "device report descriptor matches the expected size, replacing\n");
656 desc_copy_ptr = kmemdup(desc_ptr, desc_size, GFP_KERNEL);
657 if (desc_copy_ptr == NULL) {
658 rc = -ENOMEM;
659 goto cleanup;
660 }
661 desc_copy_size = desc_size;
662 } else {
663 hid_dbg(hdev,
664 "device report descriptor doesn't match the expected size (%u != %u), preserving\n",
665 hdev->dev_rsize, orig_desc_size);
666 desc_copy_ptr = NULL;
667 desc_copy_size = 0;
668 }
669
670 /* Output parameters */
671 memset(params, 0, sizeof(*params));
672 params->desc_ptr = desc_copy_ptr;
673 desc_copy_ptr = NULL;
674 params->desc_size = desc_copy_size;
675
676 rc = 0;
677cleanup:
678 kfree(desc_copy_ptr);
679 return rc;
680}
681
9614219e 682/**
5abb5445 683 * uclogic_params_huion_init() - initialize a Huion tablet interface and discover
9614219e
NK
684 * its parameters.
685 *
686 * @params: Parameters to fill in (to be cleaned with
687 * uclogic_params_cleanup()). Not modified in case of error.
688 * Cannot be NULL.
689 * @hdev: The HID device of the tablet interface to initialize and get
690 * parameters from. Cannot be NULL.
691 *
692 * Returns:
693 * Zero, if successful. A negative errno code on error.
694 */
695static int uclogic_params_huion_init(struct uclogic_params *params,
696 struct hid_device *hdev)
697{
698 int rc;
ff6b548a
JE
699 struct usb_device *udev;
700 struct usb_interface *iface;
701 __u8 bInterfaceNumber;
9614219e
NK
702 bool found;
703 /* The resulting parameters (noop) */
704 struct uclogic_params p = {0, };
2c3a88c6
NK
705 static const char transition_ver[] = "HUION_T153_160607";
706 char *ver_ptr = NULL;
707 const size_t ver_len = sizeof(transition_ver) + 1;
9614219e
NK
708
709 /* Check arguments */
710 if (params == NULL || hdev == NULL) {
711 rc = -EINVAL;
712 goto cleanup;
713 }
714
ff6b548a
JE
715 udev = hid_to_usb_dev(hdev);
716 iface = to_usb_interface(hdev->dev.parent);
717 bInterfaceNumber = iface->cur_altsetting->desc.bInterfaceNumber;
718
9614219e
NK
719 /* If it's not a pen interface */
720 if (bInterfaceNumber != 0) {
606dadc1 721 uclogic_params_init_invalid(&p);
9614219e
NK
722 goto output;
723 }
724
2c3a88c6
NK
725 /* Try to get firmware version */
726 ver_ptr = kzalloc(ver_len, GFP_KERNEL);
727 if (ver_ptr == NULL) {
728 rc = -ENOMEM;
729 goto cleanup;
730 }
731 rc = usb_string(udev, 201, ver_ptr, ver_len);
2c3a88c6
NK
732 if (rc == -EPIPE) {
733 *ver_ptr = '\0';
734 } else if (rc < 0) {
735 hid_err(hdev,
736 "failed retrieving Huion firmware version: %d\n", rc);
737 goto cleanup;
738 }
739
740 /* If this is a transition firmware */
741 if (strcmp(ver_ptr, transition_ver) == 0) {
742 hid_dbg(hdev,
743 "transition firmware detected, not probing pen v2 parameters\n");
744 } else {
745 /* Try to probe v2 pen parameters */
746 rc = uclogic_params_pen_init_v2(&p.pen, &found, hdev);
747 if (rc != 0) {
748 hid_err(hdev,
749 "failed probing pen v2 parameters: %d\n", rc);
750 goto cleanup;
751 } else if (found) {
752 hid_dbg(hdev, "pen v2 parameters found\n");
753 /* Create v2 buttonpad parameters */
754 rc = uclogic_params_frame_init_with_desc(
755 &p.frame,
756 uclogic_rdesc_buttonpad_v2_arr,
757 uclogic_rdesc_buttonpad_v2_size,
758 UCLOGIC_RDESC_BUTTONPAD_V2_ID);
759 if (rc != 0) {
760 hid_err(hdev,
761 "failed creating v2 buttonpad parameters: %d\n",
762 rc);
763 goto cleanup;
764 }
765 /* Set bitmask marking frame reports in pen reports */
766 p.pen_frame_flag = 0x20;
767 goto output;
768 }
769 hid_dbg(hdev, "pen v2 parameters not found\n");
770 }
771
eecb5b84
NK
772 /* Try to probe v1 pen parameters */
773 rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
9614219e
NK
774 if (rc != 0) {
775 hid_err(hdev,
eecb5b84 776 "failed probing pen v1 parameters: %d\n", rc);
9614219e
NK
777 goto cleanup;
778 } else if (found) {
eecb5b84
NK
779 hid_dbg(hdev, "pen v1 parameters found\n");
780 /* Try to probe v1 buttonpad */
781 rc = uclogic_params_frame_init_v1_buttonpad(
9614219e
NK
782 &p.frame,
783 &found, hdev);
784 if (rc != 0) {
785 hid_err(hdev, "v1 buttonpad probing failed: %d\n", rc);
786 goto cleanup;
787 }
eecb5b84 788 hid_dbg(hdev, "buttonpad v1 parameters%s found\n",
9614219e
NK
789 (found ? "" : " not"));
790 if (found) {
791 /* Set bitmask marking frame reports */
792 p.pen_frame_flag = 0x20;
793 }
794 goto output;
795 }
eecb5b84 796 hid_dbg(hdev, "pen v1 parameters not found\n");
9614219e
NK
797
798 uclogic_params_init_invalid(&p);
799
800output:
801 /* Output parameters */
802 memcpy(params, &p, sizeof(*params));
803 memset(&p, 0, sizeof(p));
804 rc = 0;
805cleanup:
2c3a88c6 806 kfree(ver_ptr);
9614219e
NK
807 uclogic_params_cleanup(&p);
808 return rc;
809}
810
811/**
812 * uclogic_params_init() - initialize a tablet interface and discover its
813 * parameters.
814 *
815 * @params: Parameters to fill in (to be cleaned with
816 * uclogic_params_cleanup()). Not modified in case of error.
817 * Cannot be NULL.
818 * @hdev: The HID device of the tablet interface to initialize and get
8547b778
NK
819 * parameters from. Cannot be NULL. Must be using the USB low-level
820 * driver, i.e. be an actual USB tablet.
9614219e
NK
821 *
822 * Returns:
823 * Zero, if successful. A negative errno code on error.
824 */
825int uclogic_params_init(struct uclogic_params *params,
826 struct hid_device *hdev)
827{
828 int rc;
f364c571
JE
829 struct usb_device *udev;
830 __u8 bNumInterfaces;
831 struct usb_interface *iface;
832 __u8 bInterfaceNumber;
9614219e
NK
833 bool found;
834 /* The resulting parameters (noop) */
835 struct uclogic_params p = {0, };
836
837 /* Check arguments */
f83baa0c 838 if (params == NULL || hdev == NULL || !hid_is_usb(hdev)) {
9614219e
NK
839 rc = -EINVAL;
840 goto cleanup;
841 }
842
f364c571
JE
843 udev = hid_to_usb_dev(hdev);
844 bNumInterfaces = udev->config->desc.bNumInterfaces;
845 iface = to_usb_interface(hdev->dev.parent);
846 bInterfaceNumber = iface->cur_altsetting->desc.bInterfaceNumber;
847
9614219e
NK
848 /*
849 * Set replacement report descriptor if the original matches the
850 * specified size. Otherwise keep interface unchanged.
851 */
852#define WITH_OPT_DESC(_orig_desc_token, _new_desc_token) \
853 uclogic_params_init_with_opt_desc( \
854 &p, hdev, \
855 UCLOGIC_RDESC_##_orig_desc_token##_SIZE, \
856 uclogic_rdesc_##_new_desc_token##_arr, \
857 uclogic_rdesc_##_new_desc_token##_size)
858
859#define VID_PID(_vid, _pid) \
860 (((__u32)(_vid) << 16) | ((__u32)(_pid) & U16_MAX))
861
862 /*
863 * Handle specific interfaces for specific tablets.
864 *
865 * Observe the following logic:
866 *
867 * If the interface is recognized as producing certain useful input:
868 * Mark interface as valid.
869 * Output interface parameters.
870 * Else, if the interface is recognized as *not* producing any useful
871 * input:
872 * Mark interface as invalid.
873 * Else:
874 * Mark interface as valid.
875 * Output noop parameters.
876 *
877 * Rule of thumb: it is better to disable a broken interface than let
878 * it spew garbage input.
879 */
880
881 switch (VID_PID(hdev->vendor, hdev->product)) {
882 case VID_PID(USB_VENDOR_ID_UCLOGIC,
883 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209):
884 rc = WITH_OPT_DESC(PF1209_ORIG, pf1209_fixed);
885 if (rc != 0)
886 goto cleanup;
887 break;
888 case VID_PID(USB_VENDOR_ID_UCLOGIC,
889 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U):
890 rc = WITH_OPT_DESC(WPXXXXU_ORIG, wp4030u_fixed);
891 if (rc != 0)
892 goto cleanup;
893 break;
894 case VID_PID(USB_VENDOR_ID_UCLOGIC,
895 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U):
9c17f735
NK
896 if (hdev->dev_rsize == UCLOGIC_RDESC_WP5540U_V2_ORIG_SIZE) {
897 if (bInterfaceNumber == 0) {
898 /* Try to probe v1 pen parameters */
899 rc = uclogic_params_pen_init_v1(&p.pen,
900 &found, hdev);
901 if (rc != 0) {
902 hid_err(hdev,
903 "pen probing failed: %d\n",
904 rc);
905 goto cleanup;
906 }
907 if (!found) {
908 hid_warn(hdev,
909 "pen parameters not found");
910 }
911 } else {
912 uclogic_params_init_invalid(&p);
913 }
914 } else {
915 rc = WITH_OPT_DESC(WPXXXXU_ORIG, wp5540u_fixed);
916 if (rc != 0)
917 goto cleanup;
918 }
9614219e
NK
919 break;
920 case VID_PID(USB_VENDOR_ID_UCLOGIC,
921 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U):
922 rc = WITH_OPT_DESC(WPXXXXU_ORIG, wp8060u_fixed);
923 if (rc != 0)
924 goto cleanup;
925 break;
926 case VID_PID(USB_VENDOR_ID_UCLOGIC,
927 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062):
928 rc = WITH_OPT_DESC(WP1062_ORIG, wp1062_fixed);
929 if (rc != 0)
930 goto cleanup;
931 break;
932 case VID_PID(USB_VENDOR_ID_UCLOGIC,
933 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850):
934 switch (bInterfaceNumber) {
935 case 0:
936 rc = WITH_OPT_DESC(TWHL850_ORIG0, twhl850_fixed0);
937 if (rc != 0)
938 goto cleanup;
939 break;
940 case 1:
941 rc = WITH_OPT_DESC(TWHL850_ORIG1, twhl850_fixed1);
942 if (rc != 0)
943 goto cleanup;
944 break;
945 case 2:
946 rc = WITH_OPT_DESC(TWHL850_ORIG2, twhl850_fixed2);
947 if (rc != 0)
948 goto cleanup;
949 break;
950 }
951 break;
952 case VID_PID(USB_VENDOR_ID_UCLOGIC,
953 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60):
954 /*
955 * If it is not a three-interface version, which is known to
956 * respond to initialization.
957 */
958 if (bNumInterfaces != 3) {
959 switch (bInterfaceNumber) {
960 case 0:
961 rc = WITH_OPT_DESC(TWHA60_ORIG0,
962 twha60_fixed0);
963 if (rc != 0)
964 goto cleanup;
965 break;
966 case 1:
967 rc = WITH_OPT_DESC(TWHA60_ORIG1,
968 twha60_fixed1);
969 if (rc != 0)
970 goto cleanup;
971 break;
972 }
973 break;
974 }
df561f66 975 fallthrough;
9614219e
NK
976 case VID_PID(USB_VENDOR_ID_HUION,
977 USB_DEVICE_ID_HUION_TABLET):
315ffcc9 978 case VID_PID(USB_VENDOR_ID_HUION,
85e86071 979 USB_DEVICE_ID_HUION_TABLET2):
9614219e
NK
980 case VID_PID(USB_VENDOR_ID_UCLOGIC,
981 USB_DEVICE_ID_HUION_TABLET):
982 case VID_PID(USB_VENDOR_ID_UCLOGIC,
983 USB_DEVICE_ID_YIYNOVA_TABLET):
984 case VID_PID(USB_VENDOR_ID_UCLOGIC,
985 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81):
986 case VID_PID(USB_VENDOR_ID_UCLOGIC,
987 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3):
988 case VID_PID(USB_VENDOR_ID_UCLOGIC,
989 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45):
0c15efe9
NK
990 case VID_PID(USB_VENDOR_ID_UCLOGIC,
991 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47):
9614219e
NK
992 rc = uclogic_params_huion_init(&p, hdev);
993 if (rc != 0)
994 goto cleanup;
995 break;
996 case VID_PID(USB_VENDOR_ID_UGTIZER,
997 USB_DEVICE_ID_UGTIZER_TABLET_GP0610):
022fc531
MS
998 case VID_PID(USB_VENDOR_ID_UGTIZER,
999 USB_DEVICE_ID_UGTIZER_TABLET_GT5040):
c3e5a67c
NK
1000 case VID_PID(USB_VENDOR_ID_UGEE,
1001 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540):
492a9e9a
NK
1002 case VID_PID(USB_VENDOR_ID_UGEE,
1003 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640):
88bb346d
WX
1004 case VID_PID(USB_VENDOR_ID_UGEE,
1005 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720):
9614219e
NK
1006 /* If this is the pen interface */
1007 if (bInterfaceNumber == 1) {
eecb5b84
NK
1008 /* Probe v1 pen parameters */
1009 rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
9614219e
NK
1010 if (rc != 0) {
1011 hid_err(hdev, "pen probing failed: %d\n", rc);
1012 goto cleanup;
1013 }
1014 if (!found) {
1015 hid_warn(hdev, "pen parameters not found");
1016 uclogic_params_init_invalid(&p);
1017 }
1018 } else {
606dadc1 1019 uclogic_params_init_invalid(&p);
08367be1
NK
1020 }
1021 break;
1022 case VID_PID(USB_VENDOR_ID_UGEE,
1023 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01):
1024 /* If this is the pen and frame interface */
1025 if (bInterfaceNumber == 1) {
1026 /* Probe v1 pen parameters */
1027 rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
1028 if (rc != 0) {
1029 hid_err(hdev, "pen probing failed: %d\n", rc);
1030 goto cleanup;
1031 }
1032 /* Initialize frame parameters */
1033 rc = uclogic_params_frame_init_with_desc(
1034 &p.frame,
1035 uclogic_rdesc_xppen_deco01_frame_arr,
1036 uclogic_rdesc_xppen_deco01_frame_size,
1037 0);
1038 if (rc != 0)
1039 goto cleanup;
1040 } else {
606dadc1 1041 uclogic_params_init_invalid(&p);
9614219e 1042 }
e902ed93 1043 break;
f7271b2a
CK
1044 case VID_PID(USB_VENDOR_ID_TRUST,
1045 USB_DEVICE_ID_TRUST_PANORA_TABLET):
e902ed93
NK
1046 case VID_PID(USB_VENDOR_ID_UGEE,
1047 USB_DEVICE_ID_UGEE_TABLET_G5):
1048 /* Ignore non-pen interfaces */
1049 if (bInterfaceNumber != 1) {
1050 uclogic_params_init_invalid(&p);
1051 break;
1052 }
1053
1054 rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
1055 if (rc != 0) {
1056 hid_err(hdev, "pen probing failed: %d\n", rc);
1057 goto cleanup;
1058 } else if (found) {
1059 rc = uclogic_params_frame_init_with_desc(
1060 &p.frame,
1061 uclogic_rdesc_ugee_g5_frame_arr,
1062 uclogic_rdesc_ugee_g5_frame_size,
1063 UCLOGIC_RDESC_UGEE_G5_FRAME_ID);
1064 if (rc != 0) {
1065 hid_err(hdev,
1066 "failed creating buttonpad parameters: %d\n",
1067 rc);
1068 goto cleanup;
1069 }
1070 p.frame.re_lsb =
1071 UCLOGIC_RDESC_UGEE_G5_FRAME_RE_LSB;
1072 p.frame.dev_id_byte =
1073 UCLOGIC_RDESC_UGEE_G5_FRAME_DEV_ID_BYTE;
1074 } else {
1075 hid_warn(hdev, "pen parameters not found");
1076 uclogic_params_init_invalid(&p);
1077 }
1078
1ee7c685
NK
1079 break;
1080 case VID_PID(USB_VENDOR_ID_UGEE,
1081 USB_DEVICE_ID_UGEE_TABLET_EX07S):
1082 /* Ignore non-pen interfaces */
1083 if (bInterfaceNumber != 1) {
1084 uclogic_params_init_invalid(&p);
1085 break;
1086 }
1087
1088 rc = uclogic_params_pen_init_v1(&p.pen, &found, hdev);
1089 if (rc != 0) {
1090 hid_err(hdev, "pen probing failed: %d\n", rc);
1091 goto cleanup;
1092 } else if (found) {
1093 rc = uclogic_params_frame_init_with_desc(
1094 &p.frame,
1095 uclogic_rdesc_ugee_ex07_buttonpad_arr,
1096 uclogic_rdesc_ugee_ex07_buttonpad_size,
1097 0);
1098 if (rc != 0) {
1099 hid_err(hdev,
1100 "failed creating buttonpad parameters: %d\n",
1101 rc);
1102 goto cleanup;
1103 }
1104 } else {
1105 hid_warn(hdev, "pen parameters not found");
1106 uclogic_params_init_invalid(&p);
1107 }
1108
9614219e
NK
1109 break;
1110 }
1111
1112#undef VID_PID
1113#undef WITH_OPT_DESC
1114
1115 /* Output parameters */
1116 memcpy(params, &p, sizeof(*params));
1117 memset(&p, 0, sizeof(p));
1118 rc = 0;
1119cleanup:
1120 uclogic_params_cleanup(&p);
1121 return rc;
1122}