1 From 4e767832ecdd029ca499d97d541c3154833dce25 Mon Sep 17 00:00:00 2001
2 From: Jean-Francois Dagenais <jeff.dagenais@gmail.com>
3 Date: Thu, 28 Mar 2019 12:41:11 -0400
4 Subject: [PATCH 492/782] w1: ds2408: reset on output_write retry with readback
6 commit 49695ac46861180baf2b2b92c62da8619b6bf28f upstream.
8 When we have success in 'Channel Access Write' but reading back latch
9 states fails, a write is retried without doing a proper slave reset.
10 This leads to protocol errors as the slave treats the next 'Channel
11 Access Write' as the continuation of previous command.
13 This commit is fixing this by making sure if the retry loop re-runs, a
14 reset is performed, whatever the failure (CONFIRM_BYTE or the read
17 The loop was quite due for a cleanup and this change mandated it. By
18 isolating the CONFIG_W1_SLAVE_DS2408_READBACK case into it's own
19 function, we vastly reduce the visual and branching(runtime and
22 Reported-by: Mariusz Bialonczyk <manio@skyboo.net>
23 Tested-by: Mariusz Bialonczyk <manio@skyboo.net>
24 Signed-off-by: Jean-Francois Dagenais <jeff.dagenais@gmail.com>
25 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
27 drivers/w1/slaves/w1_ds2408.c | 76 ++++++++++++++++++-----------------
28 1 file changed, 39 insertions(+), 37 deletions(-)
30 --- a/drivers/w1/slaves/w1_ds2408.c
31 +++ b/drivers/w1/slaves/w1_ds2408.c
32 @@ -138,14 +138,37 @@ static ssize_t status_control_read(struc
33 W1_F29_REG_CONTROL_AND_STATUS, buf);
36 +#ifdef fCONFIG_W1_SLAVE_DS2408_READBACK
37 +static bool optional_read_back_valid(struct w1_slave *sl, u8 expected)
41 + if (w1_reset_resume_command(sl->master))
44 + w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
45 + w1_buf[1] = W1_F29_REG_OUTPUT_LATCH_STATE;
48 + w1_write_block(sl->master, w1_buf, 3);
50 + return (w1_read_8(sl->master) == expected);
53 +static bool optional_read_back_valid(struct w1_slave *sl, u8 expected)
59 static ssize_t output_write(struct file *filp, struct kobject *kobj,
60 struct bin_attribute *bin_attr, char *buf,
61 loff_t off, size_t count)
63 struct w1_slave *sl = kobj_to_w1_slave(kobj);
66 unsigned int retries = W1_F29_RETRIES;
67 + ssize_t bytes_written = -EIO;
69 if (count != 1 || off != 0)
71 @@ -155,54 +178,33 @@ static ssize_t output_write(struct file
72 dev_dbg(&sl->dev, "mutex locked");
74 if (w1_reset_select_slave(sl))
80 w1_buf[0] = W1_F29_FUNC_CHANN_ACCESS_WRITE;
83 - w1_write_block(sl->master, w1_buf, 3);
85 - readBack = w1_read_8(sl->master);
86 + w1_write_block(sl->master, w1_buf, 3);
88 - if (readBack != W1_F29_SUCCESS_CONFIRM_BYTE) {
89 - if (w1_reset_resume_command(sl->master))
91 - /* try again, the slave is ready for a command */
93 + if (w1_read_8(sl->master) == W1_F29_SUCCESS_CONFIRM_BYTE &&
94 + optional_read_back_valid(sl, *buf)) {
99 -#ifdef CONFIG_W1_SLAVE_DS2408_READBACK
100 - /* here the master could read another byte which
101 - would be the PIO reg (the actual pin logic state)
102 - since in this driver we don't know which pins are
103 - in and outs, there's no value to read the state and
104 - compare. with (*buf) so end this command abruptly: */
105 if (w1_reset_resume_command(sl->master))
107 + goto out; /* unrecoverable error */
108 + /* try again, the slave is ready for a command */
109 + } while (--retries);
111 - /* go read back the output latches */
112 - /* (the direct effect of the write above) */
113 - w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
114 - w1_buf[1] = W1_F29_REG_OUTPUT_LATCH_STATE;
116 - w1_write_block(sl->master, w1_buf, 3);
117 - /* read the result of the READ_PIO_REGS command */
118 - if (w1_read_8(sl->master) == *buf)
122 - mutex_unlock(&sl->master->bus_mutex);
124 - "mutex unlocked, retries:%d", retries);
130 mutex_unlock(&sl->master->bus_mutex);
131 - dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries);
134 + dev_dbg(&sl->dev, "%s, mutex unlocked retries:%d\n",
135 + (bytes_written > 0) ? "succeeded" : "error", retries);
137 + return bytes_written;