hid-elo.c 7.6 KB

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