w1.c 21 KB

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