2 * HID driver for ELO usb touchscreen 4000/4500
4 * Copyright (c) 2013 Jiri Slaby
6 * Data parsing taken from elousb driver by Vojtech Pavlik.
8 * This driver is licensed under the terms of GPLv2.
11 #include <linux/hid.h>
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/usb.h>
15 #include <linux/workqueue.h>
19 #define ELO_PERIODIC_READ_INTERVAL HZ
20 #define ELO_SMARTSET_CMD_TIMEOUT 2000 /* msec */
22 /* Elo SmartSet commands */
23 #define ELO_FLUSH_SMARTSET_RESPONSES 0x02 /* Flush all pending smartset responses */
24 #define ELO_SEND_SMARTSET_COMMAND 0x05 /* Send a smartset command */
25 #define ELO_GET_SMARTSET_RESPONSE 0x06 /* Get a smartset response */
26 #define ELO_DIAG 0x64 /* Diagnostics command */
27 #define ELO_SMARTSET_PACKET_SIZE 8
30 struct usb_device *usbdev;
31 struct delayed_work work;
32 unsigned char buffer[ELO_SMARTSET_PACKET_SIZE];
35 static struct workqueue_struct *wq;
36 static bool use_fw_quirk = true;
37 module_param(use_fw_quirk, bool, S_IRUGO);
38 MODULE_PARM_DESC(use_fw_quirk, "Do periodic pokes for broken M firmwares (default = true)");
40 static void elo_input_configured(struct hid_device *hdev,
41 struct hid_input *hidinput)
43 struct input_dev *input = hidinput->input;
45 set_bit(BTN_TOUCH, input->keybit);
46 set_bit(ABS_PRESSURE, input->absbit);
47 input_set_abs_params(input, ABS_PRESSURE, 0, 256, 0, 0);
50 static void elo_process_data(struct input_dev *input, const u8 *data, int size)
54 input_report_abs(input, ABS_X, (data[3] << 8) | data[2]);
55 input_report_abs(input, ABS_Y, (data[5] << 8) | data[4]);
59 press = (data[7] << 8) | data[6];
60 input_report_abs(input, ABS_PRESSURE, press);
63 input_report_key(input, BTN_TOUCH, 1);
68 input_report_key(input, BTN_TOUCH, 0);
73 static int elo_raw_event(struct hid_device *hdev, struct hid_report *report,
76 struct hid_input *hidinput;
78 if (!(hdev->claimed & HID_CLAIMED_INPUT) || list_empty(&hdev->inputs))
81 hidinput = list_first_entry(&hdev->inputs, struct hid_input, list);
85 if (data[0] == 'T') { /* Mandatory ELO packet marker */
86 elo_process_data(hidinput->input, data, size);
90 default: /* unknown report */
91 /* Unknown report type; pass upstream */
92 hid_info(hdev, "unknown report type %d\n", report->id);
99 static int elo_smartset_send_get(struct usb_device *dev, u8 command,
105 if (command == ELO_SEND_SMARTSET_COMMAND) {
106 pipe = usb_sndctrlpipe(dev, 0);
108 } else if (command == ELO_GET_SMARTSET_RESPONSE) {
109 pipe = usb_rcvctrlpipe(dev, 0);
114 return usb_control_msg(dev, pipe, command,
115 dir | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116 0, 0, data, ELO_SMARTSET_PACKET_SIZE,
117 ELO_SMARTSET_CMD_TIMEOUT);
120 static int elo_flush_smartset_responses(struct usb_device *dev)
122 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
123 ELO_FLUSH_SMARTSET_RESPONSES,
124 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
125 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
128 static void elo_work(struct work_struct *work)
130 struct elo_priv *priv = container_of(work, struct elo_priv, work.work);
131 struct usb_device *dev = priv->usbdev;
132 unsigned char *buffer = priv->buffer;
135 ret = elo_flush_smartset_responses(dev);
137 dev_err(&dev->dev, "initial FLUSH_SMARTSET_RESPONSES failed, error %d\n",
142 /* send Diagnostics command */
144 ret = elo_smartset_send_get(dev, ELO_SEND_SMARTSET_COMMAND, buffer);
146 dev_err(&dev->dev, "send Diagnostics Command failed, error %d\n",
152 ret = elo_smartset_send_get(dev, ELO_GET_SMARTSET_RESPONSE, buffer);
154 dev_err(&dev->dev, "get Diagnostics Command response failed, error %d\n",
160 if (*buffer != 'A') {
161 ret = elo_smartset_send_get(dev, ELO_GET_SMARTSET_RESPONSE,
164 dev_err(&dev->dev, "get acknowledge response failed, error %d\n",
171 ret = elo_flush_smartset_responses(dev);
173 dev_err(&dev->dev, "final FLUSH_SMARTSET_RESPONSES failed, error %d\n",
175 queue_delayed_work(wq, &priv->work, ELO_PERIODIC_READ_INTERVAL);
179 * Not all Elo devices need the periodic HID descriptor reads.
180 * Only firmware version M needs this.
182 static bool elo_broken_firmware(struct usb_device *dev)
184 return use_fw_quirk && le16_to_cpu(dev->descriptor.bcdDevice) == 0x10d;
187 static int elo_probe(struct hid_device *hdev, const struct hid_device_id *id)
189 struct elo_priv *priv;
192 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
196 INIT_DELAYED_WORK(&priv->work, elo_work);
197 priv->usbdev = interface_to_usbdev(to_usb_interface(hdev->dev.parent));
199 hid_set_drvdata(hdev, priv);
201 ret = hid_parse(hdev);
203 hid_err(hdev, "parse failed\n");
207 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
209 hid_err(hdev, "hw start failed\n");
213 if (elo_broken_firmware(priv->usbdev)) {
214 hid_info(hdev, "broken firmware found, installing workaround\n");
215 queue_delayed_work(wq, &priv->work, ELO_PERIODIC_READ_INTERVAL);
224 static void elo_remove(struct hid_device *hdev)
226 struct elo_priv *priv = hid_get_drvdata(hdev);
233 static const struct hid_device_id elo_devices[] = {
234 { HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0009), },
235 { HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0030), },
238 MODULE_DEVICE_TABLE(hid, elo_devices);
240 static struct hid_driver elo_driver = {
242 .id_table = elo_devices,
244 .remove = elo_remove,
245 .raw_event = elo_raw_event,
246 .input_configured = elo_input_configured,
249 static int __init elo_driver_init(void)
253 wq = create_singlethread_workqueue("elousb");
257 ret = hid_register_driver(&elo_driver);
259 destroy_workqueue(wq);
263 module_init(elo_driver_init);
265 static void __exit elo_driver_exit(void)
267 hid_unregister_driver(&elo_driver);
268 destroy_workqueue(wq);
270 module_exit(elo_driver_exit);
272 MODULE_AUTHOR("Jiri Slaby <jslaby@suse.cz>");
273 MODULE_LICENSE("GPL");