[rtems-libbsd commit] rtemsbsd/devfs: Add.

Sebastian Huber sebh at rtems.org
Tue Aug 2 11:21:58 UTC 2016


Module:    rtems-libbsd
Branch:    master
Commit:    b5db3321b5b22fc33c3ac1e3da4145e40b094880
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=b5db3321b5b22fc33c3ac1e3da4145e40b094880

Author:    Christian Mauderer <Christian.Mauderer at embedded-brains.de>
Date:      Thu Jun 30 07:55:27 2016 +0200

rtemsbsd/devfs: Add.

---

 freebsd/sys/sys/conf.h             |  18 ++-
 libbsd.py                          |   1 +
 libbsd_waf.py                      |   1 +
 rtemsbsd/sys/fs/devfs/devfs_devs.c | 293 +++++++++++++++++++++++++++++++++++++
 4 files changed, 311 insertions(+), 2 deletions(-)

diff --git a/freebsd/sys/sys/conf.h b/freebsd/sys/sys/conf.h
index 4ba6f03..2abfa4f 100644
--- a/freebsd/sys/sys/conf.h
+++ b/freebsd/sys/sys/conf.h
@@ -52,10 +52,14 @@ struct cdevsw;
 struct file;
 #ifdef __rtems__
 struct ucred;
+#define RTEMS_CDEV_DIRECTORY "/dev/"
+extern const char rtems_cdev_directory[sizeof(RTEMS_CDEV_DIRECTORY)];
 #endif /* __rtems__ */
 
 struct cdev {
+#ifndef __rtems__
 	struct mount	*si_mountpt;
+#endif /* __rtems__ */
 	u_int		si_flags;
 #define	SI_ETERNAL	0x0001	/* never destroyed */
 #define SI_ALIAS	0x0002	/* carrier of alias name */
@@ -85,11 +89,11 @@ struct cdev {
 	LIST_HEAD(, cdev)	si_children;
 	LIST_ENTRY(cdev)	si_siblings;
 	struct cdev *si_parent;
-	char		*si_name;
 #endif /* __rtems__ */
+	char		*si_name;
 	void		*si_drv1, *si_drv2;
-#ifndef __rtems__
 	struct cdevsw	*si_devsw;
+#ifndef __rtems__
 	int		si_iosize_max;	/* maximum I/O size (for physio &al) */
 	u_long		si_usecount;
 	u_long		si_threadcount;
@@ -97,9 +101,19 @@ struct cdev {
 		struct snapdata *__sid_snapdata;
 	} __si_u;
 	char		__si_namebuf[SPECNAMELEN + 1];
+#else /* __rtems__ */
+	struct {
+		/* Keep this two together. They will be used as one string. */
+		char		__si_dir[sizeof(rtems_cdev_directory) - 1];
+		char		__si_name[SPECNAMELEN + 1];
+	} __si_pathstruct;
 #endif /* __rtems__ */
 };
 
+#ifdef __rtems__
+#define __si_namebuf	__si_pathstruct.__si_name
+#define si_path		__si_pathstruct.__si_dir
+#endif /* __rtems__ */
 #define si_snapdata	__si_u.__sid_snapdata
 
 #ifdef _KERNEL
diff --git a/libbsd.py b/libbsd.py
index e8b1034..0ae5808 100755
--- a/libbsd.py
+++ b/libbsd.py
@@ -142,6 +142,7 @@ def rtems(mm):
             'sys/dev/smc/if_smc_nexus.c',
             'sys/dev/ffec/if_ffec_mcf548x.c',
             'sys/dev/dw_mmc/dw_mmc.c',
+            'sys/fs/devfs/devfs_devs.c',
             'sys/net/if_ppp.c',
             'sys/net/ppp_tty.c',
             'telnetd/check_passwd.c',
diff --git a/libbsd_waf.py b/libbsd_waf.py
index ada200d..caf2538 100644
--- a/libbsd_waf.py
+++ b/libbsd_waf.py
@@ -1110,6 +1110,7 @@ def build(bld):
               'rtemsbsd/sys/dev/usb/controller/ohci_lpc.c',
               'rtemsbsd/sys/dev/usb/controller/usb_otg_transceiver.c',
               'rtemsbsd/sys/dev/usb/controller/usb_otg_transceiver_dump.c',
+              'rtemsbsd/sys/fs/devfs/devfs_devs.c',
               'rtemsbsd/sys/net/if_ppp.c',
               'rtemsbsd/sys/net/ppp_tty.c',
               'rtemsbsd/telnetd/check_passwd.c',
diff --git a/rtemsbsd/sys/fs/devfs/devfs_devs.c b/rtemsbsd/sys/fs/devfs/devfs_devs.c
new file mode 100644
index 0000000..027013b
--- /dev/null
+++ b/rtemsbsd/sys/fs/devfs/devfs_devs.c
@@ -0,0 +1,293 @@
+/*
+ * 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 <machine/pcpu.h>
+#include <rtems/imfs.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/file.h>
+#include <unistd.h>
+
+#include <sys/conf.h>
+#include <fs/devfs/devfs_int.h>
+
+const char rtems_cdev_directory[] = RTEMS_CDEV_DIRECTORY;
+
+static struct cdev *
+devfs_imfs_get_context_by_iop(rtems_libio_t *iop)
+{
+	return IMFS_generic_get_context_by_iop(iop);
+}
+
+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 thread *td = rtems_bsd_get_curthread_or_null();
+	int error;
+
+	if (td != NULL) {
+		error = cdev->si_devsw->d_open(cdev, oflag, 0, td);
+	} else {
+		error = ENOMEM;
+	}
+
+	return rtems_bsd_error_to_status_and_errno(error);
+}
+
+static int
+devfs_imfs_close(rtems_libio_t *iop)
+{
+	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+	struct thread *td = rtems_bsd_get_curthread_or_null();
+	int flags = rtems_libio_to_fcntl_flags(iop->flags);
+	int error;
+
+	if (td != NULL) {
+		error = cdev->si_devsw->d_close(cdev, flags, 0, td);
+	} else {
+		error = ENOMEM;
+	}
+
+	return rtems_bsd_error_to_status_and_errno(error);
+}
+
+static ssize_t
+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 thread *td = rtems_bsd_get_curthread_or_null();
+	struct uio uio = {
+		.uio_iov = iov,
+		.uio_iovcnt = iovcnt,
+		.uio_offset = 0,
+		.uio_resid = total,
+		.uio_segflg = UIO_USERSPACE,
+		.uio_rw = UIO_READ,
+		.uio_td = td
+	};
+	int error;
+
+	if (td != NULL) {
+		error = cdev->si_devsw->d_read(cdev, &uio,
+		    rtems_libio_to_fcntl_flags(iop->flags));
+	} else {
+		error = ENOMEM;
+	}
+
+	if (error == 0) {
+		return (total - uio.uio_resid);
+	} else {
+		rtems_set_errno_and_return_minus_one(error);
+	}
+}
+
+static ssize_t
+devfs_imfs_read(rtems_libio_t *iop, void *buffer, size_t count)
+{
+	struct iovec iov = {
+		.iov_base = buffer,
+		.iov_len = count
+	};
+
+	return (devfs_imfs_readv(iop, &iov, 1, count));
+}
+
+static ssize_t
+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 thread *td = rtems_bsd_get_curthread_or_null();
+	struct uio uio = {
+		.uio_iov = iov,
+		.uio_iovcnt = iovcnt,
+		.uio_offset = 0,
+		.uio_resid = total,
+		.uio_segflg = UIO_USERSPACE,
+		.uio_rw = UIO_WRITE,
+		.uio_td = td
+	};
+	int error;
+
+	if (td != NULL) {
+		error = cdev->si_devsw->d_write(cdev, &uio,
+		    rtems_libio_to_fcntl_flags(iop->flags));
+	} else {
+		error = ENOMEM;
+	}
+
+	if (error == 0) {
+		return (total - uio.uio_resid);
+	} else {
+		rtems_set_errno_and_return_minus_one(error);
+	}
+}
+
+static ssize_t
+devfs_imfs_write(rtems_libio_t *iop, const void *buffer, size_t count)
+{
+	struct iovec iov = {
+		.iov_base = buffer,
+		.iov_len = count
+	};
+
+	return (devfs_imfs_writev(iop, &iov, 1, count));
+}
+
+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 thread *td = rtems_bsd_get_curthread_or_null();
+	int error;
+	int flags = rtems_libio_to_fcntl_flags(iop->flags);
+
+	if (td != 0) {
+		error = cdev->si_devsw->d_ioctl(cdev, request, buffer, flags,
+		    td);
+	} else {
+		error = ENOMEM;
+	}
+
+	return (rtems_bsd_error_to_status_and_errno(error));
+}
+
+static int
+devfs_imfs_poll(rtems_libio_t *iop, int events)
+{
+	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+
+	return (cdev->si_devsw->d_poll(cdev, events,
+	    rtems_bsd_get_curthread_or_wait_forever()));
+}
+
+static int
+devfs_imfs_kqfilter(rtems_libio_t *iop, struct knote *kn)
+{
+	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
+
+	return (cdev->si_devsw->d_kqfilter(cdev, kn));
+}
+
+static const rtems_filesystem_file_handlers_r devfs_imfs_handlers = {
+	.open_h = devfs_imfs_open,
+	.close_h = devfs_imfs_close,
+	.read_h = devfs_imfs_read,
+	.write_h = devfs_imfs_write,
+	.ioctl_h = devfs_imfs_ioctl,
+	.lseek_h = rtems_filesystem_default_lseek_file,
+	.fstat_h = rtems_filesystem_default_fstat,
+	.ftruncate_h = rtems_filesystem_default_ftruncate,
+	.fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
+	.fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
+	.fcntl_h = rtems_filesystem_default_fcntl,
+	.poll_h = devfs_imfs_poll,
+	.kqfilter_h = devfs_imfs_kqfilter,
+	.readv_h = devfs_imfs_readv,
+	.writev_h = devfs_imfs_writev,
+};
+
+static const IMFS_node_control devfs_imfs_control = IMFS_GENERIC_INITIALIZER(
+    &devfs_imfs_handlers, IMFS_node_initialize_generic,
+    IMFS_node_destroy_default);
+
+struct cdev *
+devfs_alloc(int flags)
+{
+	struct cdev *cdev;
+
+	cdev = malloc(sizeof *cdev);
+	if (cdev == NULL)
+		return (NULL);
+
+	memset(cdev, 0, sizeof *cdev);
+	cdev->si_name = cdev->__si_namebuf;
+	memcpy(cdev->__si_pathstruct.__si_dir, rtems_cdev_directory,
+	    sizeof(rtems_cdev_directory) - 1);
+
+	return (cdev);
+}
+
+void
+devfs_free(struct cdev *cdev)
+{
+	free(cdev);
+}
+
+/*
+ * devfs_create() and devfs_destroy() are called from kern_conf.c and
+ * in both cases the devlock() mutex is held, so no further locking
+ * is necesary and no sleeping allowed.
+ */
+
+void
+devfs_create(struct cdev *dev)
+{
+	int rv;
+	mode_t mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO;
+
+	rv = IMFS_make_generic_node(dev->si_path, mode, &devfs_imfs_control,
+	    dev);
+	BSD_ASSERT(rv == 0);
+}
+
+void
+devfs_destroy(struct cdev *dev)
+{
+	int rv;
+
+	rv = unlink(dev->si_path);
+	BSD_ASSERT(rv == 0);
+}
+
+int
+devfs_dev_exists(const char *name)
+{
+	const size_t dirlen = sizeof(rtems_cdev_directory) - 1;
+	const size_t maxnamelen = SPECNAMELEN;
+
+	int rv;
+	char fullpath[dirlen + maxnamelen + 1];
+
+	memcpy(fullpath, rtems_cdev_directory, dirlen);
+	strncpy(fullpath + dirlen, name, maxnamelen);
+
+	rv = access(fullpath, F_OK);
+
+	if(rv == 0)
+		return 1;
+	else
+		return 0;
+}



More information about the vc mailing list