w1.c 30 KB

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