xref: /linux/drivers/hid/hid-elo.c (revision 5b193d6a8973655f19de87a7873ce31dee2d5fe6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID driver for ELO usb touchscreen 4000/4500
4  *
5  * Copyright (c) 2013 Jiri Slaby
6  *
7  * Data parsing taken from elousb driver by Vojtech Pavlik.
8  */
9 
10 #include <linux/hid.h>
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 #include <linux/workqueue.h>
15 
16 #include "hid-ids.h"
17 
18 #define ELO_PERIODIC_READ_INTERVAL	HZ
19 #define ELO_SMARTSET_CMD_TIMEOUT	2000 /* msec */
20 
21 /* Elo SmartSet commands */
22 #define ELO_FLUSH_SMARTSET_RESPONSES	0x02 /* Flush all pending smartset responses */
23 #define ELO_SEND_SMARTSET_COMMAND	0x05 /* Send a smartset command */
24 #define ELO_GET_SMARTSET_RESPONSE	0x06 /* Get a smartset response */
25 #define ELO_DIAG			0x64 /* Diagnostics command */
26 #define ELO_SMARTSET_PACKET_SIZE	8
27 
28 struct elo_priv {
29 	struct usb_device *usbdev;
30 	struct delayed_work work;
31 	unsigned char buffer[ELO_SMARTSET_PACKET_SIZE];
32 };
33 
34 static struct workqueue_struct *wq;
35 static bool use_fw_quirk = true;
36 module_param(use_fw_quirk, bool, S_IRUGO);
37 MODULE_PARM_DESC(use_fw_quirk, "Do periodic pokes for broken M firmwares (default = true)");
38 
39 static int elo_input_configured(struct hid_device *hdev,
40 		struct hid_input *hidinput)
41 {
42 	struct input_dev *input = hidinput->input;
43 
44 	/*
45 	 * ELO devices have one Button usage in GenDesk field, which makes
46 	 * hid-input map it to BTN_LEFT; that confuses userspace, which then
47 	 * considers the device to be a mouse/touchpad instead of touchscreen.
48 	 */
49 	clear_bit(BTN_LEFT, input->keybit);
50 	set_bit(BTN_TOUCH, input->keybit);
51 	set_bit(ABS_PRESSURE, input->absbit);
52 	input_set_abs_params(input, ABS_PRESSURE, 0, 256, 0, 0);
53 
54 	return 0;
55 }
56 
57 static void elo_process_data(struct input_dev *input, const u8 *data, int size)
58 {
59 	int press;
60 
61 	input_report_abs(input, ABS_X, (data[3] << 8) | data[2]);
62 	input_report_abs(input, ABS_Y, (data[5] << 8) | data[4]);
63 
64 	press = 0;
65 	if (data[1] & 0x80)
66 		press = (data[7] << 8) | data[6];
67 	input_report_abs(input, ABS_PRESSURE, press);
68 
69 	if (data[1] & 0x03) {
70 		input_report_key(input, BTN_TOUCH, 1);
71 		input_sync(input);
72 	}
73 
74 	if (data[1] & 0x04)
75 		input_report_key(input, BTN_TOUCH, 0);
76 
77 	input_sync(input);
78 }
79 
80 static int elo_raw_event(struct hid_device *hdev, struct hid_report *report,
81 	 u8 *data, int size)
82 {
83 	struct hid_input *hidinput;
84 
85 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || list_empty(&hdev->inputs))
86 		return 0;
87 
88 	if (size < ELO_SMARTSET_PACKET_SIZE)
89 		return 0;
90 
91 	hidinput = list_first_entry(&hdev->inputs, struct hid_input, list);
92 
93 	switch (report->id) {
94 	case 0:
95 		if (data[0] == 'T') {	/* Mandatory ELO packet marker */
96 			elo_process_data(hidinput->input, data, size);
97 			return 1;
98 		}
99 		break;
100 	default:	/* unknown report */
101 		/* Unknown report type; pass upstream */
102 		hid_info(hdev, "unknown report type %d\n", report->id);
103 		break;
104 	}
105 
106 	return 0;
107 }
108 
109 static int elo_smartset_send_get(struct usb_device *dev, u8 command,
110 		void *data)
111 {
112 	unsigned int pipe;
113 	u8 dir;
114 
115 	if (command == ELO_SEND_SMARTSET_COMMAND) {
116 		pipe = usb_sndctrlpipe(dev, 0);
117 		dir = USB_DIR_OUT;
118 	} else if (command == ELO_GET_SMARTSET_RESPONSE) {
119 		pipe = usb_rcvctrlpipe(dev, 0);
120 		dir = USB_DIR_IN;
121 	} else
122 		return -EINVAL;
123 
124 	return usb_control_msg(dev, pipe, command,
125 			dir | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
126 			0, 0, data, ELO_SMARTSET_PACKET_SIZE,
127 			ELO_SMARTSET_CMD_TIMEOUT);
128 }
129 
130 static int elo_flush_smartset_responses(struct usb_device *dev)
131 {
132 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
133 			ELO_FLUSH_SMARTSET_RESPONSES,
134 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
135 			0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
136 }
137 
138 static void elo_work(struct work_struct *work)
139 {
140 	struct elo_priv *priv = container_of(work, struct elo_priv, work.work);
141 	struct usb_device *dev = priv->usbdev;
142 	unsigned char *buffer = priv->buffer;
143 	int ret;
144 
145 	ret = elo_flush_smartset_responses(dev);
146 	if (ret < 0) {
147 		dev_err(&dev->dev, "initial FLUSH_SMARTSET_RESPONSES failed, error %d\n",
148 				ret);
149 		goto fail;
150 	}
151 
152 	/* send Diagnostics command */
153 	*buffer = ELO_DIAG;
154 	ret = elo_smartset_send_get(dev, ELO_SEND_SMARTSET_COMMAND, buffer);
155 	if (ret < 0) {
156 		dev_err(&dev->dev, "send Diagnostics Command failed, error %d\n",
157 				ret);
158 		goto fail;
159 	}
160 
161 	/* get the result */
162 	ret = elo_smartset_send_get(dev, ELO_GET_SMARTSET_RESPONSE, buffer);
163 	if (ret < 0) {
164 		dev_err(&dev->dev, "get Diagnostics Command response failed, error %d\n",
165 				ret);
166 		goto fail;
167 	}
168 
169 	/* read the ack */
170 	if (*buffer != 'A') {
171 		ret = elo_smartset_send_get(dev, ELO_GET_SMARTSET_RESPONSE,
172 				buffer);
173 		if (ret < 0) {
174 			dev_err(&dev->dev, "get acknowledge response failed, error %d\n",
175 					ret);
176 			goto fail;
177 		}
178 	}
179 
180 fail:
181 	ret = elo_flush_smartset_responses(dev);
182 	if (ret < 0)
183 		dev_err(&dev->dev, "final FLUSH_SMARTSET_RESPONSES failed, error %d\n",
184 				ret);
185 	queue_delayed_work(wq, &priv->work, ELO_PERIODIC_READ_INTERVAL);
186 }
187 
188 /*
189  * Not all Elo devices need the periodic HID descriptor reads.
190  * Only firmware version M needs this.
191  */
192 static bool elo_broken_firmware(struct usb_device *dev)
193 {
194 	struct usb_device *hub = dev->parent;
195 	struct usb_device *child = NULL;
196 	u16 fw_lvl = le16_to_cpu(dev->descriptor.bcdDevice);
197 	u16 child_vid, child_pid;
198 	int i;
199 
200 	if (!use_fw_quirk)
201 		return false;
202 	if (fw_lvl != 0x10d)
203 		return false;
204 
205 	/* iterate sibling devices of the touch controller */
206 	usb_hub_for_each_child(hub, i, child) {
207 		child_vid = le16_to_cpu(child->descriptor.idVendor);
208 		child_pid = le16_to_cpu(child->descriptor.idProduct);
209 
210 		/*
211 		 * If one of the devices below is present attached as a sibling of
212 		 * the touch controller then  this is a newer IBM 4820 monitor that
213 		 * does not need the IBM-requested workaround if fw level is
214 		 * 0x010d - aka 'M'.
215 		 * No other HW can have this combination.
216 		 */
217 		if (child_vid==0x04b3) {
218 			switch (child_pid) {
219 			case 0x4676: /* 4820 21x Video */
220 			case 0x4677: /* 4820 51x Video */
221 			case 0x4678: /* 4820 2Lx Video */
222 			case 0x4679: /* 4820 5Lx Video */
223 				return false;
224 			}
225 		}
226 	}
227 	return true;
228 }
229 
230 static int elo_probe(struct hid_device *hdev, const struct hid_device_id *id)
231 {
232 	struct elo_priv *priv;
233 	int ret;
234 
235 	if (!hid_is_usb(hdev))
236 		return -EINVAL;
237 
238 	priv = kzalloc_obj(*priv);
239 	if (!priv)
240 		return -ENOMEM;
241 
242 	INIT_DELAYED_WORK(&priv->work, elo_work);
243 	priv->usbdev = interface_to_usbdev(to_usb_interface(hdev->dev.parent));
244 
245 	hid_set_drvdata(hdev, priv);
246 
247 	ret = hid_parse(hdev);
248 	if (ret) {
249 		hid_err(hdev, "parse failed\n");
250 		goto err_free;
251 	}
252 
253 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
254 	if (ret) {
255 		hid_err(hdev, "hw start failed\n");
256 		goto err_free;
257 	}
258 
259 	if (elo_broken_firmware(priv->usbdev)) {
260 		hid_info(hdev, "broken firmware found, installing workaround\n");
261 		queue_delayed_work(wq, &priv->work, ELO_PERIODIC_READ_INTERVAL);
262 	}
263 
264 	return 0;
265 err_free:
266 	kfree(priv);
267 	return ret;
268 }
269 
270 static void elo_remove(struct hid_device *hdev)
271 {
272 	struct elo_priv *priv = hid_get_drvdata(hdev);
273 
274 	hid_hw_stop(hdev);
275 	cancel_delayed_work_sync(&priv->work);
276 	kfree(priv);
277 }
278 
279 static const struct hid_device_id elo_devices[] = {
280 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0009), },
281 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0030), },
282 	{ }
283 };
284 MODULE_DEVICE_TABLE(hid, elo_devices);
285 
286 static struct hid_driver elo_driver = {
287 	.name = "elo",
288 	.id_table = elo_devices,
289 	.probe = elo_probe,
290 	.remove = elo_remove,
291 	.raw_event = elo_raw_event,
292 	.input_configured = elo_input_configured,
293 };
294 
295 static int __init elo_driver_init(void)
296 {
297 	int ret;
298 
299 	wq = create_singlethread_workqueue("elousb");
300 	if (!wq)
301 		return -ENOMEM;
302 
303 	ret = hid_register_driver(&elo_driver);
304 	if (ret)
305 		destroy_workqueue(wq);
306 
307 	return ret;
308 }
309 module_init(elo_driver_init);
310 
311 static void __exit elo_driver_exit(void)
312 {
313 	hid_unregister_driver(&elo_driver);
314 	destroy_workqueue(wq);
315 }
316 module_exit(elo_driver_exit);
317 
318 MODULE_AUTHOR("Jiri Slaby <jslaby@suse.cz>");
319 MODULE_DESCRIPTION("HID driver for ELO usb touchscreen 4000/4500");
320 MODULE_LICENSE("GPL");
321