w1.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/list.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/timer.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/kthread.h>
  17. #include <linux/freezer.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/of.h>
  20. #include <linux/atomic.h>
  21. #include "w1_internal.h"
  22. #include "w1_netlink.h"
  23. #define W1_FAMILY_DEFAULT 0
  24. static int w1_timeout = 10;
  25. module_param_named(timeout, w1_timeout, int, 0);
  26. MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
  27. static int w1_timeout_us = 0;
  28. module_param_named(timeout_us, w1_timeout_us, int, 0);
  29. MODULE_PARM_DESC(timeout_us,
  30. "time in microseconds between automatic slave searches");
  31. /* A search stops when w1_max_slave_count devices have been found in that
  32. * search. The next search will start over and detect the same set of devices
  33. * on a static 1-wire bus. Memory is not allocated based on this number, just
  34. * on the number of devices known to the kernel. Having a high number does not
  35. * consume additional resources. As a special case, if there is only one
  36. * device on the network and w1_max_slave_count is set to 1, the device id can
  37. * be read directly skipping the normal slower search process.
  38. */
  39. int w1_max_slave_count = 64;
  40. module_param_named(max_slave_count, w1_max_slave_count, int, 0);
  41. MODULE_PARM_DESC(max_slave_count,
  42. "maximum number of slaves detected in a search");
  43. int w1_max_slave_ttl = 10;
  44. module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
  45. MODULE_PARM_DESC(slave_ttl,
  46. "Number of searches not seeing a slave before it will be removed");
  47. DEFINE_MUTEX(w1_mlock);
  48. LIST_HEAD(w1_masters);
  49. static int w1_master_match(struct device *dev, struct device_driver *drv)
  50. {
  51. return 1;
  52. }
  53. static int w1_master_probe(struct device *dev)
  54. {
  55. return -ENODEV;
  56. }
  57. static void w1_master_release(struct device *dev)
  58. {
  59. struct w1_master *md = dev_to_w1_master(dev);
  60. dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
  61. memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
  62. kfree(md);
  63. }
  64. static void w1_slave_release(struct device *dev)
  65. {
  66. struct w1_slave *sl = dev_to_w1_slave(dev);
  67. dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
  68. w1_family_put(sl->family);
  69. sl->master->slave_count--;
  70. }
  71. static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
  72. {
  73. struct w1_slave *sl = dev_to_w1_slave(dev);
  74. return sprintf(buf, "%s\n", sl->name);
  75. }
  76. static DEVICE_ATTR_RO(name);
  77. static ssize_t id_show(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct w1_slave *sl = dev_to_w1_slave(dev);
  81. ssize_t count = sizeof(sl->reg_num);
  82. memcpy(buf, (u8 *)&sl->reg_num, count);
  83. return count;
  84. }
  85. static DEVICE_ATTR_RO(id);
  86. static struct attribute *w1_slave_attrs[] = {
  87. &dev_attr_name.attr,
  88. &dev_attr_id.attr,
  89. NULL,
  90. };
  91. ATTRIBUTE_GROUPS(w1_slave);
  92. /* Default family */
  93. static ssize_t rw_write(struct file *filp, struct kobject *kobj,
  94. struct bin_attribute *bin_attr, char *buf, loff_t off,
  95. size_t count)
  96. {
  97. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  98. mutex_lock(&sl->master->mutex);
  99. if (w1_reset_select_slave(sl)) {
  100. count = 0;
  101. goto out_up;
  102. }
  103. w1_write_block(sl->master, buf, count);
  104. out_up:
  105. mutex_unlock(&sl->master->mutex);
  106. return count;
  107. }
  108. static ssize_t rw_read(struct file *filp, struct kobject *kobj,
  109. struct bin_attribute *bin_attr, char *buf, loff_t off,
  110. size_t count)
  111. {
  112. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  113. mutex_lock(&sl->master->mutex);
  114. w1_read_block(sl->master, buf, count);
  115. mutex_unlock(&sl->master->mutex);
  116. return count;
  117. }
  118. static BIN_ATTR_RW(rw, PAGE_SIZE);
  119. static struct bin_attribute *w1_slave_bin_attrs[] = {
  120. &bin_attr_rw,
  121. NULL,
  122. };
  123. static const struct attribute_group w1_slave_default_group = {
  124. .bin_attrs = w1_slave_bin_attrs,
  125. };
  126. static const struct attribute_group *w1_slave_default_groups[] = {
  127. &w1_slave_default_group,
  128. NULL,
  129. };
  130. static struct w1_family_ops w1_default_fops = {
  131. .groups = w1_slave_default_groups,
  132. };
  133. static struct w1_family w1_default_family = {
  134. .fops = &w1_default_fops,
  135. };
  136. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
  137. static struct bus_type w1_bus_type = {
  138. .name = "w1",
  139. .match = w1_master_match,
  140. .uevent = w1_uevent,
  141. };
  142. struct device_driver w1_master_driver = {
  143. .name = "w1_master_driver",
  144. .bus = &w1_bus_type,
  145. .probe = w1_master_probe,
  146. };
  147. struct device w1_master_device = {
  148. .parent = NULL,
  149. .bus = &w1_bus_type,
  150. .init_name = "w1 bus master",
  151. .driver = &w1_master_driver,
  152. .release = &w1_master_release
  153. };
  154. static struct device_driver w1_slave_driver = {
  155. .name = "w1_slave_driver",
  156. .bus = &w1_bus_type,
  157. };
  158. #if 0
  159. struct device w1_slave_device = {
  160. .parent = NULL,
  161. .bus = &w1_bus_type,
  162. .init_name = "w1 bus slave",
  163. .driver = &w1_slave_driver,
  164. .release = &w1_slave_release
  165. };
  166. #endif /* 0 */
  167. static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  168. {
  169. struct w1_master *md = dev_to_w1_master(dev);
  170. ssize_t count;
  171. mutex_lock(&md->mutex);
  172. count = sprintf(buf, "%s\n", md->name);
  173. mutex_unlock(&md->mutex);
  174. return count;
  175. }
  176. static ssize_t w1_master_attribute_store_search(struct device * dev,
  177. struct device_attribute *attr,
  178. const char * buf, size_t count)
  179. {
  180. long tmp;
  181. struct w1_master *md = dev_to_w1_master(dev);
  182. int ret;
  183. ret = kstrtol(buf, 0, &tmp);
  184. if (ret)
  185. return ret;
  186. mutex_lock(&md->mutex);
  187. md->search_count = tmp;
  188. mutex_unlock(&md->mutex);
  189. /* Only wake if it is going to be searching. */
  190. if (tmp)
  191. wake_up_process(md->thread);
  192. return count;
  193. }
  194. static ssize_t w1_master_attribute_show_search(struct device *dev,
  195. struct device_attribute *attr,
  196. char *buf)
  197. {
  198. struct w1_master *md = dev_to_w1_master(dev);
  199. ssize_t count;
  200. mutex_lock(&md->mutex);
  201. count = sprintf(buf, "%d\n", md->search_count);
  202. mutex_unlock(&md->mutex);
  203. return count;
  204. }
  205. static ssize_t w1_master_attribute_store_pullup(struct device *dev,
  206. struct device_attribute *attr,
  207. const char *buf, size_t count)
  208. {
  209. long tmp;
  210. struct w1_master *md = dev_to_w1_master(dev);
  211. int ret;
  212. ret = kstrtol(buf, 0, &tmp);
  213. if (ret)
  214. return ret;
  215. mutex_lock(&md->mutex);
  216. md->enable_pullup = tmp;
  217. mutex_unlock(&md->mutex);
  218. return count;
  219. }
  220. static ssize_t w1_master_attribute_show_pullup(struct device *dev,
  221. struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct w1_master *md = dev_to_w1_master(dev);
  225. ssize_t count;
  226. mutex_lock(&md->mutex);
  227. count = sprintf(buf, "%d\n", md->enable_pullup);
  228. mutex_unlock(&md->mutex);
  229. return count;
  230. }
  231. static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, 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, "0x%p\n", md->bus_master);
  237. mutex_unlock(&md->mutex);
  238. return count;
  239. }
  240. static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  241. {
  242. ssize_t count;
  243. count = sprintf(buf, "%d\n", w1_timeout);
  244. return count;
  245. }
  246. static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
  247. struct device_attribute *attr, char *buf)
  248. {
  249. ssize_t count;
  250. count = sprintf(buf, "%d\n", w1_timeout_us);
  251. return count;
  252. }
  253. static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
  254. struct device_attribute *attr, const char *buf, size_t count)
  255. {
  256. int tmp;
  257. struct w1_master *md = dev_to_w1_master(dev);
  258. if (kstrtoint(buf, 0, &tmp) || tmp < 1)
  259. return -EINVAL;
  260. mutex_lock(&md->mutex);
  261. md->max_slave_count = tmp;
  262. /* allow each time the max_slave_count is updated */
  263. clear_bit(W1_WARN_MAX_COUNT, &md->flags);
  264. mutex_unlock(&md->mutex);
  265. return count;
  266. }
  267. static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  268. {
  269. struct w1_master *md = dev_to_w1_master(dev);
  270. ssize_t count;
  271. mutex_lock(&md->mutex);
  272. count = sprintf(buf, "%d\n", md->max_slave_count);
  273. mutex_unlock(&md->mutex);
  274. return count;
  275. }
  276. static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
  277. {
  278. struct w1_master *md = dev_to_w1_master(dev);
  279. ssize_t count;
  280. mutex_lock(&md->mutex);
  281. count = sprintf(buf, "%lu\n", md->attempts);
  282. mutex_unlock(&md->mutex);
  283. return count;
  284. }
  285. static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  286. {
  287. struct w1_master *md = dev_to_w1_master(dev);
  288. ssize_t count;
  289. mutex_lock(&md->mutex);
  290. count = sprintf(buf, "%d\n", md->slave_count);
  291. mutex_unlock(&md->mutex);
  292. return count;
  293. }
  294. static ssize_t w1_master_attribute_show_slaves(struct device *dev,
  295. struct device_attribute *attr, char *buf)
  296. {
  297. struct w1_master *md = dev_to_w1_master(dev);
  298. int c = PAGE_SIZE;
  299. struct list_head *ent, *n;
  300. struct w1_slave *sl = NULL;
  301. mutex_lock(&md->list_mutex);
  302. list_for_each_safe(ent, n, &md->slist) {
  303. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  304. c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
  305. }
  306. if (!sl)
  307. c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
  308. mutex_unlock(&md->list_mutex);
  309. return PAGE_SIZE - c;
  310. }
  311. static ssize_t w1_master_attribute_show_add(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. int c = PAGE_SIZE;
  315. c -= snprintf(buf+PAGE_SIZE - c, c,
  316. "write device id xx-xxxxxxxxxxxx to add slave\n");
  317. return PAGE_SIZE - c;
  318. }
  319. static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
  320. struct w1_reg_num *rn)
  321. {
  322. unsigned int family;
  323. unsigned long long id;
  324. int i;
  325. u64 rn64_le;
  326. /* The CRC value isn't read from the user because the sysfs directory
  327. * doesn't include it and most messages from the bus search don't
  328. * print it either. It would be unreasonable for the user to then
  329. * provide it.
  330. */
  331. const char *error_msg = "bad slave string format, expecting "
  332. "ff-dddddddddddd\n";
  333. if (buf[2] != '-') {
  334. dev_err(dev, "%s", error_msg);
  335. return -EINVAL;
  336. }
  337. i = sscanf(buf, "%02x-%012llx", &family, &id);
  338. if (i != 2) {
  339. dev_err(dev, "%s", error_msg);
  340. return -EINVAL;
  341. }
  342. rn->family = family;
  343. rn->id = id;
  344. rn64_le = cpu_to_le64(*(u64 *)rn);
  345. rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
  346. #if 0
  347. dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
  348. rn->family, (unsigned long long)rn->id, rn->crc);
  349. #endif
  350. return 0;
  351. }
  352. /* Searches the slaves in the w1_master and returns a pointer or NULL.
  353. * Note: must not hold list_mutex
  354. */
  355. struct w1_slave *w1_slave_search_device(struct w1_master *dev,
  356. struct w1_reg_num *rn)
  357. {
  358. struct w1_slave *sl;
  359. mutex_lock(&dev->list_mutex);
  360. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  361. if (sl->reg_num.family == rn->family &&
  362. sl->reg_num.id == rn->id &&
  363. sl->reg_num.crc == rn->crc) {
  364. mutex_unlock(&dev->list_mutex);
  365. return sl;
  366. }
  367. }
  368. mutex_unlock(&dev->list_mutex);
  369. return NULL;
  370. }
  371. static ssize_t w1_master_attribute_store_add(struct device *dev,
  372. struct device_attribute *attr,
  373. const char *buf, size_t count)
  374. {
  375. struct w1_master *md = dev_to_w1_master(dev);
  376. struct w1_reg_num rn;
  377. struct w1_slave *sl;
  378. ssize_t result = count;
  379. if (w1_atoreg_num(dev, buf, count, &rn))
  380. return -EINVAL;
  381. mutex_lock(&md->mutex);
  382. sl = w1_slave_search_device(md, &rn);
  383. /* It would be nice to do a targeted search one the one-wire bus
  384. * for the new device to see if it is out there or not. But the
  385. * current search doesn't support that.
  386. */
  387. if (sl) {
  388. dev_info(dev, "Device %s already exists\n", sl->name);
  389. result = -EINVAL;
  390. } else {
  391. w1_attach_slave_device(md, &rn);
  392. }
  393. mutex_unlock(&md->mutex);
  394. return result;
  395. }
  396. static ssize_t w1_master_attribute_show_remove(struct device *dev,
  397. struct device_attribute *attr, char *buf)
  398. {
  399. int c = PAGE_SIZE;
  400. c -= snprintf(buf+PAGE_SIZE - c, c,
  401. "write device id xx-xxxxxxxxxxxx to remove slave\n");
  402. return PAGE_SIZE - c;
  403. }
  404. static ssize_t w1_master_attribute_store_remove(struct device *dev,
  405. struct device_attribute *attr,
  406. const char *buf, size_t count)
  407. {
  408. struct w1_master *md = dev_to_w1_master(dev);
  409. struct w1_reg_num rn;
  410. struct w1_slave *sl;
  411. ssize_t result = count;
  412. if (w1_atoreg_num(dev, buf, count, &rn))
  413. return -EINVAL;
  414. mutex_lock(&md->mutex);
  415. sl = w1_slave_search_device(md, &rn);
  416. if (sl) {
  417. result = w1_slave_detach(sl);
  418. /* refcnt 0 means it was detached in the call */
  419. if (result == 0)
  420. result = count;
  421. } else {
  422. dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
  423. (unsigned long long)rn.id);
  424. result = -EINVAL;
  425. }
  426. mutex_unlock(&md->mutex);
  427. return result;
  428. }
  429. #define W1_MASTER_ATTR_RO(_name, _mode) \
  430. struct device_attribute w1_master_attribute_##_name = \
  431. __ATTR(w1_master_##_name, _mode, \
  432. w1_master_attribute_show_##_name, NULL)
  433. #define W1_MASTER_ATTR_RW(_name, _mode) \
  434. struct device_attribute w1_master_attribute_##_name = \
  435. __ATTR(w1_master_##_name, _mode, \
  436. w1_master_attribute_show_##_name, \
  437. w1_master_attribute_store_##_name)
  438. static W1_MASTER_ATTR_RO(name, S_IRUGO);
  439. static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
  440. static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
  441. static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
  442. static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
  443. static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
  444. static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
  445. static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
  446. static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
  447. static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
  448. static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
  449. static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
  450. static struct attribute *w1_master_default_attrs[] = {
  451. &w1_master_attribute_name.attr,
  452. &w1_master_attribute_slaves.attr,
  453. &w1_master_attribute_slave_count.attr,
  454. &w1_master_attribute_max_slave_count.attr,
  455. &w1_master_attribute_attempts.attr,
  456. &w1_master_attribute_timeout.attr,
  457. &w1_master_attribute_timeout_us.attr,
  458. &w1_master_attribute_pointer.attr,
  459. &w1_master_attribute_search.attr,
  460. &w1_master_attribute_pullup.attr,
  461. &w1_master_attribute_add.attr,
  462. &w1_master_attribute_remove.attr,
  463. NULL
  464. };
  465. static const struct attribute_group w1_master_defattr_group = {
  466. .attrs = w1_master_default_attrs,
  467. };
  468. int w1_create_master_attributes(struct w1_master *master)
  469. {
  470. return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
  471. }
  472. void w1_destroy_master_attributes(struct w1_master *master)
  473. {
  474. sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
  475. }
  476. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
  477. {
  478. struct w1_master *md = NULL;
  479. struct w1_slave *sl = NULL;
  480. char *event_owner, *name;
  481. int err = 0;
  482. if (dev->driver == &w1_master_driver) {
  483. md = container_of(dev, struct w1_master, dev);
  484. event_owner = "master";
  485. name = md->name;
  486. } else if (dev->driver == &w1_slave_driver) {
  487. sl = container_of(dev, struct w1_slave, dev);
  488. event_owner = "slave";
  489. name = sl->name;
  490. } else {
  491. dev_dbg(dev, "Unknown event.\n");
  492. return -EINVAL;
  493. }
  494. dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
  495. event_owner, name, dev_name(dev));
  496. if (dev->driver != &w1_slave_driver || !sl)
  497. goto end;
  498. err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
  499. if (err)
  500. goto end;
  501. err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
  502. (unsigned long long)sl->reg_num.id);
  503. end:
  504. return err;
  505. }
  506. static int w1_family_notify(unsigned long action, struct w1_slave *sl)
  507. {
  508. struct w1_family_ops *fops;
  509. int err;
  510. fops = sl->family->fops;
  511. if (!fops)
  512. return 0;
  513. switch (action) {
  514. case BUS_NOTIFY_ADD_DEVICE:
  515. /* if the family driver needs to initialize something... */
  516. if (fops->add_slave) {
  517. err = fops->add_slave(sl);
  518. if (err < 0) {
  519. dev_err(&sl->dev,
  520. "add_slave() call failed. err=%d\n",
  521. err);
  522. return err;
  523. }
  524. }
  525. if (fops->groups) {
  526. err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
  527. if (err) {
  528. dev_err(&sl->dev,
  529. "sysfs group creation failed. err=%d\n",
  530. err);
  531. return err;
  532. }
  533. }
  534. if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info) {
  535. struct device *hwmon
  536. = hwmon_device_register_with_info(&sl->dev,
  537. "w1_slave_temp", sl,
  538. fops->chip_info,
  539. NULL);
  540. if (IS_ERR(hwmon)) {
  541. dev_warn(&sl->dev,
  542. "could not create hwmon device\n");
  543. } else {
  544. sl->hwmon = hwmon;
  545. }
  546. }
  547. break;
  548. case BUS_NOTIFY_DEL_DEVICE:
  549. if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info &&
  550. sl->hwmon)
  551. hwmon_device_unregister(sl->hwmon);
  552. if (fops->remove_slave)
  553. sl->family->fops->remove_slave(sl);
  554. if (fops->groups)
  555. sysfs_remove_groups(&sl->dev.kobj, fops->groups);
  556. break;
  557. }
  558. return 0;
  559. }
  560. static int __w1_attach_slave_device(struct w1_slave *sl)
  561. {
  562. int err;
  563. sl->dev.parent = &sl->master->dev;
  564. sl->dev.driver = &w1_slave_driver;
  565. sl->dev.bus = &w1_bus_type;
  566. sl->dev.release = &w1_slave_release;
  567. sl->dev.groups = w1_slave_groups;
  568. sl->dev.of_node = of_find_matching_node(sl->master->dev.of_node,
  569. sl->family->of_match_table);
  570. dev_set_name(&sl->dev, "%02x-%012llx",
  571. (unsigned int) sl->reg_num.family,
  572. (unsigned long long) sl->reg_num.id);
  573. snprintf(&sl->name[0], sizeof(sl->name),
  574. "%02x-%012llx",
  575. (unsigned int) sl->reg_num.family,
  576. (unsigned long long) sl->reg_num.id);
  577. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
  578. dev_name(&sl->dev), sl);
  579. /* suppress for w1_family_notify before sending KOBJ_ADD */
  580. dev_set_uevent_suppress(&sl->dev, true);
  581. err = device_register(&sl->dev);
  582. if (err < 0) {
  583. dev_err(&sl->dev,
  584. "Device registration [%s] failed. err=%d\n",
  585. dev_name(&sl->dev), err);
  586. put_device(&sl->dev);
  587. return err;
  588. }
  589. w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
  590. dev_set_uevent_suppress(&sl->dev, false);
  591. kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
  592. mutex_lock(&sl->master->list_mutex);
  593. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  594. mutex_unlock(&sl->master->list_mutex);
  595. return 0;
  596. }
  597. int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  598. {
  599. struct w1_slave *sl;
  600. struct w1_family *f;
  601. int err;
  602. struct w1_netlink_msg msg;
  603. sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
  604. if (!sl) {
  605. dev_err(&dev->dev,
  606. "%s: failed to allocate new slave device.\n",
  607. __func__);
  608. return -ENOMEM;
  609. }
  610. sl->owner = THIS_MODULE;
  611. sl->master = dev;
  612. set_bit(W1_SLAVE_ACTIVE, &sl->flags);
  613. memset(&msg, 0, sizeof(msg));
  614. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  615. atomic_set(&sl->refcnt, 1);
  616. atomic_inc(&sl->master->refcnt);
  617. dev->slave_count++;
  618. dev_info(&dev->dev, "Attaching one wire slave %02x.%012llx crc %02x\n",
  619. rn->family, (unsigned long long)rn->id, rn->crc);
  620. /* slave modules need to be loaded in a context with unlocked mutex */
  621. mutex_unlock(&dev->mutex);
  622. request_module("w1-family-0x%02X", rn->family);
  623. mutex_lock(&dev->mutex);
  624. spin_lock(&w1_flock);
  625. f = w1_family_registered(rn->family);
  626. if (!f) {
  627. f= &w1_default_family;
  628. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  629. rn->family, rn->family,
  630. (unsigned long long)rn->id, rn->crc);
  631. }
  632. __w1_family_get(f);
  633. spin_unlock(&w1_flock);
  634. sl->family = f;
  635. err = __w1_attach_slave_device(sl);
  636. if (err < 0) {
  637. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  638. sl->name);
  639. dev->slave_count--;
  640. w1_family_put(sl->family);
  641. atomic_dec(&sl->master->refcnt);
  642. kfree(sl);
  643. return err;
  644. }
  645. sl->ttl = dev->slave_ttl;
  646. memcpy(msg.id.id, rn, sizeof(msg.id));
  647. msg.type = W1_SLAVE_ADD;
  648. w1_netlink_send(dev, &msg);
  649. return 0;
  650. }
  651. int w1_unref_slave(struct w1_slave *sl)
  652. {
  653. struct w1_master *dev = sl->master;
  654. int refcnt;
  655. mutex_lock(&dev->list_mutex);
  656. refcnt = atomic_sub_return(1, &sl->refcnt);
  657. if (refcnt == 0) {
  658. struct w1_netlink_msg msg;
  659. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
  660. sl->name, sl);
  661. list_del(&sl->w1_slave_entry);
  662. memset(&msg, 0, sizeof(msg));
  663. memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
  664. msg.type = W1_SLAVE_REMOVE;
  665. w1_netlink_send(sl->master, &msg);
  666. w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
  667. device_unregister(&sl->dev);
  668. #ifdef DEBUG
  669. memset(sl, 0, sizeof(*sl));
  670. #endif
  671. kfree(sl);
  672. }
  673. atomic_dec(&dev->refcnt);
  674. mutex_unlock(&dev->list_mutex);
  675. return refcnt;
  676. }
  677. int w1_slave_detach(struct w1_slave *sl)
  678. {
  679. /* Only detach a slave once as it decreases the refcnt each time. */
  680. int destroy_now;
  681. mutex_lock(&sl->master->list_mutex);
  682. destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
  683. set_bit(W1_SLAVE_DETACH, &sl->flags);
  684. mutex_unlock(&sl->master->list_mutex);
  685. if (destroy_now)
  686. destroy_now = !w1_unref_slave(sl);
  687. return destroy_now ? 0 : -EBUSY;
  688. }
  689. struct w1_master *w1_search_master_id(u32 id)
  690. {
  691. struct w1_master *dev;
  692. int found = 0;
  693. mutex_lock(&w1_mlock);
  694. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  695. if (dev->id == id) {
  696. found = 1;
  697. atomic_inc(&dev->refcnt);
  698. break;
  699. }
  700. }
  701. mutex_unlock(&w1_mlock);
  702. return (found)?dev:NULL;
  703. }
  704. struct w1_slave *w1_search_slave(struct w1_reg_num *id)
  705. {
  706. struct w1_master *dev;
  707. struct w1_slave *sl = NULL;
  708. int found = 0;
  709. mutex_lock(&w1_mlock);
  710. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  711. mutex_lock(&dev->list_mutex);
  712. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  713. if (sl->reg_num.family == id->family &&
  714. sl->reg_num.id == id->id &&
  715. sl->reg_num.crc == id->crc) {
  716. found = 1;
  717. atomic_inc(&dev->refcnt);
  718. atomic_inc(&sl->refcnt);
  719. break;
  720. }
  721. }
  722. mutex_unlock(&dev->list_mutex);
  723. if (found)
  724. break;
  725. }
  726. mutex_unlock(&w1_mlock);
  727. return (found)?sl:NULL;
  728. }
  729. void w1_reconnect_slaves(struct w1_family *f, int attach)
  730. {
  731. struct w1_slave *sl, *sln;
  732. struct w1_master *dev;
  733. mutex_lock(&w1_mlock);
  734. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  735. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  736. "for family %02x.\n", dev->name, f->fid);
  737. mutex_lock(&dev->mutex);
  738. mutex_lock(&dev->list_mutex);
  739. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  740. /* If it is a new family, slaves with the default
  741. * family driver and are that family will be
  742. * connected. If the family is going away, devices
  743. * matching that family are reconneced.
  744. */
  745. if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
  746. && sl->reg_num.family == f->fid) ||
  747. (!attach && sl->family->fid == f->fid)) {
  748. struct w1_reg_num rn;
  749. mutex_unlock(&dev->list_mutex);
  750. memcpy(&rn, &sl->reg_num, sizeof(rn));
  751. /* If it was already in use let the automatic
  752. * scan pick it up again later.
  753. */
  754. if (!w1_slave_detach(sl))
  755. w1_attach_slave_device(dev, &rn);
  756. mutex_lock(&dev->list_mutex);
  757. }
  758. }
  759. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  760. "has been finished.\n", dev->name);
  761. mutex_unlock(&dev->list_mutex);
  762. mutex_unlock(&dev->mutex);
  763. }
  764. mutex_unlock(&w1_mlock);
  765. }
  766. void w1_slave_found(struct w1_master *dev, u64 rn)
  767. {
  768. struct w1_slave *sl;
  769. struct w1_reg_num *tmp;
  770. u64 rn_le = cpu_to_le64(rn);
  771. atomic_inc(&dev->refcnt);
  772. tmp = (struct w1_reg_num *) &rn;
  773. sl = w1_slave_search_device(dev, tmp);
  774. if (sl) {
  775. set_bit(W1_SLAVE_ACTIVE, &sl->flags);
  776. } else {
  777. if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
  778. w1_attach_slave_device(dev, tmp);
  779. }
  780. atomic_dec(&dev->refcnt);
  781. }
  782. /**
  783. * w1_search() - Performs a ROM Search & registers any devices found.
  784. * @dev: The master device to search
  785. * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
  786. * to return only devices in the alarmed state
  787. * @cb: Function to call when a device is found
  788. *
  789. * The 1-wire search is a simple binary tree search.
  790. * For each bit of the address, we read two bits and write one bit.
  791. * The bit written will put to sleep all devies that don't match that bit.
  792. * When the two reads differ, the direction choice is obvious.
  793. * When both bits are 0, we must choose a path to take.
  794. * When we can scan all 64 bits without having to choose a path, we are done.
  795. *
  796. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  797. *
  798. */
  799. void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
  800. {
  801. u64 last_rn, rn, tmp64;
  802. int i, slave_count = 0;
  803. int last_zero, last_device;
  804. int search_bit, desc_bit;
  805. u8 triplet_ret = 0;
  806. search_bit = 0;
  807. rn = dev->search_id;
  808. last_rn = 0;
  809. last_device = 0;
  810. last_zero = -1;
  811. desc_bit = 64;
  812. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  813. last_rn = rn;
  814. rn = 0;
  815. /*
  816. * Reset bus and all 1-wire device state machines
  817. * so they can respond to our requests.
  818. *
  819. * Return 0 - device(s) present, 1 - no devices present.
  820. */
  821. mutex_lock(&dev->bus_mutex);
  822. if (w1_reset_bus(dev)) {
  823. mutex_unlock(&dev->bus_mutex);
  824. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  825. break;
  826. }
  827. /* Do fast search on single slave bus */
  828. if (dev->max_slave_count == 1) {
  829. int rv;
  830. w1_write_8(dev, W1_READ_ROM);
  831. rv = w1_read_block(dev, (u8 *)&rn, 8);
  832. mutex_unlock(&dev->bus_mutex);
  833. if (rv == 8 && rn)
  834. cb(dev, rn);
  835. break;
  836. }
  837. /* Start the search */
  838. w1_write_8(dev, search_type);
  839. for (i = 0; i < 64; ++i) {
  840. /* Determine the direction/search bit */
  841. if (i == desc_bit)
  842. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  843. else if (i > desc_bit)
  844. search_bit = 0; /* take the 0 path on the next branch */
  845. else
  846. search_bit = ((last_rn >> i) & 0x1);
  847. /* Read two bits and write one bit */
  848. triplet_ret = w1_triplet(dev, search_bit);
  849. /* quit if no device responded */
  850. if ( (triplet_ret & 0x03) == 0x03 )
  851. break;
  852. /* If both directions were valid, and we took the 0 path... */
  853. if (triplet_ret == 0)
  854. last_zero = i;
  855. /* extract the direction taken & update the device number */
  856. tmp64 = (triplet_ret >> 2);
  857. rn |= (tmp64 << i);
  858. if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
  859. mutex_unlock(&dev->bus_mutex);
  860. dev_dbg(&dev->dev, "Abort w1_search\n");
  861. return;
  862. }
  863. }
  864. mutex_unlock(&dev->bus_mutex);
  865. if ( (triplet_ret & 0x03) != 0x03 ) {
  866. if ((desc_bit == last_zero) || (last_zero < 0)) {
  867. last_device = 1;
  868. dev->search_id = 0;
  869. } else {
  870. dev->search_id = rn;
  871. }
  872. desc_bit = last_zero;
  873. cb(dev, rn);
  874. }
  875. if (!last_device && slave_count == dev->max_slave_count &&
  876. !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
  877. /* Only max_slave_count will be scanned in a search,
  878. * but it will start where it left off next search
  879. * until all ids are identified and then it will start
  880. * over. A continued search will report the previous
  881. * last id as the first id (provided it is still on the
  882. * bus).
  883. */
  884. dev_info(&dev->dev, "%s: max_slave_count %d reached, "
  885. "will continue next search.\n", __func__,
  886. dev->max_slave_count);
  887. set_bit(W1_WARN_MAX_COUNT, &dev->flags);
  888. }
  889. }
  890. }
  891. void w1_search_process_cb(struct w1_master *dev, u8 search_type,
  892. w1_slave_found_callback cb)
  893. {
  894. struct w1_slave *sl, *sln;
  895. mutex_lock(&dev->list_mutex);
  896. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  897. clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
  898. mutex_unlock(&dev->list_mutex);
  899. w1_search_devices(dev, search_type, cb);
  900. mutex_lock(&dev->list_mutex);
  901. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  902. if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
  903. mutex_unlock(&dev->list_mutex);
  904. w1_slave_detach(sl);
  905. mutex_lock(&dev->list_mutex);
  906. }
  907. else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
  908. sl->ttl = dev->slave_ttl;
  909. }
  910. mutex_unlock(&dev->list_mutex);
  911. if (dev->search_count > 0)
  912. dev->search_count--;
  913. }
  914. static void w1_search_process(struct w1_master *dev, u8 search_type)
  915. {
  916. w1_search_process_cb(dev, search_type, w1_slave_found);
  917. }
  918. /**
  919. * w1_process_callbacks() - execute each dev->async_list callback entry
  920. * @dev: w1_master device
  921. *
  922. * The w1 master list_mutex must be held.
  923. *
  924. * Return: 1 if there were commands to executed 0 otherwise
  925. */
  926. int w1_process_callbacks(struct w1_master *dev)
  927. {
  928. int ret = 0;
  929. struct w1_async_cmd *async_cmd, *async_n;
  930. /* The list can be added to in another thread, loop until it is empty */
  931. while (!list_empty(&dev->async_list)) {
  932. list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
  933. async_entry) {
  934. /* drop the lock, if it is a search it can take a long
  935. * time */
  936. mutex_unlock(&dev->list_mutex);
  937. async_cmd->cb(dev, async_cmd);
  938. ret = 1;
  939. mutex_lock(&dev->list_mutex);
  940. }
  941. }
  942. return ret;
  943. }
  944. int w1_process(void *data)
  945. {
  946. struct w1_master *dev = (struct w1_master *) data;
  947. /* As long as w1_timeout is only set by a module parameter the sleep
  948. * time can be calculated in jiffies once.
  949. */
  950. const unsigned long jtime =
  951. usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
  952. /* remainder if it woke up early */
  953. unsigned long jremain = 0;
  954. for (;;) {
  955. if (!jremain && dev->search_count) {
  956. mutex_lock(&dev->mutex);
  957. w1_search_process(dev, W1_SEARCH);
  958. mutex_unlock(&dev->mutex);
  959. }
  960. mutex_lock(&dev->list_mutex);
  961. /* Note, w1_process_callback drops the lock while processing,
  962. * but locks it again before returning.
  963. */
  964. if (!w1_process_callbacks(dev) && jremain) {
  965. /* a wake up is either to stop the thread, process
  966. * callbacks, or search, it isn't process callbacks, so
  967. * schedule a search.
  968. */
  969. jremain = 1;
  970. }
  971. __set_current_state(TASK_INTERRUPTIBLE);
  972. /* hold list_mutex until after interruptible to prevent loosing
  973. * the wakeup signal when async_cmd is added.
  974. */
  975. mutex_unlock(&dev->list_mutex);
  976. if (kthread_should_stop())
  977. break;
  978. /* Only sleep when the search is active. */
  979. if (dev->search_count) {
  980. if (!jremain)
  981. jremain = jtime;
  982. jremain = schedule_timeout(jremain);
  983. }
  984. else
  985. schedule();
  986. }
  987. atomic_dec(&dev->refcnt);
  988. return 0;
  989. }
  990. static int __init w1_init(void)
  991. {
  992. int retval;
  993. pr_info("Driver for 1-wire Dallas network protocol.\n");
  994. w1_init_netlink();
  995. retval = bus_register(&w1_bus_type);
  996. if (retval) {
  997. pr_err("Failed to register bus. err=%d.\n", retval);
  998. goto err_out_exit_init;
  999. }
  1000. retval = driver_register(&w1_master_driver);
  1001. if (retval) {
  1002. pr_err("Failed to register master driver. err=%d.\n",
  1003. retval);
  1004. goto err_out_bus_unregister;
  1005. }
  1006. retval = driver_register(&w1_slave_driver);
  1007. if (retval) {
  1008. pr_err("Failed to register slave driver. err=%d.\n",
  1009. retval);
  1010. goto err_out_master_unregister;
  1011. }
  1012. return 0;
  1013. #if 0
  1014. /* For undoing the slave register if there was a step after it. */
  1015. err_out_slave_unregister:
  1016. driver_unregister(&w1_slave_driver);
  1017. #endif
  1018. err_out_master_unregister:
  1019. driver_unregister(&w1_master_driver);
  1020. err_out_bus_unregister:
  1021. bus_unregister(&w1_bus_type);
  1022. err_out_exit_init:
  1023. return retval;
  1024. }
  1025. static void __exit w1_fini(void)
  1026. {
  1027. struct w1_master *dev;
  1028. /* Set netlink removal messages and some cleanup */
  1029. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  1030. __w1_remove_master_device(dev);
  1031. w1_fini_netlink();
  1032. driver_unregister(&w1_slave_driver);
  1033. driver_unregister(&w1_master_driver);
  1034. bus_unregister(&w1_bus_type);
  1035. }
  1036. module_init(w1_init);
  1037. module_exit(w1_fini);
  1038. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  1039. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
  1040. MODULE_LICENSE("GPL");