w1_int.c 6.7 KB

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