w1.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /*
  2. * w1.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  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 <asm/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 <johnpol@2ka.mipt.ru>");
  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. printk("%s: Releasing %s.\n", __func__, sl->name);
  71. while (atomic_read(&sl->refcnt)) {
  72. printk("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 w1_slave_read_name(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 ssize_t w1_slave_read_id(struct kobject *kobj,
  87. struct bin_attribute *bin_attr,
  88. char *buf, loff_t off, size_t count)
  89. {
  90. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  91. if (off > 8) {
  92. count = 0;
  93. } else {
  94. if (off + count > 8)
  95. count = 8 - off;
  96. memcpy(buf, (u8 *)&sl->reg_num, count);
  97. }
  98. return count;
  99. }
  100. static struct device_attribute w1_slave_attr_name =
  101. __ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
  102. static struct bin_attribute w1_slave_attr_bin_id = {
  103. .attr = {
  104. .name = "id",
  105. .mode = S_IRUGO,
  106. },
  107. .size = 8,
  108. .read = w1_slave_read_id,
  109. };
  110. /* Default family */
  111. static ssize_t w1_default_write(struct kobject *kobj,
  112. struct bin_attribute *bin_attr,
  113. char *buf, loff_t off, size_t count)
  114. {
  115. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  116. mutex_lock(&sl->master->mutex);
  117. if (w1_reset_select_slave(sl)) {
  118. count = 0;
  119. goto out_up;
  120. }
  121. w1_write_block(sl->master, buf, count);
  122. out_up:
  123. mutex_unlock(&sl->master->mutex);
  124. return count;
  125. }
  126. static ssize_t w1_default_read(struct kobject *kobj,
  127. struct bin_attribute *bin_attr,
  128. char *buf, loff_t off, size_t count)
  129. {
  130. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  131. mutex_lock(&sl->master->mutex);
  132. w1_read_block(sl->master, buf, count);
  133. mutex_unlock(&sl->master->mutex);
  134. return count;
  135. }
  136. static struct bin_attribute w1_default_attr = {
  137. .attr = {
  138. .name = "rw",
  139. .mode = S_IRUGO | S_IWUSR,
  140. },
  141. .size = PAGE_SIZE,
  142. .read = w1_default_read,
  143. .write = w1_default_write,
  144. };
  145. static int w1_default_add_slave(struct w1_slave *sl)
  146. {
  147. return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr);
  148. }
  149. static void w1_default_remove_slave(struct w1_slave *sl)
  150. {
  151. sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr);
  152. }
  153. static struct w1_family_ops w1_default_fops = {
  154. .add_slave = w1_default_add_slave,
  155. .remove_slave = w1_default_remove_slave,
  156. };
  157. static struct w1_family w1_default_family = {
  158. .fops = &w1_default_fops,
  159. };
  160. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
  161. static struct bus_type w1_bus_type = {
  162. .name = "w1",
  163. .match = w1_master_match,
  164. .uevent = w1_uevent,
  165. };
  166. struct device_driver w1_master_driver = {
  167. .name = "w1_master_driver",
  168. .bus = &w1_bus_type,
  169. .probe = w1_master_probe,
  170. };
  171. struct device w1_master_device = {
  172. .parent = NULL,
  173. .bus = &w1_bus_type,
  174. .bus_id = "w1 bus master",
  175. .driver = &w1_master_driver,
  176. .release = &w1_master_release
  177. };
  178. static struct device_driver w1_slave_driver = {
  179. .name = "w1_slave_driver",
  180. .bus = &w1_bus_type,
  181. };
  182. #if 0
  183. struct device w1_slave_device = {
  184. .parent = NULL,
  185. .bus = &w1_bus_type,
  186. .bus_id = "w1 bus slave",
  187. .driver = &w1_slave_driver,
  188. .release = &w1_slave_release
  189. };
  190. #endif /* 0 */
  191. static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  192. {
  193. struct w1_master *md = dev_to_w1_master(dev);
  194. ssize_t count;
  195. mutex_lock(&md->mutex);
  196. count = sprintf(buf, "%s\n", md->name);
  197. mutex_unlock(&md->mutex);
  198. return count;
  199. }
  200. static ssize_t w1_master_attribute_store_search(struct device * dev,
  201. struct device_attribute *attr,
  202. const char * buf, size_t count)
  203. {
  204. long tmp;
  205. struct w1_master *md = dev_to_w1_master(dev);
  206. if (strict_strtol(buf, 0, &tmp) == -EINVAL)
  207. return -EINVAL;
  208. mutex_lock(&md->mutex);
  209. md->search_count = tmp;
  210. mutex_unlock(&md->mutex);
  211. wake_up_process(md->thread);
  212. return count;
  213. }
  214. static ssize_t w1_master_attribute_show_search(struct device *dev,
  215. struct device_attribute *attr,
  216. char *buf)
  217. {
  218. struct w1_master *md = dev_to_w1_master(dev);
  219. ssize_t count;
  220. mutex_lock(&md->mutex);
  221. count = sprintf(buf, "%d\n", md->search_count);
  222. mutex_unlock(&md->mutex);
  223. return count;
  224. }
  225. static ssize_t w1_master_attribute_store_pullup(struct device *dev,
  226. struct device_attribute *attr,
  227. const char *buf, size_t count)
  228. {
  229. long tmp;
  230. struct w1_master *md = dev_to_w1_master(dev);
  231. if (strict_strtol(buf, 0, &tmp) == -EINVAL)
  232. return -EINVAL;
  233. mutex_lock(&md->mutex);
  234. md->enable_pullup = tmp;
  235. mutex_unlock(&md->mutex);
  236. wake_up_process(md->thread);
  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_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  266. {
  267. struct w1_master *md = dev_to_w1_master(dev);
  268. ssize_t count;
  269. mutex_lock(&md->mutex);
  270. count = sprintf(buf, "%d\n", md->max_slave_count);
  271. mutex_unlock(&md->mutex);
  272. return count;
  273. }
  274. static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
  275. {
  276. struct w1_master *md = dev_to_w1_master(dev);
  277. ssize_t count;
  278. mutex_lock(&md->mutex);
  279. count = sprintf(buf, "%lu\n", md->attempts);
  280. mutex_unlock(&md->mutex);
  281. return count;
  282. }
  283. static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  284. {
  285. struct w1_master *md = dev_to_w1_master(dev);
  286. ssize_t count;
  287. mutex_lock(&md->mutex);
  288. count = sprintf(buf, "%d\n", md->slave_count);
  289. mutex_unlock(&md->mutex);
  290. return count;
  291. }
  292. static ssize_t w1_master_attribute_show_slaves(struct device *dev,
  293. struct device_attribute *attr, char *buf)
  294. {
  295. struct w1_master *md = dev_to_w1_master(dev);
  296. int c = PAGE_SIZE;
  297. mutex_lock(&md->mutex);
  298. if (md->slave_count == 0)
  299. c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
  300. else {
  301. struct list_head *ent, *n;
  302. struct w1_slave *sl;
  303. list_for_each_safe(ent, n, &md->slist) {
  304. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  305. c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
  306. }
  307. }
  308. mutex_unlock(&md->mutex);
  309. return PAGE_SIZE - c;
  310. }
  311. static ssize_t w1_master_attribute_show_add(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. int c = PAGE_SIZE;
  315. c -= snprintf(buf+PAGE_SIZE - c, c,
  316. "write device id xx-xxxxxxxxxxxx to add slave\n");
  317. return PAGE_SIZE - c;
  318. }
  319. static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
  320. struct w1_reg_num *rn)
  321. {
  322. unsigned int family;
  323. unsigned long long id;
  324. int i;
  325. u64 rn64_le;
  326. /* The CRC value isn't read from the user because the sysfs directory
  327. * doesn't include it and most messages from the bus search don't
  328. * print it either. It would be unreasonable for the user to then
  329. * provide it.
  330. */
  331. const char *error_msg = "bad slave string format, expecting "
  332. "ff-dddddddddddd\n";
  333. if (buf[2] != '-') {
  334. dev_err(dev, "%s", error_msg);
  335. return -EINVAL;
  336. }
  337. i = sscanf(buf, "%02x-%012llx", &family, &id);
  338. if (i != 2) {
  339. dev_err(dev, "%s", error_msg);
  340. return -EINVAL;
  341. }
  342. rn->family = family;
  343. rn->id = id;
  344. rn64_le = cpu_to_le64(*(u64 *)rn);
  345. rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
  346. #if 0
  347. dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
  348. rn->family, (unsigned long long)rn->id, rn->crc);
  349. #endif
  350. return 0;
  351. }
  352. /* Searches the slaves in the w1_master and returns a pointer or NULL.
  353. * Note: must hold the mutex
  354. */
  355. static struct w1_slave *w1_slave_search_device(struct w1_master *dev,
  356. struct w1_reg_num *rn)
  357. {
  358. struct w1_slave *sl;
  359. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  360. if (sl->reg_num.family == rn->family &&
  361. sl->reg_num.id == rn->id &&
  362. sl->reg_num.crc == rn->crc) {
  363. return sl;
  364. }
  365. }
  366. return NULL;
  367. }
  368. static ssize_t w1_master_attribute_store_add(struct device *dev,
  369. struct device_attribute *attr,
  370. const char *buf, size_t count)
  371. {
  372. struct w1_master *md = dev_to_w1_master(dev);
  373. struct w1_reg_num rn;
  374. struct w1_slave *sl;
  375. ssize_t result = count;
  376. if (w1_atoreg_num(dev, buf, count, &rn))
  377. return -EINVAL;
  378. mutex_lock(&md->mutex);
  379. sl = w1_slave_search_device(md, &rn);
  380. /* It would be nice to do a targeted search one the one-wire bus
  381. * for the new device to see if it is out there or not. But the
  382. * current search doesn't support that.
  383. */
  384. if (sl) {
  385. dev_info(dev, "Device %s already exists\n", sl->name);
  386. result = -EINVAL;
  387. } else {
  388. w1_attach_slave_device(md, &rn);
  389. }
  390. mutex_unlock(&md->mutex);
  391. return result;
  392. }
  393. static ssize_t w1_master_attribute_show_remove(struct device *dev,
  394. struct device_attribute *attr, char *buf)
  395. {
  396. int c = PAGE_SIZE;
  397. c -= snprintf(buf+PAGE_SIZE - c, c,
  398. "write device id xx-xxxxxxxxxxxx to remove slave\n");
  399. return PAGE_SIZE - c;
  400. }
  401. static ssize_t w1_master_attribute_store_remove(struct device *dev,
  402. struct device_attribute *attr,
  403. const char *buf, size_t count)
  404. {
  405. struct w1_master *md = dev_to_w1_master(dev);
  406. struct w1_reg_num rn;
  407. struct w1_slave *sl;
  408. ssize_t result = count;
  409. if (w1_atoreg_num(dev, buf, count, &rn))
  410. return -EINVAL;
  411. mutex_lock(&md->mutex);
  412. sl = w1_slave_search_device(md, &rn);
  413. if (sl) {
  414. w1_slave_detach(sl);
  415. } else {
  416. dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
  417. (unsigned long long)rn.id);
  418. result = -EINVAL;
  419. }
  420. mutex_unlock(&md->mutex);
  421. return result;
  422. }
  423. #define W1_MASTER_ATTR_RO(_name, _mode) \
  424. struct device_attribute w1_master_attribute_##_name = \
  425. __ATTR(w1_master_##_name, _mode, \
  426. w1_master_attribute_show_##_name, NULL)
  427. #define W1_MASTER_ATTR_RW(_name, _mode) \
  428. struct device_attribute w1_master_attribute_##_name = \
  429. __ATTR(w1_master_##_name, _mode, \
  430. w1_master_attribute_show_##_name, \
  431. w1_master_attribute_store_##_name)
  432. static W1_MASTER_ATTR_RO(name, S_IRUGO);
  433. static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
  434. static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
  435. static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
  436. static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
  437. static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
  438. static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
  439. static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
  440. static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUGO);
  441. static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUGO);
  442. static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUGO);
  443. static struct attribute *w1_master_default_attrs[] = {
  444. &w1_master_attribute_name.attr,
  445. &w1_master_attribute_slaves.attr,
  446. &w1_master_attribute_slave_count.attr,
  447. &w1_master_attribute_max_slave_count.attr,
  448. &w1_master_attribute_attempts.attr,
  449. &w1_master_attribute_timeout.attr,
  450. &w1_master_attribute_pointer.attr,
  451. &w1_master_attribute_search.attr,
  452. &w1_master_attribute_pullup.attr,
  453. &w1_master_attribute_add.attr,
  454. &w1_master_attribute_remove.attr,
  455. NULL
  456. };
  457. static struct attribute_group w1_master_defattr_group = {
  458. .attrs = w1_master_default_attrs,
  459. };
  460. int w1_create_master_attributes(struct w1_master *master)
  461. {
  462. return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
  463. }
  464. void w1_destroy_master_attributes(struct w1_master *master)
  465. {
  466. sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
  467. }
  468. #ifdef CONFIG_HOTPLUG
  469. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
  470. {
  471. struct w1_master *md = NULL;
  472. struct w1_slave *sl = NULL;
  473. char *event_owner, *name;
  474. int err;
  475. if (dev->driver == &w1_master_driver) {
  476. md = container_of(dev, struct w1_master, dev);
  477. event_owner = "master";
  478. name = md->name;
  479. } else if (dev->driver == &w1_slave_driver) {
  480. sl = container_of(dev, struct w1_slave, dev);
  481. event_owner = "slave";
  482. name = sl->name;
  483. } else {
  484. dev_dbg(dev, "Unknown event.\n");
  485. return -EINVAL;
  486. }
  487. dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
  488. event_owner, name, dev->bus_id);
  489. if (dev->driver != &w1_slave_driver || !sl)
  490. return 0;
  491. err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
  492. if (err)
  493. return err;
  494. err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
  495. (unsigned long long)sl->reg_num.id);
  496. if (err)
  497. return err;
  498. return 0;
  499. };
  500. #else
  501. static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
  502. {
  503. return 0;
  504. }
  505. #endif
  506. static int __w1_attach_slave_device(struct w1_slave *sl)
  507. {
  508. int err;
  509. sl->dev.parent = &sl->master->dev;
  510. sl->dev.driver = &w1_slave_driver;
  511. sl->dev.bus = &w1_bus_type;
  512. sl->dev.release = &w1_slave_release;
  513. snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
  514. "%02x-%012llx",
  515. (unsigned int) sl->reg_num.family,
  516. (unsigned long long) sl->reg_num.id);
  517. snprintf(&sl->name[0], sizeof(sl->name),
  518. "%02x-%012llx",
  519. (unsigned int) sl->reg_num.family,
  520. (unsigned long long) sl->reg_num.id);
  521. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
  522. &sl->dev.bus_id[0], sl);
  523. err = device_register(&sl->dev);
  524. if (err < 0) {
  525. dev_err(&sl->dev,
  526. "Device registration [%s] failed. err=%d\n",
  527. sl->dev.bus_id, err);
  528. return err;
  529. }
  530. /* Create "name" entry */
  531. err = device_create_file(&sl->dev, &w1_slave_attr_name);
  532. if (err < 0) {
  533. dev_err(&sl->dev,
  534. "sysfs file creation for [%s] failed. err=%d\n",
  535. sl->dev.bus_id, err);
  536. goto out_unreg;
  537. }
  538. /* Create "id" entry */
  539. err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  540. if (err < 0) {
  541. dev_err(&sl->dev,
  542. "sysfs file creation for [%s] failed. err=%d\n",
  543. sl->dev.bus_id, err);
  544. goto out_rem1;
  545. }
  546. /* if the family driver needs to initialize something... */
  547. if (sl->family->fops && sl->family->fops->add_slave &&
  548. ((err = sl->family->fops->add_slave(sl)) < 0)) {
  549. dev_err(&sl->dev,
  550. "sysfs file creation for [%s] failed. err=%d\n",
  551. sl->dev.bus_id, err);
  552. goto out_rem2;
  553. }
  554. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  555. return 0;
  556. out_rem2:
  557. sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  558. out_rem1:
  559. device_remove_file(&sl->dev, &w1_slave_attr_name);
  560. out_unreg:
  561. device_unregister(&sl->dev);
  562. return err;
  563. }
  564. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  565. {
  566. struct w1_slave *sl;
  567. struct w1_family *f;
  568. int err;
  569. struct w1_netlink_msg msg;
  570. sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
  571. if (!sl) {
  572. dev_err(&dev->dev,
  573. "%s: failed to allocate new slave device.\n",
  574. __func__);
  575. return -ENOMEM;
  576. }
  577. sl->owner = THIS_MODULE;
  578. sl->master = dev;
  579. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  580. memset(&msg, 0, sizeof(msg));
  581. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  582. atomic_set(&sl->refcnt, 0);
  583. init_completion(&sl->released);
  584. spin_lock(&w1_flock);
  585. f = w1_family_registered(rn->family);
  586. if (!f) {
  587. f= &w1_default_family;
  588. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  589. rn->family, rn->family,
  590. (unsigned long long)rn->id, rn->crc);
  591. }
  592. __w1_family_get(f);
  593. spin_unlock(&w1_flock);
  594. sl->family = f;
  595. err = __w1_attach_slave_device(sl);
  596. if (err < 0) {
  597. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  598. sl->name);
  599. w1_family_put(sl->family);
  600. kfree(sl);
  601. return err;
  602. }
  603. sl->ttl = dev->slave_ttl;
  604. dev->slave_count++;
  605. memcpy(msg.id.id, rn, sizeof(msg.id));
  606. msg.type = W1_SLAVE_ADD;
  607. w1_netlink_send(dev, &msg);
  608. return 0;
  609. }
  610. void w1_slave_detach(struct w1_slave *sl)
  611. {
  612. struct w1_netlink_msg msg;
  613. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
  614. list_del(&sl->w1_slave_entry);
  615. if (sl->family->fops && sl->family->fops->remove_slave)
  616. sl->family->fops->remove_slave(sl);
  617. memset(&msg, 0, sizeof(msg));
  618. memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
  619. msg.type = W1_SLAVE_REMOVE;
  620. w1_netlink_send(sl->master, &msg);
  621. sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  622. device_remove_file(&sl->dev, &w1_slave_attr_name);
  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. static void w1_slave_found(struct w1_master *dev, u64 rn)
  698. {
  699. int slave_count;
  700. struct w1_slave *sl;
  701. struct list_head *ent;
  702. struct w1_reg_num *tmp;
  703. u64 rn_le = cpu_to_le64(rn);
  704. atomic_inc(&dev->refcnt);
  705. tmp = (struct w1_reg_num *) &rn;
  706. slave_count = 0;
  707. list_for_each(ent, &dev->slist) {
  708. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  709. if (sl->reg_num.family == tmp->family &&
  710. sl->reg_num.id == tmp->id &&
  711. sl->reg_num.crc == tmp->crc) {
  712. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  713. break;
  714. }
  715. slave_count++;
  716. }
  717. if (slave_count == dev->slave_count &&
  718. rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
  719. w1_attach_slave_device(dev, tmp);
  720. }
  721. atomic_dec(&dev->refcnt);
  722. }
  723. /**
  724. * Performs a ROM Search & registers any devices found.
  725. * The 1-wire search is a simple binary tree search.
  726. * For each bit of the address, we read two bits and write one bit.
  727. * The bit written will put to sleep all devies that don't match that bit.
  728. * When the two reads differ, the direction choice is obvious.
  729. * When both bits are 0, we must choose a path to take.
  730. * When we can scan all 64 bits without having to choose a path, we are done.
  731. *
  732. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  733. *
  734. * @dev The master device to search
  735. * @cb Function to call when a device is found
  736. */
  737. void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
  738. {
  739. u64 last_rn, rn, tmp64;
  740. int i, slave_count = 0;
  741. int last_zero, last_device;
  742. int search_bit, desc_bit;
  743. u8 triplet_ret = 0;
  744. search_bit = 0;
  745. rn = last_rn = 0;
  746. last_device = 0;
  747. last_zero = -1;
  748. desc_bit = 64;
  749. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  750. last_rn = rn;
  751. rn = 0;
  752. /*
  753. * Reset bus and all 1-wire device state machines
  754. * so they can respond to our requests.
  755. *
  756. * Return 0 - device(s) present, 1 - no devices present.
  757. */
  758. if (w1_reset_bus(dev)) {
  759. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  760. break;
  761. }
  762. /* Start the search */
  763. w1_write_8(dev, search_type);
  764. for (i = 0; i < 64; ++i) {
  765. /* Determine the direction/search bit */
  766. if (i == desc_bit)
  767. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  768. else if (i > desc_bit)
  769. search_bit = 0; /* take the 0 path on the next branch */
  770. else
  771. search_bit = ((last_rn >> i) & 0x1);
  772. /** Read two bits and write one bit */
  773. triplet_ret = w1_triplet(dev, search_bit);
  774. /* quit if no device responded */
  775. if ( (triplet_ret & 0x03) == 0x03 )
  776. break;
  777. /* If both directions were valid, and we took the 0 path... */
  778. if (triplet_ret == 0)
  779. last_zero = i;
  780. /* extract the direction taken & update the device number */
  781. tmp64 = (triplet_ret >> 2);
  782. rn |= (tmp64 << i);
  783. if (kthread_should_stop()) {
  784. printk(KERN_INFO "Abort w1_search (exiting)\n");
  785. return;
  786. }
  787. }
  788. if ( (triplet_ret & 0x03) != 0x03 ) {
  789. if ( (desc_bit == last_zero) || (last_zero < 0))
  790. last_device = 1;
  791. desc_bit = last_zero;
  792. cb(dev, rn);
  793. }
  794. }
  795. }
  796. void w1_search_process(struct w1_master *dev, u8 search_type)
  797. {
  798. struct w1_slave *sl, *sln;
  799. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  800. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  801. w1_search_devices(dev, search_type, w1_slave_found);
  802. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  803. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl)
  804. w1_slave_detach(sl);
  805. else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  806. sl->ttl = dev->slave_ttl;
  807. }
  808. if (dev->search_count > 0)
  809. dev->search_count--;
  810. }
  811. int w1_process(void *data)
  812. {
  813. struct w1_master *dev = (struct w1_master *) data;
  814. /* As long as w1_timeout is only set by a module parameter the sleep
  815. * time can be calculated in jiffies once.
  816. */
  817. const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
  818. while (!kthread_should_stop()) {
  819. if (dev->search_count) {
  820. mutex_lock(&dev->mutex);
  821. w1_search_process(dev, W1_SEARCH);
  822. mutex_unlock(&dev->mutex);
  823. }
  824. try_to_freeze();
  825. __set_current_state(TASK_INTERRUPTIBLE);
  826. if (kthread_should_stop())
  827. break;
  828. /* Only sleep when the search is active. */
  829. if (dev->search_count)
  830. schedule_timeout(jtime);
  831. else
  832. schedule();
  833. }
  834. atomic_dec(&dev->refcnt);
  835. return 0;
  836. }
  837. static int w1_init(void)
  838. {
  839. int retval;
  840. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  841. w1_init_netlink();
  842. retval = bus_register(&w1_bus_type);
  843. if (retval) {
  844. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  845. goto err_out_exit_init;
  846. }
  847. retval = driver_register(&w1_master_driver);
  848. if (retval) {
  849. printk(KERN_ERR
  850. "Failed to register master driver. err=%d.\n",
  851. retval);
  852. goto err_out_bus_unregister;
  853. }
  854. retval = driver_register(&w1_slave_driver);
  855. if (retval) {
  856. printk(KERN_ERR
  857. "Failed to register master driver. err=%d.\n",
  858. retval);
  859. goto err_out_master_unregister;
  860. }
  861. return 0;
  862. #if 0
  863. /* For undoing the slave register if there was a step after it. */
  864. err_out_slave_unregister:
  865. driver_unregister(&w1_slave_driver);
  866. #endif
  867. err_out_master_unregister:
  868. driver_unregister(&w1_master_driver);
  869. err_out_bus_unregister:
  870. bus_unregister(&w1_bus_type);
  871. err_out_exit_init:
  872. return retval;
  873. }
  874. static void w1_fini(void)
  875. {
  876. struct w1_master *dev;
  877. /* Set netlink removal messages and some cleanup */
  878. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  879. __w1_remove_master_device(dev);
  880. w1_fini_netlink();
  881. driver_unregister(&w1_slave_driver);
  882. driver_unregister(&w1_master_driver);
  883. bus_unregister(&w1_bus_type);
  884. }
  885. module_init(w1_init);
  886. module_exit(w1_fini);