Input: ads7846 - don't use scratch for tx_buf when clearing register
Subsystems (3)
PATCH 1/1 Input: ads7846 - don't use scratch for tx_buf when clearing register
The workaround for XPT2046 clears the command register, giving the touchscreen controller a NOP. The change incorrectly re-uses the req->scratch variable which is used as rx_buf for xfer[5], so by the time xfer[6] occurs, the contents of req->scratch may not be
0. It was found that the touchscreen controller can end up in a completely unresponsive state due to it being given a command the driver does not expect.
Instead, rely on the spi_transfer behavior of tx_buf being NULL to transmit all 0 bits, moving the 3 bytes to a single message.
This change was tested on real TSC2046 and ADS7843 controllers, but not the XPT2046 the workaround was originally created for. Confirming that the original modification to clear the command register does not impact either real controller.
--- drivers/input/touchscreen/ads7846.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/input/touchscreen/ads7846.c +4 -9
@@ -327,7 +327,7 @@ struct ser_req {u8 ref_off;u16 scratch;struct spi_message msg; -struct spi_transfer xfer[8 ]; +struct spi_transfer xfer[7 ];/* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines.
@@ -403,16 +403,11 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)spi_message_add_tail (&req->xfer[5 ], &req->msg);/* clear the command register */ - req->scratch =0 ; - req->xfer[6 ].tx_buf = &req->scratch; - req->xfer[6 ].len =1 ; + req->xfer[6 ].rx_buf = &req->scratch; + req->xfer[6 ].len =3 ; +CS_CHANGE (req->xfer[6 ]);spi_message_add_tail (&req->xfer[6 ], &req->msg); - req->xfer[7 ].rx_buf = &req->scratch; - req->xfer[7 ].len =2 ; -CS_CHANGE (req->xfer[7 ]); -spi_message_add_tail (&req->xfer[7 ], &req->msg); - scoped_guard(mutex, &ts->lock) {ads7846_stop (ts); status =spi_sync (spi, &req->msg);
base-commit: dd6c438c3e64a5ff0b5d7e78f7f9be547803ef1b
Signature · 1 line
2.34.1
Quoted 16 lines · Kris Bahnsen
The workaround for XPT2046 clears the command register, giving the touchscreen controller a NOP. The change incorrectly re-uses the
req->scratchvariable which is used asrx_buffor xfer[5], so by the time xfer[6] occurs, the contents ofreq->scratchmay not be
0. It was found that the touchscreen controller can end up in a completely unresponsive state due to it being given a command the driver does not expect.Instead, rely on the
spi_transferbehavior oftx_bufbeing NULL to transmit all 0 bits, moving the 3 bytes to a single message.This change was tested on real TSC2046 and ADS7843 controllers, but not the XPT2046 the workaround was originally created for. Confirming that the original modification to clear the command register does not impact either real controller.
Quoted 5 lines
Fixes 781a07da9bb94 ("Input: ads7846 - add dummy command register clearing cycle")Co-developed-by Mark Featherston <[email protected]>Signed-off-by Mark Featherston <[email protected]>Signed-off-by Kris Bahnsen <[email protected]>
Dmitry Torokhov
maintainerHi Kris,
Quoted 39 lines · Kris Bahnsen
The workaround for XPT2046 clears the command register, giving the touchscreen controller a NOP. The change incorrectly re-uses the
req->scratchvariable which is used asrx_buffor xfer[5], so by the time xfer[6] occurs, the contents ofreq->scratchmay not be
0. It was found that the touchscreen controller can end up in a completely unresponsive state due to it being given a command the driver does not expect.Instead, rely on the
spi_transferbehavior oftx_bufbeing NULL to transmit all 0 bits, moving the 3 bytes to a single message.This change was tested on real TSC2046 and ADS7843 controllers, but not the XPT2046 the workaround was originally created for. Confirming that the original modification to clear the command register does not impact either real controller.
Fixes 781a07da9bb94 ("Input: ads7846 - add dummy command register clearing cycle")Co-developed-by Mark Featherston <[email protected]>Signed-off-by Mark Featherston <[email protected]>Signed-off-by Kris Bahnsen <[email protected]>--- drivers/input/touchscreen/ads7846.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/input/touchscreen/ads7846.c +1
-1@@ -327,7 +327,7 @@ struct ser_req {u8 ref_off;u16 scratch;struct spi_message msg; -struct spi_transfer xfer[8 ]; +struct spi_transfer xfer[7 ];/* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines.
drivers/input/touchscreen/ads7846.c static int ads7846_read12_ser(struct device *dev, unsigned command) · line 407
Quoted 8 lines
drivers/input/touchscreen/ads7846.c +2
-3@@ -403,16 +403,11 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)spi_message_add_tail (&req->xfer[5 ], &req->msg);/* clear the command register */ - req->scratch =0 ; - req->xfer[6 ].tx_buf = &req->scratch; - req->xfer[6 ].len =1 ; + req->xfer[6 ].rx_buf = &req->scratch; + req->xfer[6 ].len =3 ;
Doesn't this overflow "scratch" which is only 2 bytes? I guess there is a hole in ser_req between "scratch" and "msg" but I do not think we should rely on this.
Can we also set rx_buf to NULL to discard incoming data?
[credit to sashiko].
Thanks.
Signature · 1 line
Dmitry
Quoted 55 lines · Dmitry Torokhov
Hi Kris,
Quoted 39 lines · Kris Bahnsen
The workaround for XPT2046 clears the command register, giving the touchscreen controller a NOP. The change incorrectly re-uses the
req->scratchvariable which is used asrx_buffor xfer[5], so by the time xfer[6] occurs, the contents ofreq->scratchmay not be
0. It was found that the touchscreen controller can end up in a completely unresponsive state due to it being given a command the driver does not expect.Instead, rely on the
spi_transferbehavior oftx_bufbeing NULL to transmit all 0 bits, moving the 3 bytes to a single message.This change was tested on real TSC2046 and ADS7843 controllers, but not the XPT2046 the workaround was originally created for. Confirming that the original modification to clear the command register does not impact either real controller.
Fixes 781a07da9bb94 ("Input: ads7846 - add dummy command register clearing cycle")Co-developed-by Mark Featherston <[email protected]>Signed-off-by Mark Featherston <[email protected]>Signed-off-by Kris Bahnsen <[email protected]>--- drivers/input/touchscreen/ads7846.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/input/touchscreen/ads7846.c +1
-1@@ -327,7 +327,7 @@ struct ser_req {u8 ref_off;u16 scratch;struct spi_message msg; -struct spi_transfer xfer[8 ]; +struct spi_transfer xfer[7 ];/* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines. drivers/input/touchscreen/ads7846.c static int ads7846_read12_ser(struct device *dev, unsigned command) · line 407
Quoted 8 lines
drivers/input/touchscreen/ads7846.c +2
-3@@ -403,16 +403,11 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)spi_message_add_tail (&req->xfer[5 ], &req->msg);/* clear the command register */ - req->scratch =0 ; - req->xfer[6 ].tx_buf = &req->scratch; - req->xfer[6 ].len =1 ; + req->xfer[6 ].rx_buf = &req->scratch; + req->xfer[6 ].len =3 ;Doesn't this overflow "scratch" which is only 2 bytes? I guess there is a hole in
ser_reqbetween "scratch" and "msg" but I do not think we should rely on this.
Quoted 1 line
Can we also set
rx_bufto NULL to discard incoming data?
Well spotted! I'm quite annoyed with myself that I fixed one pointer use bug to introduce a buffer overflow.
Will send a v2 patch later today.
Quoted 4 lines
[credit to sashiko].
Thanks.
Signature · 3 lines
Kris Bahnsen Software Engineer embeddedTS