From patchwork Thu Aug 27 14:00:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352593 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=GBl5AMzI; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bcl8Z4hrnz9sRK for ; Fri, 28 Aug 2020 00:14:06 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727912AbgH0OOC (ORCPT ); Thu, 27 Aug 2020 10:14:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728067AbgH0OLf (ORCPT ); Thu, 27 Aug 2020 10:11:35 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCAB9C06121B; Thu, 27 Aug 2020 07:01:05 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id mw10so2644888pjb.2; Thu, 27 Aug 2020 07:01:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fedQaMAad6gJKi/QRC5xP0GL2BO40HvVvA1rAXDD1oY=; b=GBl5AMzIlvZhybWGvaLgu6UPCdgkvGDYSc+9qPjQno4yxfhwCK4T/WcfvblLCoSlOL jHyzuyH0+lKkPElF5bECyZbOmcrCvFon/KjgSBzFq+BmtCG6OsZlESBjEWQYfZWQRpKp gEkosSek1khI8cnxG/7misDAlpGUuoPaHOK3AXBVpyHxnKibvXuC4euRH48K0xSAh6Di zTL6vQJIwbWxY2IRLSGutmpunj21ieHy/kt3mm7ujgBS1JYMSvuNspZGJHVNq7fCelhi bSeBxzaOy82dJU8ZI2IZt4aBnY6bW8lhgHfdogY99sNro68ntGYPJSlv+P9BVYyifEY2 Cyfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fedQaMAad6gJKi/QRC5xP0GL2BO40HvVvA1rAXDD1oY=; b=UBG2gRTIK1xx5X3TYgq57Ll6REcIHYqdEW6I7fl9dmcpaouWsIFfccol6CdkCXAHD1 pqGbH8d1Do20OGGxKGgOPSp05SK9rO+wmGPe63aKFnN5UnytY8QVaJUn0j0YB5ZcN9qB LKvjW+JG+gl4/5Oyej2dvMAz0KnINLPOCj1NMxHtx4mVYPX/If+HWZTcNt8RmzGqcXkV +eaGbH1iV6WUI4qA3xi7rvAZHxgIJ7POvrVEvKpYTg6ictuYaHCnTKBMnrhxT0VCuODd rUHuIYqGTtzCG+rRRuTXCLyXgr9PEQhxt8RFNH7VBxm1Vph2c/YkzfT7J5szQQpaDJtI R4bg== X-Gm-Message-State: AOAM533ZROCxpqHG9F06GKPh31QHtTp23/EeAZH3QjRGGy7DUh/XgS9D 3mXeha55lOQTeAdqLzlAS6/6gsbDkwc= X-Google-Smtp-Source: ABdhPJz2HOayJnC9uXU68hHAjjsSHbf/scUyiTGvIEs2DN1gP7UgBoRHCysnVIolQEomelhcnIYPPA== X-Received: by 2002:a17:90a:630c:: with SMTP id e12mr10571615pjj.17.1598536864878; Thu, 27 Aug 2020 07:01:04 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.01.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:01:04 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 01/20] gpiolib: cdev: desc_to_lineinfo should set info offset Date: Thu, 27 Aug 2020 22:00:01 +0800 Message-Id: <20200827140020.159627-2-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Set the value of the line info offset in desc_to_lineinfo, rather than relying on it being passed in the info. This makes the function behave as you would expect from the name - it generates the line info corresponding to a given GPIO desc. Signed-off-by: Kent Gibson --- There are some instances where this results in the offset being set when it is already set in the info, but this is clearer especially considering that, as part of the replacement of strncpy with strscpy and to ensure kernel stack cannot be leaked to userspace, the info is initially zeroed in a subsequent patch. drivers/gpio/gpiolib-cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index e6c9b78adfc2..e95e3eab9867 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -752,6 +752,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, bool ok_for_pinctrl; unsigned long flags; + info->line_offset = gpio_chip_hwgpio(desc); /* * This function takes a mutex so we must check this before taking * the spinlock. @@ -933,7 +934,6 @@ static int lineinfo_changed_notify(struct notifier_block *nb, return NOTIFY_DONE; memset(&chg, 0, sizeof(chg)); - chg.info.line_offset = gpio_chip_hwgpio(desc); chg.event_type = action; chg.timestamp = ktime_get_ns(); gpio_desc_to_lineinfo(desc, &chg.info); From patchwork Thu Aug 27 14:00:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352594 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=LwOPTj1p; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bcl8d0YBxz9sSP for ; Fri, 28 Aug 2020 00:14:08 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726101AbgH0ONm (ORCPT ); Thu, 27 Aug 2020 10:13:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728039AbgH0OLf (ORCPT ); Thu, 27 Aug 2020 10:11:35 -0400 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 D3DC6C061232; Thu, 27 Aug 2020 07:01:20 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id m34so3412704pgl.11; Thu, 27 Aug 2020 07:01:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RcaJJxPUWfTCsa1mpxbpca/AYe7cJ1n1R+MIiQxItQI=; b=LwOPTj1pvyDECYEM6u9LoslIwXMkOHZ1sinF/z63Rf6JDi+QBGcEESSy6Wj2gqBMGH 0DvIzZphlwReOYxnq/2QvJH0SdqJEZOL9y5Q/zV7NmzPHhueGz7+ye+HfVh/ZzqAz2ed Dkiysw8psY6+J+hgspfw2/syJu6YK0+QOdtgu7WiAQAIKl9LHA6dwCYHiyfJm5nrbpuv qFHcq1RfwzihK4mAP5ix5ffrRoOBDzT1DEKPyUoPpsW7Psqsj8ikCq4lXhcquvAH59IQ AuE6dB4cxP6DyEmYf0+dKLct/nIGM0HmYiiyU/I7QYcJtqqXYVMzFe2l307Ok6P+9Pt2 TmdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RcaJJxPUWfTCsa1mpxbpca/AYe7cJ1n1R+MIiQxItQI=; b=IQDvAs0fkFitHJG23WUSg+bKwP2YxMz5wfGOyRsbp2CE0hteUvncYKwdK8E4rRYrFg PK3oLguDzSbkY1wH+BC8arz6nUPj9qJDB0/JwQT4n84cTSsan7Gnb1NfFWdzYna+wvQD fVx3D5eIHOiDqXqoCf9kR02C5B9OweP6j4RnFHY9lEAXZNAWKWLn2bxurGNlnrudXPEJ KUbHs29ncJMS+8H5pb0uKcXdmZI75n+iH8zh372nJyb3VB18PF68j9sX8eYsu66MbwvE VgqsMtdRoczI1PjzB5VDruGmJQaHglfudu3VnY2nIKUFZ2fwvZuevAjgnM2LPAvbylfA GE4g== X-Gm-Message-State: AOAM531/J2btQBSj4DwuWHS92dAAg4Ar+zjtM3DOJeVAQOy9MhOrFZW8 1RtzKD2jsl9AxU+yLLHTO1EIvsdoVxo= X-Google-Smtp-Source: ABdhPJxuUw3jHiEDWX+rPzbU1AF7NsH09kaKdhlDQ+HfPvrUeu0mrKynAUYZHEIR3PwkAzngBB3Hhw== X-Received: by 2002:a17:902:d697:: with SMTP id v23mr16530841ply.106.1598536879941; Thu, 27 Aug 2020 07:01:19 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.01.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:01:19 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson , kernel test robot Subject: [PATCH v5 02/20] gpiolib: cdev: replace strncpy with strscpy Date: Thu, 27 Aug 2020 22:00:02 +0800 Message-Id: <20200827140020.159627-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Replace usage of strncpy with strscpy to remove -Wstringop-truncation warnings. The structs being populated are zeroed, to prevent stack leakage as they are returned to userspace, so strscpy performs the equivalent function without the warnings. Reported-by: kernel test robot Signed-off-by: Kent Gibson --- The memset in gpio_desc_to_lineinfo(), in conjunction with the strscpy, is necessary as strncpy zero pads the remainder of the destination. It also guarantees that the info cannot leak kernel stack to userspace. This is useful here, but is even more important for the v2 info, that this function is changed to generate in a subsequent patch, as that struct contains padding and attribute arrays that also need to be initialised. drivers/gpio/gpiolib-cdev.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index e95e3eab9867..8b012879fe3f 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -752,6 +752,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, bool ok_for_pinctrl; unsigned long flags; + memset(info, 0, sizeof(*info)); info->line_offset = gpio_chip_hwgpio(desc); /* * This function takes a mutex so we must check this before taking @@ -765,19 +766,11 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, spin_lock_irqsave(&gpio_lock, flags); - if (desc->name) { - strncpy(info->name, desc->name, sizeof(info->name)); - info->name[sizeof(info->name) - 1] = '\0'; - } else { - info->name[0] = '\0'; - } + if (desc->name) + strscpy(info->name, desc->name, sizeof(info->name)); - if (desc->label) { - strncpy(info->consumer, desc->label, sizeof(info->consumer)); - info->consumer[sizeof(info->consumer) - 1] = '\0'; - } else { - info->consumer[0] = '\0'; - } + if (desc->label) + strscpy(info->consumer, desc->label, sizeof(info->consumer)); /* * Userspace only need to know that the kernel is using this GPIO so @@ -841,12 +834,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) memset(&chipinfo, 0, sizeof(chipinfo)); - strncpy(chipinfo.name, dev_name(&gdev->dev), + strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); - chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; - strncpy(chipinfo.label, gdev->label, + strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); - chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; chipinfo.lines = gdev->ngpio; if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) return -EFAULT; From patchwork Thu Aug 27 14:00:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352592 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=X93zKjVY; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bcl6R42Ylz9sSJ for ; Fri, 28 Aug 2020 00:12:15 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726153AbgH0OML (ORCPT ); Thu, 27 Aug 2020 10:12:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726087AbgH0OLf (ORCPT ); Thu, 27 Aug 2020 10:11:35 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF846C06123E; Thu, 27 Aug 2020 07:01:32 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id v15so3432980pgh.6; Thu, 27 Aug 2020 07:01:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s4z3c80rW1f77SHZ1DJfS7Y59f6hWqfux6WtIWEmMvU=; b=X93zKjVYRnLK3psJe1vnn68ETkVRT4vIcLP2HBNDAktT48Qg1xuowyW6pTbBck/UjW jmjaNCONBHJmKyJFOC1d/v0PWCZOclDrul9x/y9FrpZKSNm3Iqgqtb//j2vbQCLfSdXe 2jAew+Ar6MfKFLQaYIA8NHMVXYCi3BgXceXQ8lCdjkDHg1NHsdNyEUF7UMlY92Dymrum 9ScW9HRWsbIVf+YUUe3mjl6H9ZVd41mE4yRhF+iRN0D89nJXYvt+pSHcBiD+aIRHwa52 Dy4PX+1KUVGBn0mT0EiJUL3vzB/lxhCjzCqjfkLoGPXnDXl/UvxLg3W92upwWYyKPJz+ IvKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=s4z3c80rW1f77SHZ1DJfS7Y59f6hWqfux6WtIWEmMvU=; b=Jr0aNN6jQjrxMb0sJb8DMTuS5tQp3CzaGZvwAMhfmm0c+VVgiEXwMJIRxMSQ2s0WJY jT5H9nUnrIA53OmOFJXvo5ep3NCv7PFQHozioD0AZFHUuRhlbS0EoT6AoqXUZmpkq8yn F6hLdHS01PxJN6x9eynxEf3CNliIdLfhY1Dq3QxEl0UjIj994eIqZalmlWJ7cg3EHHCR DAxzRakO1oE5JWUWzyrvqS2RKOrXZJyaqF/lwZkBH97YMD5BbsMkdHrz67EVIHDKOHf9 jnA9MGAuxjfMDAjAuskDcxDn6aNx5dhCpCn2iuubTLA+wrxn+HanZ0UH5aGTmswNsTWo G7bA== X-Gm-Message-State: AOAM530JXFD7nN6MYOE9cOuIdpYZ1tMT2HPMzoLiWHmDiXLY9iUnPQmy NHX65/438L6NJHFhp1H+bUHwLV15LxM= X-Google-Smtp-Source: ABdhPJwPnpQN034nDDdIUP2fdcgrGE+q4nVyC368/LyurQ/TC81WFM3bdjVMQ16PJTv3uR8TaMv7jA== X-Received: by 2002:a17:902:8646:: with SMTP id y6mr16133410plt.19.1598536891908; Thu, 27 Aug 2020 07:01:31 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.01.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:01:31 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 03/20] gpio: uapi: define GPIO_MAX_NAME_SIZE for array sizes Date: Thu, 27 Aug 2020 22:00:03 +0800 Message-Id: <20200827140020.159627-4-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Replace constant array sizes with a macro constant to clarify the source of array sizes, provide a place to document any constraints on the size, and to simplify array sizing in userspace if constructing structs from their composite fields. Signed-off-by: Kent Gibson --- include/uapi/linux/gpio.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 9c27cecf406f..285cc10355b2 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -14,6 +14,11 @@ #include #include +/* + * The maximum size of name and label arrays. + */ +#define GPIO_MAX_NAME_SIZE 32 + /** * struct gpiochip_info - Information about a certain GPIO chip * @name: the Linux kernel name of this GPIO chip @@ -22,8 +27,8 @@ * @lines: number of GPIO lines on this chip */ struct gpiochip_info { - char name[32]; - char label[32]; + char name[GPIO_MAX_NAME_SIZE]; + char label[GPIO_MAX_NAME_SIZE]; __u32 lines; }; @@ -52,8 +57,8 @@ struct gpiochip_info { struct gpioline_info { __u32 line_offset; __u32 flags; - char name[32]; - char consumer[32]; + char name[GPIO_MAX_NAME_SIZE]; + char consumer[GPIO_MAX_NAME_SIZE]; }; /* Maximum number of requested handles */ @@ -123,7 +128,7 @@ struct gpiohandle_request { __u32 lineoffsets[GPIOHANDLES_MAX]; __u32 flags; __u8 default_values[GPIOHANDLES_MAX]; - char consumer_label[32]; + char consumer_label[GPIO_MAX_NAME_SIZE]; __u32 lines; int fd; }; @@ -182,7 +187,7 @@ struct gpioevent_request { __u32 lineoffset; __u32 handleflags; __u32 eventflags; - char consumer_label[32]; + char consumer_label[GPIO_MAX_NAME_SIZE]; int fd; }; From patchwork Thu Aug 27 14:00:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352615 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=mG5h9x+5; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BclgD2LKBz9sSJ for ; Fri, 28 Aug 2020 00:37:12 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726939AbgH0OhB (ORCPT ); Thu, 27 Aug 2020 10:37:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728090AbgH0ODq (ORCPT ); Thu, 27 Aug 2020 10:03:46 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DACDC0611E0; Thu, 27 Aug 2020 07:01:48 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id i13so2683640pjv.0; Thu, 27 Aug 2020 07:01:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HfQiW+WgxYBdXsafpd142Fc8e/yKRCdqQy/cYa2Z45k=; b=mG5h9x+5nvW2jtpZuTRQmhZc2MUFxQUHrve0ED+kLSsZGFnwzVZZOyMOiVnXQxe/Y4 tiTNl8vqftbQC2DGP0noZTxGJavQ7VlJTImbvs86HUucE7cfwQBiiP1gVTRzpz4f+dQG lUN/592bYT7eqYJ7kDKlc4Gw9Mfs9EQq46VbkPzdykTzt9Qy01BihA/U1fRSieQ6NVYT L2Xi8fwy7Of7520FBgkSfPWUNbw3ygqKFJU+Oht2vV6gGPCsR+NTlUOE51cOrYbB6LMr nwHK+foxh0P0ylBAvgqWGki3lcANJVvRCh3JYLjHAhlbbJc0/rIsYq5mTx/IWKXMfWmG aRVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HfQiW+WgxYBdXsafpd142Fc8e/yKRCdqQy/cYa2Z45k=; b=Pc27t0+U1Of0/xe2a8TEQkhM9Yy42zpDsjpzI8fT5AwKq75h/OjCt99HpUuqDEInTP p0DxCPqCN16o952KhyGeGzmDtBXtlyaCknroSB359tft7McDwtBVGv/tfzBKaFpORLj2 51CKa+uD3YKvJm1rPMvzmf7sp5DiVk5i8PJ23KE7ou34Ka6D54PNlhtjHYk5dcmocSPg o1Ls+hYwxzmaGjOl465C0MNgqiXrG/XD1GVjaWzRckKHBjLQAuuMwZHwUBQqLVtK7g1y 1k9vUbXZNysP2GfjE/FapOThFRhZhmGwHSp/PJnzHWK2MFsEGZbMl5xu8w3bWewXE9U2 a3PA== X-Gm-Message-State: AOAM531w1T0iaAFGGKulWDoLOuQ+waGTk2hOeq0FMP1h+6xCp1PmSXhL hW1YeRN6pC++N17Pe3Mii3DoAjHDacI= X-Google-Smtp-Source: ABdhPJxsJf2TszSEGNN5vfjRVxQyN0iwlm1JPheUCSnOxbfubFkTqEmooZYGYGKGFnRxZHvMiyORzQ== X-Received: by 2002:a17:90b:124e:: with SMTP id gx14mr11118659pjb.225.1598536906950; Thu, 27 Aug 2020 07:01:46 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.01.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:01:46 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 04/20] gpio: uapi: define uAPI v2 Date: Thu, 27 Aug 2020 22:00:04 +0800 Message-Id: <20200827140020.159627-5-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a new version of the uAPI to address existing 32/64-bit alignment issues, add support for debounce and event sequence numbers, allow requested lines with different configurations, and provide some future proofing by adding padding reserved for future use. The alignment issue relates to the gpioevent_data, which packs to different sizes on 32-bit and 64-bit platforms. That creates problems for 32-bit apps running on 64-bit kernels. uAPI v2 addresses that particular issue, and the problem more generally, by adding pad fields that explicitly pad structs out to 64-bit boundaries, so they will pack to the same size now, and even if some of the reserved padding is used for __u64 fields in the future. The new structs have been analysed with pahole to ensure that they are sized as expected and contain no implicit padding. The lack of future proofing in v1 makes it impossible to, for example, add the debounce feature that is included in v2. The future proofing is addressed by providing configurable attributes in line config and reserved padding in all structs for future features. Specifically, the line request, config, info, info_changed and event structs receive updated versions and new ioctls. As the majority of the structs and ioctls were being replaced, it is opportune to rework some of the other aspects of the uAPI: v1 has three different flags fields, each with their own separate bit definitions. In v2 that is collapsed to one - gpio_v2_line_flag. The handle and event requests are merged into a single request, the line request, as the two requests were mostly the same other than the edge detection provided by event requests. As a byproduct, the v2 uAPI allows for multiple lines producing edge events on the same line handle. This is a new capability as v1 only supports a single line in an event request. As a consequence, there are now only two types of file handle to be concerned with, the chip and the line, and it is clearer which ioctls apply to which type of handle. There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or line_offset, and consumer rather than consumer_label. Additionally, v1 GPIOHANDLES_MAX becomes GPIO_V2_LINES_MAX in v2 for clarity, and the gpiohandle_data __u8 array becomes a bitmap in gpio_v2_line_values. The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it. Signed-off-by: Kent Gibson --- Changes for v4: - clarify bitmap width in GPIO_V2_LINES_MAX description Changes for v3: - relocated commentary into commit description - hard limit max requested lines to 64 so bitmaps always fit in a single u64. - prefix all v2 symbols with GPIO_V2 - 64-bit flag values to ULL - use __aligned_u64 to ensure 64-bit fields are 64-bit aligned - support masked get values, as per set values. Changes for v2: - lower case V1 and V2, except in capitalized names - hyphenate 32/64-bit - rename bitmap field to bits - drop PAD_SIZE consts in favour of hard coded numbers - sort includes - change config flags to __u64 - increase padding of gpioline_event - relocate GPIOLINE_CHANGED enum into v2 section (is common with v1) - rework config to collapse direction, drive, bias and edge enums back into flags and add optional attributes that can be associated with a subset of the requested lines. Changes for v1 (since the RFC): - document the constraints on array sizes to maintain 32/64 alignment - add sequence numbers to gpioline_event - use bitmap for values instead of array of __u8 - gpioline_info_v2 contains gpioline_config instead of its composite fields - provide constants for all array sizes, especially padding - renamed "GPIOLINE_FLAG_V2_KERNEL" to "GPIOLINE_FLAG_V2_USED" - renamed "default_values" to "values" - made gpioline_direction zero based - document clock used in gpioline_event timestamp - add event_buffer_size to gpioline_request - rename debounce to debounce_period - rename lines to num_lines include/uapi/linux/gpio.h | 273 +++++++++++++++++++++++++++++++++++++- 1 file changed, 266 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 285cc10355b2..0eb1f53b47e0 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -16,6 +16,8 @@ /* * The maximum size of name and label arrays. + * + * Must be a multiple of 8 to ensure 32/64-bit alignment of structs. */ #define GPIO_MAX_NAME_SIZE 32 @@ -32,6 +34,248 @@ struct gpiochip_info { __u32 lines; }; +/* + * Maximum number of requested lines. + * + * Must be no greater than 64, as bitmaps are restricted here to 64-bits + * for simplicity, and a multiple of 2 to ensure 32/64-bit alignment of + * structs. + */ +#define GPIO_V2_LINES_MAX 64 + +/* + * The maximum number of configuration attributes associated with a line + * request. + */ +#define GPIO_V2_LINE_NUM_ATTRS_MAX 10 + +/** + * enum gpio_v2_line_flag - &struct gpio_v2_line_attribute.flags values + */ +enum gpio_v2_line_flag { + GPIO_V2_LINE_FLAG_USED = 1ULL << 0, /* line is not available for request */ + GPIO_V2_LINE_FLAG_ACTIVE_LOW = 1ULL << 1, + GPIO_V2_LINE_FLAG_INPUT = 1ULL << 2, + GPIO_V2_LINE_FLAG_OUTPUT = 1ULL << 3, + GPIO_V2_LINE_FLAG_EDGE_RISING = 1ULL << 4, + GPIO_V2_LINE_FLAG_EDGE_FALLING = 1ULL << 5, + GPIO_V2_LINE_FLAG_OPEN_DRAIN = 1ULL << 6, + GPIO_V2_LINE_FLAG_OPEN_SOURCE = 1ULL << 7, + GPIO_V2_LINE_FLAG_BIAS_PULL_UP = 1ULL << 8, + GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN = 1ULL << 9, + GPIO_V2_LINE_FLAG_BIAS_DISABLED = 1ULL << 10, +}; + +/** + * struct gpio_v2_line_values - Values of GPIO lines + * @mask: a bitmap identifying the lines to get or set, with each bit + * number corresponding to the index into &struct + * gpio_v2_line_request.offsets. + * @bits: a bitmap containing the value of the lines, set to 1 for active + * and 0 for inactive. Note that this is the logical value, which will be + * the opposite of the physical value if the line is configured as active + * low. + */ +struct gpio_v2_line_values { + __aligned_u64 mask; + __aligned_u64 bits; +}; + +/** + * enum gpio_v2_line_attr_id - &struct gpio_v2_line_attribute.id values + */ +enum gpio_v2_line_attr_id { + GPIO_V2_LINE_ATTR_ID_FLAGS = 1, + GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES = 2, + GPIO_V2_LINE_ATTR_ID_DEBOUNCE = 3, +}; + +/** + * struct gpio_v2_line_attribute - a configurable attribute of a line + * @id: attribute identifier with value from &enum gpio_v2_line_attr_id + * @padding: reserved for future use and must be zero filled + * @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO + * line, with values from enum gpio_v2_line_flag, such as + * GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed + * together. This overrides the default flags contained in the &struct + * gpio_v2_line_config for the associated line. + * @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap + * containing the values to which the lines will be set, with each bit + * number corresponding to the index into &struct + * gpio_v2_line_request.offsets. + * @debounce_period: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired + * debounce period, in microseconds + */ +struct gpio_v2_line_attribute { + __u32 id; + __u32 padding; + union { + __aligned_u64 flags; + __aligned_u64 values; + __u32 debounce_period; + }; +}; + +/** + * struct gpio_v2_line_config_attribute - a configuration attribute + * associated with one or more of the requested lines. + * @mask: a bitmap identifying the lines to which the attribute applies, + * with each bit number corresponding to the index into &struct + * gpio_v2_line_request.offsets. + * @attr: the configurable attribute + */ +struct gpio_v2_line_config_attribute { + __aligned_u64 mask; + struct gpio_v2_line_attribute attr; +}; + +/** + * struct gpio_v2_line_config - Configuration for GPIO lines + * @flags: flags for the GPIO lines, with values from enum + * gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, + * GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together. This is the default for + * all requested lines but may be overridden for particular lines using + * attrs. + * @num_attrs: the number of attributes in attrs + * @padding: reserved for future use and must be zero filled + * @attrs: the configuration attributes associated with the requested + * lines. Any attribute should only be associated with a particular line + * once. If an attribute is associated with a line multiple times then the + * first occurrence (i.e. lowest index) has precedence. + */ +struct gpio_v2_line_config { + __aligned_u64 flags; + __u32 num_attrs; + /* + * Pad to fill implicit padding and provide space for future use. + */ + __u32 padding[5]; + struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX]; +}; + +/** + * struct gpio_v2_line_request - Information about a request for GPIO lines + * @offsets: an array of desired lines, specified by offset index for the + * associated GPIO device + * @consumer: a desired consumer label for the selected GPIO lines such as + * "my-bitbanged-relay" + * @config: requested configuration for the lines. + * @num_lines: number of lines requested in this request, i.e. the number + * of valid fields in the GPIO_V2_LINES_MAX sized arrays, set to 1 to + * request a single line + * @event_buffer_size: a suggested minimum number of line events that the + * kernel should buffer. This is only relevant if edge detection is + * enabled in the configuration. Note that this is only a suggested value + * and the kernel may allocate a larger buffer or cap the size of the + * buffer. If this field is zero then the buffer size defaults to a minimum + * of num_lines*16. + * @padding: reserved for future use and must be zero filled + * @fd: if successful this field will contain a valid anonymous file handle + * after a GPIO_GET_LINE_IOCTL operation, zero or negative value means + * error + */ +struct gpio_v2_line_request { + __u32 offsets[GPIO_V2_LINES_MAX]; + char consumer[GPIO_MAX_NAME_SIZE]; + struct gpio_v2_line_config config; + __u32 num_lines; + __u32 event_buffer_size; + /* + * Pad struct to 64-bit boundary and provide space for future use. + */ + __u32 padding[5]; + __s32 fd; +}; + +/** + * struct gpio_v2_line_info - Information about a certain GPIO line + * @name: the name of this GPIO line, such as the output pin of the line on + * the chip, a rail or a pin header name on a board, as specified by the + * gpio chip, may be empty + * @consumer: a functional name for the consumer of this GPIO line as set + * by whatever is using it, will be empty if there is no current user but + * may also be empty if the consumer doesn't set this up + * @flags: flags for the GPIO line, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, + * GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together + * @offset: the local offset on this GPIO device, fill this in when + * requesting the line information from the kernel + * @num_attrs: the number of attributes in attrs + * @attrs: the configuration attributes associated with the line. + * @padding: reserved for future use + */ +struct gpio_v2_line_info { + char name[GPIO_MAX_NAME_SIZE]; + char consumer[GPIO_MAX_NAME_SIZE]; + __u32 offset; + __u32 num_attrs; + __aligned_u64 flags; + struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX]; + /* + * Pad struct to 64-bit boundary and provide space for future use. + */ + __u32 padding[4]; +}; + +enum gpio_v2_line_changed_type { + GPIO_V2_LINE_CHANGED_REQUESTED = 1, + GPIO_V2_LINE_CHANGED_RELEASED = 2, + GPIO_V2_LINE_CHANGED_CONFIG = 3, +}; + +/** + * struct gpio_v2_line_info_changed - Information about a change in status + * of a GPIO line + * @info: updated line information + * @timestamp: estimate of time of status change occurrence, in nanoseconds + * @event_type: the type of change with a value from enum + * gpio_v2_line_changed_type + * @padding: reserved for future use + */ +struct gpio_v2_line_info_changed { + struct gpio_v2_line_info info; + __aligned_u64 timestamp; + __u32 event_type; + /* + * Pad struct to 64-bit boundary and provide space for future use. + */ + __u32 padding[5]; +}; + +enum gpio_v2_line_event_id { + GPIO_V2_LINE_EVENT_RISING_EDGE = 1, + GPIO_V2_LINE_EVENT_FALLING_EDGE = 2, +}; + +/** + * struct gpio_v2_line_event - The actual event being pushed to userspace + * @timestamp: best estimate of time of event occurrence, in nanoseconds. + * The timestamp is read from CLOCK_MONOTONIC and is intended to allow the + * accurate measurement of the time between events. It does not provide + * the wall-clock time. + * @id: event identifier with value from enum gpio_v2_line_event_id + * @offset: the offset of the line that triggered the event + * @seqno: the sequence number for this event in the sequence of events for + * all the lines in this line request + * @line_seqno: the sequence number for this event in the sequence of + * events on this particular line + * @padding: reserved for future use + */ +struct gpio_v2_line_event { + __aligned_u64 timestamp; + __u32 id; + __u32 offset; + __u32 seqno; + __u32 line_seqno; + /* + * Pad struct to 64-bit boundary and provide space for future use. + */ + __u32 padding[6]; +}; + +/* + * ABI v1 + */ + /* Informational flags */ #define GPIOLINE_FLAG_KERNEL (1UL << 0) /* Line used by the kernel */ #define GPIOLINE_FLAG_IS_OUT (1UL << 1) @@ -149,8 +393,6 @@ struct gpiohandle_config { __u32 padding[4]; /* padding for future use */ }; -#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0a, struct gpiohandle_config) - /** * struct gpiohandle_data - Information of values on a GPIO handle * @values: when getting the state of lines this contains the current @@ -161,9 +403,6 @@ struct gpiohandle_data { __u8 values[GPIOHANDLES_MAX]; }; -#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data) -#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data) - /* Eventrequest flags */ #define GPIOEVENT_REQUEST_RISING_EDGE (1UL << 0) #define GPIOEVENT_REQUEST_FALLING_EDGE (1UL << 1) @@ -207,11 +446,31 @@ struct gpioevent_data { __u32 id; }; +/* + * v1 and v2 ioctl()s + */ #define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info) +#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0C, __u32) + +/* + * v2 ioctl()s + */ +#define GPIO_V2_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x05, struct gpio_v2_line_info) +#define GPIO_V2_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x06, struct gpio_v2_line_info) +#define GPIO_V2_GET_LINE_IOCTL _IOWR(0xB4, 0x07, struct gpio_v2_line_request) +#define GPIO_V2_LINE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0D, struct gpio_v2_line_config) +#define GPIO_V2_LINE_GET_VALUES_IOCTL _IOWR(0xB4, 0x0E, struct gpio_v2_line_values) +#define GPIO_V2_LINE_SET_VALUES_IOCTL _IOWR(0xB4, 0x0F, struct gpio_v2_line_values) + +/* + * v1 ioctl()s + */ #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info) -#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0b, struct gpioline_info) -#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0c, __u32) #define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request) #define GPIO_GET_LINEEVENT_IOCTL _IOWR(0xB4, 0x04, struct gpioevent_request) +#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data) +#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data) +#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0A, struct gpiohandle_config) +#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0B, struct gpioline_info) #endif /* _UAPI_GPIO_H_ */ From patchwork Thu Aug 27 14:00:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352619 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=Q/poDNVu; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bclk02x3Pz9sTK for ; Fri, 28 Aug 2020 00:39:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727878AbgH0Oi7 (ORCPT ); Thu, 27 Aug 2020 10:38:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728121AbgH0ODq (ORCPT ); Thu, 27 Aug 2020 10:03:46 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EEB7C0611E1; Thu, 27 Aug 2020 07:01:56 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id t9so3568937pfq.8; Thu, 27 Aug 2020 07:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/NSQWGG4Dd/lyfNZqBtWl2adXGHauiXHc+m8VldHKpo=; b=Q/poDNVum56M1D4JtTfEW96M6P1lqZC+eM7OReHNWgYsQTkcoJxm6fij5Ed2tNphfb BWRcHhd05tZgmivp58QcT97v3RmUMjEj43LY/eupdErLzfkDlUienXkPT0I26XSAMWQe ZUf3ZOWvV5lEl8JIO+aZPOO7DX5G517/IFa8KzQJHnM45iJQUR7mRJC0srjs1KQZDNrU ZXCkMviXWJAeYW5Iaqxv8TGPy1q1iB7/zIVq1uQFQcH+nUdyhscz6D+36mMBXRLSi15F JR6KJZX2+PJqI4uXJUIqiIun7pIVDNrLnMQIIiMh5gasVnrsa8oPRf/4gyedqY4v2E6C V9zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/NSQWGG4Dd/lyfNZqBtWl2adXGHauiXHc+m8VldHKpo=; b=WSBGJiA9ILNT3FcK6AjOlDwBJcajvFXeqYCcw2/YJqhezGiQtYdeCw29GNyyGwSGPZ 5IXNhPK9hyBJRuWawJRyr1/N0TdrDpKQgQhgwVZGDvurBkww5KR0XCc4FVxo4eOoJDSN IbBF+EQmi15Qd2OREIBTwu2znNR5sttoGcsP+UwiQzSvX6LaoVbBjtdde/2f+gCc37HD T24eN+aIGc0Kg6yqCfqaTzbihVZgSnfNNNsdttJhSBGazMPHM8O45Oxa/28ooKKNS8gn 54dLlIvOzwIpD08hOmzguNZ6vhk7naeqLjfmBMs8amZ7u+vSY/6LD5orMUweZT9lMJmt UQcg== X-Gm-Message-State: AOAM533GDkMnh/oKnf13nrEWTa9VpCneLa1UQ46jRQ0afP+KSavZN7LZ /6BAE7WpKzkDRzcyJvSO+s7m5+PrZWg= X-Google-Smtp-Source: ABdhPJwDrjKE73x/7b0LeUfXIR6oqA0Xmbrt+jQ+dSmTyLQCytJ1sLonVkj8J1aMDoIcZUiyN5Vhmw== X-Received: by 2002:a63:6f02:: with SMTP id k2mr14546485pgc.191.1598536915157; Thu, 27 Aug 2020 07:01:55 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:01:54 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 05/20] gpiolib: make cdev a build option Date: Thu, 27 Aug 2020 22:00:05 +0800 Message-Id: <20200827140020.159627-6-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Make the gpiolib-cdev module a build option. This allows the CDEV interface to be removed from the kernel to reduce kernel size in applications where is it not required, and provides the parent for other other CDEV interface specific build options to follow. Suggested-by: Bartosz Golaszewski Signed-off-by: Kent Gibson --- drivers/gpio/Kconfig | 17 +++++++++++++++-- drivers/gpio/Makefile | 2 +- drivers/gpio/gpiolib-cdev.h | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 8030fd91a3cc..8e409b9c33dc 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -66,8 +66,21 @@ config GPIO_SYSFS This ABI is deprecated. If you want to use GPIO from userspace, use the character device /dev/gpiochipN with the appropriate - ioctl() operations instead. The character device is always - available. + ioctl() operations instead. + +config GPIO_CDEV + bool + prompt "Character device (/dev/gpiochipN) support" if EXPERT + default y + help + Say Y here to add the character device /dev/gpiochipN interface + for GPIOs. The character device allows userspace to control GPIOs + using ioctl() operations. + + Only say N if you are sure that the GPIO character device is not + required. + + If unsure, say Y. config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 4f9abff4f2dc..7c24c8d77068 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -7,8 +7,8 @@ obj-$(CONFIG_GPIOLIB) += gpiolib.o obj-$(CONFIG_GPIOLIB) += gpiolib-devres.o obj-$(CONFIG_GPIOLIB) += gpiolib-legacy.o obj-$(CONFIG_GPIOLIB) += gpiolib-devprop.o -obj-$(CONFIG_GPIOLIB) += gpiolib-cdev.o obj-$(CONFIG_OF_GPIO) += gpiolib-of.o +obj-$(CONFIG_GPIO_CDEV) += gpiolib-cdev.o obj-$(CONFIG_GPIO_SYSFS) += gpiolib-sysfs.o obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o diff --git a/drivers/gpio/gpiolib-cdev.h b/drivers/gpio/gpiolib-cdev.h index 973578e7ad10..19a4e3d57120 100644 --- a/drivers/gpio/gpiolib-cdev.h +++ b/drivers/gpio/gpiolib-cdev.h @@ -5,7 +5,22 @@ #include +#ifdef CONFIG_GPIO_CDEV + int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt); void gpiolib_cdev_unregister(struct gpio_device *gdev); +#else + +static inline int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) +{ + return 0; +} + +static inline void gpiolib_cdev_unregister(struct gpio_device *gdev) +{ +} + +#endif /* CONFIG_GPIO_CDEV */ + #endif /* GPIOLIB_CDEV_H */ From patchwork Thu Aug 27 14:00:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352614 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=X4apR4B/; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BclgB6cN3z9sRK for ; Fri, 28 Aug 2020 00:37:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726217AbgH0Ogy (ORCPT ); Thu, 27 Aug 2020 10:36:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728127AbgH0ODq (ORCPT ); Thu, 27 Aug 2020 10:03:46 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1EB6C0611E2; Thu, 27 Aug 2020 07:02:07 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id 2so2672043pjx.5; Thu, 27 Aug 2020 07:02:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mL4xScgLpm6GZnT5ws/9FIwwZ74rEqIv1N27JN7upQo=; b=X4apR4B//8lGTz4JOK1BB3c9HouQiXw6zDBOEnLp1G3RBtqM/1rNn25hbIvvyCyWEc SD8csXQRRS/pQlJfn/KAIElO13CKPLtqz83CDRkgOH03XUT9zyQ17e4SWCPbgoyb6scp 3P58vJFjMOBjNxC01VwTD2SM7ExaTY/19NJa/6PTuGlgkzJGPwhtTzvR7y446Gya9iRN MKKoMVLKZIe360xl/jnataGlH7pkU6JLZW3sZsMX2kg4iJvYifmRvWjOJaySZIdEEyx5 kkqdqIpEfPGhO5OeMNTgbmh5r+Jl37OrWnFBifWR1CdGkzawemaBw+BnaQBZmTAmVPa/ Z53A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mL4xScgLpm6GZnT5ws/9FIwwZ74rEqIv1N27JN7upQo=; b=idcCGYmIT7wy2YBPc12oZbK9cRdUiKy0U0tSRY87voUfxZOF6uK/JWdSfc5G8kLqVy Hy6troDojkWyeG8Vo/bbhA3iAL7nfpqBNyl2m0VvdlegCBLCb2zHtMC7e8qkpW1RsnGe 9ImW1vf0wH7lLxXqfUwzd2XgED08PFCx5v9KPOhjtLtNtAg7bB9UUZ9lkXyvPCHOYnBi +D0cppcWYsFENQEtXMd2ACFQcOQ4/ozLzyL6KG3VsdNOB1qTriMG39OlLAvl+a04Zu18 LhCJHDsVBXbGRjqP/PUiQJapI/03pKv6iKvwJTmpOT0x2PczgYiarX+kC/bwxuPRHQdP r2+Q== X-Gm-Message-State: AOAM530FnqKSSk6S9cf+jzj//nywob0JR5Z3CwUyf4OUhFvlnxhUu4bU xgwYfqtxrGnE42I3pYWDUj6rrqXVXKc= X-Google-Smtp-Source: ABdhPJxycp3ZEidz+QyXimUL7Vj0YcVmXgcncrMbBCfAb4vsa8/B5X+dEmxWJJszYWemsh3DV3J+pw== X-Received: by 2002:a17:90b:1b45:: with SMTP id nv5mr11154625pjb.35.1598536926957; Thu, 27 Aug 2020 07:02:06 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.02.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:02:05 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 06/20] gpiolib: add build option for CDEV v1 ABI Date: Thu, 27 Aug 2020 22:00:06 +0800 Message-Id: <20200827140020.159627-7-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a build option to allow the removal of the CDEV v1 ABI. Suggested-by: Bartosz Golaszewski Signed-off-by: Kent Gibson --- This patch is before the v2 implementation, and is non-functional until that patch, as some parts of that patch would be written slightly differently if removing v1 was not considered. Adding this patch after that would necessitate revisiting the v2 changes, so this ordering results in two simpler patches. drivers/gpio/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 8e409b9c33dc..0c62e35cf3a6 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -82,6 +82,18 @@ config GPIO_CDEV If unsure, say Y. +config GPIO_CDEV_V1 + bool "Support GPIO ABI Version 1" + default y + depends on GPIO_CDEV + help + Say Y here to support version 1 of the GPIO CDEV ABI. + + This ABI version is deprecated and will be removed in the future. + Please use the latest ABI for new developments. + + If unsure, say Y. + config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers tristate From patchwork Thu Aug 27 14:00:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352621 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=LcrpKLdo; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BclsR5tttz9sTK for ; Fri, 28 Aug 2020 00:46:03 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728007AbgH0Oj4 (ORCPT ); Thu, 27 Aug 2020 10:39:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728146AbgH0ODq (ORCPT ); Thu, 27 Aug 2020 10:03:46 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 033CBC0611E9; Thu, 27 Aug 2020 07:02:18 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id mw10so2647057pjb.2; Thu, 27 Aug 2020 07:02:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=y+cUd9jWxq4U7fz9yy/sM6N4SjlSfrDO3xMgWNGotCU=; b=LcrpKLdobe1+E32kIL9OeBOKyk120ANlkfFmqBnkgxGvSbaMg+M7PXl3hd7tpv2727 yVIcPtH07vJgIf93/FBxE0LYEdFSsbaWqaVjHKZQMVpp+mUgDDOdj8KYqfaOXMjS/lo6 3YYWd9cmkabcJX/fKYIVCqGsRedwQIA/bePv+6jt2Z3VMw9Zjk03TfZ1cqWhrnkbsoT+ o4ISh8vm+RAl9k0ms5dMlmOumh1S7ZzfiimHgze794gIXU9/GDwWWfDMFVvbpf1pdAbX ReIONfxwDOuylPA7QYyPdZ3iBSrsugw8cQNnUuSX8GcumlEU/TrEmVc+OyYl7Hv6D+4S 5T4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=y+cUd9jWxq4U7fz9yy/sM6N4SjlSfrDO3xMgWNGotCU=; b=C+kDKpyUdFaFza4JHrJFwjO5HevgRwzXiYi/PSZXDa2lixl/SN5C4EvSk2+WoGVDqJ Hff32cKucCDHPL2HBVaFyrg5sS1UYam5VCIZNzmKpI+4cEBeuWzm1sn3/MPrPX1D28f+ DFk97OYrZggdZydb/PJyPg3Ot6YMVRb4Uguuwxj7w7inbDUhk6N2Oc1knD5ROpAu2vsj /T7UrVpdFN3lAfrvheevOOYCWhSlguIoyY8PWOU1fDTkiorf6ND6+YP9LQrM3gNSTh92 f78YgaDPYjcfw3/4RZ//gZEsQZEf1Y00E4a3htp6282d/Y/8/fQkwwtbgp7hFctrDe2A dmAg== X-Gm-Message-State: AOAM533fHbvN0Yys6YAxbuHjUQB1biLUKOeiVEROuJdhpt6jzhIko11k roAxMpNE8sL2SEtDVDZSeKcySLZWpqI= X-Google-Smtp-Source: ABdhPJyW63FqDnI3CTVN4jNI/AXiWZNEuwJfgY/g4f5OklrWy2oOOGcgFjDmvg4t7FlixjgDz81tjQ== X-Received: by 2002:a17:90b:138d:: with SMTP id hr13mr10224922pjb.14.1598536936711; Thu, 27 Aug 2020 07:02:16 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.02.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:02:15 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL Date: Thu, 27 Aug 2020 22:00:07 +0800 Message-Id: <20200827140020.159627-8-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for requesting lines using the GPIO_V2_GET_LINE_IOCTL, and returning their current values using GPIO_V2_LINE_GET_VALUES_IOCTL. The struct linereq implementation is based on the v1 struct linehandle implementation. Signed-off-by: Kent Gibson --- The linereq_ioctl() is a simple wrapper around linereq_get_values() here, but will be extended with other ioctls in subsequent patches. Similarly, the struct line only contains the desc here, but will receive the edge detector and debouncer fields in subsequent patches. Changes for v5: - as per cover letter Changes for v4: - fix handling of mask in line_get_values drivers/gpio/gpiolib-cdev.c | 418 ++++++++++++++++++++++++++++++++++++ 1 file changed, 418 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 8b012879fe3f..1c0ed493cb83 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include +#include #include #include #include @@ -34,6 +36,7 @@ * GPIO line handle management */ +#ifdef CONFIG_GPIO_CDEV_V1 /** * struct linehandle_state - contains the state of a userspace handle * @gdev: the GPIO device the handle pertains to @@ -376,6 +379,396 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) linehandle_free(lh); return ret; } +#endif /* CONFIG_GPIO_CDEV_V1 */ + +/** + * struct line - contains the state of a requested line + * @desc: the GPIO descriptor for this line. + */ +struct line { + struct gpio_desc *desc; +}; + +/** + * struct linereq - contains the state of a userspace line request + * @gdev: the GPIO device the line request pertains to + * @label: consumer label used to tag GPIO descriptors + * @num_lines: the number of lines in the lines array + * @lines: the lines held by this line request, with @num_lines elements. + */ +struct linereq { + struct gpio_device *gdev; + const char *label; + u32 num_lines; + struct line lines[]; +}; + +#define GPIO_V2_LINE_BIAS_FLAGS \ + (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ + GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ + GPIO_V2_LINE_FLAG_BIAS_DISABLED) + +#define GPIO_V2_LINE_DIRECTION_FLAGS \ + (GPIO_V2_LINE_FLAG_INPUT | \ + GPIO_V2_LINE_FLAG_OUTPUT) + +#define GPIO_V2_LINE_DRIVE_FLAGS \ + (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ + GPIO_V2_LINE_FLAG_OPEN_SOURCE) + +#define GPIO_V2_LINE_VALID_FLAGS \ + (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ + GPIO_V2_LINE_DIRECTION_FLAGS | \ + GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_BIAS_FLAGS) + +static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, + unsigned int line_idx) +{ + unsigned int i; + u64 mask = BIT_ULL(line_idx); + + for (i = 0; i < lc->num_attrs; i++) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && + (lc->attrs[i].mask & mask)) + return lc->attrs[i].attr.flags; + } + return lc->flags; +} + +static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, + unsigned int line_idx) +{ + unsigned int i; + u64 mask = BIT_ULL(line_idx); + + for (i = 0; i < lc->num_attrs; i++) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && + (lc->attrs[i].mask & mask)) + return !!(lc->attrs[i].attr.values & mask); + } + return 0; +} + +static int gpio_v2_line_flags_validate(u64 flags) +{ + /* Return an error if an unknown flag is set */ + if (flags & ~GPIO_V2_LINE_VALID_FLAGS) + return -EINVAL; + + /* + * Do not allow both INPUT & OUTPUT flags to be set as they are + * contradictory. + */ + if ((flags & GPIO_V2_LINE_FLAG_INPUT) && + (flags & GPIO_V2_LINE_FLAG_OUTPUT)) + return -EINVAL; + + /* + * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If + * the hardware actually supports enabling both at the same time the + * electrical result would be disastrous. + */ + if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && + (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) + return -EINVAL; + + /* Drive requires explicit output direction. */ + if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && + !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) + return -EINVAL; + + /* Bias requires explicit direction. */ + if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && + !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) + return -EINVAL; + + /* Only one bias flag can be set. */ + if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && + (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | + GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || + ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && + (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) + return -EINVAL; + + return 0; +} + +static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, + unsigned int num_lines) +{ + unsigned int i; + u64 flags; + int ret; + + if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) + return -EINVAL; + + if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) + return -EINVAL; + + for (i = 0; i < num_lines; i++) { + flags = gpio_v2_line_config_flags(lc, i); + ret = gpio_v2_line_flags_validate(flags); + if (ret) + return ret; + } + return 0; +} + +static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, + unsigned long *flagsp) +{ + assign_bit(FLAG_ACTIVE_LOW, flagsp, + flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); + if (flags & GPIO_V2_LINE_FLAG_OUTPUT) + set_bit(FLAG_IS_OUT, flagsp); + else if (flags & GPIO_V2_LINE_FLAG_INPUT) + clear_bit(FLAG_IS_OUT, flagsp); + assign_bit(FLAG_OPEN_DRAIN, flagsp, + flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); + assign_bit(FLAG_OPEN_SOURCE, flagsp, + flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); + assign_bit(FLAG_PULL_UP, flagsp, + flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); + assign_bit(FLAG_PULL_DOWN, flagsp, + flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); + assign_bit(FLAG_BIAS_DISABLE, flagsp, + flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); +} + +static long linereq_get_values(struct linereq *lr, void __user *ip) +{ + struct gpio_v2_line_values lv; + DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_desc **descs; + unsigned int i, didx, num_get; + int ret; + + /* NOTE: It's ok to read values of output lines. */ + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + for (num_get = 0, i = 0; i < lr->num_lines; i++) { + if (lv.mask & BIT_ULL(i)) { + num_get++; + descs = &lr->lines[i].desc; + } + } + + if (num_get == 0) + return -EINVAL; + + if (num_get != 1) { + descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); + for (didx = 0, i = 0; i < lr->num_lines; i++) { + if (lv.mask & BIT_ULL(i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret = gpiod_get_array_value_complex(false, true, num_get, + descs, NULL, vals); + + if (num_get != 1) + kfree(descs); + if (ret) + return ret; + + lv.bits = 0; + for (didx = 0, i = 0; i < lr->num_lines; i++) { + if (lv.mask & BIT_ULL(i)) { + if (test_bit(didx, vals)) + lv.bits |= BIT_ULL(i); + didx++; + } + } + + if (copy_to_user(ip, &lv, sizeof(lv))) + return -EFAULT; + + return 0; +} + +static long linereq_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct linereq *lr = file->private_data; + void __user *ip = (void __user *)arg; + + if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) + return linereq_get_values(lr, ip); + + return -EINVAL; +} + +#ifdef CONFIG_COMPAT +static long linereq_ioctl_compat(struct file *file, unsigned int cmd, + unsigned long arg) +{ + return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); +} +#endif + +static void linereq_free(struct linereq *lr) +{ + unsigned int i; + + for (i = 0; i < lr->num_lines; i++) { + if (lr->lines[i].desc) + gpiod_free(lr->lines[i].desc); + } + kfree(lr->label); + put_device(&lr->gdev->dev); + kfree(lr); +} + +static int linereq_release(struct inode *inode, struct file *file) +{ + struct linereq *lr = file->private_data; + + linereq_free(lr); + return 0; +} + +static const struct file_operations line_fileops = { + .release = linereq_release, + .owner = THIS_MODULE, + .llseek = noop_llseek, + .unlocked_ioctl = linereq_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = linereq_ioctl_compat, +#endif +}; + +static int linereq_create(struct gpio_device *gdev, void __user *ip) +{ + struct gpio_v2_line_request ulr; + struct gpio_v2_line_config *lc; + struct linereq *lr; + struct file *file; + unsigned long flags; + unsigned int i; + int fd, ret; + + if (copy_from_user(&ulr, ip, sizeof(ulr))) + return -EFAULT; + + if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) + return -EINVAL; + + if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) + return -EINVAL; + + lc = &ulr.config; + ret = gpio_v2_line_config_validate(lc, ulr.num_lines); + if (ret) + return ret; + + lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); + if (!lr) + return -ENOMEM; + + lr->gdev = gdev; + get_device(&gdev->dev); + + /* Make sure this is terminated */ + ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; + if (strlen(ulr.consumer)) { + lr->label = kstrdup(ulr.consumer, GFP_KERNEL); + if (!lr->label) { + ret = -ENOMEM; + goto out_free_linereq; + } + } + + lr->num_lines = ulr.num_lines; + + /* Request each GPIO */ + for (i = 0; i < ulr.num_lines; i++) { + u32 offset = ulr.offsets[i]; + struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); + + if (IS_ERR(desc)) { + ret = PTR_ERR(desc); + goto out_free_linereq; + } + + ret = gpiod_request(desc, lr->label); + if (ret) + goto out_free_linereq; + + lr->lines[i].desc = desc; + flags = gpio_v2_line_config_flags(lc, i); + gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); + + ret = gpiod_set_transitory(desc, false); + if (ret < 0) + goto out_free_linereq; + + /* + * Lines have to be requested explicitly for input + * or output, else the line will be treated "as is". + */ + if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { + int val = gpio_v2_line_config_output_value(lc, i); + + ret = gpiod_direction_output(desc, val); + if (ret) + goto out_free_linereq; + } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { + ret = gpiod_direction_input(desc); + if (ret) + goto out_free_linereq; + } + + blocking_notifier_call_chain(&desc->gdev->notifier, + GPIOLINE_CHANGED_REQUESTED, desc); + + dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", + offset); + } + + fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); + if (fd < 0) { + ret = fd; + goto out_free_linereq; + } + + file = anon_inode_getfile("gpio-line", &line_fileops, lr, + O_RDONLY | O_CLOEXEC); + if (IS_ERR(file)) { + ret = PTR_ERR(file); + goto out_put_unused_fd; + } + + ulr.fd = fd; + if (copy_to_user(ip, &ulr, sizeof(ulr))) { + /* + * fput() will trigger the release() callback, so do not go onto + * the regular error cleanup path here. + */ + fput(file); + put_unused_fd(fd); + return -EFAULT; + } + + fd_install(fd, file); + + dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", + lr->num_lines); + + return 0; + +out_put_unused_fd: + put_unused_fd(fd); +out_free_linereq: + linereq_free(lr); + return ret; +} + +#ifdef CONFIG_GPIO_CDEV_V1 /* * GPIO line event management @@ -745,6 +1138,8 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) return ret; } +#endif /* CONFIG_GPIO_CDEV_V1 */ + static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpioline_info *info) { @@ -842,6 +1237,7 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) return -EFAULT; return 0; +#ifdef CONFIG_GPIO_CDEV_V1 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { struct gpioline_info lineinfo; @@ -884,6 +1280,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) } return 0; +#endif /* CONFIG_GPIO_CDEV_V1 */ + } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { + return linereq_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { if (copy_from_user(&offset, ip, sizeof(offset))) return -EFAULT; @@ -1104,6 +1503,25 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) MAJOR(devt), gdev->id); return 0; + /* + * array sizes must ensure 64-bit alignment and not create holes in + * the struct packing. + */ + BUILD_BUG_ON(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); + BUILD_BUG_ON(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); + + /* + * check that uAPI structs are 64-bit aligned for 32/64-bit + * compatibility + */ + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); + BUILD_BUG_ON(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); } void gpiolib_cdev_unregister(struct gpio_device *gdev) From patchwork Thu Aug 27 14:00:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352616 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=ASUfnAwN; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bclgg4L5cz9sRK for ; Fri, 28 Aug 2020 00:37:35 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726794AbgH0Oha (ORCPT ); Thu, 27 Aug 2020 10:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728148AbgH0ODq (ORCPT ); Thu, 27 Aug 2020 10:03:46 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3678FC0619C0; Thu, 27 Aug 2020 07:02:27 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id p37so3446423pgl.3; Thu, 27 Aug 2020 07:02:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UdCpsZTzCCYntC6KrIJFb7IjzvVo+TE/5fdTdt2fe6Y=; b=ASUfnAwNmkVD/TgWofl9nJ/MC+C7sHZvLWNc9AcpYUxkG5r8tc5senTf6Qbefv7+yv HqTM22Wu88dCK49LWue1dzGLp9qGu+9ravln+a2g7kis+zPz79T/FdOBHn1FAeggWh+b +f3iCi2gXTylX996rbw6N5R6sSmch4lMiNo8XftpSmwkDzzPgbUiddsjN5mbEUUMuqeC x5xGH0ZVKSmNuFv22DyxzPVQkR9KACjqZ2Lo2o46zRklvLwT5ybLLRjBUB3m6PgX5L4x bbBjPG390r6ORGs/NTFNdMyqFQTW39k7wAIlyJQ7gHEwudvxwKwXX2X94i3qIJIX4EIY 7g2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UdCpsZTzCCYntC6KrIJFb7IjzvVo+TE/5fdTdt2fe6Y=; b=YqAOpxQsgwtVgb9J9SG1dayVlrnmiKoMBQpRcG8KJHTviMGBwKYLni46RAo0nnusp8 3dz5A0IasOfMkXMVa1ZEo+1BqqfvxPodywEI0aGhkYO3Y5IdlvjsEOl0UlhoDWtRC9nn wBBMScQx7v0L2sCgAvQe/Y53bR2RycrgZD9Rxc0eFRs5cj+BEnNnxvw5gEoEaloEfRsv pFPpiGhD2zdInHrVV72CR/JSA/oz8PFa0psQqI0Mpupq6gv5Ncul40bTdoD3G8pvU0LR MeOYVx+u8c9kLTDTm2N5LVZoIfoy2yT2bdQhfYTh+Uz0sNlKEm4uoHUSKH6ilaZHqQol tLFQ== X-Gm-Message-State: AOAM531+QxeC6CSZIcPnx3E65TXJYpVBIQ7HY93KVp8LUEj+T2/r1qyV 0HaS/OJjBNpwfa9FCUDYZgyAJ0JEe4E= X-Google-Smtp-Source: ABdhPJwcY4lU6YVkHzym4NZq7lQ2QKy2Z/43cb+y0RRTLlClFOss4vURbXDerjnoY79Zp0W0g26HfA== X-Received: by 2002:aa7:9d04:: with SMTP id k4mr16462305pfp.256.1598536945899; Thu, 27 Aug 2020 07:02:25 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.02.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:02:25 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL Date: Thu, 27 Aug 2020 22:00:08 +0800 Message-Id: <20200827140020.159627-9-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL. The core of this change is the event kfifo switching to contain struct gpioline_info_changed_v2, instead of v1 as v2 is richer. The two uAPI versions are mostly independent - other than where they both provide line info changes via reads on the chip fd. As the info change structs differ between v1 and v2, the infowatch implementation tracks which version of the infowatch ioctl, either GPIO_GET_LINEINFO_WATCH_IOCTL or GPIO_V2_GET_LINEINFO_WATCH_IOCTL, initiates the initial watch and returns the corresponding info change struct to the read. The version supported on that fd locks to that version on the first watch request, so subsequent watches from that process must use the same uAPI version. Signed-off-by: Kent Gibson --- Changes for v5: - as per cover letter Changes for v4: - replace strncpy with memcpy in gpio_v2_line_info_to_v1 drivers/gpio/gpiolib-cdev.c | 195 +++++++++++++++++++++++++++++++----- 1 file changed, 168 insertions(+), 27 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 1c0ed493cb83..281255e05323 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -162,7 +162,8 @@ static long linehandle_set_config(struct linehandle_state *lh, } blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_CONFIG, desc); + GPIO_V2_LINE_CHANGED_CONFIG, + desc); } return 0; } @@ -334,7 +335,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) } blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_REQUESTED, desc); + GPIO_V2_LINE_CHANGED_REQUESTED, desc); dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", offset); @@ -724,7 +725,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_REQUESTED, desc); + GPIO_V2_LINE_CHANGED_REQUESTED, desc); dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", offset); @@ -1071,7 +1072,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) goto out_free_le; blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_REQUESTED, desc); + GPIO_V2_LINE_CHANGED_REQUESTED, desc); irq = gpiod_to_irq(desc); if (irq <= 0) { @@ -1138,17 +1139,59 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) return ret; } +static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, + struct gpioline_info *info_v1) +{ + u64 flagsv2 = info_v2->flags; + + memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); + memcpy(info_v1->consumer, info_v2->consumer, + sizeof(info_v1->consumer)); + info_v1->line_offset = info_v2->offset; + info_v1->flags = 0; + + if (flagsv2 & GPIO_V2_LINE_FLAG_USED) + info_v1->flags |= GPIOLINE_FLAG_KERNEL; + + if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) + info_v1->flags |= GPIOLINE_FLAG_IS_OUT; + + if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) + info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; + + if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) + info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; + if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) + info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; + + if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) + info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; + if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) + info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; + if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) + info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; +} + +static void gpio_v2_line_info_changed_to_v1( + struct gpio_v2_line_info_changed *lic_v2, + struct gpioline_info_changed *lic_v1) +{ + gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); + lic_v1->timestamp = lic_v2->timestamp; + lic_v1->event_type = lic_v2->event_type; +} + #endif /* CONFIG_GPIO_CDEV_V1 */ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, - struct gpioline_info *info) + struct gpio_v2_line_info *info) { struct gpio_chip *gc = desc->gdev->chip; bool ok_for_pinctrl; unsigned long flags; memset(info, 0, sizeof(*info)); - info->line_offset = gpio_chip_hwgpio(desc); + info->offset = gpio_chip_hwgpio(desc); /* * This function takes a mutex so we must check this before taking * the spinlock. @@ -1157,7 +1200,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, * lock common to both frameworks? */ ok_for_pinctrl = - pinctrl_gpio_can_use_line(gc->base + info->line_offset); + pinctrl_gpio_can_use_line(gc->base + info->offset); spin_lock_irqsave(&gpio_lock, flags); @@ -1178,23 +1221,27 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, test_bit(FLAG_EXPORT, &desc->flags) || test_bit(FLAG_SYSFS, &desc->flags) || !ok_for_pinctrl) - info->flags |= GPIOLINE_FLAG_KERNEL; + info->flags |= GPIO_V2_LINE_FLAG_USED; + if (test_bit(FLAG_IS_OUT, &desc->flags)) - info->flags |= GPIOLINE_FLAG_IS_OUT; + info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; + else + info->flags |= GPIO_V2_LINE_FLAG_INPUT; + if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) - info->flags |= GPIOLINE_FLAG_ACTIVE_LOW; + info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | - GPIOLINE_FLAG_IS_OUT); + info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | - GPIOLINE_FLAG_IS_OUT); + info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; + if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) - info->flags |= GPIOLINE_FLAG_BIAS_DISABLE; + info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; if (test_bit(FLAG_PULL_DOWN, &desc->flags)) - info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; + info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; if (test_bit(FLAG_PULL_UP, &desc->flags)) - info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; + info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; spin_unlock_irqrestore(&gpio_lock, flags); } @@ -1202,11 +1249,65 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_chardev_data { struct gpio_device *gdev; wait_queue_head_t wait; - DECLARE_KFIFO(events, struct gpioline_info_changed, 32); + DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); struct notifier_block lineinfo_changed_nb; unsigned long *watched_lines; +#ifdef CONFIG_GPIO_CDEV_V1 + atomic_t watch_abi_version; +#endif }; +#ifdef CONFIG_GPIO_CDEV_V1 +static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, + unsigned int version) +{ + int abiv = atomic_read(&cdata->watch_abi_version); + + if (abiv == 0) { + atomic_cmpxchg(&cdata->watch_abi_version, 0, version); + abiv = atomic_read(&cdata->watch_abi_version); + } + if (abiv != version) + return -EPERM; + return 0; +} +#endif + +static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, + bool watch) +{ + struct gpio_desc *desc; + struct gpio_v2_line_info lineinfo; + + if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) + return -EFAULT; + + if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) + return -EINVAL; + + desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + if (watch) { +#ifdef CONFIG_GPIO_CDEV_V1 + if (lineinfo_ensure_abi_version(cdev, 2)) + return -EPERM; +#endif + if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) + return -EBUSY; + } + gpio_desc_to_lineinfo(desc, &lineinfo); + + if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { + if (watch) + clear_bit(lineinfo.offset, cdev->watched_lines); + return -EFAULT; + } + + return 0; +} + /* * gpio_ioctl() - ioctl handler for the GPIO chardev */ @@ -1216,7 +1317,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) struct gpio_device *gdev = cdev->gdev; struct gpio_chip *gc = gdev->chip; void __user *ip = (void __user *)arg; - struct gpio_desc *desc; __u32 offset; /* We fail any subsequent ioctl():s when the chip is gone */ @@ -1239,7 +1339,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return 0; #ifdef CONFIG_GPIO_CDEV_V1 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { + struct gpio_desc *desc; struct gpioline_info lineinfo; + struct gpio_v2_line_info lineinfo_v2; if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) return -EFAULT; @@ -1249,7 +1351,8 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (IS_ERR(desc)) return PTR_ERR(desc); - gpio_desc_to_lineinfo(desc, &lineinfo); + gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) return -EFAULT; @@ -1259,7 +1362,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { return lineevent_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { + struct gpio_desc *desc; struct gpioline_info lineinfo; + struct gpio_v2_line_info lineinfo_v2; if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) return -EFAULT; @@ -1269,10 +1374,14 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (IS_ERR(desc)) return PTR_ERR(desc); + if (lineinfo_ensure_abi_version(cdev, 1)) + return -EPERM; + if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) return -EBUSY; - gpio_desc_to_lineinfo(desc, &lineinfo); + gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { clear_bit(lineinfo.line_offset, cdev->watched_lines); @@ -1281,6 +1390,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return 0; #endif /* CONFIG_GPIO_CDEV_V1 */ + } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL || + cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { + return lineinfo_get(cdev, ip, + cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { return linereq_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { @@ -1316,7 +1429,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, unsigned long action, void *data) { struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); - struct gpioline_info_changed chg; + struct gpio_v2_line_info_changed chg; struct gpio_desc *desc = data; int ret; @@ -1356,12 +1469,16 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, size_t count, loff_t *off) { struct gpio_chardev_data *cdev = file->private_data; - struct gpioline_info_changed event; + struct gpio_v2_line_info_changed event; ssize_t bytes_read = 0; int ret; + size_t event_size; - if (count < sizeof(event)) +#ifndef CONFIG_GPIO_CDEV_V1 + event_size = sizeof(struct gpio_v2_line_info_changed); + if (count < event_size) return -EINVAL; +#endif do { spin_lock(&cdev->wait.lock); @@ -1383,7 +1500,17 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, return ret; } } - +#ifdef CONFIG_GPIO_CDEV_V1 + /* must be after kfifo check so watch_abi_version is set */ + if (atomic_read(&cdev->watch_abi_version) == 2) + event_size = sizeof(struct gpio_v2_line_info_changed); + else + event_size = sizeof(struct gpioline_info_changed); + if (count < event_size) { + spin_unlock(&cdev->wait.lock); + return -EINVAL; + } +#endif ret = kfifo_out(&cdev->events, &event, 1); spin_unlock(&cdev->wait.lock); if (ret != 1) { @@ -1392,9 +1519,23 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, /* We should never get here. See lineevent_read(). */ } - if (copy_to_user(buf + bytes_read, &event, sizeof(event))) +#ifdef CONFIG_GPIO_CDEV_V1 + if (event_size == sizeof(struct gpio_v2_line_info_changed)) { + if (copy_to_user(buf + bytes_read, &event, event_size)) + return -EFAULT; + } else { + struct gpioline_info_changed event_v1; + + gpio_v2_line_info_changed_to_v1(&event, &event_v1); + if (copy_to_user(buf + bytes_read, &event_v1, + event_size)) + return -EFAULT; + } +#else + if (copy_to_user(buf + bytes_read, &event, event_size)) return -EFAULT; - bytes_read += sizeof(event); +#endif + bytes_read += event_size; } while (count >= bytes_read + sizeof(event)); return bytes_read; From patchwork Thu Aug 27 14:00:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352617 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=ZJwyCBf0; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bclgm105Jz9sRK for ; Fri, 28 Aug 2020 00:37:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728007AbgH0OhU (ORCPT ); Thu, 27 Aug 2020 10:37:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728173AbgH0ODq (ORCPT ); Thu, 27 Aug 2020 10:03:46 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF553C0619C1; Thu, 27 Aug 2020 07:02:37 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id u128so3572383pfb.6; Thu, 27 Aug 2020 07:02:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=33e65RQh/g7MpsFkYIyi4LfKsB7iOe3zuOOtjfBxTO4=; b=ZJwyCBf0qNfVftmiIuBbGJBJnsPjI3e7VOJJYIjICDSDtQvgOfV97JLGWlpnvabP+D 3hE+RrmTl79QLesKAwB4Al+epTx63dHCYMexR9T9QP17lvGaPYXEi55j29D3TKUtBdlk HK1/lh2A0/aMzYlLGdK9c4mqb8c9XF+ok4VcqjQ5F3H82XREY3X9OGMlciVwNC4dQxph yWsAQoWxTQ5HMDXJBeaWwmYME/uUJOgsdArn3wMJch17IQ5fb/CX6nFDwh7JVabYAOl5 JnnTFh0BE4yMfmIZ+isO2U0dU2/ovBJkbMV4LncZ5Khbx/nxDWlwbB2OHr//jvaBHyid 0EOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=33e65RQh/g7MpsFkYIyi4LfKsB7iOe3zuOOtjfBxTO4=; b=MGZ9tr0cDPh8n2xmWf7drDEQwqrAmBRcL4GCdadl1AY5LhGtHgdneJrBWOi4Si6vq8 4LeoeFvN4Z7XLJzOE0ImpaI2ngyzXF9DO6zyFaI52oa5ySVfDwyni/T2/pVGODMwi/+T noAccm10wP9Oy1LgJtjavpfm2q9kQJHG6osNuScep7Ww0nZ1v934G4QJy2OZbhcsEpWE rjHlhVkCyq2C0v8PoGPtHN+sXF/stfng8cCT6K4h7GfJwh08VOep87U6dbpHd40Mpqn8 eWbOr0TUbCl88177CyYBhh7Wybt7EMw2CwTD9YNNFmhadKVHBUR9SwXZYKK+rhwLOzzr LWJA== X-Gm-Message-State: AOAM53251xHpmHq4saVdCvG0FHnX0kuYAJKQEv/TnqR2HX+zdwkGeSdI Mx3ZyjoZwxHzOaymeRnMU9CZ47poS+E= X-Google-Smtp-Source: ABdhPJx13ot72nhYAd2dY2kinYj2nEwut5nZHwc/Fvdd+Z8pJTHZQlRQDB3a2WwIJS/TtOGAYA9sQA== X-Received: by 2002:a65:6412:: with SMTP id a18mr14888332pgv.215.1598536956698; Thu, 27 Aug 2020 07:02:36 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.02.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:02:35 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 09/20] gpiolib: cdev: support edge detection for uAPI v2 Date: Thu, 27 Aug 2020 22:00:09 +0800 Message-Id: <20200827140020.159627-10-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for edge detection to lines requested using GPIO_V2_GET_LINE_IOCTL. The edge_detector implementation is based on the v1 lineevent implementation. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 290 +++++++++++++++++++++++++++++++++++- drivers/gpio/gpiolib.c | 2 + drivers/gpio/gpiolib.h | 2 + 3 files changed, 293 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 281255e05323..05b15f20fe2c 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -385,9 +385,32 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) /** * struct line - contains the state of a requested line * @desc: the GPIO descriptor for this line. + * @req: the corresponding line request + * @irq: the interrupt triggered in response to events on this GPIO + * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or + * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied + * @timestamp: cache for the timestamp storing it between hardirq and IRQ + * thread, used to bring the timestamp close to the actual event + * @req_seqno: the seqno for the current edge event in the sequence of + * events for the corresponding line request. This is drawn from the @req. + * @line_seqno: the seqno for the current edge event in the sequence of + * events for this line. */ struct line { struct gpio_desc *desc; + /* + * -- edge detector specific fields -- + */ + struct linereq *req; + unsigned int irq; + u64 eflags; + /* + * timestamp and req_seqno are shared by edge_irq_handler() and + * edge_irq_thread() which are themselves mutually exclusive. + */ + u64 timestamp; + u32 req_seqno; + u32 line_seqno; }; /** @@ -395,15 +418,143 @@ struct line { * @gdev: the GPIO device the line request pertains to * @label: consumer label used to tag GPIO descriptors * @num_lines: the number of lines in the lines array + * @wait: wait queue that handles blocking reads of events + * @events: KFIFO for the GPIO events + * @seqno: the sequence number for edge events generated on all lines in + * this line request. Note that this is not used when @num_lines is 1, as + * the line_seqno is then the same and is cheaper to calculate. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq { struct gpio_device *gdev; const char *label; u32 num_lines; + wait_queue_head_t wait; + DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); + atomic_t seqno; struct line lines[]; }; +static irqreturn_t edge_irq_thread(int irq, void *p) +{ + struct line *line = p; + struct linereq *lr = line->req; + struct gpio_v2_line_event le; + int ret; + + /* Do not leak kernel stack to userspace */ + memset(&le, 0, sizeof(le)); + + /* + * We may be running from a nested threaded interrupt in which case + * we didn't get the timestamp from edge_irq_handler(). + */ + if (!line->timestamp) { + le.timestamp = ktime_get_ns(); + if (lr->num_lines != 1) + line->req_seqno = atomic_inc_return(&lr->seqno); + } else { + le.timestamp = line->timestamp; + } + line->timestamp = 0; + + if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | + GPIO_V2_LINE_FLAG_EDGE_FALLING)) { + int level = gpiod_get_value_cansleep(line->desc); + + if (level) + /* Emit low-to-high event */ + le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; + else + /* Emit high-to-low event */ + le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; + } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) { + /* Emit low-to-high event */ + le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; + } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { + /* Emit high-to-low event */ + le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; + } else { + return IRQ_NONE; + } + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; + le.offset = gpio_chip_hwgpio(line->desc); + + ret = kfifo_in_spinlocked_noirqsave(&lr->events, &le, + 1, &lr->wait.lock); + if (ret) + wake_up_poll(&lr->wait, EPOLLIN); + else + pr_debug_ratelimited("event FIFO is full - event dropped\n"); + + return IRQ_HANDLED; +} + +static irqreturn_t edge_irq_handler(int irq, void *p) +{ + struct line *line = p; + struct linereq *lr = line->req; + + /* + * Just store the timestamp in hardirq context so we get it as + * close in time as possible to the actual event. + */ + line->timestamp = ktime_get_ns(); + + if (lr->num_lines != 1) + line->req_seqno = atomic_inc_return(&lr->seqno); + + return IRQ_WAKE_THREAD; +} + +static int edge_detector_start(struct line *line) +{ + unsigned long irqflags = 0; + int irq, ret; + + irq = gpiod_to_irq(line->desc); + if (irq <= 0) + return -ENODEV; + + line->req_seqno = 0; + line->line_seqno = 0; + + if (line->eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) + irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? + IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; + if (line->eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) + irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? + IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; + irqflags |= IRQF_ONESHOT; + + /* Request a thread to read the events */ + ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, + irqflags, line->req->label, line); + if (ret) + return ret; + + line->irq = irq; + return 0; +} + +static void edge_detector_stop(struct line *line) +{ + if (line->irq) { + free_irq(line->irq, line); + line->irq = 0; + } +} + +static int edge_detector_setup(struct line *line, + struct gpio_v2_line_config *lc) +{ + if (line->eflags) + return edge_detector_start(line); + return 0; +} + #define GPIO_V2_LINE_BIAS_FLAGS \ (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ @@ -417,10 +568,15 @@ struct linereq { (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ GPIO_V2_LINE_FLAG_OPEN_SOURCE) +#define GPIO_V2_LINE_EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) + #define GPIO_V2_LINE_VALID_FLAGS \ (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ GPIO_V2_LINE_DIRECTION_FLAGS | \ GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_EDGE_FLAGS | \ GPIO_V2_LINE_BIAS_FLAGS) static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, @@ -437,6 +593,21 @@ static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, return lc->flags; } +static bool gpio_v2_line_config_has_edge_detection(struct gpio_v2_line_config *lc) +{ + unsigned int i; + + if (lc->flags & GPIO_V2_LINE_EDGE_FLAGS) + return true; + + for (i = 0; i < lc->num_attrs; i--) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && + (lc->attrs[i].attr.flags & GPIO_V2_LINE_EDGE_FLAGS)) + return true; + } + return false; +} + static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, unsigned int line_idx) { @@ -465,6 +636,11 @@ static int gpio_v2_line_flags_validate(u64 flags) (flags & GPIO_V2_LINE_FLAG_OUTPUT)) return -EINVAL; + /* Edge detection requires explicit input. */ + if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && + !(flags & GPIO_V2_LINE_FLAG_INPUT)) + return -EINVAL; + /* * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If * the hardware actually supports enabling both at the same time the @@ -526,6 +702,10 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, set_bit(FLAG_IS_OUT, flagsp); else if (flags & GPIO_V2_LINE_FLAG_INPUT) clear_bit(FLAG_IS_OUT, flagsp); + assign_bit(FLAG_EDGE_RISING, flagsp, + flags & GPIO_V2_LINE_FLAG_EDGE_RISING); + assign_bit(FLAG_EDGE_FALLING, flagsp, + flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); assign_bit(FLAG_OPEN_DRAIN, flagsp, flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); assign_bit(FLAG_OPEN_SOURCE, flagsp, @@ -612,14 +792,85 @@ static long linereq_ioctl_compat(struct file *file, unsigned int cmd, } #endif +static __poll_t linereq_poll(struct file *file, + struct poll_table_struct *wait) +{ + struct linereq *lr = file->private_data; + __poll_t events = 0; + + poll_wait(file, &lr->wait, wait); + + if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, + &lr->wait.lock)) + events = EPOLLIN | EPOLLRDNORM; + + return events; +} + +static ssize_t linereq_read(struct file *file, + char __user *buf, + size_t count, + loff_t *f_ps) +{ + struct linereq *lr = file->private_data; + struct gpio_v2_line_event le; + ssize_t bytes_read = 0; + int ret; + + if (count < sizeof(le)) + return -EINVAL; + + do { + spin_lock(&lr->wait.lock); + if (kfifo_is_empty(&lr->events)) { + if (bytes_read) { + spin_unlock(&lr->wait.lock); + return bytes_read; + } + + if (file->f_flags & O_NONBLOCK) { + spin_unlock(&lr->wait.lock); + return -EAGAIN; + } + + ret = wait_event_interruptible_locked(lr->wait, + !kfifo_is_empty(&lr->events)); + if (ret) { + spin_unlock(&lr->wait.lock); + return ret; + } + } + + ret = kfifo_out(&lr->events, &le, 1); + spin_unlock(&lr->wait.lock); + if (ret != 1) { + /* + * This should never happen - we were holding the + * lock from the moment we learned the fifo is no + * longer empty until now. + */ + ret = -EIO; + break; + } + + if (copy_to_user(buf + bytes_read, &le, sizeof(le))) + return -EFAULT; + bytes_read += sizeof(le); + } while (count >= bytes_read + sizeof(le)); + + return bytes_read; +} + static void linereq_free(struct linereq *lr) { unsigned int i; for (i = 0; i < lr->num_lines; i++) { + edge_detector_stop(&lr->lines[i]); if (lr->lines[i].desc) gpiod_free(lr->lines[i].desc); } + kfifo_free(&lr->events); kfree(lr->label); put_device(&lr->gdev->dev); kfree(lr); @@ -635,6 +886,8 @@ static int linereq_release(struct inode *inode, struct file *file) static const struct file_operations line_fileops = { .release = linereq_release, + .read = linereq_read, + .poll = linereq_poll, .owner = THIS_MODULE, .llseek = noop_llseek, .unlocked_ioctl = linereq_ioctl, @@ -650,7 +903,8 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) struct linereq *lr; struct file *file; unsigned long flags; - unsigned int i; + unsigned int i, size; + bool has_edge_detection; int fd, ret; if (copy_from_user(&ulr, ip, sizeof(ulr))) @@ -667,6 +921,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) if (ret) return ret; + /* event_buffer_size is only valid with edge detection */ + has_edge_detection = gpio_v2_line_config_has_edge_detection(lc); + if (ulr.event_buffer_size && !has_edge_detection) + return -EINVAL; + lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); if (!lr) return -ENOMEM; @@ -674,6 +933,9 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev; get_device(&gdev->dev); + for (i = 0; i < ulr.num_lines; i++) + lr->lines[i].req = lr; + /* Make sure this is terminated */ ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; if (strlen(ulr.consumer)) { @@ -684,6 +946,21 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } + init_waitqueue_head(&lr->wait); + if (has_edge_detection) { + size = ulr.event_buffer_size; + + if (size > GPIO_V2_LINES_MAX*16) + size = GPIO_V2_LINES_MAX*16; + else if (size == 0) + size = ulr.num_lines*16; + + ret = kfifo_alloc(&lr->events, size, GFP_KERNEL); + if (ret) + goto out_free_linereq; + } + + atomic_set(&lr->seqno, 0); lr->num_lines = ulr.num_lines; /* Request each GPIO */ @@ -722,6 +999,10 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) ret = gpiod_direction_input(desc); if (ret) goto out_free_linereq; + lr->lines[i].eflags = flags & GPIO_V2_LINE_EDGE_FLAGS; + ret = edge_detector_setup(&lr->lines[i], lc); + if (ret) + goto out_free_linereq; } blocking_notifier_call_chain(&desc->gdev->notifier, @@ -1243,6 +1524,13 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (test_bit(FLAG_PULL_UP, &desc->flags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; + if (test_bit(FLAG_EDGE_RISING, &desc->flags)) + info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; + if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) + info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; + + info->num_attrs = 0; + spin_unlock_irqrestore(&gpio_lock, flags); } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 80137c1b3cdc..e4c81dca7f8b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2041,6 +2041,8 @@ static bool gpiod_free_commit(struct gpio_desc *desc) clear_bit(FLAG_PULL_UP, &desc->flags); clear_bit(FLAG_PULL_DOWN, &desc->flags); clear_bit(FLAG_BIAS_DISABLE, &desc->flags); + clear_bit(FLAG_EDGE_RISING, &desc->flags); + clear_bit(FLAG_EDGE_FALLING, &desc->flags); clear_bit(FLAG_IS_HOGGED, &desc->flags); #ifdef CONFIG_OF_DYNAMIC desc->hog = NULL; diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 6709f79c02dd..39b356160937 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -114,6 +114,8 @@ struct gpio_desc { #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ +#define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */ +#define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */ /* Connection label */ const char *label; From patchwork Thu Aug 27 14:00:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352613 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=cHJVwTRo; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bclf91c2Vz9sTn for ; Fri, 28 Aug 2020 00:36:17 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728009AbgH0OgN (ORCPT ); Thu, 27 Aug 2020 10:36:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726232AbgH0ODt (ORCPT ); Thu, 27 Aug 2020 10:03:49 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFD96C0619C2; Thu, 27 Aug 2020 07:02:48 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id n3so2681844pjq.1; Thu, 27 Aug 2020 07:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PPWCmf3nG0tkeT+4WJ+YoP1DR3GW9Dr8lBTML579n38=; b=cHJVwTRoFCCQBz5RDYSuiif20EIPI9tKWyxRNdU6F6mWKnH7dnWfOFvEHCPPEUXbLK /KX7fRu78PikvI8yJXnDABEWzO4BaqhwLXHrYyDRxGyH815SXD+h40pKwEGmx/E3fbKl NTW+UhvQnemzu4wp8ThY4dJIGIJCCAyeRPWhOQ4nbhoryMHM/LtHJ+4kS1HIsDCLsSCl 56+RWZB9cs81tQIDPnrY82DpI1DVhSxwjXWK7t9k6ooZdsXH70KUsmlcO1YapJK8CFLr L/XqkPV9QTNF4aU2bUUt1TkTkJS7WiDXuJ90UrEQxCPvPVi6IEAs5iE2YEhNnnrYr49X uMWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PPWCmf3nG0tkeT+4WJ+YoP1DR3GW9Dr8lBTML579n38=; b=h2e1JTgwGtu53+umumOaG0ktRDo/OANeZO9Tl8azL5ek5HBa5/M3k6w4EHGIEj5tEm 0SMHB88NpLr2L8MszY0gVvUgzV2Bd8IH5OXGS1U2VpenJfz8SAumOPRNtWBQVIzhzO6m otU77uz3Kf4n9ixsvYk2slUJSaIhjFLz0Xxh8v6BuFTd+STacu7vXn4/hY1h/KvSCuyW KVemTRbl7HKGFSP/rnmzkvLk0CWVLElPx2i4PcYKRipnWoNOxwkkZ4A2F0OQUf/5Y2gI R2r65typSit6ppw+24hFn5zmDL6zd12t+LdnCY8y7D+LNhhU2Hyt2NAuaqTPRvenBtnS Szqg== X-Gm-Message-State: AOAM533fItlQ+4rwTPoliXxZcIwLTrcFopxbOjkhdGRCg61F4bfrDR/T 2sRaa6+/0q5hkYkYtk/M2UyaIo5Usfg= X-Google-Smtp-Source: ABdhPJzDqs31URNvPEtLcqLk2V1fGmarmB+xwvvzXZNw4Ho1HNXHLNSfK4nKP7Q7kbwy4jF9bx1kBg== X-Received: by 2002:a17:90b:384b:: with SMTP id nl11mr10727536pjb.91.1598536967720; Thu, 27 Aug 2020 07:02:47 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.02.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:02:46 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL Date: Thu, 27 Aug 2020 22:00:10 +0800 Message-Id: <20200827140020.159627-11-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 05b15f20fe2c..9c1e3f5f01af 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -423,6 +424,8 @@ struct line { * @seqno: the sequence number for edge events generated on all lines in * this line request. Note that this is not used when @num_lines is 1, as * the line_seqno is then the same and is cheaper to calculate. + * @config_mutex: mutex for serializing ioctl() calls to ensure consistency + * of configuration, particularly multi-step accesses to desc flags. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq { @@ -432,6 +435,7 @@ struct linereq { wait_queue_head_t wait; DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); atomic_t seqno; + struct mutex config_mutex; struct line lines[]; }; @@ -693,6 +697,29 @@ static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, return 0; } +static int gpio_v2_line_config_change_validate(struct linereq *lr, + struct gpio_v2_line_config *lc) +{ + unsigned int i; + u64 flags; + + for (i = 0; i < lr->num_lines; i++) { + flags = gpio_v2_line_config_flags(lc, i); + /* disallow edge detection changes */ + if (lr->lines[i].eflags != (flags & GPIO_V2_LINE_EDGE_FLAGS)) + return -EINVAL; + + if (lr->lines[i].eflags & GPIO_V2_LINE_EDGE_FLAGS) { + /* disallow polarity changes */ + if (test_bit(FLAG_ACTIVE_LOW, + &lr->lines[i].desc->flags) != + ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)) + return -EINVAL; + } + } + return 0; +} + static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, unsigned long *flagsp) { @@ -772,6 +799,68 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) return 0; } +static long linereq_set_config_unlocked(struct linereq *lr, + struct gpio_v2_line_config *lc) +{ + struct gpio_desc *desc; + unsigned int i; + u64 flags; + int ret; + + ret = gpio_v2_line_config_change_validate(lr, lc); + if (ret) + return ret; + + for (i = 0; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags = gpio_v2_line_config_flags(lc, i); + + gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); + /* + * Lines have to be requested explicitly for input + * or output, else the line will be treated "as is". + */ + if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { + int val = gpio_v2_line_config_output_value(lc, i); + + edge_detector_stop(&lr->lines[i]); + ret = gpiod_direction_output(desc, val); + if (ret) + return ret; + } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { + ret = gpiod_direction_input(desc); + if (ret) + return ret; + } + + blocking_notifier_call_chain(&desc->gdev->notifier, + GPIO_V2_LINE_CHANGED_CONFIG, + desc); + } + return 0; +} + +static long linereq_set_config(struct linereq *lr, void __user *ip) +{ + struct gpio_v2_line_config lc; + int ret; + + if (copy_from_user(&lc, ip, sizeof(lc))) + return -EFAULT; + + ret = gpio_v2_line_config_validate(&lc, lr->num_lines); + if (ret) + return ret; + + mutex_lock(&lr->config_mutex); + + ret = linereq_set_config_unlocked(lr, &lc); + + mutex_unlock(&lr->config_mutex); + + return ret; +} + static long linereq_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { @@ -780,6 +869,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) return linereq_get_values(lr, ip); + else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) + return linereq_set_config(lr, ip); return -EINVAL; } @@ -946,6 +1037,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } + mutex_init(&lr->config_mutex); init_waitqueue_head(&lr->wait); if (has_edge_detection) { size = ulr.event_buffer_size; From patchwork Thu Aug 27 14:00:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352612 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=fKIP1ZdH; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bclf81yj1z9sTm for ; Fri, 28 Aug 2020 00:36:16 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727999AbgH0OgN (ORCPT ); Thu, 27 Aug 2020 10:36:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726296AbgH0ODt (ORCPT ); Thu, 27 Aug 2020 10:03:49 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E2CFC0619C3; Thu, 27 Aug 2020 07:02:58 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id s2so2056233pjr.4; Thu, 27 Aug 2020 07:02:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sW0UkKvySH75oSJX+PFR3W/jRHYtpquC/ixEl6sp2QI=; b=fKIP1ZdHXmNp26ExQxS9YbW5bZnFmC9whdfPXGG9/aebcBxJ/BWbm1E5Sls4nk/60i 42oZMhteIubV/JIgHhdLpGDBBZ9f/DhbDZWoMS1trOxiE3n8GWWX1k5RiPy0klGp8btC pUGwG8GlzkLZNqvfCDi9aqWZF2Z4X4krCFt4ABYdzQFXmFFxNPFB8+Z9/ss64PzjVdOH C/oZA7XblZUae3DHdZ0Rn9RFTefokvjMbmf5Xspdu3+k9VffInFaOzS1/CkIgjXPOttj HYXH+FEUrDTPkHuffCnYY6aM8ZzLzCF/1O6MGyqECoVEmGDft3kwPqTMRFOVCvuhhE1T dObg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sW0UkKvySH75oSJX+PFR3W/jRHYtpquC/ixEl6sp2QI=; b=jHjNwWSEiKN4LozyBxQVZUKSYO+VWHKSwpChXoRcwJMrTHMgkWnhiO8FVM7C7cjN2j x06RHyjowJVBMwKqLtTo4oBR2NogsbKhYARXRuC9FqibmHXE42pA+gBtN65RDo+TNQDu uWMO0U/MI5s2Adq01xkD6XWEQoOHYOCcX1JY6TQUBmcuA3VRxmdWQu6t/EbYcJFBhrNP bAJoD4mNLUj7jg9s2tVC+haBamHc4W8fwiNeMd8BoRgsNt4H6svh5kDhvV0qcnv/yUpk YU1CruQ83RI8aTnSgOq5/I3swkdzv+/27cV3uDV+KIoSC4ipPiQiBzrdy6BsbhJayile Cnkw== X-Gm-Message-State: AOAM531yCF7xaX8yM4576+weqAtYEIhxXGhsdvwB5aHVYKvUPmO+XdOR 2F8XgdAPIfUt58CRzrOjoqjGvOTz96w= X-Google-Smtp-Source: ABdhPJy9y+9ovRDPtjgT2GDLbGBWJ6ur77ZSM77JQSQV0R09nkpSrWDh0bcTTA5+3uLG/lfD1ofyIg== X-Received: by 2002:a17:90a:d3d4:: with SMTP id d20mr10496642pjw.111.1598536977465; Thu, 27 Aug 2020 07:02:57 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.02.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:02:56 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL Date: Thu, 27 Aug 2020 22:00:11 +0800 Message-Id: <20200827140020.159627-12-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for the GPIO_V2_LINE_SET_VALUES_IOCTL. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 9c1e3f5f01af..3d2d9eefdfa0 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -799,6 +799,63 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) return 0; } +static long linereq_set_values_unlocked(struct linereq *lr, + struct gpio_v2_line_values *lv) +{ + DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_desc **descs; + unsigned int i, didx, num_set; + int ret; + + bitmap_zero(vals, GPIO_V2_LINES_MAX); + for (num_set = 0, i = 0; i < lr->num_lines; i++) { + if (lv->mask & BIT_ULL(i)) { + if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) + return -EPERM; + if (lv->bits & BIT_ULL(i)) + __set_bit(num_set, vals); + num_set++; + descs = &lr->lines[i].desc; + } + } + if (num_set == 0) + return -EINVAL; + + if (num_set != 1) { + /* build compacted desc array and values */ + descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); + for (didx = 0, i = 0; i < lr->num_lines; i++) { + if (lv->mask & BIT_ULL(i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret = gpiod_set_array_value_complex(false, true, num_set, + descs, NULL, vals); + + if (num_set != 1) + kfree(descs); + return ret; +} + +static long linereq_set_values(struct linereq *lr, void __user *ip) +{ + struct gpio_v2_line_values lv; + int ret; + + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + mutex_lock(&lr->config_mutex); + + ret = linereq_set_values_unlocked(lr, &lv); + + mutex_unlock(&lr->config_mutex); + + return ret; +} + static long linereq_set_config_unlocked(struct linereq *lr, struct gpio_v2_line_config *lc) { @@ -869,6 +926,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) return linereq_get_values(lr, ip); + else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) + return linereq_set_values(lr, ip); else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) return linereq_set_config(lr, ip); From patchwork Thu Aug 27 14:00:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352611 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=sBiftOV2; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bclf44x3Jz9sSJ for ; Fri, 28 Aug 2020 00:36:12 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727036AbgH0OgG (ORCPT ); Thu, 27 Aug 2020 10:36:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726307AbgH0ODz (ORCPT ); Thu, 27 Aug 2020 10:03:55 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3455FC0619C4; Thu, 27 Aug 2020 07:03:07 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id p11so3573939pfn.11; Thu, 27 Aug 2020 07:03:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S4Xa3BFRPcqJPxsgjYTiIijreo6sqSK/Q3rnsnWUnmQ=; b=sBiftOV25rx2mzW7BJ7GcHH4Id6f/80yqStCtZ4TGfBMYgga7QKahHbKnKXMuEjqgP pousMFbUHf8JaO0ASl0skFN/749+jQ7LYo5GjvioBl22VCD0ogks9z6IM4yI2QLYoRjL 9x9VYlff710t07VYUrTk5FIHEjGF6WUAFeASEcfPzO1Aa1AvAR/ar2iQi3D8A3pqVZgV 4bDZWrEpmPcvkQhcf/7jnx9eu/1KK7nkA4BayIbB16khLvTEWQldX6VdUee1m/3SXxXD YNzAAUUxRnQL9WSoB+jJ8AhGWTjQOaRgjQBKJiZsTBE/GG3jiz5BlSvPbAphphw4hCAB Gc2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S4Xa3BFRPcqJPxsgjYTiIijreo6sqSK/Q3rnsnWUnmQ=; b=Af4zJ6T7uqEm2DxE4Zw3qUzwBwcwbFM8wAmhXwwgP+n+MMJSicm6L0j6mnUpm78/xv YtM+C8rwJYweauFjXpogH/5PaJnL1WtuaR9vKAz42nmjIIb1Xya85YuUZkP/zJ2h+2+1 xbl3J4RF0B73hXvgNleIFcCW85MnbtInal+zDGs5AnziDwsCmrb1Knzx0ZbC5A9MLX1i PtOxcrGlY0Xo+tjBNPJvgvKiNCggzhVGb7BOJJS4xSbuuVdldDX1HbuQBHlahSfZC2bJ 8XZ/R5voFhiQ1aCV56QIDP9p0hQBrEc/tf2u/8qMNku4tdxxuaItPlNxEC3m5iWANBeb oUIg== X-Gm-Message-State: AOAM5302IouaDp5KbpfAB5ev6XIDguHXXpVemZbKR2I//CZtgZGp2W8l b2beadCSTUojYjE3IbDOFZGrVAMBIbg= X-Google-Smtp-Source: ABdhPJzxT3Ymw6R1fm68ASqrONuanLsmJFCrzR3UoUSZynOC3PBBUk5H6OtwUpPjxiT3sncVboUlBg== X-Received: by 2002:aa7:95ad:: with SMTP id a13mr8962195pfk.136.1598536985693; Thu, 27 Aug 2020 07:03:05 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.03.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:03:05 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 12/20] gpiolib: cdev: support setting debounce Date: Thu, 27 Aug 2020 22:00:12 +0800 Message-Id: <20200827140020.159627-13-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for setting debounce on a line via the GPIO uAPI. Where debounce is not supported by hardware, a software debounce is provided. The implementation of the software debouncer waits for the line to be stable for the debounce period before determining if a level change, and a corresponding edge event, has occurred. This provides maximum protection against glitches, but also introduces a debounce_period latency to edge events. The software debouncer is integrated with the edge detection as it utilises the line interrupt, and integration is simpler than getting the two to interwork. Where software debounce AND edge detection is required, the debouncer provides both. Due to the tight integration between the debouncer and edge detection, and to avoid particular corner cases, it is not allowed to alter the debounce value if edge detection is enabled. Changing the debounce with edge detection enabled is a very unlikely use case, so it is preferable to disallow it rather than complicate the code to allow it. Should the user wish to alter the debounce value in such cases they will need to release and re-request the line. Signed-off-by: Kent Gibson --- Changes for v5: - as per cover letter Changes for v4: - fix handling of mask in line_get_values Changes for v3: - only GPIO_V2 field renaming Changes for v2: - improve documentation on fields shared by threads. - use READ_ONCE/WRITE_ONCE for shared fields rather than atomic_t which was overkill. drivers/gpio/gpiolib-cdev.c | 254 +++++++++++++++++++++++++++++++++++- drivers/gpio/gpiolib.h | 4 + 2 files changed, 252 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 3d2d9eefdfa0..b93479a98637 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ #include #include #include +#include #include #include "gpiolib.h" @@ -396,6 +398,9 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) * events for the corresponding line request. This is drawn from the @req. * @line_seqno: the seqno for the current edge event in the sequence of * events for this line. + * @work: the worker that implements software debouncing + * @sw_debounced: flag indicating if the software debouncer is active + * @level: the current debounced physical level of the line */ struct line { struct gpio_desc *desc; @@ -411,7 +416,27 @@ struct line { */ u64 timestamp; u32 req_seqno; + /* + * line_seqno is used by either edge_irq_thread() or + * debounce_work_func() which are themselves mutually exclusive. + */ u32 line_seqno; + /* + * -- debouncer specific fields -- + */ + struct delayed_work work; + /* + * sw_debounce is shared by linereq_set_config(), which is the only + * setter, and linereq_get_values(), which can live with a slightly + * stale value. + */ + unsigned int sw_debounced; + /* + * level is shared by debounce_work_func(), which is the only + * setter, and linereq_get_values() which can live with a slightly + * stale value. + */ + unsigned int level; }; /** @@ -518,6 +543,10 @@ static int edge_detector_start(struct line *line) unsigned long irqflags = 0; int irq, ret; + if (READ_ONCE(line->sw_debounced)) + /* debouncer is setup and will provide edge detection */ + return 0; + irq = gpiod_to_irq(line->desc); if (irq <= 0) return -ENODEV; @@ -543,17 +572,204 @@ static int edge_detector_start(struct line *line) return 0; } +/* + * returns the current debounced logical value. + */ +static unsigned int debounced_value(struct line *line) +{ + unsigned int value; + + /* + * minor race - debouncer may be stopped here, so edge_detector_stop + * must leave the value unchanged so the following will read the level + * from when the debouncer was last running. + */ + value = READ_ONCE(line->level); + + if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) + value = !value; + + return value; +} + +static irqreturn_t debounce_irq_handler(int irq, void *p) +{ + struct line *line = p; + + mod_delayed_work(system_wq, &line->work, + usecs_to_jiffies(READ_ONCE(line->desc->debounce_period))); + + return IRQ_HANDLED; +} + +static void debounce_work_func(struct work_struct *work) +{ + struct gpio_v2_line_event le; + struct line *line = container_of(work, struct line, work.work); + struct linereq *lr; + int ret, level; + + level = gpiod_get_raw_value_cansleep(line->desc); + if (level < 0) { + pr_debug_ratelimited("debouncer failed to read line value\n"); + return; + } + + if (READ_ONCE(line->level) == level) + return; + + WRITE_ONCE(line->level, level); + + /* -- edge detection -- */ + if (!line->eflags) + return; + + /* switch from physical level to logical - if they differ */ + if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) + level = !level; + + /* ignore edges that are not being monitored */ + if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || + ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) + return; + + /* Do not leak kernel stack to userspace */ + memset(&le, 0, sizeof(le)); + + lr = line->req; + le.timestamp = ktime_get_ns(); + le.offset = gpio_chip_hwgpio(line->desc); + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines == 1) ? + le.line_seqno : atomic_inc_return(&lr->seqno); + + if (level) + /* Emit low-to-high event */ + le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; + else + /* Emit high-to-low event */ + le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; + + ret = kfifo_in_spinlocked_noirqsave(&lr->events, &le, + 1, &lr->wait.lock); + if (ret) + wake_up_poll(&lr->wait, EPOLLIN); + else + pr_debug_ratelimited("event FIFO is full - event dropped\n"); +} + +static int debounce_setup(struct line *line, + unsigned int debounce_period) +{ + unsigned long irqflags; + int ret, level, irq; + + /* try hardware */ + ret = gpiod_set_debounce(line->desc, debounce_period); + if (!ret) { + WRITE_ONCE(line->desc->debounce_period, debounce_period); + return ret; + } + if (ret != -ENOTSUPP) + return ret; + + if (debounce_period) { + /* setup software debounce */ + level = gpiod_get_raw_value_cansleep(line->desc); + if (level < 0) + return level; + + irq = gpiod_to_irq(line->desc); + if (irq <= 0) + return -ENODEV; + + WRITE_ONCE(line->level, level); + line->line_seqno = 0; + irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; + ret = request_irq(irq, debounce_irq_handler, irqflags, + line->req->label, line); + if (ret) + return ret; + + WRITE_ONCE(line->sw_debounced, 1); + line->irq = irq; + } + WRITE_ONCE(line->desc->debounce_period, debounce_period); + return 0; +} + static void edge_detector_stop(struct line *line) { if (line->irq) { free_irq(line->irq, line); line->irq = 0; } + + cancel_delayed_work_sync(&line->work); + WRITE_ONCE(line->sw_debounced, 0); + /* do not change line->level - see comment in debounced_value */ + + if (line->desc) + WRITE_ONCE(line->desc->debounce_period, 0); +} + +static int debounce_update(struct line *line, + unsigned int debounce_period) +{ + if (READ_ONCE(line->desc->debounce_period) == debounce_period) + return 0; + + if (!READ_ONCE(line->sw_debounced)) + return debounce_setup(line, debounce_period); + + if (!debounce_period) + edge_detector_stop(line); + else + WRITE_ONCE(line->desc->debounce_period, debounce_period); + return 0; +} + +static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, + unsigned int line_idx) +{ + unsigned int i; + u64 mask = BIT_ULL(line_idx); + + for (i = 0; i < lc->num_attrs; i++) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && + (lc->attrs[i].mask & mask)) + return true; + } + return false; +} + +static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, + unsigned int line_idx) +{ + unsigned int i; + u64 mask = BIT_ULL(line_idx); + + for (i = 0; i < lc->num_attrs; i++) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && + (lc->attrs[i].mask & mask)) + return lc->attrs[i].attr.debounce_period; + } + return 0; } static int edge_detector_setup(struct line *line, - struct gpio_v2_line_config *lc) + struct gpio_v2_line_config *lc, + unsigned int line_idx) { + int ret; + + if (gpio_v2_line_config_debounced(lc, line_idx)) { + ret = debounce_setup(line, + gpio_v2_line_config_debounce_period(lc, line_idx)); + if (ret) + return ret; + } if (line->eflags) return edge_detector_start(line); return 0; @@ -693,6 +909,11 @@ static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, ret = gpio_v2_line_flags_validate(flags); if (ret) return ret; + + /* debounce requires explicit input */ + if (gpio_v2_line_config_debounced(lc, i) && + !(flags & GPIO_V2_LINE_FLAG_INPUT)) + return -EINVAL; } return 0; } @@ -750,7 +971,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) struct gpio_v2_line_values lv; DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); struct gpio_desc **descs; - unsigned int i, didx, num_get; + unsigned int i, val, didx, num_get; int ret; /* NOTE: It's ok to read values of output lines. */ @@ -787,7 +1008,11 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) lv.bits = 0; for (didx = 0, i = 0; i < lr->num_lines; i++) { if (lv.mask & BIT_ULL(i)) { - if (test_bit(didx, vals)) + if (lr->lines[i].sw_debounced) + val = debounced_value(&lr->lines[i]); + else + val = test_bit(didx, vals); + if (val) lv.bits |= BIT_ULL(i); didx++; } @@ -888,6 +1113,12 @@ static long linereq_set_config_unlocked(struct linereq *lr, ret = gpiod_direction_input(desc); if (ret) return ret; + if (gpio_v2_line_config_debounced(lc, i)) { + ret = debounce_update(&lr->lines[i], + gpio_v2_line_config_debounce_period(lc, i)); + if (ret) + return ret; + } } blocking_notifier_call_chain(&desc->gdev->notifier, @@ -1083,8 +1314,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev; get_device(&gdev->dev); - for (i = 0; i < ulr.num_lines; i++) + for (i = 0; i < ulr.num_lines; i++) { lr->lines[i].req = lr; + WRITE_ONCE(lr->lines[i].sw_debounced, 0); + INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); + } /* Make sure this is terminated */ ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; @@ -1151,7 +1385,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) if (ret) goto out_free_linereq; lr->lines[i].eflags = flags & GPIO_V2_LINE_EDGE_FLAGS; - ret = edge_detector_setup(&lr->lines[i], lc); + ret = edge_detector_setup(&lr->lines[i], lc, i); if (ret) goto out_free_linereq; } @@ -1621,6 +1855,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_chip *gc = desc->gdev->chip; bool ok_for_pinctrl; unsigned long flags; + u32 debounce_period; + unsigned int num_attrs = 0; memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); @@ -1680,7 +1916,13 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; - info->num_attrs = 0; + debounce_period = READ_ONCE(desc->debounce_period); + if (debounce_period) { + info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; + info->attrs[num_attrs].debounce_period = debounce_period; + num_attrs++; + } + info->num_attrs = num_attrs; spin_unlock_irqrestore(&gpio_lock, flags); } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 39b356160937..671805a79a15 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -124,6 +124,10 @@ struct gpio_desc { #ifdef CONFIG_OF_DYNAMIC struct device_node *hog; #endif +#ifdef CONFIG_GPIO_CDEV + /* debounce period in microseconds */ + unsigned int debounce_period; +#endif }; int gpiod_request(struct gpio_desc *desc, const char *label); From patchwork Thu Aug 27 14:00:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352610 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=cibGEd7w; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BcldQ4QGZz9sSP for ; Fri, 28 Aug 2020 00:35:38 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727048AbgH0Ofc (ORCPT ); Thu, 27 Aug 2020 10:35:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726876AbgH0OEA (ORCPT ); Thu, 27 Aug 2020 10:04:00 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3244C0619C5; Thu, 27 Aug 2020 07:03:20 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id nv17so2678856pjb.3; Thu, 27 Aug 2020 07:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9daGc+bQBQhtyKnIGABCoe3Sraaytqyp+0NjDqFhrAU=; b=cibGEd7w5SHuwMpQ4ocj9kfknHVCpQJFx3E9Jm2dd0Tx69gKU/Y5BwFkqGVYmnlSH1 FytcLBwxkj6iwfL3mFdpFbWT56D4XPASSSCnIwR2FZZttcQdSCROg+u8pN7uJNu2dHFW py8QNASLNc5OkgpSLijB3zjfEl71Sh0wFoT1lVZTyosx7wqiyuiwe+nuVe1Psgf1q97K SXwQJDK73PcoX262TA44usY6IZQQwxFMu7bK31nbbBBVM18hAL+2xWo0uW9d4Fq83XJj q0vlbx64K1mVv5tENaW1qGzzXa3iS1qvZFvKH2N5yeYkj3H5NmoEXgqyz5xRx2F2nndz jQOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9daGc+bQBQhtyKnIGABCoe3Sraaytqyp+0NjDqFhrAU=; b=t796xuDLjNpM4HBlPhWNvBL0S4uosNwPPgiasFwz0vClrybMwU9Kbnc74imxsKksh6 shxpkskVECQH7+C2FfAxjmIoWaanFDL3QSh1X5gvUcnkA4Nz8aZ30RkoVTY8scluH6rs 1baELYthgVdqV7ewGu2Q+1bfcLTVeGb4p9CetAER/eZqfJv54xYAb4HxsagKcFgHJjoQ 1/MbTcH7hPTtZ2CFcUviCUmwr81eZCrsTvRl38xzGCOA9BDH5xITapgssciiKo3ttwla +DHPyz853AirMYzLfdgjddXnKPX8cguTg24m7v+j4DzST/m3lZcvTyM22FVwzYOCt6IR fpkw== X-Gm-Message-State: AOAM531pOtTwb5k5/Q+g31AjtU/O3R9UdpodAYeiHQ05gYnbQRL+SMFO uYmrOpomehZ/2OdqtRoW0akv277bBHk= X-Google-Smtp-Source: ABdhPJxl/w5AoKJLugN5UJh8/H6o9mfA7L4cPn4EUStogfziG9oiSAzPrWj9J5IvkZfGtXpfkzhUug== X-Received: by 2002:a17:90a:e292:: with SMTP id d18mr9118171pjz.96.1598536999661; Thu, 27 Aug 2020 07:03:19 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:03:18 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 13/20] gpio: uapi: document uAPI v1 as deprecated Date: Thu, 27 Aug 2020 22:00:13 +0800 Message-Id: <20200827140020.159627-14-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Update uAPI documentation to deprecate v1 structs and ioctls. Signed-off-by: Kent Gibson --- include/uapi/linux/gpio.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 0eb1f53b47e0..4af67415d73e 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -274,6 +274,9 @@ struct gpio_v2_line_event { /* * ABI v1 + * + * This version of the ABI is deprecated and will be removed in the future. + * Use the latest version of the ABI, defined above, instead. */ /* Informational flags */ @@ -297,6 +300,9 @@ struct gpio_v2_line_event { * @consumer: a functional name for the consumer of this GPIO line as set by * whatever is using it, will be empty if there is no current user but may * also be empty if the consumer doesn't set this up + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_info instead. */ struct gpioline_info { __u32 line_offset; @@ -328,6 +334,9 @@ enum { * guarantee there are no implicit holes between it and subsequent members. * The 20-byte padding at the end makes sure we don't add any implicit padding * at the end of the structure on 64-bit architectures. + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_info_changed instead. */ struct gpioline_info_changed { struct gpioline_info info; @@ -367,6 +376,9 @@ struct gpioline_info_changed { * @fd: if successful this field will contain a valid anonymous file handle * after a GPIO_GET_LINEHANDLE_IOCTL operation, zero or negative value * means error + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_request instead. */ struct gpiohandle_request { __u32 lineoffsets[GPIOHANDLES_MAX]; @@ -386,6 +398,9 @@ struct gpiohandle_request { * this specifies the default output value, should be 0 (low) or * 1 (high), anything else than 0 or 1 will be interpreted as 1 (high) * @padding: reserved for future use and should be zero filled + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_config instead. */ struct gpiohandle_config { __u32 flags; @@ -398,6 +413,9 @@ struct gpiohandle_config { * @values: when getting the state of lines this contains the current * state of a line, when setting the state of lines these should contain * the desired target state + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_values instead. */ struct gpiohandle_data { __u8 values[GPIOHANDLES_MAX]; @@ -421,6 +439,9 @@ struct gpiohandle_data { * @fd: if successful this field will contain a valid anonymous file handle * after a GPIO_GET_LINEEVENT_IOCTL operation, zero or negative value * means error + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_request instead. */ struct gpioevent_request { __u32 lineoffset; @@ -440,6 +461,9 @@ struct gpioevent_request { * struct gpioevent_data - The actual event being pushed to userspace * @timestamp: best estimate of time of event occurrence, in nanoseconds * @id: event identifier + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_event instead. */ struct gpioevent_data { __u64 timestamp; @@ -464,6 +488,8 @@ struct gpioevent_data { /* * v1 ioctl()s + * + * These ioctl()s are deprecated. Use the v2 equivalent instead. */ #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info) #define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request) From patchwork Thu Aug 27 14:00:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352609 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=pskKla1k; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BcldP6Jjjz9sSJ for ; Fri, 28 Aug 2020 00:35:37 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726243AbgH0Ofa (ORCPT ); Thu, 27 Aug 2020 10:35:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726878AbgH0OEA (ORCPT ); Thu, 27 Aug 2020 10:04:00 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06D05C0619C6; Thu, 27 Aug 2020 07:03:30 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id u20so3591930pfn.0; Thu, 27 Aug 2020 07:03:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ADP2aGUDRbAJ2fzGJ4u2MgmoEkak0G6DX7jqH1kdmdo=; b=pskKla1kOyTKpAXy0EvUaMGChf4lXWttIW4Ki8kiHdDF3GuvOKv5yHvwTVQ/kTwiOG wEHq2OtPfKz1Q5p9IbYEIcJxrS1oH5LpAQ1PPjz+9jHDvXIJbKlPrPEjF+DNtlA6smww hN7j4Vz+ZTK1xG4GhRibUGUXmdFKRrcdh1Idv3YHyzDU3/PBgdo7wCw7dp0gsi0cDVRC ISeiI9BULQfftcuJizPk/96hIBV6YBwSsAAQmDRm37Q7o4kiLnV99/FI2QL1T8FhkVwp K7bufUUGHUiK/18jl3B9UhemG5wQOkzHl95rdCOAjxdfXh7pb/zFL+/6dsVfrM55EqfW jy9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ADP2aGUDRbAJ2fzGJ4u2MgmoEkak0G6DX7jqH1kdmdo=; b=OXq8Ncme4GdzvixYHAkjV5OEZTUm1dPtnGrd3YpMfpon0YbuDqEHyU8yR9Jm3qU084 Gl23G0HC07OpkxKOMhTiB+NVS7bF14hb5WL5duRUAqReCOzQQ4/wMzRCJTWUC/OzMpW1 Mm2TS80TjWVxsDY3Sh6OJeyeYcPaItRs2Ky953Lhg8lBftzTkHJvGLGi0HnDMove/6c8 729zQJklJ08wmMpOTDrgDMbWKCtNNtcqmaKaz36IcN7f8eq4Enw6U0Fx4zcWYyVbqHWw /keyJtdVT26XR/1RCNR0qOGuuOzbsf3/MJ8SqUZu5e/kGrggSNNS1tlNPmFm+CbulEFh f5Rg== X-Gm-Message-State: AOAM530aIPfnXtg1HOuSSEPQsC1qlApxOo3Ng4t2iHBPJVgU5yp+se1S 53xnS+hD3cz654lTjcl5ihF2LNZiRJg= X-Google-Smtp-Source: ABdhPJyd95QYOnXT2VOz7fgHmCWIqzI9VrAx9XEgx8YJFP8cGJeCWM2UsLlEUUZzCv5IK2/QEWy5KQ== X-Received: by 2002:a17:902:b497:: with SMTP id y23mr15864709plr.251.1598537009145; Thu, 27 Aug 2020 07:03:29 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.03.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:03:28 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 14/20] tools: gpio: port lsgpio to v2 uAPI Date: Thu, 27 Aug 2020 22:00:14 +0800 Message-Id: <20200827140020.159627-15-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Port the lsgpio tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson --- tools/gpio/lsgpio.c | 60 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/tools/gpio/lsgpio.c b/tools/gpio/lsgpio.c index b08d7a5e779b..deda38244026 100644 --- a/tools/gpio/lsgpio.c +++ b/tools/gpio/lsgpio.c @@ -25,57 +25,73 @@ struct gpio_flag { char *name; - unsigned long mask; + unsigned long long mask; }; struct gpio_flag flagnames[] = { { - .name = "kernel", - .mask = GPIOLINE_FLAG_KERNEL, + .name = "used", + .mask = GPIO_V2_LINE_FLAG_USED, + }, + { + .name = "input", + .mask = GPIO_V2_LINE_FLAG_INPUT, }, { .name = "output", - .mask = GPIOLINE_FLAG_IS_OUT, + .mask = GPIO_V2_LINE_FLAG_OUTPUT, }, { .name = "active-low", - .mask = GPIOLINE_FLAG_ACTIVE_LOW, + .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW, }, { .name = "open-drain", - .mask = GPIOLINE_FLAG_OPEN_DRAIN, + .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN, }, { .name = "open-source", - .mask = GPIOLINE_FLAG_OPEN_SOURCE, + .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE, }, { .name = "pull-up", - .mask = GPIOLINE_FLAG_BIAS_PULL_UP, + .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP, }, { .name = "pull-down", - .mask = GPIOLINE_FLAG_BIAS_PULL_DOWN, + .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN, }, { .name = "bias-disabled", - .mask = GPIOLINE_FLAG_BIAS_DISABLE, + .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED, }, }; -void print_flags(unsigned long flags) +static void print_attributes(struct gpio_v2_line_info *info) { int i; - int printed = 0; + const char *field_format = "%s"; for (i = 0; i < ARRAY_SIZE(flagnames); i++) { - if (flags & flagnames[i].mask) { - if (printed) - fprintf(stdout, " "); - fprintf(stdout, "%s", flagnames[i].name); - printed++; + if (info->flags & flagnames[i].mask) { + fprintf(stdout, field_format, flagnames[i].name); + field_format = ", %s"; } } + + if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) && + (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)) + fprintf(stdout, field_format, "both-edges"); + else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) + fprintf(stdout, field_format, "rising-edge"); + else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING) + fprintf(stdout, field_format, "falling-edge"); + + for (i = 0; i < info->num_attrs; i++) { + if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) + fprintf(stdout, ", debounce_period=%dusec", + info->attrs[0].debounce_period); + } } int list_device(const char *device_name) @@ -109,18 +125,18 @@ int list_device(const char *device_name) /* Loop over the lines and print info */ for (i = 0; i < cinfo.lines; i++) { - struct gpioline_info linfo; + struct gpio_v2_line_info linfo; memset(&linfo, 0, sizeof(linfo)); - linfo.line_offset = i; + linfo.offset = i; - ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo); + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo); if (ret == -1) { ret = -errno; perror("Failed to issue LINEINFO IOCTL\n"); goto exit_close_error; } - fprintf(stdout, "\tline %2d:", linfo.line_offset); + fprintf(stdout, "\tline %2d:", linfo.offset); if (linfo.name[0]) fprintf(stdout, " \"%s\"", linfo.name); else @@ -131,7 +147,7 @@ int list_device(const char *device_name) fprintf(stdout, " unused"); if (linfo.flags) { fprintf(stdout, " ["); - print_flags(linfo.flags); + print_attributes(&linfo); fprintf(stdout, "]"); } fprintf(stdout, "\n"); From patchwork Thu Aug 27 14:00:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352586 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=VoR6tt84; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bckym1WDlz9sRK for ; Fri, 28 Aug 2020 00:05:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727909AbgH0OF0 (ORCPT ); Thu, 27 Aug 2020 10:05:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727015AbgH0OEV (ORCPT ); Thu, 27 Aug 2020 10:04:21 -0400 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 A65E3C0619C7; Thu, 27 Aug 2020 07:03:41 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id g29so2422615pgl.2; Thu, 27 Aug 2020 07:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LIt8SU895oGklP+2kcXOf7XSm/B7RkLFz7bfNOknIlU=; b=VoR6tt84zLXjyu1g76/nMxTcuqWYBpv1/t4SevALg59qHWTSskUXgUXFLsg9CNGGse +V9BBRagon9lXBR9Q7w/nzSnXXPxfr2IcCvlwyt97rZVhxOU+jwP6i4qATzheSTSy3G7 PtDq4FfBh7c1OOgGPMiaubiw54m0kfWWBV3lnkGIsvFI4i99+Q0eI7vhyuQ0QXmIDMEC F+QiTsyf9NSb7zkLBxsbdrpgKeijNcTkElHOPUdrOn6ycJO59b8QDdQzSfkfeSxXVBtx gXGrHXkr51/ijBiptBS6ezjgPj56om6D6fwgP5AD8ydt3DFgjHuzNN8aevwhJ5H6YNAt KrXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LIt8SU895oGklP+2kcXOf7XSm/B7RkLFz7bfNOknIlU=; b=fXy4lrGWoAaZp+/I36I60aeRPBNLztEspvQpG430L7aZZj3xmmn3uxm7ovSEaHjoZ9 t+NvMEC9cehJzP/VlNv4/RdSpvf9T9YQS9+HRZv/hWK457PzEgYSpmelDZdiQPPNgcuD KCiMkXYDKqM7TuaMvnbT/3xFLvkaD4Xyl56sTidvk4VQhDh3YKUlFg4VI93ScuZqW1jl DIWuD3amaShlO8QZrYnvfF8mGRSmtSb9wo4tB/BxGtPal5ojAWBBkkSRnZmSNwAwm6bO TLn0cQbaJRQAk99xT0BYF3ENRTxtbLc9PUSgjw2SbobmRfp6OzQUB1hoxa0vux2SWO4G TV9w== X-Gm-Message-State: AOAM533HQOKzhTE463cx83Vg9mLjFd09CRWtr2VmIbmvIZ080rn2t8ih p9QExXjL41BE6AAHIYyyHfNdJsEvyV4= X-Google-Smtp-Source: ABdhPJyJ/ZgY7b2VbYL/BPBj3JPSkbnWBQ8Z9sDUnv9kTvMszM5VRKCaJ1X5lNlS6vUENRLqRPBbig== X-Received: by 2002:a17:902:c284:: with SMTP id i4mr16226956pld.287.1598537020709; Thu, 27 Aug 2020 07:03:40 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.03.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:03:40 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 15/20] tools: gpio: port gpio-watch to v2 uAPI Date: Thu, 27 Aug 2020 22:00:15 +0800 Message-Id: <20200827140020.159627-16-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Port the gpio-watch tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson --- tools/gpio/gpio-watch.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/gpio/gpio-watch.c b/tools/gpio/gpio-watch.c index 5cea24fddfa7..6f048350a27e 100644 --- a/tools/gpio/gpio-watch.c +++ b/tools/gpio/gpio-watch.c @@ -21,8 +21,8 @@ int main(int argc, char **argv) { - struct gpioline_info_changed chg; - struct gpioline_info req; + struct gpio_v2_line_info_changed chg; + struct gpio_v2_line_info req; struct pollfd pfd; int fd, i, j, ret; char *event, *end; @@ -40,11 +40,11 @@ int main(int argc, char **argv) for (i = 0, j = 2; i < argc - 2; i++, j++) { memset(&req, 0, sizeof(req)); - req.line_offset = strtoul(argv[j], &end, 0); + req.offset = strtoul(argv[j], &end, 0); if (*end != '\0') goto err_usage; - ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req); + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req); if (ret) { perror("unable to set up line watch"); return EXIT_FAILURE; @@ -71,13 +71,13 @@ int main(int argc, char **argv) } switch (chg.event_type) { - case GPIOLINE_CHANGED_REQUESTED: + case GPIO_V2_LINE_CHANGED_REQUESTED: event = "requested"; break; - case GPIOLINE_CHANGED_RELEASED: + case GPIO_V2_LINE_CHANGED_RELEASED: event = "released"; break; - case GPIOLINE_CHANGED_CONFIG: + case GPIO_V2_LINE_CHANGED_CONFIG: event = "config changed"; break; default: @@ -87,7 +87,7 @@ int main(int argc, char **argv) } printf("line %u: %s at %llu\n", - chg.info.line_offset, event, chg.timestamp); + chg.info.offset, event, chg.timestamp); } } From patchwork Thu Aug 27 14:00:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352587 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=P7GDRiRW; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bckyw6p1pz9sRK for ; Fri, 28 Aug 2020 00:05:44 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727906AbgH0OFb (ORCPT ); Thu, 27 Aug 2020 10:05:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727046AbgH0OEV (ORCPT ); Thu, 27 Aug 2020 10:04:21 -0400 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 5FDACC061235; Thu, 27 Aug 2020 07:03:54 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id w186so3434475pgb.8; Thu, 27 Aug 2020 07:03:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=b4qnERya2lAXwivmnc6QYVo0ihib6LVkWcetGyot3Rg=; b=P7GDRiRWVTmnSNbfwIKt0W/sJZFwCI4kQKF3mFhoM/7UXvvszItARCfCIuVkG19Uvn y9q+nyrYk6/o4U78SNTqytOD1yCJtjmGvSeiRxkEtQ+KHH8fAW7+tDnEoMLMk19uKdl4 W6YcqVgETVn/YhUUHIoWOy6+ErhoCK8rIGkhYJvVd/9J7iNJEn7DQ6sv5IHJYClvFFXL Rcc5YllJU8KQXqPb4DR7bNnKFxt2dtfYhSi6KD4gewbTKDsvkt8DCUWf+en2uAmpu54T y19Pn1mSWqIPeI/KA+0thYpuf5F+++tYiLhSpMGJe0rozyh6DBvFiJsNcTv+ZJg5Jd6L JCxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b4qnERya2lAXwivmnc6QYVo0ihib6LVkWcetGyot3Rg=; b=iJMIlAuZHfH9rN/prBBS8zxVLrnLbzp9Ii6LaxYwBEbYE80YG0bIDkGkC7SaG4v3UQ 355pxMvaQ3AKYhJWKjhmx/FbukfBzfYOhbm3rBmd5Usrn+D8ppUWBYpYnjfm4acr58JO +URfIt5/ZxQWrufUzvm9zuY3Oc++nCC6H4Cc6aJCPwszQ7CiSEXBzfBWY6oF+vWDkm9i ZLFkN8uUjukTUx6Re0reyF02/11Y4WvsIzNzIsK90r1FAta8Z+hROFZZPw3IrYmH/nWu eVAlFdoDQ5kb1xkHLIoBb+hNn+5UanYDejhA/yYCmExYWeMN8Sqy7IjctRE1maeoQKGD 9WCQ== X-Gm-Message-State: AOAM533g57a48+qL40lSV8GGUUEepSeoB3FKd6PSC/t4mmUo2J2EV6Hh YjK6z7st+9rDs/OIwqVE5WHmK74o/Fs= X-Google-Smtp-Source: ABdhPJyiZf9WFcjl2wpM6uqmcdHuVX1FilJ+BdCpopf9df0EHQzBcYsj7SQQqvbROgQ7Bn4/pxwqww== X-Received: by 2002:a63:584a:: with SMTP id i10mr15233166pgm.315.1598537033405; Thu, 27 Aug 2020 07:03:53 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:03:52 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 16/20] tools: gpio: rename nlines to num_lines Date: Thu, 27 Aug 2020 22:00:16 +0800 Message-Id: <20200827140020.159627-17-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Rename nlines to num_lines to be consistent with other usage for fields describing the number of entries in an array. Signed-off-by: Kent Gibson --- tools/gpio/gpio-hammer.c | 26 +++++++++++++------------- tools/gpio/gpio-utils.c | 20 ++++++++++---------- tools/gpio/gpio-utils.h | 6 +++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c index 9fd926e8cb52..a2c7577fad5c 100644 --- a/tools/gpio/gpio-hammer.c +++ b/tools/gpio/gpio-hammer.c @@ -22,7 +22,7 @@ #include #include "gpio-utils.h" -int hammer_device(const char *device_name, unsigned int *lines, int nlines, +int hammer_device(const char *device_name, unsigned int *lines, int num_lines, unsigned int loops) { struct gpiohandle_data data; @@ -33,7 +33,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, unsigned int iteration = 0; memset(&data.values, 0, sizeof(data.values)); - ret = gpiotools_request_linehandle(device_name, lines, nlines, + ret = gpiotools_request_linehandle(device_name, lines, num_lines, GPIOHANDLE_REQUEST_OUTPUT, &data, "gpio-hammer"); if (ret < 0) @@ -46,15 +46,15 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, goto exit_close_error; fprintf(stdout, "Hammer lines ["); - for (i = 0; i < nlines; i++) { + for (i = 0; i < num_lines; i++) { fprintf(stdout, "%d", lines[i]); - if (i != (nlines - 1)) + if (i != (num_lines - 1)) fprintf(stdout, ", "); } fprintf(stdout, "] on %s, initial states: [", device_name); - for (i = 0; i < nlines; i++) { + for (i = 0; i < num_lines; i++) { fprintf(stdout, "%d", data.values[i]); - if (i != (nlines - 1)) + if (i != (num_lines - 1)) fprintf(stdout, ", "); } fprintf(stdout, "]\n"); @@ -63,7 +63,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, j = 0; while (1) { /* Invert all lines so we blink */ - for (i = 0; i < nlines; i++) + for (i = 0; i < num_lines; i++) data.values[i] = !data.values[i]; ret = gpiotools_set_values(fd, &data); @@ -81,9 +81,9 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, j = 0; fprintf(stdout, "["); - for (i = 0; i < nlines; i++) { + for (i = 0; i < num_lines; i++) { fprintf(stdout, "%d: %d", lines[i], data.values[i]); - if (i != (nlines - 1)) + if (i != (num_lines - 1)) fprintf(stdout, ", "); } fprintf(stdout, "]\r"); @@ -121,7 +121,7 @@ int main(int argc, char **argv) const char *device_name = NULL; unsigned int lines[GPIOHANDLES_MAX]; unsigned int loops = 0; - int nlines; + int num_lines; int c; int i; @@ -158,11 +158,11 @@ int main(int argc, char **argv) return -1; } - nlines = i; + num_lines = i; - if (!device_name || !nlines) { + if (!device_name || !num_lines) { print_usage(); return -1; } - return hammer_device(device_name, lines, nlines, loops); + return hammer_device(device_name, lines, num_lines, loops); } diff --git a/tools/gpio/gpio-utils.c b/tools/gpio/gpio-utils.c index 16a5d9cb9da2..d527980bcb94 100644 --- a/tools/gpio/gpio-utils.c +++ b/tools/gpio/gpio-utils.c @@ -38,7 +38,7 @@ * such as "gpiochip0" * @lines: An array desired lines, specified by offset * index for the associated GPIO device. - * @nline: The number of lines to request. + * @num_lines: The number of lines to request. * @flag: The new flag for requsted gpio. Reference * "linux/gpio.h" for the meaning of flag. * @data: Default value will be set to gpio when flag is @@ -56,7 +56,7 @@ * On failure return the errno. */ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, - unsigned int nlines, unsigned int flag, + unsigned int num_lines, unsigned int flag, struct gpiohandle_data *data, const char *consumer_label) { @@ -78,12 +78,12 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, goto exit_free_name; } - for (i = 0; i < nlines; i++) + for (i = 0; i < num_lines; i++) req.lineoffsets[i] = lines[i]; req.flags = flag; strcpy(req.consumer_label, consumer_label); - req.lines = nlines; + req.lines = num_lines; if (flag & GPIOHANDLE_REQUEST_OUTPUT) memcpy(req.default_values, data, sizeof(req.default_values)); @@ -194,20 +194,20 @@ int gpiotools_get(const char *device_name, unsigned int line) * such as "gpiochip0". * @lines: An array desired lines, specified by offset * index for the associated GPIO device. - * @nline: The number of lines to request. + * @num_lines: The number of lines to request. * @data: The array of values get from gpiochip. * * Return: On success return 0; * On failure return the errno. */ int gpiotools_gets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data) + unsigned int num_lines, struct gpiohandle_data *data) { int fd; int ret; int ret_close; - ret = gpiotools_request_linehandle(device_name, lines, nlines, + ret = gpiotools_request_linehandle(device_name, lines, num_lines, GPIOHANDLE_REQUEST_INPUT, data, CONSUMER); if (ret < 0) @@ -245,7 +245,7 @@ int gpiotools_set(const char *device_name, unsigned int line, * such as "gpiochip0". * @lines: An array desired lines, specified by offset * index for the associated GPIO device. - * @nline: The number of lines to request. + * @num_lines: The number of lines to request. * @data: The array of values set to gpiochip, must be * 0(low) or 1(high). * @@ -253,11 +253,11 @@ int gpiotools_set(const char *device_name, unsigned int line, * On failure return the errno. */ int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data) + unsigned int num_lines, struct gpiohandle_data *data) { int ret; - ret = gpiotools_request_linehandle(device_name, lines, nlines, + ret = gpiotools_request_linehandle(device_name, lines, num_lines, GPIOHANDLE_REQUEST_OUTPUT, data, CONSUMER); if (ret < 0) diff --git a/tools/gpio/gpio-utils.h b/tools/gpio/gpio-utils.h index cf37f13f3dcb..324729577865 100644 --- a/tools/gpio/gpio-utils.h +++ b/tools/gpio/gpio-utils.h @@ -23,7 +23,7 @@ static inline int check_prefix(const char *str, const char *prefix) } int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, - unsigned int nlines, unsigned int flag, + unsigned int num_lines, unsigned int flag, struct gpiohandle_data *data, const char *consumer_label); int gpiotools_set_values(const int fd, struct gpiohandle_data *data); @@ -32,10 +32,10 @@ int gpiotools_release_linehandle(const int fd); int gpiotools_get(const char *device_name, unsigned int line); int gpiotools_gets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data); + unsigned int num_lines, struct gpiohandle_data *data); int gpiotools_set(const char *device_name, unsigned int line, unsigned int value); int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data); + unsigned int num_lines, struct gpiohandle_data *data); #endif /* _GPIO_UTILS_H_ */ From patchwork Thu Aug 27 14:00:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352590 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=XLxt0CiK; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bckz01LGlz9sSP for ; Fri, 28 Aug 2020 00:05:48 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727917AbgH0OFl (ORCPT ); Thu, 27 Aug 2020 10:05:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727860AbgH0OFi (ORCPT ); Thu, 27 Aug 2020 10:05:38 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2341C061234; Thu, 27 Aug 2020 07:04:02 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id 17so3576562pfw.9; Thu, 27 Aug 2020 07:04:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B32Dv5Raw6pqsytedEB7eSywyX272NyDde93Ip5JzyQ=; b=XLxt0CiKsXCz8uyFlu7A+i4ld2L8BzYfv1l19LNlG3JZVZCi6u/0MF5Esutxy4qJLL nENfQLvX+oRiFnubKS9SlPdx5rvXnOLIuPeM8bsu5ouzrgxN22R5yHf98W8YreqiFodW 3hU2QwhoEQKUlpJxjr89jfFlOqODwhHwnqij4GomJ9fXZ4jvpQAUchtiKqm1ygXldrNy HdUIkZS4xV6a8yGHKQa2XYPBAcPEU0idKlGfO+IX/Y/ly4o38lYo2uSYkk8wJowCakdm ASGAj2z8XtiWEzy8MsJFXJ9/C1ccOf5eXQnMpXzK93+JzYUO+j40khpAjwYfA/YmLGLM KL/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B32Dv5Raw6pqsytedEB7eSywyX272NyDde93Ip5JzyQ=; b=D+xyZdI6OD/EPvc1voKZayMp45wU+124Yer0DxyHdvX42CcSgm3PQmWWK+ZW5uxz41 9C4upT5AkQcICll+9RfdtDchDzDgh6IGZTSafLoAMw797UjOpPAqMHbqDoBHdsRauNtf mDTeiA2GPJms7fcNfevd5xYwFIg8wV6Ba4qsrM9T48+Yusdutmi4x+tvONrDpIgqoDxM 5mSjCnISj0Ggu6SIFMtT1AdV2hYsUhLOEZWEVOBlqNkTpGw6GDGtC9x3/RAXgvai6Way OnJ3RBFEoxO1zCb7h0DbHFITTtTAFfQKuja9KIv/4U8vhnj4zym8eHg3jPgPZhH/gWzT 75NQ== X-Gm-Message-State: AOAM5313B0YCa8oXXJyFcSciel8pLgcOBl0wS5wQiKns6LfKXzo3f0/V jszoBqdlKQzlmiBnSbnTQ03UKNKC41g= X-Google-Smtp-Source: ABdhPJwogDAO50lVXieuaKvfn359cNHlizZdsnB/aivvjug8RnV9Bs3oACNoEdzR/rRB0OUqM142Qw== X-Received: by 2002:a17:902:aa8d:: with SMTP id d13mr14607519plr.124.1598537041484; Thu, 27 Aug 2020 07:04:01 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.03.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:04:00 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 17/20] tools: gpio: port gpio-hammer to v2 uAPI Date: Thu, 27 Aug 2020 22:00:17 +0800 Message-Id: <20200827140020.159627-18-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Port the gpio-hammer tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson --- tools/gpio/gpio-hammer.c | 32 +++++++---- tools/gpio/gpio-utils.c | 119 ++++++++++++++++++++++----------------- tools/gpio/gpio-utils.h | 50 +++++++++++++--- 3 files changed, 128 insertions(+), 73 deletions(-) diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c index a2c7577fad5c..54fdf59dd320 100644 --- a/tools/gpio/gpio-hammer.c +++ b/tools/gpio/gpio-hammer.c @@ -25,23 +25,30 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, unsigned int loops) { - struct gpiohandle_data data; + struct gpio_v2_line_values values; + struct gpio_v2_line_config config; char swirr[] = "-\\|/"; int fd; int ret; int i, j; unsigned int iteration = 0; - memset(&data.values, 0, sizeof(data.values)); - ret = gpiotools_request_linehandle(device_name, lines, num_lines, - GPIOHANDLE_REQUEST_OUTPUT, &data, - "gpio-hammer"); + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_OUTPUT; + + ret = gpiotools_request_line(device_name, lines, num_lines, + &config, "gpio-hammer"); if (ret < 0) goto exit_error; else fd = ret; - ret = gpiotools_get_values(fd, &data); + values.mask = 0; + values.bits = 0; + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&values.mask, i); + + ret = gpiotools_get_values(fd, &values); if (ret < 0) goto exit_close_error; @@ -53,7 +60,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, } fprintf(stdout, "] on %s, initial states: [", device_name); for (i = 0; i < num_lines; i++) { - fprintf(stdout, "%d", data.values[i]); + fprintf(stdout, "%d", gpiotools_test_bit(values.bits, i)); if (i != (num_lines - 1)) fprintf(stdout, ", "); } @@ -64,14 +71,14 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, while (1) { /* Invert all lines so we blink */ for (i = 0; i < num_lines; i++) - data.values[i] = !data.values[i]; + gpiotools_change_bit(&values.bits, i); - ret = gpiotools_set_values(fd, &data); + ret = gpiotools_set_values(fd, &values); if (ret < 0) goto exit_close_error; /* Re-read values to get status */ - ret = gpiotools_get_values(fd, &data); + ret = gpiotools_get_values(fd, &values); if (ret < 0) goto exit_close_error; @@ -82,7 +89,8 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, fprintf(stdout, "["); for (i = 0; i < num_lines; i++) { - fprintf(stdout, "%d: %d", lines[i], data.values[i]); + fprintf(stdout, "%d: %d", lines[i], + gpiotools_test_bit(values.bits, i)); if (i != (num_lines - 1)) fprintf(stdout, ", "); } @@ -97,7 +105,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, ret = 0; exit_close_error: - gpiotools_release_linehandle(fd); + gpiotools_release_line(fd); exit_error: return ret; } diff --git a/tools/gpio/gpio-utils.c b/tools/gpio/gpio-utils.c index d527980bcb94..68edc1a329e2 100644 --- a/tools/gpio/gpio-utils.c +++ b/tools/gpio/gpio-utils.c @@ -33,34 +33,32 @@ * release these lines. */ /** - * gpiotools_request_linehandle() - request gpio lines in a gpiochip + * gpiotools_request_line() - request gpio lines in a gpiochip * @device_name: The name of gpiochip without prefix "/dev/", * such as "gpiochip0" * @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. - * @flag: The new flag for requsted gpio. Reference - * "linux/gpio.h" for the meaning of flag. - * @data: Default value will be set to gpio when flag is - * GPIOHANDLE_REQUEST_OUTPUT. - * @consumer_label: The name of consumer, such as "sysfs", + * @config: The new config for requested gpio. Reference + * "linux/gpio.h" for config details. + * @consumer: The name of consumer, such as "sysfs", * "powerkey". This is useful for other users to * know who is using. * * Request gpio lines through the ioctl provided by chardev. User * could call gpiotools_set_values() and gpiotools_get_values() to * read and write respectively through the returned fd. Call - * gpiotools_release_linehandle() to release these lines after that. + * gpiotools_release_line() to release these lines after that. * * Return: On success return the fd; * On failure return the errno. */ -int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, - unsigned int num_lines, unsigned int flag, - struct gpiohandle_data *data, - const char *consumer_label) +int gpiotools_request_line(const char *device_name, unsigned int *lines, + unsigned int num_lines, + struct gpio_v2_line_config *config, + const char *consumer) { - struct gpiohandle_request req; + struct gpio_v2_line_request req; char *chrdev_name; int fd; int i; @@ -78,20 +76,19 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, goto exit_free_name; } + memset(&req, 0, sizeof(req)); for (i = 0; i < num_lines; i++) - req.lineoffsets[i] = lines[i]; + req.offsets[i] = lines[i]; - req.flags = flag; - strcpy(req.consumer_label, consumer_label); - req.lines = num_lines; - if (flag & GPIOHANDLE_REQUEST_OUTPUT) - memcpy(req.default_values, data, sizeof(req.default_values)); + req.config = *config; + strcpy(req.consumer, consumer); + req.num_lines = num_lines; - ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req); + ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req); if (ret == -1) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n", - "GPIO_GET_LINEHANDLE_IOCTL", ret, strerror(errno)); + "GPIO_GET_LINE_IOCTL", ret, strerror(errno)); } if (close(fd) == -1) @@ -103,17 +100,17 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, /** * gpiotools_set_values(): Set the value of gpio(s) * @fd: The fd returned by - * gpiotools_request_linehandle(). - * @data: The array of values want to set. + * gpiotools_request_line(). + * @values: The array of values want to set. * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_set_values(const int fd, struct gpiohandle_data *data) +int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values) { int ret; - ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data); + ret = ioctl(fd, GPIO_V2_LINE_SET_VALUES_IOCTL, values); if (ret == -1) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n", @@ -127,17 +124,17 @@ int gpiotools_set_values(const int fd, struct gpiohandle_data *data) /** * gpiotools_get_values(): Get the value of gpio(s) * @fd: The fd returned by - * gpiotools_request_linehandle(). - * @data: The array of values get from hardware. + * gpiotools_request_line(). + * @values: The array of values get from hardware. * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_get_values(const int fd, struct gpiohandle_data *data) +int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values) { int ret; - ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data); + ret = ioctl(fd, GPIO_V2_LINE_GET_VALUES_IOCTL, values); if (ret == -1) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n", @@ -149,14 +146,14 @@ int gpiotools_get_values(const int fd, struct gpiohandle_data *data) } /** - * gpiotools_release_linehandle(): Release the line(s) of gpiochip + * gpiotools_release_line(): Release the line(s) of gpiochip * @fd: The fd returned by - * gpiotools_request_linehandle(). + * gpiotools_request_line(). * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_release_linehandle(const int fd) +int gpiotools_release_line(const int fd) { int ret; @@ -180,11 +177,14 @@ int gpiotools_release_linehandle(const int fd) */ int gpiotools_get(const char *device_name, unsigned int line) { - struct gpiohandle_data data; + int ret; + unsigned int value; unsigned int lines[] = {line}; - gpiotools_gets(device_name, lines, 1, &data); - return data.values[0]; + ret = gpiotools_gets(device_name, lines, 1, &value); + if (ret) + return ret; + return value; } @@ -195,27 +195,35 @@ int gpiotools_get(const char *device_name, unsigned int line) * @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. - * @data: The array of values get from gpiochip. + * @values: The array of values get from gpiochip. * * Return: On success return 0; * On failure return the errno. */ int gpiotools_gets(const char *device_name, unsigned int *lines, - unsigned int num_lines, struct gpiohandle_data *data) + unsigned int num_lines, unsigned int *values) { - int fd; + int fd, i; int ret; int ret_close; + struct gpio_v2_line_config config; + struct gpio_v2_line_values lv; - ret = gpiotools_request_linehandle(device_name, lines, num_lines, - GPIOHANDLE_REQUEST_INPUT, data, - CONSUMER); + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_INPUT; + ret = gpiotools_request_line(device_name, lines, num_lines, + &config, CONSUMER); if (ret < 0) return ret; fd = ret; - ret = gpiotools_get_values(fd, data); - ret_close = gpiotools_release_linehandle(fd); + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&lv.mask, i); + ret = gpiotools_get_values(fd, &lv); + if (!ret) + for (i = 0; i < num_lines; i++) + values[i] = gpiotools_test_bit(lv.bits, i); + ret_close = gpiotools_release_line(fd); return ret < 0 ? ret : ret_close; } @@ -232,11 +240,9 @@ int gpiotools_gets(const char *device_name, unsigned int *lines, int gpiotools_set(const char *device_name, unsigned int line, unsigned int value) { - struct gpiohandle_data data; unsigned int lines[] = {line}; - data.values[0] = value; - return gpiotools_sets(device_name, lines, 1, &data); + return gpiotools_sets(device_name, lines, 1, &value); } /** @@ -246,22 +252,31 @@ int gpiotools_set(const char *device_name, unsigned int line, * @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. - * @data: The array of values set to gpiochip, must be + * @value: The array of values set to gpiochip, must be * 0(low) or 1(high). * * Return: On success return 0; * On failure return the errno. */ int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int num_lines, struct gpiohandle_data *data) + unsigned int num_lines, unsigned int *values) { - int ret; + int ret, i; + struct gpio_v2_line_config config; - ret = gpiotools_request_linehandle(device_name, lines, num_lines, - GPIOHANDLE_REQUEST_OUTPUT, data, - CONSUMER); + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_OUTPUT; + config.num_attrs = 1; + config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES; + for (i = 0; i < num_lines; i++) { + gpiotools_set_bit(&config.attrs[0].mask, i); + gpiotools_assign_bit(&config.attrs[0].attr.values, + i, values[i]); + } + ret = gpiotools_request_line(device_name, lines, num_lines, + &config, CONSUMER); if (ret < 0) return ret; - return gpiotools_release_linehandle(ret); + return gpiotools_release_line(ret); } diff --git a/tools/gpio/gpio-utils.h b/tools/gpio/gpio-utils.h index 324729577865..58c93a90add6 100644 --- a/tools/gpio/gpio-utils.h +++ b/tools/gpio/gpio-utils.h @@ -12,7 +12,9 @@ #ifndef _GPIO_UTILS_H_ #define _GPIO_UTILS_H_ +#include #include +#include #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) @@ -22,20 +24,50 @@ static inline int check_prefix(const char *str, const char *prefix) strncmp(str, prefix, strlen(prefix)) == 0; } -int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, - unsigned int num_lines, unsigned int flag, - struct gpiohandle_data *data, - const char *consumer_label); -int gpiotools_set_values(const int fd, struct gpiohandle_data *data); -int gpiotools_get_values(const int fd, struct gpiohandle_data *data); -int gpiotools_release_linehandle(const int fd); +int gpiotools_request_line(const char *device_name, + unsigned int *lines, + unsigned int num_lines, + struct gpio_v2_line_config *config, + const char *consumer); +int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values); +int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values); +int gpiotools_release_line(const int fd); int gpiotools_get(const char *device_name, unsigned int line); int gpiotools_gets(const char *device_name, unsigned int *lines, - unsigned int num_lines, struct gpiohandle_data *data); + unsigned int num_lines, unsigned int *values); int gpiotools_set(const char *device_name, unsigned int line, unsigned int value); int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int num_lines, struct gpiohandle_data *data); + unsigned int num_lines, unsigned int *values); + +/* helper functions for gpio_v2_line_values bits */ +static inline void gpiotools_set_bit(__u64 *b, int n) +{ + *b |= 1ULL << n; +} + +static inline void gpiotools_change_bit(__u64 *b, int n) +{ + *b ^= 1ULL << n; +} + +static inline void gpiotools_clear_bit(__u64 *b, int n) +{ + *b &= ~(1ULL << n); +} + +static inline int gpiotools_test_bit(__u64 b, int n) +{ + return !!(b & 1ULL << n); +} + +static inline void gpiotools_assign_bit(__u64 *b, int n, bool value) +{ + if (value) + gpiotools_set_bit(b, n); + else + gpiotools_clear_bit(b, n); +} #endif /* _GPIO_UTILS_H_ */ From patchwork Thu Aug 27 14:00:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352588 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=qDe8RH5s; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bckyx3zKCz9sSP for ; Fri, 28 Aug 2020 00:05:45 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727946AbgH0OFn (ORCPT ); Thu, 27 Aug 2020 10:05:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727864AbgH0OFi (ORCPT ); Thu, 27 Aug 2020 10:05:38 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0E47C0619C8; Thu, 27 Aug 2020 07:04:15 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id mw10so2650183pjb.2; Thu, 27 Aug 2020 07:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NHFesHMh8urOzDGLF3BG3WK3USIsA8bYlNdGJPsk5Ng=; b=qDe8RH5sDvLJOdA1yQDmBGleVMsL5nq+Ua7MtmBNrNk2SwgsIt6yPuK8b7mLjlN2QV fEUNAcjH3K2fiYX0850kmbi6r8IRWwHVKFlb/Up0gUvPfpILl4rqhNNMuiXV7nPYhAUK qHD7y8WrN5Pb+8ESKuBDB/DsY4HdMgOvZuBf1fMiqUFH9u8MnTUFWRy0pCQfz2Utcwih OHtlo/Rm8tVkKrNu5I1t4oM2NbBRgd4dJ4iqPY2S1hvYV9KPiF0Zlu9vSCPojm1yJy5y hSsJB07q4hDCygZqy/QbsknUkJpyNzwQvt4JUCGur3OsRhGo+PEAKZemdLkgwE6fZhHW 2+aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NHFesHMh8urOzDGLF3BG3WK3USIsA8bYlNdGJPsk5Ng=; b=Tm9y1k6qh9Eer3n4+JxLeKEBz33LPBRri/xf7iHbJlBIWnVJp3eTR37aM+XJwIuiDR sR/Ie7mumURsWn7Cv8jJhaUL8Q0ghpTXUmh00pz+O+i8spbknXSO4vpJPIp6ohCgCzQY +1PNpG21IEH0qbQu+oHSmI8LKXXlHVUEsFusLiy13D0KB9xUJvrFt2aw1jvNbjujIWHC fKf5ZHgq4zH985rUPG5TZPAEP3oGkvBvQpY5LTPU+x5jzgP7KMl6H8OrdWcrNXd7cmdR ns/tZ6oau0RrGUsXtcS+nHJkn010ETeFNAzf4U/pUqIWAnSQRVrah8ldd2eioQFDAt89 WLyA== X-Gm-Message-State: AOAM530di7kFDgZ6qJTYEeRlxqSeGk3PBd59esdOW38kchQVSDswAe4+ V7yo8lu5InD7YGMBBpS/MAaZ0LvyJFg= X-Google-Smtp-Source: ABdhPJydDw+Mp/wqlKlBrk5r8ON417V78PSY4Ip9cb+pzxSDYc1gyRCDjceHtZAlTMEkgcsDVC9hvg== X-Received: by 2002:a17:90a:80ca:: with SMTP id k10mr11030071pjw.86.1598537054844; Thu, 27 Aug 2020 07:04:14 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:04:14 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 18/20] tools: gpio: port gpio-event-mon to v2 uAPI Date: Thu, 27 Aug 2020 22:00:18 +0800 Message-Id: <20200827140020.159627-19-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Port the gpio-event-mon tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson --- tools/gpio/gpio-event-mon.c | 91 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/tools/gpio/gpio-event-mon.c b/tools/gpio/gpio-event-mon.c index 1a303a81aeef..d6a831200c18 100644 --- a/tools/gpio/gpio-event-mon.c +++ b/tools/gpio/gpio-event-mon.c @@ -23,17 +23,16 @@ #include #include #include +#include "gpio-utils.h" int monitor_device(const char *device_name, unsigned int line, - uint32_t handleflags, - uint32_t eventflags, + struct gpio_v2_line_config *config, unsigned int loops) { - struct gpioevent_request req; - struct gpiohandle_data data; + struct gpio_v2_line_values values; char *chrdev_name; - int fd; + int cfd, lfd; int ret; int i = 0; @@ -41,44 +40,39 @@ int monitor_device(const char *device_name, if (ret < 0) return -ENOMEM; - fd = open(chrdev_name, 0); - if (fd == -1) { + cfd = open(chrdev_name, 0); + if (cfd == -1) { ret = -errno; fprintf(stderr, "Failed to open %s\n", chrdev_name); goto exit_free_name; } - req.lineoffset = line; - req.handleflags = handleflags; - req.eventflags = eventflags; - strcpy(req.consumer_label, "gpio-event-mon"); - - ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req); - if (ret == -1) { - ret = -errno; - fprintf(stderr, "Failed to issue GET EVENT " - "IOCTL (%d)\n", - ret); - goto exit_close_error; - } + ret = gpiotools_request_line(device_name, &line, 1, config, + "gpio-event-mon"); + if (ret < 0) + goto exit_device_close; + else + lfd = ret; /* Read initial states */ - ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); - if (ret == -1) { - ret = -errno; - fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE " - "VALUES IOCTL (%d)\n", + values.mask = 1; + values.bits = 0; + ret = gpiotools_get_values(lfd, &values); + if (ret < 0) { + fprintf(stderr, + "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n", ret); - goto exit_close_error; + goto exit_line_close; } fprintf(stdout, "Monitoring line %d on %s\n", line, device_name); - fprintf(stdout, "Initial line value: %d\n", data.values[0]); + fprintf(stdout, "Initial line value: %d\n", + gpiotools_test_bit(values.bits, 0)); while (1) { - struct gpioevent_data event; + struct gpio_v2_line_event event; - ret = read(req.fd, &event, sizeof(event)); + ret = read(lfd, &event, sizeof(event)); if (ret == -1) { if (errno == -EAGAIN) { fprintf(stderr, "nothing available\n"); @@ -96,12 +90,14 @@ int monitor_device(const char *device_name, ret = -EIO; break; } - fprintf(stdout, "GPIO EVENT %llu: ", event.timestamp); + fprintf(stdout, "GPIO EVENT at %llu on line %d (%d|%d) ", + event.timestamp, event.offset, event.line_seqno, + event.seqno); switch (event.id) { - case GPIOEVENT_EVENT_RISING_EDGE: + case GPIO_V2_LINE_EVENT_RISING_EDGE: fprintf(stdout, "rising edge"); break; - case GPIOEVENT_EVENT_FALLING_EDGE: + case GPIO_V2_LINE_EVENT_FALLING_EDGE: fprintf(stdout, "falling edge"); break; default: @@ -114,8 +110,11 @@ int monitor_device(const char *device_name, break; } -exit_close_error: - if (close(fd) == -1) +exit_line_close: + if (close(lfd) == -1) + perror("Failed to close line file"); +exit_device_close: + if (close(cfd) == -1) perror("Failed to close GPIO character device file"); exit_free_name: free(chrdev_name); @@ -140,15 +139,20 @@ void print_usage(void) ); } +#define EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) + int main(int argc, char **argv) { const char *device_name = NULL; unsigned int line = -1; unsigned int loops = 0; - uint32_t handleflags = GPIOHANDLE_REQUEST_INPUT; - uint32_t eventflags = 0; + struct gpio_v2_line_config config; int c; + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_INPUT; while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) { switch (c) { case 'c': @@ -161,16 +165,16 @@ int main(int argc, char **argv) line = strtoul(optarg, NULL, 10); break; case 'd': - handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN; + config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; break; case 's': - handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE; + config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; break; case 'r': - eventflags |= GPIOEVENT_REQUEST_RISING_EDGE; + config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; break; case 'f': - eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE; + config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; break; case '?': print_usage(); @@ -182,11 +186,10 @@ int main(int argc, char **argv) print_usage(); return -1; } - if (!eventflags) { + if (!(config.flags & EDGE_FLAGS)) { printf("No flags specified, listening on both rising and " "falling edges\n"); - eventflags = GPIOEVENT_REQUEST_BOTH_EDGES; + config.flags |= EDGE_FLAGS; } - return monitor_device(device_name, line, handleflags, - eventflags, loops); + return monitor_device(device_name, line, &config, loops); } From patchwork Thu Aug 27 14:00:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=OnhNILNH; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BclGp0M60z9sRK for ; Fri, 28 Aug 2020 00:19:30 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728009AbgH0ORC (ORCPT ); Thu, 27 Aug 2020 10:17:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727878AbgH0OFi (ORCPT ); Thu, 27 Aug 2020 10:05:38 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47A7EC061239; Thu, 27 Aug 2020 07:04:22 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id ls14so2646347pjb.3; Thu, 27 Aug 2020 07:04:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zeAqt+L6g1vrQKWXIoolqUPOf3l3HNvz8g6zB2yyrdI=; b=OnhNILNH79CwzJfLX9pU2ERgM5FfOBMz09TdRKvhkLzIZW5EGAehJtlDgnynAWsvfT T//Y5PEBkgfX45MrBipWKt/AcvQk9Ar3m8LXBI9K6yQw1qkWmNA1tIt91zC5mkT+xxt6 WU2XNWOSMVBqCiAXlPqt4at6gbYqLpOKkN1hYBtqtQFsuCJB8SucZj7K5fP7Tfgmdaby Awua82yghSmwwvMUhcROtP3iLPJ/mmJpWNo7HdY4sD4pgv6eGBaTbwgqTY7/VJ3hHjIE 27w/jJQF+hp0XatwPf3Ksx1LmjDYVWbDNHoJst9Pq6ShdfV47btGtxqzdke+imB3ZM/C qfDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zeAqt+L6g1vrQKWXIoolqUPOf3l3HNvz8g6zB2yyrdI=; b=e18zROpaKkbr1wpyR5h2TztxU7JCYjxpRwtZdNUfPWqRefre1bW3ZtKv25jXue3d+Z XCGA/mZJcmUOUZXMl62Z6gEueQCvWaoYJLj5sCOJQjUo1RblDMu93VAx0uqxSkLDcpjp CbHMaDVtSZFp8LNOi6BNqqZ27rCoD81dpbmH1x9Z5o2Oz4Sne9UDS1Uku5Wpac6mNoL/ TT5c9YZhTZLnByz4PZEyh6JcnLXmRvwCmVqEHF3TXDN49p2fGtAc+S0n3oYghv+ta4cR 56y1Th2m34msR3Bryb6ORJS33hbuhljnV4qVRTUWmb1NHk0j/T6M6O8ueHMURu/nHI27 1OIQ== X-Gm-Message-State: AOAM531ShvZVLz2u/50uDg2vO3pQHwqmesSzzAb7+MewxkNyBVYdptCf hXGZraXCBSlSJM2uwBs9V5Tbv7eQrmI= X-Google-Smtp-Source: ABdhPJyJ+9QkifpDjycw3SplohaUIxF5lHNpUuo8KMqk+GwoNlCd8161IAJcKF1+hBnL7ApGZ1YxUQ== X-Received: by 2002:a17:90a:e815:: with SMTP id i21mr2825462pjy.216.1598537061348; Thu, 27 Aug 2020 07:04:21 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.04.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:04:20 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 19/20] tools: gpio: add multi-line monitoring to gpio-event-mon Date: Thu, 27 Aug 2020 22:00:19 +0800 Message-Id: <20200827140020.159627-20-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Extend gpio-event-mon to support monitoring multiple lines. This would require multiple lineevent requests to implement using uAPI v1, but can be performed with a single line request using uAPI v2. Signed-off-by: Kent Gibson --- tools/gpio/gpio-event-mon.c | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/tools/gpio/gpio-event-mon.c b/tools/gpio/gpio-event-mon.c index d6a831200c18..e50bb107ea3a 100644 --- a/tools/gpio/gpio-event-mon.c +++ b/tools/gpio/gpio-event-mon.c @@ -26,7 +26,8 @@ #include "gpio-utils.h" int monitor_device(const char *device_name, - unsigned int line, + unsigned int *lines, + unsigned int num_lines, struct gpio_v2_line_config *config, unsigned int loops) { @@ -47,7 +48,7 @@ int monitor_device(const char *device_name, goto exit_free_name; } - ret = gpiotools_request_line(device_name, &line, 1, config, + ret = gpiotools_request_line(device_name, lines, num_lines, config, "gpio-event-mon"); if (ret < 0) goto exit_device_close; @@ -55,8 +56,10 @@ int monitor_device(const char *device_name, lfd = ret; /* Read initial states */ - values.mask = 1; + values.mask = 0; values.bits = 0; + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&values.mask, i); ret = gpiotools_get_values(lfd, &values); if (ret < 0) { fprintf(stderr, @@ -65,9 +68,23 @@ int monitor_device(const char *device_name, goto exit_line_close; } - fprintf(stdout, "Monitoring line %d on %s\n", line, device_name); - fprintf(stdout, "Initial line value: %d\n", - gpiotools_test_bit(values.bits, 0)); + if (num_lines == 1) { + fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name); + fprintf(stdout, "Initial line value: %d\n", + gpiotools_test_bit(values.bits, 0)); + } else { + fprintf(stdout, "Monitoring lines %d", lines[0]); + for (i = 1; i < num_lines - 1; i++) + fprintf(stdout, ", %d", lines[i]); + fprintf(stdout, " and %d on %s\n", lines[i], device_name); + fprintf(stdout, "Initial line values: %d", + gpiotools_test_bit(values.bits, 0)); + for (i = 1; i < num_lines - 1; i++) + fprintf(stdout, ", %d", + gpiotools_test_bit(values.bits, i)); + fprintf(stdout, " and %d\n", + gpiotools_test_bit(values.bits, i)); + } while (1) { struct gpio_v2_line_event event; @@ -126,7 +143,7 @@ void print_usage(void) fprintf(stderr, "Usage: gpio-event-mon [options]...\n" "Listen to events on GPIO lines, 0->1 1->0\n" " -n Listen on GPIOs on a named device (must be stated)\n" - " -o Offset to monitor\n" + " -o Offset of line to monitor (may be repeated)\n" " -d Set line as open drain\n" " -s Set line as open source\n" " -r Listen for rising edges\n" @@ -146,7 +163,8 @@ void print_usage(void) int main(int argc, char **argv) { const char *device_name = NULL; - unsigned int line = -1; + unsigned int lines[GPIO_V2_LINES_MAX]; + unsigned int num_lines = 0; unsigned int loops = 0; struct gpio_v2_line_config config; int c; @@ -162,7 +180,12 @@ int main(int argc, char **argv) device_name = optarg; break; case 'o': - line = strtoul(optarg, NULL, 10); + if (num_lines >= GPIO_V2_LINES_MAX) { + print_usage(); + return -1; + } + lines[num_lines] = strtoul(optarg, NULL, 10); + num_lines++; break; case 'd': config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; @@ -182,7 +205,7 @@ int main(int argc, char **argv) } } - if (!device_name || line == -1) { + if (!device_name || num_lines == 0) { print_usage(); return -1; } @@ -191,5 +214,5 @@ int main(int argc, char **argv) "falling edges\n"); config.flags |= EDGE_FLAGS; } - return monitor_device(device_name, line, &config, loops); + return monitor_device(device_name, lines, num_lines, &config, loops); } From patchwork Thu Aug 27 14:00:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1352589 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=LguBKEDZ; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4Bckyy0w3Rz9sTK for ; Fri, 28 Aug 2020 00:05:46 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727927AbgH0OFl (ORCPT ); Thu, 27 Aug 2020 10:05:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727892AbgH0OFi (ORCPT ); Thu, 27 Aug 2020 10:05:38 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BF48C0619C9; Thu, 27 Aug 2020 07:04:30 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id ds1so2684952pjb.1; Thu, 27 Aug 2020 07:04:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HaME3Z5uLAfT5ND31CqQyrNIC7MORGaQXez8ygNaWV4=; b=LguBKEDZg38nhsfY5gDkB5eMqQtPWYgd4SzXcMfIlffnaaoFXePM7C2c5R9jFADrvR 00OcpCBfsiI0K4EnkIQSzYpgSceMIZz5Fl7A3Pk1XTalnXRadOtWztDcNxJ3kntWIzBO TKmxeUq8fVwV6iEpnna8RQWpxS242Jy4KWBlf37uFH+CVoY9/YbFK8ffS3qfvV04iaid KNDUyko1S6d3r9x2RVNBuVr16CX6jplQMCN8OFZCxHI+Z589/EQw4M3BGM/HdHgdQik8 J8QbwAqSMLemWNL5JqagCzB5dRmyci++9gfnZ9JNbCU7b767CJRVNpLdT5g69F+ID9a3 /0Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HaME3Z5uLAfT5ND31CqQyrNIC7MORGaQXez8ygNaWV4=; b=nTpYRrJV5F14OYwDYXccrlAiugLgV6dxfRo00agEcXCAJvZJtFmVLAB1Vh6c+QdQL2 RvcT25uPFbNhdVFDhhNn9oinHKQjwabBc3RvctG2l7Rpvvx2ZkMXyJ4OAHmEyDt2rOey T0RLQbU5cvXxNIxaM///MFRmoNePX2PNC2GN6hb/5w/wrTp5lYhIBXCsMIrfQkkow1aU fw+wZShj6IqegRugv4SxiSPPfc6k3wW4V9KR5za1AzF9bRk5JcYTD7b1oZ7vjxvttTAY s4/bm2kiFVxv1JxUZoIUHuSemij0JDYBf+kDq2onjpC9lYthhVNnH2VcX14wSnvhAzta YwXA== X-Gm-Message-State: AOAM531Kj7F4qQtPI0alspUrwbR76+yVx7SorbH7Qm08VGutEQki0MIC m5zTeODSIDMbdIXE/4EjsOMaza9GMxk= X-Google-Smtp-Source: ABdhPJyFtzqBQBD/y88ysYIZXMsxWHe8lc0Zf/s1dX5c7sBXfH4LHmi4gDDsJtL1iJmKfcfaptFIrw== X-Received: by 2002:a17:90a:c253:: with SMTP id d19mr4892302pjx.113.1598537068544; Thu, 27 Aug 2020 07:04:28 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id fs12sm2371092pjb.21.2020.08.27.07.04.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 07:04:27 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v5 20/20] tools: gpio: add debounce support to gpio-event-mon Date: Thu, 27 Aug 2020 22:00:20 +0800 Message-Id: <20200827140020.159627-21-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200827140020.159627-1-warthog618@gmail.com> References: <20200827140020.159627-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for debouncing monitored lines to gpio-event-mon. Signed-off-by: Kent Gibson --- tools/gpio/gpio-event-mon.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/gpio/gpio-event-mon.c b/tools/gpio/gpio-event-mon.c index e50bb107ea3a..bd5ea3cc6e85 100644 --- a/tools/gpio/gpio-event-mon.c +++ b/tools/gpio/gpio-event-mon.c @@ -148,11 +148,12 @@ void print_usage(void) " -s Set line as open source\n" " -r Listen for rising edges\n" " -f Listen for falling edges\n" + " -b Debounce the line with period n microseconds\n" " [-c ] Do loops (optional, infinite loop if not stated)\n" " -? This helptext\n" "\n" "Example:\n" - "gpio-event-mon -n gpiochip0 -o 4 -r -f\n" + "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n" ); } @@ -167,11 +168,12 @@ int main(int argc, char **argv) unsigned int num_lines = 0; unsigned int loops = 0; struct gpio_v2_line_config config; - int c; + int c, attr, i; + unsigned long debounce_period = 0; memset(&config, 0, sizeof(config)); config.flags = GPIO_V2_LINE_FLAG_INPUT; - while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) { + while ((c = getopt(argc, argv, "c:n:o:b:dsrf?")) != -1) { switch (c) { case 'c': loops = strtoul(optarg, NULL, 10); @@ -187,6 +189,9 @@ int main(int argc, char **argv) lines[num_lines] = strtoul(optarg, NULL, 10); num_lines++; break; + case 'b': + debounce_period = strtoul(optarg, NULL, 10); + break; case 'd': config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; break; @@ -205,6 +210,15 @@ int main(int argc, char **argv) } } + if (debounce_period) { + attr = config.num_attrs; + config.num_attrs++; + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&config.attrs[attr].mask, i); + config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; + config.attrs[attr].attr.debounce_period = debounce_period; + } + if (!device_name || num_lines == 0) { print_usage(); return -1;