w1.c 22 KB

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