w1.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * w1.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/list.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/timer.h>
  29. #include <linux/device.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include <linux/kthread.h>
  33. #include <linux/freezer.h>
  34. #include <asm/atomic.h>
  35. #include "w1.h"
  36. #include "w1_log.h"
  37. #include "w1_int.h"
  38. #include "w1_family.h"
  39. #include "w1_netlink.h"
  40. MODULE_LICENSE("GPL");
  41. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  42. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
  43. static int w1_timeout = 10;
  44. int w1_max_slave_count = 10;
  45. int w1_max_slave_ttl = 10;
  46. module_param_named(timeout, w1_timeout, int, 0);
  47. module_param_named(max_slave_count, w1_max_slave_count, int, 0);
  48. module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
  49. DEFINE_MUTEX(w1_mlock);
  50. LIST_HEAD(w1_masters);
  51. static int w1_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. #ifdef CONFIG_HOTPLUG
  457. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
  458. {
  459. struct w1_master *md = NULL;
  460. struct w1_slave *sl = NULL;
  461. char *event_owner, *name;
  462. int err;
  463. if (dev->driver == &w1_master_driver) {
  464. md = container_of(dev, struct w1_master, dev);
  465. event_owner = "master";
  466. name = md->name;
  467. } else if (dev->driver == &w1_slave_driver) {
  468. sl = container_of(dev, struct w1_slave, dev);
  469. event_owner = "slave";
  470. name = sl->name;
  471. } else {
  472. dev_dbg(dev, "Unknown event.\n");
  473. return -EINVAL;
  474. }
  475. dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
  476. event_owner, name, dev_name(dev));
  477. if (dev->driver != &w1_slave_driver || !sl)
  478. return 0;
  479. err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
  480. if (err)
  481. return err;
  482. err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
  483. (unsigned long long)sl->reg_num.id);
  484. if (err)
  485. return err;
  486. return 0;
  487. };
  488. #else
  489. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
  490. {
  491. return 0;
  492. }
  493. #endif
  494. static int __w1_attach_slave_device(struct w1_slave *sl)
  495. {
  496. int err;
  497. sl->dev.parent = &sl->master->dev;
  498. sl->dev.driver = &w1_slave_driver;
  499. sl->dev.bus = &w1_bus_type;
  500. sl->dev.release = &w1_slave_release;
  501. dev_set_name(&sl->dev, "%02x-%012llx",
  502. (unsigned int) sl->reg_num.family,
  503. (unsigned long long) sl->reg_num.id);
  504. snprintf(&sl->name[0], sizeof(sl->name),
  505. "%02x-%012llx",
  506. (unsigned int) sl->reg_num.family,
  507. (unsigned long long) sl->reg_num.id);
  508. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
  509. dev_name(&sl->dev), sl);
  510. err = device_register(&sl->dev);
  511. if (err < 0) {
  512. dev_err(&sl->dev,
  513. "Device registration [%s] failed. err=%d\n",
  514. dev_name(&sl->dev), err);
  515. return err;
  516. }
  517. /* Create "name" entry */
  518. err = device_create_file(&sl->dev, &w1_slave_attr_name);
  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_unreg;
  524. }
  525. /* Create "id" entry */
  526. err = device_create_file(&sl->dev, &w1_slave_attr_id);
  527. if (err < 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_rem1;
  532. }
  533. /* if the family driver needs to initialize something... */
  534. if (sl->family->fops && sl->family->fops->add_slave &&
  535. ((err = sl->family->fops->add_slave(sl)) < 0)) {
  536. dev_err(&sl->dev,
  537. "sysfs file creation for [%s] failed. err=%d\n",
  538. dev_name(&sl->dev), err);
  539. goto out_rem2;
  540. }
  541. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  542. return 0;
  543. out_rem2:
  544. device_remove_file(&sl->dev, &w1_slave_attr_id);
  545. out_rem1:
  546. device_remove_file(&sl->dev, &w1_slave_attr_name);
  547. out_unreg:
  548. device_unregister(&sl->dev);
  549. return err;
  550. }
  551. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  552. {
  553. struct w1_slave *sl;
  554. struct w1_family *f;
  555. int err;
  556. struct w1_netlink_msg msg;
  557. sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
  558. if (!sl) {
  559. dev_err(&dev->dev,
  560. "%s: failed to allocate new slave device.\n",
  561. __func__);
  562. return -ENOMEM;
  563. }
  564. sl->owner = THIS_MODULE;
  565. sl->master = dev;
  566. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  567. memset(&msg, 0, sizeof(msg));
  568. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  569. atomic_set(&sl->refcnt, 0);
  570. init_completion(&sl->released);
  571. spin_lock(&w1_flock);
  572. f = w1_family_registered(rn->family);
  573. if (!f) {
  574. f= &w1_default_family;
  575. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  576. rn->family, rn->family,
  577. (unsigned long long)rn->id, rn->crc);
  578. }
  579. __w1_family_get(f);
  580. spin_unlock(&w1_flock);
  581. sl->family = f;
  582. err = __w1_attach_slave_device(sl);
  583. if (err < 0) {
  584. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  585. sl->name);
  586. w1_family_put(sl->family);
  587. kfree(sl);
  588. return err;
  589. }
  590. sl->ttl = dev->slave_ttl;
  591. dev->slave_count++;
  592. memcpy(msg.id.id, rn, sizeof(msg.id));
  593. msg.type = W1_SLAVE_ADD;
  594. w1_netlink_send(dev, &msg);
  595. return 0;
  596. }
  597. void w1_slave_detach(struct w1_slave *sl)
  598. {
  599. struct w1_netlink_msg msg;
  600. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
  601. list_del(&sl->w1_slave_entry);
  602. if (sl->family->fops && sl->family->fops->remove_slave)
  603. sl->family->fops->remove_slave(sl);
  604. memset(&msg, 0, sizeof(msg));
  605. memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
  606. msg.type = W1_SLAVE_REMOVE;
  607. w1_netlink_send(sl->master, &msg);
  608. device_remove_file(&sl->dev, &w1_slave_attr_id);
  609. device_remove_file(&sl->dev, &w1_slave_attr_name);
  610. device_unregister(&sl->dev);
  611. wait_for_completion(&sl->released);
  612. kfree(sl);
  613. }
  614. struct w1_master *w1_search_master_id(u32 id)
  615. {
  616. struct w1_master *dev;
  617. int found = 0;
  618. mutex_lock(&w1_mlock);
  619. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  620. if (dev->id == id) {
  621. found = 1;
  622. atomic_inc(&dev->refcnt);
  623. break;
  624. }
  625. }
  626. mutex_unlock(&w1_mlock);
  627. return (found)?dev:NULL;
  628. }
  629. struct w1_slave *w1_search_slave(struct w1_reg_num *id)
  630. {
  631. struct w1_master *dev;
  632. struct w1_slave *sl = NULL;
  633. int found = 0;
  634. mutex_lock(&w1_mlock);
  635. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  636. mutex_lock(&dev->mutex);
  637. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  638. if (sl->reg_num.family == id->family &&
  639. sl->reg_num.id == id->id &&
  640. sl->reg_num.crc == id->crc) {
  641. found = 1;
  642. atomic_inc(&dev->refcnt);
  643. atomic_inc(&sl->refcnt);
  644. break;
  645. }
  646. }
  647. mutex_unlock(&dev->mutex);
  648. if (found)
  649. break;
  650. }
  651. mutex_unlock(&w1_mlock);
  652. return (found)?sl:NULL;
  653. }
  654. void w1_reconnect_slaves(struct w1_family *f, int attach)
  655. {
  656. struct w1_slave *sl, *sln;
  657. struct w1_master *dev;
  658. mutex_lock(&w1_mlock);
  659. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  660. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  661. "for family %02x.\n", dev->name, f->fid);
  662. mutex_lock(&dev->mutex);
  663. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  664. /* If it is a new family, slaves with the default
  665. * family driver and are that family will be
  666. * connected. If the family is going away, devices
  667. * matching that family are reconneced.
  668. */
  669. if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
  670. && sl->reg_num.family == f->fid) ||
  671. (!attach && sl->family->fid == f->fid)) {
  672. struct w1_reg_num rn;
  673. memcpy(&rn, &sl->reg_num, sizeof(rn));
  674. w1_slave_detach(sl);
  675. w1_attach_slave_device(dev, &rn);
  676. }
  677. }
  678. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  679. "has been finished.\n", dev->name);
  680. mutex_unlock(&dev->mutex);
  681. }
  682. mutex_unlock(&w1_mlock);
  683. }
  684. void w1_slave_found(struct w1_master *dev, u64 rn)
  685. {
  686. struct w1_slave *sl;
  687. struct w1_reg_num *tmp;
  688. u64 rn_le = cpu_to_le64(rn);
  689. atomic_inc(&dev->refcnt);
  690. tmp = (struct w1_reg_num *) &rn;
  691. sl = w1_slave_search_device(dev, tmp);
  692. if (sl) {
  693. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  694. } else {
  695. if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
  696. w1_attach_slave_device(dev, tmp);
  697. }
  698. atomic_dec(&dev->refcnt);
  699. }
  700. /**
  701. * Performs a ROM Search & registers any devices found.
  702. * The 1-wire search is a simple binary tree search.
  703. * For each bit of the address, we read two bits and write one bit.
  704. * The bit written will put to sleep all devies that don't match that bit.
  705. * When the two reads differ, the direction choice is obvious.
  706. * When both bits are 0, we must choose a path to take.
  707. * When we can scan all 64 bits without having to choose a path, we are done.
  708. *
  709. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  710. *
  711. * @dev The master device to search
  712. * @cb Function to call when a device is found
  713. */
  714. void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
  715. {
  716. u64 last_rn, rn, tmp64;
  717. int i, slave_count = 0;
  718. int last_zero, last_device;
  719. int search_bit, desc_bit;
  720. u8 triplet_ret = 0;
  721. search_bit = 0;
  722. rn = last_rn = 0;
  723. last_device = 0;
  724. last_zero = -1;
  725. desc_bit = 64;
  726. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  727. last_rn = rn;
  728. rn = 0;
  729. /*
  730. * Reset bus and all 1-wire device state machines
  731. * so they can respond to our requests.
  732. *
  733. * Return 0 - device(s) present, 1 - no devices present.
  734. */
  735. if (w1_reset_bus(dev)) {
  736. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  737. break;
  738. }
  739. /* Start the search */
  740. w1_write_8(dev, search_type);
  741. for (i = 0; i < 64; ++i) {
  742. /* Determine the direction/search bit */
  743. if (i == desc_bit)
  744. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  745. else if (i > desc_bit)
  746. search_bit = 0; /* take the 0 path on the next branch */
  747. else
  748. search_bit = ((last_rn >> i) & 0x1);
  749. /** Read two bits and write one bit */
  750. triplet_ret = w1_triplet(dev, search_bit);
  751. /* quit if no device responded */
  752. if ( (triplet_ret & 0x03) == 0x03 )
  753. break;
  754. /* If both directions were valid, and we took the 0 path... */
  755. if (triplet_ret == 0)
  756. last_zero = i;
  757. /* extract the direction taken & update the device number */
  758. tmp64 = (triplet_ret >> 2);
  759. rn |= (tmp64 << i);
  760. if (kthread_should_stop()) {
  761. dev_dbg(&dev->dev, "Abort w1_search\n");
  762. return;
  763. }
  764. }
  765. if ( (triplet_ret & 0x03) != 0x03 ) {
  766. if ( (desc_bit == last_zero) || (last_zero < 0))
  767. last_device = 1;
  768. desc_bit = last_zero;
  769. cb(dev, rn);
  770. }
  771. }
  772. }
  773. void w1_search_process_cb(struct w1_master *dev, u8 search_type,
  774. w1_slave_found_callback cb)
  775. {
  776. struct w1_slave *sl, *sln;
  777. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  778. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  779. w1_search_devices(dev, search_type, cb);
  780. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  781. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl)
  782. w1_slave_detach(sl);
  783. else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  784. sl->ttl = dev->slave_ttl;
  785. }
  786. if (dev->search_count > 0)
  787. dev->search_count--;
  788. }
  789. static void w1_search_process(struct w1_master *dev, u8 search_type)
  790. {
  791. w1_search_process_cb(dev, search_type, w1_slave_found);
  792. }
  793. int w1_process(void *data)
  794. {
  795. struct w1_master *dev = (struct w1_master *) data;
  796. /* As long as w1_timeout is only set by a module parameter the sleep
  797. * time can be calculated in jiffies once.
  798. */
  799. const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
  800. while (!kthread_should_stop()) {
  801. if (dev->search_count) {
  802. mutex_lock(&dev->mutex);
  803. w1_search_process(dev, W1_SEARCH);
  804. mutex_unlock(&dev->mutex);
  805. }
  806. try_to_freeze();
  807. __set_current_state(TASK_INTERRUPTIBLE);
  808. if (kthread_should_stop())
  809. break;
  810. /* Only sleep when the search is active. */
  811. if (dev->search_count)
  812. schedule_timeout(jtime);
  813. else
  814. schedule();
  815. }
  816. atomic_dec(&dev->refcnt);
  817. return 0;
  818. }
  819. static int __init w1_init(void)
  820. {
  821. int retval;
  822. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  823. w1_init_netlink();
  824. retval = bus_register(&w1_bus_type);
  825. if (retval) {
  826. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  827. goto err_out_exit_init;
  828. }
  829. retval = driver_register(&w1_master_driver);
  830. if (retval) {
  831. printk(KERN_ERR
  832. "Failed to register master driver. err=%d.\n",
  833. retval);
  834. goto err_out_bus_unregister;
  835. }
  836. retval = driver_register(&w1_slave_driver);
  837. if (retval) {
  838. printk(KERN_ERR
  839. "Failed to register master driver. err=%d.\n",
  840. retval);
  841. goto err_out_master_unregister;
  842. }
  843. return 0;
  844. #if 0
  845. /* For undoing the slave register if there was a step after it. */
  846. err_out_slave_unregister:
  847. driver_unregister(&w1_slave_driver);
  848. #endif
  849. err_out_master_unregister:
  850. driver_unregister(&w1_master_driver);
  851. err_out_bus_unregister:
  852. bus_unregister(&w1_bus_type);
  853. err_out_exit_init:
  854. return retval;
  855. }
  856. static void __exit w1_fini(void)
  857. {
  858. struct w1_master *dev;
  859. /* Set netlink removal messages and some cleanup */
  860. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  861. __w1_remove_master_device(dev);
  862. w1_fini_netlink();
  863. driver_unregister(&w1_slave_driver);
  864. driver_unregister(&w1_master_driver);
  865. bus_unregister(&w1_bus_type);
  866. }
  867. module_init(w1_init);
  868. module_exit(w1_fini);