hid-elo.c 7.3 KB

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