[PATCH 7/7] w1: ds2490: support buffer sizes bigger than 128

From: marc . ferland
Date: Fri Nov 17 2023 - 14:30:41 EST


From: Marc Ferland <marc.ferland@xxxxxxxxxxxx>

The ds_read_block and ds_write_block functions only support buffer
sizes up to 128 bytes, which is the depth of the fifo on the ds2490.

Sending bigger buffers will fail with a: -110 (ETIMEDOUT) from
usb_control_msg():

usb 5-1: Failed to write 1-wire data to ep0x2: err=-110.

Also, from the datasheet (2995.pdf, page 22, BLOCK I/O command):

For a block write sequence the EP2 FIFO must be pre-filled with
data before command execution. Additionally, for block sizes
greater then the FIFO size, the FIFO content status must be
monitored by host SW so that additional data can be sent to the
FIFO when necessary. A similar EP3 FIFO content monitoring
requirement exists for block read sequences. During a block read
the number of bytes loaded into the EP3 FIFO must be monitored so
that the data can be read before the FIFO overflows.

Breaking the buffer in 128 bytes blocks and simply calling the
original code sequence has solved the issue for me.

Tested with a DS1490F usb<->one-wire adapter and a DS28EC20 eeprom
memory.

Signed-off-by: Marc Ferland <marc.ferland@xxxxxxxxxxxx>
---
drivers/w1/masters/ds2490.c | 45 ++++++++++++++++++++++++++++++++-----
1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/drivers/w1/masters/ds2490.c b/drivers/w1/masters/ds2490.c
index 5f5b97e24700..d0a1a0b8b7ff 100644
--- a/drivers/w1/masters/ds2490.c
+++ b/drivers/w1/masters/ds2490.c
@@ -98,6 +98,8 @@
#define ST_EPOF 0x80
/* Status transfer size, 16 bytes status, 16 byte result flags */
#define ST_SIZE 0x20
+/* 1-wire data i/o fifo size, 128 bytes */
+#define FIFO_SIZE 0x80

/* Result Register flags */
#define RR_DETECT 0xA5 /* New device detected */
@@ -614,14 +616,11 @@ static int ds_read_byte(struct ds_device *dev, u8 *byte)
return 0;
}

-static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
+static int __read_block(struct ds_device *dev, u8 *buf, int len)
{
struct ds_status st;
int err;

- if (len > 64*1024)
- return -E2BIG;
-
memset(buf, 0xFF, len);

err = ds_send_data(dev, buf, len);
@@ -640,7 +639,25 @@ static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
return err;
}

-static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
+static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
+{
+ int err, to_read, rem = len;
+
+ if (len > 64*1024)
+ return -E2BIG;
+
+ do {
+ to_read = rem <= FIFO_SIZE ? rem : FIFO_SIZE;
+ err = __read_block(dev, &buf[len - rem], to_read);
+ if (err < 0)
+ return err;
+ rem -= to_read;
+ } while (rem);
+
+ return err;
+}
+
+static int __write_block(struct ds_device *dev, u8 *buf, int len)
{
int err;
struct ds_status st;
@@ -665,6 +682,24 @@ static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
return !(err == len);
}

+static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
+{
+ int err, to_write, rem = len;
+
+ if (len > 64*1024)
+ return -E2BIG;
+
+ do {
+ to_write = rem <= FIFO_SIZE ? rem : FIFO_SIZE;
+ err = __write_block(dev, &buf[len - rem], to_write);
+ if (err < 0)
+ return err;
+ rem -= to_write;
+ } while (rem);
+
+ return err;
+}
+
static void ds9490r_search(void *data, struct w1_master *master,
u8 search_type, w1_slave_found_callback callback)
{
--
2.34.1