w1.c 30 KB

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