0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/device.h>
0012 #include <linux/types.h>
0013 #include <linux/delay.h>
0014 #include <linux/slab.h>
0015 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0016 #include <linux/crc16.h>
0017
0018 #define CRC16_INIT 0
0019 #define CRC16_VALID 0xb001
0020
0021 #endif
0022
0023 #include <linux/w1.h>
0024
0025 #define W1_EEPROM_DS2433 0x23
0026
0027 #define W1_EEPROM_SIZE 512
0028 #define W1_PAGE_COUNT 16
0029 #define W1_PAGE_SIZE 32
0030 #define W1_PAGE_BITS 5
0031 #define W1_PAGE_MASK 0x1F
0032
0033 #define W1_F23_TIME 300
0034
0035 #define W1_F23_READ_EEPROM 0xF0
0036 #define W1_F23_WRITE_SCRATCH 0x0F
0037 #define W1_F23_READ_SCRATCH 0xAA
0038 #define W1_F23_COPY_SCRATCH 0x55
0039
0040 struct w1_f23_data {
0041 u8 memory[W1_EEPROM_SIZE];
0042 u32 validcrc;
0043 };
0044
0045
0046
0047
0048
0049 static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
0050 {
0051 if (off > size)
0052 return 0;
0053
0054 if ((off + count) > size)
0055 return (size - off);
0056
0057 return count;
0058 }
0059
0060 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0061 static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data,
0062 int block)
0063 {
0064 u8 wrbuf[3];
0065 int off = block * W1_PAGE_SIZE;
0066
0067 if (data->validcrc & (1 << block))
0068 return 0;
0069
0070 if (w1_reset_select_slave(sl)) {
0071 data->validcrc = 0;
0072 return -EIO;
0073 }
0074
0075 wrbuf[0] = W1_F23_READ_EEPROM;
0076 wrbuf[1] = off & 0xff;
0077 wrbuf[2] = off >> 8;
0078 w1_write_block(sl->master, wrbuf, 3);
0079 w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
0080
0081
0082 if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
0083 data->validcrc |= (1 << block);
0084
0085 return 0;
0086 }
0087 #endif
0088
0089 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
0090 struct bin_attribute *bin_attr, char *buf,
0091 loff_t off, size_t count)
0092 {
0093 struct w1_slave *sl = kobj_to_w1_slave(kobj);
0094 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0095 struct w1_f23_data *data = sl->family_data;
0096 int i, min_page, max_page;
0097 #else
0098 u8 wrbuf[3];
0099 #endif
0100
0101 if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
0102 return 0;
0103
0104 mutex_lock(&sl->master->bus_mutex);
0105
0106 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0107
0108 min_page = (off >> W1_PAGE_BITS);
0109 max_page = (off + count - 1) >> W1_PAGE_BITS;
0110 for (i = min_page; i <= max_page; i++) {
0111 if (w1_f23_refresh_block(sl, data, i)) {
0112 count = -EIO;
0113 goto out_up;
0114 }
0115 }
0116 memcpy(buf, &data->memory[off], count);
0117
0118 #else
0119
0120
0121 if (w1_reset_select_slave(sl)) {
0122 count = -EIO;
0123 goto out_up;
0124 }
0125
0126 wrbuf[0] = W1_F23_READ_EEPROM;
0127 wrbuf[1] = off & 0xff;
0128 wrbuf[2] = off >> 8;
0129 w1_write_block(sl->master, wrbuf, 3);
0130 w1_read_block(sl->master, buf, count);
0131
0132 #endif
0133
0134 out_up:
0135 mutex_unlock(&sl->master->bus_mutex);
0136
0137 return count;
0138 }
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
0153 {
0154 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0155 struct w1_f23_data *f23 = sl->family_data;
0156 #endif
0157 u8 wrbuf[4];
0158 u8 rdbuf[W1_PAGE_SIZE + 3];
0159 u8 es = (addr + len - 1) & 0x1f;
0160
0161
0162 if (w1_reset_select_slave(sl))
0163 return -1;
0164
0165 wrbuf[0] = W1_F23_WRITE_SCRATCH;
0166 wrbuf[1] = addr & 0xff;
0167 wrbuf[2] = addr >> 8;
0168
0169 w1_write_block(sl->master, wrbuf, 3);
0170 w1_write_block(sl->master, data, len);
0171
0172
0173 if (w1_reset_select_slave(sl))
0174 return -1;
0175
0176 w1_write_8(sl->master, W1_F23_READ_SCRATCH);
0177 w1_read_block(sl->master, rdbuf, len + 3);
0178
0179
0180 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
0181 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
0182 return -1;
0183
0184
0185 if (w1_reset_select_slave(sl))
0186 return -1;
0187
0188 wrbuf[0] = W1_F23_COPY_SCRATCH;
0189 wrbuf[3] = es;
0190 w1_write_block(sl->master, wrbuf, 4);
0191
0192
0193 msleep(5);
0194
0195
0196 w1_reset_bus(sl->master);
0197 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0198 f23->validcrc &= ~(1 << (addr >> W1_PAGE_BITS));
0199 #endif
0200 return 0;
0201 }
0202
0203 static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
0204 struct bin_attribute *bin_attr, char *buf,
0205 loff_t off, size_t count)
0206 {
0207 struct w1_slave *sl = kobj_to_w1_slave(kobj);
0208 int addr, len, idx;
0209
0210 if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
0211 return 0;
0212
0213 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0214
0215 if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
0216 dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
0217 (int)off, count);
0218 return -EINVAL;
0219 }
0220
0221
0222 for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
0223 if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
0224 dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
0225 return -EINVAL;
0226 }
0227 }
0228 #endif
0229
0230 mutex_lock(&sl->master->bus_mutex);
0231
0232
0233 idx = 0;
0234 while (idx < count) {
0235 addr = off + idx;
0236 len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
0237 if (len > (count - idx))
0238 len = count - idx;
0239
0240 if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
0241 count = -EIO;
0242 goto out_up;
0243 }
0244 idx += len;
0245 }
0246
0247 out_up:
0248 mutex_unlock(&sl->master->bus_mutex);
0249
0250 return count;
0251 }
0252
0253 static BIN_ATTR_RW(eeprom, W1_EEPROM_SIZE);
0254
0255 static struct bin_attribute *w1_f23_bin_attributes[] = {
0256 &bin_attr_eeprom,
0257 NULL,
0258 };
0259
0260 static const struct attribute_group w1_f23_group = {
0261 .bin_attrs = w1_f23_bin_attributes,
0262 };
0263
0264 static const struct attribute_group *w1_f23_groups[] = {
0265 &w1_f23_group,
0266 NULL,
0267 };
0268
0269 static int w1_f23_add_slave(struct w1_slave *sl)
0270 {
0271 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0272 struct w1_f23_data *data;
0273
0274 data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
0275 if (!data)
0276 return -ENOMEM;
0277 sl->family_data = data;
0278
0279 #endif
0280 return 0;
0281 }
0282
0283 static void w1_f23_remove_slave(struct w1_slave *sl)
0284 {
0285 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
0286 kfree(sl->family_data);
0287 sl->family_data = NULL;
0288 #endif
0289 }
0290
0291 static const struct w1_family_ops w1_f23_fops = {
0292 .add_slave = w1_f23_add_slave,
0293 .remove_slave = w1_f23_remove_slave,
0294 .groups = w1_f23_groups,
0295 };
0296
0297 static struct w1_family w1_family_23 = {
0298 .fid = W1_EEPROM_DS2433,
0299 .fops = &w1_f23_fops,
0300 };
0301 module_w1_family(w1_family_23);
0302
0303 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
0304 MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM");
0305 MODULE_LICENSE("GPL");
0306 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2433));