w1.c 31 KB

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