public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Michael Lawnick <nospam_lawnick-Mmb7MZpHnFY@public.gmane.org>
To: linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: RFD: Illegal code in lm75.c (and other drivers?)
Date: Wed, 11 Mar 2009 12:30:31 +0100	[thread overview]
Message-ID: <gp87cn$vj6$1@ger.gmane.org> (raw)

Hi all,

Request For Discussion

I've already patched lm75.c for me in kernel 2.6.26.6, now I find the
same code again in 2.6.28.7:

> 	/* Now, we do the remaining detection. There is no identification-
> 	   dedicated register so we have to rely on several tricks:
> 	   unused bits, registers cycling over 8-address boundaries,
> 	   addresses 0x04-0x07 returning the last read value.
> 	   The cycling+unused addresses combination is not tested,
> 	   since it would significantly slow the detection down and would
> 	   hardly add any value. */
> 	if (kind < 0) {
> 		int cur, conf, hyst, os;
> 
> 		/* Unused addresses */
> 		cur = i2c_smbus_read_word_data(new_client, 0);
> 		conf = i2c_smbus_read_byte_data(new_client, 1);
> 		hyst = i2c_smbus_read_word_data(new_client, 2);
> 		if (i2c_smbus_read_word_data(new_client, 4) != hyst
> 		 || i2c_smbus_read_word_data(new_client, 5) != hyst
> 		 || i2c_smbus_read_word_data(new_client, 6) != hyst
> 		 || i2c_smbus_read_word_data(new_client, 7) != hyst)
> 			return -ENODEV;
>
> 		os = i2c_smbus_read_word_data(new_client, 3);
> 		if (i2c_smbus_read_word_data(new_client, 4) != os
> 		 || i2c_smbus_read_word_data(new_client, 5) != os
> 		 || i2c_smbus_read_word_data(new_client, 6) != os
> 		 || i2c_smbus_read_word_data(new_client, 7) != os)
> 			return -ENODEV;
> 
> 		/* Unused bits */
> 		if (conf & 0xe0)
> 			return -ENODEV;
> 
> 		/* Addresses cycling */
> 		for (i = 8; i < 0xff; i += 8)
> 			if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
> 			 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
> 			 || i2c_smbus_read_word_data(new_client, i + 3) != os)
> 				return -ENODEV;
> 	}
> 

LM75 Manual (National Semiconductors, April 2001) states on page 10:
> 1.11 POINTER REGISTER
> (Selects which registers will be read from or written to):
>           +--+--+--+--+--+--+----+----+
> |P7|P6|P5|P4|P3|P2| P1 | P0 |
> +--+--+--+--+--+--+----+----+
> | 0| 0| 0| 0| 0| 0|Register |
> |  |  |  |  |  |  | Select  |
> +--+--+--+--+--+--+----+----+
> 
> P0-P1: Register Select:
>           +--+--+------------------------------------------+
>           |P1|P0|Register                                  |
>           +--+--+------------------------------------------+
>           | 0| 0|Temperature (Read only) (Power-up default)|
>           +--+--+------------------------------------------+
>           | 0| 1|Configuration (Read/Write)                |
>           +--+--+------------------------------------------+
>           | 1| 0|THYST (Read/Write)                        |
>           +--+--+------------------------------------------+
>           | 1| 1|TOS (Read/Write)                          |
>           +--+--+------------------------------------------+
> 
> P2–P7: Must be kept zero.

Therefore the above code does illegal actions. It tries to access
offsets, that are not allowed.
The code may work on system with SMBUS controllers, but it does not (on
all) systems with IIC:
Already the offset-write phase fails (NACK from LM75) and so
i2c_smbus_read_byte_data() returns with an error code.

My proposal:
reduce LM75 code (and of all other devices that do same illegal things)
to test what is testable:

> 	if (kind < 0) {
> 		/* Check config register for the unused bits to be 0 */
> 		if (i2c_smbus_read_byte_data(new_client, 1) & 0xe0)
> 			return -ENODEV;
>
> 	}
>

Any comments before patch?

-- 
Kind regards,
Michael

             reply	other threads:[~2009-03-11 11:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-11 11:30 Michael Lawnick [this message]
2009-03-11 11:36 ` RFD: Illegal code in lm75.c (and other drivers?) Wolfram Sang
     [not found]   ` <20090311113603.GD3053-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2009-03-11 12:00     ` Michael Lawnick

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='gp87cn$vj6$1@ger.gmane.org' \
    --to=nospam_lawnick-mmb7mzphnfy@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox