w1.c 30 KB

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