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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
From 855a8d82506c7c2428e13beebe8dbf7739ea6176 Mon Sep 17 00:00:00 2001
From: Akira Shimahara <akira215corp@gmail.com>
Date: Mon, 11 May 2020 22:37:25 +0200
Subject: [PATCH] w1_therm: adding eeprom sysfs entry
commit 45d457a4cf24455eefd076a01a3d86414fc2ff1e upstream.
The driver implement 2 hardware functions to access device RAM:
* copy_scratchpad
* recall_scratchpad
They act according to device specifications.
As EEPROM operations are not device dependent (all w1_therm can perform
EEPROM read/write operation following the same protocol), it is removed
from device families structures.
Updating Documentation/ABI/testing/sysfs-driver-w1_therm accordingly.
Signed-off-by: Akira Shimahara <akira215corp@gmail.com>
Link: https://lore.kernel.org/r/20200511203725.410844-1-akira215corp@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
.../ABI/testing/sysfs-driver-w1_therm | 14 ++
drivers/w1/slaves/w1_therm.c | 175 ++++++++++++------
2 files changed, 132 insertions(+), 57 deletions(-)
--- a/Documentation/ABI/testing/sysfs-driver-w1_therm
+++ b/Documentation/ABI/testing/sysfs-driver-w1_therm
@@ -1,3 +1,17 @@
+What: /sys/bus/w1/devices/.../eeprom
+Date: May 2020
+Contact: Akira Shimahara <akira215corp@gmail.com>
+Description:
+ (WO) writing that file will either trigger a save of the
+ device data to its embedded EEPROM, either restore data
+ embedded in device EEPROM. Be aware that devices support
+ limited EEPROM writing cycles (typical 50k)
+ * 'save': save device RAM to EEPROM
+ * 'restore': restore EEPROM data in device RAM
+Users: any user space application which wants to communicate with
+ w1_term device
+
+
What: /sys/bus/w1/devices/.../ext_power
Date: May 2020
Contact: Akira Shimahara <akira215corp@gmail.com>
--- a/drivers/w1/slaves/w1_therm.c
+++ b/drivers/w1/slaves/w1_therm.c
@@ -43,12 +43,21 @@
static int w1_strong_pullup = 1;
module_param_named(strong_pullup, w1_strong_pullup, int, 0);
+/* This command should be in public header w1.h but is not */
+#define W1_RECALL_EEPROM 0xB8
+
/* Nb of try for an operation */
#define W1_THERM_MAX_TRY 5
/* ms delay to retry bus mutex */
#define W1_THERM_RETRY_DELAY 20
+/* delay in ms to write in EEPROM */
+#define W1_THERM_EEPROM_WRITE_DELAY 10
+
+#define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */
+#define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */
+
/* Helpers Macros */
/*
@@ -86,7 +95,6 @@ module_param_named(strong_pullup, w1_str
* @convert: pointer to the device conversion function
* @set_resolution: pointer to the device set_resolution function
* @get_resolution: pointer to the device get_resolution function
- * @eeprom: pointer to eeprom function
*/
struct w1_therm_family_converter {
u8 broken;
@@ -95,7 +103,6 @@ struct w1_therm_family_converter {
int (*convert)(u8 rom[9]);
int (*set_resolution)(struct w1_slave *sl, int val);
int (*get_resolution)(struct w1_slave *sl);
- int (*eeprom)(struct device *device);
};
/**
@@ -166,6 +173,22 @@ static int read_scratchpad(struct w1_sla
static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
/**
+ * copy_scratchpad() - Copy the content of scratchpad in device EEPROM
+ * @sl: slave involved
+ *
+ * Return: 0 if success, -kernel error code otherwise
+ */
+static int copy_scratchpad(struct w1_slave *sl);
+
+/**
+ * recall_eeprom() - Restore EEPROM data to device RAM
+ * @sl: slave involved
+ *
+ * Return: 0 if success, -kernel error code otherwise
+ */
+static int recall_eeprom(struct w1_slave *sl);
+
+/**
* read_powermode() - Query the power mode of the slave
* @sl: slave to retrieve the power mode
*
@@ -199,12 +222,16 @@ static ssize_t resolution_show(struct de
static ssize_t resolution_store(struct device *device,
struct device_attribute *attr, const char *buf, size_t size);
+static ssize_t eeprom_store(struct device *device,
+ struct device_attribute *attr, const char *buf, size_t size);
+
/* Attributes declarations */
static DEVICE_ATTR_RW(w1_slave);
static DEVICE_ATTR_RO(w1_seq);
static DEVICE_ATTR_RO(ext_power);
static DEVICE_ATTR_RW(resolution);
+static DEVICE_ATTR_WO(eeprom);
/* Interface Functions declaration */
@@ -234,12 +261,14 @@ static struct attribute *w1_therm_attrs[
&dev_attr_w1_slave.attr,
&dev_attr_ext_power.attr,
&dev_attr_resolution.attr,
+ &dev_attr_eeprom.attr,
NULL,
};
static struct attribute *w1_ds18s20_attrs[] = {
&dev_attr_w1_slave.attr,
&dev_attr_ext_power.attr,
+ &dev_attr_eeprom.attr,
NULL,
};
@@ -248,6 +277,7 @@ static struct attribute *w1_ds28ea00_att
&dev_attr_w1_seq.attr,
&dev_attr_ext_power.attr,
&dev_attr_resolution.attr,
+ &dev_attr_eeprom.attr,
NULL,
};
@@ -359,9 +389,6 @@ static struct w1_family w1_therm_family_
/* Device dependent func */
-/* write configuration to eeprom */
-static inline int w1_therm_eeprom(struct device *device);
-
static inline int w1_DS18B20_write_data(struct w1_slave *sl,
const u8 *data)
{
@@ -477,35 +504,30 @@ static struct w1_therm_family_converter
.convert = w1_DS18S20_convert_temp,
.set_resolution = NULL, /* no config register */
.get_resolution = NULL, /* no config register */
- .eeprom = w1_therm_eeprom
},
{
.f = &w1_therm_family_DS1822,
.convert = w1_DS18B20_convert_temp,
.set_resolution = w1_DS18B20_set_resolution,
.get_resolution = w1_DS18B20_get_resolution,
- .eeprom = w1_therm_eeprom
},
{
.f = &w1_therm_family_DS18B20,
.convert = w1_DS18B20_convert_temp,
.set_resolution = w1_DS18B20_set_resolution,
.get_resolution = w1_DS18B20_get_resolution,
- .eeprom = w1_therm_eeprom
},
{
.f = &w1_therm_family_DS28EA00,
.convert = w1_DS18B20_convert_temp,
.set_resolution = w1_DS18B20_set_resolution,
.get_resolution = w1_DS18B20_get_resolution,
- .eeprom = w1_therm_eeprom
},
{
.f = &w1_therm_family_DS1825,
.convert = w1_DS18B20_convert_temp,
.set_resolution = w1_DS18B20_set_resolution,
.get_resolution = w1_DS18B20_get_resolution,
- .eeprom = w1_therm_eeprom
}
};
@@ -838,75 +860,94 @@ error:
return ret;
}
-static inline int w1_therm_eeprom(struct device *device)
+static int copy_scratchpad(struct w1_slave *sl)
{
- struct w1_slave *sl = dev_to_w1_slave(device);
- struct w1_master *dev = sl->master;
- u8 rom[9], external_power;
- int ret, max_trying = 10;
- u8 *family_data = sl->family_data;
+ struct w1_master *dev_master = sl->master;
+ int max_trying = W1_THERM_MAX_TRY;
+ int t_write, ret = -ENODEV;
+ bool strong_pullup;
- if (!sl->family_data) {
- ret = -ENODEV;
+ if (!sl->family_data)
goto error;
- }
+
+ t_write = W1_THERM_EEPROM_WRITE_DELAY;
+ strong_pullup = (w1_strong_pullup == 2 ||
+ (!SLAVE_POWERMODE(sl) &&
+ w1_strong_pullup));
/* prevent the slave from going away in sleep */
- atomic_inc(THERM_REFCNT(family_data));
+ atomic_inc(THERM_REFCNT(sl->family_data));
- ret = mutex_lock_interruptible(&dev->bus_mutex);
- if (ret != 0)
+ if (!bus_mutex_lock(&dev_master->bus_mutex)) {
+ ret = -EAGAIN; /* Didn't acquire the mutex */
goto dec_refcnt;
+ }
- memset(rom, 0, sizeof(rom));
-
- while (max_trying--) {
+ while (max_trying-- && ret) { /* ret should be 0 */
+ /* safe version to select slave */
if (!reset_select_slave(sl)) {
- unsigned int tm = 10;
unsigned long sleep_rem;
- /* check if in parasite mode */
- w1_write_8(dev, W1_READ_PSUPPLY);
- external_power = w1_read_8(dev);
-
- if (reset_select_slave(sl))
- continue;
-
- /* 10ms strong pullup/delay after the copy command */
- if (w1_strong_pullup == 2 ||
- (!external_power && w1_strong_pullup))
- w1_next_pullup(dev, tm);
-
- w1_write_8(dev, W1_COPY_SCRATCHPAD);
-
- if (external_power) {
- mutex_unlock(&dev->bus_mutex);
+ /* 10ms strong pullup (or delay) after the convert */
+ if (strong_pullup)
+ w1_next_pullup(dev_master, t_write);
- sleep_rem = msleep_interruptible(tm);
- if (sleep_rem != 0) {
- ret = -EINTR;
- goto dec_refcnt;
- }
+ w1_write_8(dev_master, W1_COPY_SCRATCHPAD);
- ret = mutex_lock_interruptible(&dev->bus_mutex);
- if (ret != 0)
- goto dec_refcnt;
- } else if (!w1_strong_pullup) {
- sleep_rem = msleep_interruptible(tm);
+ if (strong_pullup) {
+ sleep_rem = msleep_interruptible(t_write);
if (sleep_rem != 0) {
ret = -EINTR;
goto mt_unlock;
}
}
-
- break;
+ ret = 0;
}
+
}
mt_unlock:
- mutex_unlock(&dev->bus_mutex);
+ mutex_unlock(&dev_master->bus_mutex);
dec_refcnt:
- atomic_dec(THERM_REFCNT(family_data));
+ atomic_dec(THERM_REFCNT(sl->family_data));
+error:
+ return ret;
+}
+
+static int recall_eeprom(struct w1_slave *sl)
+{
+ struct w1_master *dev_master = sl->master;
+ int max_trying = W1_THERM_MAX_TRY;
+ int ret = -ENODEV;
+
+ if (!sl->family_data)
+ goto error;
+
+ /* prevent the slave from going away in sleep */
+ atomic_inc(THERM_REFCNT(sl->family_data));
+
+ if (!bus_mutex_lock(&dev_master->bus_mutex)) {
+ ret = -EAGAIN; /* Didn't acquire the mutex */
+ goto dec_refcnt;
+ }
+
+ while (max_trying-- && ret) { /* ret should be 0 */
+ /* safe version to select slave */
+ if (!reset_select_slave(sl)) {
+
+ w1_write_8(dev_master, W1_RECALL_EEPROM);
+
+ ret = 1; /* Slave will pull line to 0 */
+ while (ret)
+ ret = 1 - w1_touch_bit(dev_master, 1);
+ }
+
+ }
+
+ mutex_unlock(&dev_master->bus_mutex);
+
+dec_refcnt:
+ atomic_dec(THERM_REFCNT(sl->family_data));
error:
return ret;
}
@@ -1006,7 +1047,7 @@ static ssize_t w1_slave_store(struct dev
}
if (val == 0) /* val=0 : trigger a EEPROM save */
- ret = SLAVE_SPECIFIC_FUNC(sl)->eeprom(device);
+ ret = copy_scratchpad(sl);
else {
if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
@@ -1104,6 +1145,26 @@ static ssize_t resolution_store(struct d
return size;
}
+
+static ssize_t eeprom_store(struct device *device,
+ struct device_attribute *attr, const char *buf, size_t size)
+{
+ struct w1_slave *sl = dev_to_w1_slave(device);
+ int ret = -EINVAL; /* Invalid argument */
+
+ if (size == sizeof(EEPROM_CMD_WRITE)) {
+ if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
+ ret = copy_scratchpad(sl);
+ } else if (size == sizeof(EEPROM_CMD_READ)) {
+ if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
+ ret = recall_eeprom(sl);
+ }
+
+ if (ret)
+ dev_info(device, "%s: error in process %d\n", __func__, ret);
+
+ return size;
+}
#if IS_REACHABLE(CONFIG_HWMON)
static int w1_read_temp(struct device *device, u32 attr, int channel,
|