w1.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * w1.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/delay.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/list.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/timer.h>
  29. #include <linux/device.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include <asm/atomic.h>
  33. #include "w1.h"
  34. #include "w1_io.h"
  35. #include "w1_log.h"
  36. #include "w1_int.h"
  37. #include "w1_family.h"
  38. #include "w1_netlink.h"
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  41. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
  42. static int w1_timeout = 10;
  43. int w1_max_slave_count = 10;
  44. int w1_max_slave_ttl = 10;
  45. module_param_named(timeout, w1_timeout, int, 0);
  46. module_param_named(max_slave_count, w1_max_slave_count, int, 0);
  47. module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
  48. DEFINE_SPINLOCK(w1_mlock);
  49. LIST_HEAD(w1_masters);
  50. static pid_t control_thread;
  51. static int control_needs_exit;
  52. static DECLARE_COMPLETION(w1_control_complete);
  53. /* stuff for the default family */
  54. static ssize_t w1_famdefault_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  55. {
  56. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  57. return(sprintf(buf, "%s\n", sl->name));
  58. }
  59. static struct w1_family_ops w1_default_fops = {
  60. .rname = &w1_famdefault_read_name,
  61. };
  62. static struct w1_family w1_default_family = {
  63. .fops = &w1_default_fops,
  64. };
  65. static int w1_master_match(struct device *dev, struct device_driver *drv)
  66. {
  67. return 1;
  68. }
  69. static int w1_master_probe(struct device *dev)
  70. {
  71. return -ENODEV;
  72. }
  73. static int w1_master_remove(struct device *dev)
  74. {
  75. return 0;
  76. }
  77. static void w1_master_release(struct device *dev)
  78. {
  79. struct w1_master *md = container_of(dev, struct w1_master, dev);
  80. complete(&md->dev_released);
  81. }
  82. static void w1_slave_release(struct device *dev)
  83. {
  84. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  85. complete(&sl->dev_released);
  86. }
  87. static ssize_t w1_default_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  88. {
  89. return sprintf(buf, "No family registered.\n");
  90. }
  91. static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
  92. size_t count)
  93. {
  94. return sprintf(buf, "No family registered.\n");
  95. }
  96. static struct device_attribute w1_slave_attribute =
  97. __ATTR(name, S_IRUGO, w1_default_read_name, NULL);
  98. static struct bin_attribute w1_slave_bin_attribute = {
  99. .attr = {
  100. .name = "w1_slave",
  101. .mode = S_IRUGO,
  102. .owner = THIS_MODULE,
  103. },
  104. .size = W1_SLAVE_DATA_SIZE,
  105. .read = &w1_default_read_bin,
  106. };
  107. static struct bus_type w1_bus_type = {
  108. .name = "w1",
  109. .match = w1_master_match,
  110. };
  111. struct device_driver w1_driver = {
  112. .name = "w1_driver",
  113. .bus = &w1_bus_type,
  114. .probe = w1_master_probe,
  115. .remove = w1_master_remove,
  116. };
  117. struct device w1_device = {
  118. .parent = NULL,
  119. .bus = &w1_bus_type,
  120. .bus_id = "w1 bus master",
  121. .driver = &w1_driver,
  122. .release = &w1_master_release
  123. };
  124. static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  125. {
  126. struct w1_master *md = container_of(dev, struct w1_master, dev);
  127. ssize_t count;
  128. if (down_interruptible (&md->mutex))
  129. return -EBUSY;
  130. count = sprintf(buf, "%s\n", md->name);
  131. up(&md->mutex);
  132. return count;
  133. }
  134. static ssize_t w1_master_attribute_store_search(struct device * dev,
  135. struct device_attribute *attr,
  136. const char * buf, size_t count)
  137. {
  138. struct w1_master *md = container_of(dev, struct w1_master, dev);
  139. if (down_interruptible (&md->mutex))
  140. return -EBUSY;
  141. md->search_count = simple_strtol(buf, NULL, 0);
  142. up(&md->mutex);
  143. return count;
  144. }
  145. static ssize_t w1_master_attribute_show_search(struct device *dev,
  146. struct device_attribute *attr,
  147. char *buf)
  148. {
  149. struct w1_master *md = container_of(dev, struct w1_master, dev);
  150. ssize_t count;
  151. if (down_interruptible (&md->mutex))
  152. return -EBUSY;
  153. count = sprintf(buf, "%d\n", md->search_count);
  154. up(&md->mutex);
  155. return count;
  156. }
  157. static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
  158. {
  159. struct w1_master *md = container_of(dev, struct w1_master, dev);
  160. ssize_t count;
  161. if (down_interruptible(&md->mutex))
  162. return -EBUSY;
  163. count = sprintf(buf, "0x%p\n", md->bus_master);
  164. up(&md->mutex);
  165. return count;
  166. }
  167. static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  168. {
  169. ssize_t count;
  170. count = sprintf(buf, "%d\n", w1_timeout);
  171. return count;
  172. }
  173. static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  174. {
  175. struct w1_master *md = container_of(dev, struct w1_master, dev);
  176. ssize_t count;
  177. if (down_interruptible(&md->mutex))
  178. return -EBUSY;
  179. count = sprintf(buf, "%d\n", md->max_slave_count);
  180. up(&md->mutex);
  181. return count;
  182. }
  183. static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
  184. {
  185. struct w1_master *md = container_of(dev, struct w1_master, dev);
  186. ssize_t count;
  187. if (down_interruptible(&md->mutex))
  188. return -EBUSY;
  189. count = sprintf(buf, "%lu\n", md->attempts);
  190. up(&md->mutex);
  191. return count;
  192. }
  193. static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. struct w1_master *md = container_of(dev, struct w1_master, dev);
  196. ssize_t count;
  197. if (down_interruptible(&md->mutex))
  198. return -EBUSY;
  199. count = sprintf(buf, "%d\n", md->slave_count);
  200. up(&md->mutex);
  201. return count;
  202. }
  203. static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
  204. {
  205. struct w1_master *md = container_of(dev, struct w1_master, dev);
  206. int c = PAGE_SIZE;
  207. if (down_interruptible(&md->mutex))
  208. return -EBUSY;
  209. if (md->slave_count == 0)
  210. c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
  211. else {
  212. struct list_head *ent, *n;
  213. struct w1_slave *sl;
  214. list_for_each_safe(ent, n, &md->slist) {
  215. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  216. c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
  217. }
  218. }
  219. up(&md->mutex);
  220. return PAGE_SIZE - c;
  221. }
  222. #define W1_MASTER_ATTR_RO(_name, _mode) \
  223. struct device_attribute w1_master_attribute_##_name = \
  224. __ATTR(w1_master_##_name, _mode, \
  225. w1_master_attribute_show_##_name, NULL)
  226. #define W1_MASTER_ATTR_RW(_name, _mode) \
  227. struct device_attribute w1_master_attribute_##_name = \
  228. __ATTR(w1_master_##_name, _mode, \
  229. w1_master_attribute_show_##_name, \
  230. w1_master_attribute_store_##_name)
  231. static W1_MASTER_ATTR_RO(name, S_IRUGO);
  232. static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
  233. static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
  234. static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
  235. static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
  236. static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
  237. static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
  238. static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
  239. static struct attribute *w1_master_default_attrs[] = {
  240. &w1_master_attribute_name.attr,
  241. &w1_master_attribute_slaves.attr,
  242. &w1_master_attribute_slave_count.attr,
  243. &w1_master_attribute_max_slave_count.attr,
  244. &w1_master_attribute_attempts.attr,
  245. &w1_master_attribute_timeout.attr,
  246. &w1_master_attribute_pointer.attr,
  247. &w1_master_attribute_search.attr,
  248. NULL
  249. };
  250. static struct attribute_group w1_master_defattr_group = {
  251. .attrs = w1_master_default_attrs,
  252. };
  253. int w1_create_master_attributes(struct w1_master *master)
  254. {
  255. return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
  256. }
  257. void w1_destroy_master_attributes(struct w1_master *master)
  258. {
  259. sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
  260. }
  261. static int __w1_attach_slave_device(struct w1_slave *sl)
  262. {
  263. int err;
  264. sl->dev.parent = &sl->master->dev;
  265. sl->dev.driver = sl->master->driver;
  266. sl->dev.bus = &w1_bus_type;
  267. sl->dev.release = &w1_slave_release;
  268. snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
  269. "%02x-%012llx",
  270. (unsigned int) sl->reg_num.family,
  271. (unsigned long long) sl->reg_num.id);
  272. snprintf(&sl->name[0], sizeof(sl->name),
  273. "%02x-%012llx",
  274. (unsigned int) sl->reg_num.family,
  275. (unsigned long long) sl->reg_num.id);
  276. dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
  277. &sl->dev.bus_id[0]);
  278. err = device_register(&sl->dev);
  279. if (err < 0) {
  280. dev_err(&sl->dev,
  281. "Device registration [%s] failed. err=%d\n",
  282. sl->dev.bus_id, err);
  283. return err;
  284. }
  285. memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
  286. memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
  287. sl->attr_bin.read = sl->family->fops->rbin;
  288. sl->attr_name.show = sl->family->fops->rname;
  289. err = device_create_file(&sl->dev, &sl->attr_name);
  290. if (err < 0) {
  291. dev_err(&sl->dev,
  292. "sysfs file creation for [%s] failed. err=%d\n",
  293. sl->dev.bus_id, err);
  294. device_unregister(&sl->dev);
  295. return err;
  296. }
  297. if ( sl->attr_bin.read ) {
  298. err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
  299. if (err < 0) {
  300. dev_err(&sl->dev,
  301. "sysfs file creation for [%s] failed. err=%d\n",
  302. sl->dev.bus_id, err);
  303. device_remove_file(&sl->dev, &sl->attr_name);
  304. device_unregister(&sl->dev);
  305. return err;
  306. }
  307. }
  308. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  309. return 0;
  310. }
  311. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  312. {
  313. struct w1_slave *sl;
  314. struct w1_family *f;
  315. int err;
  316. struct w1_netlink_msg msg;
  317. sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
  318. if (!sl) {
  319. dev_err(&dev->dev,
  320. "%s: failed to allocate new slave device.\n",
  321. __func__);
  322. return -ENOMEM;
  323. }
  324. memset(sl, 0, sizeof(*sl));
  325. sl->owner = THIS_MODULE;
  326. sl->master = dev;
  327. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  328. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  329. atomic_set(&sl->refcnt, 0);
  330. init_completion(&sl->dev_released);
  331. spin_lock(&w1_flock);
  332. f = w1_family_registered(rn->family);
  333. if (!f) {
  334. f= &w1_default_family;
  335. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  336. rn->family, rn->family,
  337. (unsigned long long)rn->id, rn->crc);
  338. }
  339. __w1_family_get(f);
  340. spin_unlock(&w1_flock);
  341. sl->family = f;
  342. err = __w1_attach_slave_device(sl);
  343. if (err < 0) {
  344. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  345. sl->name);
  346. w1_family_put(sl->family);
  347. kfree(sl);
  348. return err;
  349. }
  350. sl->ttl = dev->slave_ttl;
  351. dev->slave_count++;
  352. memcpy(&msg.id.id, rn, sizeof(msg.id.id));
  353. msg.type = W1_SLAVE_ADD;
  354. w1_netlink_send(dev, &msg);
  355. return 0;
  356. }
  357. static void w1_slave_detach(struct w1_slave *sl)
  358. {
  359. struct w1_netlink_msg msg;
  360. dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
  361. while (atomic_read(&sl->refcnt)) {
  362. printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
  363. sl->name, atomic_read(&sl->refcnt));
  364. if (msleep_interruptible(1000))
  365. flush_signals(current);
  366. }
  367. if ( sl->attr_bin.read ) {
  368. sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
  369. }
  370. device_remove_file(&sl->dev, &sl->attr_name);
  371. device_unregister(&sl->dev);
  372. w1_family_put(sl->family);
  373. sl->master->slave_count--;
  374. memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
  375. msg.type = W1_SLAVE_REMOVE;
  376. w1_netlink_send(sl->master, &msg);
  377. }
  378. static struct w1_master *w1_search_master(unsigned long data)
  379. {
  380. struct w1_master *dev;
  381. int found = 0;
  382. spin_lock_bh(&w1_mlock);
  383. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  384. if (dev->bus_master->data == data) {
  385. found = 1;
  386. atomic_inc(&dev->refcnt);
  387. break;
  388. }
  389. }
  390. spin_unlock_bh(&w1_mlock);
  391. return (found)?dev:NULL;
  392. }
  393. void w1_reconnect_slaves(struct w1_family *f)
  394. {
  395. struct w1_master *dev;
  396. spin_lock_bh(&w1_mlock);
  397. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  398. dev_info(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
  399. dev->name, f->fid);
  400. set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
  401. }
  402. spin_unlock_bh(&w1_mlock);
  403. }
  404. static void w1_slave_found(unsigned long data, u64 rn)
  405. {
  406. int slave_count;
  407. struct w1_slave *sl;
  408. struct list_head *ent;
  409. struct w1_reg_num *tmp;
  410. int family_found = 0;
  411. struct w1_master *dev;
  412. u64 rn_le = cpu_to_le64(rn);
  413. dev = w1_search_master(data);
  414. if (!dev) {
  415. printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
  416. data);
  417. return;
  418. }
  419. tmp = (struct w1_reg_num *) &rn;
  420. slave_count = 0;
  421. list_for_each(ent, &dev->slist) {
  422. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  423. if (sl->reg_num.family == tmp->family &&
  424. sl->reg_num.id == tmp->id &&
  425. sl->reg_num.crc == tmp->crc) {
  426. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  427. break;
  428. } else if (sl->reg_num.family == tmp->family) {
  429. family_found = 1;
  430. break;
  431. }
  432. slave_count++;
  433. }
  434. if (slave_count == dev->slave_count &&
  435. rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
  436. w1_attach_slave_device(dev, tmp);
  437. }
  438. atomic_dec(&dev->refcnt);
  439. }
  440. /**
  441. * Performs a ROM Search & registers any devices found.
  442. * The 1-wire search is a simple binary tree search.
  443. * For each bit of the address, we read two bits and write one bit.
  444. * The bit written will put to sleep all devies that don't match that bit.
  445. * When the two reads differ, the direction choice is obvious.
  446. * When both bits are 0, we must choose a path to take.
  447. * When we can scan all 64 bits without having to choose a path, we are done.
  448. *
  449. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  450. *
  451. * @dev The master device to search
  452. * @cb Function to call when a device is found
  453. */
  454. void w1_search(struct w1_master *dev, w1_slave_found_callback cb)
  455. {
  456. u64 last_rn, rn, tmp64;
  457. int i, slave_count = 0;
  458. int last_zero, last_device;
  459. int search_bit, desc_bit;
  460. u8 triplet_ret = 0;
  461. search_bit = 0;
  462. rn = last_rn = 0;
  463. last_device = 0;
  464. last_zero = -1;
  465. desc_bit = 64;
  466. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  467. last_rn = rn;
  468. rn = 0;
  469. /*
  470. * Reset bus and all 1-wire device state machines
  471. * so they can respond to our requests.
  472. *
  473. * Return 0 - device(s) present, 1 - no devices present.
  474. */
  475. if (w1_reset_bus(dev)) {
  476. dev_info(&dev->dev, "No devices present on the wire.\n");
  477. break;
  478. }
  479. /* Start the search */
  480. w1_write_8(dev, W1_SEARCH);
  481. for (i = 0; i < 64; ++i) {
  482. /* Determine the direction/search bit */
  483. if (i == desc_bit)
  484. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  485. else if (i > desc_bit)
  486. search_bit = 0; /* take the 0 path on the next branch */
  487. else
  488. search_bit = ((last_rn >> i) & 0x1);
  489. /** Read two bits and write one bit */
  490. triplet_ret = w1_triplet(dev, search_bit);
  491. /* quit if no device responded */
  492. if ( (triplet_ret & 0x03) == 0x03 )
  493. break;
  494. /* If both directions were valid, and we took the 0 path... */
  495. if (triplet_ret == 0)
  496. last_zero = i;
  497. /* extract the direction taken & update the device number */
  498. tmp64 = (triplet_ret >> 2);
  499. rn |= (tmp64 << i);
  500. }
  501. if ( (triplet_ret & 0x03) != 0x03 ) {
  502. if ( (desc_bit == last_zero) || (last_zero < 0))
  503. last_device = 1;
  504. desc_bit = last_zero;
  505. cb(dev->bus_master->data, rn);
  506. }
  507. }
  508. }
  509. static int w1_control(void *data)
  510. {
  511. struct w1_slave *sl, *sln;
  512. struct w1_master *dev, *n;
  513. int err, have_to_wait = 0;
  514. daemonize("w1_control");
  515. allow_signal(SIGTERM);
  516. while (!control_needs_exit || have_to_wait) {
  517. have_to_wait = 0;
  518. try_to_freeze();
  519. msleep_interruptible(w1_timeout * 1000);
  520. if (signal_pending(current))
  521. flush_signals(current);
  522. list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
  523. if (!control_needs_exit && !dev->flags)
  524. continue;
  525. /*
  526. * Little race: we can create thread but not set the flag.
  527. * Get a chance for external process to set flag up.
  528. */
  529. if (!dev->initialized) {
  530. have_to_wait = 1;
  531. continue;
  532. }
  533. if (control_needs_exit) {
  534. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  535. err = kill_proc(dev->kpid, SIGTERM, 1);
  536. if (err)
  537. dev_err(&dev->dev,
  538. "Failed to send signal to w1 kernel thread %d.\n",
  539. dev->kpid);
  540. }
  541. if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
  542. wait_for_completion(&dev->dev_exited);
  543. spin_lock_bh(&w1_mlock);
  544. list_del(&dev->w1_master_entry);
  545. spin_unlock_bh(&w1_mlock);
  546. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  547. list_del(&sl->w1_slave_entry);
  548. w1_slave_detach(sl);
  549. kfree(sl);
  550. }
  551. w1_destroy_master_attributes(dev);
  552. atomic_dec(&dev->refcnt);
  553. continue;
  554. }
  555. if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
  556. dev_info(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
  557. down(&dev->mutex);
  558. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  559. if (sl->family->fid == W1_FAMILY_DEFAULT) {
  560. struct w1_reg_num rn;
  561. list_del(&sl->w1_slave_entry);
  562. w1_slave_detach(sl);
  563. memcpy(&rn, &sl->reg_num, sizeof(rn));
  564. kfree(sl);
  565. w1_attach_slave_device(dev, &rn);
  566. }
  567. }
  568. clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
  569. up(&dev->mutex);
  570. }
  571. }
  572. }
  573. complete_and_exit(&w1_control_complete, 0);
  574. }
  575. int w1_process(void *data)
  576. {
  577. struct w1_master *dev = (struct w1_master *) data;
  578. struct w1_slave *sl, *sln;
  579. daemonize("%s", dev->name);
  580. allow_signal(SIGTERM);
  581. while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
  582. try_to_freeze();
  583. msleep_interruptible(w1_timeout * 1000);
  584. if (signal_pending(current))
  585. flush_signals(current);
  586. if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
  587. break;
  588. if (!dev->initialized)
  589. continue;
  590. if (dev->search_count == 0)
  591. continue;
  592. if (down_interruptible(&dev->mutex))
  593. continue;
  594. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  595. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  596. w1_search_devices(dev, w1_slave_found);
  597. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  598. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
  599. list_del (&sl->w1_slave_entry);
  600. w1_slave_detach (sl);
  601. kfree (sl);
  602. dev->slave_count--;
  603. } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  604. sl->ttl = dev->slave_ttl;
  605. }
  606. if (dev->search_count > 0)
  607. dev->search_count--;
  608. up(&dev->mutex);
  609. }
  610. atomic_dec(&dev->refcnt);
  611. complete_and_exit(&dev->dev_exited, 0);
  612. return 0;
  613. }
  614. static int w1_init(void)
  615. {
  616. int retval;
  617. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  618. retval = bus_register(&w1_bus_type);
  619. if (retval) {
  620. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  621. goto err_out_exit_init;
  622. }
  623. retval = driver_register(&w1_driver);
  624. if (retval) {
  625. printk(KERN_ERR
  626. "Failed to register master driver. err=%d.\n",
  627. retval);
  628. goto err_out_bus_unregister;
  629. }
  630. control_thread = kernel_thread(&w1_control, NULL, 0);
  631. if (control_thread < 0) {
  632. printk(KERN_ERR "Failed to create control thread. err=%d\n",
  633. control_thread);
  634. retval = control_thread;
  635. goto err_out_driver_unregister;
  636. }
  637. return 0;
  638. err_out_driver_unregister:
  639. driver_unregister(&w1_driver);
  640. err_out_bus_unregister:
  641. bus_unregister(&w1_bus_type);
  642. err_out_exit_init:
  643. return retval;
  644. }
  645. static void w1_fini(void)
  646. {
  647. struct w1_master *dev;
  648. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  649. __w1_remove_master_device(dev);
  650. control_needs_exit = 1;
  651. wait_for_completion(&w1_control_complete);
  652. driver_unregister(&w1_driver);
  653. bus_unregister(&w1_bus_type);
  654. }
  655. module_init(w1_init);
  656. module_exit(w1_fini);