w1.c 32 KB

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