w1.c 23 KB

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