w1_int.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2004 Evgeniy Polyakov <[email protected]>
  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. /* For __w1_remove_master_device to decrement
  45. */
  46. atomic_set(&dev->refcnt, 1);
  47. INIT_LIST_HEAD(&dev->slist);
  48. INIT_LIST_HEAD(&dev->async_list);
  49. mutex_init(&dev->mutex);
  50. mutex_init(&dev->bus_mutex);
  51. mutex_init(&dev->list_mutex);
  52. memcpy(&dev->dev, device, sizeof(struct device));
  53. dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
  54. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  55. dev->dev.init_name = dev->name;
  56. dev->driver = driver;
  57. dev->seq = 1;
  58. err = device_register(&dev->dev);
  59. if (err) {
  60. pr_err("Failed to register master device. err=%d\n", err);
  61. put_device(&dev->dev);
  62. dev = NULL;
  63. }
  64. return dev;
  65. }
  66. static void w1_free_dev(struct w1_master *dev)
  67. {
  68. device_unregister(&dev->dev);
  69. }
  70. /**
  71. * w1_add_master_device() - registers a new master device
  72. * @master: master bus device to register
  73. */
  74. int w1_add_master_device(struct w1_bus_master *master)
  75. {
  76. struct w1_master *dev, *entry;
  77. int retval = 0;
  78. struct w1_netlink_msg msg;
  79. int id, found;
  80. /* validate minimum functionality */
  81. if (!(master->touch_bit && master->reset_bus) &&
  82. !(master->write_bit && master->read_bit) &&
  83. !(master->write_byte && master->read_byte && master->reset_bus)) {
  84. pr_err("w1_add_master_device: invalid function set\n");
  85. return(-EINVAL);
  86. }
  87. /* Lock until the device is added (or not) to w1_masters. */
  88. mutex_lock(&w1_mlock);
  89. /* Search for the first available id (starting at 1). */
  90. id = 0;
  91. do {
  92. ++id;
  93. found = 0;
  94. list_for_each_entry(entry, &w1_masters, w1_master_entry) {
  95. if (entry->id == id) {
  96. found = 1;
  97. break;
  98. }
  99. }
  100. } while (found);
  101. dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
  102. &w1_master_driver, &w1_master_device);
  103. if (!dev) {
  104. mutex_unlock(&w1_mlock);
  105. return -ENOMEM;
  106. }
  107. retval = w1_create_master_attributes(dev);
  108. if (retval) {
  109. mutex_unlock(&w1_mlock);
  110. goto err_out_free_dev;
  111. }
  112. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  113. dev->initialized = 1;
  114. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  115. if (IS_ERR(dev->thread)) {
  116. retval = PTR_ERR(dev->thread);
  117. dev_err(&dev->dev,
  118. "Failed to create new kernel thread. err=%d\n",
  119. retval);
  120. mutex_unlock(&w1_mlock);
  121. goto err_out_rm_attr;
  122. }
  123. list_add(&dev->w1_master_entry, &w1_masters);
  124. mutex_unlock(&w1_mlock);
  125. memset(&msg, 0, sizeof(msg));
  126. msg.id.mst.id = dev->id;
  127. msg.type = W1_MASTER_ADD;
  128. w1_netlink_send(dev, &msg);
  129. return 0;
  130. #if 0 /* Thread cleanup code, not required currently. */
  131. err_out_kill_thread:
  132. set_bit(W1_ABORT_SEARCH, &dev->flags);
  133. kthread_stop(dev->thread);
  134. #endif
  135. err_out_rm_attr:
  136. w1_destroy_master_attributes(dev);
  137. err_out_free_dev:
  138. w1_free_dev(dev);
  139. return retval;
  140. }
  141. EXPORT_SYMBOL(w1_add_master_device);
  142. void __w1_remove_master_device(struct w1_master *dev)
  143. {
  144. struct w1_netlink_msg msg;
  145. struct w1_slave *sl, *sln;
  146. mutex_lock(&w1_mlock);
  147. list_del(&dev->w1_master_entry);
  148. mutex_unlock(&w1_mlock);
  149. set_bit(W1_ABORT_SEARCH, &dev->flags);
  150. kthread_stop(dev->thread);
  151. mutex_lock(&dev->mutex);
  152. mutex_lock(&dev->list_mutex);
  153. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  154. mutex_unlock(&dev->list_mutex);
  155. w1_slave_detach(sl);
  156. mutex_lock(&dev->list_mutex);
  157. }
  158. w1_destroy_master_attributes(dev);
  159. mutex_unlock(&dev->list_mutex);
  160. mutex_unlock(&dev->mutex);
  161. atomic_dec(&dev->refcnt);
  162. while (atomic_read(&dev->refcnt)) {
  163. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  164. dev->name, atomic_read(&dev->refcnt));
  165. if (msleep_interruptible(1000))
  166. flush_signals(current);
  167. mutex_lock(&dev->list_mutex);
  168. w1_process_callbacks(dev);
  169. mutex_unlock(&dev->list_mutex);
  170. }
  171. mutex_lock(&dev->list_mutex);
  172. w1_process_callbacks(dev);
  173. mutex_unlock(&dev->list_mutex);
  174. memset(&msg, 0, sizeof(msg));
  175. msg.id.mst.id = dev->id;
  176. msg.type = W1_MASTER_REMOVE;
  177. w1_netlink_send(dev, &msg);
  178. w1_free_dev(dev);
  179. }
  180. /**
  181. * w1_remove_master_device() - unregister a master device
  182. * @bm: master bus device to remove
  183. */
  184. void w1_remove_master_device(struct w1_bus_master *bm)
  185. {
  186. struct w1_master *dev, *found = NULL;
  187. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  188. if (!dev->initialized)
  189. continue;
  190. if (dev->bus_master->data == bm->data) {
  191. found = dev;
  192. break;
  193. }
  194. }
  195. if (!found) {
  196. pr_err("Device doesn't exist.\n");
  197. return;
  198. }
  199. __w1_remove_master_device(found);
  200. }
  201. EXPORT_SYMBOL(w1_remove_master_device);