psmouse-smbus.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2017 Red Hat, Inc
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/libps2.h>
  12. #include <linux/i2c.h>
  13. #include <linux/serio.h>
  14. #include <linux/slab.h>
  15. #include <linux/workqueue.h>
  16. #include "psmouse.h"
  17. struct psmouse_smbus_dev {
  18. struct i2c_board_info board;
  19. struct psmouse *psmouse;
  20. struct i2c_client *client;
  21. struct list_head node;
  22. bool dead;
  23. bool need_deactivate;
  24. };
  25. static LIST_HEAD(psmouse_smbus_list);
  26. static DEFINE_MUTEX(psmouse_smbus_mutex);
  27. static void psmouse_smbus_check_adapter(struct i2c_adapter *adapter)
  28. {
  29. struct psmouse_smbus_dev *smbdev;
  30. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
  31. return;
  32. mutex_lock(&psmouse_smbus_mutex);
  33. list_for_each_entry(smbdev, &psmouse_smbus_list, node) {
  34. if (smbdev->dead)
  35. continue;
  36. if (smbdev->client)
  37. continue;
  38. /*
  39. * Here would be a good place to check if device is actually
  40. * present, but it seems that SMBus will not respond unless we
  41. * fully reset PS/2 connection. So cross our fingers, and try
  42. * to switch over, hopefully our system will not have too many
  43. * "host notify" I2C adapters.
  44. */
  45. psmouse_dbg(smbdev->psmouse,
  46. "SMBus candidate adapter appeared, triggering rescan\n");
  47. serio_rescan(smbdev->psmouse->ps2dev.serio);
  48. }
  49. mutex_unlock(&psmouse_smbus_mutex);
  50. }
  51. static void psmouse_smbus_detach_i2c_client(struct i2c_client *client)
  52. {
  53. struct psmouse_smbus_dev *smbdev, *tmp;
  54. mutex_lock(&psmouse_smbus_mutex);
  55. list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
  56. if (smbdev->client != client)
  57. continue;
  58. kfree(client->dev.platform_data);
  59. client->dev.platform_data = NULL;
  60. if (!smbdev->dead) {
  61. psmouse_dbg(smbdev->psmouse,
  62. "Marking SMBus companion %s as gone\n",
  63. dev_name(&smbdev->client->dev));
  64. smbdev->dead = true;
  65. serio_rescan(smbdev->psmouse->ps2dev.serio);
  66. } else {
  67. list_del(&smbdev->node);
  68. kfree(smbdev);
  69. }
  70. }
  71. mutex_unlock(&psmouse_smbus_mutex);
  72. }
  73. static int psmouse_smbus_notifier_call(struct notifier_block *nb,
  74. unsigned long action, void *data)
  75. {
  76. struct device *dev = data;
  77. switch (action) {
  78. case BUS_NOTIFY_ADD_DEVICE:
  79. if (dev->type == &i2c_adapter_type)
  80. psmouse_smbus_check_adapter(to_i2c_adapter(dev));
  81. break;
  82. case BUS_NOTIFY_REMOVED_DEVICE:
  83. if (dev->type == &i2c_client_type)
  84. psmouse_smbus_detach_i2c_client(to_i2c_client(dev));
  85. break;
  86. }
  87. return 0;
  88. }
  89. static struct notifier_block psmouse_smbus_notifier = {
  90. .notifier_call = psmouse_smbus_notifier_call,
  91. };
  92. static psmouse_ret_t psmouse_smbus_process_byte(struct psmouse *psmouse)
  93. {
  94. return PSMOUSE_FULL_PACKET;
  95. }
  96. static int psmouse_smbus_reconnect(struct psmouse *psmouse)
  97. {
  98. struct psmouse_smbus_dev *smbdev = psmouse->private;
  99. if (smbdev->need_deactivate)
  100. psmouse_deactivate(psmouse);
  101. return 0;
  102. }
  103. struct psmouse_smbus_removal_work {
  104. struct work_struct work;
  105. struct i2c_client *client;
  106. };
  107. static void psmouse_smbus_remove_i2c_device(struct work_struct *work)
  108. {
  109. struct psmouse_smbus_removal_work *rwork =
  110. container_of(work, struct psmouse_smbus_removal_work, work);
  111. dev_dbg(&rwork->client->dev, "destroying SMBus companion device\n");
  112. i2c_unregister_device(rwork->client);
  113. kfree(rwork);
  114. }
  115. /*
  116. * This schedules removal of SMBus companion device. We have to do
  117. * it in a separate tread to avoid deadlocking on psmouse_mutex in
  118. * case the device has a trackstick (which is also driven by psmouse).
  119. *
  120. * Note that this may be racing with i2c adapter removal, but we
  121. * can't do anything about that: i2c automatically destroys clients
  122. * attached to an adapter that is being removed. This has to be
  123. * fixed in i2c core.
  124. */
  125. static void psmouse_smbus_schedule_remove(struct i2c_client *client)
  126. {
  127. struct psmouse_smbus_removal_work *rwork;
  128. rwork = kzalloc(sizeof(*rwork), GFP_KERNEL);
  129. if (rwork) {
  130. INIT_WORK(&rwork->work, psmouse_smbus_remove_i2c_device);
  131. rwork->client = client;
  132. schedule_work(&rwork->work);
  133. }
  134. }
  135. static void psmouse_smbus_disconnect(struct psmouse *psmouse)
  136. {
  137. struct psmouse_smbus_dev *smbdev = psmouse->private;
  138. mutex_lock(&psmouse_smbus_mutex);
  139. if (smbdev->dead) {
  140. list_del(&smbdev->node);
  141. kfree(smbdev);
  142. } else {
  143. smbdev->dead = true;
  144. psmouse_dbg(smbdev->psmouse,
  145. "posting removal request for SMBus companion %s\n",
  146. dev_name(&smbdev->client->dev));
  147. psmouse_smbus_schedule_remove(smbdev->client);
  148. }
  149. mutex_unlock(&psmouse_smbus_mutex);
  150. psmouse->private = NULL;
  151. }
  152. static int psmouse_smbus_create_companion(struct device *dev, void *data)
  153. {
  154. struct psmouse_smbus_dev *smbdev = data;
  155. unsigned short addr_list[] = { smbdev->board.addr, I2C_CLIENT_END };
  156. struct i2c_adapter *adapter;
  157. adapter = i2c_verify_adapter(dev);
  158. if (!adapter)
  159. return 0;
  160. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HOST_NOTIFY))
  161. return 0;
  162. smbdev->client = i2c_new_probed_device(adapter, &smbdev->board,
  163. addr_list, NULL);
  164. if (!smbdev->client)
  165. return 0;
  166. /* We have our(?) device, stop iterating i2c bus. */
  167. return 1;
  168. }
  169. void psmouse_smbus_cleanup(struct psmouse *psmouse)
  170. {
  171. struct psmouse_smbus_dev *smbdev, *tmp;
  172. mutex_lock(&psmouse_smbus_mutex);
  173. list_for_each_entry_safe(smbdev, tmp, &psmouse_smbus_list, node) {
  174. if (psmouse == smbdev->psmouse) {
  175. list_del(&smbdev->node);
  176. kfree(smbdev);
  177. }
  178. }
  179. mutex_unlock(&psmouse_smbus_mutex);
  180. }
  181. int psmouse_smbus_init(struct psmouse *psmouse,
  182. const struct i2c_board_info *board,
  183. const void *pdata, size_t pdata_size,
  184. bool need_deactivate,
  185. bool leave_breadcrumbs)
  186. {
  187. struct psmouse_smbus_dev *smbdev;
  188. int error;
  189. smbdev = kzalloc(sizeof(*smbdev), GFP_KERNEL);
  190. if (!smbdev)
  191. return -ENOMEM;
  192. smbdev->psmouse = psmouse;
  193. smbdev->board = *board;
  194. smbdev->need_deactivate = need_deactivate;
  195. if (pdata) {
  196. smbdev->board.platform_data = kmemdup(pdata, pdata_size,
  197. GFP_KERNEL);
  198. if (!smbdev->board.platform_data) {
  199. kfree(smbdev);
  200. return -ENOMEM;
  201. }
  202. }
  203. if (need_deactivate)
  204. psmouse_deactivate(psmouse);
  205. psmouse->private = smbdev;
  206. psmouse->protocol_handler = psmouse_smbus_process_byte;
  207. psmouse->reconnect = psmouse_smbus_reconnect;
  208. psmouse->fast_reconnect = psmouse_smbus_reconnect;
  209. psmouse->disconnect = psmouse_smbus_disconnect;
  210. psmouse->resync_time = 0;
  211. mutex_lock(&psmouse_smbus_mutex);
  212. list_add_tail(&smbdev->node, &psmouse_smbus_list);
  213. mutex_unlock(&psmouse_smbus_mutex);
  214. /* Bind to already existing adapters right away */
  215. error = i2c_for_each_dev(smbdev, psmouse_smbus_create_companion);
  216. if (smbdev->client) {
  217. /* We have our companion device */
  218. return 0;
  219. }
  220. /*
  221. * If we did not create i2c device we will not need platform
  222. * data even if we are leaving breadcrumbs.
  223. */
  224. kfree(smbdev->board.platform_data);
  225. smbdev->board.platform_data = NULL;
  226. if (error < 0 || !leave_breadcrumbs) {
  227. mutex_lock(&psmouse_smbus_mutex);
  228. list_del(&smbdev->node);
  229. mutex_unlock(&psmouse_smbus_mutex);
  230. kfree(smbdev);
  231. }
  232. return error < 0 ? error : -EAGAIN;
  233. }
  234. int __init psmouse_smbus_module_init(void)
  235. {
  236. int error;
  237. error = bus_register_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
  238. if (error) {
  239. pr_err("failed to register i2c bus notifier: %d\n", error);
  240. return error;
  241. }
  242. return 0;
  243. }
  244. void psmouse_smbus_module_exit(void)
  245. {
  246. bus_unregister_notifier(&i2c_bus_type, &psmouse_smbus_notifier);
  247. flush_scheduled_work();
  248. }