w1.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 <asm/atomic.h>
  34. #include "w1.h"
  35. #include "w1_log.h"
  36. #include "w1_int.h"
  37. #include "w1_family.h"
  38. #include "w1_netlink.h"
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  41. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
  42. static int w1_timeout = 10;
  43. static int w1_control_timeout = 1;
  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(control_timeout, w1_control_timeout, int, 0);
  48. module_param_named(max_slave_count, w1_max_slave_count, int, 0);
  49. module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
  50. DECLARE_MUTEX(w1_mlock);
  51. LIST_HEAD(w1_masters);
  52. static struct task_struct *w1_control_thread;
  53. static int w1_master_match(struct device *dev, struct device_driver *drv)
  54. {
  55. return 1;
  56. }
  57. static int w1_master_probe(struct device *dev)
  58. {
  59. return -ENODEV;
  60. }
  61. static void w1_master_release(struct device *dev)
  62. {
  63. struct w1_master *md = dev_to_w1_master(dev);
  64. dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
  65. memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
  66. kfree(md);
  67. }
  68. static void w1_slave_release(struct device *dev)
  69. {
  70. struct w1_slave *sl = dev_to_w1_slave(dev);
  71. printk("%s: Releasing %s.\n", __func__, sl->name);
  72. while (atomic_read(&sl->refcnt)) {
  73. printk("Waiting for %s to become free: refcnt=%d.\n",
  74. sl->name, atomic_read(&sl->refcnt));
  75. if (msleep_interruptible(1000))
  76. flush_signals(current);
  77. }
  78. w1_family_put(sl->family);
  79. sl->master->slave_count--;
  80. complete(&sl->released);
  81. }
  82. static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
  83. {
  84. struct w1_slave *sl = dev_to_w1_slave(dev);
  85. return sprintf(buf, "%s\n", sl->name);
  86. }
  87. static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, size_t count)
  88. {
  89. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  90. if (off > 8) {
  91. count = 0;
  92. } else {
  93. if (off + count > 8)
  94. count = 8 - off;
  95. memcpy(buf, (u8 *)&sl->reg_num, count);
  96. }
  97. return count;
  98. }
  99. static struct device_attribute w1_slave_attr_name =
  100. __ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
  101. static struct bin_attribute w1_slave_attr_bin_id = {
  102. .attr = {
  103. .name = "id",
  104. .mode = S_IRUGO,
  105. .owner = THIS_MODULE,
  106. },
  107. .size = 8,
  108. .read = w1_slave_read_id,
  109. };
  110. /* Default family */
  111. static ssize_t w1_default_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
  112. {
  113. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  114. if (down_interruptible(&sl->master->mutex)) {
  115. count = 0;
  116. goto out;
  117. }
  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. up(&sl->master->mutex);
  125. out:
  126. return count;
  127. }
  128. static ssize_t w1_default_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
  129. {
  130. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  131. if (down_interruptible(&sl->master->mutex)) {
  132. count = 0;
  133. goto out;
  134. }
  135. w1_read_block(sl->master, buf, count);
  136. up(&sl->master->mutex);
  137. out:
  138. return count;
  139. }
  140. static struct bin_attribute w1_default_attr = {
  141. .attr = {
  142. .name = "rw",
  143. .mode = S_IRUGO | S_IWUSR,
  144. .owner = THIS_MODULE,
  145. },
  146. .size = PAGE_SIZE,
  147. .read = w1_default_read,
  148. .write = w1_default_write,
  149. };
  150. static int w1_default_add_slave(struct w1_slave *sl)
  151. {
  152. return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr);
  153. }
  154. static void w1_default_remove_slave(struct w1_slave *sl)
  155. {
  156. sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr);
  157. }
  158. static struct w1_family_ops w1_default_fops = {
  159. .add_slave = w1_default_add_slave,
  160. .remove_slave = w1_default_remove_slave,
  161. };
  162. static struct w1_family w1_default_family = {
  163. .fops = &w1_default_fops,
  164. };
  165. static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
  166. static struct bus_type w1_bus_type = {
  167. .name = "w1",
  168. .match = w1_master_match,
  169. .uevent = w1_uevent,
  170. };
  171. struct device_driver w1_master_driver = {
  172. .name = "w1_master_driver",
  173. .bus = &w1_bus_type,
  174. .probe = w1_master_probe,
  175. };
  176. struct device w1_master_device = {
  177. .parent = NULL,
  178. .bus = &w1_bus_type,
  179. .bus_id = "w1 bus master",
  180. .driver = &w1_master_driver,
  181. .release = &w1_master_release
  182. };
  183. struct device_driver w1_slave_driver = {
  184. .name = "w1_slave_driver",
  185. .bus = &w1_bus_type,
  186. };
  187. struct device w1_slave_device = {
  188. .parent = NULL,
  189. .bus = &w1_bus_type,
  190. .bus_id = "w1 bus slave",
  191. .driver = &w1_slave_driver,
  192. .release = &w1_slave_release
  193. };
  194. static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  195. {
  196. struct w1_master *md = dev_to_w1_master(dev);
  197. ssize_t count;
  198. if (down_interruptible (&md->mutex))
  199. return -EBUSY;
  200. count = sprintf(buf, "%s\n", md->name);
  201. up(&md->mutex);
  202. return count;
  203. }
  204. static ssize_t w1_master_attribute_store_search(struct device * dev,
  205. struct device_attribute *attr,
  206. const char * buf, size_t count)
  207. {
  208. struct w1_master *md = dev_to_w1_master(dev);
  209. if (down_interruptible (&md->mutex))
  210. return -EBUSY;
  211. md->search_count = simple_strtol(buf, NULL, 0);
  212. up(&md->mutex);
  213. return count;
  214. }
  215. static ssize_t w1_master_attribute_show_search(struct device *dev,
  216. struct device_attribute *attr,
  217. char *buf)
  218. {
  219. struct w1_master *md = dev_to_w1_master(dev);
  220. ssize_t count;
  221. if (down_interruptible (&md->mutex))
  222. return -EBUSY;
  223. count = sprintf(buf, "%d\n", md->search_count);
  224. up(&md->mutex);
  225. return count;
  226. }
  227. static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
  228. {
  229. struct w1_master *md = dev_to_w1_master(dev);
  230. ssize_t count;
  231. if (down_interruptible(&md->mutex))
  232. return -EBUSY;
  233. count = sprintf(buf, "0x%p\n", md->bus_master);
  234. up(&md->mutex);
  235. return count;
  236. }
  237. static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  238. {
  239. ssize_t count;
  240. count = sprintf(buf, "%d\n", w1_timeout);
  241. return count;
  242. }
  243. static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  244. {
  245. struct w1_master *md = dev_to_w1_master(dev);
  246. ssize_t count;
  247. if (down_interruptible(&md->mutex))
  248. return -EBUSY;
  249. count = sprintf(buf, "%d\n", md->max_slave_count);
  250. up(&md->mutex);
  251. return count;
  252. }
  253. static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
  254. {
  255. struct w1_master *md = dev_to_w1_master(dev);
  256. ssize_t count;
  257. if (down_interruptible(&md->mutex))
  258. return -EBUSY;
  259. count = sprintf(buf, "%lu\n", md->attempts);
  260. up(&md->mutex);
  261. return count;
  262. }
  263. static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
  264. {
  265. struct w1_master *md = dev_to_w1_master(dev);
  266. ssize_t count;
  267. if (down_interruptible(&md->mutex))
  268. return -EBUSY;
  269. count = sprintf(buf, "%d\n", md->slave_count);
  270. up(&md->mutex);
  271. return count;
  272. }
  273. static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
  274. {
  275. struct w1_master *md = dev_to_w1_master(dev);
  276. int c = PAGE_SIZE;
  277. if (down_interruptible(&md->mutex))
  278. return -EBUSY;
  279. if (md->slave_count == 0)
  280. c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
  281. else {
  282. struct list_head *ent, *n;
  283. struct w1_slave *sl;
  284. list_for_each_safe(ent, n, &md->slist) {
  285. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  286. c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
  287. }
  288. }
  289. up(&md->mutex);
  290. return PAGE_SIZE - c;
  291. }
  292. #define W1_MASTER_ATTR_RO(_name, _mode) \
  293. struct device_attribute w1_master_attribute_##_name = \
  294. __ATTR(w1_master_##_name, _mode, \
  295. w1_master_attribute_show_##_name, NULL)
  296. #define W1_MASTER_ATTR_RW(_name, _mode) \
  297. struct device_attribute w1_master_attribute_##_name = \
  298. __ATTR(w1_master_##_name, _mode, \
  299. w1_master_attribute_show_##_name, \
  300. w1_master_attribute_store_##_name)
  301. static W1_MASTER_ATTR_RO(name, S_IRUGO);
  302. static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
  303. static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
  304. static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
  305. static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
  306. static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
  307. static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
  308. static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
  309. static struct attribute *w1_master_default_attrs[] = {
  310. &w1_master_attribute_name.attr,
  311. &w1_master_attribute_slaves.attr,
  312. &w1_master_attribute_slave_count.attr,
  313. &w1_master_attribute_max_slave_count.attr,
  314. &w1_master_attribute_attempts.attr,
  315. &w1_master_attribute_timeout.attr,
  316. &w1_master_attribute_pointer.attr,
  317. &w1_master_attribute_search.attr,
  318. NULL
  319. };
  320. static struct attribute_group w1_master_defattr_group = {
  321. .attrs = w1_master_default_attrs,
  322. };
  323. int w1_create_master_attributes(struct w1_master *master)
  324. {
  325. return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
  326. }
  327. void w1_destroy_master_attributes(struct w1_master *master)
  328. {
  329. sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
  330. }
  331. #ifdef CONFIG_HOTPLUG
  332. static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  333. {
  334. struct w1_master *md = NULL;
  335. struct w1_slave *sl = NULL;
  336. char *event_owner, *name;
  337. int err, cur_index=0, cur_len=0;
  338. if (dev->driver == &w1_master_driver) {
  339. md = container_of(dev, struct w1_master, dev);
  340. event_owner = "master";
  341. name = md->name;
  342. } else if (dev->driver == &w1_slave_driver) {
  343. sl = container_of(dev, struct w1_slave, dev);
  344. event_owner = "slave";
  345. name = sl->name;
  346. } else {
  347. dev_dbg(dev, "Unknown event.\n");
  348. return -EINVAL;
  349. }
  350. dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id);
  351. if (dev->driver != &w1_slave_driver || !sl)
  352. return 0;
  353. err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family);
  354. if (err)
  355. return err;
  356. err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id);
  357. if (err)
  358. return err;
  359. return 0;
  360. };
  361. #else
  362. static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  363. {
  364. return 0;
  365. }
  366. #endif
  367. static int __w1_attach_slave_device(struct w1_slave *sl)
  368. {
  369. int err;
  370. sl->dev.parent = &sl->master->dev;
  371. sl->dev.driver = &w1_slave_driver;
  372. sl->dev.bus = &w1_bus_type;
  373. sl->dev.release = &w1_slave_release;
  374. snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
  375. "%02x-%012llx",
  376. (unsigned int) sl->reg_num.family,
  377. (unsigned long long) sl->reg_num.id);
  378. snprintf(&sl->name[0], sizeof(sl->name),
  379. "%02x-%012llx",
  380. (unsigned int) sl->reg_num.family,
  381. (unsigned long long) sl->reg_num.id);
  382. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]);
  383. err = device_register(&sl->dev);
  384. if (err < 0) {
  385. dev_err(&sl->dev,
  386. "Device registration [%s] failed. err=%d\n",
  387. sl->dev.bus_id, err);
  388. return err;
  389. }
  390. /* Create "name" entry */
  391. err = device_create_file(&sl->dev, &w1_slave_attr_name);
  392. if (err < 0) {
  393. dev_err(&sl->dev,
  394. "sysfs file creation for [%s] failed. err=%d\n",
  395. sl->dev.bus_id, err);
  396. goto out_unreg;
  397. }
  398. /* Create "id" entry */
  399. err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  400. if (err < 0) {
  401. dev_err(&sl->dev,
  402. "sysfs file creation for [%s] failed. err=%d\n",
  403. sl->dev.bus_id, err);
  404. goto out_rem1;
  405. }
  406. /* if the family driver needs to initialize something... */
  407. if (sl->family->fops && sl->family->fops->add_slave &&
  408. ((err = sl->family->fops->add_slave(sl)) < 0)) {
  409. dev_err(&sl->dev,
  410. "sysfs file creation for [%s] failed. err=%d\n",
  411. sl->dev.bus_id, err);
  412. goto out_rem2;
  413. }
  414. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  415. return 0;
  416. out_rem2:
  417. sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  418. out_rem1:
  419. device_remove_file(&sl->dev, &w1_slave_attr_name);
  420. out_unreg:
  421. device_unregister(&sl->dev);
  422. return err;
  423. }
  424. static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  425. {
  426. struct w1_slave *sl;
  427. struct w1_family *f;
  428. int err;
  429. struct w1_netlink_msg msg;
  430. sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
  431. if (!sl) {
  432. dev_err(&dev->dev,
  433. "%s: failed to allocate new slave device.\n",
  434. __func__);
  435. return -ENOMEM;
  436. }
  437. memset(sl, 0, sizeof(*sl));
  438. sl->owner = THIS_MODULE;
  439. sl->master = dev;
  440. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  441. memset(&msg, 0, sizeof(msg));
  442. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  443. atomic_set(&sl->refcnt, 0);
  444. init_completion(&sl->released);
  445. spin_lock(&w1_flock);
  446. f = w1_family_registered(rn->family);
  447. if (!f) {
  448. f= &w1_default_family;
  449. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  450. rn->family, rn->family,
  451. (unsigned long long)rn->id, rn->crc);
  452. }
  453. __w1_family_get(f);
  454. spin_unlock(&w1_flock);
  455. sl->family = f;
  456. err = __w1_attach_slave_device(sl);
  457. if (err < 0) {
  458. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  459. sl->name);
  460. w1_family_put(sl->family);
  461. kfree(sl);
  462. return err;
  463. }
  464. sl->ttl = dev->slave_ttl;
  465. dev->slave_count++;
  466. memcpy(msg.id.id, rn, sizeof(msg.id));
  467. msg.type = W1_SLAVE_ADD;
  468. w1_netlink_send(dev, &msg);
  469. return 0;
  470. }
  471. static void w1_slave_detach(struct w1_slave *sl)
  472. {
  473. struct w1_netlink_msg msg;
  474. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
  475. list_del(&sl->w1_slave_entry);
  476. if (sl->family->fops && sl->family->fops->remove_slave)
  477. sl->family->fops->remove_slave(sl);
  478. memset(&msg, 0, sizeof(msg));
  479. memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
  480. msg.type = W1_SLAVE_REMOVE;
  481. w1_netlink_send(sl->master, &msg);
  482. sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
  483. device_remove_file(&sl->dev, &w1_slave_attr_name);
  484. device_unregister(&sl->dev);
  485. wait_for_completion(&sl->released);
  486. kfree(sl);
  487. }
  488. static struct w1_master *w1_search_master(void *data)
  489. {
  490. struct w1_master *dev;
  491. int found = 0;
  492. down(&w1_mlock);
  493. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  494. if (dev->bus_master->data == data) {
  495. found = 1;
  496. atomic_inc(&dev->refcnt);
  497. break;
  498. }
  499. }
  500. up(&w1_mlock);
  501. return (found)?dev:NULL;
  502. }
  503. struct w1_master *w1_search_master_id(u32 id)
  504. {
  505. struct w1_master *dev;
  506. int found = 0;
  507. down(&w1_mlock);
  508. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  509. if (dev->id == id) {
  510. found = 1;
  511. atomic_inc(&dev->refcnt);
  512. break;
  513. }
  514. }
  515. up(&w1_mlock);
  516. return (found)?dev:NULL;
  517. }
  518. struct w1_slave *w1_search_slave(struct w1_reg_num *id)
  519. {
  520. struct w1_master *dev;
  521. struct w1_slave *sl = NULL;
  522. int found = 0;
  523. down(&w1_mlock);
  524. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  525. down(&dev->mutex);
  526. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  527. if (sl->reg_num.family == id->family &&
  528. sl->reg_num.id == id->id &&
  529. sl->reg_num.crc == id->crc) {
  530. found = 1;
  531. atomic_inc(&dev->refcnt);
  532. atomic_inc(&sl->refcnt);
  533. break;
  534. }
  535. }
  536. up(&dev->mutex);
  537. if (found)
  538. break;
  539. }
  540. up(&w1_mlock);
  541. return (found)?sl:NULL;
  542. }
  543. void w1_reconnect_slaves(struct w1_family *f)
  544. {
  545. struct w1_master *dev;
  546. down(&w1_mlock);
  547. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  548. dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
  549. dev->name, f->fid);
  550. set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
  551. }
  552. up(&w1_mlock);
  553. }
  554. static void w1_slave_found(void *data, u64 rn)
  555. {
  556. int slave_count;
  557. struct w1_slave *sl;
  558. struct list_head *ent;
  559. struct w1_reg_num *tmp;
  560. int family_found = 0;
  561. struct w1_master *dev;
  562. u64 rn_le = cpu_to_le64(rn);
  563. dev = w1_search_master(data);
  564. if (!dev) {
  565. printk(KERN_ERR "Failed to find w1 master device for data %p, "
  566. "it is impossible.\n", data);
  567. return;
  568. }
  569. tmp = (struct w1_reg_num *) &rn;
  570. slave_count = 0;
  571. list_for_each(ent, &dev->slist) {
  572. sl = list_entry(ent, struct w1_slave, w1_slave_entry);
  573. if (sl->reg_num.family == tmp->family &&
  574. sl->reg_num.id == tmp->id &&
  575. sl->reg_num.crc == tmp->crc) {
  576. set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  577. break;
  578. } else if (sl->reg_num.family == tmp->family) {
  579. family_found = 1;
  580. break;
  581. }
  582. slave_count++;
  583. }
  584. if (slave_count == dev->slave_count &&
  585. rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
  586. w1_attach_slave_device(dev, tmp);
  587. }
  588. atomic_dec(&dev->refcnt);
  589. }
  590. /**
  591. * Performs a ROM Search & registers any devices found.
  592. * The 1-wire search is a simple binary tree search.
  593. * For each bit of the address, we read two bits and write one bit.
  594. * The bit written will put to sleep all devies that don't match that bit.
  595. * When the two reads differ, the direction choice is obvious.
  596. * When both bits are 0, we must choose a path to take.
  597. * When we can scan all 64 bits without having to choose a path, we are done.
  598. *
  599. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  600. *
  601. * @dev The master device to search
  602. * @cb Function to call when a device is found
  603. */
  604. void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
  605. {
  606. u64 last_rn, rn, tmp64;
  607. int i, slave_count = 0;
  608. int last_zero, last_device;
  609. int search_bit, desc_bit;
  610. u8 triplet_ret = 0;
  611. search_bit = 0;
  612. rn = last_rn = 0;
  613. last_device = 0;
  614. last_zero = -1;
  615. desc_bit = 64;
  616. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  617. last_rn = rn;
  618. rn = 0;
  619. /*
  620. * Reset bus and all 1-wire device state machines
  621. * so they can respond to our requests.
  622. *
  623. * Return 0 - device(s) present, 1 - no devices present.
  624. */
  625. if (w1_reset_bus(dev)) {
  626. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  627. break;
  628. }
  629. /* Start the search */
  630. w1_write_8(dev, search_type);
  631. for (i = 0; i < 64; ++i) {
  632. /* Determine the direction/search bit */
  633. if (i == desc_bit)
  634. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  635. else if (i > desc_bit)
  636. search_bit = 0; /* take the 0 path on the next branch */
  637. else
  638. search_bit = ((last_rn >> i) & 0x1);
  639. /** Read two bits and write one bit */
  640. triplet_ret = w1_triplet(dev, search_bit);
  641. /* quit if no device responded */
  642. if ( (triplet_ret & 0x03) == 0x03 )
  643. break;
  644. /* If both directions were valid, and we took the 0 path... */
  645. if (triplet_ret == 0)
  646. last_zero = i;
  647. /* extract the direction taken & update the device number */
  648. tmp64 = (triplet_ret >> 2);
  649. rn |= (tmp64 << i);
  650. }
  651. if ( (triplet_ret & 0x03) != 0x03 ) {
  652. if ( (desc_bit == last_zero) || (last_zero < 0))
  653. last_device = 1;
  654. desc_bit = last_zero;
  655. cb(dev->bus_master->data, rn);
  656. }
  657. }
  658. }
  659. static int w1_control(void *data)
  660. {
  661. struct w1_slave *sl, *sln;
  662. struct w1_master *dev, *n;
  663. int have_to_wait = 0;
  664. while (!kthread_should_stop() || have_to_wait) {
  665. have_to_wait = 0;
  666. try_to_freeze();
  667. msleep_interruptible(w1_control_timeout * 1000);
  668. list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
  669. if (!kthread_should_stop() && !dev->flags)
  670. continue;
  671. /*
  672. * Little race: we can create thread but not set the flag.
  673. * Get a chance for external process to set flag up.
  674. */
  675. if (!dev->initialized) {
  676. have_to_wait = 1;
  677. continue;
  678. }
  679. if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
  680. set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
  681. down(&w1_mlock);
  682. list_del(&dev->w1_master_entry);
  683. up(&w1_mlock);
  684. down(&dev->mutex);
  685. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  686. w1_slave_detach(sl);
  687. }
  688. w1_destroy_master_attributes(dev);
  689. up(&dev->mutex);
  690. atomic_dec(&dev->refcnt);
  691. continue;
  692. }
  693. if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
  694. dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
  695. down(&dev->mutex);
  696. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  697. if (sl->family->fid == W1_FAMILY_DEFAULT) {
  698. struct w1_reg_num rn;
  699. memcpy(&rn, &sl->reg_num, sizeof(rn));
  700. w1_slave_detach(sl);
  701. w1_attach_slave_device(dev, &rn);
  702. }
  703. }
  704. dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name);
  705. clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
  706. up(&dev->mutex);
  707. }
  708. }
  709. }
  710. return 0;
  711. }
  712. void w1_search_process(struct w1_master *dev, u8 search_type)
  713. {
  714. struct w1_slave *sl, *sln;
  715. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  716. clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
  717. w1_search_devices(dev, search_type, w1_slave_found);
  718. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  719. if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
  720. w1_slave_detach(sl);
  721. dev->slave_count--;
  722. } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
  723. sl->ttl = dev->slave_ttl;
  724. }
  725. if (dev->search_count > 0)
  726. dev->search_count--;
  727. }
  728. int w1_process(void *data)
  729. {
  730. struct w1_master *dev = (struct w1_master *) data;
  731. while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
  732. try_to_freeze();
  733. msleep_interruptible(w1_timeout * 1000);
  734. if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
  735. break;
  736. if (!dev->initialized)
  737. continue;
  738. if (dev->search_count == 0)
  739. continue;
  740. if (down_interruptible(&dev->mutex))
  741. continue;
  742. w1_search_process(dev, W1_SEARCH);
  743. up(&dev->mutex);
  744. }
  745. atomic_dec(&dev->refcnt);
  746. return 0;
  747. }
  748. static int w1_init(void)
  749. {
  750. int retval;
  751. printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
  752. w1_init_netlink();
  753. retval = bus_register(&w1_bus_type);
  754. if (retval) {
  755. printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
  756. goto err_out_exit_init;
  757. }
  758. retval = driver_register(&w1_master_driver);
  759. if (retval) {
  760. printk(KERN_ERR
  761. "Failed to register master driver. err=%d.\n",
  762. retval);
  763. goto err_out_bus_unregister;
  764. }
  765. retval = driver_register(&w1_slave_driver);
  766. if (retval) {
  767. printk(KERN_ERR
  768. "Failed to register master driver. err=%d.\n",
  769. retval);
  770. goto err_out_master_unregister;
  771. }
  772. w1_control_thread = kthread_run(w1_control, NULL, "w1_control");
  773. if (IS_ERR(w1_control_thread)) {
  774. retval = PTR_ERR(w1_control_thread);
  775. printk(KERN_ERR "Failed to create control thread. err=%d\n",
  776. retval);
  777. goto err_out_slave_unregister;
  778. }
  779. return 0;
  780. err_out_slave_unregister:
  781. driver_unregister(&w1_slave_driver);
  782. err_out_master_unregister:
  783. driver_unregister(&w1_master_driver);
  784. err_out_bus_unregister:
  785. bus_unregister(&w1_bus_type);
  786. err_out_exit_init:
  787. return retval;
  788. }
  789. static void w1_fini(void)
  790. {
  791. struct w1_master *dev;
  792. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  793. __w1_remove_master_device(dev);
  794. w1_fini_netlink();
  795. kthread_stop(w1_control_thread);
  796. driver_unregister(&w1_slave_driver);
  797. driver_unregister(&w1_master_driver);
  798. bus_unregister(&w1_bus_type);
  799. }
  800. module_init(w1_init);
  801. module_exit(w1_fini);