rmi_f03.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2015-2016 Red Hat
  3. * Copyright (C) 2015 Lyude Paul <thatslyude@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/serio.h>
  12. #include <linux/notifier.h>
  13. #include "rmi_driver.h"
  14. #define RMI_F03_RX_DATA_OFB 0x01
  15. #define RMI_F03_OB_SIZE 2
  16. #define RMI_F03_OB_OFFSET 2
  17. #define RMI_F03_OB_DATA_OFFSET 1
  18. #define RMI_F03_OB_FLAG_TIMEOUT BIT(6)
  19. #define RMI_F03_OB_FLAG_PARITY BIT(7)
  20. #define RMI_F03_DEVICE_COUNT 0x07
  21. #define RMI_F03_BYTES_PER_DEVICE 0x07
  22. #define RMI_F03_BYTES_PER_DEVICE_SHIFT 4
  23. #define RMI_F03_QUEUE_LENGTH 0x0F
  24. #define PSMOUSE_OOB_EXTRA_BTNS 0x01
  25. struct f03_data {
  26. struct rmi_function *fn;
  27. struct serio *serio;
  28. bool serio_registered;
  29. unsigned int overwrite_buttons;
  30. u8 device_count;
  31. u8 rx_queue_length;
  32. };
  33. int rmi_f03_overwrite_button(struct rmi_function *fn, unsigned int button,
  34. int value)
  35. {
  36. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  37. unsigned int bit;
  38. if (button < BTN_LEFT || button > BTN_MIDDLE)
  39. return -EINVAL;
  40. bit = BIT(button - BTN_LEFT);
  41. if (value)
  42. f03->overwrite_buttons |= bit;
  43. else
  44. f03->overwrite_buttons &= ~bit;
  45. return 0;
  46. }
  47. void rmi_f03_commit_buttons(struct rmi_function *fn)
  48. {
  49. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  50. struct serio *serio = f03->serio;
  51. serio_pause_rx(serio);
  52. if (serio->drv) {
  53. serio->drv->interrupt(serio, PSMOUSE_OOB_EXTRA_BTNS,
  54. SERIO_OOB_DATA);
  55. serio->drv->interrupt(serio, f03->overwrite_buttons,
  56. SERIO_OOB_DATA);
  57. }
  58. serio_continue_rx(serio);
  59. }
  60. static int rmi_f03_pt_write(struct serio *id, unsigned char val)
  61. {
  62. struct f03_data *f03 = id->port_data;
  63. int error;
  64. rmi_dbg(RMI_DEBUG_FN, &f03->fn->dev,
  65. "%s: Wrote %.2hhx to PS/2 passthrough address",
  66. __func__, val);
  67. error = rmi_write(f03->fn->rmi_dev, f03->fn->fd.data_base_addr, val);
  68. if (error) {
  69. dev_err(&f03->fn->dev,
  70. "%s: Failed to write to F03 TX register (%d).\n",
  71. __func__, error);
  72. return error;
  73. }
  74. return 0;
  75. }
  76. static int rmi_f03_initialize(struct f03_data *f03)
  77. {
  78. struct rmi_function *fn = f03->fn;
  79. struct device *dev = &fn->dev;
  80. int error;
  81. u8 bytes_per_device;
  82. u8 query1;
  83. u8 query2[RMI_F03_DEVICE_COUNT * RMI_F03_BYTES_PER_DEVICE];
  84. size_t query2_len;
  85. error = rmi_read(fn->rmi_dev, fn->fd.query_base_addr, &query1);
  86. if (error) {
  87. dev_err(dev, "Failed to read query register (%d).\n", error);
  88. return error;
  89. }
  90. f03->device_count = query1 & RMI_F03_DEVICE_COUNT;
  91. bytes_per_device = (query1 >> RMI_F03_BYTES_PER_DEVICE_SHIFT) &
  92. RMI_F03_BYTES_PER_DEVICE;
  93. query2_len = f03->device_count * bytes_per_device;
  94. /*
  95. * The first generation of image sensors don't have a second part to
  96. * their f03 query, as such we have to set some of these values manually
  97. */
  98. if (query2_len < 1) {
  99. f03->device_count = 1;
  100. f03->rx_queue_length = 7;
  101. } else {
  102. error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr + 1,
  103. query2, query2_len);
  104. if (error) {
  105. dev_err(dev,
  106. "Failed to read second set of query registers (%d).\n",
  107. error);
  108. return error;
  109. }
  110. f03->rx_queue_length = query2[0] & RMI_F03_QUEUE_LENGTH;
  111. }
  112. return 0;
  113. }
  114. static int rmi_f03_pt_open(struct serio *serio)
  115. {
  116. struct f03_data *f03 = serio->port_data;
  117. struct rmi_function *fn = f03->fn;
  118. const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
  119. const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
  120. u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
  121. int error;
  122. /*
  123. * Consume any pending data. Some devices like to spam with
  124. * 0xaa 0x00 announcements which may confuse us as we try to
  125. * probe the device.
  126. */
  127. error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
  128. if (!error)
  129. rmi_dbg(RMI_DEBUG_FN, &fn->dev,
  130. "%s: Consumed %*ph (%d) from PS2 guest\n",
  131. __func__, ob_len, obs, ob_len);
  132. return fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
  133. }
  134. static void rmi_f03_pt_close(struct serio *serio)
  135. {
  136. struct f03_data *f03 = serio->port_data;
  137. struct rmi_function *fn = f03->fn;
  138. fn->rmi_dev->driver->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
  139. }
  140. static int rmi_f03_register_pt(struct f03_data *f03)
  141. {
  142. struct serio *serio;
  143. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  144. if (!serio)
  145. return -ENOMEM;
  146. serio->id.type = SERIO_PS_PSTHRU;
  147. serio->write = rmi_f03_pt_write;
  148. serio->open = rmi_f03_pt_open;
  149. serio->close = rmi_f03_pt_close;
  150. serio->port_data = f03;
  151. strlcpy(serio->name, "RMI4 PS/2 pass-through", sizeof(serio->name));
  152. snprintf(serio->phys, sizeof(serio->phys), "%s/serio0",
  153. dev_name(&f03->fn->dev));
  154. serio->dev.parent = &f03->fn->dev;
  155. f03->serio = serio;
  156. printk(KERN_INFO "serio: %s port at %s\n",
  157. serio->name, dev_name(&f03->fn->dev));
  158. serio_register_port(serio);
  159. return 0;
  160. }
  161. static int rmi_f03_probe(struct rmi_function *fn)
  162. {
  163. struct device *dev = &fn->dev;
  164. struct f03_data *f03;
  165. int error;
  166. f03 = devm_kzalloc(dev, sizeof(struct f03_data), GFP_KERNEL);
  167. if (!f03)
  168. return -ENOMEM;
  169. f03->fn = fn;
  170. error = rmi_f03_initialize(f03);
  171. if (error < 0)
  172. return error;
  173. if (f03->device_count != 1)
  174. dev_warn(dev, "found %d devices on PS/2 passthrough",
  175. f03->device_count);
  176. dev_set_drvdata(dev, f03);
  177. return 0;
  178. }
  179. static int rmi_f03_config(struct rmi_function *fn)
  180. {
  181. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  182. int error;
  183. if (!f03->serio_registered) {
  184. error = rmi_f03_register_pt(f03);
  185. if (error)
  186. return error;
  187. f03->serio_registered = true;
  188. } else {
  189. /*
  190. * We must be re-configuring the sensor, just enable
  191. * interrupts for this function.
  192. */
  193. fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
  194. }
  195. return 0;
  196. }
  197. static irqreturn_t rmi_f03_attention(int irq, void *ctx)
  198. {
  199. struct rmi_function *fn = ctx;
  200. struct rmi_device *rmi_dev = fn->rmi_dev;
  201. struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
  202. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  203. const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
  204. const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
  205. u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
  206. u8 ob_status;
  207. u8 ob_data;
  208. unsigned int serio_flags;
  209. int i;
  210. int error;
  211. if (drvdata->attn_data.data) {
  212. /* First grab the data passed by the transport device */
  213. if (drvdata->attn_data.size < ob_len) {
  214. dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
  215. return IRQ_HANDLED;
  216. }
  217. memcpy(obs, drvdata->attn_data.data, ob_len);
  218. drvdata->attn_data.data += ob_len;
  219. drvdata->attn_data.size -= ob_len;
  220. } else {
  221. /* Grab all of the data registers, and check them for data */
  222. error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
  223. if (error) {
  224. dev_err(&fn->dev,
  225. "%s: Failed to read F03 output buffers: %d\n",
  226. __func__, error);
  227. serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
  228. return IRQ_RETVAL(error);
  229. }
  230. }
  231. for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
  232. ob_status = obs[i];
  233. ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
  234. serio_flags = 0;
  235. if (!(ob_status & RMI_F03_RX_DATA_OFB))
  236. continue;
  237. if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
  238. serio_flags |= SERIO_TIMEOUT;
  239. if (ob_status & RMI_F03_OB_FLAG_PARITY)
  240. serio_flags |= SERIO_PARITY;
  241. rmi_dbg(RMI_DEBUG_FN, &fn->dev,
  242. "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
  243. __func__, ob_data,
  244. serio_flags & SERIO_TIMEOUT ? 'Y' : 'N',
  245. serio_flags & SERIO_PARITY ? 'Y' : 'N');
  246. serio_interrupt(f03->serio, ob_data, serio_flags);
  247. }
  248. return IRQ_HANDLED;
  249. }
  250. static void rmi_f03_remove(struct rmi_function *fn)
  251. {
  252. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  253. if (f03->serio_registered)
  254. serio_unregister_port(f03->serio);
  255. }
  256. struct rmi_function_handler rmi_f03_handler = {
  257. .driver = {
  258. .name = "rmi4_f03",
  259. },
  260. .func = 0x03,
  261. .probe = rmi_f03_probe,
  262. .config = rmi_f03_config,
  263. .attention = rmi_f03_attention,
  264. .remove = rmi_f03_remove,
  265. };
  266. MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
  267. MODULE_DESCRIPTION("RMI F03 module");
  268. MODULE_LICENSE("GPL");