w1_int.c 5.5 KB

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