w1_ds2423.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * w1_ds2423.c
  3. *
  4. * Copyright (c) 2010 Mika Laitio <lamikr@pilppa.org>
  5. *
  6. * This driver will read and write the value of 4 counters to w1_slave file in
  7. * sys filesystem.
  8. * Inspired by the w1_therm and w1_ds2431 drivers.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the therms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/device.h>
  28. #include <linux/types.h>
  29. #include <linux/delay.h>
  30. #include <linux/crc16.h>
  31. #include "../w1.h"
  32. #include "../w1_int.h"
  33. #include "../w1_family.h"
  34. #define CRC16_VALID 0xb001
  35. #define CRC16_INIT 0
  36. #define COUNTER_COUNT 4
  37. #define READ_BYTE_COUNT 42
  38. static ssize_t w1_counter_read(struct device *device,
  39. struct device_attribute *attr, char *buf);
  40. static struct device_attribute w1_counter_attr =
  41. __ATTR(w1_slave, S_IRUGO, w1_counter_read, NULL);
  42. static ssize_t w1_counter_read(struct device *device,
  43. struct device_attribute *attr, char *out_buf)
  44. {
  45. struct w1_slave *sl = dev_to_w1_slave(device);
  46. struct w1_master *dev = sl->master;
  47. u8 rbuf[COUNTER_COUNT * READ_BYTE_COUNT];
  48. u8 wrbuf[3];
  49. int rom_addr;
  50. int read_byte_count;
  51. int result;
  52. ssize_t c;
  53. int ii;
  54. int p;
  55. int crc;
  56. c = PAGE_SIZE;
  57. rom_addr = (12 << 5) + 31;
  58. wrbuf[0] = 0xA5;
  59. wrbuf[1] = rom_addr & 0xFF;
  60. wrbuf[2] = rom_addr >> 8;
  61. mutex_lock(&dev->mutex);
  62. if (!w1_reset_select_slave(sl)) {
  63. w1_write_block(dev, wrbuf, 3);
  64. read_byte_count = 0;
  65. for (p = 0; p < 4; p++) {
  66. /*
  67. * 1 byte for first bytes in ram page read
  68. * 4 bytes for counter
  69. * 4 bytes for zero bits
  70. * 2 bytes for crc
  71. * 31 remaining bytes from the ram page
  72. */
  73. read_byte_count += w1_read_block(dev,
  74. rbuf + (p * READ_BYTE_COUNT), READ_BYTE_COUNT);
  75. for (ii = 0; ii < READ_BYTE_COUNT; ++ii)
  76. c -= snprintf(out_buf + PAGE_SIZE - c,
  77. c, "%02x ",
  78. rbuf[(p * READ_BYTE_COUNT) + ii]);
  79. if (read_byte_count != (p + 1) * READ_BYTE_COUNT) {
  80. dev_warn(device,
  81. "w1_counter_read() returned %u bytes "
  82. "instead of %d bytes wanted.\n",
  83. read_byte_count,
  84. READ_BYTE_COUNT);
  85. c -= snprintf(out_buf + PAGE_SIZE - c,
  86. c, "crc=NO\n");
  87. } else {
  88. if (p == 0) {
  89. crc = crc16(CRC16_INIT, wrbuf, 3);
  90. crc = crc16(crc, rbuf, 11);
  91. } else {
  92. /*
  93. * DS2423 calculates crc from all bytes
  94. * read after the previous crc bytes.
  95. */
  96. crc = crc16(CRC16_INIT,
  97. (rbuf + 11) +
  98. ((p - 1) * READ_BYTE_COUNT),
  99. READ_BYTE_COUNT);
  100. }
  101. if (crc == CRC16_VALID) {
  102. result = 0;
  103. for (ii = 4; ii > 0; ii--) {
  104. result <<= 8;
  105. result |= rbuf[(p *
  106. READ_BYTE_COUNT) + ii];
  107. }
  108. c -= snprintf(out_buf + PAGE_SIZE - c,
  109. c, "crc=YES c=%d\n", result);
  110. } else {
  111. c -= snprintf(out_buf + PAGE_SIZE - c,
  112. c, "crc=NO\n");
  113. }
  114. }
  115. }
  116. } else {
  117. c -= snprintf(out_buf + PAGE_SIZE - c, c, "Connection error");
  118. }
  119. mutex_unlock(&dev->mutex);
  120. return PAGE_SIZE - c;
  121. }
  122. static int w1_f1d_add_slave(struct w1_slave *sl)
  123. {
  124. return device_create_file(&sl->dev, &w1_counter_attr);
  125. }
  126. static void w1_f1d_remove_slave(struct w1_slave *sl)
  127. {
  128. device_remove_file(&sl->dev, &w1_counter_attr);
  129. }
  130. static struct w1_family_ops w1_f1d_fops = {
  131. .add_slave = w1_f1d_add_slave,
  132. .remove_slave = w1_f1d_remove_slave,
  133. };
  134. static struct w1_family w1_family_1d = {
  135. .fid = W1_COUNTER_DS2423,
  136. .fops = &w1_f1d_fops,
  137. };
  138. static int __init w1_f1d_init(void)
  139. {
  140. return w1_register_family(&w1_family_1d);
  141. }
  142. static void __exit w1_f1d_exit(void)
  143. {
  144. w1_unregister_family(&w1_family_1d);
  145. }
  146. module_init(w1_f1d_init);
  147. module_exit(w1_f1d_exit);
  148. MODULE_LICENSE("GPL");
  149. MODULE_AUTHOR("Mika Laitio <lamikr@pilppa.org>");
  150. MODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");