w1_ds2406.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <linux/w1.h>
  19. #define W1_FAMILY_DS2406 0x12
  20. #define W1_F12_FUNC_READ_STATUS 0xAA
  21. #define W1_F12_FUNC_WRITE_STATUS 0x55
  22. static ssize_t w1_f12_read_state(
  23. struct file *filp, struct kobject *kobj,
  24. struct bin_attribute *bin_attr,
  25. char *buf, loff_t off, size_t count)
  26. {
  27. u8 w1_buf[6]={W1_F12_FUNC_READ_STATUS, 7, 0, 0, 0, 0};
  28. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  29. u16 crc=0;
  30. int i;
  31. ssize_t rtnval=1;
  32. if (off != 0)
  33. return 0;
  34. if (!buf)
  35. return -EINVAL;
  36. mutex_lock(&sl->master->bus_mutex);
  37. if (w1_reset_select_slave(sl)) {
  38. mutex_unlock(&sl->master->bus_mutex);
  39. return -EIO;
  40. }
  41. w1_write_block(sl->master, w1_buf, 3);
  42. w1_read_block(sl->master, w1_buf+3, 3);
  43. for (i=0; i<6; i++)
  44. crc=crc16_byte(crc, w1_buf[i]);
  45. if (crc==0xb001) /* good read? */
  46. *buf=((w1_buf[3]>>5)&3)|0x30;
  47. else
  48. rtnval=-EIO;
  49. mutex_unlock(&sl->master->bus_mutex);
  50. return rtnval;
  51. }
  52. static ssize_t w1_f12_write_output(
  53. struct file *filp, struct kobject *kobj,
  54. struct bin_attribute *bin_attr,
  55. char *buf, loff_t off, size_t count)
  56. {
  57. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  58. u8 w1_buf[6]={W1_F12_FUNC_WRITE_STATUS, 7, 0, 0, 0, 0};
  59. u16 crc=0;
  60. int i;
  61. ssize_t rtnval=1;
  62. if (count != 1 || off != 0)
  63. return -EFAULT;
  64. mutex_lock(&sl->master->bus_mutex);
  65. if (w1_reset_select_slave(sl)) {
  66. mutex_unlock(&sl->master->bus_mutex);
  67. return -EIO;
  68. }
  69. w1_buf[3] = (((*buf)&3)<<5)|0x1F;
  70. w1_write_block(sl->master, w1_buf, 4);
  71. w1_read_block(sl->master, w1_buf+4, 2);
  72. for (i=0; i<6; i++)
  73. crc=crc16_byte(crc, w1_buf[i]);
  74. if (crc==0xb001) /* good read? */
  75. w1_write_8(sl->master, 0xFF);
  76. else
  77. rtnval=-EIO;
  78. mutex_unlock(&sl->master->bus_mutex);
  79. return rtnval;
  80. }
  81. #define NB_SYSFS_BIN_FILES 2
  82. static struct bin_attribute w1_f12_sysfs_bin_files[NB_SYSFS_BIN_FILES] = {
  83. {
  84. .attr = {
  85. .name = "state",
  86. .mode = S_IRUGO,
  87. },
  88. .size = 1,
  89. .read = w1_f12_read_state,
  90. },
  91. {
  92. .attr = {
  93. .name = "output",
  94. .mode = S_IRUGO | S_IWUSR | S_IWGRP,
  95. },
  96. .size = 1,
  97. .write = w1_f12_write_output,
  98. }
  99. };
  100. static int w1_f12_add_slave(struct w1_slave *sl)
  101. {
  102. int err = 0;
  103. int i;
  104. for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
  105. err = sysfs_create_bin_file(
  106. &sl->dev.kobj,
  107. &(w1_f12_sysfs_bin_files[i]));
  108. if (err)
  109. while (--i >= 0)
  110. sysfs_remove_bin_file(&sl->dev.kobj,
  111. &(w1_f12_sysfs_bin_files[i]));
  112. return err;
  113. }
  114. static void w1_f12_remove_slave(struct w1_slave *sl)
  115. {
  116. int i;
  117. for (i = NB_SYSFS_BIN_FILES - 1; i >= 0; --i)
  118. sysfs_remove_bin_file(&sl->dev.kobj,
  119. &(w1_f12_sysfs_bin_files[i]));
  120. }
  121. static struct w1_family_ops w1_f12_fops = {
  122. .add_slave = w1_f12_add_slave,
  123. .remove_slave = w1_f12_remove_slave,
  124. };
  125. static struct w1_family w1_family_12 = {
  126. .fid = W1_FAMILY_DS2406,
  127. .fops = &w1_f12_fops,
  128. };
  129. module_w1_family(w1_family_12);
  130. MODULE_AUTHOR("Scott Alfter <scott@alfter.us>");
  131. MODULE_DESCRIPTION("w1 family 12 driver for DS2406 2 Pin IO");
  132. MODULE_LICENSE("GPL");