w1_int.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 = kmalloc(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. memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
  47. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  48. dev->owner = THIS_MODULE;
  49. dev->max_slave_count = slave_count;
  50. dev->slave_count = 0;
  51. dev->attempts = 0;
  52. dev->initialized = 0;
  53. dev->id = id;
  54. dev->slave_ttl = slave_ttl;
  55. dev->search_count = -1; /* continual scan */
  56. atomic_set(&dev->refcnt, 2);
  57. INIT_LIST_HEAD(&dev->slist);
  58. mutex_init(&dev->mutex);
  59. memcpy(&dev->dev, device, sizeof(struct device));
  60. snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
  61. "w1_bus_master%u", dev->id);
  62. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  63. dev->driver = driver;
  64. dev->seq = 1;
  65. err = device_register(&dev->dev);
  66. if (err) {
  67. printk(KERN_ERR "Failed to register master device. err=%d\n", err);
  68. memset(dev, 0, sizeof(struct w1_master));
  69. kfree(dev);
  70. dev = NULL;
  71. }
  72. return dev;
  73. }
  74. static void w1_free_dev(struct w1_master *dev)
  75. {
  76. device_unregister(&dev->dev);
  77. }
  78. int w1_add_master_device(struct w1_bus_master *master)
  79. {
  80. struct w1_master *dev;
  81. int retval = 0;
  82. struct w1_netlink_msg msg;
  83. /* validate minimum functionality */
  84. if (!(master->touch_bit && master->reset_bus) &&
  85. !(master->write_bit && master->read_bit)) {
  86. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  87. return(-EINVAL);
  88. }
  89. dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
  90. if (!dev)
  91. return -ENOMEM;
  92. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  93. if (IS_ERR(dev->thread)) {
  94. retval = PTR_ERR(dev->thread);
  95. dev_err(&dev->dev,
  96. "Failed to create new kernel thread. err=%d\n",
  97. retval);
  98. goto err_out_free_dev;
  99. }
  100. retval = w1_create_master_attributes(dev);
  101. if (retval)
  102. goto err_out_kill_thread;
  103. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  104. dev->initialized = 1;
  105. mutex_lock(&w1_mlock);
  106. list_add(&dev->w1_master_entry, &w1_masters);
  107. mutex_unlock(&w1_mlock);
  108. memset(&msg, 0, sizeof(msg));
  109. msg.id.mst.id = dev->id;
  110. msg.type = W1_MASTER_ADD;
  111. w1_netlink_send(dev, &msg);
  112. return 0;
  113. err_out_kill_thread:
  114. kthread_stop(dev->thread);
  115. err_out_free_dev:
  116. w1_free_dev(dev);
  117. return retval;
  118. }
  119. void __w1_remove_master_device(struct w1_master *dev)
  120. {
  121. struct w1_netlink_msg msg;
  122. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  123. kthread_stop(dev->thread);
  124. while (atomic_read(&dev->refcnt)) {
  125. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  126. dev->name, atomic_read(&dev->refcnt));
  127. if (msleep_interruptible(1000))
  128. flush_signals(current);
  129. }
  130. memset(&msg, 0, sizeof(msg));
  131. msg.id.mst.id = dev->id;
  132. msg.type = W1_MASTER_REMOVE;
  133. w1_netlink_send(dev, &msg);
  134. w1_free_dev(dev);
  135. }
  136. void w1_remove_master_device(struct w1_bus_master *bm)
  137. {
  138. struct w1_master *dev = NULL;
  139. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  140. if (!dev->initialized)
  141. continue;
  142. if (dev->bus_master->data == bm->data)
  143. break;
  144. }
  145. if (!dev) {
  146. printk(KERN_ERR "Device doesn't exist.\n");
  147. return;
  148. }
  149. __w1_remove_master_device(dev);
  150. }
  151. EXPORT_SYMBOL(w1_add_master_device);
  152. EXPORT_SYMBOL(w1_remove_master_device);