Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  HID driver for some a4tech "special" devices
  4 *
  5 *  Copyright (c) 1999 Andreas Gal
  6 *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  7 *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  8 *  Copyright (c) 2006-2007 Jiri Kosina
 
  9 *  Copyright (c) 2008 Jiri Slaby
 10 */
 11
 12/*
 
 
 
 
 13 */
 14
 15#include <linux/device.h>
 16#include <linux/input.h>
 17#include <linux/hid.h>
 18#include <linux/module.h>
 19#include <linux/slab.h>
 20
 21#include "hid-ids.h"
 22
 23#define A4_2WHEEL_MOUSE_HACK_7	0x01
 24#define A4_2WHEEL_MOUSE_HACK_B8	0x02
 25
 26#define A4_WHEEL_ORIENTATION	(HID_UP_GENDESK | 0x000000b8)
 27
 28struct a4tech_sc {
 29	unsigned long quirks;
 30	unsigned int hw_wheel;
 31	__s32 delayed_value;
 32};
 33
 34static int a4_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 35			    struct hid_field *field, struct hid_usage *usage,
 36			    unsigned long **bit, int *max)
 37{
 38	struct a4tech_sc *a4 = hid_get_drvdata(hdev);
 39
 40	if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8 &&
 41	    usage->hid == A4_WHEEL_ORIENTATION) {
 42		/*
 43		 * We do not want to have this usage mapped to anything as it's
 44		 * nonstandard and doesn't really behave like an HID report.
 45		 * It's only selecting the orientation (vertical/horizontal) of
 46		 * the previous mouse wheel report. The input_events will be
 47		 * generated once both reports are recorded in a4_event().
 48		 */
 49		return -1;
 50	}
 51
 52	return 0;
 53
 54}
 55
 56static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi,
 57		struct hid_field *field, struct hid_usage *usage,
 58		unsigned long **bit, int *max)
 59{
 60	struct a4tech_sc *a4 = hid_get_drvdata(hdev);
 61
 62	if (usage->type == EV_REL && usage->code == REL_WHEEL_HI_RES) {
 63		set_bit(REL_HWHEEL, *bit);
 64		set_bit(REL_HWHEEL_HI_RES, *bit);
 65	}
 66
 67	if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007)
 68		return -1;
 69
 70	return 0;
 71}
 72
 73static int a4_event(struct hid_device *hdev, struct hid_field *field,
 74		struct hid_usage *usage, __s32 value)
 75{
 76	struct a4tech_sc *a4 = hid_get_drvdata(hdev);
 77	struct input_dev *input;
 78
 79	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput)
 
 80		return 0;
 81
 82	input = field->hidinput->input;
 83
 84	if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) {
 85		if (usage->type == EV_REL && usage->code == REL_WHEEL_HI_RES) {
 86			a4->delayed_value = value;
 87			return 1;
 88		}
 89
 90		if (usage->hid == A4_WHEEL_ORIENTATION) {
 91			input_event(input, EV_REL, value ? REL_HWHEEL :
 92					REL_WHEEL, a4->delayed_value);
 93			input_event(input, EV_REL, value ? REL_HWHEEL_HI_RES :
 94					REL_WHEEL_HI_RES, a4->delayed_value * 120);
 95			return 1;
 96		}
 97	}
 98
 99	if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) {
100		a4->hw_wheel = !!value;
101		return 1;
102	}
103
104	if (usage->code == REL_WHEEL_HI_RES && a4->hw_wheel) {
105		input_event(input, usage->type, REL_HWHEEL, value);
106		input_event(input, usage->type, REL_HWHEEL_HI_RES, value * 120);
107		return 1;
108	}
109
110	return 0;
111}
112
113static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id)
114{
115	struct a4tech_sc *a4;
116	int ret;
117
118	a4 = devm_kzalloc(&hdev->dev, sizeof(*a4), GFP_KERNEL);
119	if (a4 == NULL) {
120		hid_err(hdev, "can't alloc device descriptor\n");
121		return -ENOMEM;
 
122	}
123
124	a4->quirks = id->driver_data;
125
126	hid_set_drvdata(hdev, a4);
127
128	ret = hid_parse(hdev);
129	if (ret) {
130		hid_err(hdev, "parse failed\n");
131		return ret;
132	}
133
134	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
135	if (ret) {
136		hid_err(hdev, "hw start failed\n");
137		return ret;
138	}
139
140	return 0;
 
 
 
 
 
 
 
 
 
 
 
141}
142
143static const struct hid_device_id a4_devices[] = {
144	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU),
145		.driver_data = A4_2WHEEL_MOUSE_HACK_7 },
146	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D),
147		.driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
148	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649),
149		.driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
150	{ }
151};
152MODULE_DEVICE_TABLE(hid, a4_devices);
153
154static struct hid_driver a4_driver = {
155	.name = "a4tech",
156	.id_table = a4_devices,
157	.input_mapping = a4_input_mapping,
158	.input_mapped = a4_input_mapped,
159	.event = a4_event,
160	.probe = a4_probe,
 
161};
162module_hid_driver(a4_driver);
163
 
 
 
 
 
 
 
 
 
 
 
 
164MODULE_LICENSE("GPL");
v3.1
 
  1/*
  2 *  HID driver for some a4tech "special" devices
  3 *
  4 *  Copyright (c) 1999 Andreas Gal
  5 *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6 *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7 *  Copyright (c) 2006-2007 Jiri Kosina
  8 *  Copyright (c) 2007 Paul Walmsley
  9 *  Copyright (c) 2008 Jiri Slaby
 10 */
 11
 12/*
 13 * This program is free software; you can redistribute it and/or modify it
 14 * under the terms of the GNU General Public License as published by the Free
 15 * Software Foundation; either version 2 of the License, or (at your option)
 16 * any later version.
 17 */
 18
 19#include <linux/device.h>
 20#include <linux/input.h>
 21#include <linux/hid.h>
 22#include <linux/module.h>
 23#include <linux/slab.h>
 24
 25#include "hid-ids.h"
 26
 27#define A4_2WHEEL_MOUSE_HACK_7	0x01
 28#define A4_2WHEEL_MOUSE_HACK_B8	0x02
 29
 
 
 30struct a4tech_sc {
 31	unsigned long quirks;
 32	unsigned int hw_wheel;
 33	__s32 delayed_value;
 34};
 35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 36static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi,
 37		struct hid_field *field, struct hid_usage *usage,
 38		unsigned long **bit, int *max)
 39{
 40	struct a4tech_sc *a4 = hid_get_drvdata(hdev);
 41
 42	if (usage->type == EV_REL && usage->code == REL_WHEEL)
 43		set_bit(REL_HWHEEL, *bit);
 
 
 44
 45	if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007)
 46		return -1;
 47
 48	return 0;
 49}
 50
 51static int a4_event(struct hid_device *hdev, struct hid_field *field,
 52		struct hid_usage *usage, __s32 value)
 53{
 54	struct a4tech_sc *a4 = hid_get_drvdata(hdev);
 55	struct input_dev *input;
 56
 57	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
 58			!usage->type)
 59		return 0;
 60
 61	input = field->hidinput->input;
 62
 63	if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) {
 64		if (usage->type == EV_REL && usage->code == REL_WHEEL) {
 65			a4->delayed_value = value;
 66			return 1;
 67		}
 68
 69		if (usage->hid == 0x000100b8) {
 70			input_event(input, EV_REL, value ? REL_HWHEEL :
 71					REL_WHEEL, a4->delayed_value);
 
 
 72			return 1;
 73		}
 74	}
 75
 76	if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) {
 77		a4->hw_wheel = !!value;
 78		return 1;
 79	}
 80
 81	if (usage->code == REL_WHEEL && a4->hw_wheel) {
 82		input_event(input, usage->type, REL_HWHEEL, value);
 
 83		return 1;
 84	}
 85
 86	return 0;
 87}
 88
 89static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id)
 90{
 91	struct a4tech_sc *a4;
 92	int ret;
 93
 94	a4 = kzalloc(sizeof(*a4), GFP_KERNEL);
 95	if (a4 == NULL) {
 96		hid_err(hdev, "can't alloc device descriptor\n");
 97		ret = -ENOMEM;
 98		goto err_free;
 99	}
100
101	a4->quirks = id->driver_data;
102
103	hid_set_drvdata(hdev, a4);
104
105	ret = hid_parse(hdev);
106	if (ret) {
107		hid_err(hdev, "parse failed\n");
108		goto err_free;
109	}
110
111	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
112	if (ret) {
113		hid_err(hdev, "hw start failed\n");
114		goto err_free;
115	}
116
117	return 0;
118err_free:
119	kfree(a4);
120	return ret;
121}
122
123static void a4_remove(struct hid_device *hdev)
124{
125	struct a4tech_sc *a4 = hid_get_drvdata(hdev);
126
127	hid_hw_stop(hdev);
128	kfree(a4);
129}
130
131static const struct hid_device_id a4_devices[] = {
132	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU),
133		.driver_data = A4_2WHEEL_MOUSE_HACK_7 },
134	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D),
135		.driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
136	{ HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649),
137		.driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
138	{ }
139};
140MODULE_DEVICE_TABLE(hid, a4_devices);
141
142static struct hid_driver a4_driver = {
143	.name = "a4tech",
144	.id_table = a4_devices,
 
145	.input_mapped = a4_input_mapped,
146	.event = a4_event,
147	.probe = a4_probe,
148	.remove = a4_remove,
149};
 
150
151static int __init a4_init(void)
152{
153	return hid_register_driver(&a4_driver);
154}
155
156static void __exit a4_exit(void)
157{
158	hid_unregister_driver(&a4_driver);
159}
160
161module_init(a4_init);
162module_exit(a4_exit);
163MODULE_LICENSE("GPL");