w1_int.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_netlink.h"
  24. #include "w1_int.h"
  25. static int w1_search_count = -1; /* Default is continual scan */
  26. module_param_named(search_count, w1_search_count, int, 0);
  27. static int w1_enable_pullup = 1;
  28. module_param_named(enable_pullup, w1_enable_pullup, int, 0);
  29. static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
  30. struct device_driver *driver,
  31. struct device *device)
  32. {
  33. struct w1_master *dev;
  34. int err;
  35. /*
  36. * We are in process context(kernel thread), so can sleep.
  37. */
  38. dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
  39. if (!dev) {
  40. pr_err("Failed to allocate %zd bytes for new w1 device.\n",
  41. sizeof(struct w1_master));
  42. return NULL;
  43. }
  44. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  45. dev->owner = THIS_MODULE;
  46. dev->max_slave_count = slave_count;
  47. dev->slave_count = 0;
  48. dev->attempts = 0;
  49. dev->initialized = 0;
  50. dev->id = id;
  51. dev->slave_ttl = slave_ttl;
  52. dev->search_count = w1_search_count;
  53. dev->enable_pullup = w1_enable_pullup;
  54. /* 1 for w1_process to decrement
  55. * 1 for __w1_remove_master_device to decrement
  56. */
  57. atomic_set(&dev->refcnt, 2);
  58. INIT_LIST_HEAD(&dev->slist);
  59. INIT_LIST_HEAD(&dev->async_list);
  60. mutex_init(&dev->mutex);
  61. mutex_init(&dev->bus_mutex);
  62. mutex_init(&dev->list_mutex);
  63. memcpy(&dev->dev, device, sizeof(struct device));
  64. dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
  65. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  66. dev->dev.init_name = dev->name;
  67. dev->driver = driver;
  68. dev->seq = 1;
  69. err = device_register(&dev->dev);
  70. if (err) {
  71. pr_err("Failed to register master device. err=%d\n", err);
  72. put_device(&dev->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. /**
  82. * w1_add_master_device() - registers a new master device
  83. * @master: master bus device to register
  84. */
  85. int w1_add_master_device(struct w1_bus_master *master)
  86. {
  87. struct w1_master *dev, *entry;
  88. int retval = 0;
  89. struct w1_netlink_msg msg;
  90. int id, found;
  91. /* validate minimum functionality */
  92. if (!(master->touch_bit && master->reset_bus) &&
  93. !(master->write_bit && master->read_bit) &&
  94. !(master->write_byte && master->read_byte && master->reset_bus)) {
  95. pr_err("w1_add_master_device: invalid function set\n");
  96. return(-EINVAL);
  97. }
  98. /* Lock until the device is added (or not) to w1_masters. */
  99. mutex_lock(&w1_mlock);
  100. /* Search for the first available id (starting at 1). */
  101. id = 0;
  102. do {
  103. ++id;
  104. found = 0;
  105. list_for_each_entry(entry, &w1_masters, w1_master_entry) {
  106. if (entry->id == id) {
  107. found = 1;
  108. break;
  109. }
  110. }
  111. } while (found);
  112. dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
  113. &w1_master_driver, &w1_master_device);
  114. if (!dev) {
  115. mutex_unlock(&w1_mlock);
  116. return -ENOMEM;
  117. }
  118. retval = w1_create_master_attributes(dev);
  119. if (retval) {
  120. mutex_unlock(&w1_mlock);
  121. goto err_out_free_dev;
  122. }
  123. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  124. dev->initialized = 1;
  125. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  126. if (IS_ERR(dev->thread)) {
  127. retval = PTR_ERR(dev->thread);
  128. dev_err(&dev->dev,
  129. "Failed to create new kernel thread. err=%d\n",
  130. retval);
  131. mutex_unlock(&w1_mlock);
  132. goto err_out_rm_attr;
  133. }
  134. list_add(&dev->w1_master_entry, &w1_masters);
  135. mutex_unlock(&w1_mlock);
  136. memset(&msg, 0, sizeof(msg));
  137. msg.id.mst.id = dev->id;
  138. msg.type = W1_MASTER_ADD;
  139. w1_netlink_send(dev, &msg);
  140. return 0;
  141. #if 0 /* Thread cleanup code, not required currently. */
  142. err_out_kill_thread:
  143. set_bit(W1_ABORT_SEARCH, &dev->flags);
  144. kthread_stop(dev->thread);
  145. #endif
  146. err_out_rm_attr:
  147. w1_destroy_master_attributes(dev);
  148. err_out_free_dev:
  149. w1_free_dev(dev);
  150. return retval;
  151. }
  152. void __w1_remove_master_device(struct w1_master *dev)
  153. {
  154. struct w1_netlink_msg msg;
  155. struct w1_slave *sl, *sln;
  156. mutex_lock(&w1_mlock);
  157. list_del(&dev->w1_master_entry);
  158. mutex_unlock(&w1_mlock);
  159. set_bit(W1_ABORT_SEARCH, &dev->flags);
  160. kthread_stop(dev->thread);
  161. mutex_lock(&dev->mutex);
  162. mutex_lock(&dev->list_mutex);
  163. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  164. mutex_unlock(&dev->list_mutex);
  165. w1_slave_detach(sl);
  166. mutex_lock(&dev->list_mutex);
  167. }
  168. w1_destroy_master_attributes(dev);
  169. mutex_unlock(&dev->list_mutex);
  170. mutex_unlock(&dev->mutex);
  171. atomic_dec(&dev->refcnt);
  172. while (atomic_read(&dev->refcnt)) {
  173. dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
  174. dev->name, atomic_read(&dev->refcnt));
  175. if (msleep_interruptible(1000))
  176. flush_signals(current);
  177. mutex_lock(&dev->list_mutex);
  178. w1_process_callbacks(dev);
  179. mutex_unlock(&dev->list_mutex);
  180. }
  181. mutex_lock(&dev->list_mutex);
  182. w1_process_callbacks(dev);
  183. mutex_unlock(&dev->list_mutex);
  184. memset(&msg, 0, sizeof(msg));
  185. msg.id.mst.id = dev->id;
  186. msg.type = W1_MASTER_REMOVE;
  187. w1_netlink_send(dev, &msg);
  188. w1_free_dev(dev);
  189. }
  190. /**
  191. * w1_remove_master_device() - unregister a master device
  192. * @bm: master bus device to remove
  193. */
  194. void w1_remove_master_device(struct w1_bus_master *bm)
  195. {
  196. struct w1_master *dev, *found = NULL;
  197. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  198. if (!dev->initialized)
  199. continue;
  200. if (dev->bus_master->data == bm->data) {
  201. found = dev;
  202. break;
  203. }
  204. }
  205. if (!found) {
  206. pr_err("Device doesn't exist.\n");
  207. return;
  208. }
  209. __w1_remove_master_device(found);
  210. }
  211. EXPORT_SYMBOL(w1_add_master_device);
  212. EXPORT_SYMBOL(w1_remove_master_device);