w1.c 31 KB

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