1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 214a21cd4SJiri Slaby /* 314a21cd4SJiri Slaby * HID driver for some a4tech "special" devices 414a21cd4SJiri Slaby * 514a21cd4SJiri Slaby * Copyright (c) 1999 Andreas Gal 614a21cd4SJiri Slaby * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> 714a21cd4SJiri Slaby * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc 814a21cd4SJiri Slaby * Copyright (c) 2006-2007 Jiri Kosina 914a21cd4SJiri Slaby * Copyright (c) 2008 Jiri Slaby 1014a21cd4SJiri Slaby */ 1114a21cd4SJiri Slaby 1214a21cd4SJiri Slaby /* 1314a21cd4SJiri Slaby */ 1414a21cd4SJiri Slaby 1514a21cd4SJiri Slaby #include <linux/device.h> 1614a21cd4SJiri Slaby #include <linux/input.h> 1714a21cd4SJiri Slaby #include <linux/hid.h> 1814a21cd4SJiri Slaby #include <linux/module.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 2014a21cd4SJiri Slaby 2114a21cd4SJiri Slaby #include "hid-ids.h" 2214a21cd4SJiri Slaby 2314a21cd4SJiri Slaby #define A4_2WHEEL_MOUSE_HACK_7 0x01 2414a21cd4SJiri Slaby #define A4_2WHEEL_MOUSE_HACK_B8 0x02 2514a21cd4SJiri Slaby 2614a21cd4SJiri Slaby struct a4tech_sc { 2714a21cd4SJiri Slaby unsigned long quirks; 2814a21cd4SJiri Slaby unsigned int hw_wheel; 2914a21cd4SJiri Slaby __s32 delayed_value; 3014a21cd4SJiri Slaby }; 3114a21cd4SJiri Slaby 3214a21cd4SJiri Slaby static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi, 3314a21cd4SJiri Slaby struct hid_field *field, struct hid_usage *usage, 3414a21cd4SJiri Slaby unsigned long **bit, int *max) 3514a21cd4SJiri Slaby { 3614a21cd4SJiri Slaby struct a4tech_sc *a4 = hid_get_drvdata(hdev); 3714a21cd4SJiri Slaby 3814a21cd4SJiri Slaby if (usage->type == EV_REL && usage->code == REL_WHEEL) 3914a21cd4SJiri Slaby set_bit(REL_HWHEEL, *bit); 4014a21cd4SJiri Slaby 4114a21cd4SJiri Slaby if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) 4214a21cd4SJiri Slaby return -1; 4314a21cd4SJiri Slaby 4414a21cd4SJiri Slaby return 0; 4514a21cd4SJiri Slaby } 4614a21cd4SJiri Slaby 4714a21cd4SJiri Slaby static int a4_event(struct hid_device *hdev, struct hid_field *field, 4814a21cd4SJiri Slaby struct hid_usage *usage, __s32 value) 4914a21cd4SJiri Slaby { 5014a21cd4SJiri Slaby struct a4tech_sc *a4 = hid_get_drvdata(hdev); 5114a21cd4SJiri Slaby struct input_dev *input; 5214a21cd4SJiri Slaby 5314a21cd4SJiri Slaby if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput || 5414a21cd4SJiri Slaby !usage->type) 5514a21cd4SJiri Slaby return 0; 5614a21cd4SJiri Slaby 5714a21cd4SJiri Slaby input = field->hidinput->input; 5814a21cd4SJiri Slaby 5914a21cd4SJiri Slaby if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) { 6014a21cd4SJiri Slaby if (usage->type == EV_REL && usage->code == REL_WHEEL) { 6114a21cd4SJiri Slaby a4->delayed_value = value; 6214a21cd4SJiri Slaby return 1; 6314a21cd4SJiri Slaby } 6414a21cd4SJiri Slaby 6514a21cd4SJiri Slaby if (usage->hid == 0x000100b8) { 6614a21cd4SJiri Slaby input_event(input, EV_REL, value ? REL_HWHEEL : 6714a21cd4SJiri Slaby REL_WHEEL, a4->delayed_value); 6814a21cd4SJiri Slaby return 1; 6914a21cd4SJiri Slaby } 7014a21cd4SJiri Slaby } 7114a21cd4SJiri Slaby 7214a21cd4SJiri Slaby if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) { 7314a21cd4SJiri Slaby a4->hw_wheel = !!value; 7414a21cd4SJiri Slaby return 1; 7514a21cd4SJiri Slaby } 7614a21cd4SJiri Slaby 7714a21cd4SJiri Slaby if (usage->code == REL_WHEEL && a4->hw_wheel) { 7814a21cd4SJiri Slaby input_event(input, usage->type, REL_HWHEEL, value); 7914a21cd4SJiri Slaby return 1; 8014a21cd4SJiri Slaby } 8114a21cd4SJiri Slaby 8214a21cd4SJiri Slaby return 0; 8314a21cd4SJiri Slaby } 8414a21cd4SJiri Slaby 8514a21cd4SJiri Slaby static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id) 8614a21cd4SJiri Slaby { 8714a21cd4SJiri Slaby struct a4tech_sc *a4; 8814a21cd4SJiri Slaby int ret; 8914a21cd4SJiri Slaby 90abf832bfSBenjamin Tissoires a4 = devm_kzalloc(&hdev->dev, sizeof(*a4), GFP_KERNEL); 9114a21cd4SJiri Slaby if (a4 == NULL) { 924291ee30SJoe Perches hid_err(hdev, "can't alloc device descriptor\n"); 93abf832bfSBenjamin Tissoires return -ENOMEM; 9414a21cd4SJiri Slaby } 9514a21cd4SJiri Slaby 9614a21cd4SJiri Slaby a4->quirks = id->driver_data; 9714a21cd4SJiri Slaby 9814a21cd4SJiri Slaby hid_set_drvdata(hdev, a4); 9914a21cd4SJiri Slaby 10014a21cd4SJiri Slaby ret = hid_parse(hdev); 10114a21cd4SJiri Slaby if (ret) { 1024291ee30SJoe Perches hid_err(hdev, "parse failed\n"); 103abf832bfSBenjamin Tissoires return ret; 10414a21cd4SJiri Slaby } 10514a21cd4SJiri Slaby 10693c10132SJiri Slaby ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 10714a21cd4SJiri Slaby if (ret) { 1084291ee30SJoe Perches hid_err(hdev, "hw start failed\n"); 10914a21cd4SJiri Slaby return ret; 11014a21cd4SJiri Slaby } 11114a21cd4SJiri Slaby 112abf832bfSBenjamin Tissoires return 0; 11314a21cd4SJiri Slaby } 11414a21cd4SJiri Slaby 11514a21cd4SJiri Slaby static const struct hid_device_id a4_devices[] = { 11614a21cd4SJiri Slaby { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU), 11714a21cd4SJiri Slaby .driver_data = A4_2WHEEL_MOUSE_HACK_7 }, 11814a21cd4SJiri Slaby { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D), 11914a21cd4SJiri Slaby .driver_data = A4_2WHEEL_MOUSE_HACK_B8 }, 120b7e1b203SLech Perczak { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649), 121b7e1b203SLech Perczak .driver_data = A4_2WHEEL_MOUSE_HACK_B8 }, 12214a21cd4SJiri Slaby { } 12314a21cd4SJiri Slaby }; 12414a21cd4SJiri Slaby MODULE_DEVICE_TABLE(hid, a4_devices); 12514a21cd4SJiri Slaby 12614a21cd4SJiri Slaby static struct hid_driver a4_driver = { 12714a21cd4SJiri Slaby .name = "a4tech", 12814a21cd4SJiri Slaby .id_table = a4_devices, 12914a21cd4SJiri Slaby .input_mapped = a4_input_mapped, 13014a21cd4SJiri Slaby .event = a4_event, 13114a21cd4SJiri Slaby .probe = a4_probe, 13214a21cd4SJiri Slaby }; 133f425458eSH Hartley Sweeten module_hid_driver(a4_driver); 13414a21cd4SJiri Slaby 13514a21cd4SJiri Slaby MODULE_LICENSE("GPL"); 136