w1_ds2406.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * w1_ds2406.c - w1 family 12 (DS2406) driver
  3. * based on w1_ds2413.c by Mariusz Bialonczyk <manio@skyboo.net>
  4. *
  5. * Copyright (c) 2014 Scott Alfter <scott@alfter.us>
  6. *
  7. * This source code is licensed under the GNU General Public License,
  8. * Version 2. See the file COPYING for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/device.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/crc16.h>
  18. #include "../w1.h"
  19. #include "../w1_int.h"
  20. #include "../w1_family.h"
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Scott Alfter <scott@alfter.us>");
  23. MODULE_DESCRIPTION("w1 family 12 driver for DS2406 2 Pin IO");
  24. #define W1_F12_FUNC_READ_STATUS 0xAA
  25. #define W1_F12_FUNC_WRITE_STATUS 0x55
  26. static ssize_t w1_f12_read_state(
  27. struct file *filp, struct kobject *kobj,
  28. struct bin_attribute *bin_attr,
  29. char *buf, loff_t off, size_t count)
  30. {
  31. u8 w1_buf[6]={W1_F12_FUNC_READ_STATUS, 7, 0, 0, 0, 0};
  32. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  33. u16 crc=0;
  34. int i;
  35. ssize_t rtnval=1;
  36. if (off != 0)
  37. return 0;
  38. if (!buf)
  39. return -EINVAL;
  40. mutex_lock(&sl->master->bus_mutex);
  41. if (w1_reset_select_slave(sl)) {
  42. mutex_unlock(&sl->master->bus_mutex);
  43. return -EIO;
  44. }
  45. w1_write_block(sl->master, w1_buf, 3);
  46. w1_read_block(sl->master, w1_buf+3, 3);
  47. for (i=0; i<6; i++)
  48. crc=crc16_byte(crc, w1_buf[i]);
  49. if (crc==0xb001) /* good read? */
  50. *buf=((w1_buf[3]>>5)&3)|0x30;
  51. else
  52. rtnval=-EIO;
  53. mutex_unlock(&sl->master->bus_mutex);
  54. return rtnval;
  55. }
  56. static ssize_t w1_f12_write_output(
  57. struct file *filp, struct kobject *kobj,
  58. struct bin_attribute *bin_attr,
  59. char *buf, loff_t off, size_t count)
  60. {
  61. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  62. u8 w1_buf[6]={W1_F12_FUNC_WRITE_STATUS, 7, 0, 0, 0, 0};
  63. u16 crc=0;
  64. int i;
  65. ssize_t rtnval=1;
  66. if (count != 1 || off != 0)
  67. return -EFAULT;
  68. mutex_lock(&sl->master->bus_mutex);
  69. if (w1_reset_select_slave(sl)) {
  70. mutex_unlock(&sl->master->bus_mutex);
  71. return -EIO;
  72. }
  73. w1_buf[3] = (((*buf)&3)<<5)|0x1F;
  74. w1_write_block(sl->master, w1_buf, 4);
  75. w1_read_block(sl->master, w1_buf+4, 2);
  76. for (i=0; i<6; i++)
  77. crc=crc16_byte(crc, w1_buf[i]);
  78. if (crc==0xb001) /* good read? */
  79. w1_write_8(sl->master, 0xFF);
  80. else
  81. rtnval=-EIO;
  82. mutex_unlock(&sl->master->bus_mutex);
  83. return rtnval;
  84. }
  85. #define NB_SYSFS_BIN_FILES 2
  86. static struct bin_attribute w1_f12_sysfs_bin_files[NB_SYSFS_BIN_FILES] = {
  87. {
  88. .attr = {
  89. .name = "state",
  90. .mode = S_IRUGO,
  91. },
  92. .size = 1,
  93. .read = w1_f12_read_state,
  94. },
  95. {
  96. .attr = {
  97. .name = "output",
  98. .mode = S_IRUGO | S_IWUSR | S_IWGRP,
  99. },
  100. .size = 1,
  101. .write = w1_f12_write_output,
  102. }
  103. };
  104. static int w1_f12_add_slave(struct w1_slave *sl)
  105. {
  106. int err = 0;
  107. int i;
  108. for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
  109. err = sysfs_create_bin_file(
  110. &sl->dev.kobj,
  111. &(w1_f12_sysfs_bin_files[i]));
  112. if (err)
  113. while (--i >= 0)
  114. sysfs_remove_bin_file(&sl->dev.kobj,
  115. &(w1_f12_sysfs_bin_files[i]));
  116. return err;
  117. }
  118. static void w1_f12_remove_slave(struct w1_slave *sl)
  119. {
  120. int i;
  121. for (i = NB_SYSFS_BIN_FILES - 1; i >= 0; --i)
  122. sysfs_remove_bin_file(&sl->dev.kobj,
  123. &(w1_f12_sysfs_bin_files[i]));
  124. }
  125. static struct w1_family_ops w1_f12_fops = {
  126. .add_slave = w1_f12_add_slave,
  127. .remove_slave = w1_f12_remove_slave,
  128. };
  129. static struct w1_family w1_family_12 = {
  130. .fid = W1_FAMILY_DS2406,
  131. .fops = &w1_f12_fops,
  132. };
  133. static int __init w1_f12_init(void)
  134. {
  135. return w1_register_family(&w1_family_12);
  136. }
  137. static void __exit w1_f12_exit(void)
  138. {
  139. w1_unregister_family(&w1_family_12);
  140. }
  141. module_init(w1_f12_init);
  142. module_exit(w1_f12_exit);