w1_int.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/delay.h>
  17. #include <linux/kthread.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/export.h>
  21. #include <linux/moduleparam.h>
  22. #include "w1.h"
  23. #include "w1_log.h"
  24. #include "w1_netlink.h"
  25. #include "w1_int.h"
  26. static int w1_search_count = -1; /* Default is continual scan */
  27. module_param_named(search_count, w1_search_count, int, 0);
  28. static int w1_enable_pullup = 1;
  29. module_param_named(enable_pullup, w1_enable_pullup, int, 0);
  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. pr_err("Failed to allocate %zd bytes for new w1 device.\n",
  42. sizeof(struct w1_master));
  43. return NULL;
  44. }
  45. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  46. dev->owner = THIS_MODULE;
  47. dev->max_slave_count = slave_count;
  48. dev->slave_count = 0;
  49. dev->attempts = 0;
  50. dev->initialized = 0;
  51. dev->id = id;
  52. dev->slave_ttl = slave_ttl;
  53. dev->search_count = w1_search_count;
  54. dev->enable_pullup = w1_enable_pullup;
  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. INIT_LIST_HEAD(&dev->async_list);
  61. mutex_init(&dev->mutex);
  62. mutex_init(&dev->bus_mutex);
  63. mutex_init(&dev->list_mutex);
  64. memcpy(&dev->dev, device, sizeof(struct device));
  65. dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
  66. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  67. dev->dev.init_name = dev->name;
  68. dev->driver = driver;
  69. dev->seq = 1;
  70. err = device_register(&dev->dev);
  71. if (err) {
  72. pr_err("Failed to register master device. err=%d\n", err);
  73. put_device(&dev->dev);
  74. dev = NULL;
  75. }
  76. return dev;
  77. }
  78. static void w1_free_dev(struct w1_master *dev)
  79. {
  80. device_unregister(&dev->dev);
  81. }
  82. /**
  83. * w1_add_master_device() - registers a new master device
  84. * @master: master bus device to register
  85. */
  86. int w1_add_master_device(struct w1_bus_master *master)
  87. {
  88. struct w1_master *dev, *entry;
  89. int retval = 0;
  90. struct w1_netlink_msg msg;
  91. int id, found;
  92. /* validate minimum functionality */
  93. if (!(master->touch_bit && master->reset_bus) &&
  94. !(master->write_bit && master->read_bit) &&
  95. !(master->write_byte && master->read_byte && master->reset_bus)) {
  96. pr_err("w1_add_master_device: invalid function set\n");
  97. return(-EINVAL);
  98. }
  99. /* Lock until the device is added (or not) to w1_masters. */
  100. mutex_lock(&w1_mlock);
  101. /* Search for the first available id (starting at 1). */
  102. id = 0;
  103. do {
  104. ++id;
  105. found = 0;
  106. list_for_each_entry(entry, &w1_masters, w1_master_entry) {
  107. if (entry->id == id) {
  108. found = 1;
  109. break;
  110. }
  111. }
  112. } while (found);
  113. dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
  114. &w1_master_driver, &w1_master_device);
  115. if (!dev) {
  116. mutex_unlock(&w1_mlock);
  117. return -ENOMEM;
  118. }
  119. retval = w1_create_master_attributes(dev);
  120. if (retval) {
  121. mutex_unlock(&w1_mlock);
  122. goto err_out_free_dev;
  123. }
  124. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  125. dev->initialized = 1;
  126. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  127. if (IS_ERR(dev->thread)) {
  128. retval = PTR_ERR(dev->thread);
  129. dev_err(&dev->dev,
  130. "Failed to create new kernel thread. err=%d\n",
  131. retval);
  132. mutex_unlock(&w1_mlock);
  133. goto err_out_rm_attr;
  134. }
  135. list_add(&dev->w1_master_entry, &w1_masters);
  136. mutex_unlock(&w1_mlock);
  137. memset(&msg, 0, sizeof(msg));
  138. msg.id.mst.id = dev->id;
  139. msg.type = W1_MASTER_ADD;
  140. w1_netlink_send(dev, &msg);
  141. return 0;
  142. #if 0 /* Thread cleanup code, not required currently. */
  143. err_out_kill_thread:
  144. set_bit(W1_ABORT_SEARCH, &dev->flags);
  145. kthread_stop(dev->thread);
  146. #endif
  147. err_out_rm_attr:
  148. w1_destroy_master_attributes(dev);
  149. err_out_free_dev:
  150. w1_free_dev(dev);
  151. return retval;
  152. }
  153. void __w1_remove_master_device(struct w1_master *dev)
  154. {
  155. struct w1_netlink_msg msg;
  156. struct w1_slave *sl, *sln;
  157. mutex_lock(&w1_mlock);
  158. list_del(&dev->w1_master_entry);
  159. mutex_unlock(&w1_mlock);
  160. set_bit(W1_ABORT_SEARCH, &dev->flags);
  161. kthread_stop(dev->thread);
  162. mutex_lock(&dev->mutex);
  163. mutex_lock(&dev->list_mutex);
  164. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  165. mutex_unlock(&dev->list_mutex);
  166. w1_slave_detach(sl);
  167. mutex_lock(&dev->list_mutex);
  168. }
  169. w1_destroy_master_attributes(dev);
  170. mutex_unlock(&dev->list_mutex);
  171. mutex_unlock(&dev->mutex);
  172. atomic_dec(&dev->refcnt);
  173. while (atomic_read(&dev->refcnt)) {
  174. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  175. dev->name, atomic_read(&dev->refcnt));
  176. if (msleep_interruptible(1000))
  177. flush_signals(current);
  178. mutex_lock(&dev->list_mutex);
  179. w1_process_callbacks(dev);
  180. mutex_unlock(&dev->list_mutex);
  181. }
  182. mutex_lock(&dev->list_mutex);
  183. w1_process_callbacks(dev);
  184. mutex_unlock(&dev->list_mutex);
  185. memset(&msg, 0, sizeof(msg));
  186. msg.id.mst.id = dev->id;
  187. msg.type = W1_MASTER_REMOVE;
  188. w1_netlink_send(dev, &msg);
  189. w1_free_dev(dev);
  190. }
  191. /**
  192. * w1_remove_master_device() - unregister a master device
  193. * @bm: master bus device to remove
  194. */
  195. void w1_remove_master_device(struct w1_bus_master *bm)
  196. {
  197. struct w1_master *dev, *found = NULL;
  198. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  199. if (!dev->initialized)
  200. continue;
  201. if (dev->bus_master->data == bm->data) {
  202. found = dev;
  203. break;
  204. }
  205. }
  206. if (!found) {
  207. pr_err("Device doesn't exist.\n");
  208. return;
  209. }
  210. __w1_remove_master_device(found);
  211. }
  212. EXPORT_SYMBOL(w1_add_master_device);
  213. EXPORT_SYMBOL(w1_remove_master_device);