w1.c 32 KB

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