From: "Uwe Kleine-König" <uwe@kleine-koenig.org>
To: Jacek Anaszewski <jacek.anaszewski@gmail.com>,
Pavel Machek <pavel@ucw.cz>, Dan Murphy <dmurphy@ti.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jslaby@suse.com>
Cc: linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel@pengutronix.de, linux-serial@vger.kernel.org
Subject: [PATCH v6 1/4] lib: new helper kstrtodev_t()
Date: Thu, 13 Feb 2020 10:15:57 +0100 [thread overview]
Message-ID: <20200213091600.554-2-uwe@kleine-koenig.org> (raw)
In-Reply-To: <20200213091600.554-1-uwe@kleine-koenig.org>
This function is in the same spirit as the other kstrto* functions and
uses the same calling convention. It expects the input string to be in
the format %u:%u and implements stricter parsing than sscanf as it
returns an error on trailing data (other than the usual \n).
Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
---
include/linux/kdev_t.h | 2 ++
include/linux/kernel.h | 1 +
lib/kstrtox.c | 46 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
diff --git a/include/linux/kdev_t.h b/include/linux/kdev_t.h
index 85b5151911cf..3a5c24bd8fa4 100644
--- a/include/linux/kdev_t.h
+++ b/include/linux/kdev_t.h
@@ -4,8 +4,10 @@
#include <uapi/linux/kdev_t.h>
+/* dev_t is 32 bit wide, 20 bits are used for MINOR, 12 for major */
#define MINORBITS 20
#define MINORMASK ((1U << MINORBITS) - 1)
+#define MAJORBITS 12
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 0d9db2a14f44..9cf694c5d2c3 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -410,6 +410,7 @@ int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
int __must_check kstrtobool(const char *s, bool *res);
+int __must_check kstrtodev_t(const char *s, dev_t *res);
int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index 1006bf70bf74..e1b896635c6a 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -19,6 +19,7 @@
#include <linux/export.h>
#include <linux/types.h>
#include <linux/uaccess.h>
+#include <linux/kdev_t.h>
#include "kstrtox.h"
const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
@@ -367,6 +368,51 @@ int kstrtobool(const char *s, bool *res)
}
EXPORT_SYMBOL(kstrtobool);
+/**
+ * kstrtodev_t - convert a string in format %u:%u to a dev_t
+ * @s: input string
+ * @res: result
+ *
+ * This is the reverse of print_dev_t. The first number is interpreted as major,
+ * the second as minor.
+ */
+int kstrtodev_t(const char *s, dev_t *res)
+{
+ unsigned long long _res;
+ unsigned int ma, mi;
+ int rv;
+
+ rv = _parse_integer(s, 10, &_res);
+ if (rv < 0)
+ return rv;
+ if (rv & KSTRTOX_OVERFLOW || _res >= (1U << MAJORBITS))
+ return -ERANGE;
+ ma = _res;
+
+ s += rv;
+
+ if (*s++ != ':')
+ return -EINVAL;
+
+ rv = _parse_integer(s, 10, &_res);
+ if (rv < 0)
+ return rv;
+ if (rv & KSTRTOX_OVERFLOW || _res >= (1U << MINORBITS))
+ return -ERANGE;
+ mi = _res;
+
+ s += rv;
+
+ if (*s == '\n')
+ s++;
+ if (*s)
+ return -EINVAL;
+
+ *res = MKDEV(ma, mi);
+ return 0;
+}
+EXPORT_SYMBOL(kstrtodev_t);
+
/*
* Since "base" would be a nonsense argument, this open-codes the
* _from_user helper instead of using the helper macro below.
--
2.24.0
next prev parent reply other threads:[~2020-02-13 9:25 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-13 9:15 [PATCH v6 0/4] leds: trigger: implement a tty trigger Uwe Kleine-König
2020-02-13 9:15 ` Uwe Kleine-König [this message]
2020-02-19 19:50 ` [PATCH v6 1/4] lib: new helper kstrtodev_t() Andy Shevchenko
2020-02-20 7:49 ` Uwe Kleine-König
2020-02-20 10:22 ` Andy Shevchenko
2020-02-20 10:57 ` Uwe Kleine-König
2020-02-20 11:46 ` Andy Shevchenko
2020-02-20 11:57 ` Andy Shevchenko
2020-02-20 14:01 ` Uwe Kleine-König
2020-02-21 8:42 ` Andy Shevchenko
2020-02-21 10:53 ` Uwe Kleine-König
2020-04-25 7:07 ` Pavel Machek
2020-02-13 9:15 ` [PATCH v6 2/4] tty: rename tty_kopen() and add new function tty_kopen_shared() Uwe Kleine-König
2020-02-19 13:21 ` Johan Hovold
2020-02-19 16:37 ` Uwe Kleine-König
2020-02-19 17:17 ` Johan Hovold
2020-02-20 11:04 ` Uwe Kleine-König
2020-02-25 8:55 ` Johan Hovold
2020-02-25 9:05 ` Uwe Kleine-König
2020-02-13 9:15 ` [PATCH v6 3/4] tty: new helper function tty_get_icount() Uwe Kleine-König
2020-02-13 9:16 ` [PATCH v6 4/4] leds: trigger: implement a tty trigger Uwe Kleine-König
2020-02-19 10:52 ` Johan Hovold
2020-02-19 11:03 ` Uwe Kleine-König
2020-02-19 11:19 ` Johan Hovold
2020-02-19 12:48 ` Johan Hovold
2020-02-19 10:40 ` [PATCH v6 0/4] " Greg Kroah-Hartman
2020-02-26 14:02 ` Pavel Machek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200213091600.554-2-uwe@kleine-koenig.org \
--to=uwe@kleine-koenig.org \
--cc=dmurphy@ti.com \
--cc=gregkh@linuxfoundation.org \
--cc=jacek.anaszewski@gmail.com \
--cc=jslaby@suse.com \
--cc=kernel@pengutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=pavel@ucw.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).