rmi_f03.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. unsigned int overwrite_buttons;
  29. u8 device_count;
  30. u8 rx_queue_length;
  31. };
  32. int rmi_f03_overwrite_button(struct rmi_function *fn, unsigned int button,
  33. int value)
  34. {
  35. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  36. unsigned int bit;
  37. if (button < BTN_LEFT || button > BTN_MIDDLE)
  38. return -EINVAL;
  39. bit = BIT(button - BTN_LEFT);
  40. if (value)
  41. f03->overwrite_buttons |= bit;
  42. else
  43. f03->overwrite_buttons &= ~bit;
  44. return 0;
  45. }
  46. void rmi_f03_commit_buttons(struct rmi_function *fn)
  47. {
  48. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  49. struct serio *serio = f03->serio;
  50. serio_pause_rx(serio);
  51. if (serio->drv) {
  52. serio->drv->interrupt(serio, PSMOUSE_OOB_EXTRA_BTNS,
  53. SERIO_OOB_DATA);
  54. serio->drv->interrupt(serio, f03->overwrite_buttons,
  55. SERIO_OOB_DATA);
  56. }
  57. serio_continue_rx(serio);
  58. }
  59. static int rmi_f03_pt_write(struct serio *id, unsigned char val)
  60. {
  61. struct f03_data *f03 = id->port_data;
  62. int error;
  63. rmi_dbg(RMI_DEBUG_FN, &f03->fn->dev,
  64. "%s: Wrote %.2hhx to PS/2 passthrough address",
  65. __func__, val);
  66. error = rmi_write(f03->fn->rmi_dev, f03->fn->fd.data_base_addr, val);
  67. if (error) {
  68. dev_err(&f03->fn->dev,
  69. "%s: Failed to write to F03 TX register (%d).\n",
  70. __func__, error);
  71. return error;
  72. }
  73. return 0;
  74. }
  75. static int rmi_f03_initialize(struct f03_data *f03)
  76. {
  77. struct rmi_function *fn = f03->fn;
  78. struct device *dev = &fn->dev;
  79. int error;
  80. u8 bytes_per_device;
  81. u8 query1;
  82. u8 query2[RMI_F03_DEVICE_COUNT * RMI_F03_BYTES_PER_DEVICE];
  83. size_t query2_len;
  84. error = rmi_read(fn->rmi_dev, fn->fd.query_base_addr, &query1);
  85. if (error) {
  86. dev_err(dev, "Failed to read query register (%d).\n", error);
  87. return error;
  88. }
  89. f03->device_count = query1 & RMI_F03_DEVICE_COUNT;
  90. bytes_per_device = (query1 >> RMI_F03_BYTES_PER_DEVICE_SHIFT) &
  91. RMI_F03_BYTES_PER_DEVICE;
  92. query2_len = f03->device_count * bytes_per_device;
  93. /*
  94. * The first generation of image sensors don't have a second part to
  95. * their f03 query, as such we have to set some of these values manually
  96. */
  97. if (query2_len < 1) {
  98. f03->device_count = 1;
  99. f03->rx_queue_length = 7;
  100. } else {
  101. error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr + 1,
  102. query2, query2_len);
  103. if (error) {
  104. dev_err(dev,
  105. "Failed to read second set of query registers (%d).\n",
  106. error);
  107. return error;
  108. }
  109. f03->rx_queue_length = query2[0] & RMI_F03_QUEUE_LENGTH;
  110. }
  111. return 0;
  112. }
  113. static int rmi_f03_register_pt(struct f03_data *f03)
  114. {
  115. struct serio *serio;
  116. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  117. if (!serio)
  118. return -ENOMEM;
  119. serio->id.type = SERIO_8042;
  120. serio->write = rmi_f03_pt_write;
  121. serio->port_data = f03;
  122. strlcpy(serio->name, "Synaptics RMI4 PS/2 pass-through",
  123. sizeof(serio->name));
  124. strlcpy(serio->phys, "synaptics-rmi4-pt/serio1",
  125. sizeof(serio->phys));
  126. serio->dev.parent = &f03->fn->dev;
  127. f03->serio = serio;
  128. serio_register_port(serio);
  129. return 0;
  130. }
  131. static int rmi_f03_probe(struct rmi_function *fn)
  132. {
  133. struct device *dev = &fn->dev;
  134. struct f03_data *f03;
  135. int error;
  136. f03 = devm_kzalloc(dev, sizeof(struct f03_data), GFP_KERNEL);
  137. if (!f03)
  138. return -ENOMEM;
  139. f03->fn = fn;
  140. error = rmi_f03_initialize(f03);
  141. if (error < 0)
  142. return error;
  143. if (f03->device_count != 1)
  144. dev_warn(dev, "found %d devices on PS/2 passthrough",
  145. f03->device_count);
  146. dev_set_drvdata(dev, f03);
  147. error = rmi_f03_register_pt(f03);
  148. if (error)
  149. return error;
  150. return 0;
  151. }
  152. static int rmi_f03_config(struct rmi_function *fn)
  153. {
  154. fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
  155. return 0;
  156. }
  157. static int rmi_f03_attention(struct rmi_function *fn, unsigned long *irq_bits)
  158. {
  159. struct rmi_device *rmi_dev = fn->rmi_dev;
  160. struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
  161. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  162. u16 data_addr = fn->fd.data_base_addr;
  163. const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
  164. u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
  165. u8 ob_status;
  166. u8 ob_data;
  167. unsigned int serio_flags;
  168. int i;
  169. int error;
  170. if (drvdata->attn_data.data) {
  171. /* First grab the data passed by the transport device */
  172. if (drvdata->attn_data.size < ob_len) {
  173. dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
  174. return 0;
  175. }
  176. memcpy(obs, drvdata->attn_data.data, ob_len);
  177. drvdata->attn_data.data += ob_len;
  178. drvdata->attn_data.size -= ob_len;
  179. } else {
  180. /* Grab all of the data registers, and check them for data */
  181. error = rmi_read_block(fn->rmi_dev, data_addr + RMI_F03_OB_OFFSET,
  182. &obs, ob_len);
  183. if (error) {
  184. dev_err(&fn->dev,
  185. "%s: Failed to read F03 output buffers: %d\n",
  186. __func__, error);
  187. serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
  188. return error;
  189. }
  190. }
  191. for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
  192. ob_status = obs[i];
  193. ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
  194. serio_flags = 0;
  195. if (!(ob_status & RMI_F03_RX_DATA_OFB))
  196. continue;
  197. if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
  198. serio_flags |= SERIO_TIMEOUT;
  199. if (ob_status & RMI_F03_OB_FLAG_PARITY)
  200. serio_flags |= SERIO_PARITY;
  201. rmi_dbg(RMI_DEBUG_FN, &fn->dev,
  202. "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
  203. __func__, ob_data,
  204. serio_flags & SERIO_TIMEOUT ? 'Y' : 'N',
  205. serio_flags & SERIO_PARITY ? 'Y' : 'N');
  206. serio_interrupt(f03->serio, ob_data, serio_flags);
  207. }
  208. return 0;
  209. }
  210. static void rmi_f03_remove(struct rmi_function *fn)
  211. {
  212. struct f03_data *f03 = dev_get_drvdata(&fn->dev);
  213. serio_unregister_port(f03->serio);
  214. }
  215. struct rmi_function_handler rmi_f03_handler = {
  216. .driver = {
  217. .name = "rmi4_f03",
  218. },
  219. .func = 0x03,
  220. .probe = rmi_f03_probe,
  221. .config = rmi_f03_config,
  222. .attention = rmi_f03_attention,
  223. .remove = rmi_f03_remove,
  224. };
  225. MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
  226. MODULE_DESCRIPTION("RMI F03 module");
  227. MODULE_LICENSE("GPL");