From patchwork Wed Oct 25 19:52:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eddie James X-Patchwork-Id: 10027181 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D9BD66032C for ; Wed, 25 Oct 2017 19:53:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C080428C40 for ; Wed, 25 Oct 2017 19:53:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B56AB28C43; Wed, 25 Oct 2017 19:53:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83F5928C40 for ; Wed, 25 Oct 2017 19:53:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751857AbdJYTxC (ORCPT ); Wed, 25 Oct 2017 15:53:02 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:53992 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751702AbdJYTxB (ORCPT ); Wed, 25 Oct 2017 15:53:01 -0400 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v9PJoQHm010344 for ; Wed, 25 Oct 2017 15:53:01 -0400 Received: from e37.co.us.ibm.com (e37.co.us.ibm.com [32.97.110.158]) by mx0b-001b2d01.pphosted.com with ESMTP id 2du0k79fn0-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 25 Oct 2017 15:53:00 -0400 Received: from localhost by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 25 Oct 2017 13:52:59 -0600 Received: from b03cxnp07029.gho.boulder.ibm.com (9.17.130.16) by e37.co.us.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 25 Oct 2017 13:52:56 -0600 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v9PJqtJW9306566; Wed, 25 Oct 2017 12:52:55 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CB3B478037; Wed, 25 Oct 2017 13:52:55 -0600 (MDT) Received: from oc3016140333.ibm.com (unknown [9.41.174.252]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id 6D96B78043; Wed, 25 Oct 2017 13:52:55 -0600 (MDT) From: Eddie James To: linux@roeck-us.net Cc: jdelvare@suse.com, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, andrew@aj.id.au, eajames@linux.vnet.ibm.com, "Edward A. James" Subject: [prefix=PATCH] drivers: pmbus: core: Prevent calling pmbus_set_page with negative page Date: Wed, 25 Oct 2017 14:52:53 -0500 X-Mailer: git-send-email 1.8.3.1 X-TM-AS-GCONF: 00 x-cbid: 17102519-0024-0000-0000-0000176372C3 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007949; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000239; SDB=6.00936389; UDB=6.00471858; IPR=6.00716654; BA=6.00005660; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017713; XFM=3.00000015; UTC=2017-10-25 19:52:59 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17102519-0025-0000-0000-00004D3EC79C Message-Id: <1508961173-22686-1-git-send-email-eajames@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-25_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1710250261 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: "Edward A. James" For devices with word data registers, the pmbus core may call read/write word data functions with a page value of -1, in order to perform the operation without setting the page. However, the read/write word data functions accept only unsigned 8-bit page numbers, resulting in setting a page of 0xFF in this situation. This may result in errors or undefined behavior of some devices (specifically the ir35221, which allows the page to be set to 0xFF, but some subsequent operations to read registers may fail). Signed-off-by: Edward A. James --- drivers/hwmon/pmbus/pmbus.h | 4 ++-- drivers/hwmon/pmbus/pmbus_core.c | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus.h b/drivers/hwmon/pmbus/pmbus.h index 4efa2bd..4a16da6 100644 --- a/drivers/hwmon/pmbus/pmbus.h +++ b/drivers/hwmon/pmbus/pmbus.h @@ -405,8 +405,8 @@ struct pmbus_driver_info { void pmbus_clear_cache(struct i2c_client *client); int pmbus_set_page(struct i2c_client *client, u8 page); -int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg); -int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word); +int pmbus_read_word_data(struct i2c_client *client, int page, u8 reg); +int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg, u16 word); int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg); int pmbus_write_byte(struct i2c_client *client, int page, u8 value); int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 302f0ae..6d6e030 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -186,13 +186,16 @@ static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) return pmbus_write_byte(client, page, value); } -int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word) +int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg, + u16 word) { int rv; - rv = pmbus_set_page(client, page); - if (rv < 0) - return rv; + if (page >= 0) { + rv = pmbus_set_page(client, page); + if (rv < 0) + return rv; + } return i2c_smbus_write_word_data(client, reg, word); } @@ -219,13 +222,15 @@ static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, return pmbus_write_word_data(client, page, reg, word); } -int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg) +int pmbus_read_word_data(struct i2c_client *client, int page, u8 reg) { int rv; - rv = pmbus_set_page(client, page); - if (rv < 0) - return rv; + if (page >= 0) { + rv = pmbus_set_page(client, page); + if (rv < 0) + return rv; + } return i2c_smbus_read_word_data(client, reg); } @@ -269,9 +274,11 @@ int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value) { int rv; - rv = pmbus_set_page(client, page); - if (rv < 0) - return rv; + if (page >= 0) { + rv = pmbus_set_page(client, page); + if (rv < 0) + return rv; + } return i2c_smbus_write_byte_data(client, reg, value); }