Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  HID driver for UC-Logic devices not fully compliant with HID standard
0004  *
0005  *  Copyright (c) 2010-2014 Nikolai Kondrashov
0006  *  Copyright (c) 2013 Martin Rusko
0007  */
0008 
0009 /*
0010  * This program is free software; you can redistribute it and/or modify it
0011  * under the terms of the GNU General Public License as published by the Free
0012  * Software Foundation; either version 2 of the License, or (at your option)
0013  * any later version.
0014  */
0015 
0016 #include <linux/device.h>
0017 #include <linux/hid.h>
0018 #include <linux/module.h>
0019 #include <linux/timer.h>
0020 #include "usbhid/usbhid.h"
0021 #include "hid-uclogic-params.h"
0022 
0023 #include "hid-ids.h"
0024 
0025 /* Driver data */
0026 struct uclogic_drvdata {
0027     /* Interface parameters */
0028     struct uclogic_params params;
0029     /* Pointer to the replacement report descriptor. NULL if none. */
0030     __u8 *desc_ptr;
0031     /*
0032      * Size of the replacement report descriptor.
0033      * Only valid if desc_ptr is not NULL
0034      */
0035     unsigned int desc_size;
0036     /* Pen input device */
0037     struct input_dev *pen_input;
0038     /* In-range timer */
0039     struct timer_list inrange_timer;
0040     /* Last rotary encoder state, or U8_MAX for none */
0041     u8 re_state;
0042 };
0043 
0044 /**
0045  * uclogic_inrange_timeout - handle pen in-range state timeout.
0046  * Emulate input events normally generated when pen goes out of range for
0047  * tablets which don't report that.
0048  *
0049  * @t:  The timer the timeout handler is attached to, stored in a struct
0050  *  uclogic_drvdata.
0051  */
0052 static void uclogic_inrange_timeout(struct timer_list *t)
0053 {
0054     struct uclogic_drvdata *drvdata = from_timer(drvdata, t,
0055                             inrange_timer);
0056     struct input_dev *input = drvdata->pen_input;
0057 
0058     if (input == NULL)
0059         return;
0060     input_report_abs(input, ABS_PRESSURE, 0);
0061     /* If BTN_TOUCH state is changing */
0062     if (test_bit(BTN_TOUCH, input->key)) {
0063         input_event(input, EV_MSC, MSC_SCAN,
0064                 /* Digitizer Tip Switch usage */
0065                 0xd0042);
0066         input_report_key(input, BTN_TOUCH, 0);
0067     }
0068     input_report_key(input, BTN_TOOL_PEN, 0);
0069     input_sync(input);
0070 }
0071 
0072 static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
0073                     unsigned int *rsize)
0074 {
0075     struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
0076 
0077     if (drvdata->desc_ptr != NULL) {
0078         rdesc = drvdata->desc_ptr;
0079         *rsize = drvdata->desc_size;
0080     }
0081     return rdesc;
0082 }
0083 
0084 static int uclogic_input_mapping(struct hid_device *hdev,
0085                  struct hid_input *hi,
0086                  struct hid_field *field,
0087                  struct hid_usage *usage,
0088                  unsigned long **bit,
0089                  int *max)
0090 {
0091     struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
0092     struct uclogic_params *params = &drvdata->params;
0093 
0094     /* Discard invalid pen usages */
0095     if (params->pen.usage_invalid && (field->application == HID_DG_PEN))
0096         return -1;
0097 
0098     /* Let hid-core decide what to do */
0099     return 0;
0100 }
0101 
0102 static int uclogic_input_configured(struct hid_device *hdev,
0103         struct hid_input *hi)
0104 {
0105     struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
0106     struct uclogic_params *params = &drvdata->params;
0107     char *name;
0108     const char *suffix = NULL;
0109     struct hid_field *field;
0110     size_t len;
0111     size_t i;
0112     const struct uclogic_params_frame *frame;
0113 
0114     /* no report associated (HID_QUIRK_MULTI_INPUT not set) */
0115     if (!hi->report)
0116         return 0;
0117 
0118     /*
0119      * If this is the input corresponding to the pen report
0120      * in need of tweaking.
0121      */
0122     if (hi->report->id == params->pen.id) {
0123         /* Remember the input device so we can simulate events */
0124         drvdata->pen_input = hi->input;
0125     }
0126 
0127     /* If it's one of the frame devices */
0128     for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
0129         frame = &params->frame_list[i];
0130         if (hi->report->id == frame->id) {
0131             /* Assign custom suffix, if any */
0132             suffix = frame->suffix;
0133             /*
0134              * Disable EV_MSC reports for touch ring interfaces to
0135              * make the Wacom driver pickup touch ring extents
0136              */
0137             if (frame->touch_byte > 0)
0138                 __clear_bit(EV_MSC, hi->input->evbit);
0139         }
0140     }
0141 
0142     if (!suffix) {
0143         field = hi->report->field[0];
0144 
0145         switch (field->application) {
0146         case HID_GD_KEYBOARD:
0147             suffix = "Keyboard";
0148             break;
0149         case HID_GD_MOUSE:
0150             suffix = "Mouse";
0151             break;
0152         case HID_GD_KEYPAD:
0153             suffix = "Pad";
0154             break;
0155         case HID_DG_PEN:
0156             suffix = "Pen";
0157             break;
0158         case HID_CP_CONSUMER_CONTROL:
0159             suffix = "Consumer Control";
0160             break;
0161         case HID_GD_SYSTEM_CONTROL:
0162             suffix = "System Control";
0163             break;
0164         }
0165     }
0166 
0167     if (suffix) {
0168         len = strlen(hdev->name) + 2 + strlen(suffix);
0169         name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL);
0170         if (name) {
0171             snprintf(name, len, "%s %s", hdev->name, suffix);
0172             hi->input->name = name;
0173         }
0174     }
0175 
0176     return 0;
0177 }
0178 
0179 static int uclogic_probe(struct hid_device *hdev,
0180         const struct hid_device_id *id)
0181 {
0182     int rc;
0183     struct uclogic_drvdata *drvdata = NULL;
0184     bool params_initialized = false;
0185 
0186     if (!hid_is_usb(hdev))
0187         return -EINVAL;
0188 
0189     /*
0190      * libinput requires the pad interface to be on a different node
0191      * than the pen, so use QUIRK_MULTI_INPUT for all tablets.
0192      */
0193     hdev->quirks |= HID_QUIRK_MULTI_INPUT;
0194 
0195     /* Allocate and assign driver data */
0196     drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
0197     if (drvdata == NULL) {
0198         rc = -ENOMEM;
0199         goto failure;
0200     }
0201     timer_setup(&drvdata->inrange_timer, uclogic_inrange_timeout, 0);
0202     drvdata->re_state = U8_MAX;
0203     hid_set_drvdata(hdev, drvdata);
0204 
0205     /* Initialize the device and retrieve interface parameters */
0206     rc = uclogic_params_init(&drvdata->params, hdev);
0207     if (rc != 0) {
0208         hid_err(hdev, "failed probing parameters: %d\n", rc);
0209         goto failure;
0210     }
0211     params_initialized = true;
0212     hid_dbg(hdev, "parameters:\n");
0213     uclogic_params_hid_dbg(hdev, &drvdata->params);
0214     if (drvdata->params.invalid) {
0215         hid_info(hdev, "interface is invalid, ignoring\n");
0216         rc = -ENODEV;
0217         goto failure;
0218     }
0219 
0220     /* Generate replacement report descriptor */
0221     rc = uclogic_params_get_desc(&drvdata->params,
0222                      &drvdata->desc_ptr,
0223                      &drvdata->desc_size);
0224     if (rc) {
0225         hid_err(hdev,
0226             "failed generating replacement report descriptor: %d\n",
0227             rc);
0228         goto failure;
0229     }
0230 
0231     rc = hid_parse(hdev);
0232     if (rc) {
0233         hid_err(hdev, "parse failed\n");
0234         goto failure;
0235     }
0236 
0237     rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0238     if (rc) {
0239         hid_err(hdev, "hw start failed\n");
0240         goto failure;
0241     }
0242 
0243     return 0;
0244 failure:
0245     /* Assume "remove" might not be called if "probe" failed */
0246     if (params_initialized)
0247         uclogic_params_cleanup(&drvdata->params);
0248     return rc;
0249 }
0250 
0251 #ifdef CONFIG_PM
0252 static int uclogic_resume(struct hid_device *hdev)
0253 {
0254     int rc;
0255     struct uclogic_params params;
0256 
0257     /* Re-initialize the device, but discard parameters */
0258     rc = uclogic_params_init(&params, hdev);
0259     if (rc != 0)
0260         hid_err(hdev, "failed to re-initialize the device\n");
0261     else
0262         uclogic_params_cleanup(&params);
0263 
0264     return rc;
0265 }
0266 #endif
0267 
0268 /**
0269  * uclogic_raw_event_pen - handle raw pen events (pen HID reports).
0270  *
0271  * @drvdata:    Driver data.
0272  * @data:   Report data buffer, can be modified.
0273  * @size:   Report data size, bytes.
0274  *
0275  * Returns:
0276  *  Negative value on error (stops event delivery), zero for success.
0277  */
0278 static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata,
0279                     u8 *data, int size)
0280 {
0281     struct uclogic_params_pen *pen = &drvdata->params.pen;
0282 
0283     WARN_ON(drvdata == NULL);
0284     WARN_ON(data == NULL && size != 0);
0285 
0286     /* If in-range reports are inverted */
0287     if (pen->inrange ==
0288         UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) {
0289         /* Invert the in-range bit */
0290         data[1] ^= 0x40;
0291     }
0292     /*
0293      * If report contains fragmented high-resolution pen
0294      * coordinates
0295      */
0296     if (size >= 10 && pen->fragmented_hires) {
0297         u8 pressure_low_byte;
0298         u8 pressure_high_byte;
0299 
0300         /* Lift pressure bytes */
0301         pressure_low_byte = data[6];
0302         pressure_high_byte = data[7];
0303         /*
0304          * Move Y coord to make space for high-order X
0305          * coord byte
0306          */
0307         data[6] = data[5];
0308         data[5] = data[4];
0309         /* Move high-order X coord byte */
0310         data[4] = data[8];
0311         /* Move high-order Y coord byte */
0312         data[7] = data[9];
0313         /* Place pressure bytes */
0314         data[8] = pressure_low_byte;
0315         data[9] = pressure_high_byte;
0316     }
0317     /* If we need to emulate in-range detection */
0318     if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) {
0319         /* Set in-range bit */
0320         data[1] |= 0x40;
0321         /* (Re-)start in-range timeout */
0322         mod_timer(&drvdata->inrange_timer,
0323                 jiffies + msecs_to_jiffies(100));
0324     }
0325     /* If we report tilt and Y direction is flipped */
0326     if (size >= 12 && pen->tilt_y_flipped)
0327         data[11] = -data[11];
0328 
0329     return 0;
0330 }
0331 
0332 /**
0333  * uclogic_raw_event_frame - handle raw frame events (frame HID reports).
0334  *
0335  * @drvdata:    Driver data.
0336  * @frame:  The parameters of the frame controls to handle.
0337  * @data:   Report data buffer, can be modified.
0338  * @size:   Report data size, bytes.
0339  *
0340  * Returns:
0341  *  Negative value on error (stops event delivery), zero for success.
0342  */
0343 static int uclogic_raw_event_frame(
0344         struct uclogic_drvdata *drvdata,
0345         const struct uclogic_params_frame *frame,
0346         u8 *data, int size)
0347 {
0348     WARN_ON(drvdata == NULL);
0349     WARN_ON(data == NULL && size != 0);
0350 
0351     /* If need to, and can, set pad device ID for Wacom drivers */
0352     if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) {
0353         /* If we also have a touch ring and the finger left it */
0354         if (frame->touch_byte > 0 && frame->touch_byte < size &&
0355             data[frame->touch_byte] == 0) {
0356             data[frame->dev_id_byte] = 0;
0357         } else {
0358             data[frame->dev_id_byte] = 0xf;
0359         }
0360     }
0361 
0362     /* If need to, and can, read rotary encoder state change */
0363     if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) {
0364         unsigned int byte = frame->re_lsb / 8;
0365         unsigned int bit = frame->re_lsb % 8;
0366 
0367         u8 change;
0368         u8 prev_state = drvdata->re_state;
0369         /* Read Gray-coded state */
0370         u8 state = (data[byte] >> bit) & 0x3;
0371         /* Encode state change into 2-bit signed integer */
0372         if ((prev_state == 1 && state == 0) ||
0373             (prev_state == 2 && state == 3)) {
0374             change = 1;
0375         } else if ((prev_state == 2 && state == 0) ||
0376                (prev_state == 1 && state == 3)) {
0377             change = 3;
0378         } else {
0379             change = 0;
0380         }
0381         /* Write change */
0382         data[byte] = (data[byte] & ~((u8)3 << bit)) |
0383                 (change << bit);
0384         /* Remember state */
0385         drvdata->re_state = state;
0386     }
0387 
0388     /* If need to, and can, transform the touch ring reports */
0389     if (frame->touch_byte > 0 && frame->touch_byte < size) {
0390         __s8 value = data[frame->touch_byte];
0391 
0392         if (value != 0) {
0393             if (frame->touch_flip_at != 0) {
0394                 value = frame->touch_flip_at - value;
0395                 if (value <= 0)
0396                     value = frame->touch_max + value;
0397             }
0398             data[frame->touch_byte] = value - 1;
0399         }
0400     }
0401 
0402     /* If need to, and can, transform the bitmap dial reports */
0403     if (frame->bitmap_dial_byte > 0 && frame->bitmap_dial_byte < size) {
0404         if (data[frame->bitmap_dial_byte] == 2)
0405             data[frame->bitmap_dial_byte] = -1;
0406     }
0407 
0408     return 0;
0409 }
0410 
0411 static int uclogic_raw_event(struct hid_device *hdev,
0412                 struct hid_report *report,
0413                 u8 *data, int size)
0414 {
0415     unsigned int report_id = report->id;
0416     struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
0417     struct uclogic_params *params = &drvdata->params;
0418     struct uclogic_params_pen_subreport *subreport;
0419     struct uclogic_params_pen_subreport *subreport_list_end;
0420     size_t i;
0421 
0422     /* Do not handle anything but input reports */
0423     if (report->type != HID_INPUT_REPORT)
0424         return 0;
0425 
0426     while (true) {
0427         /* Tweak pen reports, if necessary */
0428         if ((report_id == params->pen.id) && (size >= 2)) {
0429             subreport_list_end =
0430                 params->pen.subreport_list +
0431                 ARRAY_SIZE(params->pen.subreport_list);
0432             /* Try to match a subreport */
0433             for (subreport = params->pen.subreport_list;
0434                  subreport < subreport_list_end; subreport++) {
0435                 if (subreport->value != 0 &&
0436                     subreport->value == data[1]) {
0437                     break;
0438                 }
0439             }
0440             /* If a subreport matched */
0441             if (subreport < subreport_list_end) {
0442                 /* Change to subreport ID, and restart */
0443                 report_id = data[0] = subreport->id;
0444                 continue;
0445             } else {
0446                 return uclogic_raw_event_pen(drvdata, data, size);
0447             }
0448         }
0449 
0450         /* Tweak frame control reports, if necessary */
0451         for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) {
0452             if (report_id == params->frame_list[i].id) {
0453                 return uclogic_raw_event_frame(
0454                     drvdata, &params->frame_list[i],
0455                     data, size);
0456             }
0457         }
0458 
0459         break;
0460     }
0461 
0462     return 0;
0463 }
0464 
0465 static void uclogic_remove(struct hid_device *hdev)
0466 {
0467     struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev);
0468 
0469     del_timer_sync(&drvdata->inrange_timer);
0470     hid_hw_stop(hdev);
0471     kfree(drvdata->desc_ptr);
0472     uclogic_params_cleanup(&drvdata->params);
0473 }
0474 
0475 static const struct hid_device_id uclogic_devices[] = {
0476     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0477                 USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) },
0478     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0479                 USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) },
0480     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0481                 USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) },
0482     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0483                 USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) },
0484     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0485                 USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) },
0486     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0487                 USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) },
0488     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0489                 USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) },
0490     { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
0491                 USB_DEVICE_ID_HUION_TABLET) },
0492     { HID_USB_DEVICE(USB_VENDOR_ID_HUION,
0493                 USB_DEVICE_ID_HUION_TABLET2) },
0494     { HID_USB_DEVICE(USB_VENDOR_ID_TRUST,
0495                 USB_DEVICE_ID_TRUST_PANORA_TABLET) },
0496     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0497                 USB_DEVICE_ID_HUION_TABLET) },
0498     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0499                 USB_DEVICE_ID_YIYNOVA_TABLET) },
0500     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0501                 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) },
0502     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0503                 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) },
0504     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0505                 USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_47) },
0506     { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC,
0507                 USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) },
0508     { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
0509                 USB_DEVICE_ID_UGTIZER_TABLET_GP0610) },
0510     { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER,
0511                 USB_DEVICE_ID_UGTIZER_TABLET_GT5040) },
0512     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0513                 USB_DEVICE_ID_UGEE_TABLET_G5) },
0514     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0515                 USB_DEVICE_ID_UGEE_TABLET_EX07S) },
0516     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0517                 USB_DEVICE_ID_UGEE_TABLET_RAINBOW_CV720) },
0518     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0519                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G540) },
0520     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0521                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_G640) },
0522     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0523                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO01) },
0524     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0525                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_DECO_L) },
0526     { HID_USB_DEVICE(USB_VENDOR_ID_UGEE,
0527                 USB_DEVICE_ID_UGEE_XPPEN_TABLET_STAR06) },
0528     { }
0529 };
0530 MODULE_DEVICE_TABLE(hid, uclogic_devices);
0531 
0532 static struct hid_driver uclogic_driver = {
0533     .name = "uclogic",
0534     .id_table = uclogic_devices,
0535     .probe = uclogic_probe,
0536     .remove = uclogic_remove,
0537     .report_fixup = uclogic_report_fixup,
0538     .raw_event = uclogic_raw_event,
0539     .input_mapping = uclogic_input_mapping,
0540     .input_configured = uclogic_input_configured,
0541 #ifdef CONFIG_PM
0542     .resume           = uclogic_resume,
0543     .reset_resume     = uclogic_resume,
0544 #endif
0545 };
0546 module_hid_driver(uclogic_driver);
0547 
0548 MODULE_AUTHOR("Martin Rusko");
0549 MODULE_AUTHOR("Nikolai Kondrashov");
0550 MODULE_LICENSE("GPL");