w1.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. if (!fops)
  509. return 0;
  510. switch (action) {
  511. case BUS_NOTIFY_ADD_DEVICE:
  512. /* if the family driver needs to initialize something... */
  513. if (fops->add_slave) {
  514. err = fops->add_slave(sl);
  515. if (err < 0) {
  516. dev_err(&sl->dev,
  517. "add_slave() call failed. err=%d\n",
  518. err);
  519. return err;
  520. }
  521. }
  522. if (fops->groups) {
  523. err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
  524. if (err) {
  525. dev_err(&sl->dev,
  526. "sysfs group creation failed. err=%d\n",
  527. err);
  528. return err;
  529. }
  530. }
  531. break;
  532. case BUS_NOTIFY_DEL_DEVICE:
  533. if (fops->remove_slave)
  534. sl->family->fops->remove_slave(sl);
  535. if (fops->groups)
  536. sysfs_remove_groups(&sl->dev.kobj, fops->groups);
  537. break;
  538. }
  539. return 0;
  540. }
  541. static struct notifier_block w1_bus_nb = {
  542. .notifier_call = w1_bus_notify,
  543. };
  544. static int __w1_attach_slave_device(struct w1_slave *sl)
  545. {
  546. int err;
  547. sl->dev.parent = &sl->master->dev;
  548. sl->dev.driver = &w1_slave_driver;
  549. sl->dev.bus = &w1_bus_type;
  550. sl->dev.release = &w1_slave_release;
  551. sl->dev.groups = w1_slave_groups;
  552. dev_set_name(&sl->dev, "%02x-%012llx",
  553. (unsigned int) sl->reg_num.family,
  554. (unsigned long long) sl->reg_num.id);
  555. snprintf(&sl->name[0], sizeof(sl->name),
  556. "%02x-%012llx",
  557. (unsigned int) sl->reg_num.family,
  558. (unsigned long long) sl->reg_num.id);
  559. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
  560. dev_name(&sl->dev), sl);
  561. err = device_register(&sl->dev);
  562. if (err < 0) {
  563. dev_err(&sl->dev,
  564. "Device registration [%s] failed. err=%d\n",
  565. dev_name(&sl->dev), err);
  566. return err;
  567. }
  568. dev_set_uevent_suppress(&sl->dev, false);
  569. kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
  570. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  571. return 0;
  572. }
  573. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  574. {
  575. struct w1_slave *sl;
  576. struct w1_family *f;
  577. int err;
  578. struct w1_netlink_msg msg;
  579. sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
  580. if (!sl) {
  581. dev_err(&dev->dev,
  582. "%s: failed to allocate new slave device.\n",
  583. __func__);
  584. return -ENOMEM;
  585. }
  586. sl->owner = THIS_MODULE;
  587. sl->master = dev;
  588. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  589. memset(&msg, 0, sizeof(msg));
  590. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  591. atomic_set(&sl->refcnt, 0);
  592. init_completion(&sl->released);
  593. /* slave modules need to be loaded in a context with unlocked mutex */
  594. mutex_unlock(&dev->mutex);
  595. request_module("w1-family-0x%0x", rn->family);
  596. mutex_lock(&dev->mutex);
  597. spin_lock(&w1_flock);
  598. f = w1_family_registered(rn->family);
  599. if (!f) {
  600. f= &w1_default_family;
  601. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  602. rn->family, rn->family,
  603. (unsigned long long)rn->id, rn->crc);
  604. }
  605. __w1_family_get(f);
  606. spin_unlock(&w1_flock);
  607. sl->family = f;
  608. err = __w1_attach_slave_device(sl);
  609. if (err < 0) {
  610. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  611. sl->name);
  612. w1_family_put(sl->family);
  613. kfree(sl);
  614. return err;
  615. }
  616. sl->ttl = dev->slave_ttl;
  617. dev->slave_count++;
  618. memcpy(msg.id.id, rn, sizeof(msg.id));
  619. msg.type = W1_SLAVE_ADD;
  620. w1_netlink_send(dev, &msg);
  621. return 0;
  622. }
  623. void w1_slave_detach(struct w1_slave *sl)
  624. {
  625. struct w1_netlink_msg msg;
  626. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
  627. list_del(&sl->w1_slave_entry);
  628. memset(&msg, 0, sizeof(msg));
  629. memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
  630. msg.type = W1_SLAVE_REMOVE;
  631. w1_netlink_send(sl->master, &msg);
  632. device_unregister(&sl->dev);
  633. wait_for_completion(&sl->released);
  634. kfree(sl);
  635. }
  636. struct w1_master *w1_search_master_id(u32 id)
  637. {
  638. struct w1_master *dev;
  639. int found = 0;
  640. mutex_lock(&w1_mlock);
  641. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  642. if (dev->id == id) {
  643. found = 1;
  644. atomic_inc(&dev->refcnt);
  645. break;
  646. }
  647. }
  648. mutex_unlock(&w1_mlock);
  649. return (found)?dev:NULL;
  650. }
  651. struct w1_slave *w1_search_slave(struct w1_reg_num *id)
  652. {
  653. struct w1_master *dev;
  654. struct w1_slave *sl = NULL;
  655. int found = 0;
  656. mutex_lock(&w1_mlock);
  657. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  658. mutex_lock(&dev->mutex);
  659. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  660. if (sl->reg_num.family == id->family &&
  661. sl->reg_num.id == id->id &&
  662. sl->reg_num.crc == id->crc) {
  663. found = 1;
  664. atomic_inc(&dev->refcnt);
  665. atomic_inc(&sl->refcnt);
  666. break;
  667. }
  668. }
  669. mutex_unlock(&dev->mutex);
  670. if (found)
  671. break;
  672. }
  673. mutex_unlock(&w1_mlock);
  674. return (found)?sl:NULL;
  675. }
  676. void w1_reconnect_slaves(struct w1_family *f, int attach)
  677. {
  678. struct w1_slave *sl, *sln;
  679. struct w1_master *dev;
  680. mutex_lock(&w1_mlock);
  681. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  682. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  683. "for family %02x.\n", dev->name, f->fid);
  684. mutex_lock(&dev->mutex);
  685. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  686. /* If it is a new family, slaves with the default
  687. * family driver and are that family will be
  688. * connected. If the family is going away, devices
  689. * matching that family are reconneced.
  690. */
  691. if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
  692. && sl->reg_num.family == f->fid) ||
  693. (!attach && sl->family->fid == f->fid)) {
  694. struct w1_reg_num rn;
  695. memcpy(&rn, &sl->reg_num, sizeof(rn));
  696. w1_slave_detach(sl);
  697. w1_attach_slave_device(dev, &rn);
  698. }
  699. }
  700. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  701. "has been finished.\n", dev->name);
  702. mutex_unlock(&dev->mutex);
  703. }
  704. mutex_unlock(&w1_mlock);
  705. }
  706. void w1_slave_found(struct w1_master *dev, u64 rn)
  707. {
  708. struct w1_slave *sl;
  709. struct w1_reg_num *tmp;
  710. u64 rn_le = cpu_to_le64(rn);
  711. atomic_inc(&dev->refcnt);
  712. tmp = (struct w1_reg_num *) &rn;
  713. sl = w1_slave_search_device(dev, tmp);
  714. if (sl) {
  715. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  716. } else {
  717. if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
  718. w1_attach_slave_device(dev, tmp);
  719. }
  720. atomic_dec(&dev->refcnt);
  721. }
  722. /**
  723. * Performs a ROM Search & registers any devices found.
  724. * The 1-wire search is a simple binary tree search.
  725. * For each bit of the address, we read two bits and write one bit.
  726. * The bit written will put to sleep all devies that don't match that bit.
  727. * When the two reads differ, the direction choice is obvious.
  728. * When both bits are 0, we must choose a path to take.
  729. * When we can scan all 64 bits without having to choose a path, we are done.
  730. *
  731. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  732. *
  733. * @dev The master device to search
  734. * @cb Function to call when a device is found
  735. */
  736. void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
  737. {
  738. u64 last_rn, rn, tmp64;
  739. int i, slave_count = 0;
  740. int last_zero, last_device;
  741. int search_bit, desc_bit;
  742. u8 triplet_ret = 0;
  743. search_bit = 0;
  744. rn = last_rn = 0;
  745. last_device = 0;
  746. last_zero = -1;
  747. desc_bit = 64;
  748. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  749. last_rn = rn;
  750. rn = 0;
  751. /*
  752. * Reset bus and all 1-wire device state machines
  753. * so they can respond to our requests.
  754. *
  755. * Return 0 - device(s) present, 1 - no devices present.
  756. */
  757. mutex_lock(&dev->bus_mutex);
  758. if (w1_reset_bus(dev)) {
  759. mutex_unlock(&dev->bus_mutex);
  760. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  761. break;
  762. }
  763. /* Do fast search on single slave bus */
  764. if (dev->max_slave_count == 1) {
  765. int rv;
  766. w1_write_8(dev, W1_READ_ROM);
  767. rv = w1_read_block(dev, (u8 *)&rn, 8);
  768. mutex_unlock(&dev->bus_mutex);
  769. if (rv == 8 && rn)
  770. cb(dev, rn);
  771. break;
  772. }
  773. /* Start the search */
  774. w1_write_8(dev, search_type);
  775. for (i = 0; i < 64; ++i) {
  776. /* Determine the direction/search bit */
  777. if (i == desc_bit)
  778. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  779. else if (i > desc_bit)
  780. search_bit = 0; /* take the 0 path on the next branch */
  781. else
  782. search_bit = ((last_rn >> i) & 0x1);
  783. /** Read two bits and write one bit */
  784. triplet_ret = w1_triplet(dev, search_bit);
  785. /* quit if no device responded */
  786. if ( (triplet_ret & 0x03) == 0x03 )
  787. break;
  788. /* If both directions were valid, and we took the 0 path... */
  789. if (triplet_ret == 0)
  790. last_zero = i;
  791. /* extract the direction taken & update the device number */
  792. tmp64 = (triplet_ret >> 2);
  793. rn |= (tmp64 << i);
  794. /* ensure we're called from kthread and not by netlink callback */
  795. if (!dev->priv && kthread_should_stop()) {
  796. mutex_unlock(&dev->bus_mutex);
  797. dev_dbg(&dev->dev, "Abort w1_search\n");
  798. return;
  799. }
  800. }
  801. mutex_unlock(&dev->bus_mutex);
  802. if ( (triplet_ret & 0x03) != 0x03 ) {
  803. if ( (desc_bit == last_zero) || (last_zero < 0))
  804. last_device = 1;
  805. desc_bit = last_zero;
  806. cb(dev, rn);
  807. }
  808. }
  809. }
  810. void w1_search_process_cb(struct w1_master *dev, u8 search_type,
  811. w1_slave_found_callback cb)
  812. {
  813. struct w1_slave *sl, *sln;
  814. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  815. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  816. w1_search_devices(dev, search_type, cb);
  817. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  818. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl)
  819. w1_slave_detach(sl);
  820. else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  821. sl->ttl = dev->slave_ttl;
  822. }
  823. if (dev->search_count > 0)
  824. dev->search_count--;
  825. }
  826. static void w1_search_process(struct w1_master *dev, u8 search_type)
  827. {
  828. w1_search_process_cb(dev, search_type, w1_slave_found);
  829. }
  830. int w1_process(void *data)
  831. {
  832. struct w1_master *dev = (struct w1_master *) data;
  833. /* As long as w1_timeout is only set by a module parameter the sleep
  834. * time can be calculated in jiffies once.
  835. */
  836. const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
  837. while (!kthread_should_stop()) {
  838. if (dev->search_count) {
  839. mutex_lock(&dev->mutex);
  840. w1_search_process(dev, W1_SEARCH);
  841. mutex_unlock(&dev->mutex);
  842. }
  843. try_to_freeze();
  844. __set_current_state(TASK_INTERRUPTIBLE);
  845. if (kthread_should_stop())
  846. break;
  847. /* Only sleep when the search is active. */
  848. if (dev->search_count)
  849. schedule_timeout(jtime);
  850. else
  851. schedule();
  852. }
  853. atomic_dec(&dev->refcnt);
  854. return 0;
  855. }
  856. static int __init w1_init(void)
  857. {
  858. int retval;
  859. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  860. w1_init_netlink();
  861. retval = bus_register(&w1_bus_type);
  862. if (retval) {
  863. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  864. goto err_out_exit_init;
  865. }
  866. retval = bus_register_notifier(&w1_bus_type, &w1_bus_nb);
  867. if (retval)
  868. goto err_out_bus_unregister;
  869. retval = driver_register(&w1_master_driver);
  870. if (retval) {
  871. printk(KERN_ERR
  872. "Failed to register master driver. err=%d.\n",
  873. retval);
  874. goto err_out_bus_unregister;
  875. }
  876. retval = driver_register(&w1_slave_driver);
  877. if (retval) {
  878. printk(KERN_ERR
  879. "Failed to register slave driver. err=%d.\n",
  880. retval);
  881. goto err_out_master_unregister;
  882. }
  883. return 0;
  884. #if 0
  885. /* For undoing the slave register if there was a step after it. */
  886. err_out_slave_unregister:
  887. driver_unregister(&w1_slave_driver);
  888. #endif
  889. err_out_master_unregister:
  890. driver_unregister(&w1_master_driver);
  891. err_out_bus_unregister:
  892. bus_unregister(&w1_bus_type);
  893. err_out_exit_init:
  894. return retval;
  895. }
  896. static void __exit w1_fini(void)
  897. {
  898. struct w1_master *dev;
  899. /* Set netlink removal messages and some cleanup */
  900. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  901. __w1_remove_master_device(dev);
  902. w1_fini_netlink();
  903. driver_unregister(&w1_slave_driver);
  904. driver_unregister(&w1_master_driver);
  905. bus_unregister(&w1_bus_type);
  906. }
  907. module_init(w1_init);
  908. module_exit(w1_fini);