w1.c 22 KB

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