hid-elo.c 7.6 KB

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