w1_int.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "w1.h"
  28. #include "w1_log.h"
  29. #include "w1_netlink.h"
  30. #include "w1_int.h"
  31. static int w1_search_count = -1; /* Default is continual scan */
  32. module_param_named(search_count, w1_search_count, int, 0);
  33. static int w1_enable_pullup = 1;
  34. module_param_named(enable_pullup, w1_enable_pullup, int, 0);
  35. static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
  36. struct device_driver *driver,
  37. struct device *device)
  38. {
  39. struct w1_master *dev;
  40. int err;
  41. /*
  42. * We are in process context(kernel thread), so can sleep.
  43. */
  44. dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
  45. if (!dev) {
  46. printk(KERN_ERR
  47. "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. mutex_init(&dev->mutex);
  67. memcpy(&dev->dev, device, sizeof(struct device));
  68. dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
  69. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  70. dev->driver = driver;
  71. dev->seq = 1;
  72. err = device_register(&dev->dev);
  73. if (err) {
  74. printk(KERN_ERR "Failed to register master device. err=%d\n", err);
  75. memset(dev, 0, sizeof(struct w1_master));
  76. kfree(dev);
  77. dev = NULL;
  78. }
  79. return dev;
  80. }
  81. static void w1_free_dev(struct w1_master *dev)
  82. {
  83. device_unregister(&dev->dev);
  84. }
  85. int w1_add_master_device(struct w1_bus_master *master)
  86. {
  87. struct w1_master *dev, *entry;
  88. int retval = 0;
  89. struct w1_netlink_msg msg;
  90. int id, found;
  91. /* validate minimum functionality */
  92. if (!(master->touch_bit && master->reset_bus) &&
  93. !(master->write_bit && master->read_bit) &&
  94. !(master->write_byte && master->read_byte && master->reset_bus)) {
  95. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  96. return(-EINVAL);
  97. }
  98. /* While it would be electrically possible to make a device that
  99. * generated a strong pullup in bit bang mode, only hardare that
  100. * controls 1-wire time frames are even expected to support a strong
  101. * pullup. w1_io.c would need to support calling set_pullup before
  102. * the last write_bit operation of a w1_write_8 which it currently
  103. * doesn't.
  104. */
  105. if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  106. printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  107. "write_byte or touch_bit, disabling\n");
  108. master->set_pullup = NULL;
  109. }
  110. /* Lock until the device is added (or not) to w1_masters. */
  111. mutex_lock(&w1_mlock);
  112. /* Search for the first available id (starting at 1). */
  113. id = 0;
  114. do {
  115. ++id;
  116. found = 0;
  117. list_for_each_entry(entry, &w1_masters, w1_master_entry) {
  118. if (entry->id == id) {
  119. found = 1;
  120. break;
  121. }
  122. }
  123. } while (found);
  124. dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
  125. &w1_master_driver, &w1_master_device);
  126. if (!dev) {
  127. mutex_unlock(&w1_mlock);
  128. return -ENOMEM;
  129. }
  130. retval = w1_create_master_attributes(dev);
  131. if (retval) {
  132. mutex_unlock(&w1_mlock);
  133. goto err_out_free_dev;
  134. }
  135. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  136. dev->initialized = 1;
  137. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  138. if (IS_ERR(dev->thread)) {
  139. retval = PTR_ERR(dev->thread);
  140. dev_err(&dev->dev,
  141. "Failed to create new kernel thread. err=%d\n",
  142. retval);
  143. mutex_unlock(&w1_mlock);
  144. goto err_out_rm_attr;
  145. }
  146. list_add(&dev->w1_master_entry, &w1_masters);
  147. mutex_unlock(&w1_mlock);
  148. memset(&msg, 0, sizeof(msg));
  149. msg.id.mst.id = dev->id;
  150. msg.type = W1_MASTER_ADD;
  151. w1_netlink_send(dev, &msg);
  152. return 0;
  153. #if 0 /* Thread cleanup code, not required currently. */
  154. err_out_kill_thread:
  155. kthread_stop(dev->thread);
  156. #endif
  157. err_out_rm_attr:
  158. w1_destroy_master_attributes(dev);
  159. err_out_free_dev:
  160. w1_free_dev(dev);
  161. return retval;
  162. }
  163. void __w1_remove_master_device(struct w1_master *dev)
  164. {
  165. struct w1_netlink_msg msg;
  166. struct w1_slave *sl, *sln;
  167. kthread_stop(dev->thread);
  168. mutex_lock(&w1_mlock);
  169. list_del(&dev->w1_master_entry);
  170. mutex_unlock(&w1_mlock);
  171. mutex_lock(&dev->mutex);
  172. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry)
  173. w1_slave_detach(sl);
  174. w1_destroy_master_attributes(dev);
  175. mutex_unlock(&dev->mutex);
  176. atomic_dec(&dev->refcnt);
  177. while (atomic_read(&dev->refcnt)) {
  178. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  179. dev->name, atomic_read(&dev->refcnt));
  180. if (msleep_interruptible(1000))
  181. flush_signals(current);
  182. }
  183. memset(&msg, 0, sizeof(msg));
  184. msg.id.mst.id = dev->id;
  185. msg.type = W1_MASTER_REMOVE;
  186. w1_netlink_send(dev, &msg);
  187. w1_free_dev(dev);
  188. }
  189. void w1_remove_master_device(struct w1_bus_master *bm)
  190. {
  191. struct w1_master *dev, *found = NULL;
  192. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  193. if (!dev->initialized)
  194. continue;
  195. if (dev->bus_master->data == bm->data) {
  196. found = dev;
  197. break;
  198. }
  199. }
  200. if (!found) {
  201. printk(KERN_ERR "Device doesn't exist.\n");
  202. return;
  203. }
  204. __w1_remove_master_device(found);
  205. }
  206. EXPORT_SYMBOL(w1_add_master_device);
  207. EXPORT_SYMBOL(w1_remove_master_device);