1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/device.h>
29#include <linux/types.h>
30#include <linux/delay.h>
31#include <linux/crc16.h>
32
33#include "../w1.h"
34#include "../w1_int.h"
35#include "../w1_family.h"
36
37#define CRC16_VALID 0xb001
38#define CRC16_INIT 0
39
40#define COUNTER_COUNT 4
41#define READ_BYTE_COUNT 42
42
43static ssize_t w1_slave_show(struct device *device,
44 struct device_attribute *attr, char *out_buf)
45{
46 struct w1_slave *sl = dev_to_w1_slave(device);
47 struct w1_master *dev = sl->master;
48 u8 rbuf[COUNTER_COUNT * READ_BYTE_COUNT];
49 u8 wrbuf[3];
50 int rom_addr;
51 int read_byte_count;
52 int result;
53 ssize_t c;
54 int ii;
55 int p;
56 int crc;
57
58 c = PAGE_SIZE;
59 rom_addr = (12 << 5) + 31;
60 wrbuf[0] = 0xA5;
61 wrbuf[1] = rom_addr & 0xFF;
62 wrbuf[2] = rom_addr >> 8;
63 mutex_lock(&dev->bus_mutex);
64 if (!w1_reset_select_slave(sl)) {
65 w1_write_block(dev, wrbuf, 3);
66 read_byte_count = 0;
67 for (p = 0; p < 4; p++) {
68
69
70
71
72
73
74
75 read_byte_count += w1_read_block(dev,
76 rbuf + (p * READ_BYTE_COUNT), READ_BYTE_COUNT);
77 for (ii = 0; ii < READ_BYTE_COUNT; ++ii)
78 c -= snprintf(out_buf + PAGE_SIZE - c,
79 c, "%02x ",
80 rbuf[(p * READ_BYTE_COUNT) + ii]);
81 if (read_byte_count != (p + 1) * READ_BYTE_COUNT) {
82 dev_warn(device,
83 "w1_counter_read() returned %u bytes "
84 "instead of %d bytes wanted.\n",
85 read_byte_count,
86 READ_BYTE_COUNT);
87 c -= snprintf(out_buf + PAGE_SIZE - c,
88 c, "crc=NO\n");
89 } else {
90 if (p == 0) {
91 crc = crc16(CRC16_INIT, wrbuf, 3);
92 crc = crc16(crc, rbuf, 11);
93 } else {
94
95
96
97
98 crc = crc16(CRC16_INIT,
99 (rbuf + 11) +
100 ((p - 1) * READ_BYTE_COUNT),
101 READ_BYTE_COUNT);
102 }
103 if (crc == CRC16_VALID) {
104 result = 0;
105 for (ii = 4; ii > 0; ii--) {
106 result <<= 8;
107 result |= rbuf[(p *
108 READ_BYTE_COUNT) + ii];
109 }
110 c -= snprintf(out_buf + PAGE_SIZE - c,
111 c, "crc=YES c=%d\n", result);
112 } else {
113 c -= snprintf(out_buf + PAGE_SIZE - c,
114 c, "crc=NO\n");
115 }
116 }
117 }
118 } else {
119 c -= snprintf(out_buf + PAGE_SIZE - c, c, "Connection error");
120 }
121 mutex_unlock(&dev->bus_mutex);
122 return PAGE_SIZE - c;
123}
124
125static DEVICE_ATTR_RO(w1_slave);
126
127static struct attribute *w1_f1d_attrs[] = {
128 &dev_attr_w1_slave.attr,
129 NULL,
130};
131ATTRIBUTE_GROUPS(w1_f1d);
132
133static struct w1_family_ops w1_f1d_fops = {
134 .groups = w1_f1d_groups,
135};
136
137static struct w1_family w1_family_1d = {
138 .fid = W1_COUNTER_DS2423,
139 .fops = &w1_f1d_fops,
140};
141
142static int __init w1_f1d_init(void)
143{
144 return w1_register_family(&w1_family_1d);
145}
146
147static void __exit w1_f1d_exit(void)
148{
149 w1_unregister_family(&w1_family_1d);
150}
151
152module_init(w1_f1d_init);
153module_exit(w1_f1d_exit);
154
155MODULE_LICENSE("GPL");
156MODULE_AUTHOR("Mika Laitio <lamikr@pilppa.org>");
157MODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");
158MODULE_ALIAS("w1-family-" __stringify(W1_COUNTER_DS2423));
159