w1.c 26 KB

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