w1.c 30 KB

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