1ff0c13d6SNikolai Kondrashov // SPDX-License-Identifier: GPL-2.0+ 2ff0c13d6SNikolai Kondrashov /* 3ff0c13d6SNikolai Kondrashov * HID driver for UC-Logic devices not fully compliant with HID standard 4ff0c13d6SNikolai Kondrashov * 5ff0c13d6SNikolai Kondrashov * Copyright (c) 2010-2014 Nikolai Kondrashov 6ff0c13d6SNikolai Kondrashov * Copyright (c) 2013 Martin Rusko 7ff0c13d6SNikolai Kondrashov */ 8ff0c13d6SNikolai Kondrashov 9ff0c13d6SNikolai Kondrashov /* 10ff0c13d6SNikolai Kondrashov * This program is free software; you can redistribute it and/or modify it 11ff0c13d6SNikolai Kondrashov * under the terms of the GNU General Public License as published by the Free 12ff0c13d6SNikolai Kondrashov * Software Foundation; either version 2 of the License, or (at your option) 13ff0c13d6SNikolai Kondrashov * any later version. 14ff0c13d6SNikolai Kondrashov */ 15ff0c13d6SNikolai Kondrashov 16ff0c13d6SNikolai Kondrashov #include <linux/device.h> 17ff0c13d6SNikolai Kondrashov #include <linux/hid.h> 18ff0c13d6SNikolai Kondrashov #include <linux/module.h> 19ff0c13d6SNikolai Kondrashov #include "usbhid/usbhid.h" 209614219eSNikolai Kondrashov #include "hid-uclogic-params.h" 21ff0c13d6SNikolai Kondrashov 22ff0c13d6SNikolai Kondrashov #include "hid-ids.h" 23ff0c13d6SNikolai Kondrashov 24ff0c13d6SNikolai Kondrashov /* Driver data */ 25ff0c13d6SNikolai Kondrashov struct uclogic_drvdata { 269614219eSNikolai Kondrashov /* Interface parameters */ 279614219eSNikolai Kondrashov struct uclogic_params params; 289614219eSNikolai Kondrashov /* Pointer to the replacement report descriptor. NULL if none. */ 299614219eSNikolai Kondrashov __u8 *desc_ptr; 309614219eSNikolai Kondrashov /* 319614219eSNikolai Kondrashov * Size of the replacement report descriptor. 329614219eSNikolai Kondrashov * Only valid if desc_ptr is not NULL 339614219eSNikolai Kondrashov */ 349614219eSNikolai Kondrashov unsigned int desc_size; 35ff0c13d6SNikolai Kondrashov }; 36ff0c13d6SNikolai Kondrashov 37ff0c13d6SNikolai Kondrashov static __u8 *uclogic_report_fixup(struct hid_device *hdev, __u8 *rdesc, 38ff0c13d6SNikolai Kondrashov unsigned int *rsize) 39ff0c13d6SNikolai Kondrashov { 40ff0c13d6SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 41ff0c13d6SNikolai Kondrashov 429614219eSNikolai Kondrashov if (drvdata->desc_ptr != NULL) { 439614219eSNikolai Kondrashov rdesc = drvdata->desc_ptr; 449614219eSNikolai Kondrashov *rsize = drvdata->desc_size; 459614219eSNikolai Kondrashov } 46ff0c13d6SNikolai Kondrashov return rdesc; 47ff0c13d6SNikolai Kondrashov } 48ff0c13d6SNikolai Kondrashov 499614219eSNikolai Kondrashov static int uclogic_input_mapping(struct hid_device *hdev, 509614219eSNikolai Kondrashov struct hid_input *hi, 519614219eSNikolai Kondrashov struct hid_field *field, 529614219eSNikolai Kondrashov struct hid_usage *usage, 539614219eSNikolai Kondrashov unsigned long **bit, 549614219eSNikolai Kondrashov int *max) 55ff0c13d6SNikolai Kondrashov { 56ff0c13d6SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 579614219eSNikolai Kondrashov struct uclogic_params *params = &drvdata->params; 58ff0c13d6SNikolai Kondrashov 59ff0c13d6SNikolai Kondrashov /* discard the unused pen interface */ 609614219eSNikolai Kondrashov if (params->pen_unused && (field->application == HID_DG_PEN)) 61ff0c13d6SNikolai Kondrashov return -1; 62ff0c13d6SNikolai Kondrashov 63ff0c13d6SNikolai Kondrashov /* let hid-core decide what to do */ 64ff0c13d6SNikolai Kondrashov return 0; 65ff0c13d6SNikolai Kondrashov } 66ff0c13d6SNikolai Kondrashov 67ff0c13d6SNikolai Kondrashov static int uclogic_input_configured(struct hid_device *hdev, 68ff0c13d6SNikolai Kondrashov struct hid_input *hi) 69ff0c13d6SNikolai Kondrashov { 70ff0c13d6SNikolai Kondrashov char *name; 71ff0c13d6SNikolai Kondrashov const char *suffix = NULL; 72ff0c13d6SNikolai Kondrashov struct hid_field *field; 73ff0c13d6SNikolai Kondrashov size_t len; 74ff0c13d6SNikolai Kondrashov 75ff0c13d6SNikolai Kondrashov /* no report associated (HID_QUIRK_MULTI_INPUT not set) */ 76ff0c13d6SNikolai Kondrashov if (!hi->report) 77ff0c13d6SNikolai Kondrashov return 0; 78ff0c13d6SNikolai Kondrashov 79ff0c13d6SNikolai Kondrashov field = hi->report->field[0]; 80ff0c13d6SNikolai Kondrashov 81ff0c13d6SNikolai Kondrashov switch (field->application) { 82ff0c13d6SNikolai Kondrashov case HID_GD_KEYBOARD: 83ff0c13d6SNikolai Kondrashov suffix = "Keyboard"; 84ff0c13d6SNikolai Kondrashov break; 85ff0c13d6SNikolai Kondrashov case HID_GD_MOUSE: 86ff0c13d6SNikolai Kondrashov suffix = "Mouse"; 87ff0c13d6SNikolai Kondrashov break; 88ff0c13d6SNikolai Kondrashov case HID_GD_KEYPAD: 89ff0c13d6SNikolai Kondrashov suffix = "Pad"; 90ff0c13d6SNikolai Kondrashov break; 91ff0c13d6SNikolai Kondrashov case HID_DG_PEN: 92ff0c13d6SNikolai Kondrashov suffix = "Pen"; 93ff0c13d6SNikolai Kondrashov break; 94ff0c13d6SNikolai Kondrashov case HID_CP_CONSUMER_CONTROL: 95ff0c13d6SNikolai Kondrashov suffix = "Consumer Control"; 96ff0c13d6SNikolai Kondrashov break; 97ff0c13d6SNikolai Kondrashov case HID_GD_SYSTEM_CONTROL: 98ff0c13d6SNikolai Kondrashov suffix = "System Control"; 99ff0c13d6SNikolai Kondrashov break; 100ff0c13d6SNikolai Kondrashov } 101ff0c13d6SNikolai Kondrashov 102ff0c13d6SNikolai Kondrashov if (suffix) { 103ff0c13d6SNikolai Kondrashov len = strlen(hdev->name) + 2 + strlen(suffix); 104ff0c13d6SNikolai Kondrashov name = devm_kzalloc(&hi->input->dev, len, GFP_KERNEL); 105ff0c13d6SNikolai Kondrashov if (name) { 106ff0c13d6SNikolai Kondrashov snprintf(name, len, "%s %s", hdev->name, suffix); 107ff0c13d6SNikolai Kondrashov hi->input->name = name; 108ff0c13d6SNikolai Kondrashov } 109ff0c13d6SNikolai Kondrashov } 110ff0c13d6SNikolai Kondrashov 111ff0c13d6SNikolai Kondrashov return 0; 112ff0c13d6SNikolai Kondrashov } 113ff0c13d6SNikolai Kondrashov 114ff0c13d6SNikolai Kondrashov static int uclogic_probe(struct hid_device *hdev, 115ff0c13d6SNikolai Kondrashov const struct hid_device_id *id) 116ff0c13d6SNikolai Kondrashov { 117ff0c13d6SNikolai Kondrashov int rc; 1189614219eSNikolai Kondrashov struct uclogic_drvdata *drvdata = NULL; 1199614219eSNikolai Kondrashov bool params_initialized = false; 120ff0c13d6SNikolai Kondrashov 121ff0c13d6SNikolai Kondrashov /* 122ff0c13d6SNikolai Kondrashov * libinput requires the pad interface to be on a different node 123ff0c13d6SNikolai Kondrashov * than the pen, so use QUIRK_MULTI_INPUT for all tablets. 124ff0c13d6SNikolai Kondrashov */ 125ff0c13d6SNikolai Kondrashov hdev->quirks |= HID_QUIRK_MULTI_INPUT; 126ff0c13d6SNikolai Kondrashov 127ff0c13d6SNikolai Kondrashov /* Allocate and assign driver data */ 128ff0c13d6SNikolai Kondrashov drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL); 1299614219eSNikolai Kondrashov if (drvdata == NULL) { 1309614219eSNikolai Kondrashov rc = -ENOMEM; 1319614219eSNikolai Kondrashov goto failure; 1329614219eSNikolai Kondrashov } 133ff0c13d6SNikolai Kondrashov hid_set_drvdata(hdev, drvdata); 134ff0c13d6SNikolai Kondrashov 1359614219eSNikolai Kondrashov /* Initialize the device and retrieve interface parameters */ 1369614219eSNikolai Kondrashov rc = uclogic_params_init(&drvdata->params, hdev); 1379614219eSNikolai Kondrashov if (rc != 0) { 1389614219eSNikolai Kondrashov hid_err(hdev, "failed probing parameters: %d\n", rc); 1399614219eSNikolai Kondrashov goto failure; 140ff0c13d6SNikolai Kondrashov } 1419614219eSNikolai Kondrashov params_initialized = true; 1429614219eSNikolai Kondrashov hid_dbg(hdev, "parameters:\n" UCLOGIC_PARAMS_FMT_STR, 1439614219eSNikolai Kondrashov UCLOGIC_PARAMS_FMT_ARGS(&drvdata->params)); 1449614219eSNikolai Kondrashov if (drvdata->params.invalid) { 1459614219eSNikolai Kondrashov hid_info(hdev, "interface is invalid, ignoring\n"); 1469614219eSNikolai Kondrashov rc = -ENODEV; 1479614219eSNikolai Kondrashov goto failure; 1489614219eSNikolai Kondrashov } 149ff0c13d6SNikolai Kondrashov 1509614219eSNikolai Kondrashov /* Generate replacement report descriptor */ 1519614219eSNikolai Kondrashov rc = uclogic_params_get_desc(&drvdata->params, 1529614219eSNikolai Kondrashov &drvdata->desc_ptr, 1539614219eSNikolai Kondrashov &drvdata->desc_size); 154ff0c13d6SNikolai Kondrashov if (rc) { 1559614219eSNikolai Kondrashov hid_err(hdev, 1569614219eSNikolai Kondrashov "failed generating replacement report descriptor: %d\n", 1579614219eSNikolai Kondrashov rc); 1589614219eSNikolai Kondrashov goto failure; 159ff0c13d6SNikolai Kondrashov } 160ff0c13d6SNikolai Kondrashov 161ff0c13d6SNikolai Kondrashov rc = hid_parse(hdev); 162ff0c13d6SNikolai Kondrashov if (rc) { 163ff0c13d6SNikolai Kondrashov hid_err(hdev, "parse failed\n"); 1649614219eSNikolai Kondrashov goto failure; 165ff0c13d6SNikolai Kondrashov } 166ff0c13d6SNikolai Kondrashov 167ff0c13d6SNikolai Kondrashov rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 168ff0c13d6SNikolai Kondrashov if (rc) { 169ff0c13d6SNikolai Kondrashov hid_err(hdev, "hw start failed\n"); 1709614219eSNikolai Kondrashov goto failure; 1719614219eSNikolai Kondrashov } 1729614219eSNikolai Kondrashov 1739614219eSNikolai Kondrashov return 0; 1749614219eSNikolai Kondrashov failure: 1759614219eSNikolai Kondrashov /* Assume "remove" might not be called if "probe" failed */ 1769614219eSNikolai Kondrashov if (params_initialized) 1779614219eSNikolai Kondrashov uclogic_params_cleanup(&drvdata->params); 178ff0c13d6SNikolai Kondrashov return rc; 179ff0c13d6SNikolai Kondrashov } 180ff0c13d6SNikolai Kondrashov 181*251b4275SNikolai Kondrashov #ifdef CONFIG_PM 182*251b4275SNikolai Kondrashov static int uclogic_resume(struct hid_device *hdev) 183*251b4275SNikolai Kondrashov { 184*251b4275SNikolai Kondrashov int rc; 185*251b4275SNikolai Kondrashov struct uclogic_params params; 186*251b4275SNikolai Kondrashov 187*251b4275SNikolai Kondrashov /* Re-initialize the device, but discard parameters */ 188*251b4275SNikolai Kondrashov rc = uclogic_params_init(¶ms, hdev); 189*251b4275SNikolai Kondrashov if (rc != 0) 190*251b4275SNikolai Kondrashov hid_err(hdev, "failed to re-initialize the device\n"); 191*251b4275SNikolai Kondrashov else 192*251b4275SNikolai Kondrashov uclogic_params_cleanup(¶ms); 193*251b4275SNikolai Kondrashov 194*251b4275SNikolai Kondrashov return rc; 195*251b4275SNikolai Kondrashov } 196*251b4275SNikolai Kondrashov #endif 197*251b4275SNikolai Kondrashov 1989614219eSNikolai Kondrashov static int uclogic_raw_event(struct hid_device *hdev, 1999614219eSNikolai Kondrashov struct hid_report *report, 200ff0c13d6SNikolai Kondrashov u8 *data, int size) 201ff0c13d6SNikolai Kondrashov { 202ff0c13d6SNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 2039614219eSNikolai Kondrashov struct uclogic_params *params = &drvdata->params; 204ff0c13d6SNikolai Kondrashov 2059614219eSNikolai Kondrashov /* Tweak pen reports, if necessary */ 2069614219eSNikolai Kondrashov if (!params->pen_unused && 2079614219eSNikolai Kondrashov (report->type == HID_INPUT_REPORT) && 2089614219eSNikolai Kondrashov (report->id == params->pen.id) && 209ff0c13d6SNikolai Kondrashov (size >= 2)) { 2109614219eSNikolai Kondrashov /* If it's the "virtual" frame controls report */ 2119614219eSNikolai Kondrashov if (params->frame.id != 0 && 2129614219eSNikolai Kondrashov data[1] & params->pen_frame_flag) { 2139614219eSNikolai Kondrashov /* Change to virtual frame controls report ID */ 2149614219eSNikolai Kondrashov data[0] = params->frame.id; 2159614219eSNikolai Kondrashov return 0; 2169614219eSNikolai Kondrashov } 2179614219eSNikolai Kondrashov /* If in-range reports are inverted */ 2189614219eSNikolai Kondrashov if (params->pen.inrange == 2199614219eSNikolai Kondrashov UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) { 220ff0c13d6SNikolai Kondrashov /* Invert the in-range bit */ 221ff0c13d6SNikolai Kondrashov data[1] ^= 0x40; 222ff0c13d6SNikolai Kondrashov } 2239614219eSNikolai Kondrashov } 224ff0c13d6SNikolai Kondrashov 225ff0c13d6SNikolai Kondrashov return 0; 226ff0c13d6SNikolai Kondrashov } 227ff0c13d6SNikolai Kondrashov 2289614219eSNikolai Kondrashov static void uclogic_remove(struct hid_device *hdev) 2299614219eSNikolai Kondrashov { 2309614219eSNikolai Kondrashov struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 2319614219eSNikolai Kondrashov 2329614219eSNikolai Kondrashov hid_hw_stop(hdev); 2339614219eSNikolai Kondrashov kfree(drvdata->desc_ptr); 2349614219eSNikolai Kondrashov uclogic_params_cleanup(&drvdata->params); 2359614219eSNikolai Kondrashov } 2369614219eSNikolai Kondrashov 237ff0c13d6SNikolai Kondrashov static const struct hid_device_id uclogic_devices[] = { 238ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 239ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_PF1209) }, 240ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 241ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP4030U) }, 242ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 243ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP5540U) }, 244ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 245ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP8060U) }, 246ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 247ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_WP1062) }, 248ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 249ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_WIRELESS_TABLET_TWHL850) }, 250ff0c13d6SNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 251ff0c13d6SNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_TABLET_TWHA60) }, 2529614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 2539614219eSNikolai Kondrashov USB_DEVICE_ID_HUION_TABLET) }, 2549614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 2559614219eSNikolai Kondrashov USB_DEVICE_ID_HUION_TABLET) }, 2569614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 2579614219eSNikolai Kondrashov USB_DEVICE_ID_YIYNOVA_TABLET) }, 2589614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 2599614219eSNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_81) }, 2609614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 2619614219eSNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_UGEE_TABLET_45) }, 2629614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, 2639614219eSNikolai Kondrashov USB_DEVICE_ID_UCLOGIC_DRAWIMAGE_G3) }, 2649614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGTIZER, 2659614219eSNikolai Kondrashov USB_DEVICE_ID_UGTIZER_TABLET_GP0610) }, 2669614219eSNikolai Kondrashov { HID_USB_DEVICE(USB_VENDOR_ID_UGEE, 2679614219eSNikolai Kondrashov USB_DEVICE_ID_UGEE_TABLET_EX07S) }, 268ff0c13d6SNikolai Kondrashov { } 269ff0c13d6SNikolai Kondrashov }; 270ff0c13d6SNikolai Kondrashov MODULE_DEVICE_TABLE(hid, uclogic_devices); 271ff0c13d6SNikolai Kondrashov 272ff0c13d6SNikolai Kondrashov static struct hid_driver uclogic_driver = { 273ff0c13d6SNikolai Kondrashov .name = "uclogic", 274ff0c13d6SNikolai Kondrashov .id_table = uclogic_devices, 275ff0c13d6SNikolai Kondrashov .probe = uclogic_probe, 2769614219eSNikolai Kondrashov .remove = uclogic_remove, 277ff0c13d6SNikolai Kondrashov .report_fixup = uclogic_report_fixup, 278ff0c13d6SNikolai Kondrashov .raw_event = uclogic_raw_event, 279ff0c13d6SNikolai Kondrashov .input_mapping = uclogic_input_mapping, 280ff0c13d6SNikolai Kondrashov .input_configured = uclogic_input_configured, 281*251b4275SNikolai Kondrashov #ifdef CONFIG_PM 282*251b4275SNikolai Kondrashov .resume = uclogic_resume, 283*251b4275SNikolai Kondrashov .reset_resume = uclogic_resume, 284*251b4275SNikolai Kondrashov #endif 285ff0c13d6SNikolai Kondrashov }; 286ff0c13d6SNikolai Kondrashov module_hid_driver(uclogic_driver); 287ff0c13d6SNikolai Kondrashov 288ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Martin Rusko"); 289ff0c13d6SNikolai Kondrashov MODULE_AUTHOR("Nikolai Kondrashov"); 290ff0c13d6SNikolai Kondrashov MODULE_LICENSE("GPL"); 291