w1_int.c 4.8 KB

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