Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2914322pxk; Mon, 28 Sep 2020 03:43:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIUdfKR2AuP6mJQIHLpKAuX6zf70k5NfVjhRZqMLCtuWSHjlP94rejUC4Xw0V91H1gNREs X-Received: by 2002:a17:906:fb04:: with SMTP id lz4mr995476ejb.535.1601289832788; Mon, 28 Sep 2020 03:43:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601289832; cv=none; d=google.com; s=arc-20160816; b=nUP2ixpr65eKsqLIRCeCPiBzGs/JMKuWHrT1Y5WPbrYqiG/1HJ8Et/WA96Kh1NfQ2I 3C1bA9VNyhYSNFvOCCTn6y8bL42BeRFF9etB0Kzee7bOj/EeVxUZpIoZBWOneaXY4u7A YYhO/PiigsnWfXyDjOpOuY5/lBPg9Fvd2pQ625JexXPJeGyOE+0j/bvgaDPSAfUwlb7t ueedj1frRnIG/JtpYBIa2t/Qu3/oVieuSj2tZUnec9g9PSQ8pensQwBVkolgGRUkRcaA QjjjOHxKTquXuNemVZYip36vNSyfgBeAAje+wm0O2LHW8peBLIq+szQ6nG74lZlw/tYa I+gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WjvGzyO62y8+TE10NX89uaVC3jyOwciikUlJLtdxrSc=; b=eSmYZaP5GENDMYBNNRzYhOnWM9hINAQ6L+upZSF62TIZeA+CJOMS5lQQQ95OAEG0v6 jot/I31Ke6mqksRM73N5AMJVqbdvQ8ypSz+Odeq3wWPBHuzhBet0CNLM4S2ZplIY2mzp z8gV4HoJ6A5ly49Y5aMePjGdRTNZ3HJrUdXbBgrehxINvyD6dvCcwWUDZ7Br2+M7WcBH JoCkVHazUVpP5KYWNRTAd+gcp6cXjK0Wn7qi+a0IBRy7bHPcEDb1WyIplImMPz2DKU7b Lfo9ZTebr/KVhCMl2yu4kD+D+DrG5xQyro9+RQ3AIyMecuB2r0JQYlS894BXFJqM1O2z 9rYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=UP6FbU97; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qo10si325320ejb.730.2020.09.28.03.43.30; Mon, 28 Sep 2020 03:43:52 -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=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=UP6FbU97; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726693AbgI1KmU (ORCPT + 99 others); Mon, 28 Sep 2020 06:42:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726651AbgI1KmM (ORCPT ); Mon, 28 Sep 2020 06:42:12 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73824C0613CE for ; Mon, 28 Sep 2020 03:42:11 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id j2so742392wrx.7 for ; Mon, 28 Sep 2020 03:42:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WjvGzyO62y8+TE10NX89uaVC3jyOwciikUlJLtdxrSc=; b=UP6FbU97hiLI3MWndLsukFiAqWATwepolyDq9C2sft9a4uW61HXqHHdlmryW5fOeGk AvFbXjzWssoioAlwHVB3m5lWEYWDLceWZl0e58Vm5AQGKqTIvcUPq/VD42dMRUDleFXJ rbWSwvwmEFUQ+1NWBfpmnNzrNh5aJfp9m8GjGfTqhtBJrigyA/oFNUJo9kM5zZXt3IVA n84qcoYzzop6WqR2np2o1mJ0liJiPJFKTNFnmuykYCp+5qpnhRV4e7F87pD7fLqgzMYs rxq4OxP+5BO29PuQHj1fKaMi41SisX7vk9ByFuBsfwPV6Ia/fSHrzTnfen6IWxajQS1+ v/sA== 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=WjvGzyO62y8+TE10NX89uaVC3jyOwciikUlJLtdxrSc=; b=djNjImTvLYt5edVmIBKxaIu+G8gP+uXOPOAm4gKnTXvYZUBiWoSON0Bxkkab5nzRhS jAmXFreJ0pHlvvvixsY0dfL6LtFVfwAWFsjMGqfEObwpWxPDgfsphUjF0Yi/fT4ZLNgn 4TiY09G+q/ovGbpb5dwLzNg3Xzkwj6L57nbzwGVoXIwD8rqtxDsAF9exhcuk1APrGtqa qkvL48iweCWs1B4qOWElzZ7Gp6SNEFaZswSITYBWae3KE6Stct2hGZfyec3u+grZ8m60 ldGYAqV1IkrC9Fh4eQC/wdSIn+CbrVyWq73MuOfsgUsYUz9EzvPIfgiLq69qknRXgXFl 2fCg== X-Gm-Message-State: AOAM530jA4gqMOqZn5N2k27FVUUJxS9FshhexYQtQdi7b6hSqXJeiANQ rTtD2OX+lbeGt6IEMXG3P39SUkXw69zQaw== X-Received: by 2002:a5d:43cb:: with SMTP id v11mr1044613wrr.188.1601289730189; Mon, 28 Sep 2020 03:42:10 -0700 (PDT) Received: from debian-brgl.home (lfbn-nic-1-68-20.w2-15.abo.wanadoo.fr. [2.15.159.20]) by smtp.gmail.com with ESMTPSA id f14sm939258wrt.53.2020.09.28.03.42.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 03:42:09 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Jonathan Corbet , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 9/9] gpio: mockup: refactor the module init function Date: Mon, 28 Sep 2020 12:41:55 +0200 Message-Id: <20200928104155.7385-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200928104155.7385-1-brgl@bgdev.pl> References: <20200928104155.7385-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski Let's move the code preparing the device properties into a separate routine. This has the advantage of simplifying the error handling and makes the indentation less deep. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpio-mockup.c | 96 +++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c index 47b7de6d5ab1..02eea05a09dd 100644 --- a/drivers/gpio/gpio-mockup.c +++ b/drivers/gpio/gpio-mockup.c @@ -503,16 +503,59 @@ static __init char **gpio_mockup_make_line_names(const char *label, return names; } -static int __init gpio_mockup_init(void) +static int __init gpio_mockup_register_chip(int idx) { struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; - int i, prop, num_chips, err = 0, base; struct platform_device_info pdevinfo; struct platform_device *pdev; + char **line_names = NULL; char chip_label[32]; - char **line_names; + int prop = 0, base; u16 ngpio; + memset(properties, 0, sizeof(properties)); + memset(&pdevinfo, 0, sizeof(pdevinfo)); + + snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A'); + properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label); + + base = gpio_mockup_range_base(idx); + if (base >= 0) + properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base); + + ngpio = base < 0 ? gpio_mockup_range_ngpio(idx) + : gpio_mockup_range_ngpio(idx) - base; + properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); + + if (gpio_mockup_named_lines) { + line_names = gpio_mockup_make_line_names(chip_label, ngpio); + if (!line_names) + return -ENOMEM; + + properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( + "gpio-line-names", line_names, ngpio); + } + + pdevinfo.name = "gpio-mockup"; + pdevinfo.id = idx; + pdevinfo.properties = properties; + + pdev = platform_device_register_full(&pdevinfo); + kfree_strarray(line_names, ngpio); + if (IS_ERR(pdev)) { + pr_err("error registering device"); + return PTR_ERR(pdev); + } + + gpio_mockup_pdevs[idx] = pdev; + + return 0; +} + +static int __init gpio_mockup_init(void) +{ + int i, num_chips, err; + if ((gpio_mockup_num_ranges < 2) || (gpio_mockup_num_ranges % 2) || (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) @@ -540,54 +583,13 @@ static int __init gpio_mockup_init(void) } for (i = 0; i < num_chips; i++) { - memset(properties, 0, sizeof(properties)); - memset(&pdevinfo, 0, sizeof(pdevinfo)); - prop = 0; - line_names = NULL; - - snprintf(chip_label, sizeof(chip_label), - "gpio-mockup-%c", i + 'A'); - properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", - chip_label); - - base = gpio_mockup_range_base(i); - if (base >= 0) - properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", - base); - - ngpio = base < 0 ? gpio_mockup_range_ngpio(i) - : gpio_mockup_range_ngpio(i) - base; - properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); - - if (gpio_mockup_named_lines) { - line_names = gpio_mockup_make_line_names(chip_label, - ngpio); - if (!line_names) { - platform_driver_unregister(&gpio_mockup_driver); - gpio_mockup_unregister_pdevs(); - return -ENOMEM; - } - - properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( - "gpio-line-names", - line_names, ngpio); - } - - pdevinfo.name = "gpio-mockup"; - pdevinfo.id = i; - pdevinfo.properties = properties; - - pdev = platform_device_register_full(&pdevinfo); - kfree_strarray(line_names, ngpio); - if (IS_ERR(pdev)) { - pr_err("error registering device"); + err = gpio_mockup_register_chip(i); + if (err) { platform_driver_unregister(&gpio_mockup_driver); gpio_mockup_unregister_pdevs(); debugfs_remove_recursive(gpio_mockup_dbg_dir); - return PTR_ERR(pdev); + return err; } - - gpio_mockup_pdevs[i] = pdev; } return 0; -- 2.26.1