w1.c 22 KB

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