w1.c 24 KB

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