w1_int.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "w1.h"
  25. #include "w1_log.h"
  26. #include "w1_netlink.h"
  27. static u32 w1_ids = 1;
  28. extern struct device_driver w1_master_driver;
  29. extern struct bus_type w1_bus_type;
  30. extern struct device w1_master_device;
  31. extern int w1_max_slave_count;
  32. extern int w1_max_slave_ttl;
  33. extern struct list_head w1_masters;
  34. extern spinlock_t w1_mlock;
  35. extern int w1_process(void *);
  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 = kmalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
  46. if (!dev) {
  47. printk(KERN_ERR
  48. "Failed to allocate %zd bytes for new w1 device.\n",
  49. sizeof(struct w1_master));
  50. return NULL;
  51. }
  52. memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
  53. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  54. dev->owner = THIS_MODULE;
  55. dev->max_slave_count = slave_count;
  56. dev->slave_count = 0;
  57. dev->attempts = 0;
  58. dev->kpid = -1;
  59. dev->initialized = 0;
  60. dev->id = id;
  61. dev->slave_ttl = slave_ttl;
  62. dev->search_count = -1; /* continual scan */
  63. atomic_set(&dev->refcnt, 2);
  64. INIT_LIST_HEAD(&dev->slist);
  65. init_MUTEX(&dev->mutex);
  66. init_completion(&dev->dev_exited);
  67. memcpy(&dev->dev, device, sizeof(struct device));
  68. snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
  69. "w1_bus_master%u", dev->id);
  70. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  71. dev->driver = driver;
  72. dev->groups = 1;
  73. dev->seq = 1;
  74. dev_init_netlink(dev);
  75. err = device_register(&dev->dev);
  76. if (err) {
  77. printk(KERN_ERR "Failed to register master device. err=%d\n", err);
  78. dev_fini_netlink(dev);
  79. memset(dev, 0, sizeof(struct w1_master));
  80. kfree(dev);
  81. dev = NULL;
  82. }
  83. return dev;
  84. }
  85. void w1_free_dev(struct w1_master *dev)
  86. {
  87. device_unregister(&dev->dev);
  88. }
  89. int w1_add_master_device(struct w1_bus_master *master)
  90. {
  91. struct w1_master *dev;
  92. int retval = 0;
  93. struct w1_netlink_msg msg;
  94. /* validate minimum functionality */
  95. if (!(master->touch_bit && master->reset_bus) &&
  96. !(master->write_bit && master->read_bit)) {
  97. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  98. return(-EINVAL);
  99. }
  100. dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
  101. if (!dev)
  102. return -ENOMEM;
  103. dev->kpid = kernel_thread(&w1_process, dev, 0);
  104. if (dev->kpid < 0) {
  105. dev_err(&dev->dev,
  106. "Failed to create new kernel thread. err=%d\n",
  107. dev->kpid);
  108. retval = dev->kpid;
  109. goto err_out_free_dev;
  110. }
  111. retval = w1_create_master_attributes(dev);
  112. if (retval)
  113. goto err_out_kill_thread;
  114. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  115. dev->initialized = 1;
  116. spin_lock(&w1_mlock);
  117. list_add(&dev->w1_master_entry, &w1_masters);
  118. spin_unlock(&w1_mlock);
  119. msg.id.mst.id = dev->id;
  120. msg.id.mst.pid = dev->kpid;
  121. msg.type = W1_MASTER_ADD;
  122. w1_netlink_send(dev, &msg);
  123. return 0;
  124. err_out_kill_thread:
  125. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  126. if (kill_proc(dev->kpid, SIGTERM, 1))
  127. dev_err(&dev->dev,
  128. "Failed to send signal to w1 kernel thread %d.\n",
  129. dev->kpid);
  130. wait_for_completion(&dev->dev_exited);
  131. err_out_free_dev:
  132. w1_free_dev(dev);
  133. return retval;
  134. }
  135. void __w1_remove_master_device(struct w1_master *dev)
  136. {
  137. int err;
  138. struct w1_netlink_msg msg;
  139. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  140. err = kill_proc(dev->kpid, SIGTERM, 1);
  141. if (err)
  142. dev_err(&dev->dev,
  143. "%s: Failed to send signal to w1 kernel thread %d.\n",
  144. __func__, dev->kpid);
  145. while (atomic_read(&dev->refcnt)) {
  146. dev_dbg(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  147. dev->name, atomic_read(&dev->refcnt));
  148. if (msleep_interruptible(1000))
  149. flush_signals(current);
  150. }
  151. msg.id.mst.id = dev->id;
  152. msg.id.mst.pid = dev->kpid;
  153. msg.type = W1_MASTER_REMOVE;
  154. w1_netlink_send(dev, &msg);
  155. w1_free_dev(dev);
  156. }
  157. void w1_remove_master_device(struct w1_bus_master *bm)
  158. {
  159. struct w1_master *dev = NULL;
  160. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  161. if (!dev->initialized)
  162. continue;
  163. if (dev->bus_master->data == bm->data)
  164. break;
  165. }
  166. if (!dev) {
  167. printk(KERN_ERR "Device doesn't exist.\n");
  168. return;
  169. }
  170. __w1_remove_master_device(dev);
  171. }
  172. EXPORT_SYMBOL(w1_add_master_device);
  173. EXPORT_SYMBOL(w1_remove_master_device);
  174. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_W1);