mirror of
https://git.FreeBSD.org/src.git
synced 2024-12-23 11:18:54 +00:00
Add a C libary and a Python module that implements an API similar
to bus_space(9) and that uses the proto(4) driver for talking to hardware. If the I/O resource is a memory mapped I/O resource, then mmap(2) will be attempted to avoid read(2)/write(2) overhead. Sponsored by: Juniper Networks, Inc.
This commit is contained in:
parent
67fb10f30c
commit
0c2b180a35
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=265056
9
tools/bus_space/C/Makefile
Normal file
9
tools/bus_space/C/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
# $FreeBSD$
|
||||
|
||||
LIB= bus_space
|
||||
SHLIB_MAJOR= 0
|
||||
SRCS= lang.c
|
||||
|
||||
CFLAGS+= -I${.CURDIR}/..
|
||||
|
||||
.include <bsd.lib.mk>
|
100
tools/bus_space/C/lang.c
Normal file
100
tools/bus_space/C/lang.c
Normal file
@ -0,0 +1,100 @@
|
||||
/*-
|
||||
* Copyright (c) 2014 Marcel Moolenaar
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 ``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 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 <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "bus_space.h"
|
||||
#include "libbus_space.h"
|
||||
|
||||
int
|
||||
bus_space_read_1(int rid, long ofs)
|
||||
{
|
||||
uint8_t val;
|
||||
|
||||
return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_read_2(int rid, long ofs)
|
||||
{
|
||||
uint16_t val;
|
||||
|
||||
return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
|
||||
}
|
||||
|
||||
int64_t
|
||||
bus_space_read_4(int rid, long ofs)
|
||||
{
|
||||
uint32_t val;
|
||||
|
||||
return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int64_t)val);
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_write_1(int rid, long ofs, uint8_t val)
|
||||
{
|
||||
|
||||
return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_write_2(int rid, long ofs, uint16_t val)
|
||||
{
|
||||
|
||||
return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_write_4(int rid, long ofs, uint32_t val)
|
||||
{
|
||||
|
||||
return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_map(const char *dev)
|
||||
{
|
||||
|
||||
return (bs_map(dev));
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_unmap(int rid)
|
||||
{
|
||||
|
||||
return ((!bs_unmap(rid)) ? errno : 0);
|
||||
}
|
||||
|
||||
int
|
||||
bus_space_subregion(int rid, long ofs, long sz)
|
||||
{
|
||||
|
||||
return (bs_subregion(rid, ofs, sz));
|
||||
}
|
42
tools/bus_space/C/libbus_space.h
Normal file
42
tools/bus_space/C/libbus_space.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*-
|
||||
* Copyright (c) 2014 Marcel Moolenaar
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 ``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 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.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#ifndef _LIBBUS_SPACE_H_
|
||||
#define _LIBBUS_SPACE_H_
|
||||
|
||||
int bus_space_map(const char *dev);
|
||||
int bus_space_read_1(int rid, long ofs);
|
||||
int bus_space_read_2(int rid, long ofs);
|
||||
int64_t bus_space_read_4(int rid, long ofs);
|
||||
int bus_space_subregion(int rid, long ofs, long sz);
|
||||
int bus_space_unmap(int rid);
|
||||
int bus_space_write_1(int rid, long ofs, uint8_t val);
|
||||
int bus_space_write_2(int rid, long ofs, uint16_t val);
|
||||
int bus_space_write_4(int rid, long ofs, uint32_t val);
|
||||
|
||||
#endif /* _LIBBUS_SPACE_H_ */
|
7
tools/bus_space/Makefile
Normal file
7
tools/bus_space/Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
# $FreeBSD$
|
||||
|
||||
SUBDIR= \
|
||||
C \
|
||||
Python
|
||||
|
||||
.include <bsd.subdir.mk>
|
4
tools/bus_space/Makefile.inc
Normal file
4
tools/bus_space/Makefile.inc
Normal file
@ -0,0 +1,4 @@
|
||||
# $FreeBSD$
|
||||
|
||||
.PATH: ${.CURDIR}/..
|
||||
SRCS+= bus_space.c
|
9
tools/bus_space/Python/Makefile
Normal file
9
tools/bus_space/Python/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
# $FreeBSD$
|
||||
|
||||
SHLIB_NAME= bus_space.so
|
||||
SRCS= lang.c
|
||||
|
||||
CFLAGS+= -I${.CURDIR}/.. -I/usr/local/include/python2.7
|
||||
LDFLAGS+= -L/usr/local/lib -lpython2.7
|
||||
|
||||
.include <bsd.lib.mk>
|
200
tools/bus_space/Python/lang.c
Normal file
200
tools/bus_space/Python/lang.c
Normal file
@ -0,0 +1,200 @@
|
||||
/*-
|
||||
* Copyright (c) 2014 Marcel Moolenaar
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 ``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 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 <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include "bus_space.h"
|
||||
|
||||
static PyObject *
|
||||
bus_read_1(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs;
|
||||
int rid;
|
||||
uint8_t val;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
|
||||
return (NULL);
|
||||
if (!bs_read(rid, ofs, &val, sizeof(val))) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (Py_BuildValue("B", val));
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_read_2(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs;
|
||||
int rid;
|
||||
uint16_t val;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
|
||||
return (NULL);
|
||||
if (!bs_read(rid, ofs, &val, sizeof(val))) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (Py_BuildValue("H", val));
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_read_4(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs;
|
||||
int rid;
|
||||
uint32_t val;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
|
||||
return (NULL);
|
||||
if (!bs_read(rid, ofs, &val, sizeof(val))) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (Py_BuildValue("I", val));
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_write_1(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs;
|
||||
int rid;
|
||||
uint8_t val;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ilB", &rid, &ofs, &val))
|
||||
return (NULL);
|
||||
if (!bs_write(rid, ofs, &val, sizeof(val))) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_write_2(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs;
|
||||
int rid;
|
||||
uint16_t val;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ilH", &rid, &ofs, &val))
|
||||
return (NULL);
|
||||
if (!bs_write(rid, ofs, &val, sizeof(val))) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_write_4(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs;
|
||||
int rid;
|
||||
uint32_t val;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ilI", &rid, &ofs, &val))
|
||||
return (NULL);
|
||||
if (!bs_write(rid, ofs, &val, sizeof(val))) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_map(PyObject *self, PyObject *args)
|
||||
{
|
||||
char *dev;
|
||||
int rid;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &dev))
|
||||
return (NULL);
|
||||
rid = bs_map(dev);
|
||||
if (rid == -1) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (Py_BuildValue("i", rid));
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_unmap(PyObject *self, PyObject *args)
|
||||
{
|
||||
int rid;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i", &rid))
|
||||
return (NULL);
|
||||
if (!bs_unmap(rid)) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bus_subregion(PyObject *self, PyObject *args)
|
||||
{
|
||||
long ofs, sz;
|
||||
int rid0, rid;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ill", &rid0, &ofs, &sz))
|
||||
return (NULL);
|
||||
rid = bs_subregion(rid0, ofs, sz);
|
||||
if (rid == -1) {
|
||||
PyErr_SetString(PyExc_IOError, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (Py_BuildValue("i", rid));
|
||||
}
|
||||
|
||||
static PyMethodDef bus_space_methods[] = {
|
||||
{ "read_1", bus_read_1, METH_VARARGS, "Read a 1-byte data item." },
|
||||
{ "read_2", bus_read_2, METH_VARARGS, "Read a 2-byte data item." },
|
||||
{ "read_4", bus_read_4, METH_VARARGS, "Read a 4-byte data item." },
|
||||
|
||||
{ "write_1", bus_write_1, METH_VARARGS, "Write a 1-byte data item." },
|
||||
{ "write_2", bus_write_2, METH_VARARGS, "Write a 2-byte data item." },
|
||||
{ "write_4", bus_write_4, METH_VARARGS, "Write a 4-byte data item." },
|
||||
|
||||
{ "map", bus_map, METH_VARARGS,
|
||||
"Return a resource ID for a device file created by proto(4)" },
|
||||
{ "unmap", bus_unmap, METH_VARARGS,
|
||||
"Free a resource ID" },
|
||||
{ "subregion", bus_subregion, METH_VARARGS,
|
||||
"Return a resource ID for a subregion of another resource ID" },
|
||||
|
||||
{ NULL, NULL, 0, NULL }
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC
|
||||
initbus_space(void)
|
||||
{
|
||||
|
||||
Py_InitModule("bus_space", bus_space_methods);
|
||||
}
|
263
tools/bus_space/bus_space.c
Normal file
263
tools/bus_space/bus_space.c
Normal file
@ -0,0 +1,263 @@
|
||||
/*-
|
||||
* Copyright (c) 2014 Marcel Moolenaar
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 ``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 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 <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bus_space.h"
|
||||
|
||||
#include "../../sys/dev/proto/proto_dev.h"
|
||||
|
||||
struct resource {
|
||||
int rid;
|
||||
int fd;
|
||||
long addr;
|
||||
long size;
|
||||
off_t ofs;
|
||||
caddr_t ptr;
|
||||
};
|
||||
|
||||
static struct resource *ridtbl = NULL;
|
||||
static int nrids = 0;
|
||||
|
||||
static int
|
||||
rid_alloc(void)
|
||||
{
|
||||
void *newtbl;
|
||||
int rid;
|
||||
|
||||
for (rid = 0; rid < nrids; rid++) {
|
||||
if (ridtbl[rid].fd == -1)
|
||||
break;
|
||||
}
|
||||
if (rid == nrids) {
|
||||
nrids++;
|
||||
newtbl = realloc(ridtbl, sizeof(struct resource) * nrids);
|
||||
if (newtbl == NULL) {
|
||||
nrids--;
|
||||
return (-1);
|
||||
} else
|
||||
ridtbl = newtbl;
|
||||
}
|
||||
ridtbl[rid].fd = INT_MAX;
|
||||
return (rid);
|
||||
}
|
||||
|
||||
static struct resource *
|
||||
rid_lookup(int rid)
|
||||
{
|
||||
struct resource *r;
|
||||
|
||||
if (rid < 0 || rid >= nrids) {
|
||||
errno = EINVAL;
|
||||
return (NULL);
|
||||
}
|
||||
r = ridtbl + rid;
|
||||
if (r->fd == -1) {
|
||||
errno = ENXIO;
|
||||
return (NULL);
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
int
|
||||
bs_map(const char *dev)
|
||||
{
|
||||
struct proto_ioc_region region;
|
||||
struct resource *r;
|
||||
int rid;
|
||||
|
||||
rid = rid_alloc();
|
||||
if (rid == -1)
|
||||
return (-1);
|
||||
r = rid_lookup(rid);
|
||||
if (r == NULL)
|
||||
return (-1);
|
||||
r->fd = open(dev, O_RDWR);
|
||||
if (r->fd == -1)
|
||||
return (-1);
|
||||
r->rid = -1;
|
||||
if (ioctl(r->fd, PROTO_IOC_REGION, ®ion) == -1) {
|
||||
close(r->fd);
|
||||
r->fd = -1;
|
||||
return (-1);
|
||||
}
|
||||
r->addr = region.address;
|
||||
r->size = region.size;
|
||||
r->ofs = 0;
|
||||
r->ptr = mmap(NULL, r->size, PROT_READ | PROT_WRITE,
|
||||
MAP_NOCORE | MAP_SHARED, r->fd, r->ofs);
|
||||
return (rid);
|
||||
}
|
||||
|
||||
int
|
||||
bs_read(int rid, off_t ofs, void *buf, ssize_t bufsz)
|
||||
{
|
||||
struct resource *r;
|
||||
volatile void *ptr;
|
||||
off_t o;
|
||||
ssize_t s;
|
||||
|
||||
r = rid_lookup(rid);
|
||||
if (r == NULL)
|
||||
return (0);
|
||||
if (ofs < 0 || ofs > r->size - bufsz) {
|
||||
errno = ESPIPE;
|
||||
return (0);
|
||||
}
|
||||
ofs += r->ofs;
|
||||
if (r->ptr != MAP_FAILED) {
|
||||
ptr = r->ptr + ofs;
|
||||
switch (bufsz) {
|
||||
case 1:
|
||||
*((uint8_t *)buf) = *((volatile uint8_t *)ptr);
|
||||
break;
|
||||
case 2:
|
||||
*((uint16_t *)buf) = *((volatile uint16_t *)ptr);
|
||||
break;
|
||||
case 4:
|
||||
*((uint32_t *)buf) = *((volatile uint32_t *)ptr);
|
||||
break;
|
||||
default:
|
||||
errno = EIO;
|
||||
return (0);
|
||||
}
|
||||
} else {
|
||||
o = lseek(r->fd, ofs, SEEK_SET);
|
||||
if (o != ofs)
|
||||
return (0);
|
||||
s = read(r->fd, buf, bufsz);
|
||||
if (s != bufsz)
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int
|
||||
bs_subregion(int rid0, long ofs, long sz)
|
||||
{
|
||||
struct resource *r;
|
||||
void *ptr0;
|
||||
long addr0, ofs0;
|
||||
int fd0, rid;
|
||||
|
||||
r = rid_lookup(rid0);
|
||||
if (r == NULL)
|
||||
return (-1);
|
||||
if (ofs < 0 || sz < 1) {
|
||||
errno = EINVAL;
|
||||
return (-1);
|
||||
}
|
||||
if (ofs + sz > r->size) {
|
||||
errno = ENOSPC;
|
||||
return (-1);
|
||||
}
|
||||
fd0 = r->fd;
|
||||
addr0 = r->addr;
|
||||
ofs0 = r->ofs;
|
||||
ptr0 = r->ptr;
|
||||
rid = rid_alloc();
|
||||
if (rid == -1)
|
||||
return (-1);
|
||||
r = rid_lookup(rid);
|
||||
if (r == NULL)
|
||||
return (-1);
|
||||
r->rid = rid0;
|
||||
r->fd = fd0;
|
||||
r->addr = addr0 + ofs;
|
||||
r->size = sz;
|
||||
r->ofs = ofs0 + ofs;
|
||||
r->ptr = ptr0;
|
||||
return (rid);
|
||||
}
|
||||
|
||||
int
|
||||
bs_unmap(int rid)
|
||||
{
|
||||
struct resource *r;
|
||||
|
||||
r = rid_lookup(rid);
|
||||
if (r == NULL)
|
||||
return (0);
|
||||
if (r->rid == -1) {
|
||||
if (r->ptr != MAP_FAILED)
|
||||
munmap(r->ptr, r->size);
|
||||
close(r->fd);
|
||||
}
|
||||
r->fd = -1;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int
|
||||
bs_write(int rid, off_t ofs, void *buf, ssize_t bufsz)
|
||||
{
|
||||
struct resource *r;
|
||||
volatile void *ptr;
|
||||
off_t o;
|
||||
ssize_t s;
|
||||
|
||||
r = rid_lookup(rid);
|
||||
if (r == NULL)
|
||||
return (0);
|
||||
if (ofs < 0 || ofs > r->size - bufsz) {
|
||||
errno = ESPIPE;
|
||||
return (0);
|
||||
}
|
||||
ofs += r->ofs;
|
||||
if (r->ptr != MAP_FAILED) {
|
||||
ptr = r->ptr + ofs;
|
||||
switch (bufsz) {
|
||||
case 1:
|
||||
*((volatile uint8_t *)ptr) = *((uint8_t *)buf);
|
||||
break;
|
||||
case 2:
|
||||
*((volatile uint16_t *)ptr) = *((uint16_t *)buf);
|
||||
break;
|
||||
case 4:
|
||||
*((volatile uint32_t *)ptr) = *((uint32_t *)buf);
|
||||
break;
|
||||
default:
|
||||
errno = EIO;
|
||||
return (0);
|
||||
}
|
||||
} else {
|
||||
o = lseek(r->fd, ofs, SEEK_SET);
|
||||
if (o != ofs)
|
||||
return (0);
|
||||
s = write(r->fd, buf, bufsz);
|
||||
if (s != bufsz)
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
38
tools/bus_space/bus_space.h
Normal file
38
tools/bus_space/bus_space.h
Normal file
@ -0,0 +1,38 @@
|
||||
/*-
|
||||
* Copyright (c) 2014 Marcel Moolenaar
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 ``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 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.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#ifndef _TOOLS_BUS_SPACE_H_
|
||||
#define _TOOLS_BUS_SPACE_H_
|
||||
|
||||
int bs_map(const char *dev);
|
||||
int bs_read(int rid, off_t ofs, void *buf, ssize_t bufsz);
|
||||
int bs_subregion(int rid0, long ofs, long sz);
|
||||
int bs_unmap(int rid);
|
||||
int bs_write(int rid, off_t ofs, void *buf, ssize_t bufsz);
|
||||
|
||||
#endif /* _TOOLS_BUS_SPACE_H_ */
|
Loading…
Reference in New Issue
Block a user