w1.c 26 KB

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