w1.c 21 KB

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