Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  w1_ds2423.c
0004  *
0005  * Copyright (c) 2010 Mika Laitio <lamikr@pilppa.org>
0006  *
0007  * This driver will read and write the value of 4 counters to w1_slave file in
0008  * sys filesystem.
0009  * Inspired by the w1_therm and w1_ds2431 drivers.
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/moduleparam.h>
0015 #include <linux/device.h>
0016 #include <linux/types.h>
0017 #include <linux/delay.h>
0018 #include <linux/crc16.h>
0019 
0020 #include <linux/w1.h>
0021 
0022 #define W1_COUNTER_DS2423   0x1D
0023 
0024 #define CRC16_VALID 0xb001
0025 #define CRC16_INIT  0
0026 
0027 #define COUNTER_COUNT 4
0028 #define READ_BYTE_COUNT 42
0029 
0030 static ssize_t w1_slave_show(struct device *device,
0031                  struct device_attribute *attr, char *out_buf)
0032 {
0033     struct w1_slave *sl = dev_to_w1_slave(device);
0034     struct w1_master *dev = sl->master;
0035     u8 rbuf[COUNTER_COUNT * READ_BYTE_COUNT];
0036     u8 wrbuf[3];
0037     int rom_addr;
0038     int read_byte_count;
0039     int result;
0040     ssize_t c;
0041     int ii;
0042     int p;
0043     int crc;
0044 
0045     c       = PAGE_SIZE;
0046     rom_addr    = (12 << 5) + 31;
0047     wrbuf[0]    = 0xA5;
0048     wrbuf[1]    = rom_addr & 0xFF;
0049     wrbuf[2]    = rom_addr >> 8;
0050     mutex_lock(&dev->bus_mutex);
0051     if (!w1_reset_select_slave(sl)) {
0052         w1_write_block(dev, wrbuf, 3);
0053         read_byte_count = 0;
0054         for (p = 0; p < 4; p++) {
0055             /*
0056              * 1 byte for first bytes in ram page read
0057              * 4 bytes for counter
0058              * 4 bytes for zero bits
0059              * 2 bytes for crc
0060              * 31 remaining bytes from the ram page
0061              */
0062             read_byte_count += w1_read_block(dev,
0063                 rbuf + (p * READ_BYTE_COUNT), READ_BYTE_COUNT);
0064             for (ii = 0; ii < READ_BYTE_COUNT; ++ii)
0065                 c -= snprintf(out_buf + PAGE_SIZE - c,
0066                     c, "%02x ",
0067                     rbuf[(p * READ_BYTE_COUNT) + ii]);
0068             if (read_byte_count != (p + 1) * READ_BYTE_COUNT) {
0069                 dev_warn(device,
0070                     "w1_counter_read() returned %u bytes "
0071                     "instead of %d bytes wanted.\n",
0072                     read_byte_count,
0073                     READ_BYTE_COUNT);
0074                 c -= snprintf(out_buf + PAGE_SIZE - c,
0075                     c, "crc=NO\n");
0076             } else {
0077                 if (p == 0) {
0078                     crc = crc16(CRC16_INIT, wrbuf, 3);
0079                     crc = crc16(crc, rbuf, 11);
0080                 } else {
0081                     /*
0082                      * DS2423 calculates crc from all bytes
0083                      * read after the previous crc bytes.
0084                      */
0085                     crc = crc16(CRC16_INIT,
0086                         (rbuf + 11) +
0087                         ((p - 1) * READ_BYTE_COUNT),
0088                         READ_BYTE_COUNT);
0089                 }
0090                 if (crc == CRC16_VALID) {
0091                     result = 0;
0092                     for (ii = 4; ii > 0; ii--) {
0093                         result <<= 8;
0094                         result |= rbuf[(p *
0095                             READ_BYTE_COUNT) + ii];
0096                     }
0097                     c -= snprintf(out_buf + PAGE_SIZE - c,
0098                         c, "crc=YES c=%d\n", result);
0099                 } else {
0100                     c -= snprintf(out_buf + PAGE_SIZE - c,
0101                         c, "crc=NO\n");
0102                 }
0103             }
0104         }
0105     } else {
0106         c -= snprintf(out_buf + PAGE_SIZE - c, c, "Connection error");
0107     }
0108     mutex_unlock(&dev->bus_mutex);
0109     return PAGE_SIZE - c;
0110 }
0111 
0112 static DEVICE_ATTR_RO(w1_slave);
0113 
0114 static struct attribute *w1_f1d_attrs[] = {
0115     &dev_attr_w1_slave.attr,
0116     NULL,
0117 };
0118 ATTRIBUTE_GROUPS(w1_f1d);
0119 
0120 static const struct w1_family_ops w1_f1d_fops = {
0121     .groups     = w1_f1d_groups,
0122 };
0123 
0124 static struct w1_family w1_family_1d = {
0125     .fid = W1_COUNTER_DS2423,
0126     .fops = &w1_f1d_fops,
0127 };
0128 module_w1_family(w1_family_1d);
0129 
0130 MODULE_AUTHOR("Mika Laitio <lamikr@pilppa.org>");
0131 MODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");
0132 MODULE_LICENSE("GPL");
0133 MODULE_ALIAS("w1-family-" __stringify(W1_COUNTER_DS2423));