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.h"
  29. #include "w1_int.h"
  30. #include "w1_family.h"
  31. #include "w1_netlink.h"
  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 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. break;
  543. case BUS_NOTIFY_DEL_DEVICE:
  544. if (fops->remove_slave)
  545. sl->family->fops->remove_slave(sl);
  546. if (fops->groups)
  547. sysfs_remove_groups(&sl->dev.kobj, fops->groups);
  548. break;
  549. }
  550. return 0;
  551. }
  552. static int __w1_attach_slave_device(struct w1_slave *sl)
  553. {
  554. int err;
  555. sl->dev.parent = &sl->master->dev;
  556. sl->dev.driver = &w1_slave_driver;
  557. sl->dev.bus = &w1_bus_type;
  558. sl->dev.release = &w1_slave_release;
  559. sl->dev.groups = w1_slave_groups;
  560. dev_set_name(&sl->dev, "%02x-%012llx",
  561. (unsigned int) sl->reg_num.family,
  562. (unsigned long long) sl->reg_num.id);
  563. snprintf(&sl->name[0], sizeof(sl->name),
  564. "%02x-%012llx",
  565. (unsigned int) sl->reg_num.family,
  566. (unsigned long long) sl->reg_num.id);
  567. dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
  568. dev_name(&sl->dev), sl);
  569. /* suppress for w1_family_notify before sending KOBJ_ADD */
  570. dev_set_uevent_suppress(&sl->dev, true);
  571. err = device_register(&sl->dev);
  572. if (err < 0) {
  573. dev_err(&sl->dev,
  574. "Device registration [%s] failed. err=%d\n",
  575. dev_name(&sl->dev), err);
  576. return err;
  577. }
  578. w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
  579. dev_set_uevent_suppress(&sl->dev, false);
  580. kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
  581. mutex_lock(&sl->master->list_mutex);
  582. list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
  583. mutex_unlock(&sl->master->list_mutex);
  584. return 0;
  585. }
  586. int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
  587. {
  588. struct w1_slave *sl;
  589. struct w1_family *f;
  590. int err;
  591. struct w1_netlink_msg msg;
  592. sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
  593. if (!sl) {
  594. dev_err(&dev->dev,
  595. "%s: failed to allocate new slave device.\n",
  596. __func__);
  597. return -ENOMEM;
  598. }
  599. sl->owner = THIS_MODULE;
  600. sl->master = dev;
  601. set_bit(W1_SLAVE_ACTIVE, &sl->flags);
  602. memset(&msg, 0, sizeof(msg));
  603. memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
  604. atomic_set(&sl->refcnt, 1);
  605. atomic_inc(&sl->master->refcnt);
  606. /* slave modules need to be loaded in a context with unlocked mutex */
  607. mutex_unlock(&dev->mutex);
  608. request_module("w1-family-0x%02x", rn->family);
  609. mutex_lock(&dev->mutex);
  610. spin_lock(&w1_flock);
  611. f = w1_family_registered(rn->family);
  612. if (!f) {
  613. f= &w1_default_family;
  614. dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
  615. rn->family, rn->family,
  616. (unsigned long long)rn->id, rn->crc);
  617. }
  618. __w1_family_get(f);
  619. spin_unlock(&w1_flock);
  620. sl->family = f;
  621. err = __w1_attach_slave_device(sl);
  622. if (err < 0) {
  623. dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
  624. sl->name);
  625. w1_family_put(sl->family);
  626. atomic_dec(&sl->master->refcnt);
  627. kfree(sl);
  628. return err;
  629. }
  630. sl->ttl = dev->slave_ttl;
  631. dev->slave_count++;
  632. memcpy(msg.id.id, rn, sizeof(msg.id));
  633. msg.type = W1_SLAVE_ADD;
  634. w1_netlink_send(dev, &msg);
  635. return 0;
  636. }
  637. int w1_unref_slave(struct w1_slave *sl)
  638. {
  639. struct w1_master *dev = sl->master;
  640. int refcnt;
  641. mutex_lock(&dev->list_mutex);
  642. refcnt = atomic_sub_return(1, &sl->refcnt);
  643. if (refcnt == 0) {
  644. struct w1_netlink_msg msg;
  645. dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
  646. sl->name, sl);
  647. list_del(&sl->w1_slave_entry);
  648. memset(&msg, 0, sizeof(msg));
  649. memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
  650. msg.type = W1_SLAVE_REMOVE;
  651. w1_netlink_send(sl->master, &msg);
  652. w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
  653. device_unregister(&sl->dev);
  654. #ifdef DEBUG
  655. memset(sl, 0, sizeof(*sl));
  656. #endif
  657. kfree(sl);
  658. }
  659. atomic_dec(&dev->refcnt);
  660. mutex_unlock(&dev->list_mutex);
  661. return refcnt;
  662. }
  663. int w1_slave_detach(struct w1_slave *sl)
  664. {
  665. /* Only detach a slave once as it decreases the refcnt each time. */
  666. int destroy_now;
  667. mutex_lock(&sl->master->list_mutex);
  668. destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
  669. set_bit(W1_SLAVE_DETACH, &sl->flags);
  670. mutex_unlock(&sl->master->list_mutex);
  671. if (destroy_now)
  672. destroy_now = !w1_unref_slave(sl);
  673. return destroy_now ? 0 : -EBUSY;
  674. }
  675. struct w1_master *w1_search_master_id(u32 id)
  676. {
  677. struct w1_master *dev;
  678. int found = 0;
  679. mutex_lock(&w1_mlock);
  680. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  681. if (dev->id == id) {
  682. found = 1;
  683. atomic_inc(&dev->refcnt);
  684. break;
  685. }
  686. }
  687. mutex_unlock(&w1_mlock);
  688. return (found)?dev:NULL;
  689. }
  690. struct w1_slave *w1_search_slave(struct w1_reg_num *id)
  691. {
  692. struct w1_master *dev;
  693. struct w1_slave *sl = NULL;
  694. int found = 0;
  695. mutex_lock(&w1_mlock);
  696. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  697. mutex_lock(&dev->list_mutex);
  698. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  699. if (sl->reg_num.family == id->family &&
  700. sl->reg_num.id == id->id &&
  701. sl->reg_num.crc == id->crc) {
  702. found = 1;
  703. atomic_inc(&dev->refcnt);
  704. atomic_inc(&sl->refcnt);
  705. break;
  706. }
  707. }
  708. mutex_unlock(&dev->list_mutex);
  709. if (found)
  710. break;
  711. }
  712. mutex_unlock(&w1_mlock);
  713. return (found)?sl:NULL;
  714. }
  715. void w1_reconnect_slaves(struct w1_family *f, int attach)
  716. {
  717. struct w1_slave *sl, *sln;
  718. struct w1_master *dev;
  719. mutex_lock(&w1_mlock);
  720. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  721. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  722. "for family %02x.\n", dev->name, f->fid);
  723. mutex_lock(&dev->mutex);
  724. mutex_lock(&dev->list_mutex);
  725. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  726. /* If it is a new family, slaves with the default
  727. * family driver and are that family will be
  728. * connected. If the family is going away, devices
  729. * matching that family are reconneced.
  730. */
  731. if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
  732. && sl->reg_num.family == f->fid) ||
  733. (!attach && sl->family->fid == f->fid)) {
  734. struct w1_reg_num rn;
  735. mutex_unlock(&dev->list_mutex);
  736. memcpy(&rn, &sl->reg_num, sizeof(rn));
  737. /* If it was already in use let the automatic
  738. * scan pick it up again later.
  739. */
  740. if (!w1_slave_detach(sl))
  741. w1_attach_slave_device(dev, &rn);
  742. mutex_lock(&dev->list_mutex);
  743. }
  744. }
  745. dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
  746. "has been finished.\n", dev->name);
  747. mutex_unlock(&dev->list_mutex);
  748. mutex_unlock(&dev->mutex);
  749. }
  750. mutex_unlock(&w1_mlock);
  751. }
  752. void w1_slave_found(struct w1_master *dev, u64 rn)
  753. {
  754. struct w1_slave *sl;
  755. struct w1_reg_num *tmp;
  756. u64 rn_le = cpu_to_le64(rn);
  757. atomic_inc(&dev->refcnt);
  758. tmp = (struct w1_reg_num *) &rn;
  759. sl = w1_slave_search_device(dev, tmp);
  760. if (sl) {
  761. set_bit(W1_SLAVE_ACTIVE, &sl->flags);
  762. } else {
  763. if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
  764. w1_attach_slave_device(dev, tmp);
  765. }
  766. atomic_dec(&dev->refcnt);
  767. }
  768. /**
  769. * w1_search() - Performs a ROM Search & registers any devices found.
  770. * @dev: The master device to search
  771. * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
  772. * to return only devices in the alarmed state
  773. * @cb: Function to call when a device is found
  774. *
  775. * The 1-wire search is a simple binary tree search.
  776. * For each bit of the address, we read two bits and write one bit.
  777. * The bit written will put to sleep all devies that don't match that bit.
  778. * When the two reads differ, the direction choice is obvious.
  779. * When both bits are 0, we must choose a path to take.
  780. * When we can scan all 64 bits without having to choose a path, we are done.
  781. *
  782. * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
  783. *
  784. */
  785. void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
  786. {
  787. u64 last_rn, rn, tmp64;
  788. int i, slave_count = 0;
  789. int last_zero, last_device;
  790. int search_bit, desc_bit;
  791. u8 triplet_ret = 0;
  792. search_bit = 0;
  793. rn = dev->search_id;
  794. last_rn = 0;
  795. last_device = 0;
  796. last_zero = -1;
  797. desc_bit = 64;
  798. while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
  799. last_rn = rn;
  800. rn = 0;
  801. /*
  802. * Reset bus and all 1-wire device state machines
  803. * so they can respond to our requests.
  804. *
  805. * Return 0 - device(s) present, 1 - no devices present.
  806. */
  807. mutex_lock(&dev->bus_mutex);
  808. if (w1_reset_bus(dev)) {
  809. mutex_unlock(&dev->bus_mutex);
  810. dev_dbg(&dev->dev, "No devices present on the wire.\n");
  811. break;
  812. }
  813. /* Do fast search on single slave bus */
  814. if (dev->max_slave_count == 1) {
  815. int rv;
  816. w1_write_8(dev, W1_READ_ROM);
  817. rv = w1_read_block(dev, (u8 *)&rn, 8);
  818. mutex_unlock(&dev->bus_mutex);
  819. if (rv == 8 && rn)
  820. cb(dev, rn);
  821. break;
  822. }
  823. /* Start the search */
  824. w1_write_8(dev, search_type);
  825. for (i = 0; i < 64; ++i) {
  826. /* Determine the direction/search bit */
  827. if (i == desc_bit)
  828. search_bit = 1; /* took the 0 path last time, so take the 1 path */
  829. else if (i > desc_bit)
  830. search_bit = 0; /* take the 0 path on the next branch */
  831. else
  832. search_bit = ((last_rn >> i) & 0x1);
  833. /* Read two bits and write one bit */
  834. triplet_ret = w1_triplet(dev, search_bit);
  835. /* quit if no device responded */
  836. if ( (triplet_ret & 0x03) == 0x03 )
  837. break;
  838. /* If both directions were valid, and we took the 0 path... */
  839. if (triplet_ret == 0)
  840. last_zero = i;
  841. /* extract the direction taken & update the device number */
  842. tmp64 = (triplet_ret >> 2);
  843. rn |= (tmp64 << i);
  844. if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
  845. mutex_unlock(&dev->bus_mutex);
  846. dev_dbg(&dev->dev, "Abort w1_search\n");
  847. return;
  848. }
  849. }
  850. mutex_unlock(&dev->bus_mutex);
  851. if ( (triplet_ret & 0x03) != 0x03 ) {
  852. if ((desc_bit == last_zero) || (last_zero < 0)) {
  853. last_device = 1;
  854. dev->search_id = 0;
  855. } else {
  856. dev->search_id = rn;
  857. }
  858. desc_bit = last_zero;
  859. cb(dev, rn);
  860. }
  861. if (!last_device && slave_count == dev->max_slave_count &&
  862. !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
  863. /* Only max_slave_count will be scanned in a search,
  864. * but it will start where it left off next search
  865. * until all ids are identified and then it will start
  866. * over. A continued search will report the previous
  867. * last id as the first id (provided it is still on the
  868. * bus).
  869. */
  870. dev_info(&dev->dev, "%s: max_slave_count %d reached, "
  871. "will continue next search.\n", __func__,
  872. dev->max_slave_count);
  873. set_bit(W1_WARN_MAX_COUNT, &dev->flags);
  874. }
  875. }
  876. }
  877. void w1_search_process_cb(struct w1_master *dev, u8 search_type,
  878. w1_slave_found_callback cb)
  879. {
  880. struct w1_slave *sl, *sln;
  881. mutex_lock(&dev->list_mutex);
  882. list_for_each_entry(sl, &dev->slist, w1_slave_entry)
  883. clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
  884. mutex_unlock(&dev->list_mutex);
  885. w1_search_devices(dev, search_type, cb);
  886. mutex_lock(&dev->list_mutex);
  887. list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
  888. if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
  889. mutex_unlock(&dev->list_mutex);
  890. w1_slave_detach(sl);
  891. mutex_lock(&dev->list_mutex);
  892. }
  893. else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
  894. sl->ttl = dev->slave_ttl;
  895. }
  896. mutex_unlock(&dev->list_mutex);
  897. if (dev->search_count > 0)
  898. dev->search_count--;
  899. }
  900. static void w1_search_process(struct w1_master *dev, u8 search_type)
  901. {
  902. w1_search_process_cb(dev, search_type, w1_slave_found);
  903. }
  904. /**
  905. * w1_process_callbacks() - execute each dev->async_list callback entry
  906. * @dev: w1_master device
  907. *
  908. * The w1 master list_mutex must be held.
  909. *
  910. * Return: 1 if there were commands to executed 0 otherwise
  911. */
  912. int w1_process_callbacks(struct w1_master *dev)
  913. {
  914. int ret = 0;
  915. struct w1_async_cmd *async_cmd, *async_n;
  916. /* The list can be added to in another thread, loop until it is empty */
  917. while (!list_empty(&dev->async_list)) {
  918. list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
  919. async_entry) {
  920. /* drop the lock, if it is a search it can take a long
  921. * time */
  922. mutex_unlock(&dev->list_mutex);
  923. async_cmd->cb(dev, async_cmd);
  924. ret = 1;
  925. mutex_lock(&dev->list_mutex);
  926. }
  927. }
  928. return ret;
  929. }
  930. int w1_process(void *data)
  931. {
  932. struct w1_master *dev = (struct w1_master *) data;
  933. /* As long as w1_timeout is only set by a module parameter the sleep
  934. * time can be calculated in jiffies once.
  935. */
  936. const unsigned long jtime =
  937. usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
  938. /* remainder if it woke up early */
  939. unsigned long jremain = 0;
  940. for (;;) {
  941. if (!jremain && dev->search_count) {
  942. mutex_lock(&dev->mutex);
  943. w1_search_process(dev, W1_SEARCH);
  944. mutex_unlock(&dev->mutex);
  945. }
  946. mutex_lock(&dev->list_mutex);
  947. /* Note, w1_process_callback drops the lock while processing,
  948. * but locks it again before returning.
  949. */
  950. if (!w1_process_callbacks(dev) && jremain) {
  951. /* a wake up is either to stop the thread, process
  952. * callbacks, or search, it isn't process callbacks, so
  953. * schedule a search.
  954. */
  955. jremain = 1;
  956. }
  957. __set_current_state(TASK_INTERRUPTIBLE);
  958. /* hold list_mutex until after interruptible to prevent loosing
  959. * the wakeup signal when async_cmd is added.
  960. */
  961. mutex_unlock(&dev->list_mutex);
  962. if (kthread_should_stop())
  963. break;
  964. /* Only sleep when the search is active. */
  965. if (dev->search_count) {
  966. if (!jremain)
  967. jremain = jtime;
  968. jremain = schedule_timeout(jremain);
  969. }
  970. else
  971. schedule();
  972. }
  973. atomic_dec(&dev->refcnt);
  974. return 0;
  975. }
  976. static int __init w1_init(void)
  977. {
  978. int retval;
  979. pr_info("Driver for 1-wire Dallas network protocol.\n");
  980. w1_init_netlink();
  981. retval = bus_register(&w1_bus_type);
  982. if (retval) {
  983. pr_err("Failed to register bus. err=%d.\n", retval);
  984. goto err_out_exit_init;
  985. }
  986. retval = driver_register(&w1_master_driver);
  987. if (retval) {
  988. pr_err("Failed to register master driver. err=%d.\n",
  989. retval);
  990. goto err_out_bus_unregister;
  991. }
  992. retval = driver_register(&w1_slave_driver);
  993. if (retval) {
  994. pr_err("Failed to register slave driver. err=%d.\n",
  995. retval);
  996. goto err_out_master_unregister;
  997. }
  998. return 0;
  999. #if 0
  1000. /* For undoing the slave register if there was a step after it. */
  1001. err_out_slave_unregister:
  1002. driver_unregister(&w1_slave_driver);
  1003. #endif
  1004. err_out_master_unregister:
  1005. driver_unregister(&w1_master_driver);
  1006. err_out_bus_unregister:
  1007. bus_unregister(&w1_bus_type);
  1008. err_out_exit_init:
  1009. return retval;
  1010. }
  1011. static void __exit w1_fini(void)
  1012. {
  1013. struct w1_master *dev;
  1014. /* Set netlink removal messages and some cleanup */
  1015. list_for_each_entry(dev, &w1_masters, w1_master_entry)
  1016. __w1_remove_master_device(dev);
  1017. w1_fini_netlink();
  1018. driver_unregister(&w1_slave_driver);
  1019. driver_unregister(&w1_master_driver);
  1020. bus_unregister(&w1_bus_type);
  1021. }
  1022. module_init(w1_init);
  1023. module_exit(w1_fini);
  1024. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  1025. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
  1026. MODULE_LICENSE("GPL");