From patchwork Fri Sep 3 20:45:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506810 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 28ECCC4332F for ; Fri, 3 Sep 2021 20:46:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0E80F603E7 for ; Fri, 3 Sep 2021 20:46:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350253AbhICUq7 (ORCPT ); Fri, 3 Sep 2021 16:46:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235953AbhICUq7 (ORCPT ); Fri, 3 Sep 2021 16:46:59 -0400 Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB8C3C061575; Fri, 3 Sep 2021 13:45:58 -0700 (PDT) Received: by mail-oi1-x22f.google.com with SMTP id q39so648199oiw.12; Fri, 03 Sep 2021 13:45:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DcmHwQY+Etz5k7FUE3JqhXtdTnQXSGBqgITy4Qw50K4=; b=CuHn2SNGr+271aKjZ9ZM6ouKgChjkF5acCD6vX8d6uny0T+NL1Q8Jl52YDY3/Hlazi EcxHkiocTKZCq/QD39znsE+Yfbqvr/G4rphdsVv6Z5HbgRvLFcDBnFB7PfvElMcPk4Ec gq1iRuZ9HKgKBYOcWJoec1+Ll6yZssKPw7BGbtk1QK+wTOV5swMw0Yn+hlsyPJvYdsPw 8MfyfFvdXDHrtcqYAU4kqnEv5Z/FoFSfPFzWMxu7VeRhZFL9XdK8JK2VtTYUS/ZjC+CH XteBwfmfx8A0QiOc5qCzrPswZD7sySieoRtl3793/BOTzYE1OROimZ9jYjDXHK25WcWs ocqw== 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=DcmHwQY+Etz5k7FUE3JqhXtdTnQXSGBqgITy4Qw50K4=; b=Xx9WE90svnurBvFZEFPwHe+KkDyfotrbC9u7Z4uS0NvjFaPkmn+++mS28Ap4xUqdNA uqquCao9wPEypZJ3ch4j5fW2ViibgxJa6OpzrJBjGELhMpF46ngBK+Bmjwo3QzOs6SE7 TIHuS5WpWKr4Md8laNhkqV3Yr6Juh9E+IgMXRkUh5pUxWvl50dnywuRE+833t+QOVixB KyqB+WSDcMUxLF1plEtEC/1WJgPyi+e7C/bylLnCZgQSmw0tf0fGcqkog0edjoX607vC dabWHFLed2crN8yyH+FVca36P7onSJRlLM75UoA+2Nh39z27Wzi/S4y5eYI/zJ3JBsp7 PplA== X-Gm-Message-State: AOAM531D9dLk8T6B8OR8s5fLfaLMKcD8/liKB3FFHdvriBkTBdcprRpQ vSJWoFhI4GE3JiVQ662T8q8= X-Google-Smtp-Source: ABdhPJzKs8vv7VRGpciVIKeCNbKtdqvZ0tCV4n6/gjHjcQazfAzAybzFF0oFvkPHZQrtDcXAGTBbbA== X-Received: by 2002:aca:c481:: with SMTP id u123mr487740oif.23.1630701958251; Fri, 03 Sep 2021 13:45:58 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.45.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:45:57 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 01/18] docs: Add block device (blkdev) LED trigger documentation Date: Fri, 3 Sep 2021 15:45:31 -0500 Message-Id: <20210903204548.2745354-2-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Add Documentation/ABI/testing/sysfs-class-led-trigger-blkdev to document: * /sys/class/leds//blink_time * /sys/class/leds//interval * /sys/class/leds//mode * /sys/class/leds//add_blkdev * /sys/class/leds//delete_blkdev * /sys/class/leds//block_devices Add /sys/block//blkdev_leds to Documentation/ABI/testing/sysfs-block Add overview in Documentation/leds/ledtrig-blkdev.rst Signed-off-by: Ian Pilcher --- Documentation/ABI/testing/sysfs-block | 9 ++ .../testing/sysfs-class-led-trigger-blkdev | 48 ++++++ Documentation/leds/index.rst | 1 + Documentation/leds/ledtrig-blkdev.rst | 144 ++++++++++++++++++ 4 files changed, 202 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-blkdev create mode 100644 Documentation/leds/ledtrig-blkdev.rst diff --git a/Documentation/ABI/testing/sysfs-block b/Documentation/ABI/testing/sysfs-block index a0ed87386639..4d27e2d98091 100644 --- a/Documentation/ABI/testing/sysfs-block +++ b/Documentation/ABI/testing/sysfs-block @@ -328,3 +328,12 @@ Description: does not complete in this time then the block driver timeout handler is invoked. That timeout handler can decide to retry the request, to fail it or to start a device recovery strategy. + +What: /sys/block//blkdev_leds +Date: September 2021 +Contact: Ian Pilcher +Description: + Directory containing links to all LEDs that are associated + with this block device through the blkdev LED trigger. Only + present when at least one LED is associated. (See + Documentation/leds/ledtrig-blkdev.rst.) diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-blkdev b/Documentation/ABI/testing/sysfs-class-led-trigger-blkdev new file mode 100644 index 000000000000..514f23f9a72d --- /dev/null +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-blkdev @@ -0,0 +1,48 @@ +What: /sys/class/leds//blink_time +Date: September 2021 +Contact: Ian Pilcher +Description: + Time (in milliseconds) that the LED will be on during a single + "blink". + +What: /sys/class/leds//interval +Date: September 2021 +Contact: Ian Pilcher +Description: + Frequency (in milliseconds) with which block devices associated + with the blkdev LED trigger will be checked for activity. + + NOTE that this attribute is a global setting. All changes + apply to all LEDs associated with the blkdev LED trigger. + +What: /sys/class/leds//mode +Date: September 2021 +Contact: Ian Pilcher +Description: + Type of events for which LED will blink - read, write, + or rw (both). Note that any activity that changes the state of + the device's non-volatile storage (including discards and cache + flushes) is considered to be a write. + +What: /sys/class/leds//add_blkdev +Date: September 2021 +Contact: Ian Pilcher +Description: + Associate a block device with this LED by writing its kernel + name (as shown in /sys/block) to this attribute. Multiple + device names may be written at once, separated by whitespace. + +What: /sys/class/leds//delete_blkdev +Date: September 2021 +Contact: Ian Pilcher +Description: + Remove the association between this LED and a block device by + writing the device's kernel name to this attribute. Multiple + device names may be written at once, separated by whitespace. + +What: /sys/class/leds//block_devices +Date: September 2021 +Contact: Ian Pilcher +Description: + Directory containing links to all block devices that are + associated with this LED. diff --git a/Documentation/leds/index.rst b/Documentation/leds/index.rst index e5d63b940045..e3c24e468cbc 100644 --- a/Documentation/leds/index.rst +++ b/Documentation/leds/index.rst @@ -10,6 +10,7 @@ LEDs leds-class leds-class-flash leds-class-multicolor + ledtrig-blkdev ledtrig-oneshot ledtrig-transient ledtrig-usbport diff --git a/Documentation/leds/ledtrig-blkdev.rst b/Documentation/leds/ledtrig-blkdev.rst new file mode 100644 index 000000000000..1a2b6c406258 --- /dev/null +++ b/Documentation/leds/ledtrig-blkdev.rst @@ -0,0 +1,144 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================= +Block Device (blkdev) LED Trigger +================================= + +Available when ``CONFIG_LEDS_TRIGGER_BLKDEV=y`` or +``CONFIG_LEDS_TRIGGER_BLKDEV=m``. + +See also: + +* ``Documentation/ABI/testing/sysfs-class-led-trigger-blkdev`` +* ``Documentation/ABI/testing/sysfs-block`` (``/sys/block//leds``) + +Overview +======== + +.. note:: + The examples below use ```` to refer to the name of a + system-specific LED. If no suitable LED is available on a test + system (in a virtual machine, for example), it is possible to + use a userspace LED. (See ``Documentation/leds/uleds.rst``.) + +Verify that the ``blkdev`` LED trigger is available:: + + # grep blkdev /sys/class/leds//trigger + ... rfkill-none blkdev + +(If the previous command produces no output, you may need to load the trigger +module - ``modprobe ledtrig_blkdev``. If the module is not available, check +the value of ``CONFIG_LEDS_TRIGGER_BLKDEV`` in your kernel configuration.) + +Associate the LED with the ``blkdev`` LED trigger:: + + # echo blkdev > /sys/class/leds//trigger + + # cat /sys/class/leds//trigger + ... rfkill-none [blkdev] + +Note that several new device attributes are available in the +``/sys/class/leds/`` directory. + +* ``add_blkdev`` and ``delete_blkdev`` are used to associate block devices with + this LED, and to remove associations. + +* ``mode`` is used to control the type of device activity that will cause this + LED to blink - read activity, write activity, or both. (Note that any + activity that changes the state of a device's non-volatile storage is + considered to be a write. This includes discard and cache flush requests.) + +* ``blink_time`` is the duration (in milliseconds) of each blink of this LED. + +* ``interval`` is the frequency (in milliseconds) with which devices are checked + for activity. (Note that this is a global setting. Any change affects all + LEDs associated with the ``blkdev`` trigger.) + +* The ``block_devices`` directory will contain a symbolic link to every device + that is associated with this LED. + +Associate the LED with the block device:: + + # echo sda > /sys/class/leds//add_blkdev + + # ls /sys/class/leds//block_devices + sda + +Reads and write activity on the device should cause the LED to blink. The +duration of each blink (in milliseconds) can be adjusted by setting +``/sys/class/leds//blink_on``, and the minimum delay between blinks can +be set via ``/sys/class/leds//blink_off``. + +Associate a second device with the LED:: + + # echo sdb > /sys/class/leds//add_blkdev + + # ls /sys/class/leds//block_devices + sda sdb + +When a block device is associated with one or more LEDs, the LEDs are linked +from the device's ``blkdev_leds`` directory:: + + # ls /sys/block/sd{a,b}/blkdev_leds + /sys/block/sda/blkdev_leds: + + + /sys/block/sdb/blkdev_leds: + + +(The ``blkdev_leds`` directory only exists when the block device is associated +with at least one LED.) + +The ``add_blkdev`` and ``delete_blkdev`` attributes both accept multiple, +whitespace separated, devices. For example:: + + # echo sda sdb > /sys/class/leds//delete_blkdev + + # ls /sys/class/leds//block_devices + +``interval`` and ``blink_time`` +=============================== + +* The ``interval`` attribute is a global setting. Changing the value via + ``/sys/class/leds//interval`` will affect all LEDs associated with + the ``blkdev`` LED trigger. + +* All associated devices are checked for activity every ``interval`` + milliseconds, and a blink is triggered on appropriate LEDs. The duration + of an LED's blink is determined by its ``blink_time`` attribute (also in + milliseconds). Thus (assuming that activity of the relevant type has occurred + on one of an LED's associated devices), the LED will be on for ``blink_time`` + milliseconds and off for ``interval - blink_time`` milliseconds. + +* The LED subsystem ignores new blink requests for an LED that is currently in + in the process of blinking, so setting a ``blink_time`` greater than or equal + to ``interval`` will cause some blinks to be dropped. + +* Because of processing times, scheduling latencies, etc., avoiding missed + blinks actually requires a difference of at least a few milliseconds between + the ``blink_time`` and ``interval``. The required difference is likely to + vary from system to system. As a reference, a Thecus N5550 NAS requires a + difference of 7 milliseconds (``interval == 100``, ``blink_time == 93``). + +* The default values (``interval == 100``, ``blink_time == 75``) cause the LED + associated with a continuously active device to blink rapidly. For a more + "constantly on" effect, increase the ``blink_time`` (but not too much; see + the previous bullet). + +Other Notes +=========== + +* Many (possibly all) types of block devices work with this trigger, including: + + * SCSI (including SATA and USB) hard disk drives and SSDs + * SCSI (including SATA and USB) optical drives + * NVMe SSDs + * SD cards + * loopback block devices (``/dev/loop*``) + * device mapper devices, such as LVM logical volumes + * MD RAID devices + * zRAM compressed RAM-disks + +* The ``blkdev`` LED trigger supports many-to-many device/LED associations. + A device can be associated with multiple LEDs, and an LED can be associated + with multiple devices. From patchwork Fri Sep 3 20:45:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506809 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 3E87EC433FE for ; Fri, 3 Sep 2021 20:46:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2B2F861100 for ; Fri, 3 Sep 2021 20:46:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350991AbhICUrB (ORCPT ); Fri, 3 Sep 2021 16:47:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235953AbhICUrB (ORCPT ); Fri, 3 Sep 2021 16:47:01 -0400 Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13400C0613C1; Fri, 3 Sep 2021 13:46:01 -0700 (PDT) Received: by mail-oi1-x234.google.com with SMTP id r26so694867oij.2; Fri, 03 Sep 2021 13:46:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5IvB2ie/eR/p0ZXjKpNOoLs51YY8X2DxXyaLyjEGU5s=; b=YCZigrz5HRASifSFQmQ+iyFnfxeRbHvnj0TYLkOUsnLAgDERauNK3mRpQShZPB1WRv u9aAgchJLl8OR2iKIKE7OH723cVvMZDcmpkyISJOq/+zQ6Qf91Bl3PZYI34ddeJsc0y2 guZanR9DWcpSUb7UIeavVf/KTZL3Hvejxi3GNkG9ePUUM7kjEvraaK6e4AWXVEBaijD/ Gr1/xdDCJulIGhuUXCqOI2rwrFjNr0uQQqowhO8CaI3orm6Nh5IjG2GPm2n6HgUvJTgX c2VxrtOtW34F1P0TpLdwaH4diZNTASpcKudNh5je80YIFwLl2EiskxJahNFxVooUkAyR Ve5Q== 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=5IvB2ie/eR/p0ZXjKpNOoLs51YY8X2DxXyaLyjEGU5s=; b=c4lFiXNH1ag0Mth+HBtiLTikYwH01HUjR33/cgBKdG0JjcCjT98C/Hm2PRu8r06OSL SkwqPmwumpPx6sV6UejkouYSIdOgObCLSxbvmQsLH64VkDgVoifJbctkcLpQyO6gjtgo bwSqTnSOFyMOkwNbU8reiagt6ZJXyZIHoOynchQ2NbHbFj8PNVAKr6k6LMzNmBEdnqn4 gO5uLXsSPoAyl7dkoDVKqJhlZJjB2VXJZa3iuRQTAAOhAGE1NHVUkh9Fx9ei9JGFH7Rh bGha+NV9LScsEDyTbFGjF/dBshYMhNV7C8i5B+DJy7SFccbeBmbyYwzMkfaRkpobn7MY xhog== X-Gm-Message-State: AOAM533fEOF7Jy2DJsVNFHLMuEPGt7CM/dTv71njE7dLexHyy1rWJhRm hLZ0QvF37eoKC4GRdVvf/jw= X-Google-Smtp-Source: ABdhPJyJAaZc/kL+e5qS7AY/S/FKJ4jwSG2ea+uz4iv66JyQAH/tmA+Tr1Nu31C9f2usbgrQhFtmFw== X-Received: by 2002:aca:f143:: with SMTP id p64mr485284oih.161.1630701960452; Fri, 03 Sep 2021 13:46:00 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.45.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:00 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 04/18] block: Add block device LED trigger integrations Date: Fri, 3 Sep 2021 15:45:34 -0500 Message-Id: <20210903204548.2745354-5-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Add LED trigger disk info pointer to gendisk structure Call ledtrig_blkdev_disk_init() from device_add_disk() to ensure that ledtrig is initialized to NULL, in case a driver allocates the structure itself and doesn't use kzalloc() Call ledtrig_blkdev_disk_cleanup() from del_gendisk() to ensure that the LED trigger stops trying to check the disk for activity Signed-off-by: Ian Pilcher --- block/genhd.c | 4 ++++ include/linux/genhd.h | 3 +++ 2 files changed, 7 insertions(+) diff --git a/block/genhd.c b/block/genhd.c index 567549a011d1..6f340a717099 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "blk.h" @@ -390,6 +391,8 @@ int device_add_disk(struct device *parent, struct gendisk *disk, struct device *ddev = disk_to_dev(disk); int ret; + ledtrig_blkdev_disk_init(disk); + /* * The disk queue should now be all set with enough information about * the device for the elevator code to pick an adequate default @@ -559,6 +562,7 @@ void del_gendisk(struct gendisk *disk) if (WARN_ON_ONCE(!disk_live(disk) && !(disk->flags & GENHD_FL_HIDDEN))) return; + ledtrig_blkdev_disk_cleanup(disk); blk_integrity_del(disk); disk_del_events(disk); diff --git a/include/linux/genhd.h b/include/linux/genhd.h index c68d83c87f83..29039367ccad 100644 --- a/include/linux/genhd.h +++ b/include/linux/genhd.h @@ -166,6 +166,9 @@ struct gendisk { #endif /* CONFIG_BLK_DEV_INTEGRITY */ #if IS_ENABLED(CONFIG_CDROM) struct cdrom_device_info *cdi; +#endif +#if IS_ENABLED(CONFIG_LEDS_TRIGGER_BLKDEV) + struct ledtrig_blkdev_disk *ledtrig; #endif int node_id; struct badblocks *bb; From patchwork Fri Sep 3 20:45:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506808 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 4204BC433FE for ; Fri, 3 Sep 2021 20:46:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 148B461107 for ; Fri, 3 Sep 2021 20:46:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351017AbhICUrE (ORCPT ); Fri, 3 Sep 2021 16:47:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235953AbhICUrD (ORCPT ); Fri, 3 Sep 2021 16:47:03 -0400 Received: from mail-oo1-xc31.google.com (mail-oo1-xc31.google.com [IPv6:2607:f8b0:4864:20::c31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD5EDC061575; Fri, 3 Sep 2021 13:46:02 -0700 (PDT) Received: by mail-oo1-xc31.google.com with SMTP id g4-20020a4ab044000000b002900bf3b03fso63076oon.1; Fri, 03 Sep 2021 13:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kS1HWDTxQaaD17dKdhb3nU8krX8n75We9gBrrNQ1qbE=; b=oioz021xv2ej7NcA2SOVVwCjRuLLSQVyectt4KHmHW/nuu7y1WCA3RLVJ4qykyP6a4 jAIMmt4UC+DDOpFA8u4OpbEF8SKo7/keaC8a4pL++d0O6zBigoSV4vWrxwv+nL4iziK0 jLtrsGhKM8EPxQECl0wR1Qg0ZNonBDSvz2kJ1JWW2dA9RWNCgN8UfMg8JIJ9XrPqIVR9 yLdlwrVNI+V/tmsYAayAVZGZDQAPneCwdYJ70lUINxl1W5m6bMP63ep5WJeXAJUfvbSG I7Pydt53kFazIG+RcLk1jWZn+xsZh5RfEJyYT1fkpJyHuGl4BuVpfF3Td/xaSp2Nk1PC 9VLA== 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=kS1HWDTxQaaD17dKdhb3nU8krX8n75We9gBrrNQ1qbE=; b=rDa45KmHm2dDRT4usxpIzD5L6G9zK5HLfnyjchTK0zPG6fmLqNyb6baFuUsKVVcc0S pbTg+zyetgz4O7dMQrWwUA1ZdYKV5KK6yjrUpEO9ClVM6im0CM3SyyOpUD2aJXoL38Pq MWN1MXDEM4yUaWv/cFAJvFBw+fdABtL06OCYscNosqF3kV9gkHnLN7A6bkV7pcosQuYP 4sZ/YERDi58RJyQ3OoOavwECb1rgrtsMdFtM26Y8TXKG+d9U/4Sk1ratwNg1La8vzl32 nym5E7lrkThvIoHxQy56ifPu39c7TOHBhRJ+WisSCSiZNftTwj24Zp0UUOKfRqav1/sY ilkg== X-Gm-Message-State: AOAM531HIJFO23sHJAKTx150X8R35bTOBItdawd6YDJQBEuDXuf9Vtt0 gDOmloP8po8W86X9SBhPHZI= X-Google-Smtp-Source: ABdhPJxJ2w7ihwZfZKubkK4VZF57fshAKP3cb239ZeVxjjjFBqyydT7e7ThwgGHec0XZRsq45Em1hA== X-Received: by 2002:a4a:c88e:: with SMTP id t14mr4565635ooq.77.1630701962156; Fri, 03 Sep 2021 13:46:02 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:01 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 06/18] ledtrig-blkdev: Add function to get gendisk by name Date: Fri, 3 Sep 2021 15:45:36 -0500 Message-Id: <20210903204548.2745354-7-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Add ledtrig_blkdev_get_disk() to find block device by name and increment its reference count. (Caller must call put_disk().) Must be built in to access block_class and disk_type symbols. Add (inline) helper function to compare C string with non-terminated character sequence (within the buffer passed to a sysfs attribute store function). Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev-core.c | 43 ++++++++++++++++++++++ drivers/leds/trigger/ledtrig-blkdev.h | 12 ++++++ 2 files changed, 55 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev-core.c b/drivers/leds/trigger/ledtrig-blkdev-core.c index 102cdbe26d66..87d439f425ad 100644 --- a/drivers/leds/trigger/ledtrig-blkdev-core.c +++ b/drivers/leds/trigger/ledtrig-blkdev-core.c @@ -33,3 +33,46 @@ void ledtrig_blkdev_disk_cleanup(struct gendisk *const gd) mutex_unlock(&ledtrig_blkdev_mutex); } + + +/* + * + * ledtrig_blkdev_get_disk() - get a gendisk by name + * + * Must be built in for access to block_class and disk_type + * Caller must call put_disk() + * + */ + +/* Non-null-terminated character sequence of known length */ +struct ledtrig_blkdev_gdname { + const char *buf; + size_t len; +}; + +/* Match function for ledtrig_blkdev_get_disk() */ +static int blkdev_match_gdname(struct device *const dev, const void *const data) +{ + const struct ledtrig_blkdev_gdname *const gdname = data; + + if (dev->type != &disk_type) + return 0; + + return ledtrig_blkdev_streq(dev_to_disk(dev)->disk_name, + gdname->buf, gdname->len); +} + +struct gendisk *ledtrig_blkdev_get_disk(const char *const name, + const size_t len) +{ + const struct ledtrig_blkdev_gdname gdname = { .buf = name, .len = len }; + struct device *dev; + + dev = class_find_device(&block_class, NULL, + &gdname, blkdev_match_gdname); + if (dev == NULL) + return NULL; + + return dev_to_disk(dev); +} +EXPORT_SYMBOL_NS_GPL(ledtrig_blkdev_get_disk, LEDTRIG_BLKDEV); diff --git a/drivers/leds/trigger/ledtrig-blkdev.h b/drivers/leds/trigger/ledtrig-blkdev.h index 914fb1523a2f..9a3528fd183a 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.h +++ b/drivers/leds/trigger/ledtrig-blkdev.h @@ -11,5 +11,17 @@ extern struct mutex ledtrig_blkdev_mutex; extern void (*__ledtrig_blkdev_disk_cleanup)(struct gendisk *gd); +extern struct gendisk *ledtrig_blkdev_get_disk(const char *name, size_t len); + +/* + * Compare a null-terminated C string with a non-null-terminated character + * sequence of a known length. Returns true (1) if equal, false (0) if not. + */ +static inline bool ledtrig_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; +} #endif /* __LEDTRIG_BLKDEV_H */ From patchwork Fri Sep 3 20:45:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506807 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 EFBC9C433F5 for ; Fri, 3 Sep 2021 20:46:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D9F60610E7 for ; Fri, 3 Sep 2021 20:46:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351040AbhICUrL (ORCPT ); Fri, 3 Sep 2021 16:47:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351022AbhICUrF (ORCPT ); Fri, 3 Sep 2021 16:47:05 -0400 Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65375C0613C1; Fri, 3 Sep 2021 13:46:04 -0700 (PDT) Received: by mail-oi1-x230.google.com with SMTP id c79so656474oib.11; Fri, 03 Sep 2021 13:46:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=joEWcX6/HEUX6AMOsJywxpKlr4Qh1MnJ1mIBW/70C7k=; b=QYAg4F18BQA7/3t5hS8ay0LIQPKY/u01Nfj4iYRBkgqNQ3Q1HFeWji/M967vnZC8Ir XSyBY/F9+3JqANS7L8/eqIcAGdDO5U0Uvkjwazr4EFt2odnpvqA2eCtssc4c2f4//ZGx 4IzOCytiwDnsmKgeqOG1WB9tkJX0GCIykEtaXl/KV1mj1u6Q90agDtkpkmpnTAG25mUw bCoJtNqsK1mf8ul/OETFzaTsH8HM4+3g1/QP4m4klZMeUiIXd9IcWUGYUzJD+xeo2N9e pxFzfRqphlSL3jrPG5kCMeWrVpCgbWmiQIyOloZR+i2ZXGreLjhWhJyhPCQIvFfVAUnE 3k/w== 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=joEWcX6/HEUX6AMOsJywxpKlr4Qh1MnJ1mIBW/70C7k=; b=cXImG2DNkMpy3v+JzxMO4ef7Mc+bQ8l96sJ7WSCZuQdwu9XGmATU+Cua77xriV5EEd vpZqOS1NT752kBcuvCAH6QdVQ4qwLntoZLuB6HsUzsIkC7VWBF7zsrlJAR4K38pRH6E2 0cUwGImgAweaDLBcNVHmL1PznMJowLNddUvIqzZwjgetl4R9FUQee0rQDgV4YxzvSZGi ShqNKinXWrbMAl1Mixj8yCKYDxd2L8+nMv0H0IWYgwozff5QRswM4m+vQb9Wm24s501V FULRTmktaSZwxYFkLfSWd8YRan3HoXoBt2hRzxrkZDMdzzaQVwTbPb5ibeC/BSO4W6Tb m7sw== X-Gm-Message-State: AOAM532THFQifNwuv/X+WYy9jIWvnD7H+d0qr+drrtEJUDyjjZq2eTRA j+AMzqAB/97GAzpRpsROzQEQGRVYcVz+yQ== X-Google-Smtp-Source: ABdhPJy4oIpkbxu9uwa51ya0ql9PQJPSRy8hG6tU29R02/9VKnIEQ72IWIfWfrHWpG2aC7H55Bbzvg== X-Received: by 2002:aca:ad55:: with SMTP id w82mr510224oie.45.1630701963808; Fri, 03 Sep 2021 13:46:03 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:03 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 08/18] ledtrig-blkdev: Add miscellaneous helper functions Date: Fri, 3 Sep 2021 15:45:38 -0500 Message-Id: <20210903204548.2745354-9-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Add blkdev_skip_space() and blkdev_find_space() for parsing writes to sysfs attributes Add blkdev_read_mode() and blkdev_write_mode() LED comparison helpers Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index db8326874400..1f319529c3be 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 "ledtrig-blkdev.h" @@ -66,3 +67,46 @@ static unsigned int ledtrig_blkdev_count; /* How often to check for drive activity - in jiffies */ static unsigned int ledtrig_blkdev_interval; + + +/* + * + * Miscellaneous helper functions + * + */ + +/* + * 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; +} From patchwork Fri Sep 3 20:45:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506806 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 91BEAC43217 for ; Fri, 3 Sep 2021 20:46:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7C86360C3E for ; Fri, 3 Sep 2021 20:46:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351053AbhICUrO (ORCPT ); Fri, 3 Sep 2021 16:47:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351041AbhICUrM (ORCPT ); Fri, 3 Sep 2021 16:47:12 -0400 Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 349C6C0617A8; Fri, 3 Sep 2021 13:46:06 -0700 (PDT) Received: by mail-oi1-x229.google.com with SMTP id s20so695702oiw.3; Fri, 03 Sep 2021 13:46:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Z3IpWY6hC6vHRC5+LJLa1BK+7bWdECMM9FiUHKFu/VA=; b=oqSzxLsNghPexCOOhno0g6Lv//8JbqpywNeCIiF8cuseSEpzr0W9szJr+630j/2fYR 9zqIrXOnlO6RdDm4UZ8WS/WasTNlesC2ei//QCBBmim00iC/uYS6cMsXm2fb86N5SEgR HP1otL7KshsSzgFSyZzi4zVfJZEJUuuZZh5sbtVl+AQA2GUW2wPmU4qFmMo96csesr+F ZD6eyglZsOLN1I53gKx6mPYSqHsomL0YYAF1Vwsg1n/Iv2v0yOe/HOvQc/bIasGQbvT4 z0hsl9q1WgNy2RLNrIWKWfwsNtlsDiM2Vi9Rh3lzrTNo8VanjloF0JLQ+Juw3D2AYdo9 zRQw== 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=Z3IpWY6hC6vHRC5+LJLa1BK+7bWdECMM9FiUHKFu/VA=; b=rgb5Zn+3Q8P90f77tS6bP/sH/1NvIMVegsyvhl0wQwprgoQegtP/6jyOZo+HkERA5V 08WcRLriPpnzILLt7NCf8/R/EtCiy5FUPHyzNrmXcsdcNiRIZGYYfGvq7u0hRmrSo8zt a/0U4gBOVkf6K0uvx1SOfSSRUys6WhbdkoiUbpX89wBgSy6Dgq6J4Jpcyy9BMsoY3KiQ +Aziupt96tUby0T0+i04NYnPK2p2sAJEp3dz007E8hYO52LUk3pV+e11Esdbd7eLoHzh jtUXctxzko33bczG8HOl0ggBarbkz6I3GkoebdRydLn0wwDjPHiULBRqM02pE4ExLCaA /N8w== X-Gm-Message-State: AOAM532BpKoZm5rAwu1cPZtc00OaRr9dt3V3BQxTwFG6zUtEljgx/oTo 6ioPePqu0SSmWIYeXbFRwo6lRDEXBFr0Nw== X-Google-Smtp-Source: ABdhPJzLcOQXa3N/OD1cL1ho5sT+kfch84I/CVG+3Ubt8gmtcXkolDXBVTkHKmVWaRUQqQwbUp9Sng== X-Received: by 2002:a05:6808:5c7:: with SMTP id d7mr525910oij.147.1630701965608; Fri, 03 Sep 2021 13:46:05 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:05 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 10/18] ledtrig-blkdev: Add function to associate the trigger with an LED Date: Fri, 3 Sep 2021 15:45:40 -0500 Message-Id: <20210903204548.2745354-11-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Allocate per-LED data structure and initialize with default values Create /sys/class/leds//block_devices directory Increment module reference count. Module can only be removed when no LEDs are associated with the trigger. Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index 37ba9bb3542e..bbf71ff18057 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.c +++ b/drivers/leds/trigger/ledtrig-blkdev.c @@ -198,3 +198,57 @@ static void blkdev_process(struct work_struct *const work) delay = READ_ONCE(ledtrig_blkdev_interval); WARN_ON_ONCE(!schedule_delayed_work(&ledtrig_blkdev_work, delay)); } + + +/* + * + * Associate an LED with the blkdev trigger + * + */ + +static int blkdev_activate(struct led_classdev *const led_dev) +{ + struct ledtrig_blkdev_led *led; + int ret; + + if (WARN_ON(!try_module_get(THIS_MODULE))) { + ret = -ENODEV; /* -ESHOULDNEVERHAPPEN */ + goto exit_return; + } + + led = kmalloc(sizeof(*led), GFP_KERNEL); + if (led == NULL) { + ret = -ENOMEM; + goto exit_put_module; + } + + led->led_dev = led_dev; + led->blink_msec = LEDTRIG_BLKDEV_BLINK_MSEC; + led->mode = LEDTRIG_BLKDEV_MODE_RW; + INIT_HLIST_HEAD(&led->disks); + + ret = mutex_lock_interruptible(&ledtrig_blkdev_mutex); + if (ret != 0) + goto exit_free; + + led->dir = kobject_create_and_add("block_devices", &led_dev->dev->kobj); + if (led->dir == NULL) { + ret = -ENOMEM; + goto exit_unlock; + } + + hlist_add_head(&led->leds_node, &ledtrig_blkdev_leds); + led_set_trigger_data(led_dev, led); + ret = 0; + +exit_unlock: + mutex_unlock(&ledtrig_blkdev_mutex); +exit_free: + if (ret != 0) + kfree(led); +exit_put_module: + if (ret != 0) + module_put(THIS_MODULE); +exit_return: + return ret; +} From patchwork Fri Sep 3 20:45:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506805 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 6031EC4167B for ; Fri, 3 Sep 2021 20:46:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EEEB60C3E for ; Fri, 3 Sep 2021 20:46:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351044AbhICUrO (ORCPT ); Fri, 3 Sep 2021 16:47:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351022AbhICUrN (ORCPT ); Fri, 3 Sep 2021 16:47:13 -0400 Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5080C0617AE; Fri, 3 Sep 2021 13:46:06 -0700 (PDT) Received: by mail-ot1-x32c.google.com with SMTP id c42-20020a05683034aa00b0051f4b99c40cso586676otu.0; Fri, 03 Sep 2021 13:46:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4JcwdMYWeD2CXgYcF0/13R+BnPwvx/9AVWBS8bvHNaQ=; b=PpgxQSF94zkf0qRn6Mje6Huh9Yn9925qauW8zOcEJstvxh54PCCy7AmCCE5szZsg1p VvIb3cm8veewaZyp0Nq4rvT0aZsR3nqfMIG2Y06UhtO/XVf1uVNhjP0wXF1apbrp+n70 2cg8P4bCdgh99UYlEy4qXUAcnFGzqNaoVaEDVx3T2QCfQi9++fI6a4RNpJ5ua/rzC1f7 UH23J9B99uo7/0UQkN37IwhUY3SNC2oewJzWR5HDaYsyaI2HP/eJ86/7a/tcGP0OfUMy PoBFV+i1zUtkhV/58xpub8yluKeEAThpjfNvc4MjFypZGaS+NhZGgtmdKUXgJQppQX6j Y1sw== 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=4JcwdMYWeD2CXgYcF0/13R+BnPwvx/9AVWBS8bvHNaQ=; b=sMWn6DIpUghZ2iyDAVUurD57EYqr969fOAgGq2bZXd5ABScQHYZb2oDjtR+G0ABkNf evMXT0V6slA21sNYm9gtoKtkGTHMSDo1Dpo/Aio4srxgTX6/XpmV/KF7hnPh5/ObrqFi fckr8KfGnpJonymGmpJzP8lP8zuxbTQfCeTxBL/qTXbnX212Fi0oiLFFWMWEeNncWhIH B45YlIXfhbTzfYHG6YFuK7kG/+K0gJIVS1SnjJ8DIEtVMwz/ZVTRvXFmqMnR9HASN68V rMaFHqLvJ9Q/dvq7tV1NMLveDLbl3H2AlRiCnLjRyUPWDKwtVZ5YZLwin1pD3emgo4+Q oWVA== X-Gm-Message-State: AOAM533qxbDjyKgL7aKfqo71SYYbugfYcSgeR9TtYtt7GkcDOC96AiB3 BIZeyOXzF1x6BBV6fW8s/j3dJXEhpjhUAA== X-Google-Smtp-Source: ABdhPJwh6nMceHqJGvwdRnRQLbBsaWAgwmCDT9u6C6ZgExvmV0SGx7O8uwXtP00DUt8DkgcRRKAtNA== X-Received: by 2002:a05:6830:1f0a:: with SMTP id u10mr822351otg.53.1630701966339; Fri, 03 Sep 2021 13:46:06 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:05 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 11/18] ledtrig-blkdev: Add function to associate a device with an LED Date: Fri, 3 Sep 2021 15:45:41 -0500 Message-Id: <20210903204548.2745354-12-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org If this is the first LED associated with the device, create the /sys/block//blkdev_leds directory. Otherwise, increment its reference count. Create symlinks in /sys/class/leds//block_devices and /sys/block//blkdev_leds If this the first device associated with any LED, schedule delayed work to periodically check associated devices and blink LEDs Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 157 ++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index bbf71ff18057..e3169d1f0e38 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.c +++ b/drivers/leds/trigger/ledtrig-blkdev.c @@ -252,3 +252,160 @@ static int blkdev_activate(struct led_classdev *const led_dev) exit_return: return ret; } + + +/* + * + * Associate a block device with an LED + * + */ + +/* Gets or allocs & initializes the blkdev disk for a gendisk */ +static int blkdev_get_disk(struct gendisk *const gd) +{ + struct ledtrig_blkdev_disk *disk; + struct kobject *dir; + + if (gd->ledtrig != NULL) { + kobject_get(gd->ledtrig->dir); + return 0; + } + + disk = kmalloc(sizeof(*disk), GFP_KERNEL); + if (disk == NULL) + return -ENOMEM; + + dir = kobject_create_and_add("blkdev_leds", &disk_to_dev(gd)->kobj); + if (dir == NULL) { + kfree(disk); + return -ENOMEM; + } + + INIT_HLIST_HEAD(&disk->leds); + disk->gd = gd; + disk->dir = dir; + disk->read_ios = 0; + disk->write_ios = 0; + + gd->ledtrig = disk; + + return 0; +} + +static void blkdev_put_disk(struct ledtrig_blkdev_disk *const disk) +{ + kobject_put(disk->dir); + + if (hlist_empty(&disk->leds)) { + disk->gd->ledtrig = NULL; + kfree(disk); + } +} + +static int blkdev_disk_add_locked(struct ledtrig_blkdev_led *const led, + struct gendisk *const gd) +{ + struct ledtrig_blkdev_link *link; + struct ledtrig_blkdev_disk *disk; + unsigned long delay; + int ret; + + link = kmalloc(sizeof(*link), GFP_KERNEL); + if (link == NULL) { + ret = -ENOMEM; + goto error_return; + } + + ret = blkdev_get_disk(gd); + if (ret != 0) + goto error_free_link; + + disk = gd->ledtrig; + + ret = sysfs_create_link(disk->dir, &led->led_dev->dev->kobj, + led->led_dev->name); + if (ret != 0) + goto error_put_disk; + + ret = sysfs_create_link(led->dir, &disk_to_dev(gd)->kobj, + gd->disk_name); + if (ret != 0) + goto error_remove_link; + + link->disk = disk; + link->led = led; + hlist_add_head(&link->led_disks_node, &led->disks); + hlist_add_head(&link->disk_leds_node, &disk->leds); + + if (ledtrig_blkdev_count == 0) { + delay = READ_ONCE(ledtrig_blkdev_interval); + WARN_ON(!schedule_delayed_work(&ledtrig_blkdev_work, delay)); + } + + ++ledtrig_blkdev_count; + + return 0; + +error_remove_link: + sysfs_remove_link(disk->dir, led->led_dev->name); +error_put_disk: + blkdev_put_disk(disk); +error_free_link: + kfree(link); +error_return: + return ret; +} + +static bool blkdev_already_linked(const struct ledtrig_blkdev_led *const led, + const struct gendisk *const gd) +{ + const struct ledtrig_blkdev_link *link; + + if (gd->ledtrig == NULL) + return false; + + hlist_for_each_entry(link, &gd->ledtrig->leds, disk_leds_node) { + + if (link->led == led) { + pr_info("blkdev LED: %s already associated with %s\n", + gd->disk_name, led->led_dev->name); + return true; + } + } + + return false; +} + +static int blkdev_disk_add(struct ledtrig_blkdev_led *const led, + const char *const disk_name, const size_t name_len) +{ + struct gendisk *gd; + int ret; + + ret = mutex_lock_interruptible(&ledtrig_blkdev_mutex); + if (ret != 0) + goto exit_return; + + gd = ledtrig_blkdev_get_disk(disk_name, name_len); + if (gd == NULL) { + pr_info("blkdev LED: no such block device %.*s\n", + (int)name_len, disk_name); + ret = -ENODEV; + goto exit_unlock; + } + + if (blkdev_already_linked(led, gd)) { + ret = -EEXIST; + goto exit_put_disk; + } + + ret = blkdev_disk_add_locked(led, gd); + +exit_put_disk: + if (ret != 0) + put_disk(gd); +exit_unlock: + mutex_unlock(&ledtrig_blkdev_mutex); +exit_return: + return ret; +} From patchwork Fri Sep 3 20:45:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506803 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 318A7C43217 for ; Fri, 3 Sep 2021 20:46:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1D93860041 for ; Fri, 3 Sep 2021 20:46:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351038AbhICUrQ (ORCPT ); Fri, 3 Sep 2021 16:47:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351033AbhICUrN (ORCPT ); Fri, 3 Sep 2021 16:47:13 -0400 Received: from mail-oo1-xc31.google.com (mail-oo1-xc31.google.com [IPv6:2607:f8b0:4864:20::c31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CC78C06129E; Fri, 3 Sep 2021 13:46:11 -0700 (PDT) Received: by mail-oo1-xc31.google.com with SMTP id m14-20020a4ad50e000000b002912a944a47so53560oos.12; Fri, 03 Sep 2021 13:46:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uE+OoM267dFPh/kKA/VGTkuQVxZM2722ivJ6mdLlhA0=; b=Weo3JI/uObhJNd0BU5gCrAEnyYCkGdwpndCtmmqpOE2Po43fuU7yHbO4+WkTLeX397 F0LdVDGZu/e1GsqSb6UVEJ9JWOrelRxEXQGiP9sKd7euKz41x7lbiod+ngwBOR/8xxaU /d3wijX0eWH6HlrB2RzLD3QxtJIpJAQx8wdFezq38IbQ2myTl9fBcYHPzjH4ybU56S86 1/M4GX26Hkhql1+CG/MStOBb7DvL91T7HeaoAa1h1kkNQL/T3Wl7EPVhP31uao/VxUrX P8wIKa/NgKSMn6GtNmA1snNHuq/EGXyHCZM41a0FybbeqFVs5vNs4M6HXzNePEpnWl/u 02cw== 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=uE+OoM267dFPh/kKA/VGTkuQVxZM2722ivJ6mdLlhA0=; b=Z3js19uxUPhk+TqmBJLlWg8USuWfDeotvMssyRL6ZtcM2LZHTfWChdo3cPjRBC7PmR i4Mh8gZ1x6BW/T4p6sBiS9hOyRl0Zwnm6mOc6cKNYu9muotypLjpLLCdOfZ4gGYyTYQn RYsTObgZk1YjP0Q+IgbIANBj0pUc+OYRWZZWm9GtBQ4hon2LBHSgeRPuQbsmBIclsO6U mKtBT+X5NsPvISmSbRiA0JsOO86Wg8DT8hoV4ZWw/d5qeIUT6NqVaMiVrs8ElNklFsH/ 8rFlrqg0TzwwSjyvZoVlJaABPREx9gghpO3L5Gz00byJbYpAHdiqGP7+AshagEIjqWH8 s++A== X-Gm-Message-State: AOAM530ocioAPM+r4FT8Vf3C7rTwt6Ld/zwT6ihoxy3SzjgIW3U2GB6e P54jRUCjmNSMbm3FpkEwDWk= X-Google-Smtp-Source: ABdhPJzbX6P/qtWzYtqTYF/9QJ5d1VZCn3vDqoTEcCyU2jaoLAyBFHtjlQ+RtdPuMCFgsFGQixh6Tw== X-Received: by 2002:a4a:c119:: with SMTP id s25mr4645248oop.22.1630701969395; Fri, 03 Sep 2021 13:46:09 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:09 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 15/18] ledtrig-blkdev: Add sysfs attributes to [un]link LEDs & devices Date: Fri, 3 Sep 2021 15:45:45 -0500 Message-Id: <20210903204548.2745354-16-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org /sys/class/leds//add_blkdev - to create device/LED associations /sys/class/leds//delete_blkdev to remove device/LED associations For both attributes, accept multiple device names separated by whitespace Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index b2ec85b805d0..db82d37fc721 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.c +++ b/drivers/leds/trigger/ledtrig-blkdev.c @@ -509,3 +509,51 @@ static void blkdev_deactivate(struct led_classdev *const led_dev) module_put(THIS_MODULE); } + + +/* + * + * sysfs attributes to add & delete devices from LEDs + * + */ + +static ssize_t blkdev_add_or_del(struct device *const dev, + struct device_attribute *const attr, + const char *const buf, const size_t count); + +static struct device_attribute ledtrig_blkdev_attr_add = + __ATTR(add_blkdev, 0200, NULL, blkdev_add_or_del); + +static struct device_attribute ledtrig_blkdev_attr_del = + __ATTR(delete_blkdev, 0200, NULL, blkdev_add_or_del); + +static ssize_t blkdev_add_or_del(struct device *const dev, + struct device_attribute *const attr, + const char *const buf, const size_t count) +{ + struct ledtrig_blkdev_led *const led = led_trigger_get_drvdata(dev); + const char *const disk_name = blkdev_skip_space(buf); + const char *const endp = blkdev_find_space(disk_name); + const ptrdiff_t name_len = endp - disk_name; /* always >= 0 */ + int ret; + + if (name_len == 0) { + pr_info("blkdev LED: empty block device name\n"); + return -EINVAL; + } + + if (attr == &ledtrig_blkdev_attr_del) { + blkdev_disk_delete(led, disk_name, name_len); + } else { /* attr == &ledtrig_blkdev_attr_add */ + ret = blkdev_disk_add(led, disk_name, name_len); + if (ret != 0) + return ret; + } + + /* + * Consume everything up to the next non-whitespace token (or the end + * of the input). Avoids "empty block device name" error if there is + * whitespace (such as a newline) after the last token. + */ + return blkdev_skip_space(endp) - buf; +} From patchwork Fri Sep 3 20:45:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506804 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 B4642C4167E for ; Fri, 3 Sep 2021 20:46:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9909A60041 for ; Fri, 3 Sep 2021 20:46:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351025AbhICUrQ (ORCPT ); Fri, 3 Sep 2021 16:47:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351023AbhICUrN (ORCPT ); Fri, 3 Sep 2021 16:47:13 -0400 Received: from mail-oo1-xc29.google.com (mail-oo1-xc29.google.com [IPv6:2607:f8b0:4864:20::c29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE58EC0613C1; Fri, 3 Sep 2021 13:46:10 -0700 (PDT) Received: by mail-oo1-xc29.google.com with SMTP id e206-20020a4a55d7000000b00291379cb2baso55356oob.9; Fri, 03 Sep 2021 13:46:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7tVbRI5hQo9IfNFJPGEJe7Lec3t9vFzANnU7EaHDqfA=; b=FjAkJ1ZoWQ7UkmDl2G7AjtHkNfCCBdj2g2wlHUpnTU003/GMkbE0BMYB3t2jyR1hSa sHY9tNl5W5HDmPY9tLeJUYsd8WYmDM6qeayAsT1C3zedU/AtYdUdC9WXPQzoUSlAFTs1 jQmbJa9t8Oeal3HF6vj7gsZvHJiVwXSXMstZFWMfWRDbqW8kDFnFvQ8YiXQZb77bUzg+ YfdxTGJCG6v15Vsey2I31ODvwtCltVuucaMsMiZy/g8SMZnRLbIk332/xhPo3GuOI22e 6bz7r7V/odNTK2bil6YqdG79OAFPnovxm5YTuJXtHsdr5oTSQXTB2u4UdDVZ0Cl8HZJm 6nsQ== 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=7tVbRI5hQo9IfNFJPGEJe7Lec3t9vFzANnU7EaHDqfA=; b=lTJOhENeEr3aGfMABAHHEiGlfGdgyOGgt2FMg2FJFmKtjG0kthyAuirFK+MIWmehZz fC9NoJAz+/kuvRYwuGbf7jFfJeD6O45fYUwjl+MdWJA8K6xLCdFD3EnPoSxehd7DO8vh RQvza/DUin4WN227Dgeu271NsLklT/0x2u73X6YwM6FC3zD1EEva3cqh+079D/Lw6fg1 tYMQceQLFDfHHJtOJZDjk/1HwpwgKxWjFbyFXpZntF0FIzBgW9jOglej658MDsvx8F+L cFzKeC7Tb8/osr40HnmraltN0hKcNds07/C0Tu69jA+31lPaOZOrEWMpukq6blU9CSJV tNwg== X-Gm-Message-State: AOAM5302+BO+RQnyGxyx81osca6z2NwsAfG6lnP+Ij0p2qxarufU/8MN 33En6UwfTt17hJjFuT2snV8= X-Google-Smtp-Source: ABdhPJwQ0uEjzN/HiUlFAcHBqvQD+XPAXkOMGPEEOF1aMteoAVf0ypFUZbNnu0v1GYTrRqpRnQ1RwA== X-Received: by 2002:a4a:ca83:: with SMTP id x3mr4547336ooq.2.1630701970119; Fri, 03 Sep 2021 13:46:10 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:09 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 16/18] ledtrig-blkdev: Add blink_time & interval sysfs attributes Date: Fri, 3 Sep 2021 15:45:46 -0500 Message-Id: <20210903204548.2745354-17-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org /sys/class/leds//blink_time controls - per-LED blink duration /sys/class/leds//interval - global frequency with which devices are checked for activity and LEDs are blinked Enforce 25 millisecond minimum for both attributes Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index db82d37fc721..b23a5c98de98 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.c +++ b/drivers/leds/trigger/ledtrig-blkdev.c @@ -557,3 +557,66 @@ static ssize_t blkdev_add_or_del(struct device *const dev, */ return blkdev_skip_space(endp) - buf; } + + +/* + * + * blink_time & interval device attributes + * + */ + +static ssize_t blkdev_time_show(struct device *const dev, + struct device_attribute *const attr, + char *const buf); + +static ssize_t blkdev_time_store(struct device *const dev, + struct device_attribute *const attr, + const char *const buf, const size_t count); + +static struct device_attribute ledtrig_blkdev_attr_blink_time = + __ATTR(blink_time, 0644, blkdev_time_show, blkdev_time_store); + +static struct device_attribute ledtrig_blkdev_attr_interval = + __ATTR(interval, 0644, blkdev_time_show, blkdev_time_store); + +static ssize_t blkdev_time_show(struct device *const dev, + struct device_attribute *const attr, + char *const buf) +{ + const struct ledtrig_blkdev_led *const led = + led_trigger_get_drvdata(dev); + unsigned int value; + + if (attr == &ledtrig_blkdev_attr_blink_time) + value = READ_ONCE(led->blink_msec); + else // attr == &ledtrig_blkdev_attr_interval + value = jiffies_to_msecs(READ_ONCE(ledtrig_blkdev_interval)); + + return sprintf(buf, "%u\n", value); +} + +static ssize_t blkdev_time_store(struct device *const dev, + struct device_attribute *const attr, + const char *const buf, const size_t count) +{ + struct ledtrig_blkdev_led *const led = led_trigger_get_drvdata(dev); + unsigned int value; + int ret; + + ret = kstrtouint(buf, 0, &value); + if (ret != 0) + return ret; + + if (value < LEDTRIG_BLKDEV_MIN_TIME) { + pr_info("blkdev LED: attempt to set time < %s milliseconds\n", + __stringify(LEDTRIG_BLKDEV_MIN_TIME)); + return -ERANGE; + } + + if (attr == &ledtrig_blkdev_attr_blink_time) + WRITE_ONCE(led->blink_msec, value); + else // attr == &ledtrig_blkdev_attr_interval + WRITE_ONCE(ledtrig_blkdev_interval, msecs_to_jiffies(value)); + + return count; +} From patchwork Fri Sep 3 20:45:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 506802 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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=ham 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 004FAC433FE for ; Fri, 3 Sep 2021 20:46:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E3ACC610E5 for ; Fri, 3 Sep 2021 20:46:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350975AbhICUrR (ORCPT ); Fri, 3 Sep 2021 16:47:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351048AbhICUrO (ORCPT ); Fri, 3 Sep 2021 16:47:14 -0400 Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D380C061575; Fri, 3 Sep 2021 13:46:12 -0700 (PDT) Received: by mail-oi1-x236.google.com with SMTP id h133so673379oib.7; Fri, 03 Sep 2021 13:46:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6RxZPutweWrWoAFf03RV/RX9kRQZgKxHijlvrwy4xeA=; b=RgPP6Ox8lfChSOGOvKCshakVU8K42fiVf9AekVvRd+sls6YR9BWYOJ+QnHFvLtCbUc dTp3QoWjxs673/bsgaCYI017mIXxfw+Gyt+lveYDg1Q+r781kFq9zYR/EIwtWn9NXVlU LhL8oIFMmsmoTliD/nwOy3xx/kXd7O3pIhX/YkHjTd7UMXS6KZPNXyPEzfRFufCyjY2f HfuabWsWmYWjAFYMEEIgru67GE0vlvLvkcp4t9pYZHZ9CU4cyFBAbDocmbsf2acXLdaW hwOgBKbuplFY2AEvr9tD8Dj9vF3bD/Yq3pL1mLNPJfGnD/i/3gVL2ZOfIlRZlMqRZUW/ TL1Q== 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=6RxZPutweWrWoAFf03RV/RX9kRQZgKxHijlvrwy4xeA=; b=IQwzDF23iYvXN7x1uxrszURvoBLLvHQOMRUIVC1+Jx4W/l/h8/9LBRrJpIjbJubj4Q rDiVkEjS5rJYzwZo66JO1HhY40N8GoVLwvlzZ1HWbqc3lc2rKkuLOvnhzylxMMGJB4N9 Ic+sPpMSHEhEpxWPxJE9i2fLiv7J96HxFF1ZVCZOVc3l3+AyGt0Wm/N5A+zqnaAnvAg7 vcRLZAMZuu6dqLZ6K8lMdKE927Id1C6eOjpxC99rFiWWCMRE4UZmblPU9dl9K0ofI4vV vNR1SB4iHZyrV5QIfyAOqxa/WoTsKhvb/EDeqTfj6ZDtaBKnTA8TxI47oiBfK3ukI6gh G3qw== X-Gm-Message-State: AOAM532FjmSAOejPHHlOqPquVUiDcVxMri5CctzIG9HTGXlUsa03zhRm Cs0g4dCUvR+/XIO31w4tGx4= X-Google-Smtp-Source: ABdhPJyX1DP4KjospELGfmNuC3H0LsiPs0SiUM4J8LnFz91opZoawTlnBvfOqwgEOVpLq7a95+oUaA== X-Received: by 2002:a05:6808:1911:: with SMTP id bf17mr536077oib.91.1630701971737; Fri, 03 Sep 2021 13:46:11 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id be5sm90023oib.10.2021.09.03.13.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 13:46:11 -0700 (PDT) From: Ian Pilcher To: axboe@kernel.dk, pavel@ucw.cz Cc: linux-leds@vger.kernel.org, linux-block@vger.kernel.org, linux@vger.kernel.org, gregkh@linuxfoundation.org, kabel@kernel.org Subject: [PATCH 18/18] ledtrig-blkdev: Add initialization & exit functions Date: Fri, 3 Sep 2021 15:45:48 -0500 Message-Id: <20210903204548.2745354-19-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903204548.2745354-1-arequipeno@gmail.com> References: <20210903204548.2745354-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Init function: * Initialize interval (convert default value to jiffies) * Initialize __ledtrig_blkdev_disk_cleanup function pointer * Register the block device LED trigger Exit functioon: * Unregister the LED trigger * Set __ledtrig_blkdev_disk_cleanup back to NULL Signed-off-by: Ian Pilcher --- drivers/leds/trigger/ledtrig-blkdev.c | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c index ec167633e329..e6ff5baada2e 100644 --- a/drivers/leds/trigger/ledtrig-blkdev.c +++ b/drivers/leds/trigger/ledtrig-blkdev.c @@ -687,3 +687,81 @@ static ssize_t blkdev_mode_store(struct device *const dev, static struct device_attribute ledtrig_blkdev_attr_mode = __ATTR(mode, 0644, blkdev_mode_show, blkdev_mode_store); + + +/* + * + * Initialization - register the trigger + * + */ + +static struct attribute *ledtrig_blkdev_attrs[] = { + &ledtrig_blkdev_attr_add.attr, + &ledtrig_blkdev_attr_del.attr, + &ledtrig_blkdev_attr_blink_time.attr, + &ledtrig_blkdev_attr_interval.attr, + &ledtrig_blkdev_attr_mode.attr, + NULL +}; + +static const struct attribute_group ledtrig_blkdev_attr_group = { + .attrs = ledtrig_blkdev_attrs, +}; + +static const struct attribute_group *ledtrig_blkdev_attr_groups[] = { + &ledtrig_blkdev_attr_group, + NULL +}; + +static struct led_trigger ledtrig_blkdev_trigger = { + .name = "blkdev", + .activate = blkdev_activate, + .deactivate = blkdev_deactivate, + .groups = ledtrig_blkdev_attr_groups, +}; + +static int __init blkdev_init(void) +{ + int ret; + + ret = mutex_lock_interruptible(&ledtrig_blkdev_mutex); + if (ret != 0) + return ret; + + ledtrig_blkdev_interval = msecs_to_jiffies(LEDTRIG_BLKDEV_INTERVAL); + __ledtrig_blkdev_disk_cleanup = blkdev_disk_cleanup; + + /* + * Can't call led_trigger_register() with ledtrig_blkdev_mutex locked. + * If an LED has blkdev as its default_trigger, blkdev_activate() will + * be called for that LED, and it will try to lock the mutex, which will + * hang. + */ + mutex_unlock(&ledtrig_blkdev_mutex); + + ret = led_trigger_register(&ledtrig_blkdev_trigger); + if (ret != 0) { + mutex_lock(&ledtrig_blkdev_mutex); + __ledtrig_blkdev_disk_cleanup = NULL; + mutex_unlock(&ledtrig_blkdev_mutex); + } + + return ret; +} +module_init(blkdev_init); + +static void __exit blkdev_exit(void) +{ + mutex_lock(&ledtrig_blkdev_mutex); + + /* + * It's OK to call led_trigger_unregister() with the mutex locked, + * because the module can only be unloaded when no LEDs are using + * the blkdev trigger, so blkdev_deactivate() won't be called. + */ + led_trigger_unregister(&ledtrig_blkdev_trigger); + __ledtrig_blkdev_disk_cleanup = NULL; + + mutex_unlock(&ledtrig_blkdev_mutex); +} +module_exit(blkdev_exit);