w1_int.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * w1_int.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  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 "w1.h"
  26. #include "w1_log.h"
  27. #include "w1_netlink.h"
  28. #include "w1_int.h"
  29. static u32 w1_ids = 1;
  30. static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
  31. struct device_driver *driver,
  32. struct device *device)
  33. {
  34. struct w1_master *dev;
  35. int err;
  36. /*
  37. * We are in process context(kernel thread), so can sleep.
  38. */
  39. dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
  40. if (!dev) {
  41. printk(KERN_ERR
  42. "Failed to allocate %zd bytes for new w1 device.\n",
  43. sizeof(struct w1_master));
  44. return NULL;
  45. }
  46. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  47. dev->owner = THIS_MODULE;
  48. dev->max_slave_count = slave_count;
  49. dev->slave_count = 0;
  50. dev->attempts = 0;
  51. dev->initialized = 0;
  52. dev->id = id;
  53. dev->slave_ttl = slave_ttl;
  54. dev->search_count = -1; /* continual scan */
  55. /* 1 for w1_process to decrement
  56. * 1 for __w1_remove_master_device to decrement
  57. */
  58. atomic_set(&dev->refcnt, 2);
  59. INIT_LIST_HEAD(&dev->slist);
  60. mutex_init(&dev->mutex);
  61. memcpy(&dev->dev, device, sizeof(struct device));
  62. snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
  63. "w1_bus_master%u", dev->id);
  64. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  65. dev->driver = driver;
  66. dev->seq = 1;
  67. err = device_register(&dev->dev);
  68. if (err) {
  69. printk(KERN_ERR "Failed to register master device. err=%d\n", err);
  70. memset(dev, 0, sizeof(struct w1_master));
  71. kfree(dev);
  72. dev = NULL;
  73. }
  74. return dev;
  75. }
  76. static void w1_free_dev(struct w1_master *dev)
  77. {
  78. device_unregister(&dev->dev);
  79. }
  80. int w1_add_master_device(struct w1_bus_master *master)
  81. {
  82. struct w1_master *dev;
  83. int retval = 0;
  84. struct w1_netlink_msg msg;
  85. /* validate minimum functionality */
  86. if (!(master->touch_bit && master->reset_bus) &&
  87. !(master->write_bit && master->read_bit) &&
  88. !(master->write_byte && master->read_byte && master->reset_bus)) {
  89. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  90. return(-EINVAL);
  91. }
  92. dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
  93. if (!dev)
  94. return -ENOMEM;
  95. retval = w1_create_master_attributes(dev);
  96. if (retval)
  97. goto err_out_free_dev;
  98. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  99. dev->initialized = 1;
  100. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  101. if (IS_ERR(dev->thread)) {
  102. retval = PTR_ERR(dev->thread);
  103. dev_err(&dev->dev,
  104. "Failed to create new kernel thread. err=%d\n",
  105. retval);
  106. goto err_out_rm_attr;
  107. }
  108. mutex_lock(&w1_mlock);
  109. list_add(&dev->w1_master_entry, &w1_masters);
  110. mutex_unlock(&w1_mlock);
  111. memset(&msg, 0, sizeof(msg));
  112. msg.id.mst.id = dev->id;
  113. msg.type = W1_MASTER_ADD;
  114. w1_netlink_send(dev, &msg);
  115. return 0;
  116. #if 0 /* Thread cleanup code, not required currently. */
  117. err_out_kill_thread:
  118. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  119. kthread_stop(dev->thread);
  120. #endif
  121. err_out_rm_attr:
  122. w1_destroy_master_attributes(dev);
  123. err_out_free_dev:
  124. w1_free_dev(dev);
  125. return retval;
  126. }
  127. void __w1_remove_master_device(struct w1_master *dev)
  128. {
  129. struct w1_netlink_msg msg;
  130. struct w1_slave *sl, *sln;
  131. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  132. kthread_stop(dev->thread);
  133. mutex_lock(&w1_mlock);
  134. list_del(&dev->w1_master_entry);
  135. mutex_unlock(&w1_mlock);
  136. mutex_lock(&dev->mutex);
  137. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry)
  138. w1_slave_detach(sl);
  139. w1_destroy_master_attributes(dev);
  140. mutex_unlock(&dev->mutex);
  141. atomic_dec(&dev->refcnt);
  142. while (atomic_read(&dev->refcnt)) {
  143. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  144. dev->name, atomic_read(&dev->refcnt));
  145. if (msleep_interruptible(1000))
  146. flush_signals(current);
  147. }
  148. memset(&msg, 0, sizeof(msg));
  149. msg.id.mst.id = dev->id;
  150. msg.type = W1_MASTER_REMOVE;
  151. w1_netlink_send(dev, &msg);
  152. w1_free_dev(dev);
  153. }
  154. void w1_remove_master_device(struct w1_bus_master *bm)
  155. {
  156. struct w1_master *dev, *found = NULL;
  157. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  158. if (!dev->initialized)
  159. continue;
  160. if (dev->bus_master->data == bm->data) {
  161. found = dev;
  162. break;
  163. }
  164. }
  165. if (!found) {
  166. printk(KERN_ERR "Device doesn't exist.\n");
  167. return;
  168. }
  169. __w1_remove_master_device(found);
  170. }
  171. EXPORT_SYMBOL(w1_add_master_device);
  172. EXPORT_SYMBOL(w1_remove_master_device);