w1.c 23 KB

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