Skip to contentSkip to conversation

w1_ds2408 linux driver

PATCH In review Jean-Francois Dagenais 3 participants 9 messages Feb 19, 2013 – Mar 15, 2013 1 message not in the archive W1 dallas's 1-wire bus

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Hi Olivier,

I'm forwarding this to Evgeniy and the linux-kernel mailing list as this thread may have historical value.

Quoted 4 lines · Olivier KSIKES

Dear Mr Dagenais,

I have just tried your w1-ds2408 linux driver on a TP link mini router running openwrt with w1-gpio. I was able to successfully update a 2408 outputs by writing a byte to the /output file. However, I would like to drive an LCD screen, and this method seems limited to 3/4 chars per second.

Yikes!

Quoted 2 lines

I had little more luck driving the 2408 thru the generic w1 driver (using the rw file), where I reached around 25 bytes/s.

This is a big surprise to me... Are you sure about the result, i.e. that no errors are occurring?

Quoted 1 line

This is however far from the actual 1wire bus capabilities.

Indeed. Did you try turning on the "dev_dbg" prints using "#define DEBUG" at the top of the file? This will give you clues as to whether bus errors are occurring and retries are made.

Quoted 3 lines

Thus, I'm wondering if there is a faster way to output a series of bytes to the 2408 using your driver? Is there any doc available apart from w1_ds2408.c comments?

A quick inspection of the w1_ds2408 driver reveals that for each byte write operation, we need to write 3 bytes, then read 1, then a bus reset, then the resume command (1byte) and after this we go back to read back to see if the value we wrote worked, which is again 3 bytes written, and 1 read. This is certainly much longer that doing it the easy way and may be a bit paranoid. I could see a patch that would make this double checking optional, say when the module is build with "#define DEBUG" or perhaps with a module option in the Kconfig which would have the double checking behaviour on by default.

The reason I designed it this way is because 1-wire busses tend to be close to flaky. Certainly the environment I developed the driver in was flaky. Finally we resolved the issues with pull-ups and what not, but there are other examples where the 1-wire bus spec is stretched a bit by certain component.

Having a Kconfig value could enable the double check code to be present for stabilization purposes, and then turned off once things work so it can run faster.

Send a patch!

Quoted 4 lines

With best regards and congratulations for your great work,

Olivier Ksikes

#2 lore raw
In reply to a message not in the archive
Quoted 8 lines · Olivier KSIKES

Dear Mr Dagenais,

I have just tried your w1-ds2408 linux driver on a TP link mini router running openwrt with w1-gpio. I was able to successfully update a 2408 outputs by writing a byte to the /output file. However, I would like to drive an LCD screen, and this method seems limited to 3/4 chars per second.

I had little more luck driving the 2408 thru the generic w1 driver (using the rw file), where I reached around 25 bytes/s. This is however far from the actual 1wire bus capabilities.

Quoted 6 lines

Thus, I'm wondering if there is a faster way to output a series of bytes to the 2408 using your driver? Is there any doc available apart from w1_ds2408.c comments?

With best regards and congratulations for your great work,

Olivier Ksikes

Here are my test results.

I have a simple main() that opens the /sys/bus/w1/devices/29-0000000xyz/output file and keeps it open.

I sweep from 0 to 255 in a tight loop that simply does

   for (int i=0; i <= 1024; ++i) {
      file.putChar(i);
      file.seek(0);
   }
   return 0;

I invoke this using "time" and do 1024/timespent to get:
Current driver (w/read-back ): ~48 chars/seconds Improved driver (wo/read-back): ~98 chars/seconds

patch will follow...

Cheers.

#3 lore raw

PATCH 1/2 w1: ds2408: make value read-back check a Kconfig option

De-activating this reading back will effectively half the time required for a write to the output register.

---
 drivers/w1/slaves/Kconfig     | 10 ++++++++++
 drivers/w1/slaves/w1_ds2408.c | 18 ++++++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)
drivers/w1/slaves/Kconfig +10 -0
index 762561f..5e6a3c9 100644
@@ -22,6 +22,16 @@ config W1_SLAVE_DS2408
 	  Say Y here if you want to use a 1-wire
 	  DS2408 8-Channel Addressable Switch device support
 
+config W1_SLAVE_DS2408_READBACK
+	bool "Read-back values written to DS2408's output register"
+	depends on W1_SLAVE_DS2408
+	default y
+	help
+	  Enabling this will cause the driver to read back the values written
+	  to the chip's output register in order to detect errors.
+
+	  This is slower but useful when debugging chips and/or busses.
+
 config W1_SLAVE_DS2413
 	tristate "Dual Channel Addressable Switch 0x3a family support (DS2413)"
 	help
drivers/w1/slaves/w1_ds2408.c +12 -6
index 441ad3a..25a5168 100644
@@ -178,6 +178,15 @@ static ssize_t w1_f29_write_output(
 		w1_write_block(sl->master, w1_buf, 3);
 
 		readBack = w1_read_8(sl->master);
+
+		if (readBack != W1_F29_SUCCESS_CONFIRM_BYTE) {
+			if (w1_reset_resume_command(sl->master))
+				goto error;
+			/* try again, the slave is ready for a command */
+			continue;
+		}
+
+#ifdef CONFIG_W1_SLAVE_DS2408_READBACK
 		/* here the master could read another byte which
 		   would be the PIO reg (the actual pin logic state)
 		   since in this driver we don't know which pins are
@@ -186,11 +195,6 @@ static ssize_t w1_f29_write_output(
 		if (w1_reset_resume_command(sl->master))
 			goto error;
 
-		if (readBack != 0xAA) {
-			/* try again, the slave is ready for a command */
-			continue;
-		}
-
 		/* go read back the output latches */
 		/* (the direct effect of the write above) */
 		w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
@@ -198,7 +202,9 @@ static ssize_t w1_f29_write_output(
 		w1_buf[2] = 0;
 		w1_write_block(sl->master, w1_buf, 3);
 		/* read the result of the READ_PIO_REGS command */
-		if (w1_read_8(sl->master) == *buf) {
+		if (w1_read_8(sl->master) == *buf)
+#endif
+		{
 			/* success! */
 			mutex_unlock(&sl->master->bus_mutex);
 			dev_dbg(&sl->dev,
Signature · 1 line
1.8.1.1
#4 lore raw

PATCH 2/2 w1: ds2408: use ARRAY_SIZE instead of hard-coded number

---
 drivers/w1/slaves/w1_ds2408.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/w1/slaves/w1_ds2408.c +3 -4
index 25a5168..e45eca1 100644
@@ -303,8 +303,7 @@ error:
 
 
 
-#define NB_SYSFS_BIN_FILES 6
-static struct bin_attribute w1_f29_sysfs_bin_files[NB_SYSFS_BIN_FILES] = {
+static struct bin_attribute w1_f29_sysfs_bin_files[] = {
 	{
 		.attr =	{
 			.name = "state",
@@ -363,7 +362,7 @@ static int w1_f29_add_slave(struct w1_slave *sl)
 	int err = 0;
 	int i;
 
-	for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
+	for (i = 0; i < ARRAY_SIZE(w1_f29_sysfs_bin_files) && !err; ++i)
 		err = sysfs_create_bin_file(
 			&sl->dev.kobj,
 			&(w1_f29_sysfs_bin_files[i]));
@@ -377,7 +376,7 @@ static int w1_f29_add_slave(struct w1_slave *sl)
 static void w1_f29_remove_slave(struct w1_slave *sl)
 {
 	int i;
-	for (i = NB_SYSFS_BIN_FILES - 1; i >= 0; --i)
+	for (i = ARRAY_SIZE(w1_f29_sysfs_bin_files) - 1; i >= 0; --i)
 		sysfs_remove_bin_file(&sl->dev.kobj,
 			&(w1_f29_sysfs_bin_files[i]));
 }
Signature · 1 line
1.8.1.1
#5 lore raw

Re: [PATCH 1/2] w1: ds2408: make value read-back check a Kconfig option

In reply to Jean-Francois Dagenais

Hi

Quoted 2 lines · Jean-Francois Dagenais

De-activating this reading back will effectively half the time required for a write to the output register.

Are you sure you want this to be compile-time option and not module parameter?

Signature · 1 line
	Evgeniy Polyakov
#6 lore raw

Re: [PATCH 1/2] w1: ds2408: make value read-back check a Kconfig option

Quoted 8 lines · Evgeniy Polyakov

Hi

Quoted 2 lines · Jean-Francois Dagenais

De-activating this reading back will effectively half the time required for a write to the output register.

Are you sure you want this to be compile-time option and not module parameter?

My experience showed that having this kind of check was only useful for HW/SW debug development phases, which usually require debug configs. Later in the development process, when HW and SW are stable, we usually optimize for actual production. Removing un-needed debug features and modules.

Assuming that, for historical reasons, implementing this new option as a module parameter would imply initializing it to "true" (i.e. read-back check enabled), then user-space or kernel invocation would have to override this to "false" in order to benefit from the enhancement. I felt that this was an awkward burden to propagate and maintain outside the kernel's config (.config). Especially since usually, the debug and release configs are meant exactly for this kind of stuff.

Also, it's not so off from the "Protect DS2433 data with a CRC16" option nearby.

We could do the "best of both worlds" and have the Kconfig influence the init value of a new module parameter which controls whether we read-back or not...

All that said, kernel style and good practices should win here over my own experiences as the extra run-time "if" check is not an issue really an issue on most platform.

Thoughts?
/jfd

#8 lore raw

PATCH 1/2 w1: ds2408: make value read-back check a Kconfig option

De-activating this reading back will effectively half the time required for a write to the output register.

---
 drivers/w1/slaves/Kconfig     | 10 ++++++++++
 drivers/w1/slaves/w1_ds2408.c | 18 ++++++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)
drivers/w1/slaves/Kconfig +10 -0
index 762561f..5e6a3c9 100644
@@ -22,6 +22,16 @@ config W1_SLAVE_DS2408
 	  Say Y here if you want to use a 1-wire
 	  DS2408 8-Channel Addressable Switch device support
 
+config W1_SLAVE_DS2408_READBACK
+	bool "Read-back values written to DS2408's output register"
+	depends on W1_SLAVE_DS2408
+	default y
+	help
+	  Enabling this will cause the driver to read back the values written
+	  to the chip's output register in order to detect errors.
+
+	  This is slower but useful when debugging chips and/or busses.
+
 config W1_SLAVE_DS2413
 	tristate "Dual Channel Addressable Switch 0x3a family support (DS2413)"
 	help
drivers/w1/slaves/w1_ds2408.c +12 -6
index 441ad3a..25a5168 100644
@@ -178,6 +178,15 @@ static ssize_t w1_f29_write_output(
 		w1_write_block(sl->master, w1_buf, 3);
 
 		readBack = w1_read_8(sl->master);
+
+		if (readBack != W1_F29_SUCCESS_CONFIRM_BYTE) {
+			if (w1_reset_resume_command(sl->master))
+				goto error;
+			/* try again, the slave is ready for a command */
+			continue;
+		}
+
+#ifdef CONFIG_W1_SLAVE_DS2408_READBACK
 		/* here the master could read another byte which
 		   would be the PIO reg (the actual pin logic state)
 		   since in this driver we don't know which pins are
@@ -186,11 +195,6 @@ static ssize_t w1_f29_write_output(
 		if (w1_reset_resume_command(sl->master))
 			goto error;
 
-		if (readBack != 0xAA) {
-			/* try again, the slave is ready for a command */
-			continue;
-		}
-
 		/* go read back the output latches */
 		/* (the direct effect of the write above) */
 		w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
@@ -198,7 +202,9 @@ static ssize_t w1_f29_write_output(
 		w1_buf[2] = 0;
 		w1_write_block(sl->master, w1_buf, 3);
 		/* read the result of the READ_PIO_REGS command */
-		if (w1_read_8(sl->master) == *buf) {
+		if (w1_read_8(sl->master) == *buf)
+#endif
+		{
 			/* success! */
 			mutex_unlock(&sl->master->bus_mutex);
 			dev_dbg(&sl->dev,
Signature · 1 line
1.8.1.1
#9 lore raw

PATCH 2/2 w1: ds2408: use ARRAY_SIZE instead of hard-coded number

---
 drivers/w1/slaves/w1_ds2408.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/w1/slaves/w1_ds2408.c +3 -4
index 25a5168..e45eca1 100644
@@ -303,8 +303,7 @@ error:
 
 
 
-#define NB_SYSFS_BIN_FILES 6
-static struct bin_attribute w1_f29_sysfs_bin_files[NB_SYSFS_BIN_FILES] = {
+static struct bin_attribute w1_f29_sysfs_bin_files[] = {
 	{
 		.attr =	{
 			.name = "state",
@@ -363,7 +362,7 @@ static int w1_f29_add_slave(struct w1_slave *sl)
 	int err = 0;
 	int i;
 
-	for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
+	for (i = 0; i < ARRAY_SIZE(w1_f29_sysfs_bin_files) && !err; ++i)
 		err = sysfs_create_bin_file(
 			&sl->dev.kobj,
 			&(w1_f29_sysfs_bin_files[i]));
@@ -377,7 +376,7 @@ static int w1_f29_add_slave(struct w1_slave *sl)
 static void w1_f29_remove_slave(struct w1_slave *sl)
 {
 	int i;
-	for (i = NB_SYSFS_BIN_FILES - 1; i >= 0; --i)
+	for (i = ARRAY_SIZE(w1_f29_sysfs_bin_files) - 1; i >= 0; --i)
 		sysfs_remove_bin_file(&sl->dev.kobj,
 			&(w1_f29_sysfs_bin_files[i]));
 }
Signature · 1 line
1.8.1.1