From patchwork Fri Aug 12 11:32:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 73845 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp125913qga; Fri, 12 Aug 2016 04:33:06 -0700 (PDT) X-Received: by 10.66.150.39 with SMTP id uf7mr26225659pab.23.1471001586391; Fri, 12 Aug 2016 04:33:06 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id si4si8583727pab.123.2016.08.12.04.33.06; Fri, 12 Aug 2016 04:33:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@baylibre-com.20150623.gappssmtp.com; spf=pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-i2c-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751982AbcHLLdE (ORCPT + 1 other); Fri, 12 Aug 2016 07:33:04 -0400 Received: from mail-wm0-f53.google.com ([74.125.82.53]:37266 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751895AbcHLLdD (ORCPT ); Fri, 12 Aug 2016 07:33:03 -0400 Received: by mail-wm0-f53.google.com with SMTP id i5so27214849wmg.0 for ; Fri, 12 Aug 2016 04:33:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=03Um3O9TqLLyqOc2Zy63zkYuj4yEevwoumVXdb6jRYk=; b=1Fqmw4t7N1wVfjF/ed0vH6+ysU8yYJA9528094vJRJq4OX9ryYsYgyP7alMVxbMg/3 UBlWG0PhJncbzxSbsXESheDNoUmGtcAIDHAis2qbYRdByUjUxhm+zG88mxy4wFrjaTRd SwZRngBoThO8d8ue7RQOjMs9uBRD+tMvy+9v6VEldyp3QMejU/0krXuP4AzutGluIbm1 2QuHrrWwiloj7eGmZs3nSLNuUxZr9iUvlGqtD17X1eTphPeLcnJGI5Sg3DEj9r3bCQAL 8A0kD3/94KGxr1Gf4qKcwzA0CmzIkPv26mo/Zd9XQSCYiecpf2grrp8QrDFK9jzxvT35 xWqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=03Um3O9TqLLyqOc2Zy63zkYuj4yEevwoumVXdb6jRYk=; b=SxId2npM/yKTTrBk+BATjz6wIPNc30m9QLgWm5D0/N5+TzMqMy+wybN3KzSMboMNcV XtLuFJGV5CgTLjA39LsdPUtWfEhw6BW1354NCle+VLgVDFPBIzum3Zxam8lO+9Q04sLD 5TxrYUAhq/lBYe7LdEuSIkM9UE5B0QkqLj68ttUgqEg8edhyQC8lMwRf4HyV1ibgmGLo 7tjIlXAZpxw/sPWNRm1yMBFHhVn9ybGNXli3+f1QFBBC/sEoMeUHZ3a3Hdq//KF8KFWH wVL5uQAjzB0IAgF4EUQwtLWgZ7ZIaNCJMZ1hshEphF8Oeslj500A0vONTuwq3frxl0Vh d6sw== X-Gm-Message-State: AEkoouuYMw+kRu17u+Ab5XQsXG1Hy7nKcMdGaCXuFg8lPYDCDD6qfCxiMR/PQv19aA5mEd7V X-Received: by 10.194.221.232 with SMTP id qh8mr14831337wjc.117.1471001582416; Fri, 12 Aug 2016 04:33:02 -0700 (PDT) Received: from bgdev-debian.lan (jua06-1-82-242-157-225.fbx.proxad.net. [82.242.157.225]) by smtp.gmail.com with ESMTPSA id i66sm2165235wmg.9.2016.08.12.04.33.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Aug 2016 04:33:01 -0700 (PDT) From: Bartosz Golaszewski To: Wolfram Sang , linux-i2c , LKML Cc: Bartosz Golaszewski Subject: [PATCH v2] eeprom: at24: check if the chip is functional in probe() Date: Fri, 12 Aug 2016 13:32:57 +0200 Message-Id: <1471001577-9269-1-git-send-email-bgolaszewski@baylibre.com> X-Mailer: git-send-email 2.1.4 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org The at24 driver doesn't check if the chip is functional in its probe function. This leads to instantiating devices that are not physically present. For example the cape EEPROMs for BeagleBone Black are defined in the device tree at four addresses on i2c2, but normally only one of them is present. If the userspace doesn't know the location in advance, it will need to check if reading the nvmem attributes fails to determine which EEPROM is actually there. Try to read a single byte in probe() and bail-out with -ENODEV if the read fails. Signed-off-by: Bartosz Golaszewski --- v1 -> v2: - renamed c to test_byte - changed the test_byte type to u8 (was char) - moved the check above the nvmem registration - moved the i2c_set_clientdata() call above both the nvmem registration and the test byte read - removed the error message drivers/misc/eeprom/at24.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-i2c" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c index 3cdf8e1..051b147 100644 --- a/drivers/misc/eeprom/at24.c +++ b/drivers/misc/eeprom/at24.c @@ -593,6 +593,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) struct at24_data *at24; int err; unsigned i, num_addresses; + u8 test_byte; if (client->dev.platform_data) { chip = *(struct at24_platform_data *)client->dev.platform_data; @@ -743,6 +744,18 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) } } + i2c_set_clientdata(client, at24); + + /* + * Perform a one-byte test read to verify that the + * chip is functional. + */ + err = at24_read(at24, 0, &test_byte, 1); + if (err) { + err = -ENODEV; + goto err_clients; + } + at24->nvmem_config.name = dev_name(&client->dev); at24->nvmem_config.dev = &client->dev; at24->nvmem_config.read_only = !writable; @@ -764,8 +777,6 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) goto err_clients; } - i2c_set_clientdata(client, at24); - dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n", chip.byte_len, client->name, writable ? "writable" : "read-only", at24->write_max);