w1_int.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_internal.h"
  23. #include "w1_netlink.h"
  24. static int w1_search_count = -1; /* Default is continual scan */
  25. module_param_named(search_count, w1_search_count, int, 0);
  26. static int w1_enable_pullup = 1;
  27. module_param_named(enable_pullup, w1_enable_pullup, int, 0);
  28. static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
  29. struct device_driver *driver,
  30. struct device *device)
  31. {
  32. struct w1_master *dev;
  33. int err;
  34. /*
  35. * We are in process context(kernel thread), so can sleep.
  36. */
  37. dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
  38. if (!dev) {
  39. pr_err("Failed to allocate %zd bytes for new w1 device.\n",
  40. sizeof(struct w1_master));
  41. return NULL;
  42. }
  43. dev->bus_master = (struct w1_bus_master *)(dev + 1);
  44. dev->owner = THIS_MODULE;
  45. dev->max_slave_count = slave_count;
  46. dev->slave_count = 0;
  47. dev->attempts = 0;
  48. dev->initialized = 0;
  49. dev->id = id;
  50. dev->slave_ttl = slave_ttl;
  51. dev->search_count = w1_search_count;
  52. dev->enable_pullup = w1_enable_pullup;
  53. /* 1 for w1_process to decrement
  54. * 1 for __w1_remove_master_device to decrement
  55. */
  56. atomic_set(&dev->refcnt, 2);
  57. INIT_LIST_HEAD(&dev->slist);
  58. INIT_LIST_HEAD(&dev->async_list);
  59. mutex_init(&dev->mutex);
  60. mutex_init(&dev->bus_mutex);
  61. mutex_init(&dev->list_mutex);
  62. memcpy(&dev->dev, device, sizeof(struct device));
  63. dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
  64. snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
  65. dev->dev.init_name = dev->name;
  66. dev->driver = driver;
  67. dev->seq = 1;
  68. err = device_register(&dev->dev);
  69. if (err) {
  70. pr_err("Failed to register master device. err=%d\n", err);
  71. put_device(&dev->dev);
  72. dev = NULL;
  73. }
  74. return dev;
  75. }
  76. static void w1_free_dev(struct w1_master *dev)
  77. {
  78. device_unregister(&dev->dev);
  79. }
  80. /**
  81. * w1_add_master_device() - registers a new master device
  82. * @master: master bus device to register
  83. */
  84. int w1_add_master_device(struct w1_bus_master *master)
  85. {
  86. struct w1_master *dev, *entry;
  87. int retval = 0;
  88. struct w1_netlink_msg msg;
  89. int id, found;
  90. /* validate minimum functionality */
  91. if (!(master->touch_bit && master->reset_bus) &&
  92. !(master->write_bit && master->read_bit) &&
  93. !(master->write_byte && master->read_byte && master->reset_bus)) {
  94. pr_err("w1_add_master_device: invalid function set\n");
  95. return(-EINVAL);
  96. }
  97. /* Lock until the device is added (or not) to w1_masters. */
  98. mutex_lock(&w1_mlock);
  99. /* Search for the first available id (starting at 1). */
  100. id = 0;
  101. do {
  102. ++id;
  103. found = 0;
  104. list_for_each_entry(entry, &w1_masters, w1_master_entry) {
  105. if (entry->id == id) {
  106. found = 1;
  107. break;
  108. }
  109. }
  110. } while (found);
  111. dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
  112. &w1_master_driver, &w1_master_device);
  113. if (!dev) {
  114. mutex_unlock(&w1_mlock);
  115. return -ENOMEM;
  116. }
  117. retval = w1_create_master_attributes(dev);
  118. if (retval) {
  119. mutex_unlock(&w1_mlock);
  120. goto err_out_free_dev;
  121. }
  122. memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
  123. dev->initialized = 1;
  124. dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
  125. if (IS_ERR(dev->thread)) {
  126. retval = PTR_ERR(dev->thread);
  127. dev_err(&dev->dev,
  128. "Failed to create new kernel thread. err=%d\n",
  129. retval);
  130. mutex_unlock(&w1_mlock);
  131. goto err_out_rm_attr;
  132. }
  133. list_add(&dev->w1_master_entry, &w1_masters);
  134. mutex_unlock(&w1_mlock);
  135. memset(&msg, 0, sizeof(msg));
  136. msg.id.mst.id = dev->id;
  137. msg.type = W1_MASTER_ADD;
  138. w1_netlink_send(dev, &msg);
  139. return 0;
  140. #if 0 /* Thread cleanup code, not required currently. */
  141. err_out_kill_thread:
  142. set_bit(W1_ABORT_SEARCH, &dev->flags);
  143. kthread_stop(dev->thread);
  144. #endif
  145. err_out_rm_attr:
  146. w1_destroy_master_attributes(dev);
  147. err_out_free_dev:
  148. w1_free_dev(dev);
  149. return retval;
  150. }
  151. EXPORT_SYMBOL(w1_add_master_device);
  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_remove_master_device);