psmouse-smbus.c 7.2 KB

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