w1_int.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. init_MUTEX(&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->groups = 1;
  65. dev->seq = 1;
  66. dev_init_netlink(dev);
  67. err = device_register(&dev->dev);
  68. if (err) {
  69. printk(KERN_ERR "Failed to register master device. err=%d\n", err);
  70. dev_fini_netlink(dev);
  71. memset(dev, 0, sizeof(struct w1_master));
  72. kfree(dev);
  73. dev = NULL;
  74. }
  75. return dev;
  76. }
  77. static void w1_free_dev(struct w1_master *dev)
  78. {
  79. device_unregister(&dev->dev);
  80. }
  81. int w1_add_master_device(struct w1_bus_master *master)
  82. {
  83. struct w1_master *dev;
  84. int retval = 0;
  85. struct w1_netlink_msg msg;
  86. /* validate minimum functionality */
  87. if (!(master->touch_bit && master->reset_bus) &&
  88. !(master->write_bit && master->read_bit)) {
  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. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  96. if (IS_ERR(dev->thread)) {
  97. retval = PTR_ERR(dev->thread);
  98. dev_err(&dev->dev,
  99. "Failed to create new kernel thread. err=%d\n",
  100. retval);
  101. goto err_out_free_dev;
  102. }
  103. retval = w1_create_master_attributes(dev);
  104. if (retval)
  105. goto err_out_kill_thread;
  106. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  107. dev->initialized = 1;
  108. spin_lock(&w1_mlock);
  109. list_add(&dev->w1_master_entry, &w1_masters);
  110. spin_unlock(&w1_mlock);
  111. msg.id.mst.id = dev->id;
  112. msg.id.mst.pid = dev->thread->pid;
  113. msg.type = W1_MASTER_ADD;
  114. w1_netlink_send(dev, &msg);
  115. return 0;
  116. err_out_kill_thread:
  117. kthread_stop(dev->thread);
  118. err_out_free_dev:
  119. w1_free_dev(dev);
  120. return retval;
  121. }
  122. void __w1_remove_master_device(struct w1_master *dev)
  123. {
  124. struct w1_netlink_msg msg;
  125. pid_t pid = dev->thread->pid;
  126. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  127. kthread_stop(dev->thread);
  128. while (atomic_read(&dev->refcnt)) {
  129. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  130. dev->name, atomic_read(&dev->refcnt));
  131. if (msleep_interruptible(1000))
  132. flush_signals(current);
  133. }
  134. msg.id.mst.id = dev->id;
  135. msg.id.mst.pid = pid;
  136. msg.type = W1_MASTER_REMOVE;
  137. w1_netlink_send(dev, &msg);
  138. w1_free_dev(dev);
  139. }
  140. void w1_remove_master_device(struct w1_bus_master *bm)
  141. {
  142. struct w1_master *dev = NULL;
  143. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  144. if (!dev->initialized)
  145. continue;
  146. if (dev->bus_master->data == bm->data)
  147. break;
  148. }
  149. if (!dev) {
  150. printk(KERN_ERR "Device doesn't exist.\n");
  151. return;
  152. }
  153. __w1_remove_master_device(dev);
  154. }
  155. EXPORT_SYMBOL(w1_add_master_device);
  156. EXPORT_SYMBOL(w1_remove_master_device);