w1.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. static int w1_master_match(struct device *dev, struct device_driver *drv)
  54. {
  55. return 1;
  56. }
  57. static int w1_master_probe(struct device *dev)
  58. {
  59. return -ENODEV;
  60. }
  61. static int w1_master_remove(struct device *dev)
  62. {
  63. return 0;
  64. }
  65. static void w1_master_release(struct device *dev)
  66. {
  67. struct w1_master *md = container_of(dev, struct w1_master, dev);
  68. complete(&md->dev_released);
  69. }
  70. static void w1_slave_release(struct device *dev)
  71. {
  72. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  73. complete(&sl->dev_released);
  74. }
  75. static ssize_t w1_default_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  76. {
  77. return sprintf(buf, "No family registered.\n");
  78. }
  79. static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
  80. size_t count)
  81. {
  82. return sprintf(buf, "No family registered.\n");
  83. }
  84. static struct device_attribute w1_slave_attribute =
  85. __ATTR(name, S_IRUGO, w1_default_read_name, NULL);
  86. static struct device_attribute w1_slave_attribute_val =
  87. __ATTR(value, S_IRUGO, w1_default_read_name, NULL);
  88. static struct bin_attribute w1_slave_bin_attribute = {
  89. .attr = {
  90. .name = "w1_slave",
  91. .mode = S_IRUGO,
  92. .owner = THIS_MODULE,
  93. },
  94. .size = W1_SLAVE_DATA_SIZE,
  95. .read = &w1_default_read_bin,
  96. };
  97. static struct bus_type w1_bus_type = {
  98. .name = "w1",
  99. .match = w1_master_match,
  100. };
  101. struct device_driver w1_driver = {
  102. .name = "w1_driver",
  103. .bus = &w1_bus_type,
  104. .probe = w1_master_probe,
  105. .remove = w1_master_remove,
  106. };
  107. struct device w1_device = {
  108. .parent = NULL,
  109. .bus = &w1_bus_type,
  110. .bus_id = "w1 bus master",
  111. .driver = &w1_driver,
  112. .release = &w1_master_release
  113. };
  114. static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  115. {
  116. struct w1_master *md = container_of (dev, struct w1_master, dev);
  117. ssize_t count;
  118. if (down_interruptible (&md->mutex))
  119. return -EBUSY;
  120. count = sprintf(buf, "%s\n", md->name);
  121. up(&md->mutex);
  122. return count;
  123. }
  124. static ssize_t w1_master_attribute_show_pointer(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, "0x%p\n", md->bus_master);
  131. up(&md->mutex);
  132. return count;
  133. }
  134. static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  135. {
  136. ssize_t count;
  137. count = sprintf(buf, "%d\n", w1_timeout);
  138. return count;
  139. }
  140. static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  141. {
  142. struct w1_master *md = container_of(dev, struct w1_master, dev);
  143. ssize_t count;
  144. if (down_interruptible(&md->mutex))
  145. return -EBUSY;
  146. count = sprintf(buf, "%d\n", md->max_slave_count);
  147. up(&md->mutex);
  148. return count;
  149. }
  150. static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
  151. {
  152. struct w1_master *md = container_of(dev, struct w1_master, dev);
  153. ssize_t count;
  154. if (down_interruptible(&md->mutex))
  155. return -EBUSY;
  156. count = sprintf(buf, "%lu\n", md->attempts);
  157. up(&md->mutex);
  158. return count;
  159. }
  160. static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  161. {
  162. struct w1_master *md = container_of(dev, struct w1_master, dev);
  163. ssize_t count;
  164. if (down_interruptible(&md->mutex))
  165. return -EBUSY;
  166. count = sprintf(buf, "%d\n", md->slave_count);
  167. up(&md->mutex);
  168. return count;
  169. }
  170. static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
  171. {
  172. struct w1_master *md = container_of(dev, struct w1_master, dev);
  173. int c = PAGE_SIZE;
  174. if (down_interruptible(&md->mutex))
  175. return -EBUSY;
  176. if (md->slave_count == 0)
  177. c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
  178. else {
  179. struct list_head *ent, *n;
  180. struct w1_slave *sl;
  181. list_for_each_safe(ent, n, &md->slist) {
  182. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  183. c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
  184. }
  185. }
  186. up(&md->mutex);
  187. return PAGE_SIZE - c;
  188. }
  189. #define W1_MASTER_ATTR_RO(_name, _mode) \
  190. struct device_attribute w1_master_attribute_##_name = \
  191. __ATTR(w1_master_##_name, _mode, \
  192. w1_master_attribute_show_##_name, NULL)
  193. static W1_MASTER_ATTR_RO(name, S_IRUGO);
  194. static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
  195. static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
  196. static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
  197. static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
  198. static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
  199. static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
  200. static struct attribute *w1_master_default_attrs[] = {
  201. &w1_master_attribute_name.attr,
  202. &w1_master_attribute_slaves.attr,
  203. &w1_master_attribute_slave_count.attr,
  204. &w1_master_attribute_max_slave_count.attr,
  205. &w1_master_attribute_attempts.attr,
  206. &w1_master_attribute_timeout.attr,
  207. &w1_master_attribute_pointer.attr,
  208. NULL
  209. };
  210. static struct attribute_group w1_master_defattr_group = {
  211. .attrs = w1_master_default_attrs,
  212. };
  213. int w1_create_master_attributes(struct w1_master *master)
  214. {
  215. return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
  216. }
  217. void w1_destroy_master_attributes(struct w1_master *master)
  218. {
  219. sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
  220. }
  221. static int __w1_attach_slave_device(struct w1_slave *sl)
  222. {
  223. int err;
  224. sl->dev.parent = &sl->master->dev;
  225. sl->dev.driver = sl->master->driver;
  226. sl->dev.bus = &w1_bus_type;
  227. sl->dev.release = &w1_slave_release;
  228. snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
  229. "%02x-%012llx",
  230. (unsigned int) sl->reg_num.family,
  231. (unsigned long long) sl->reg_num.id);
  232. snprintf (&sl->name[0], sizeof(sl->name),
  233. "%02x-%012llx",
  234. (unsigned int) sl->reg_num.family,
  235. (unsigned long long) sl->reg_num.id);
  236. dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
  237. &sl->dev.bus_id[0]);
  238. err = device_register(&sl->dev);
  239. if (err < 0) {
  240. dev_err(&sl->dev,
  241. "Device registration [%s] failed. err=%d\n",
  242. sl->dev.bus_id, err);
  243. return err;
  244. }
  245. memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
  246. memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
  247. memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));
  248. sl->attr_bin.read = sl->family->fops->rbin;
  249. sl->attr_name.show = sl->family->fops->rname;
  250. sl->attr_val.show = sl->family->fops->rval;
  251. sl->attr_val.attr.name = sl->family->fops->rvalname;
  252. err = device_create_file(&sl->dev, &sl->attr_name);
  253. if (err < 0) {
  254. dev_err(&sl->dev,
  255. "sysfs file creation for [%s] failed. err=%d\n",
  256. sl->dev.bus_id, err);
  257. device_unregister(&sl->dev);
  258. return err;
  259. }
  260. err = device_create_file(&sl->dev, &sl->attr_val);
  261. if (err < 0) {
  262. dev_err(&sl->dev,
  263. "sysfs file creation for [%s] failed. err=%d\n",
  264. sl->dev.bus_id, err);
  265. device_remove_file(&sl->dev, &sl->attr_name);
  266. device_unregister(&sl->dev);
  267. return err;
  268. }
  269. err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
  270. if (err < 0) {
  271. dev_err(&sl->dev,
  272. "sysfs file creation for [%s] failed. err=%d\n",
  273. sl->dev.bus_id, err);
  274. device_remove_file(&sl->dev, &sl->attr_name);
  275. device_remove_file(&sl->dev, &sl->attr_val);
  276. device_unregister(&sl->dev);
  277. return err;
  278. }
  279. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  280. return 0;
  281. }
  282. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  283. {
  284. struct w1_slave *sl;
  285. struct w1_family *f;
  286. int err;
  287. struct w1_netlink_msg msg;
  288. sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
  289. if (!sl) {
  290. dev_err(&dev->dev,
  291. "%s: failed to allocate new slave device.\n",
  292. __func__);
  293. return -ENOMEM;
  294. }
  295. memset(sl, 0, sizeof(*sl));
  296. sl->owner = THIS_MODULE;
  297. sl->master = dev;
  298. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  299. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  300. atomic_set(&sl->refcnt, 0);
  301. init_completion(&sl->dev_released);
  302. spin_lock(&w1_flock);
  303. f = w1_family_registered(rn->family);
  304. if (!f) {
  305. spin_unlock(&w1_flock);
  306. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  307. rn->family, rn->family,
  308. (unsigned long long)rn->id, rn->crc);
  309. kfree(sl);
  310. return -ENODEV;
  311. }
  312. __w1_family_get(f);
  313. spin_unlock(&w1_flock);
  314. sl->family = f;
  315. err = __w1_attach_slave_device(sl);
  316. if (err < 0) {
  317. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  318. sl->name);
  319. w1_family_put(sl->family);
  320. kfree(sl);
  321. return err;
  322. }
  323. sl->ttl = dev->slave_ttl;
  324. dev->slave_count++;
  325. memcpy(&msg.id.id, rn, sizeof(msg.id.id));
  326. msg.type = W1_SLAVE_ADD;
  327. w1_netlink_send(dev, &msg);
  328. return 0;
  329. }
  330. static void w1_slave_detach(struct w1_slave *sl)
  331. {
  332. struct w1_netlink_msg msg;
  333. dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
  334. while (atomic_read(&sl->refcnt)) {
  335. printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
  336. sl->name, atomic_read(&sl->refcnt));
  337. if (msleep_interruptible(1000))
  338. flush_signals(current);
  339. }
  340. sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
  341. device_remove_file(&sl->dev, &sl->attr_name);
  342. device_remove_file(&sl->dev, &sl->attr_val);
  343. device_unregister(&sl->dev);
  344. w1_family_put(sl->family);
  345. memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
  346. msg.type = W1_SLAVE_REMOVE;
  347. w1_netlink_send(sl->master, &msg);
  348. }
  349. static struct w1_master *w1_search_master(unsigned long data)
  350. {
  351. struct w1_master *dev;
  352. int found = 0;
  353. spin_lock_bh(&w1_mlock);
  354. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  355. if (dev->bus_master->data == data) {
  356. found = 1;
  357. atomic_inc(&dev->refcnt);
  358. break;
  359. }
  360. }
  361. spin_unlock_bh(&w1_mlock);
  362. return (found)?dev:NULL;
  363. }
  364. static void w1_slave_found(unsigned long data, u64 rn)
  365. {
  366. int slave_count;
  367. struct w1_slave *sl;
  368. struct list_head *ent;
  369. struct w1_reg_num *tmp;
  370. int family_found = 0;
  371. struct w1_master *dev;
  372. dev = w1_search_master(data);
  373. if (!dev) {
  374. printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
  375. data);
  376. return;
  377. }
  378. tmp = (struct w1_reg_num *) &rn;
  379. slave_count = 0;
  380. list_for_each(ent, &dev->slist) {
  381. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  382. if (sl->reg_num.family == tmp->family &&
  383. sl->reg_num.id == tmp->id &&
  384. sl->reg_num.crc == tmp->crc) {
  385. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  386. break;
  387. } else if (sl->reg_num.family == tmp->family) {
  388. family_found = 1;
  389. break;
  390. }
  391. slave_count++;
  392. }
  393. rn = cpu_to_le64(rn);
  394. if (slave_count == dev->slave_count &&
  395. rn && ((le64_to_cpu(rn) >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) {
  396. w1_attach_slave_device(dev, tmp);
  397. }
  398. atomic_dec(&dev->refcnt);
  399. }
  400. void w1_search(struct w1_master *dev)
  401. {
  402. u64 last, rn, tmp;
  403. int i, count = 0;
  404. int last_family_desc, last_zero, last_device;
  405. int search_bit, id_bit, comp_bit, desc_bit;
  406. search_bit = id_bit = comp_bit = 0;
  407. rn = tmp = last = 0;
  408. last_device = last_zero = last_family_desc = 0;
  409. desc_bit = 64;
  410. while (!(id_bit && comp_bit) && !last_device &&
  411. count++ < dev->max_slave_count) {
  412. last = rn;
  413. rn = 0;
  414. last_family_desc = 0;
  415. /*
  416. * Reset bus and all 1-wire device state machines
  417. * so they can respond to our requests.
  418. *
  419. * Return 0 - device(s) present, 1 - no devices present.
  420. */
  421. if (w1_reset_bus(dev)) {
  422. dev_info(&dev->dev, "No devices present on the wire.\n");
  423. break;
  424. }
  425. #if 1
  426. w1_write_8(dev, W1_SEARCH);
  427. for (i = 0; i < 64; ++i) {
  428. /*
  429. * Read 2 bits from bus.
  430. * All who don't sleep must send ID bit and COMPLEMENT ID bit.
  431. * They actually are ANDed between all senders.
  432. */
  433. id_bit = w1_touch_bit(dev, 1);
  434. comp_bit = w1_touch_bit(dev, 1);
  435. if (id_bit && comp_bit)
  436. break;
  437. if (id_bit == 0 && comp_bit == 0) {
  438. if (i == desc_bit)
  439. search_bit = 1;
  440. else if (i > desc_bit)
  441. search_bit = 0;
  442. else
  443. search_bit = ((last >> i) & 0x1);
  444. if (search_bit == 0) {
  445. last_zero = i;
  446. if (last_zero < 9)
  447. last_family_desc = last_zero;
  448. }
  449. } else
  450. search_bit = id_bit;
  451. tmp = search_bit;
  452. rn |= (tmp << i);
  453. /*
  454. * Write 1 bit to bus
  455. * and make all who don't have "search_bit" in "i"'th position
  456. * in it's registration number sleep.
  457. */
  458. if (dev->bus_master->touch_bit)
  459. w1_touch_bit(dev, search_bit);
  460. else
  461. w1_write_bit(dev, search_bit);
  462. }
  463. #endif
  464. if (desc_bit == last_zero)
  465. last_device = 1;
  466. desc_bit = last_zero;
  467. w1_slave_found(dev->bus_master->data, rn);
  468. }
  469. }
  470. static int w1_control(void *data)
  471. {
  472. struct w1_slave *sl, *sln;
  473. struct w1_master *dev, *n;
  474. int err, have_to_wait = 0;
  475. daemonize("w1_control");
  476. allow_signal(SIGTERM);
  477. while (!control_needs_exit || have_to_wait) {
  478. have_to_wait = 0;
  479. try_to_freeze(PF_FREEZE);
  480. msleep_interruptible(w1_timeout * 1000);
  481. if (signal_pending(current))
  482. flush_signals(current);
  483. list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
  484. if (!control_needs_exit && !dev->need_exit)
  485. continue;
  486. /*
  487. * Little race: we can create thread but not set the flag.
  488. * Get a chance for external process to set flag up.
  489. */
  490. if (!dev->initialized) {
  491. have_to_wait = 1;
  492. continue;
  493. }
  494. spin_lock_bh(&w1_mlock);
  495. list_del(&dev->w1_master_entry);
  496. spin_unlock_bh(&w1_mlock);
  497. if (control_needs_exit) {
  498. dev->need_exit = 1;
  499. err = kill_proc(dev->kpid, SIGTERM, 1);
  500. if (err)
  501. dev_err(&dev->dev,
  502. "Failed to send signal to w1 kernel thread %d.\n",
  503. dev->kpid);
  504. }
  505. wait_for_completion(&dev->dev_exited);
  506. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  507. list_del(&sl->w1_slave_entry);
  508. w1_slave_detach(sl);
  509. kfree(sl);
  510. }
  511. w1_destroy_master_attributes(dev);
  512. atomic_dec(&dev->refcnt);
  513. }
  514. }
  515. complete_and_exit(&w1_control_complete, 0);
  516. }
  517. int w1_process(void *data)
  518. {
  519. struct w1_master *dev = (struct w1_master *) data;
  520. struct w1_slave *sl, *sln;
  521. daemonize("%s", dev->name);
  522. allow_signal(SIGTERM);
  523. while (!dev->need_exit) {
  524. try_to_freeze(PF_FREEZE);
  525. msleep_interruptible(w1_timeout * 1000);
  526. if (signal_pending(current))
  527. flush_signals(current);
  528. if (dev->need_exit)
  529. break;
  530. if (!dev->initialized)
  531. continue;
  532. if (down_interruptible(&dev->mutex))
  533. continue;
  534. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  535. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  536. w1_search_devices(dev, w1_slave_found);
  537. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  538. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
  539. list_del (&sl->w1_slave_entry);
  540. w1_slave_detach (sl);
  541. kfree (sl);
  542. dev->slave_count--;
  543. } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  544. sl->ttl = dev->slave_ttl;
  545. }
  546. up(&dev->mutex);
  547. }
  548. atomic_dec(&dev->refcnt);
  549. complete_and_exit(&dev->dev_exited, 0);
  550. return 0;
  551. }
  552. static int w1_init(void)
  553. {
  554. int retval;
  555. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  556. retval = bus_register(&w1_bus_type);
  557. if (retval) {
  558. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  559. goto err_out_exit_init;
  560. }
  561. retval = driver_register(&w1_driver);
  562. if (retval) {
  563. printk(KERN_ERR
  564. "Failed to register master driver. err=%d.\n",
  565. retval);
  566. goto err_out_bus_unregister;
  567. }
  568. control_thread = kernel_thread(&w1_control, NULL, 0);
  569. if (control_thread < 0) {
  570. printk(KERN_ERR "Failed to create control thread. err=%d\n",
  571. control_thread);
  572. retval = control_thread;
  573. goto err_out_driver_unregister;
  574. }
  575. return 0;
  576. err_out_driver_unregister:
  577. driver_unregister(&w1_driver);
  578. err_out_bus_unregister:
  579. bus_unregister(&w1_bus_type);
  580. err_out_exit_init:
  581. return retval;
  582. }
  583. static void w1_fini(void)
  584. {
  585. struct w1_master *dev;
  586. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  587. __w1_remove_master_device(dev);
  588. control_needs_exit = 1;
  589. wait_for_completion(&w1_control_complete);
  590. driver_unregister(&w1_driver);
  591. bus_unregister(&w1_bus_type);
  592. }
  593. module_init(w1_init);
  594. module_exit(w1_fini);