Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2456654rwb; Wed, 30 Nov 2022 06:54:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf5uIvfUTzbLah86/Asx1NT32SPppaviWbLwA2hEvOdafz4m7qbMmOhN08HfSq/av3WulCPh X-Received: by 2002:a05:6402:e97:b0:461:beb2:76e with SMTP id h23-20020a0564020e9700b00461beb2076emr55339300eda.5.1669820068240; Wed, 30 Nov 2022 06:54:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669820068; cv=none; d=google.com; s=arc-20160816; b=UTUIB8SOQLhpnRoc7/MGqVo5m74OdC6r+gqd5HWVmhjdmUd0W78ib7TmqGs/6PY3is dHojyPZDilrVMPL8DCmvqWTqgYmSR2k4SDAVnWrMNYEKfK+X2Sg89iHbSlFV1j/K012w 0pQGIb9XEdbRPghx/Ykqr80hWqHG49NrKZObrChwCVxZQnEk6ye8UDAL+spGPTuur05c wdtltAg9OGfKuxBpnMWUjCsgEu0QS9u9KDmgSrQqzmDshGxufJhgLH2A7KSU9+TcDwPe FpXIs83DDqczpOJZ/aaRu1Ab7G+i5VQ1cS7BRBwkd5s5iEFGnpfD+m9B8Gek9/QXhL4u 9Maw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=dpt5mrvPw7RM7k6Ey4ll2TJ6SXaTSEx8JWL9PuzsCpk=; b=s3aXNkE/+n0hWl2AUGR/WhK7sZOATwzwREosoRNuvp5W+c3LmvlRZgsXHvjJ04/gjm ylbPW0DvTTIMDn/gIwD8zuQNdtGo6hD23ctzkYeVz0nAMBCRdk0oZToVGua4oJq3/K4e iUuJbiG5qndfkfSZ9CFYYh7F5n47pUd8Z+27m+cxrTqPauyT8dcEO6B3o80d2QxYbl4r mjR9s4GmjGS6dimYqIQyHufe50ecg90cEqadVEq4tXEKUhjoNMscx+3HoXzhmD/j1Ewp dWh+HpSgJ6O98RJsXDcLbAePqlbhTv1bIZ4EgWsk9DmlmxpbwyFKUaairjfzX7n7eKWB /vNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=DRLweHjI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b6-20020a056402278600b00468f9f21c11si1621945ede.245.2022.11.30.06.54.06; Wed, 30 Nov 2022 06:54:28 -0800 (PST) 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=@siemens.com header.s=fm1 header.b=DRLweHjI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229772AbiK3OlG (ORCPT + 83 others); Wed, 30 Nov 2022 09:41:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbiK3OlF (ORCPT ); Wed, 30 Nov 2022 09:41:05 -0500 X-Greylist: delayed 61 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 30 Nov 2022 06:41:01 PST Received: from mta-64-227.siemens.flowmailer.net (mta-64-227.siemens.flowmailer.net [185.136.64.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF0242FFE8 for ; Wed, 30 Nov 2022 06:41:00 -0800 (PST) Received: by mta-64-227.siemens.flowmailer.net with ESMTPSA id 20221130143956f1027e955ed21a8562 for ; Wed, 30 Nov 2022 15:39:56 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=alexander.sverdlin@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc; bh=dpt5mrvPw7RM7k6Ey4ll2TJ6SXaTSEx8JWL9PuzsCpk=; b=DRLweHjIruPYZjaXyBdguWxe1SX2EiS6ywDH+hMPwfiN520wk1EfkE4Ofov8tv1Fd3S5GR yTA1gcDL+3KkeFQoqEGxcilvhre+wva8mp+DMiqONebpm+daMZ80GQHD7qDpI66Bn+Rf9D27 Dh8eJdwhEXwI8NsVpCw7p97BRRjLs=; From: "A. Sverdlin" To: linux-spi@vger.kernel.org Cc: Alexander Sverdlin , Mark Brown , linux-kernel@vger.kernel.org, Linus Walleij , Lukas Wunner Subject: [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE Date: Wed, 30 Nov 2022 15:39:48 +0100 Message-Id: <20221130143948.426640-1-alexander.sverdlin@siemens.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-456497:519-21489:flowmailer X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS 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 From: Alexander Sverdlin Commit f3186dd87669 ("spi: Optionally use GPIO descriptors for CS GPIOs") has changed the user-space interface so that bogus SPI_CS_HIGH started to appear in the mask returned by SPI_IOC_RD_MODE even for active-low CS pins. Commit 138c9c32f090 ("spi: spidev: Fix CS polarity if GPIO descriptors are used") fixed only SPI_IOC_WR_MODE part of the problem. Let's fix SPI_IOC_RD_MODE symmetrically. Test case: #include #include #include int main(int argc, char **argv) { char modew = SPI_CPHA; char moder; int f = open("/dev/spidev0.0", O_RDWR); if (f < 0) return 1; ioctl(f, SPI_IOC_WR_MODE, &modew); ioctl(f, SPI_IOC_RD_MODE, &moder); return moder == modew ? 0 : 2; } Fixes: f3186dd87669 ("spi: Optionally use GPIO descriptors for CS GPIOs") Signed-off-by: Alexander Sverdlin --- drivers/spi/spidev.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index b2775d82d2d7b..23177a7c916c3 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -377,8 +377,17 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) switch (cmd) { /* read requests */ case SPI_IOC_RD_MODE: - retval = put_user(spi->mode & SPI_MODE_MASK, - (__u8 __user *)arg); + tmp = spi->mode; + + { + struct spi_controller *ctlr = spi->controller; + + if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods && + ctlr->cs_gpiods[spi->chip_select]) + tmp &= ~SPI_CS_HIGH; + } + + retval = put_user(tmp & SPI_MODE_MASK, (__u8 __user *)arg); break; case SPI_IOC_RD_MODE32: retval = put_user(spi->mode & SPI_MODE_MASK, -- 2.34.1