From patchwork Tue Jun 25 14:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 807488 Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C85116D9DA for ; Tue, 25 Jun 2024 14:54:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327274; cv=none; b=phYOVNa3xg6Nd6oFq4H8QmObvOFKM4IHZ8U+hzWiiLJmKbgivEt+5KVIwgXMLyYm6qrmSWaMl32Qjr43vhoSsJtzrEdo7xVtbUBFy+ERR0wsNuCxbThEkbJ4wpA+qZVh33FuVcpShCH5s7aVX0Hzs5whh7bXCxX6ipnAaV78UyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327274; c=relaxed/simple; bh=E+vOJjv2UvdlrXvKnRgzVGWs8xl24PgUq84JxgbpN+M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=H8LD20fgM+Prvd5IeQD0ps2JkW6MhJGgpCGAosbMOVow7AKOSrDgRtjSmicp8dca6c2CyGzc0RXo7SqUPoOLCIQuGweDtlNtmAAqIXvM+5xqbrekDfw/Nbo4taQGecA41xkDU5TnIaGDFNdmYXIV5I6vUKVqaEboys8LTdAkwwU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=ot8D9QpH; arc=none smtp.client-ip=209.85.167.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ot8D9QpH" Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-52bbf73f334so4352317e87.2 for ; Tue, 25 Jun 2024 07:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719327270; x=1719932070; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=JAfmKGbaHSRN3BDyb2Ps0ChAgC9WErixxC6u5jSyHcs=; b=ot8D9QpH0Xknfeg0Zy7p5K+JY98y55p+aozKKMdlJ0TInRBT9ZaZUkxHFxC5NzATSx TFWPcowvZvVWhuzfn67ldXmKbn28sIEx8A/qQUieuKfG/bZoLSFyqP6EE2ddX7VyFolp IwB+J4wNr9oYLrC4eqcRDV4/bbCsjQXhP7eOe1bjujUEhRIpsO2k6gl/4pyqiReVeUdZ ajXuYUBYmokH1oyMeSqLO8hYaqt//bk+WEjUf7daLXMz7lP45QH+0hPCjb6Cu+kl7Xhs 7pqBcD7lLK0vsYHUZelP0nPDP8axRfruo3M+GZJW60XqWUdLruD5ScNecRZMxWHVZCQD YYiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719327270; x=1719932070; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JAfmKGbaHSRN3BDyb2Ps0ChAgC9WErixxC6u5jSyHcs=; b=L0hT8KGF1A+lq36+ev9Io3jajF+D/NZs4fp5/c8fgYgDDShheZ0gKLY9at3aqcrjgo zvRDLiDTyFp9htp5dHjLmgPUgiTqD8pAx4FPbIJCXe4yI+UlibEWaB7gHbaNC+/VLtHV lotgJA+e7ZuAk1VlKvl3bISPaZ5c2gwtwN6TzweFSQrVQfP+acnBp2BYiDHR+qA3QgBG vObFjAYp9B/txzLZlBmOM6+aJEEYlpTsGkFuS/3+s2PBNnUHaFWCAe7xtIRyVUKhdUAM +V5TyXxf8/P24tVT214oiX1MNLY1UJ/9Si4elnSK+pTUZ/iVzGal5WL1j0ywpQNINTT+ b/Vw== X-Forwarded-Encrypted: i=1; AJvYcCU/O/es1S0BLHab1UfSjIyMqoZhXPHh6rWKWNEkfZYRCIMsLaOArQTUCZv+n7VtR90acMhXDkJBFn5SfyXKTxJtM6gwFKDd82Ct X-Gm-Message-State: AOJu0YxyDf/Ouf+eH8EqzJBeT7n1QrVt25+sQr5bcU9TUCn8S6cOxRTw lrD6U7hmB+f1mpbdi4PGDWsdVhh0nLUQWrZOHQ70+NNje+nHhpytZI1jyEAxFdo= X-Google-Smtp-Source: AGHT+IGZFf/nSGa2e2d62pxL5I+qOFEUEnjG9EKzs41xvEO8rAlHlgWJuddbGqx0uxqUQDIYsbz3Rw== X-Received: by 2002:a05:6512:453:b0:52c:d943:300f with SMTP id 2adb3069b0e04-52cdf7f6705mr5392914e87.38.1719327270161; Tue, 25 Jun 2024 07:54:30 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52cd64328b7sm1251877e87.221.2024.06.25.07.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 07:54:29 -0700 (PDT) From: Dmitry Baryshkov Date: Tue, 25 Jun 2024 17:54:26 +0300 Subject: [PATCH v3 1/7] usb: typec: ucsi: move ucsi_acknowledge() from ucsi_read_error() Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240625-ucsi-rework-interface-v3-1-7a6c8e17be3a@linaro.org> References: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> In-Reply-To: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> To: Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue Cc: Nikita Travkin , Neil Armstrong , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1294; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=E+vOJjv2UvdlrXvKnRgzVGWs8xl24PgUq84JxgbpN+M=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmetojmtp4D7cmDhXFyPFb6rzvpO9SqHUmDn68o x84cE4DSNWJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZnraIwAKCRCLPIo+Aiko 1dsqB/wKU/73Da/F7fXX3mKihmmgqnEt8z5DsYc/2Ek86sZ2QOVH5pcoOyQjGE2tEHJM1A460DU fniZ9SvZ17LHTf335zpqrqK1baAzCc7as/KU5FPsnsANZRV0di+AWzepIIFaNaN3bkEDgvyhGv3 D9plJZY56ZqgzDBYL4OkX/69YyQk2ndc/DcQOa6sbOSLN+5tjeH9gnPwh9coPGY9Cf2n3auuqJz 1jSXserA6xSdR0TK2kuorPqnD+k9K6yufTUWdeB5SNheTQwuDx4uTO2SUb42q1522ZwVKmxA7Bp KSyWF53xjvQ5qHB3NY1c+iU9Gf62IqbUUg5WbVjtBFRAZ1HM X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A As a preparation for reworking UCSI command handling, move ucsi_acknowledge() for the failed command from ucsi_read_error() to ucsi_exec_command(). Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 76c48d873b2a..297d939e8da8 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -70,11 +70,6 @@ static int ucsi_read_error(struct ucsi *ucsi) u16 error; int ret; - /* Acknowledge the command that failed */ - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; - ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS); if (ret < 0) return ret; @@ -153,13 +148,14 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) } if (cci & UCSI_CCI_ERROR) { - if (cmd == UCSI_GET_ERROR_STATUS) { - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; + /* Acknowledge the command that failed */ + ret = ucsi_acknowledge(ucsi, false); + if (ret) + return ret; + if (cmd == UCSI_GET_ERROR_STATUS) return -EIO; - } + return ucsi_read_error(ucsi); } From patchwork Tue Jun 25 14:54:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 807486 Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8ADDB16F0F6 for ; Tue, 25 Jun 2024 14:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327278; cv=none; b=BuIc+FP9WtGSScK191N/eLWz1taoCxkO22tb3IkVStWb763q3MMBaQTQwo89zGygvMoVGb9v6U0j5UqKR7vXfms5eZcnnjNov2DSbbTRsXwg3hPV3GOUoQlF09uZLHVTe5g0A7Au3SnBdlGJ2rdQCNVsPwuZxXKN+7vNG30aIRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327278; c=relaxed/simple; bh=4wZHRHUTLDa+Ajur4pcLq9kjg9ZZkfOeNHnW5AjdB+U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jSful9Yftzk41eHTexytS1fX3cQ8jPc5er7Myqq1iFciSrBDvm4N0a2psq8iEtRZ5AkRWXcwRJH1vABR2VgMZmUQVo10Ai2NRR/140VCAg/8O5VGDNb3JC4o9aUcIGXM0ZzyhpA7Azc7JsR9KpjCnpXsnlwYzDoiNpJcjgvlQ1c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=EOgUbgky; arc=none smtp.client-ip=209.85.167.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="EOgUbgky" Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-5295e488248so6429957e87.2 for ; Tue, 25 Jun 2024 07:54:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719327272; x=1719932072; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=JHzYKHNjBXf+9UzjvIdsaqBY3cbTBbTnQBUGbCvbkus=; b=EOgUbgkylyYKGo6OYlr4LUtG9dXh3I4yyLbigphEN1dzUcm00R21FIA/8l5ZL4NNDS O4/T87OnWLbGwz5H2yO9J/2Y/QFD4OyK654074SYYLPLNpmzMndDBzXKweb8hyczlSR/ UpVPe+teHkBbt8lP+ZcNMT+TDgPVArXOo2En9QXlvA/ns7JO4PkcytLLplTqCiRQDPIe 3nqat28UbGB6z4KnjjVEMAdVoOBNkW61ondZJHC6JDT4JRx0DRSNEVkbNmKPZKEvqpOz YSr9/is7c2ZzsCF+2cFWBctRr1eq771TJe2QYP8Ibp+X3bPqtIm9VQVdYlM8xt6pZtNs Acfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719327272; x=1719932072; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JHzYKHNjBXf+9UzjvIdsaqBY3cbTBbTnQBUGbCvbkus=; b=ugyRyHxAfgTHlxlYEUVX+cDhJati2zsfeLqkm3syJDHvnQV0SMqClMa+iwR6K+9TYw IMGBgch499w6F1kJT3B/q70MAnrQKRSbxfehOsWvd5vzG5Bkj3P5DP1TBYCIW6/dQPYP opkSOqmnG3go7NeoeyPdYTsM6CRru0Er8okpclOk9AdnAhjTbw2rm4LRwaWtO5ZIBgZY Wp3/TRX8u7zDHiBgW49n34PrXeCMqbuuV+Qjsqk3u1+6Db1wfsMqQr6R25FPyLSz31R6 /SQpAdSDGOIE9Epjm7S8bfwzB91Ym78dFeNscfdD3ZPtf9e2luh0+9Im6R8BOUEwyI66 Kt7A== X-Forwarded-Encrypted: i=1; AJvYcCX/9l0sH0U2Lxfkubm9aEarJIG0IauYR668fF4BR6jMmFC6lYzufLdRtd92UuS6vUOgnMi9mFbBPZVeVzUGwAHUNOPHIdGvmY49 X-Gm-Message-State: AOJu0YxYQb7U79p5t+dC04chbQ4D+tlqQ52j9oJCzHMDwBy//lrEsHmn 3RRVoNmKs+Inakub8AzAJnGtlGXkk+K9jN1eV6Wa5QN+05Z4JKEO/oH2dQDU4mc= X-Google-Smtp-Source: AGHT+IGhqQ+VBKIxaQ2lp2Pfy14XvRHw9UB90ZVR4jtUgoXS1297PMq+RIWx0NHafF59+uQIKs23DQ== X-Received: by 2002:ac2:550d:0:b0:52c:e126:3d50 with SMTP id 2adb3069b0e04-52ce183bf44mr4231354e87.36.1719327271692; Tue, 25 Jun 2024 07:54:31 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52cd64328b7sm1251877e87.221.2024.06.25.07.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 07:54:31 -0700 (PDT) From: Dmitry Baryshkov Date: Tue, 25 Jun 2024 17:54:28 +0300 Subject: [PATCH v3 3/7] usb: typec: ucsi: split read operation Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240625-ucsi-rework-interface-v3-3-7a6c8e17be3a@linaro.org> References: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> In-Reply-To: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> To: Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue Cc: Nikita Travkin , Neil Armstrong , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=16964; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=4wZHRHUTLDa+Ajur4pcLq9kjg9ZZkfOeNHnW5AjdB+U=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmetojz5ODXJomc1rapxF2ktWVUz9a/WEel79T5 OixCXVC8qKJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZnraIwAKCRCLPIo+Aiko 1YChB/9blK+zkNFOWhPUzE/TGHcI/2rtvvJI4F2Rg9lNVf8REPX01zLsxS7WGfj3I0CoSHjDJBm KvwwE/ZRjiTrLYdW4RxTF97d6LJQF2O6lWpXemKOfr/teiJav76MobxTQia8x/EV2Zlgw2e0KHi 9z5a9a8P1IsN6lAYlX2k9C75bn249CnAHnvCvqwfoa5Ea42VBS0ytYxLq5AdJ8x1/fFSV/vDBKt JiXwyITWCxjt4AN96lIs+zUBXxZL7ty0pIT7AtSQHgtftxRsDc5+qQfO38CHTsQaSGoorM6fsqH zKb88YD2sw/SYq0opHpHiOeYUzEe1mykDNGcDbICfvbWdB03 X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A The read operation is only used to read fixed data at fixed offsets (UCSI_VERSION, UCSI_CCI, UCSI_MESSAGE_IN). In some cases drivers apply offset-specific overrides. Split the read() operation into three operations, read_version(), read_cci(), read_message_in(). Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 20 ++++----- drivers/usb/typec/ucsi/ucsi.h | 9 +++-- drivers/usb/typec/ucsi/ucsi_acpi.c | 72 ++++++++++++++++++++++++++------- drivers/usb/typec/ucsi/ucsi_ccg.c | 50 ++++++++++++----------- drivers/usb/typec/ucsi/ucsi_glink.c | 19 ++++++++- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 19 ++++++++- drivers/usb/typec/ucsi/ucsi_yoga_c630.c | 52 +++++++++++++++--------- 7 files changed, 169 insertions(+), 72 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 52903eea7274..db25f51ff71d 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -46,7 +46,7 @@ static int ucsi_read_message_in(struct ucsi *ucsi, void *buf, if (ucsi->version <= UCSI_VERSION_1_2) buf_size = clamp(buf_size, 0, 16); - return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size); + return ucsi->ops->read_message_in(ucsi, buf, buf_size); } static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) @@ -130,7 +130,7 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) if (ret) return ret; - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret) return ret; @@ -1309,7 +1309,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) mutex_lock(&ucsi->ppm_lock); - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret < 0) goto out; @@ -1327,8 +1327,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); do { - ret = ucsi->ops->read(ucsi, UCSI_CCI, - &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret < 0) goto out; if (cci & UCSI_CCI_COMMAND_COMPLETE) @@ -1357,7 +1356,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) /* Give the PPM time to process a reset before reading CCI */ msleep(20); - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret) goto out; @@ -1777,7 +1776,7 @@ static int ucsi_init(struct ucsi *ucsi) ucsi->ntfy = ntfy; mutex_lock(&ucsi->ppm_lock); - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); mutex_unlock(&ucsi->ppm_lock); if (ret) return ret; @@ -1891,7 +1890,9 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) { struct ucsi *ucsi; - if (!ops || !ops->read || !ops->sync_control || !ops->async_control) + if (!ops || + !ops->read_version || !ops->read_cci || !ops->read_message_in || + !ops->sync_control || !ops->async_control) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); @@ -1927,8 +1928,7 @@ int ucsi_register(struct ucsi *ucsi) { int ret; - ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version, - sizeof(ucsi->version)); + ret = ucsi->ops->read_version(ucsi, &ucsi->version); if (ret) return ret; diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 3ad64acffda1..5c5a8c63090d 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -56,7 +56,9 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations - * @read: Read operation + * @read_version: Read implemented UCSI version + * @read_cci: Read CCI register + * @read_message_in: Read message data from UCSI * @sync_control: Blocking control operation * @async_control: Non-blocking control operation * @update_altmodes: Squashes duplicate DP altmodes @@ -68,8 +70,9 @@ struct dentry; * return immediately after sending the data to the PPM. */ struct ucsi_operations { - int (*read)(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len); + int (*read_version)(struct ucsi *ucsi, u16 *version); + int (*read_cci)(struct ucsi *ucsi, u32 *cci); + int (*read_message_in)(struct ucsi *ucsi, void *val, size_t val_len); int (*sync_control)(struct ucsi *ucsi, u64 command); int (*async_control)(struct ucsi *ucsi, u64 command); bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig, diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index f54e4722d8f6..3660dc3e6d32 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -46,8 +46,7 @@ static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func) return 0; } -static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_acpi_read_version(struct ucsi *ucsi, u16 *version) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); int ret; @@ -56,7 +55,35 @@ static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, if (ret) return ret; - memcpy(val, ua->base + offset, val_len); + memcpy(version, ua->base + UCSI_VERSION, sizeof(*version)); + + return 0; +} + +static int ucsi_acpi_read_cci(struct ucsi *ucsi, u32 *cci) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + int ret; + + ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); + if (ret) + return ret; + + memcpy(cci, ua->base + UCSI_CCI, sizeof(*cci)); + + return 0; +} + +static int ucsi_acpi_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + int ret; + + ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); + if (ret) + return ret; + + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); return 0; } @@ -99,36 +126,50 @@ static int ucsi_acpi_sync_control(struct ucsi *ucsi, u64 command) } static const struct ucsi_operations ucsi_acpi_ops = { - .read = ucsi_acpi_read, + .read_version = ucsi_acpi_read_version, + .read_cci = ucsi_acpi_read_cci, + .read_message_in = ucsi_acpi_read_message_in, .sync_control = ucsi_acpi_sync_control, .async_control = ucsi_acpi_async_control }; static int -ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len) +ucsi_zenbook_read_cci(struct ucsi *ucsi, u32 *cci) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); int ret; - if (offset == UCSI_VERSION || UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) { + if (UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) { ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); if (ret) return ret; } - memcpy(val, ua->base + offset, val_len); + memcpy(cci, ua->base + UCSI_CCI, sizeof(*cci)); + + return 0; +} + +static int +ucsi_zenbook_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + + /* UCSI_MESSAGE_IN is never read for PPM_RESET, return stored data */ + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); return 0; } static const struct ucsi_operations ucsi_zenbook_ops = { - .read = ucsi_zenbook_read, + .read_version = ucsi_acpi_read_version, + .read_cci = ucsi_zenbook_read_cci, + .read_message_in = ucsi_zenbook_read_message_in, .sync_control = ucsi_acpi_sync_control, .async_control = ucsi_acpi_async_control }; -static int ucsi_gram_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_gram_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) { u16 bogus_change = UCSI_CONSTAT_POWER_LEVEL_CHANGE | UCSI_CONSTAT_PDOS_CHANGE; @@ -136,13 +177,12 @@ static int ucsi_gram_read(struct ucsi *ucsi, unsigned int offset, struct ucsi_connector_status *status; int ret; - ret = ucsi_acpi_read(ucsi, offset, val, val_len); + ret = ucsi_acpi_read_message_in(ucsi, val, val_len); if (ret < 0) return ret; if (UCSI_COMMAND(ua->cmd) == UCSI_GET_CONNECTOR_STATUS && - test_bit(UCSI_ACPI_CHECK_BOGUS_EVENT, &ua->flags) && - offset == UCSI_MESSAGE_IN) { + test_bit(UCSI_ACPI_CHECK_BOGUS_EVENT, &ua->flags)) { status = (struct ucsi_connector_status *)val; /* Clear the bogus change */ @@ -173,7 +213,9 @@ static int ucsi_gram_sync_control(struct ucsi *ucsi, u64 command) } static const struct ucsi_operations ucsi_gram_ops = { - .read = ucsi_gram_read, + .read_version = ucsi_acpi_read_version, + .read_cci = ucsi_acpi_read_cci, + .read_message_in = ucsi_gram_read_message_in, .sync_control = ucsi_gram_sync_control, .async_control = ucsi_acpi_async_control }; @@ -203,7 +245,7 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data) u32 cci; int ret; - ret = ua->ucsi->ops->read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ua->ucsi->ops->read_cci(ua->ucsi, &cci); if (ret) return; diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 76b39bb9762d..6ccc394f268e 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -556,32 +556,34 @@ static void ucsi_ccg_nvidia_altmode(struct ucsi_ccg *uc, } } -static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_ccg_read_version(struct ucsi *ucsi, u16 *version) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); - u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset); - struct ucsi_capability *cap; - struct ucsi_altmode *alt; - int ret = 0; + u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_VERSION); - if (offset == UCSI_CCI) { - spin_lock(&uc->op_lock); - memcpy(val, &(uc->op_data).cci, val_len); - spin_unlock(&uc->op_lock); - } else if (offset == UCSI_MESSAGE_IN) { - spin_lock(&uc->op_lock); - memcpy(val, &(uc->op_data).message_in, val_len); - spin_unlock(&uc->op_lock); - } else { - ret = ccg_read(uc, reg, val, val_len); - } + return ccg_read(uc, reg, (u8 *)version, sizeof(*version)); +} - if (ret) - return ret; +static int ucsi_ccg_read_cci(struct ucsi *ucsi, u32 *cci) +{ + struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); - if (offset != UCSI_MESSAGE_IN) - return ret; + spin_lock(&uc->op_lock); + *cci = uc->op_data.cci; + spin_unlock(&uc->op_lock); + + return 0; +} + +static int ucsi_ccg_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); + struct ucsi_capability *cap; + struct ucsi_altmode *alt; + + spin_lock(&uc->op_lock); + memcpy(val, uc->op_data.message_in, val_len); + spin_unlock(&uc->op_lock); switch (UCSI_COMMAND(uc->last_cmd_sent)) { case UCSI_GET_CURRENT_CAM: @@ -607,7 +609,7 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, } uc->last_cmd_sent = 0; - return ret; + return 0; } static int ucsi_ccg_async_control(struct ucsi *ucsi, u64 command) @@ -663,7 +665,9 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command) } static const struct ucsi_operations ucsi_ccg_ops = { - .read = ucsi_ccg_read, + .read_version = ucsi_ccg_read_version, + .read_cci = ucsi_ccg_read_cci, + .read_message_in = ucsi_ccg_read_message_in, .sync_control = ucsi_ccg_sync_control, .async_control = ucsi_ccg_async_control, .update_altmodes = ucsi_ccg_update_altmodes diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index ebd76257c4fc..56bad054e78f 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -114,6 +114,21 @@ static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, return ret; } +static int pmic_glink_ucsi_read_version(struct ucsi *ucsi, u16 *version) +{ + return pmic_glink_ucsi_read(ucsi, UCSI_VERSION, version, sizeof(*version)); +} + +static int pmic_glink_ucsi_read_cci(struct ucsi *ucsi, u32 *cci) +{ + return pmic_glink_ucsi_read(ucsi, UCSI_CCI, cci, sizeof(*cci)); +} + +static int pmic_glink_ucsi_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + return pmic_glink_ucsi_read(ucsi, UCSI_MESSAGE_IN, val, val_len); +} + static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -214,7 +229,9 @@ static void pmic_glink_ucsi_connector_status(struct ucsi_connector *con) } static const struct ucsi_operations pmic_glink_ucsi_ops = { - .read = pmic_glink_ucsi_read, + .read_version = pmic_glink_ucsi_read_version, + .read_cci = pmic_glink_ucsi_read_cci, + .read_message_in = pmic_glink_ucsi_read_message_in, .sync_control = pmic_glink_ucsi_sync_control, .async_control = pmic_glink_ucsi_async_control, .update_connector = pmic_glink_ucsi_update_connector, diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index 396e2090e7c3..14737ca3724c 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -359,6 +359,21 @@ static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, return 0; } +static int ucsi_stm32g0_read_version(struct ucsi *ucsi, u16 *version) +{ + return ucsi_stm32g0_read(ucsi, UCSI_VERSION, version, sizeof(*version)); +} + +static int ucsi_stm32g0_read_cci(struct ucsi *ucsi, u32 *cci) +{ + return ucsi_stm32g0_read(ucsi, UCSI_CCI, cci, sizeof(*cci)); +} + +static int ucsi_stm32g0_read_message_in(struct ucsi *ucsi, void *val, size_t len) +{ + return ucsi_stm32g0_read(ucsi, UCSI_MESSAGE_IN, val, len); +} + static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); @@ -446,7 +461,9 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) } static const struct ucsi_operations ucsi_stm32g0_ops = { - .read = ucsi_stm32g0_read, + .read_version = ucsi_stm32g0_read_version, + .read_cci = ucsi_stm32g0_read_cci, + .read_message_in = ucsi_stm32g0_read_message_in, .sync_control = ucsi_stm32g0_sync_control, .async_control = ucsi_stm32g0_async_control, }; diff --git a/drivers/usb/typec/ucsi/ucsi_yoga_c630.c b/drivers/usb/typec/ucsi/ucsi_yoga_c630.c index e5e8ba0c0eaa..95a333ad5496 100644 --- a/drivers/usb/typec/ucsi/ucsi_yoga_c630.c +++ b/drivers/usb/typec/ucsi/ucsi_yoga_c630.c @@ -27,8 +27,16 @@ struct yoga_c630_ucsi { u16 version; }; -static int yoga_c630_ucsi_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int yoga_c630_ucsi_read_version(struct ucsi *ucsi, u16 *version) +{ + struct yoga_c630_ucsi *uec = ucsi_get_drvdata(ucsi); + + *version = uec->version; + + return 0; +} + +static int yoga_c630_ucsi_read_cci(struct ucsi *ucsi, u32 *cci) { struct yoga_c630_ucsi *uec = ucsi_get_drvdata(ucsi); u8 buf[YOGA_C630_UCSI_READ_SIZE]; @@ -38,22 +46,26 @@ static int yoga_c630_ucsi_read(struct ucsi *ucsi, unsigned int offset, if (ret) return ret; - if (offset == UCSI_VERSION) { - memcpy(val, &uec->version, min(val_len, sizeof(uec->version))); - return 0; - } + memcpy(cci, buf, sizeof(*cci)); - switch (offset) { - case UCSI_CCI: - memcpy(val, buf, min(val_len, YOGA_C630_UCSI_CCI_SIZE)); - return 0; - case UCSI_MESSAGE_IN: - memcpy(val, buf + YOGA_C630_UCSI_CCI_SIZE, - min(val_len, YOGA_C630_UCSI_DATA_SIZE)); - return 0; - default: - return -EINVAL; - } + return 0; +} + +static int yoga_c630_ucsi_read_message_in(struct ucsi *ucsi, + void *val, size_t val_len) +{ + struct yoga_c630_ucsi *uec = ucsi_get_drvdata(ucsi); + u8 buf[YOGA_C630_UCSI_READ_SIZE]; + int ret; + + ret = yoga_c630_ec_ucsi_read(uec->ec, buf); + if (ret) + return ret; + + memcpy(val, buf + YOGA_C630_UCSI_CCI_SIZE, + min(val_len, YOGA_C630_UCSI_DATA_SIZE)); + + return 0; } static int yoga_c630_ucsi_async_control(struct ucsi *ucsi, u64 command) @@ -93,7 +105,9 @@ static int yoga_c630_ucsi_sync_control(struct ucsi *ucsi, u64 command) } const struct ucsi_operations yoga_c630_ucsi_ops = { - .read = yoga_c630_ucsi_read, + .read_version = yoga_c630_ucsi_read_version, + .read_cci = yoga_c630_ucsi_read_cci, + .read_message_in = yoga_c630_ucsi_read_message_in, .sync_control = yoga_c630_ucsi_sync_control, .async_control = yoga_c630_ucsi_async_control, }; @@ -126,7 +140,7 @@ static int yoga_c630_ucsi_notify(struct notifier_block *nb, return NOTIFY_OK; case LENOVO_EC_EVENT_UCSI: - ret = uec->ucsi->ops->read(uec->ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = uec->ucsi->ops->read_cci(uec->ucsi, &cci); if (ret) return NOTIFY_DONE; From patchwork Tue Jun 25 14:54:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 807487 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33E6516F8E4 for ; Tue, 25 Jun 2024 14:54:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327277; cv=none; b=PKQbWqVWzle18I1wPMne/bDs5/e0YFLzeQQzaff76GTz2rsfT3sDrlN3GFGyyk0yDVE49NSMkJhpvPY/nH7EDNhFOyjiqzaJm7R7Cr/SJEAOSkuS450fDx+pDkyBFIqWbZlk5lypK2WQE4wzUdppNZVbQQHQlR/b9XQxDxrZZYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327277; c=relaxed/simple; bh=iGz/1CTodFFLxhp6bKYq0V9qXiKSF7JlfOGZtn0n3AI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jNcLEGq0F2z0MsVZYZOxKbSAbVsLZFS8/4SFQfAeG+I2huF2QlqyvEMZRUVMn5GajsJUY0QcdB6PgiCiHGgx4IVwSSbI10O3iSk5xUz3s3i+f7KUsCKss69vrAoUhqtaIpkNqxHniX4Bf/vrIVIbOZMuCUYJvc9ids6qmR4cgRU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=hiTIGksk; arc=none smtp.client-ip=209.85.167.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="hiTIGksk" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-52cdebf9f6fso4106688e87.0 for ; Tue, 25 Jun 2024 07:54:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719327273; x=1719932073; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=+KB8pq5NQAZ6n/rTI4y8OIrQXRuJ/BDLkN3YelnKCzI=; b=hiTIGksk89zMJUFJnB69SRRfhUbMnTHFX5+Yg1/cEyLcRyaW97rCBBBAnNnapjclLN qBzm3ROHVVdo1orBXXhyXeleF6CFYR8TA+88uaRj85QUGDFLKH3HrKOL5NI9WCXPh7Se qrViRtkI4MeA+BJYjbqHTJbcwtMdQdiE2hzI/NRyWxR01RNuZ/S/8RhlbVYhC/YRjMeW ekUDI0/QxqESOSaAuJSkQgvSHUUZ23x96kjXkh1q8VH0Xj7tKPaNOWZw1HJebbpzFEps JArub/0sUfzQVmY+OkXuQ218nMqSatf3PPvyX4cmY0y3SN3ech8ZPr7Pm00VRvppf8wt fFbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719327273; x=1719932073; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+KB8pq5NQAZ6n/rTI4y8OIrQXRuJ/BDLkN3YelnKCzI=; b=VcoQm7inGOBs5MQj5rLvgt/WOFd0U+73bG9Xg608wu2SynLJ1dg3OcIC/WWMPRbNRo QoltXYAfjppo1Nt2vdgRbWQm3uQKGqwTSVV8FP3EbOBP/fL9UNQOGD9p0pFC0rSQ9uiI rLpsGUehFFgXZTAw57FvPOp8J79pCk4t0Jt9Jdy/wNpiBYLWNYvgetJaI+HrNw4xyLl1 6T3N393fBu0N5aX7FMpnH0H+yVNqc6PJuzjmJm/TLuEx0Zxt6gY5030wl3aibnlVGlJf 7b8dpIp58h/bGqDVCHKtQ9lVm0pxCWyzMmx8EsEhgfCtob7YSi2QlKOB7Kuuwpv5RuKt pEvQ== X-Forwarded-Encrypted: i=1; AJvYcCVJAYvgjRoDAneO+69E+SUiJANLxJZPcdsv9afJ7oDAM2rd0B2r7uWOHiAerMU0S3xGaN4/mXueKAVteqLnHcXjxgkzECw6zk+Y X-Gm-Message-State: AOJu0Yy8Gtw6Ap8inS8ns/Wh/HW+PTNpFjSH+7yp0dsUB2Me7qMOQ6Q8 COIlKRVCTLI44eCYWeuU+bL41lBCJzFo9DCWHVVK+CQdcMrS1+uUMmIiz2YxMRo= X-Google-Smtp-Source: AGHT+IGtQxo5jV6cMwfXqOOOPD8YZNEebt3pWP+GVzXLpNydBk0U1gGSF/EvGWsWZGvWy6VumzOMyA== X-Received: by 2002:ac2:58f4:0:b0:52c:dacf:e5ac with SMTP id 2adb3069b0e04-52ce185d218mr5415211e87.54.1719327273412; Tue, 25 Jun 2024 07:54:33 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52cd64328b7sm1251877e87.221.2024.06.25.07.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 07:54:32 -0700 (PDT) From: Dmitry Baryshkov Date: Tue, 25 Jun 2024 17:54:29 +0300 Subject: [PATCH v3 4/7] usb: typec: ucsi: rework command execution functions Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240625-ucsi-rework-interface-v3-4-7a6c8e17be3a@linaro.org> References: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> In-Reply-To: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> To: Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue Cc: Nikita Travkin , Neil Armstrong , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4449; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=iGz/1CTodFFLxhp6bKYq0V9qXiKSF7JlfOGZtn0n3AI=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmetojsGNpr741Om5LXvH7sB4VKPc9Jwl3kjr86 7L8QXP96iqJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZnraIwAKCRCLPIo+Aiko 1WCzCACcWhUjxOL5ffytSZW+F/ypklLcxsDF6YxBlJcdE7up+io2SCqomp/iKmX+5dY/ORLmd66 lJuECdbPx9ngWe/pTO+CJNJYrH/dMWh81YM5i2PFb8n73II9/yqhxJmdrhlsw2LgK7If03UgJ2+ k+iO+dPgvCxceKi4Y0cN36dzDSztbdn1k2lHng2/DUhrTuUPscI8g120u9GusllZQ+D8YZjhBuo rUBkA3HL1EDs5Z0URtChJIRQ7i6Vots9o3vGO35gdat2BHFsIzzFeHrLvwp8vV5bQ0mLZVCxQ95 jS/FEKz7Q4a/a0K7/6AjoUN9hZ3Uf0r86rV6mYKJDAiL2jh5 X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A Rework command execution code to remove recursive calls of ucsi_exec_command. This also streamlines the sync_control / read(CCI) read (MESSAGE_IN) sequence, allowing further rework of the command code. Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 134 ++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 70 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index db25f51ff71d..5f8e5ed9e0f9 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -63,25 +63,74 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) return ucsi->ops->sync_control(ucsi, ctrl); } -static int ucsi_exec_command(struct ucsi *ucsi, u64 command); - -static int ucsi_read_error(struct ucsi *ucsi) +static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, + void *data, size_t size, bool conn_ack) { - u16 error; int ret; - ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS); - if (ret < 0) + *cci = 0; + + ret = ucsi->ops->sync_control(ucsi, command); + if (ret) return ret; - ret = ucsi_read_message_in(ucsi, &error, sizeof(error)); + ret = ucsi->ops->read_cci(ucsi, cci); if (ret) return ret; - ret = ucsi_acknowledge(ucsi, false); + if (*cci & UCSI_CCI_BUSY) + return -EBUSY; + + if (!(*cci & UCSI_CCI_COMMAND_COMPLETE)) + return -EIO; + + if (*cci & UCSI_CCI_NOT_SUPPORTED) { + if (ucsi_acknowledge(ucsi, false) < 0) + dev_err(ucsi->dev, + "ACK of unsupported command failed\n"); + return -EOPNOTSUPP; + } + + if (*cci & UCSI_CCI_ERROR) { + /* Acknowledge the command that failed */ + ret = ucsi_acknowledge(ucsi, false); + return ret ? ret : -EIO; + } + + if (data) { + ret = ucsi_read_message_in(ucsi, data, size); + if (ret) + return ret; + } + + ret = ucsi_acknowledge(ucsi, conn_ack); if (ret) return ret; + return 0; +} + +static int ucsi_read_error(struct ucsi *ucsi) +{ + u16 error; + u32 cci; + int ret; + + ret = ucsi_run_command(ucsi, UCSI_GET_ERROR_STATUS, &cci, + &error, sizeof(error), false); + + if (cci & UCSI_CCI_BUSY) { + ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false); + + return ret ? ret : -EBUSY; + } + + if (ret < 0) + return ret; + + if (cci & UCSI_CCI_ERROR) + return -EIO; + switch (error) { case UCSI_ERROR_INCOMPATIBLE_PARTNER: return -EOPNOTSUPP; @@ -121,78 +170,23 @@ static int ucsi_read_error(struct ucsi *ucsi) return -EIO; } -static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) -{ - u32 cci; - int ret; - - ret = ucsi->ops->sync_control(ucsi, cmd); - if (ret) - return ret; - - ret = ucsi->ops->read_cci(ucsi, &cci); - if (ret) - return ret; - - if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY) - return ucsi_exec_command(ucsi, UCSI_CANCEL); - - if (!(cci & UCSI_CCI_COMMAND_COMPLETE)) - return -EIO; - - if (cci & UCSI_CCI_NOT_SUPPORTED) { - if (ucsi_acknowledge(ucsi, false) < 0) - dev_err(ucsi->dev, - "ACK of unsupported command failed\n"); - return -EOPNOTSUPP; - } - - if (cci & UCSI_CCI_ERROR) { - /* Acknowledge the command that failed */ - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; - - if (cmd == UCSI_GET_ERROR_STATUS) - return -EIO; - - return ucsi_read_error(ucsi); - } - - if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) { - ret = ucsi_acknowledge(ucsi, false); - return ret ? ret : -EBUSY; - } - - return UCSI_CCI_LENGTH(cci); -} - static int ucsi_send_command_common(struct ucsi *ucsi, u64 command, void *data, size_t size, bool conn_ack) { - u8 length; + u32 cci; int ret; mutex_lock(&ucsi->ppm_lock); - ret = ucsi_exec_command(ucsi, command); - if (ret < 0) - goto out; - - length = ret; - - if (data) { - ret = ucsi_read_message_in(ucsi, data, size); - if (ret) - goto out; + ret = ucsi_run_command(ucsi, command, &cci, data, size, conn_ack); + if (cci & UCSI_CCI_BUSY) { + ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false); + return ret ? ret : -EBUSY; } - ret = ucsi_acknowledge(ucsi, conn_ack); - if (ret) - goto out; + if (cci & UCSI_CCI_ERROR) + return ucsi_read_error(ucsi); - ret = length; -out: mutex_unlock(&ucsi->ppm_lock); return ret; } From patchwork Tue Jun 25 14:54:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 807485 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E67F17082C for ; Tue, 25 Jun 2024 14:54:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327279; cv=none; b=Z3c14arOVFvYYv9Le75Soz/e9kmruzag4KCPeOW2cqLv/J7HpOr8nytFMPDE61250iFVOXg1AcD0FCFRYLfILrt/wmpxdZfRy+NF5THPYShBIFBDG3K9BoLoPVS9C01DizlzFu9a63X9wionSko7OZ3YS8HVLM+mWmY96v7lLjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719327279; c=relaxed/simple; bh=c1mFXRw/1F7jX+PLOZli37YidJtYzYIZwD0eERkZXpg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=p4c4h81nLCbnjrk8lM9ed9sT4L/90X8itK0Qy9MMHRT+a3aSA3jlMhI3tqgZondYmiYoaM5G8SsbeTiQ85t9tdUggMJ3BatRQaE9NEpv3e+3gzkTbJCDHhPeoH5T+qM1CWw+GFtzqCcTyxAg49I7gnMVrrokPdot7xKzK7LxCLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=taXzIsgv; arc=none smtp.client-ip=209.85.167.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="taXzIsgv" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-52cdebf9f53so3028757e87.2 for ; Tue, 25 Jun 2024 07:54:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719327275; x=1719932075; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=g2gxtQIN+xKJeYZrrZg4eTx3pd8ql0F1T/zN9SBRQsY=; b=taXzIsgvpysuGi9cEp+t84xI8DTRmvYy+7KRZHNOqNUe8RfBM7kMRi0dg+9QaDO1Dh vu35FRL0SVHMz1ncI1n8/HlmNufUbOdA7kdzb2QJd5GMTxM/HIIujG7PJZCkMvw+Z4TC C5maMj9MPgFD49JHA+Nm4edre5AcML2X/waqsrG1kWG+gQONLajC71ZN74ZgVnCdqGyJ z7DN3vKAyf49SDLZTlNYdw1KFtUPpeHNmU+MqqGPkvqJ6WLjc+6tHmr1ihoqWHbHdAQt DdmdvmC+48ANffjhduprSFRcbH8g12rDqXVlIwrzAQsYI40nDTOr4q6oQ3NHaFY7XAR9 oWjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719327275; x=1719932075; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g2gxtQIN+xKJeYZrrZg4eTx3pd8ql0F1T/zN9SBRQsY=; b=UFec7SloVo1lVZHXQxZRycDfKppY607BIEpWXJwZ/sa0iI6KXvACZ9Zn3JgXwKZbxZ yFUJxNGIvDeCoNcIfiDxxw9YSuZbXyGsLPnOrLbsXoGjMp+nmEp9aktYDzvkKc3Lce47 6RhsiwLBmeHPBRLPJB/d9JfaHx7ETh8361yAfOIZDqfOo6VJx9ndYlL10hb8wJv60blJ UNwGCfKYD1KLd1Vno8K5/XUlhU/i9FJCM2KctZZ/Ed6TOaW7Zp6u+LNOD4ivIB6Nk+AI CCrC2iKBbvVxgZ4X2+QDMACO1lhsa/xtmR7+lNpNOO7dx5nTrmg4KegmWsDRdcku45RY EdiA== X-Forwarded-Encrypted: i=1; AJvYcCX8R2FriHHlILX92TJhM5/AIC9ors/G5r6gYglCSCI5UUaiVKMvtlzTyYVTs84vHi3RWDMNit4MBrVeSbH2PC3bnWxt0JMgNPgB X-Gm-Message-State: AOJu0YyoGPAvqPznMfkVnrDl9yVgABFB8KdsuCAJ8sp4Pj4/2Ct1kw02 x4MONYwvWTIpAWv8xCQijLvt5aKfXczo1OOCIUUgLpBXIAcarP6G2VD70XOaYcw= X-Google-Smtp-Source: AGHT+IFgTfwsx7ElY5vsvTSzReP2HLuqQHHg6f+F1C3RO23ndtkGTJu8+wBSZAIAD6eWetNxoUG4Kg== X-Received: by 2002:a05:6512:46f:b0:52c:9f9e:d8e3 with SMTP id 2adb3069b0e04-52ce064e3d8mr4973783e87.31.1719327275739; Tue, 25 Jun 2024 07:54:35 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52cd64328b7sm1251877e87.221.2024.06.25.07.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 07:54:35 -0700 (PDT) From: Dmitry Baryshkov Date: Tue, 25 Jun 2024 17:54:32 +0300 Subject: [PATCH v3 7/7] usb: typec: ucsi: reorder operations in ucsi_run_command() Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240625-ucsi-rework-interface-v3-7-7a6c8e17be3a@linaro.org> References: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> In-Reply-To: <20240625-ucsi-rework-interface-v3-0-7a6c8e17be3a@linaro.org> To: Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue Cc: Nikita Travkin , Neil Armstrong , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2070; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=c1mFXRw/1F7jX+PLOZli37YidJtYzYIZwD0eERkZXpg=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmetokKTBUAvA860F4TZ7LaGT2vn3//h0eO5fRP eXnVpE4f++JATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZnraJAAKCRCLPIo+Aiko 1Y7VCACNYuSM7sEYoqQvGqND2MRdAoZLFky1iBiqT6oimIP7qis1KVCv3F+gBFz/bdKtij+cztp Z5awCnGfW7Httm4zKIIT7NjJpuUDn4ia7oZOW3FAe99Zdq92p1EKUvXKnXApxNz6eom/iOY7qGH ZuClKE5VA15dUN4sl0zW6TTatSGt7zTfkToJ2jq8u83k9ID/maCxlb5V8FISVxuKEqgOJwLGGoE OvAeeooTh9x2NFAt/eeMWZ2D8kgVxeDZw+j5aVfO6x8v8emzAdbZuHaM+qRTH+T7uUXJOiJNXkB 9HVHfVpFVz76fdbmU925vbf0ul+6bFBUI9SNsbusDr5OPJul X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A Streamline control stream of ucsi_run_command(). Reorder operations so that there is only one call to ucsi_acknowledge(), making sure that all complete commands are acknowledged. This also makes sure that the command is acknowledged even if reading MESSAGE_IN data returns an error. Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 3fb5d3a52b80..0d091f06abd3 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -95,7 +95,7 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, void *data, size_t size, bool conn_ack) { - int ret; + int ret, err; *cci = 0; @@ -120,30 +120,24 @@ static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, if (!(*cci & UCSI_CCI_COMMAND_COMPLETE)) return -EIO; - if (*cci & UCSI_CCI_NOT_SUPPORTED) { - if (ucsi_acknowledge(ucsi, false) < 0) - dev_err(ucsi->dev, - "ACK of unsupported command failed\n"); - return -EOPNOTSUPP; - } - - if (*cci & UCSI_CCI_ERROR) { - /* Acknowledge the command that failed */ - ret = ucsi_acknowledge(ucsi, false); - return ret ? ret : -EIO; - } + if (*cci & UCSI_CCI_NOT_SUPPORTED) + err = -EOPNOTSUPP; + else if (*cci & UCSI_CCI_ERROR) + err = -EIO; + else + err = 0; - if (data) { - ret = ucsi->ops->read_message_in(ucsi, data, size); - if (ret) - return ret; - } + if (!err && data && UCSI_CCI_LENGTH(*cci)) + err = ucsi->ops->read_message_in(ucsi, data, size); - ret = ucsi_acknowledge(ucsi, conn_ack); + /* + * Don't ACK connection change if there was an error. + */ + ret = ucsi_acknowledge(ucsi, err ? false : conn_ack); if (ret) return ret; - return 0; + return err; } static int ucsi_read_error(struct ucsi *ucsi)