w1.c 31 KB

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