diff mbox series

[RFC,v3,04/18] ledtrig-blkdev: Add misc. helper functions to blkdev LED trigger

Message ID 20210819025053.222710-5-arequipeno@gmail.com
State New
Headers show
Series Add block device LED trigger | expand

Commit Message

Ian Pilcher Aug. 19, 2021, 2:50 a.m. UTC
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 <arequipeno@gmail.com>
---
 drivers/leds/trigger/ledtrig-blkdev.c | 74 +++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
diff mbox series

Patch

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 <arequipeno@gmail.com>
  */
 
+#include <linux/ctype.h>
 #include <linux/leds.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
@@ -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;
+}