w1.c 31 KB

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