Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp3625772pxa; Sun, 9 Aug 2020 06:28:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgAr0hCFIzoi12vanu3Dzjaq+TgliYxK3YhwkxMMtlV63Ze+s4V8gSMrjBQm5DNMikzl2Y X-Received: by 2002:a17:906:bcc4:: with SMTP id lw4mr16654917ejb.361.1596979715087; Sun, 09 Aug 2020 06:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596979715; cv=none; d=google.com; s=arc-20160816; b=n0xOkUm8Z1D2ybwdASUEhdofLhbOrwKCmdn5QpMD+sAuqD9oesxKrkXNhs2ipkqOPz GcBEbHrlbZPW7N4Stp/4j6/MlgO7PthRpWRsq+ps20UKbBvR67k39h96r0wDeAVn75K2 D9OEfqMQ+0qAufapa83rFBzo32Lp0jZHAQdtuPuwhr45g5KkBNqxcnkrWpuOJxAvP1KY yq/GemYLRftHmssYgnzsGjviYpjyWfckRvdyl1TdyA0fOTUWRoXAuSfePjceMOX0phdD L1mEMJWK5ZJ5nvNMzKURGPwaSsmbE0MnS1m5ljZg4Peg7z3qnqCnrjpxxwSXXMBooAWy g3rA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RcrvDeCNpQs7mXE2OMv5MhmIl6eGSB/h4qSTdc8P7tk=; b=LvkprkWTUM237vD09idaL+ncm18GtKZNczgSPmUmXhNK/b4MCEAlCvpIPmLeKxBroZ wDNN0nk36rcnG6IvGB9yFo6UyU0xX6oC/Q/UW5KFf0SDke+gzYJAr/uyiZrlwN0jboiv txgJ43PF084gWQmjhVzodcc6X4cEf/qsyhlvewobTknq8Uf+KycCDrFgRjNNve99zqLY wqD9veE910ZBZ+3i7+Hpe7I4ikFm1Gj3CvltQiQk1RfZKsScahKdLempVhLh4YaWhd+2 sJJrvTo208ME5aJxezCKoHAfYfYCasL2jW18GuJti2Jg8aSP6jWvz8HQ+0Sh+jQLETY5 IB/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="I/+fEa3K"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ss19si9020951ejb.252.2020.08.09.06.28.11; Sun, 09 Aug 2020 06:28:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="I/+fEa3K"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726547AbgHIN06 (ORCPT + 99 others); Sun, 9 Aug 2020 09:26:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726486AbgHIN05 (ORCPT ); Sun, 9 Aug 2020 09:26:57 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2E48C061756; Sun, 9 Aug 2020 06:26:57 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id o13so3463961pgf.0; Sun, 09 Aug 2020 06:26:57 -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=RcrvDeCNpQs7mXE2OMv5MhmIl6eGSB/h4qSTdc8P7tk=; b=I/+fEa3Kd/eqsGjR8s5R9iBnzcGnKXi1sLiuFitK2Xa4CXScjPC0vZjEh3wInzwufN 3hLBWpm5v/Uzw5LVDU47LqncTG2aoHHC5Cl2HWGqzjBiaUF2PKSRlsvvdTCXw6sLoaCX eQcxGMgkYKuwh4a3vnqfk45zozcm+P7qQjpIkWlAhx5RQYH6ZzJN5U4GyqmG92zYc8vT gHZGVgAIOdtiruKfqnBJka0qS4edzAOMWoSEWw/UA9HcrOTOErAPp/YhYI7G0Os/wmAX x2aaLTeIwhKfIzBA2mqfU9jEB53AnT8FTnrAvrtPGnhYzg2h5PPn2kDBxdzQumd2NQ48 /KrQ== 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=RcrvDeCNpQs7mXE2OMv5MhmIl6eGSB/h4qSTdc8P7tk=; b=EnFKnWdHt4kdPuS7P/0lcCmI5mgZCHZr5L75v8NkFsVv2TgclaCVR1Cr0LtnQaZFAs iQXb745jOc9D3SBZe6Yc1UDsN4cf+XEi5etFzcx+XwpvMgLEhN9GWulvVHcPtm/MwTNa AhDHH8u/w1sCo//pZUnmt3T9f6kqCHrIG4yk9SDY3AeEu82rtfyURNDDM6Koe3z7XLvs ekgwoQJduYOkhPiQQ2CuLttOQECr32fTXqtKGw+YNRboJRI1nF77MUdLZcjKArjwHBAq 8TOATK/j8S6lYhsZHPntD5sFb0kuWFSdfnj1wcmbfRVJQygfngeF8jszNzXD0f90wDnZ Js5g== X-Gm-Message-State: AOAM530eprsix0ZfPuGLBkOGnqeStGvUf9DJb5EWas8mDYXn3LtXt4bM VSYgYT5DsowXjCPe8TXJ9M8jhWIR X-Received: by 2002:a65:6289:: with SMTP id f9mr18218295pgv.272.1596979617017; Sun, 09 Aug 2020 06:26:57 -0700 (PDT) Received: from sol.lan (106-69-185-93.dyn.iinet.net.au. [106.69.185.93]) by smtp.gmail.com with ESMTPSA id j10sm9127414pff.171.2020.08.09.06.26.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Aug 2020 06:26: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 v3 08/18] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL Date: Sun, 9 Aug 2020 21:25:19 +0800 Message-Id: <20200809132529.264312-9-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200809132529.264312-1-warthog618@gmail.com> References: <20200809132529.264312-1-warthog618@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 c1b916224240..d34dad50a048 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -418,6 +419,8 @@ struct edge_detector { * @seqno: the sequence number for edge events generated on all lines in * this line request. Note that this is not used when @num_descs 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. * @edets: an array of edge detectors, of size @num_descs * @descs: the GPIO descriptors held by this line request, with @num_descs * elements. @@ -429,6 +432,7 @@ struct line { wait_queue_head_t wait; DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); atomic_t seqno; + struct mutex config_mutex; struct edge_detector *edets; struct gpio_desc *descs[]; }; @@ -703,6 +707,30 @@ static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, return 0; } +static int gpio_v2_line_config_change_validate(struct line *line, + struct gpio_v2_line_config *lc) +{ + int i; + u64 flags; + struct gpio_desc *desc; + + for (i = 0; i < line->num_descs; i++) { + desc = line->descs[i]; + flags = gpio_v2_line_config_flags(lc, i); + /* disallow edge detection changes */ + if (line->edets[i].flags != (flags & GPIO_V2_LINE_EDGE_FLAGS)) + return -EINVAL; + + if (line->edets[i].flags) { + /* disallow polarity changes */ + if (test_bit(FLAG_ACTIVE_LOW, &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) { @@ -783,6 +811,67 @@ static long line_get_values(struct line *line, void __user *ip) return 0; } +static long line_set_config_locked(struct line *line, + struct gpio_v2_line_config *lc) +{ + struct gpio_desc *desc; + int i, ret; + u64 flags; + + ret = gpio_v2_line_config_change_validate(line, lc); + if (ret) + return ret; + + for (i = 0; i < line->num_descs; i++) { + desc = line->descs[i]; + 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(&line->edets[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 line_set_config(struct line *line, 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, line->num_descs); + if (ret) + return ret; + + mutex_lock(&line->config_mutex); + + ret = line_set_config_locked(line, &lc); + + mutex_unlock(&line->config_mutex); + + return ret; +} + static long line_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { @@ -791,6 +880,8 @@ static long line_ioctl(struct file *file, unsigned int cmd, if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) return line_get_values(line, ip); + else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) + return line_set_config(line, ip); return -EINVAL; } @@ -964,6 +1055,7 @@ static int line_create(struct gpio_device *gdev, void __user *ip) } } + mutex_init(&line->config_mutex); init_waitqueue_head(&line->wait); if (has_edge_detection) { size = lr.event_buffer_size; -- 2.28.0