w1.c 31 KB

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