hid-elo.c 7.3 KB

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