From patchwork Mon Jun 19 14:12:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alvin_=C5=A0ipraga?= X-Patchwork-Id: 110037 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3050314vqr; Mon, 19 Jun 2023 07:37:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ438sV5lfZTv0aZ9nCvPWEPOYWct9mbpE//RATNH7oQ3vDur1/c5S/sMxmKcWaZ0+LO4bWp X-Received: by 2002:a17:90a:414c:b0:25e:c00b:f639 with SMTP id m12-20020a17090a414c00b0025ec00bf639mr9015680pjg.30.1687185422102; Mon, 19 Jun 2023 07:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687185422; cv=none; d=google.com; s=arc-20160816; b=PhgckXgBHG4cLrv+0lmpf9CBe+WjLBnZG3CpXqTLN3fb5oLuFHGgcxNvEuI9tftWJQ eIcIBjOkoYbT8RkBoION4vxRS79GT46g/Jy2rLHafD0ep9xmy38IY1wmIU7EU9gJs/yc /dVMDGY3i07G4cf1K0bfguBVuoEKOih8x2Hg82LOD5ZCdlmDIALFC+JgiJKG/3hs1FTV sPYI8HVf5z4NB9Zv5sMCy3MiV9AQRsSmAdRPYUEhsrgW/AsXMG7QP6vNvhIfyCae877b ZQn+EZEUiBTrim3qQom7+1BEJ9gus7+0gPErJEyTfutz5A0Y7DtdFPtfwUInhosMrHGV 2/Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=iZZvuUg/XJppy1RQPzIvzHMS06C+pQD8xRtk2Ai4foU=; b=eOEhp/FPMBy6E/o8zyREiwl+7Bjh5Ti3UmBuJtphYzgpxMrqvhjiWE/mIJFVKoJZbq lZ/hbtlU/1D9cnC5SD3U/8QlCorfeP+IYEg0rP5gSVDtwSss57ozUatDvmHv/y+zFEh5 sD1IFo0ZUvy4Kbfut5sjFCXkKU1eNxq4aW7g7zDHVCPNSWA+XeDNXEOWjCv+gxPt+zSC hnns3Bzhwaz4+BZnnnwF6WTzRmK/ohM87jsiojbX+CasuFNqzA9uw2zcNrc+63/eC3op M5irNACrLrRp7eCw0nsXw4TEOCottUZd4LjpbhXQ368TKQ6OkQQLxk+/ux4vJQFq+lpB XoYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pqrs.dk header.s=google header.b=NzscS+z2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z6-20020a17090a468600b0025ed02c4fd8si5580259pjf.78.2023.06.19.07.36.49; Mon, 19 Jun 2023 07:37:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@pqrs.dk header.s=google header.b=NzscS+z2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232138AbjFSONT (ORCPT + 99 others); Mon, 19 Jun 2023 10:13:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbjFSONQ (ORCPT ); Mon, 19 Jun 2023 10:13:16 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2ED0599 for ; Mon, 19 Jun 2023 07:13:14 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-51a2c8e5a2cso5023094a12.2 for ; Mon, 19 Jun 2023 07:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqrs.dk; s=google; t=1687183992; x=1689775992; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=iZZvuUg/XJppy1RQPzIvzHMS06C+pQD8xRtk2Ai4foU=; b=NzscS+z2Afd9uBqEqkAty/34jFNErBTkXTRKILnq/lKMpEqiZbRbWzWVArRQJQqj56 SRmVzl84OC9RaUWQaUC9/NmY9QhHVkue5RGZNwtIHglAR7SDY2CEl1fGL3DyGSx9Khbi plEhh5etCtzE9e4LLmRUykkqB2NTEd9TdVByizZbvak7sIDlnuLL5fwlTMDOsmerKOIh Hi6Jqjb2qtPQSPZfg8W+ZaLBJvGJ7+77hfwtQUPegXtGhyIfHaa6t27MugkPR1j28ixD 2Sy1IdhonaqvuFbnE99ON0SQlXojXihSnWa28PA7CNxj4wR5YE9UzjFQC5EiZLo425HB ckEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687183992; x=1689775992; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iZZvuUg/XJppy1RQPzIvzHMS06C+pQD8xRtk2Ai4foU=; b=iamf/Rc7Q/DbsbewHGCd3Th7I4+ULcw9x/sR5RwA3itQgEZJsS9++BWcwrlTnAutJ6 fhs53cXxsozkSWJzTs/EbYmS6GvcOkqzOD/Jjzjh1nlkqGiKJQXpzuQipPv2PjdSbzZT 98xIYWcgr88gpj/QU+4jOS+C3Pe8xXg/AYxQwn94QKQFQUKVLU+cj7Pq6w00oJlSEid3 Sh64Rj2ePf4P11Ln9NXJb/JMFoxnWSf616L0xxqputBkHtTnygOm4xunuSKHQjMqfsSK /6v7mHU1B8PM+VURzzmeL6xX+TU370jeZYL68669HVYRFnBmNt+k5akiabHwUzOOakvC K37w== X-Gm-Message-State: AC+VfDxhQ25g/HSddkL2e64bvuYmYd4aVYImP8vXjgT+mJiuZdqXPI9a EM9rLOireKC6yX4ok3etztFbFq7g0DNN6oGEexM= X-Received: by 2002:a05:6402:545:b0:51a:5abc:668d with SMTP id i5-20020a056402054500b0051a5abc668dmr1987364edx.3.1687183992326; Mon, 19 Jun 2023 07:13:12 -0700 (PDT) Received: from localhost.localdomain (80.71.142.18.ipv4.parknet.dk. [80.71.142.18]) by smtp.gmail.com with ESMTPSA id m1-20020aa7d341000000b0051a44a5636asm2788489edr.44.2023.06.19.07.13.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 07:13:12 -0700 (PDT) From: =?utf-8?q?Alvin_=C5=A0ipraga?= To: Jonathan Cameron , Lars-Peter Clausen , Marc Titinger Cc: =?utf-8?q?Alvin_=C5=A0ipraga?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] iio: adc: ina2xx: avoid NULL pointer dereference on OF device match Date: Mon, 19 Jun 2023 16:12:39 +0200 Message-Id: <20230619141239.2257392-1-alvin@pqrs.dk> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769142140986620484?= X-GMAIL-MSGID: =?utf-8?q?1769142140986620484?= From: Alvin Šipraga The affected lines were resulting in a NULL pointer dereference on our platform because the device tree contained the following list of compatible strings: power-sensor@40 { compatible = "ti,ina232", "ti,ina231"; ... }; Since the driver doesn't declare a compatible string "ti,ina232", the OF matching succeeds on "ti,ina231". But the I2C device ID info is populated via the first compatible string, cf. modalias population in of_i2c_get_board_info(). Since there is no "ina232" entry in the legacy I2C device ID table either, the struct i2c_device_id *id pointer in the probe function is NULL. Fix this by using the already populated type variable instead, which points to the proper driver data. Since the name is also wanted, add a generic one to the ina2xx_config table. Signed-off-by: Alvin Šipraga Fixes: c43a102e67db ("iio: ina2xx: add support for TI INA2xx Power Monitors") --- drivers/iio/adc/ina2xx-adc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c index 213526c1592f..aea83f369437 100644 --- a/drivers/iio/adc/ina2xx-adc.c +++ b/drivers/iio/adc/ina2xx-adc.c @@ -124,6 +124,7 @@ static const struct regmap_config ina2xx_regmap_config = { enum ina2xx_ids { ina219, ina226 }; struct ina2xx_config { + const char *name; u16 config_default; int calibration_value; int shunt_voltage_lsb; /* nV */ @@ -155,6 +156,7 @@ struct ina2xx_chip_info { static const struct ina2xx_config ina2xx_config[] = { [ina219] = { + .name = "ina219", .config_default = INA219_CONFIG_DEFAULT, .calibration_value = 4096, .shunt_voltage_lsb = 10000, @@ -164,6 +166,7 @@ static const struct ina2xx_config ina2xx_config[] = { .chip_id = ina219, }, [ina226] = { + .name = "ina226", .config_default = INA226_CONFIG_DEFAULT, .calibration_value = 2048, .shunt_voltage_lsb = 2500, @@ -996,7 +999,7 @@ static int ina2xx_probe(struct i2c_client *client) /* Patch the current config register with default. */ val = chip->config->config_default; - if (id->driver_data == ina226) { + if (type == ina226) { ina226_set_average(chip, INA226_DEFAULT_AVG, &val); ina226_set_int_time_vbus(chip, INA226_DEFAULT_IT, &val); ina226_set_int_time_vshunt(chip, INA226_DEFAULT_IT, &val); @@ -1015,7 +1018,7 @@ static int ina2xx_probe(struct i2c_client *client) } indio_dev->modes = INDIO_DIRECT_MODE; - if (id->driver_data == ina226) { + if (type == ina226) { indio_dev->channels = ina226_channels; indio_dev->num_channels = ARRAY_SIZE(ina226_channels); indio_dev->info = &ina226_info; @@ -1024,7 +1027,7 @@ static int ina2xx_probe(struct i2c_client *client) indio_dev->num_channels = ARRAY_SIZE(ina219_channels); indio_dev->info = &ina219_info; } - indio_dev->name = id->name; + indio_dev->name = id ? id->name : chip->config->name; ret = devm_iio_kfifo_buffer_setup(&client->dev, indio_dev, &ina2xx_setup_ops);