w1.c 19 KB

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