w1.c 26 KB

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