Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1934099imm; Thu, 20 Sep 2018 05:21:27 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZ31ibdjLL8SJk68xlChxnME6mjblUoURD7/MeA+++kcdkk3RxsQqlPewopGDUG12alOVqq X-Received: by 2002:a65:490e:: with SMTP id p14-v6mr36460432pgs.437.1537446087902; Thu, 20 Sep 2018 05:21:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537446087; cv=none; d=google.com; s=arc-20160816; b=m5jL6k6n+0u5XdK/f0FBz2DNTEPYW9ZGjtimZEuPbr4Y6ITg/V0PMY0z98E03joGH9 cRbh7bsoNlY8hcIRuNYlTsJ2JqDYr3tX11sylCEKSKVKqcyGYNM/yDDPUJlS7SMNBcHr xHIBZUSSuKTeE4CK0FV14BF49zzPVU7wP4POORUvSCXub4oK6OEj+k87Nik22JyC5nqd SWA8gFWRxXuBk+ILeuJrJh6dh7/POYWI9sHS2AOGPTb+pgkGuhUUbArYFfHL8/jOMxP2 6B5r38zTZQHGxbk7qZC4NBBWrd3GT8SyZkqF+nnnrL94HHsBp7Re3XSBSnsod9WxSpxf /7lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:subject:from; bh=znj6X8BIpwaSej3rfZG5UT/EvZgMKnLup7X6p3GI0pc=; b=MPs/qhGaMj+jyzofXyELPRUABbq9p/Rtg69GnLu3/eNpWOgFdGhzKoXrh8agYUGCx0 c1cuunYKFldc+a89ns7gJdzz7y8gjRfk9NAs1XwAmzm4Zd1m4piertDABSoayfLKBsEm Cev6q5jrz0SyYA1wsONeX64Cch8WpN7bGuz01T5vl7UbPRkw8wOoZ2SB9BLmPFJJKXgw 4PSRjuVj1+1IfWp+atyHMQ9TLLz6YBZmfoKYe+RbTFCSU+qdaNFP9YYg3tL9tWwsukm6 nTHXip+NbRK+DqfhL224G31faEFknmF68O/uO2vJioCtcJEXo1x1JKRiWUD3e0P3M8YE TS6A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f94-v6si16666971plb.10.2018.09.20.05.21.11; Thu, 20 Sep 2018 05:21:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732652AbeITSDh (ORCPT + 99 others); Thu, 20 Sep 2018 14:03:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:44398 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732247AbeITSDh (ORCPT ); Thu, 20 Sep 2018 14:03:37 -0400 Received: from [192.168.1.75] (cpe-24-28-70-126.austin.res.rr.com [24.28.70.126]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7679C204EC; Thu, 20 Sep 2018 12:20:22 +0000 (UTC) From: Timur Tabi Subject: Re: [PATCH] gpiolib: Show correct direction from the beginning To: Ricardo Ribalda Delgado Cc: Linus Walleij , swboyd@chromium.org, linux-gpio@vger.kernel.org, LKML References: <20180914070839.4667-1-ricardo.ribalda@gmail.com> <20180914070839.4667-2-ricardo.ribalda@gmail.com> Message-ID: <015715f7-64bc-aca6-77fe-68ddb6c938a8@kernel.org> Date: Thu, 20 Sep 2018 07:20:18 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/19/2018 10:27 AM, Ricardo Ribalda Delgado wrote: > Let me explain my current setup > > I have a board with input and output gpios, the direction is defined > via pdata. When I run gpioinfo all the gpios are shown as input, > regardless if they are input or outputs: Eg: > > root@qt5022:/tmp# ./gpioinfo > > gpiochip0 - 16 lines: > line 0: "PROG_B" unused input active-high > line 1: "M0" unused input active-high > line 2: "M1" unused input active-high > line 3: "M2" unused input active-high > line 4: "DIN" unused input active-high > line 5: "CCLK" unused input active-high > line 6: unnamed unused input active-high > line 7: unnamed unused input active-high > line 8: "DONE" unused input active-high > line 9: "INIT_B" unused input active-high > line 10: unnamed unused input active-high > line 11: unnamed unused input active-high > line 12: unnamed unused input active-high > line 13: unnamed unused input active-high > line 14: unnamed unused input active-high > line 15: unnamed unused input active-high Yes, this is a known problem that should be fixed. > That is wrong and very confusing to the user, it can also lead to a > mayor fuckup if the user decides to connect two output gpio pins > because he expects that both are input. (This is the programming port, > but I also have 24 V -high current GPIOs) Users are expected to program the direction for every GPIO they want to use, regardless of whatever it's set to before they open it. > There is a function in the API to tell libgpio if a gpio is out our > in. Why not use it? Because calling that API before properly claiming the GPIO is a programming error. > - If the configuration is hardcoded, the driver will return a fixed value > - If it is cheap to query the hardware, the driver will query the hardware, > - If it is expensive to query the hardware the driver can either > return a cached value or a fake value (current situation) The reason why the Qualcomm driver is impacted the most is because on ACPI platforms, the GPIO map is "sparse". That is, not every GPIO between 0 and n-1 actually exists. So reading a GPIO that doesn't exist is invalid. The way to protect against that is to claim the GPIO first. If the claim is rejected, then you know that you can't access that GPIO. The bug is that the original code that I deleted (and that you're trying to put back) doesn't claim the GPIO first. >>From my point of view: "The get_direction callback normally triggers > a read/write to hardware, but we shouldn't be touching the hardware > for an individual GPIO until after it's been properly claimed." is > an statement specific for your platform and should be fixed in your > driver. > > Either that, or I have completely missunderstund the purpouse of gpiod > :), and that could easily be the case. It's not a platform-specific statement. It applies to all drivers. In some drivers, the get_direction function had side-effects (like programming muxes, IIRC) that no one really cared about but was technically wrong. I'm not sure how to properly fix this, but I wonder if we need some kind of late-stage initialization where gpiolib scans all the GPIOs by claiming them first, reading the directions, and then releasing them.