linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josh Triplett <josh@joshtriplett.org>
To: Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
	linux-api@vger.kernel.org
Subject: [PATCH] drivers/char/mem.c: Add /dev/ioports, supporting 16-bit and 32-bit ports
Date: Fri, 9 May 2014 12:19:16 -0700	[thread overview]
Message-ID: <20140509191914.GA7286@jtriplet-mobl1> (raw)

/dev/port only supports reading and writing 8-bit ports; multi-byte
operations on /dev/port will just operate on multiple successive 8-bit
ports.

Add a new device, /dev/ioports, which supports reading and writing
16-bit and 32-bit ports.  This makes it possible to perform arbitrary
I/O port operations cleanly from privileged userspace processes, without
using iopl or ioperm.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---

Written after encountering yet another out-of-tree omnibus "do arbitrary
I/O for test purposes" driver; this one's main reason for existence was
the inability to operate on 16-bit and 32-bit I/O ports.  Let's get a
proper interface into the kernel, to make such drivers obsolete.

I've also written a corresponding manpage patch, which I'll submit as a
reply to this one.

 drivers/char/mem.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 2 deletions(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 917403f..84e0526 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -35,6 +35,7 @@
 #endif
 
 #define DEVPORT_MINOR	4
+#define DEVIOPORTS_MINOR	12
 
 static inline unsigned long size_inside_page(unsigned long start,
 					     unsigned long size)
@@ -584,6 +585,69 @@ static ssize_t write_port(struct file *file, const char __user *buf,
 	*ppos = i;
 	return tmp-buf;
 }
+
+static ssize_t read_ioports(struct file *file, char __user *buf,
+			    size_t count, loff_t *ppos)
+{
+	unsigned long port = *ppos;
+
+	if (!access_ok(VERIFY_WRITE, buf, count))
+		return -EFAULT;
+	if (port > 65535)
+		return 0;
+	switch (count) {
+	case 1:
+		if (__put_user(inb(port), buf) < 0)
+			return -EFAULT;
+		return 1;
+	case 2:
+		if (__put_user(inw(port), buf) < 0)
+			return -EFAULT;
+		return 2;
+	case 4:
+		if (__put_user(inl(port), buf) < 0)
+			return -EFAULT;
+		return 4;
+	default:
+		return -EINVAL;
+	}
+}
+
+static ssize_t write_ioports(struct file *file, const char __user *buf,
+			     size_t count, loff_t *ppos)
+{
+	unsigned long port = *ppos;
+
+	if (!access_ok(VERIFY_READ, buf, count))
+		return -EFAULT;
+	if (port > 65535)
+		return 0;
+	switch (count) {
+	case 1: {
+		u8 b;
+		if (__get_user(b, buf))
+			return -EFAULT;
+		outb(b, port);
+		return 1;
+	}
+	case 2: {
+		u16 w;
+		if (__get_user(w, buf))
+			return -EFAULT;
+		outw(w, port);
+		return 2;
+	}
+	case 4: {
+		u32 l;
+		if (__get_user(l, buf))
+			return -EFAULT;
+		outl(l, port);
+		return 4;
+	}
+	default:
+		return -EINVAL;
+	}
+}
 #endif
 
 static ssize_t read_null(struct file *file, char __user *buf,
@@ -779,6 +843,13 @@ static const struct file_operations port_fops = {
 	.write		= write_port,
 	.open		= open_port,
 };
+
+static const struct file_operations ioports_fops = {
+	.llseek		= memory_lseek,
+	.read		= read_ioports,
+	.write		= write_ioports,
+	.open		= open_port,
+};
 #endif
 
 static const struct file_operations zero_fops = {
@@ -827,6 +898,9 @@ static const struct memdev {
 #ifdef CONFIG_PRINTK
 	[11] = { "kmsg", 0644, &kmsg_fops, NULL },
 #endif
+#ifdef CONFIG_DEVPORT
+	 [12] = { "ioports", 0, &ioports_fops, NULL },
+#endif
 };
 
 static int memory_open(struct inode *inode, struct file *filp)
@@ -892,9 +966,10 @@ static int __init chr_dev_init(void)
 			continue;
 
 		/*
-		 * Create /dev/port?
+		 * Create /dev/port and /dev/ioports?
 		 */
-		if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
+		if ((minor == DEVPORT_MINOR || minor == DEVIOPORTS_MINOR)
+		    && !arch_has_dev_port())
 			continue;
 
 		device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
-- 
2.0.0.rc2


             reply	other threads:[~2014-05-09 19:19 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-09 19:19 Josh Triplett [this message]
2014-05-09 19:21 ` [PATCH] mem.4, ioports.4: Document /dev/ioports Josh Triplett
2014-05-13  8:27   ` Michael Kerrisk (man-pages)
2014-05-09 19:58 ` [PATCH] drivers/char/mem.c: Add /dev/ioports, supporting 16-bit and 32-bit ports Arnd Bergmann
2014-05-09 20:54   ` H. Peter Anvin
2014-05-09 21:12     ` Arnd Bergmann
2014-05-09 21:20       ` H. Peter Anvin
2014-05-09 22:38         ` Josh Triplett
2014-05-13 22:10           ` H. Peter Anvin
2014-05-15 21:56             ` josh
2014-05-19 12:36               ` Arnd Bergmann
2014-05-28 21:41                 ` H. Peter Anvin
2014-05-29  9:26                   ` Arnd Bergmann
2014-05-29 13:38                     ` H. Peter Anvin
2014-05-30 11:32                       ` Arnd Bergmann
2015-12-22 10:52                         ` Santosh Shukla
2015-12-22 21:56                           ` Arnd Bergmann
2015-12-22 22:02                             ` H. Peter Anvin
2015-12-22 22:11                               ` Arnd Bergmann
2015-12-23 11:34                             ` Santosh Shukla
2015-12-29 13:28                               ` Arnd Bergmann
2015-12-29 15:53                                 ` Santosh Shukla
2015-12-29 15:55                                   ` Santosh Shukla
2015-12-29 16:20                                     ` Arnd Bergmann
2015-12-29 16:30                                       ` Santosh Shukla
2015-12-29 17:31                                         ` Alex Williamson
2015-12-31  9:33                                           ` Santosh Shukla
2015-12-31 15:41                                             ` Alex Williamson
2016-01-07  9:31                                               ` Santosh Shukla
2014-05-10  7:07 ` Jann Horn
2014-05-10 19:32   ` Josh Triplett
2014-05-11 12:50     ` Jann Horn
2014-05-11 21:05       ` Josh Triplett
2014-06-01 10:35         ` Maciej W. Rozycki
2014-06-04 22:59           ` H. Peter Anvin
2014-06-06  9:02             ` Maciej W. Rozycki
2014-05-10 17:18 ` Greg Kroah-Hartman
2014-05-10 19:36   ` Josh Triplett

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=20140509191914.GA7286@jtriplet-mobl1 \
    --to=josh@joshtriplett.org \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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).