w1.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. if (md->nls && md->nls->sk_socket)
  68. sock_release(md->nls->sk_socket);
  69. memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
  70. kfree(md);
  71. }
  72. static void w1_slave_release(struct device *dev)
  73. {
  74. struct w1_slave *sl = dev_to_w1_slave(dev);
  75. dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name);
  76. while (atomic_read(&sl->refcnt)) {
  77. dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n",
  78. sl->name, atomic_read(&sl->refcnt));
  79. if (msleep_interruptible(1000))
  80. flush_signals(current);
  81. }
  82. w1_family_put(sl->family);
  83. sl->master->slave_count--;
  84. complete(&sl->released);
  85. }
  86. static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  87. {
  88. struct w1_slave *sl = dev_to_w1_slave(dev);
  89. return sprintf(buf, "%s\n", sl->name);
  90. }
  91. static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, size_t count)
  92. {
  93. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  94. atomic_inc(&sl->refcnt);
  95. if (off > 8) {
  96. count = 0;
  97. } else {
  98. if (off + count > 8)
  99. count = 8 - off;
  100. memcpy(buf, (u8 *)&sl->reg_num, count);
  101. }
  102. atomic_dec(&sl->refcnt);
  103. return count;
  104. }
  105. static struct device_attribute w1_slave_attr_name =
  106. __ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
  107. static struct bin_attribute w1_slave_attr_bin_id = {
  108. .attr = {
  109. .name = "id",
  110. .mode = S_IRUGO,
  111. .owner = THIS_MODULE,
  112. },
  113. .size = 8,
  114. .read = w1_slave_read_id,
  115. };
  116. /* Default family */
  117. static struct w1_family w1_default_family;
  118. static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
  119. static struct bus_type w1_bus_type = {
  120. .name = "w1",
  121. .match = w1_master_match,
  122. .hotplug = w1_hotplug,
  123. };
  124. struct device_driver w1_master_driver = {
  125. .name = "w1_master_driver",
  126. .bus = &w1_bus_type,
  127. .probe = w1_master_probe,
  128. };
  129. struct device w1_master_device = {
  130. .parent = NULL,
  131. .bus = &w1_bus_type,
  132. .bus_id = "w1 bus master",
  133. .driver = &w1_master_driver,
  134. .release = &w1_master_release
  135. };
  136. struct device_driver w1_slave_driver = {
  137. .name = "w1_slave_driver",
  138. .bus = &w1_bus_type,
  139. };
  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. 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. 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_hotplug(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 hotplug 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_hotplug_env_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family);
  307. if (err)
  308. return err;
  309. err = add_hotplug_env_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id);
  310. if (err)
  311. return err;
  312. return 0;
  313. };
  314. #else
  315. static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  316. {
  317. return 0;
  318. }
  319. #endif
  320. static int __w1_attach_slave_device(struct w1_slave *sl)
  321. {
  322. int err;
  323. sl->dev.parent = &sl->master->dev;
  324. sl->dev.driver = &w1_slave_driver;
  325. sl->dev.bus = &w1_bus_type;
  326. sl->dev.release = &w1_slave_release;
  327. snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
  328. "%02x-%012llx",
  329. (unsigned int) sl->reg_num.family,
  330. (unsigned long long) sl->reg_num.id);
  331. snprintf(&sl->name[0], sizeof(sl->name),
  332. "%02x-%012llx",
  333. (unsigned int) sl->reg_num.family,
  334. (unsigned long long) sl->reg_num.id);
  335. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]);
  336. err = device_register(&sl->dev);
  337. if (err < 0) {
  338. dev_err(&sl->dev,
  339. "Device registration [%s] failed. err=%d\n",
  340. sl->dev.bus_id, err);
  341. return err;
  342. }
  343. /* Create "name" entry */
  344. err = device_create_file(&sl->dev, &w1_slave_attr_name);
  345. if (err < 0) {
  346. dev_err(&sl->dev,
  347. "sysfs file creation for [%s] failed. err=%d\n",
  348. sl->dev.bus_id, err);
  349. goto out_unreg;
  350. }
  351. /* Create "id" entry */
  352. err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  353. if (err < 0) {
  354. dev_err(&sl->dev,
  355. "sysfs file creation for [%s] failed. err=%d\n",
  356. sl->dev.bus_id, err);
  357. goto out_rem1;
  358. }
  359. /* if the family driver needs to initialize something... */
  360. if (sl->family->fops && sl->family->fops->add_slave &&
  361. ((err = sl->family->fops->add_slave(sl)) < 0)) {
  362. dev_err(&sl->dev,
  363. "sysfs file creation for [%s] failed. err=%d\n",
  364. sl->dev.bus_id, err);
  365. goto out_rem2;
  366. }
  367. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  368. return 0;
  369. out_rem2:
  370. sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  371. out_rem1:
  372. device_remove_file(&sl->dev, &w1_slave_attr_name);
  373. out_unreg:
  374. device_unregister(&sl->dev);
  375. return err;
  376. }
  377. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  378. {
  379. struct w1_slave *sl;
  380. struct w1_family *f;
  381. int err;
  382. struct w1_netlink_msg msg;
  383. sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
  384. if (!sl) {
  385. dev_err(&dev->dev,
  386. "%s: failed to allocate new slave device.\n",
  387. __func__);
  388. return -ENOMEM;
  389. }
  390. memset(sl, 0, sizeof(*sl));
  391. sl->owner = THIS_MODULE;
  392. sl->master = dev;
  393. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  394. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  395. atomic_set(&sl->refcnt, 0);
  396. init_completion(&sl->released);
  397. spin_lock(&w1_flock);
  398. f = w1_family_registered(rn->family);
  399. if (!f) {
  400. f= &w1_default_family;
  401. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  402. rn->family, rn->family,
  403. (unsigned long long)rn->id, rn->crc);
  404. }
  405. __w1_family_get(f);
  406. spin_unlock(&w1_flock);
  407. sl->family = f;
  408. err = __w1_attach_slave_device(sl);
  409. if (err < 0) {
  410. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  411. sl->name);
  412. w1_family_put(sl->family);
  413. kfree(sl);
  414. return err;
  415. }
  416. sl->ttl = dev->slave_ttl;
  417. dev->slave_count++;
  418. memcpy(&msg.id.id, rn, sizeof(msg.id.id));
  419. msg.type = W1_SLAVE_ADD;
  420. w1_netlink_send(dev, &msg);
  421. return 0;
  422. }
  423. static void w1_slave_detach(struct w1_slave *sl)
  424. {
  425. struct w1_netlink_msg msg;
  426. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
  427. list_del(&sl->w1_slave_entry);
  428. if (sl->family->fops && sl->family->fops->remove_slave)
  429. sl->family->fops->remove_slave(sl);
  430. memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
  431. msg.type = W1_SLAVE_REMOVE;
  432. w1_netlink_send(sl->master, &msg);
  433. sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  434. device_remove_file(&sl->dev, &w1_slave_attr_name);
  435. device_unregister(&sl->dev);
  436. wait_for_completion(&sl->released);
  437. kfree(sl);
  438. }
  439. static struct w1_master *w1_search_master(unsigned long data)
  440. {
  441. struct w1_master *dev;
  442. int found = 0;
  443. spin_lock_bh(&w1_mlock);
  444. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  445. if (dev->bus_master->data == data) {
  446. found = 1;
  447. atomic_inc(&dev->refcnt);
  448. break;
  449. }
  450. }
  451. spin_unlock_bh(&w1_mlock);
  452. return (found)?dev:NULL;
  453. }
  454. void w1_reconnect_slaves(struct w1_family *f)
  455. {
  456. struct w1_master *dev;
  457. spin_lock_bh(&w1_mlock);
  458. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  459. dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
  460. dev->name, f->fid);
  461. set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
  462. }
  463. spin_unlock_bh(&w1_mlock);
  464. }
  465. static void w1_slave_found(unsigned long data, u64 rn)
  466. {
  467. int slave_count;
  468. struct w1_slave *sl;
  469. struct list_head *ent;
  470. struct w1_reg_num *tmp;
  471. int family_found = 0;
  472. struct w1_master *dev;
  473. u64 rn_le = cpu_to_le64(rn);
  474. dev = w1_search_master(data);
  475. if (!dev) {
  476. printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
  477. data);
  478. return;
  479. }
  480. tmp = (struct w1_reg_num *) &rn;
  481. slave_count = 0;
  482. list_for_each(ent, &dev->slist) {
  483. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  484. if (sl->reg_num.family == tmp->family &&
  485. sl->reg_num.id == tmp->id &&
  486. sl->reg_num.crc == tmp->crc) {
  487. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  488. break;
  489. } else if (sl->reg_num.family == tmp->family) {
  490. family_found = 1;
  491. break;
  492. }
  493. slave_count++;
  494. }
  495. if (slave_count == dev->slave_count &&
  496. rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
  497. w1_attach_slave_device(dev, tmp);
  498. }
  499. atomic_dec(&dev->refcnt);
  500. }
  501. /**
  502. * Performs a ROM Search & registers any devices found.
  503. * The 1-wire search is a simple binary tree search.
  504. * For each bit of the address, we read two bits and write one bit.
  505. * The bit written will put to sleep all devies that don't match that bit.
  506. * When the two reads differ, the direction choice is obvious.
  507. * When both bits are 0, we must choose a path to take.
  508. * When we can scan all 64 bits without having to choose a path, we are done.
  509. *
  510. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  511. *
  512. * @dev The master device to search
  513. * @cb Function to call when a device is found
  514. */
  515. void w1_search(struct w1_master *dev, w1_slave_found_callback cb)
  516. {
  517. u64 last_rn, rn, tmp64;
  518. int i, slave_count = 0;
  519. int last_zero, last_device;
  520. int search_bit, desc_bit;
  521. u8 triplet_ret = 0;
  522. search_bit = 0;
  523. rn = last_rn = 0;
  524. last_device = 0;
  525. last_zero = -1;
  526. desc_bit = 64;
  527. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  528. last_rn = rn;
  529. rn = 0;
  530. /*
  531. * Reset bus and all 1-wire device state machines
  532. * so they can respond to our requests.
  533. *
  534. * Return 0 - device(s) present, 1 - no devices present.
  535. */
  536. if (w1_reset_bus(dev)) {
  537. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  538. break;
  539. }
  540. /* Start the search */
  541. w1_write_8(dev, W1_SEARCH);
  542. for (i = 0; i < 64; ++i) {
  543. /* Determine the direction/search bit */
  544. if (i == desc_bit)
  545. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  546. else if (i > desc_bit)
  547. search_bit = 0; /* take the 0 path on the next branch */
  548. else
  549. search_bit = ((last_rn >> i) & 0x1);
  550. /** Read two bits and write one bit */
  551. triplet_ret = w1_triplet(dev, search_bit);
  552. /* quit if no device responded */
  553. if ( (triplet_ret & 0x03) == 0x03 )
  554. break;
  555. /* If both directions were valid, and we took the 0 path... */
  556. if (triplet_ret == 0)
  557. last_zero = i;
  558. /* extract the direction taken & update the device number */
  559. tmp64 = (triplet_ret >> 2);
  560. rn |= (tmp64 << i);
  561. }
  562. if ( (triplet_ret & 0x03) != 0x03 ) {
  563. if ( (desc_bit == last_zero) || (last_zero < 0))
  564. last_device = 1;
  565. desc_bit = last_zero;
  566. cb(dev->bus_master->data, rn);
  567. }
  568. }
  569. }
  570. static int w1_control(void *data)
  571. {
  572. struct w1_slave *sl, *sln;
  573. struct w1_master *dev, *n;
  574. int err, have_to_wait = 0;
  575. daemonize("w1_control");
  576. allow_signal(SIGTERM);
  577. while (!control_needs_exit || have_to_wait) {
  578. have_to_wait = 0;
  579. try_to_freeze();
  580. msleep_interruptible(w1_control_timeout * 1000);
  581. if (signal_pending(current))
  582. flush_signals(current);
  583. list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
  584. if (!control_needs_exit && !dev->flags)
  585. continue;
  586. /*
  587. * Little race: we can create thread but not set the flag.
  588. * Get a chance for external process to set flag up.
  589. */
  590. if (!dev->initialized) {
  591. have_to_wait = 1;
  592. continue;
  593. }
  594. if (control_needs_exit) {
  595. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  596. err = kill_proc(dev->kpid, SIGTERM, 1);
  597. if (err)
  598. dev_err(&dev->dev,
  599. "Failed to send signal to w1 kernel thread %d.\n",
  600. dev->kpid);
  601. }
  602. if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
  603. wait_for_completion(&dev->dev_exited);
  604. spin_lock_bh(&w1_mlock);
  605. list_del(&dev->w1_master_entry);
  606. spin_unlock_bh(&w1_mlock);
  607. down(&dev->mutex);
  608. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  609. w1_slave_detach(sl);
  610. }
  611. w1_destroy_master_attributes(dev);
  612. up(&dev->mutex);
  613. atomic_dec(&dev->refcnt);
  614. continue;
  615. }
  616. if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
  617. dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
  618. down(&dev->mutex);
  619. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  620. if (sl->family->fid == W1_FAMILY_DEFAULT) {
  621. struct w1_reg_num rn;
  622. memcpy(&rn, &sl->reg_num, sizeof(rn));
  623. w1_slave_detach(sl);
  624. w1_attach_slave_device(dev, &rn);
  625. }
  626. }
  627. dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name);
  628. clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
  629. up(&dev->mutex);
  630. }
  631. }
  632. }
  633. complete_and_exit(&w1_control_complete, 0);
  634. }
  635. int w1_process(void *data)
  636. {
  637. struct w1_master *dev = (struct w1_master *) data;
  638. struct w1_slave *sl, *sln;
  639. daemonize("%s", dev->name);
  640. allow_signal(SIGTERM);
  641. while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
  642. try_to_freeze();
  643. msleep_interruptible(w1_timeout * 1000);
  644. if (signal_pending(current))
  645. flush_signals(current);
  646. if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
  647. break;
  648. if (!dev->initialized)
  649. continue;
  650. if (dev->search_count == 0)
  651. continue;
  652. if (down_interruptible(&dev->mutex))
  653. continue;
  654. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  655. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  656. w1_search_devices(dev, w1_slave_found);
  657. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  658. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
  659. w1_slave_detach(sl);
  660. dev->slave_count--;
  661. } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  662. sl->ttl = dev->slave_ttl;
  663. }
  664. if (dev->search_count > 0)
  665. dev->search_count--;
  666. up(&dev->mutex);
  667. }
  668. atomic_dec(&dev->refcnt);
  669. complete_and_exit(&dev->dev_exited, 0);
  670. return 0;
  671. }
  672. static int w1_init(void)
  673. {
  674. int retval;
  675. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  676. retval = bus_register(&w1_bus_type);
  677. if (retval) {
  678. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  679. goto err_out_exit_init;
  680. }
  681. retval = driver_register(&w1_master_driver);
  682. if (retval) {
  683. printk(KERN_ERR
  684. "Failed to register master driver. err=%d.\n",
  685. retval);
  686. goto err_out_bus_unregister;
  687. }
  688. retval = driver_register(&w1_slave_driver);
  689. if (retval) {
  690. printk(KERN_ERR
  691. "Failed to register master driver. err=%d.\n",
  692. retval);
  693. goto err_out_master_unregister;
  694. }
  695. control_thread = kernel_thread(&w1_control, NULL, 0);
  696. if (control_thread < 0) {
  697. printk(KERN_ERR "Failed to create control thread. err=%d\n",
  698. control_thread);
  699. retval = control_thread;
  700. goto err_out_slave_unregister;
  701. }
  702. return 0;
  703. err_out_slave_unregister:
  704. driver_unregister(&w1_slave_driver);
  705. err_out_master_unregister:
  706. driver_unregister(&w1_master_driver);
  707. err_out_bus_unregister:
  708. bus_unregister(&w1_bus_type);
  709. err_out_exit_init:
  710. return retval;
  711. }
  712. static void w1_fini(void)
  713. {
  714. struct w1_master *dev;
  715. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  716. __w1_remove_master_device(dev);
  717. control_needs_exit = 1;
  718. wait_for_completion(&w1_control_complete);
  719. driver_unregister(&w1_slave_driver);
  720. driver_unregister(&w1_master_driver);
  721. bus_unregister(&w1_bus_type);
  722. }
  723. module_init(w1_init);
  724. module_exit(w1_fini);