Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2192687iof; Tue, 7 Jun 2022 22:45:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxXVtvK3v3c+lhWDe/ZhbcUTFgCPY3Nvds8U/ld+m3bPaxQswRdaX4D+2DkTwGGt7qZ2ys X-Received: by 2002:a17:90b:1d90:b0:1e8:5a98:d591 with SMTP id pf16-20020a17090b1d9000b001e85a98d591mr21903295pjb.126.1654667136265; Tue, 07 Jun 2022 22:45:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667136; cv=none; d=google.com; s=arc-20160816; b=eFXxsI4pM89DC88/UGUoWmhi5Lli1uwBSSuF9om7VxEPknAJ6gY/vSN8KahK/zEopg OPoh22rFD/ejDBbBGbDw9lx/M3azVqvCurFMRQ26kKfqFeLogyj3nudERciiq6oacBs1 my+8VplwGrtIWEhLkjwjjl36WuKoy7ryNxjn6Ix22yd/4VGutkQmRntNpTjYMINgzcIz oHcRAjdrgqbloFXwkYoMaGftZ3TOzJTx8wMLotGdZF7LzrbhPLSMl1IdkOQ1DCjoI7AD 3CQUvqtrIyJllRDb9BhSUSgVeH9Xv/AScD0AM0I5rxkWtvF+e5QBHSoocPtMJ/PvK54I I/1A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kJibglPlrQbwZ+AhiHh8etcDfGncgXVeBaptXG5PGIc=; b=snrriep09LCiv/S9LucNLBN6IRViqlhOhbPq0ieiSdhKbBtzh/5VL4h6epkSt912Eg erjxFMPBkuUnAl73besqL4XmvZtVI7qitJBjN92inklduvtY2ZQ92+cBslmpg4Q3X/zj FduZHlHpXK/Ag/x6gcfUnrJ+crDMBOexxJCkC3FZyQ7Bka08XnemGH2Jk+rgORfYNZri xhInHM9/BXgL3OJmpuCWTcNmarCvjXlUmQ6hrVypc5l8k534tOTWUmCm9/FYi37olixu zo6bOFDpxpHAcIlszyniZGo/j6e69KbkVZuLhQ3y8QdZ866FCkHZU0TOKdCXdRHKOZ2o EztA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zkf+QKNq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id pi15-20020a17090b1e4f00b001e85049e709si19780733pjb.72.2022.06.07.22.45.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:45:36 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zkf+QKNq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 98A703CFABF; Tue, 7 Jun 2022 22:10:40 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343759AbiFGO6O (ORCPT + 99 others); Tue, 7 Jun 2022 10:58:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343621AbiFGO5O (ORCPT ); Tue, 7 Jun 2022 10:57:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 377BAB7F8 for ; Tue, 7 Jun 2022 07:57:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B5DDCB81FD1 for ; Tue, 7 Jun 2022 14:57:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E398C34119; Tue, 7 Jun 2022 14:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654613830; bh=BwS0lmRYZxrb8yBXIGN+OmFGJ9VGr15bsAFQoqheMmc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zkf+QKNqZrS/KDoCZusx4wr+w4S8IeJ8+5AfoaxhStD7z6j1/mqMClra9Ib2a/9Ex fxms/hOKatXWAW6VBH8CmIqUirzFjjEFV2g7MKgSRMK3n3Fk9RpP1L8N1DOjsBCewX Es2aBt+QcP+RgkB+LiaRYXqUwoczgbMK/IrbBuEgtqqiQ/EApQ5AwUxjYt2LyX+gnT CwaJWCjxreAIvrqX0IukInMWKzM6bovLfOgheNZ+GznzNib8O7IVmD6HQYaB2v5NVq j1E45J1Z0uZPSKhvK+mAlxfDvk7x4wtTI/uyVkQFyDThvTZwM2PXsYJfJwbyAiuqUY ZdHuc6mRb+dWQ== From: Tzung-Bi Shih To: bleung@chromium.org, groeck@chromium.org Cc: chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, tzungbi@kernel.org Subject: [PATCH v2 15/15] platform/chrome: cros_ec_proto: return 0 on getting wake mask success Date: Tue, 7 Jun 2022 14:56:39 +0000 Message-Id: <20220607145639.2362750-16-tzungbi@kernel.org> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog In-Reply-To: <20220607145639.2362750-1-tzungbi@kernel.org> References: <20220607145639.2362750-1-tzungbi@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 cros_ec_get_host_event_wake_mask() used to return value from send_command() which is number of bytes for input payload on success (i.e. sizeof(struct ec_response_host_event_mask)). However, the callers don't need to know how many bytes are available. - Fix cros_ec_get_host_event_wake_mask() to return 0 on success; negative integers on error. - Add a Kunit test for guarding if send_command() returns 0 in get_host_event_wake_mask(). Signed-off-by: Tzung-Bi Shih --- Changes from v1: - Return 0 on success; otherwise, negative intergers. drivers/platform/chrome/cros_ec_proto.c | 23 ++-- drivers/platform/chrome/cros_ec_proto_test.c | 128 +++++++++++++++++++ 2 files changed, 142 insertions(+), 9 deletions(-) diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c index 9d96ed16244f..04c852aa790b 100644 --- a/drivers/platform/chrome/cros_ec_proto.c +++ b/drivers/platform/chrome/cros_ec_proto.c @@ -256,18 +256,23 @@ static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint3 msg->insize = sizeof(*r); ret = send_command(ec_dev, msg); - if (ret >= 0) { - mapped = cros_ec_map_error(msg->result); - if (mapped) { - ret = mapped; - goto exit; - } + if (ret < 0) + goto exit; + + mapped = cros_ec_map_error(msg->result); + if (mapped) { + ret = mapped; + goto exit; } - if (ret > 0) { - r = (struct ec_response_host_event_mask *)msg->data; - *mask = r->mask; + + if (ret == 0) { + ret = -EPROTO; + goto exit; } + r = (struct ec_response_host_event_mask *)msg->data; + *mask = r->mask; + ret = 0; exit: kfree(msg); return ret; diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c index af69410f2978..3ee0de337d53 100644 --- a/drivers/platform/chrome/cros_ec_proto_test.c +++ b/drivers/platform/chrome/cros_ec_proto_test.c @@ -1406,6 +1406,133 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct k } } +static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit *test) +{ + struct cros_ec_proto_test_priv *priv = test->priv; + struct cros_ec_device *ec_dev = &priv->ec_dev; + struct ec_xfer_mock *mock; + int ret; + + /* Set some garbage bytes. */ + ec_dev->host_event_wake_mask = U32_MAX; + + /* For cros_ec_get_proto_info() without passthru. */ + { + struct ec_response_get_protocol_info *data; + + mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data)); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + + /* + * Although it doesn't check the value, provides valid sizes so that + * cros_ec_query_all() allocates din and dout correctly. + */ + data = (struct ec_response_get_protocol_info *)mock->o_data; + data->max_request_packet_size = 0xbe; + data->max_response_packet_size = 0xef; + } + + /* For cros_ec_get_proto_info() with passthru. */ + { + mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + } + + /* For cros_ec_get_host_command_version_mask() for MKBP. */ + { + mock = cros_kunit_ec_xfer_mock_add(test, 0); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + } + + /* For cros_ec_get_host_command_version_mask() for host sleep v1. */ + { + mock = cros_kunit_ec_xfer_mock_add(test, 0); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + } + + /* For get_host_event_wake_mask(). */ + { + mock = cros_kunit_ec_xfer_mock_add(test, 0); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + } + + cros_ec_proto_test_query_all_pretest(test); + ret = cros_ec_query_all(ec_dev); + KUNIT_EXPECT_EQ(test, ret, 0); + + /* For cros_ec_get_proto_info() without passthru. */ + { + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO); + KUNIT_EXPECT_EQ(test, mock->msg.insize, + sizeof(struct ec_response_get_protocol_info)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); + } + + /* For cros_ec_get_proto_info() with passthru. */ + { + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, + EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) | + EC_CMD_GET_PROTOCOL_INFO); + KUNIT_EXPECT_EQ(test, mock->msg.insize, + sizeof(struct ec_response_get_protocol_info)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); + } + + /* For cros_ec_get_host_command_version_mask() for MKBP. */ + { + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS); + KUNIT_EXPECT_EQ(test, mock->msg.insize, + sizeof(struct ec_response_get_cmd_versions)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions)); + } + + /* For cros_ec_get_host_command_version_mask() for host sleep v1. */ + { + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS); + KUNIT_EXPECT_EQ(test, mock->msg.insize, + sizeof(struct ec_response_get_cmd_versions)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions)); + } + + /* For get_host_event_wake_mask(). */ + { + u32 mask; + + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK); + KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); + + mask = ec_dev->host_event_wake_mask; + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0); + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0); + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0); + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0); + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0); + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0); + KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0); + } +} + static void cros_ec_proto_test_release(struct device *dev) { } @@ -1471,6 +1598,7 @@ static struct kunit_case cros_ec_proto_test_cases[] = { KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep), KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep_return0), KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error), + KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return0), {} }; -- 2.36.1.255.ge46751e96f-goog