w1.c 30 KB

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