w1_int.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/list.h>
  7. #include <linux/delay.h>
  8. #include <linux/kthread.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/export.h>
  12. #include <linux/moduleparam.h>
  13. #include "w1_internal.h"
  14. #include "w1_netlink.h"
  15. static int w1_search_count = -1; /* Default is continual scan */
  16. module_param_named(search_count, w1_search_count, int, 0);
  17. static int w1_enable_pullup = 1;
  18. module_param_named(enable_pullup, w1_enable_pullup, int, 0);
  19. static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
  20. struct device_driver *driver,
  21. struct device *device)
  22. {
  23. struct w1_master *dev;
  24. int err;
  25. /*
  26. * We are in process context(kernel thread), so can sleep.
  27. */
  28. dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
  29. if (!dev) {
  30. pr_err("Failed to allocate %zd bytes for new w1 device.\n",
  31. sizeof(struct w1_master));
  32. return NULL;
  33. }
  34. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  35. dev->owner = THIS_MODULE;
  36. dev->max_slave_count = slave_count;
  37. dev->slave_count = 0;
  38. dev->attempts = 0;
  39. dev->initialized = 0;
  40. dev->id = id;
  41. dev->slave_ttl = slave_ttl;
  42. dev->search_count = w1_search_count;
  43. dev->enable_pullup = w1_enable_pullup;
  44. /* 1 for w1_process to decrement
  45. * 1 for __w1_remove_master_device to decrement
  46. */
  47. atomic_set(&dev->refcnt, 2);
  48. INIT_LIST_HEAD(&dev->slist);
  49. INIT_LIST_HEAD(&dev->async_list);
  50. mutex_init(&dev->mutex);
  51. mutex_init(&dev->bus_mutex);
  52. mutex_init(&dev->list_mutex);
  53. memcpy(&dev->dev, device, sizeof(struct device));
  54. dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
  55. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  56. dev->dev.init_name = dev->name;
  57. dev->driver = driver;
  58. dev->seq = 1;
  59. err = device_register(&dev->dev);
  60. if (err) {
  61. pr_err("Failed to register master device. err=%d\n", err);
  62. put_device(&dev->dev);
  63. dev = NULL;
  64. }
  65. return dev;
  66. }
  67. static void w1_free_dev(struct w1_master *dev)
  68. {
  69. device_unregister(&dev->dev);
  70. }
  71. /**
  72. * w1_add_master_device() - registers a new master device
  73. * @master: master bus device to register
  74. */
  75. int w1_add_master_device(struct w1_bus_master *master)
  76. {
  77. struct w1_master *dev, *entry;
  78. int retval = 0;
  79. struct w1_netlink_msg msg;
  80. int id, found;
  81. /* validate minimum functionality */
  82. if (!(master->touch_bit && master->reset_bus) &&
  83. !(master->write_bit && master->read_bit) &&
  84. !(master->write_byte && master->read_byte && master->reset_bus)) {
  85. pr_err("w1_add_master_device: invalid function set\n");
  86. return(-EINVAL);
  87. }
  88. /* Lock until the device is added (or not) to w1_masters. */
  89. mutex_lock(&w1_mlock);
  90. /* Search for the first available id (starting at 1). */
  91. id = 0;
  92. do {
  93. ++id;
  94. found = 0;
  95. list_for_each_entry(entry, &w1_masters, w1_master_entry) {
  96. if (entry->id == id) {
  97. found = 1;
  98. break;
  99. }
  100. }
  101. } while (found);
  102. dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
  103. &w1_master_driver, &w1_master_device);
  104. if (!dev) {
  105. mutex_unlock(&w1_mlock);
  106. return -ENOMEM;
  107. }
  108. retval = w1_create_master_attributes(dev);
  109. if (retval) {
  110. mutex_unlock(&w1_mlock);
  111. goto err_out_free_dev;
  112. }
  113. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  114. dev->initialized = 1;
  115. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  116. if (IS_ERR(dev->thread)) {
  117. retval = PTR_ERR(dev->thread);
  118. dev_err(&dev->dev,
  119. "Failed to create new kernel thread. err=%d\n",
  120. retval);
  121. mutex_unlock(&w1_mlock);
  122. goto err_out_rm_attr;
  123. }
  124. list_add(&dev->w1_master_entry, &w1_masters);
  125. mutex_unlock(&w1_mlock);
  126. memset(&msg, 0, sizeof(msg));
  127. msg.id.mst.id = dev->id;
  128. msg.type = W1_MASTER_ADD;
  129. w1_netlink_send(dev, &msg);
  130. return 0;
  131. #if 0 /* Thread cleanup code, not required currently. */
  132. err_out_kill_thread:
  133. set_bit(W1_ABORT_SEARCH, &dev->flags);
  134. kthread_stop(dev->thread);
  135. #endif
  136. err_out_rm_attr:
  137. w1_destroy_master_attributes(dev);
  138. err_out_free_dev:
  139. w1_free_dev(dev);
  140. return retval;
  141. }
  142. EXPORT_SYMBOL(w1_add_master_device);
  143. void __w1_remove_master_device(struct w1_master *dev)
  144. {
  145. struct w1_netlink_msg msg;
  146. struct w1_slave *sl, *sln;
  147. mutex_lock(&w1_mlock);
  148. list_del(&dev->w1_master_entry);
  149. mutex_unlock(&w1_mlock);
  150. set_bit(W1_ABORT_SEARCH, &dev->flags);
  151. kthread_stop(dev->thread);
  152. mutex_lock(&dev->mutex);
  153. mutex_lock(&dev->list_mutex);
  154. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  155. mutex_unlock(&dev->list_mutex);
  156. w1_slave_detach(sl);
  157. mutex_lock(&dev->list_mutex);
  158. }
  159. w1_destroy_master_attributes(dev);
  160. mutex_unlock(&dev->list_mutex);
  161. mutex_unlock(&dev->mutex);
  162. atomic_dec(&dev->refcnt);
  163. while (atomic_read(&dev->refcnt)) {
  164. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  165. dev->name, atomic_read(&dev->refcnt));
  166. if (msleep_interruptible(1000))
  167. flush_signals(current);
  168. mutex_lock(&dev->list_mutex);
  169. w1_process_callbacks(dev);
  170. mutex_unlock(&dev->list_mutex);
  171. }
  172. mutex_lock(&dev->list_mutex);
  173. w1_process_callbacks(dev);
  174. mutex_unlock(&dev->list_mutex);
  175. memset(&msg, 0, sizeof(msg));
  176. msg.id.mst.id = dev->id;
  177. msg.type = W1_MASTER_REMOVE;
  178. w1_netlink_send(dev, &msg);
  179. w1_free_dev(dev);
  180. }
  181. /**
  182. * w1_remove_master_device() - unregister a master device
  183. * @bm: master bus device to remove
  184. */
  185. void w1_remove_master_device(struct w1_bus_master *bm)
  186. {
  187. struct w1_master *dev, *found = NULL;
  188. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  189. if (!dev->initialized)
  190. continue;
  191. if (dev->bus_master->data == bm->data) {
  192. found = dev;
  193. break;
  194. }
  195. }
  196. if (!found) {
  197. pr_err("Device doesn't exist.\n");
  198. return;
  199. }
  200. __w1_remove_master_device(found);
  201. }
  202. EXPORT_SYMBOL(w1_remove_master_device);