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