From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 275CFC4338F for ; Thu, 19 Aug 2021 02:52:15 +0000 (UTC) Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D0296610FE for ; Thu, 19 Aug 2021 02:52:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D0296610FE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kernelnewbies.org Received: from localhost ([::1] helo=shelob.surriel.com) by shelob.surriel.com with esmtp (Exim 4.94.2) (envelope-from ) id 1mGYA5-0007Zm-NA; Wed, 18 Aug 2021 22:52:09 -0400 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by shelob.surriel.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1mGY95-0005Q2-6L for kernelnewbies@kernelnewbies.org; Wed, 18 Aug 2021 22:51:07 -0400 Received: by mail-ot1-x336.google.com with SMTP id r17-20020a0568302371b0290504f3f418fbso6795362oth.12 for ; Wed, 18 Aug 2021 19:51: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=qbI1pPRxf25pr2Ei2jsbhNXjyN2At2UDUcIa0sXZlcc=; b=jXCqHfVzezbg84KWtIvjU7ss5ey/Rscavur5r8e3DywfgBSXVBtUkY0NzxSGKM2Pzd TnKa5l+QAyOXOczLXa9CIxleWka6VNkPvFj5L345VIsLGeZu1b5gxa7BVTMnuR7LyrkN w0KVQeBbisl1VJD8oQqaB/lQLbQKSF1kENggJbztncIFrIwp1a38AIHtU8Q18KV8rIcV XMSrxER3QcZE/N2X/D3/An7Z5JaIvlU3rkR2wP5stZFxjYNqoLfhHjt0CG7vYHwFsqtn g0Zy81YI8T647dIpQSK2lZQMJqqjcO7D4MCpApxvKIUvdkKC9hGhpq4VUns8zAdoArIM qweA== 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=qbI1pPRxf25pr2Ei2jsbhNXjyN2At2UDUcIa0sXZlcc=; b=Myp0xGdWNMWzASLrJU/eDk4c0z0+ilASHtMsngzYA7AeQXCpT2Jcn0eHfH/TounkNn BG6zHZiZI/lNVh/eOdsLuKC6i9RCkbhm9C8QeErndZnWcTcAJbFOgoMK0aBc0HhnK9WJ KA/bR6KDHAeguY2y5HmB9YbUlUby/KHGKrtU74sH2WrE8PwRpCzYtdycpkds+yZ+5/JW AixUxkN0dFC/YCqpgRjiu6mWxbVRsVCgopNNXEKgrXdhGkMBrnGWivMpgzLd3AjDEqxf zpWetpwJpFAwbiKvJ56w9UlhOV+Bw6JiNKO2QkCYfATzl6zfoZHpc6Do2BQYwfScNIPn 5Bng== X-Gm-Message-State: AOAM533gBAQjXL+wpN1teMvIyuvwnl91MkZ0NyHYGNmnlmSPszpZiUD+ Zqc5Sq3ickbz3gnAd6of6Is= X-Google-Smtp-Source: ABdhPJxhwdZVhIqT/etxLmyLnab1ijJigyk+plkkQknuZwxkylxcmMbHRDdpygEecfT5B5vYKjBhPQ== X-Received: by 2002:a9d:560a:: with SMTP id e10mr9928797oti.219.1629341466512; Wed, 18 Aug 2021 19:51:06 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id w15sm156792oiw.19.2021.08.18.19.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Aug 2021 19:51:06 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Subject: [RFC PATCH v3 04/18] ledtrig-blkdev: Add misc. helper functions to blkdev LED trigger Date: Wed, 18 Aug 2021 21:50:39 -0500 Message-Id: <20210819025053.222710-5-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210819025053.222710-1-arequipeno@gmail.com> References: <20210819025053.222710-1-arequipeno@gmail.com> MIME-Version: 1.0 Cc: axboe@kernel.dk, kabel@kernel.org, linux-kernel@vger.kernel.org, pavel@ucw.cz, kernelnewbies@kernelnewbies.org X-BeenThere: kernelnewbies@kernelnewbies.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Learn about the Linux kernel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kernelnewbies-bounces@kernelnewbies.org Add various helper functions to the block device LED trigger: * blkdev_mkdir() - create a sysfs directory (and don't swallow error codes) * blkdev_streq(), blkdev_skip_space() & blkdev_find_space() - for parsing writes to sysfs attributes * blkdev_read_mode() & blkdev_write_mode() - LED mode activity type helpers Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index 28ccbd7946ba..fcae7ce63b92 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.c +++ b/drivers/leds/trigger/ledtrig-blkdev.c @@ -6,6 +6,7 @@ * Copyright 2021 Ian Pilcher */ +#include #include #include #include @@ -64,3 +65,76 @@ static unsigned int ledtrig_blkdev_count; /* How often to check for drive activity - in jiffies */ static unsigned int ledtrig_blkdev_interval; + + +/* + * + * Miscellaneous helper functions + * + */ + +/* Like kobject_create_and_add(), but doesn't swallow error codes */ +static struct kobject *blkdev_mkdir(const char *const name, + struct kobject *const parent) +{ + struct kobject *dir; + int ret; + + dir = kobject_create(); + if (dir == NULL) + return ERR_PTR(-ENOMEM); + + ret = kobject_add(dir, parent, "%s", name); + if (ret != 0) { + kobject_put(dir); + return ERR_PTR(ret); + } + + return dir; +} + +/* + * Compare a null-terminated C string with a non-null-terminated character + * sequence of a known length. Returns true if equal, false if not. + */ +static bool blkdev_streq(const char *const cstr, + const char *const cbuf, const size_t buf_len) +{ + return (strlen(cstr) == buf_len) && (memcmp(cstr, cbuf, buf_len) == 0); +} + +/* + * Returns a pointer to the first non-whitespace character in s + * (or a pointer to the terminating null). + */ +static const char *blkdev_skip_space(const char *s) +{ + while (*s != 0 && isspace(*s)) + ++s; + + return s; +} + +/* + * Returns a pointer to the first whitespace character in s (or a pointer to the + * terminating null), which is effectively a pointer to the position *after* the + * last character in the non-whitespace token at the beginning of s. (s is + * expected to be the result of a previous call to blkdev_skip_space()). + */ +static const char *blkdev_find_space(const char *s) +{ + while (*s != 0 && !isspace(*s)) + ++s; + + return s; +} + +static bool blkdev_read_mode(const enum ledtrig_blkdev_mode mode) +{ + return mode != LEDTRIG_BLKDEV_MODE_WO; +} + +static bool blkdev_write_mode(const enum ledtrig_blkdev_mode mode) +{ + return mode != LEDTRIG_BLKDEV_MODE_RO; +} -- 2.31.1 _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies