w1.c 21 KB

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