[PATCH 5/7] Adding RTEMS support for FREEBSD TTY

Kevin Kirspel kevin-kirspel at idexx.com
Thu Apr 6 01:11:16 UTC 2017


---
 rtemsbsd/include/machine/rtems-bsd-commands.h     |   2 +
 rtemsbsd/include/machine/rtems-bsd-kernel-space.h |   1 +
 rtemsbsd/include/rtems/bsd/local/opt_gdb.h        |   1 +
 rtemsbsd/include/rtems/netcmds-config.h           |   2 +
 rtemsbsd/rtems/rtems-bsd-shell-stty.c             |  40 +++++++
 rtemsbsd/sys/fs/devfs/devfs_devs.c                |  86 +++++++++++++-
 rtemsbsd/sys/fs/devfs/devfs_vnops.c               | 136 ++++++++++++++++++++++
 rtemsbsd/sys/net/ppp_tty.c                        |  10 +-
 8 files changed, 263 insertions(+), 15 deletions(-)
 create mode 100644 rtemsbsd/include/rtems/bsd/local/opt_gdb.h
 create mode 100644 rtemsbsd/rtems/rtems-bsd-shell-stty.c
 mode change 100755 => 100644 rtemsbsd/sys/fs/devfs/devfs_devs.c
 create mode 100644 rtemsbsd/sys/fs/devfs/devfs_vnops.c

diff --git a/rtemsbsd/include/machine/rtems-bsd-commands.h b/rtemsbsd/include/machine/rtems-bsd-commands.h
index c0524c8..9e9ed1c 100644
--- a/rtemsbsd/include/machine/rtems-bsd-commands.h
+++ b/rtemsbsd/include/machine/rtems-bsd-commands.h
@@ -70,6 +70,8 @@ int rtems_bsd_command_vmstat(int argc, char **argv);
 
 int rtems_bsd_command_wlanstats(int argc, char **argv);
 
+int rtems_bsd_command_stty(int argc, char **argv);
+
 __END_DECLS
 
 #endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_COMMANDS_H_ */
diff --git a/rtemsbsd/include/machine/rtems-bsd-kernel-space.h b/rtemsbsd/include/machine/rtems-bsd-kernel-space.h
index 10ce9d2..9d0484a 100644
--- a/rtemsbsd/include/machine/rtems-bsd-kernel-space.h
+++ b/rtemsbsd/include/machine/rtems-bsd-kernel-space.h
@@ -48,6 +48,7 @@
 #define	__GLOBL(sym)	__GLOBL1(sym)
 
 #define O_CLOEXEC 0
+#define IO_NDELAY O_NONBLOCK
 
 #define __FreeBSD__ 1
 
diff --git a/rtemsbsd/include/rtems/bsd/local/opt_gdb.h b/rtemsbsd/include/rtems/bsd/local/opt_gdb.h
new file mode 100644
index 0000000..936ffd8
--- /dev/null
+++ b/rtemsbsd/include/rtems/bsd/local/opt_gdb.h
@@ -0,0 +1 @@
+/* EMPTY */
diff --git a/rtemsbsd/include/rtems/netcmds-config.h b/rtemsbsd/include/rtems/netcmds-config.h
index b3bc66f..515b265 100644
--- a/rtemsbsd/include/rtems/netcmds-config.h
+++ b/rtemsbsd/include/rtems/netcmds-config.h
@@ -45,6 +45,8 @@ extern rtems_shell_cmd_t rtems_shell_VMSTAT_Command;
 
 extern rtems_shell_cmd_t rtems_shell_WLANSTATS_Command;
 
+extern rtems_shell_cmd_t rtems_shell_STTY_Command;
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/rtemsbsd/rtems/rtems-bsd-shell-stty.c b/rtemsbsd/rtems/rtems-bsd-shell-stty.c
new file mode 100644
index 0000000..530b442
--- /dev/null
+++ b/rtemsbsd/rtems/rtems-bsd-shell-stty.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <rtems/netcmds-config.h>
+#include <machine/rtems-bsd-commands.h>
+
+rtems_shell_cmd_t rtems_shell_STTY_Command = {
+  .name = "stty",
+  .usage = "stty [args]",
+  .topic = "serial",
+  .command = rtems_bsd_command_stty
+};
diff --git a/rtemsbsd/sys/fs/devfs/devfs_devs.c b/rtemsbsd/sys/fs/devfs/devfs_devs.c
old mode 100755
new mode 100644
index 0ee71ff..a91a02e
--- a/rtemsbsd/sys/fs/devfs/devfs_devs.c
+++ b/rtemsbsd/sys/fs/devfs/devfs_devs.c
@@ -36,6 +36,7 @@
 #include <sys/kernel.h>
 #include <sys/file.h>
 #include <sys/malloc.h>
+#include <sys/proc.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -50,8 +51,16 @@
 
 const char rtems_cdev_directory[] = RTEMS_CDEV_DIRECTORY;
 
+/*
+ * The one true (but secret) list of active devices in the system.
+ * Locked by dev_lock()/devmtx
+ */
+struct cdev_priv_list cdevp_list = TAILQ_HEAD_INITIALIZER(cdevp_list);
+
 struct unrhdr *devfs_inos;
 
+static MALLOC_DEFINE(M_CDEVP, "DEVFS1", "DEVFS cdev_priv storage");
+
 static struct cdev *
 devfs_imfs_get_context_by_iop(rtems_libio_t *iop)
 {
@@ -62,11 +71,19 @@ static int
 devfs_imfs_open(rtems_libio_t *iop, const char *path, int oflag, mode_t mode)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
+	struct file *fpop;
 	int error;
 
 	if (td != NULL) {
+		fpop = td->td_fpop;
+		curthread->td_fpop = fp;
 		error = cdev->si_devsw->d_open(cdev, oflag, 0, td);
+		/* Clean up any cdevpriv upon error. */
+		if (error != 0)
+			devfs_clear_cdevpriv();
+		curthread->td_fpop = fpop;
 	} else {
 		error = ENOMEM;
 	}
@@ -78,12 +95,17 @@ static int
 devfs_imfs_close(rtems_libio_t *iop)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
 	int flags = rtems_libio_to_fcntl_flags(iop->flags);
+	struct file *fpop;
 	int error;
 
 	if (td != NULL) {
+		fpop = td->td_fpop;
+		curthread->td_fpop = fp;
 		error = cdev->si_devsw->d_close(cdev, flags, 0, td);
+		curthread->td_fpop = fpop;
 	} else {
 		error = ENOMEM;
 	}
@@ -96,6 +118,7 @@ devfs_imfs_readv(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
     ssize_t total)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
 	struct uio uio = {
 		.uio_iov = __DECONST(struct iovec *, iov),
@@ -106,11 +129,15 @@ devfs_imfs_readv(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
 		.uio_rw = UIO_READ,
 		.uio_td = td
 	};
+	struct file *fpop;
 	int error;
 
 	if (td != NULL) {
+		fpop = td->td_fpop;
+		curthread->td_fpop = fp;
 		error = cdev->si_devsw->d_read(cdev, &uio,
 		    rtems_libio_to_fcntl_flags(iop->flags));
+		td->td_fpop = fpop;
 	} else {
 		error = ENOMEM;
 	}
@@ -138,6 +165,7 @@ devfs_imfs_writev(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
     ssize_t total)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
 	struct uio uio = {
 		.uio_iov = __DECONST(struct iovec *, iov),
@@ -148,11 +176,15 @@ devfs_imfs_writev(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
 		.uio_rw = UIO_WRITE,
 		.uio_td = td
 	};
+	struct file *fpop;
 	int error;
 
 	if (td != NULL) {
+		fpop = td->td_fpop;
+		curthread->td_fpop = fp;
 		error = cdev->si_devsw->d_write(cdev, &uio,
 		    rtems_libio_to_fcntl_flags(iop->flags));
+		td->td_fpop = fpop;
 	} else {
 		error = ENOMEM;
 	}
@@ -179,13 +211,18 @@ static int
 devfs_imfs_ioctl(rtems_libio_t *iop, ioctl_command_t request, void *buffer)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
+	struct file *fpop;
 	int error;
 	int flags = rtems_libio_to_fcntl_flags(iop->flags);
 
 	if (td != 0) {
+		fpop = td->td_fpop;
+		curthread->td_fpop = fp;
 		error = cdev->si_devsw->d_ioctl(cdev, request, buffer, flags,
 		    td);
+		td->td_fpop = fpop;
 	} else {
 		error = ENOMEM;
 	}
@@ -197,17 +234,34 @@ static int
 devfs_imfs_poll(rtems_libio_t *iop, int events)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
+	struct thread *td = rtems_bsd_get_curthread_or_wait_forever();
+	struct file *fpop;
+	int error;
+
+	fpop = td->td_fpop;
+	curthread->td_fpop = fp;
+	error = cdev->si_devsw->d_poll(cdev, events, td);
+	td->td_fpop = fpop;
 
-	return (cdev->si_devsw->d_poll(cdev, events,
-	    rtems_bsd_get_curthread_or_wait_forever()));
+	return error;
 }
 
 static int
 devfs_imfs_kqfilter(rtems_libio_t *iop, struct knote *kn)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct file *fp = rtems_bsd_iop_to_fp(iop);
+	struct thread *td = rtems_bsd_get_curthread_or_wait_forever();
+	struct file *fpop;
+	int error;
+
+	fpop = td->td_fpop;
+	curthread->td_fpop = fp;
+	error = cdev->si_devsw->d_kqfilter(cdev, kn);
+	td->td_fpop = fpop;
 
-	return (cdev->si_devsw->d_kqfilter(cdev, kn));
+	return error;
 }
 
 static const rtems_filesystem_file_handlers_r devfs_imfs_handlers = {
@@ -235,12 +289,17 @@ static const IMFS_node_control devfs_imfs_control = IMFS_GENERIC_INITIALIZER(
 struct cdev *
 devfs_alloc(int flags)
 {
+	struct cdev_priv *cdp;
 	struct cdev *cdev;
 
-	cdev = malloc(sizeof *cdev, M_TEMP, M_ZERO);
-	if (cdev == NULL)
+	cdp = malloc(sizeof *cdp, M_CDEVP, M_ZERO |
+	    ((flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK));
+	if (cdp == NULL)
 		return (NULL);
 
+	cdev = &cdp->cdp_c;
+	LIST_INIT(&cdev->si_children);
+
 	memcpy(cdev->si_path, rtems_cdev_directory, sizeof(cdev->si_path));
 	return (cdev);
 }
@@ -248,7 +307,11 @@ devfs_alloc(int flags)
 void
 devfs_free(struct cdev *cdev)
 {
-	free(cdev, M_TEMP);
+	struct cdev_priv *cdp;
+
+	cdp = cdev2priv(cdev);
+	devfs_free_cdp_inode(cdp->cdp_inode);
+	free(cdp, M_CDEVP);
 }
 
 /*
@@ -286,6 +349,7 @@ devfs_create_directory(const char *devname)
 void
 devfs_create(struct cdev *dev)
 {
+	struct cdev_priv *cdp;
 	int rv;
 	mode_t mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO;
 
@@ -294,13 +358,23 @@ devfs_create(struct cdev *dev)
 	rv = IMFS_make_generic_node(dev->si_path, mode, &devfs_imfs_control,
 	    dev);
 	BSD_ASSERT(rv == 0);
+
+	cdp = cdev2priv(dev);
+	cdp->cdp_flags |= CDP_ACTIVE;
+	cdp->cdp_inode = alloc_unrl(devfs_inos);
+	dev_refl(dev);
+	TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
 }
 
 void
 devfs_destroy(struct cdev *dev)
 {
+	struct cdev_priv *cdp;
 	int rv;
 
+	cdp = cdev2priv(dev);
+	cdp->cdp_flags &= ~CDP_ACTIVE;
+
 	rv = unlink(dev->si_path);
 	BSD_ASSERT(rv == 0);
 
diff --git a/rtemsbsd/sys/fs/devfs/devfs_vnops.c b/rtemsbsd/sys/fs/devfs/devfs_vnops.c
new file mode 100644
index 0000000..8c4a786
--- /dev/null
+++ b/rtemsbsd/sys/fs/devfs/devfs_vnops.c
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <machine/rtems-bsd-kernel-space.h>
+
+#include <sys/types.h>
+#include <sys/conf.h>
+#include <sys/kernel.h>
+#include <sys/file.h>
+#include <sys/malloc.h>
+#include <sys/proc.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <fs/devfs/devfs_int.h>
+
+static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
+
+struct mtx	cdevpriv_mtx;
+MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
+
+int
+devfs_get_cdevpriv(void **datap)
+{
+	struct file *fp;
+	struct cdev_privdata *p;
+	int error;
+
+	fp = curthread->td_fpop;
+	if (fp == NULL)
+		return (EBADF);
+	p = fp->f_cdevpriv;
+	if (p != NULL) {
+		error = 0;
+		*datap = p->cdpd_data;
+	} else
+		error = ENOENT;
+	return (error);
+}
+
+int
+devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
+{
+	struct file *fp;
+	struct cdev_priv *cdp;
+	struct cdev_privdata *p;
+	int error;
+
+	fp = curthread->td_fpop;
+	if (fp == NULL)
+		return (ENOENT);
+	cdp = cdev2priv((struct cdev *)fp->f_data);
+	p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
+	p->cdpd_data = priv;
+	p->cdpd_dtr = priv_dtr;
+	p->cdpd_fp = fp;
+	mtx_lock(&cdevpriv_mtx);
+	if (fp->f_cdevpriv == NULL) {
+		LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
+		fp->f_cdevpriv = p;
+		mtx_unlock(&cdevpriv_mtx);
+		error = 0;
+	} else {
+		mtx_unlock(&cdevpriv_mtx);
+		free(p, M_CDEVPDATA);
+		error = EBUSY;
+	}
+	return (error);
+}
+
+void
+devfs_destroy_cdevpriv(struct cdev_privdata *p)
+{
+
+	mtx_assert(&cdevpriv_mtx, MA_OWNED);
+	KASSERT(p->cdpd_fp->f_cdevpriv == p,
+	    ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
+	p->cdpd_fp->f_cdevpriv = NULL;
+	LIST_REMOVE(p, cdpd_list);
+	mtx_unlock(&cdevpriv_mtx);
+	(p->cdpd_dtr)(p->cdpd_data);
+	free(p, M_CDEVPDATA);
+}
+
+static void
+devfs_fpdrop(struct file *fp)
+{
+  struct cdev_privdata *p;
+
+  mtx_lock(&cdevpriv_mtx);
+  if ((p = fp->f_cdevpriv) == NULL) {
+    mtx_unlock(&cdevpriv_mtx);
+    return;
+  }
+  devfs_destroy_cdevpriv(p);
+}
+
+void
+devfs_clear_cdevpriv(void)
+{
+  struct file *fp;
+
+  fp = curthread->td_fpop;
+  if (fp == NULL)
+    return;
+  devfs_fpdrop(fp);
+}
diff --git a/rtemsbsd/sys/net/ppp_tty.c b/rtemsbsd/sys/net/ppp_tty.c
index 9d416ea..718b146 100644
--- a/rtemsbsd/sys/net/ppp_tty.c
+++ b/rtemsbsd/sys/net/ppp_tty.c
@@ -426,16 +426,8 @@ ppptioctl(struct rtems_termios_tty *tty, rtems_libio_ioctl_args_t *args)
     struct ppp_softc   *sc    = tty->t_sc;
 
     switch (cmd) {
-    case RTEMS_IO_RCVWAKEUP:
     case RTEMS_IO_SNDWAKEUP:
-    case TIOCDRAIN:
-    case TIOCFLUSH:
-    case TIOCGETA:
-    case TIOCGETD:
-    case TIOCSETA:
-    case TIOCSETAF:
-    case TIOCSETAW:
-    case TIOCSETD:
+    case RTEMS_IO_RCVWAKEUP:
         error = rtems_termios_ioctl(args);
 	break;
 
-- 
1.9.1



More information about the devel mailing list