w1_int.c 6.4 KB

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