10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/types.h>
16 #include <linux/slab.h>
21 #define CRC16_VALID 0xb001
24 #include "../w1_int.h"
25 #include "../w1_family.h"
37 static int w1_strong_pullup = 1;
41 static char w1_enable_crccheck = 1;
43 #define W1_EEPROM_SIZE 512
44 #define W1_PAGE_COUNT 16
45 #define W1_PAGE_SIZE 32
46 #define W1_PAGE_BITS 5
47 #define W1_PAGE_MASK 0x1F
49 #define W1_F1C_READ_EEPROM 0xF0
50 #define W1_F1C_WRITE_SCRATCH 0x0F
51 #define W1_F1C_READ_SCRATCH 0xAA
52 #define W1_F1C_COPY_SCRATCH 0x55
53 #define W1_F1C_ACCESS_WRITE 0x5A
55 #define W1_1C_REG_LOGIC_STATE 0x220
66 static inline size_t w1_f1C_fix_count(loff_t off,
size_t count,
size_t size)
71 if ((off + count) > size)
92 wrbuf[1] = off & 0xff;
104 static int w1_f1C_read(
struct w1_slave *sl,
int addr,
int len,
char *data)
113 wrbuf[1] = addr & 0xff;
114 wrbuf[2] = addr >> 8;
122 char *
buf, loff_t off,
size_t count)
124 struct w1_slave *sl = kobj_to_w1_slave(kobj);
126 int i, min_page, max_page;
134 if (w1_enable_crccheck) {
137 for (i = min_page; i <= max_page; i++) {
138 if (w1_f1C_refresh_block(sl, data, i)) {
145 count = w1_f1C_read(sl, off, count, buf);
166 static int w1_f1C_write(
struct w1_slave *sl,
int addr,
int len,
const u8 *data)
169 u8 rdbuf[W1_PAGE_SIZE + 3];
170 u8 es = (addr + len - 1) & 0x1f;
171 unsigned int tm = 10;
180 wrbuf[1] = addr & 0xff;
181 wrbuf[2] = addr >> 8;
194 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
195 (rdbuf[2] != es) || (
memcmp(data, &rdbuf[3], len) != 0))
205 for (i = 0; i <
sizeof(wrbuf); ++
i) {
208 if (w1_strong_pullup && i ==
sizeof(wrbuf)-1)
214 if (!w1_strong_pullup)
217 if (w1_enable_crccheck) {
230 char *buf, loff_t off,
size_t count)
233 struct w1_slave *sl = kobj_to_w1_slave(kobj);
240 if (w1_enable_crccheck) {
243 dev_err(&sl->dev,
"invalid offset/count off=%d cnt=%zd\n",
252 dev_err(&sl->dev,
"bad CRC at offset %d\n",
263 while (idx < count) {
266 if (len > (count - idx))
269 if (w1_f1C_write(sl, addr, len, &buf[idx]) < 0) {
284 char *buf, loff_t off,
size_t count)
287 struct w1_slave *sl = kobj_to_w1_slave(kobj);
291 if (off != 0 || count != 1 || buf ==
NULL)
303 char *buf, loff_t off,
size_t count)
306 struct w1_slave *sl = kobj_to_w1_slave(kobj);
311 if (off != 0 || count != 1 || buf ==
NULL)
344 if (
put_user(w1_enable_crccheck + 0x30, buf))
347 return sizeof(w1_enable_crccheck);
352 const char *buf,
size_t count)
356 if (count != 1 || !buf)
364 if (val != 0 && val != 1)
368 w1_enable_crccheck =
val;
370 return sizeof(w1_enable_crccheck);
373 #define NB_SYSFS_BIN_FILES 2
381 .read = w1_f1C_read_bin,
382 .write = w1_f1C_write_bin,
390 .read = w1_f1C_read_pio,
391 .write = w1_f1C_write_pio,
396 w1_f1C_show_crccheck, w1_f1C_store_crccheck);
398 static int w1_f1C_add_slave(
struct w1_slave *sl)
404 if (w1_enable_crccheck) {
408 sl->family_data =
data;
414 &sl->dev.kobj, &(w1_f1C_bin_attr[i]));
425 &sl->dev.kobj, &(w1_f1C_bin_attr[i]));
433 static void w1_f1C_remove_slave(
struct w1_slave *sl)
437 kfree(sl->family_data);
438 sl->family_data =
NULL;
449 .add_slave = w1_f1C_add_slave,
450 .remove_slave = w1_f1C_remove_slave,
455 .fops = &w1_f1C_fops,
458 static int __init w1_f1C_init(
void)
463 static void __exit w1_f1C_fini(
void)