Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp279966pxu; Wed, 25 Nov 2020 03:02:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzcHAvwHSvm3arnycPPecZYLwQraWMuuu/ln6vtg40n+s/SvgKc/CQSO0PXDBGtvaUuCJl1 X-Received: by 2002:a05:6402:a57:: with SMTP id bt23mr2843008edb.62.1606302176380; Wed, 25 Nov 2020 03:02:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606302176; cv=none; d=google.com; s=arc-20160816; b=t4h9VzS4zANfeS/0s8sXOxTeCj+BoGxIrVGQ3I422OF2Nl5zi36AbORtz58r4LJDSf 8eI3MrcCYQCT7D/S62mygyi5uICa6evupUzLaV6SU3jk3XXeGLjc8FUBp2DfnLuo/gjO Q4SXcwn4lPJKp2itu+M6n56iRJ1z72yBVZABUs/g4oXPQHqz86/N+xnV1/4ircZj5dWy rYLtPLXtBLl31lUCwnuXVFfXEV3/bjwYH8DcrNhZ/yyCy75kY9QhoJ7gUWsqrANuLC2/ u84RariU1qK7y6fwpfBi4Qsfac0MLDs/b2u029aSsWyFSAF8wVtmS37zoyAHV2tLJ4cg guJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:date:from:dkim-signature; bh=R9jPt2NH0PK1K5G5eT5q3EFReOKpDq7B+jBKCFjohwc=; b=c6khCej2zWi9bgsfsmNuVIGnCIPo0sbV8qqHuTICknFiN8lzO8pKftZ64X88LilgK9 8Srx3MAjGV/z+KvmvANUM6wEfQK+FWijqv7r4JzeOGIYCemKKWqvVCL1i/HRmaaUr3dW fZ6r8NjKY+l5BQM+COHVg7zpDSLt+3cYb7xBEBaAqkIITmRp2JDObglzOIJJQvrzSmDl qmGCt54Q4sBwuYl+pxcv7zkwAUuyBtYirUZGqIpIPZ8HQrLGHUQmTinhFXrZav+xLDUM e0jertGbIm1DWB5ndvzWg91uUlAry8DnR4B1g2rGX9UfZqT5O/bn6p5u/Orz/5kcg6KK s4wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kNfN86rU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p62si954472edd.213.2020.11.25.03.02.32; Wed, 25 Nov 2020 03:02:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kNfN86rU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727348AbgKYK6Q (ORCPT + 99 others); Wed, 25 Nov 2020 05:58:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726190AbgKYK6Q (ORCPT ); Wed, 25 Nov 2020 05:58:16 -0500 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3E3EC0613D4; Wed, 25 Nov 2020 02:57:57 -0800 (PST) Received: by mail-pg1-x541.google.com with SMTP id s63so2122240pgc.8; Wed, 25 Nov 2020 02:57:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=R9jPt2NH0PK1K5G5eT5q3EFReOKpDq7B+jBKCFjohwc=; b=kNfN86rUCgkHBYEGXYY3KxiFEGCkN9x2OrS7GJ5DMpZhvENrmY6hPmOyoCnIPLgsyc UDeMXa2CCUbOzJzAN7uwGa1J6LObL4515ObltxkOsk9pTVHhAPsJboqC2wmCMrGmDRdA 6C6+48OE6hYSgqW9p29YrWc9+3+ciEv/QaJ7efQojK1j5tW+l1PWGPURb9LvtR+ouLzb pFxwwKLMJ2AYnT1xQ3+3AVD1sERk27A93pUMIlrcKK3zM1lCwP3kygeK8ZNDgio6h82c Tn7Gu6p05noyjfa5Rpe2ZGCo8izkVwWWw8HljimpnRLAYIFDKCcF/JXe397UQHvO3zP2 HdWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=R9jPt2NH0PK1K5G5eT5q3EFReOKpDq7B+jBKCFjohwc=; b=EOqsHRfwBhjP/JuRfyTeTIjHlazwU2kDPiTntVVchapGKB7aZfZ9Iqq9EtiJnQyNCO +g/YIduLYn9VjpxXZJmS1il6L2Yn7RkIVsT3jcmqAGI57KVcUJVAeqbBKVPOrU4Z285L yQcOPAG47/n6fa/TzBHlyae7OS7DyohfSx34CCdDkhohFq6CT4f/v19k2PH5TZA6k5E+ IzwCg9xupsr+7u1GO0kvDYonz7Fattx0hRH8H7VCzDJzYEw24ddZ1S+wI6KfaTNwGW8c B/NTFkbUQN9UwP99Cc0a/ElyAGu60zppM4muBg5gkh1LjcOmP6zTZDP2vp4HdaryGt2o epqQ== X-Gm-Message-State: AOAM5325ALV5TYsy+nzs7SEHHYseCw73lkgo+N5aPX2JQ+8Eu8qq6Phr BfhHmIGWWiDnabnJd6r3bArsShg0vK0QpFYn X-Received: by 2002:a17:90a:ec06:: with SMTP id l6mr3455086pjy.38.1606301877362; Wed, 25 Nov 2020 02:57:57 -0800 (PST) Received: from localhost ([2001:e42:102:1532:160:16:113:140]) by smtp.gmail.com with ESMTPSA id t128sm1673438pfb.111.2020.11.25.02.57.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2020 02:57:56 -0800 (PST) From: Coiby Xu X-Google-Original-From: Coiby Xu Date: Wed, 25 Nov 2020 18:57:20 +0800 To: =?utf-8?Q?Barnab=C3=A1s_P=C5=91cze?= Cc: "linux-input@vger.kernel.org" , Helmut Stult , "stable@vger.kernel.org" , Jiri Kosina , Benjamin Tissoires , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v3] HID: i2c-hid: add polling mode based on connected GPIO chip's pin status Message-ID: <20201125105720.xatyiva7psrfyzbi@Rk> References: <20201021134931.462560-1-coiby.xu@gmail.com> <20201122101525.j265hvj6lqgbtfi2@Rk> <20201123143613.zzrm3wgm4m6ngvrz@Rk> <1FeR4cJ-m2i5GGyb68drDocoWP-yJ47BeKKEi2IkYbkppLFRCQPTQT4D6xqVCQcmUIjIsoe9HXhwycxxt5XxtsESO6w4uVMzISa987s_T-U=@protonmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1FeR4cJ-m2i5GGyb68drDocoWP-yJ47BeKKEi2IkYbkppLFRCQPTQT4D6xqVCQcmUIjIsoe9HXhwycxxt5XxtsESO6w4uVMzISa987s_T-U=@protonmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 23, 2020 at 04:32:40PM +0000, Barnabás Pőcze wrote: >> [...] >> >> >> +static int get_gpio_pin_state(struct irq_desc *irq_desc) >> >> >> +{ >> >> >> + struct gpio_chip *gc = irq_data_get_irq_chip_data(&irq_desc->irq_data); >> >> >> + >> >> >> + return gc->get(gc, irq_desc->irq_data.hwirq); >> >> >> +} >> >> [...] >> >> >> + ssize_t status = get_gpio_pin_state(irq_desc); >> >> > >> >> >`get_gpio_pin_state()` returns an `int`, so I am not sure why `ssize_t` is used here. >> >> > >> >> >> >> I used `ssize_t` because I found gpiolib-sysfs.c uses `ssize_t` >> >> >> >> // drivers/gpio/gpiolib-sysfs.c >> >> static ssize_t value_show(struct device *dev, >> >> struct device_attribute *attr, char *buf) >> >> { >> >> struct gpiod_data *data = dev_get_drvdata(dev); >> >> struct gpio_desc *desc = data->desc; >> >> ssize_t status; >> >> >> >> mutex_lock(&data->mutex); >> >> >> >> status = gpiod_get_value_cansleep(desc); >> >> ... >> >> return status; >> >> } >> >> >> >> According to the book Advanced Programming in the UNIX Environment by >> >> W. Richard Stevens, >> >> With the 1990 POSIX.1 standard, the primitive system data type >> >> ssize_t was introduced to provide the signed return value... >> >> >> >> So ssize_t is fairly common, for example, the read and write syscall >> >> return a value of type ssize_t. But I haven't found out why ssize_t is >> >> better int. >> >> > >> > >> >Sorry if I wasn't clear, what prompted me to ask that question is the following: >> >`gc->get()` returns `int`, `get_gpio_pin_state()` returns `int`, yet you still >> >save the return value of `get_gpio_pin_state()` into a variable with type >> >`ssize_t` for no apparent reason. In the example you cited, `ssize_t` is used >> >because the show() callback of a sysfs attribute must return `ssize_t`, but here, >> >`interrupt_line_active()` returns `bool`, so I don't see any advantage over a >> >plain `int`. Anyways, I believe either one is fine, I just found it odd. >> > >> I don't understand why "the show() callback of a sysfs attribute >> must return `ssize_t`" instead of int. Do you think the rationale >> behind it is the same for this case? If yes, using "ssize_t" for >> status could be justified. >> [...] > >Because it was decided that way, `ssize_t` is a better choice for that purpose >than plain `int`. You can see it in include/linux/device.h, that both the >show() and store() methods must return `ssize_t`. > Could you explain why `ssize_t` is a better choice? AFAIU, ssize_t is used because we can return negative value to indicate an error. If we use ssize_t here, it's a reminder that reading a GPIO pin's status could fail. And ssize_t reminds us it's a operation similar to read or write. So ssize_t is better than int here. And maybe it's the same reason why "it was decided that way". >What I'm arguing here, is that there is no reason to use `ssize_t` in this case. >Because `get_gpio_pin_state()` returns `int`. So when you do >``` >ssize_t status = get_gpio_pin_state(...); >``` >then the return value of `get_gpio_pin_state()` (which is an `int`), will be >converted to an `ssize_t`, and saved into `status`. I'm arguing that that is >unnecessary and a plain `int` would work perfectly well in this case. Anyways, >both work fine, I just found the unnecessary use of `ssize_t` here odd. > > >Regards, >Barnabás Pőcze -- Best regards, Coiby