Index: head/tools/bus_space/bus_space.c =================================================================== --- head/tools/bus_space/bus_space.c (revision 284227) +++ head/tools/bus_space/bus_space.c (nonexistent) @@ -1,263 +0,0 @@ -/*- - * 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 -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include - -#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); -} Property changes on: head/tools/bus_space/bus_space.c ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/tools/bus_space/bus_space.h =================================================================== --- head/tools/bus_space/bus_space.h (revision 284227) +++ head/tools/bus_space/bus_space.h (nonexistent) @@ -1,38 +0,0 @@ -/*- - * 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_ */ Property changes on: head/tools/bus_space/bus_space.h ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/tools/bus_space/C/libbus_space.h =================================================================== --- head/tools/bus_space/C/libbus_space.h (revision 284227) +++ head/tools/bus_space/C/libbus_space.h (nonexistent) @@ -1,60 +0,0 @@ -/*- - * Copyright (c) 2014, 2015 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); -int16_t bus_space_read_1(int rid, long ofs); -int32_t 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); - -typedef unsigned long bus_addr_t; -typedef unsigned long bus_size_t; -typedef int busdma_tag_t; -typedef int busdma_md_t; - -int busdma_tag_create(const char *dev, bus_addr_t align, bus_addr_t bndry, - bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, - bus_size_t maxsegsz, u_int datarate, u_int flags, - busdma_tag_t *out_p); -int busdma_tag_derive(busdma_tag_t tag, bus_addr_t align, bus_addr_t bndry, - bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, - bus_size_t maxsegsz, u_int datarate, u_int flags, - busdma_tag_t *out_p); -int busdma_tag_destroy(busdma_tag_t tag); - -int busdma_mem_alloc(busdma_tag_t tag, u_int flags, busdma_md_t *out_p); -int busdma_mem_free(busdma_md_t md); - -#endif /* _LIBBUS_SPACE_H_ */ Property changes on: head/tools/bus_space/C/libbus_space.h ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:keywords ## -1 +0,0 ## -FreeBSD=%H \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/tools/bus_space/C/Makefile =================================================================== --- head/tools/bus_space/C/Makefile (revision 284227) +++ head/tools/bus_space/C/Makefile (revision 284228) @@ -1,10 +1,10 @@ # $FreeBSD$ -LIB= bus_space +LIB= bus SHLIB_MAJOR= 0 SRCS= lang.c -INCS= libbus_space.h +INCS= libbus.h CFLAGS+= -I${.CURDIR}/.. .include Index: head/tools/bus_space/C/lang.c =================================================================== --- head/tools/bus_space/C/lang.c (revision 284227) +++ head/tools/bus_space/C/lang.c (revision 284228) @@ -1,157 +1,157 @@ /*- * 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 __FBSDID("$FreeBSD$"); #include #include -#include "bus_space.h" +#include "bus.h" #include "busdma.h" -#include "libbus_space.h" +#include "libbus.h" int16_t -bus_space_read_1(int rid, long ofs) +bus_read_1(int rid, long ofs) { uint8_t val; return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val); } int32_t -bus_space_read_2(int rid, long ofs) +bus_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) +bus_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) +bus_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) +bus_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) +bus_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) +bus_map(const char *dev) { return (bs_map(dev)); } int -bus_space_unmap(int rid) +bus_unmap(int rid) { return ((!bs_unmap(rid)) ? errno : 0); } int -bus_space_subregion(int rid, long ofs, long sz) +bus_subregion(int rid, long ofs, long sz) { return (bs_subregion(rid, ofs, sz)); } int busdma_tag_create(const char *dev, bus_addr_t align, bus_addr_t bndry, bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz, u_int datarate, u_int flags, busdma_tag_t *out_p) { int res; res = bd_tag_create(dev, align, bndry, maxaddr, maxsz, nsegs, maxsegsz, datarate, flags); if (res == -1) return (errno); *out_p = res; return (0); } int busdma_tag_derive(busdma_tag_t tag, bus_addr_t align, bus_addr_t bndry, bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz, u_int datarate, u_int flags, busdma_tag_t *out_p) { int res; res = bd_tag_derive(tag, align, bndry, maxaddr, maxsz, nsegs, maxsegsz, datarate, flags); if (res == -1) return (errno); *out_p = res; return (0); } int busdma_tag_destroy(busdma_tag_t tag) { return (bd_tag_destroy(tag)); } int busdma_mem_alloc(busdma_tag_t tag, u_int flags, busdma_md_t *out_p) { int res; res = bd_mem_alloc(tag, flags); if (res == -1) return (errno); *out_p = res; return (0); } int busdma_mem_free(busdma_md_t md) { return (bd_mem_free(md)); } Index: head/tools/bus_space/C/libbus.h =================================================================== --- head/tools/bus_space/C/libbus.h (nonexistent) +++ head/tools/bus_space/C/libbus.h (revision 284228) @@ -0,0 +1,60 @@ +/*- + * Copyright (c) 2014, 2015 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_map(const char *dev); +int16_t bus_read_1(int rid, long ofs); +int32_t bus_read_2(int rid, long ofs); +int64_t bus_read_4(int rid, long ofs); +int bus_subregion(int rid, long ofs, long sz); +int bus_unmap(int rid); +int bus_write_1(int rid, long ofs, uint8_t val); +int bus_write_2(int rid, long ofs, uint16_t val); +int bus_write_4(int rid, long ofs, uint32_t val); + +typedef unsigned long bus_addr_t; +typedef unsigned long bus_size_t; +typedef int busdma_tag_t; +typedef int busdma_md_t; + +int busdma_tag_create(const char *dev, bus_addr_t align, bus_addr_t bndry, + bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, + bus_size_t maxsegsz, u_int datarate, u_int flags, + busdma_tag_t *out_p); +int busdma_tag_derive(busdma_tag_t tag, bus_addr_t align, bus_addr_t bndry, + bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, + bus_size_t maxsegsz, u_int datarate, u_int flags, + busdma_tag_t *out_p); +int busdma_tag_destroy(busdma_tag_t tag); + +int busdma_mem_alloc(busdma_tag_t tag, u_int flags, busdma_md_t *out_p); +int busdma_mem_free(busdma_md_t md); + +#endif /* _LIBBUS_SPACE_H_ */ Property changes on: head/tools/bus_space/C/libbus.h ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/tools/bus_space/Makefile.inc =================================================================== --- head/tools/bus_space/Makefile.inc (revision 284227) +++ head/tools/bus_space/Makefile.inc (revision 284228) @@ -1,4 +1,4 @@ # $FreeBSD$ .PATH: ${.CURDIR}/.. -SRCS+= bus_space.c busdma.c +SRCS+= bus.c busdma.c Index: head/tools/bus_space/Python/Makefile =================================================================== --- head/tools/bus_space/Python/Makefile (revision 284227) +++ head/tools/bus_space/Python/Makefile (revision 284228) @@ -1,9 +1,9 @@ # $FreeBSD$ -SHLIB_NAME= bus_space.so +SHLIB_NAME= bus.so SRCS= lang.c CFLAGS+= -I${.CURDIR}/.. -I/usr/local/include/python2.7 LDFLAGS+= -L/usr/local/lib -lpython2.7 .include Index: head/tools/bus_space/Python/lang.c =================================================================== --- head/tools/bus_space/Python/lang.c (revision 284227) +++ head/tools/bus_space/Python/lang.c (revision 284228) @@ -1,301 +1,301 @@ /*- * 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 __FBSDID("$FreeBSD$"); #include -#include "bus_space.h" +#include "bus.h" #include "busdma.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 PyObject * busdma_tag_create(PyObject *self, PyObject *args) { char *dev; u_long align, bndry, maxaddr, maxsz, maxsegsz; u_int nsegs, datarate, flags; int tid; if (!PyArg_ParseTuple(args, "skkkkIkII", &dev, &align, &bndry, &maxaddr, &maxsz, &nsegs, &maxsegsz, &datarate, &flags)) return (NULL); tid = bd_tag_create(dev, align, bndry, maxaddr, maxsz, nsegs, maxsegsz, datarate, flags); if (tid == -1) { PyErr_SetString(PyExc_IOError, strerror(errno)); return (NULL); } return (Py_BuildValue("i", tid)); } static PyObject * busdma_tag_derive(PyObject *self, PyObject *args) { u_long align, bndry, maxaddr, maxsz, maxsegsz; u_int nsegs, datarate, flags; int ptid, tid; if (!PyArg_ParseTuple(args, "ikkkkIkII", &ptid, &align, &bndry, &maxaddr, &maxsz, &nsegs, &maxsegsz, &datarate, &flags)) return (NULL); tid = bd_tag_derive(ptid, align, bndry, maxaddr, maxsz, nsegs, maxsegsz, datarate, flags); if (tid == -1) { PyErr_SetString(PyExc_IOError, strerror(errno)); return (NULL); } return (Py_BuildValue("i", tid)); } static PyObject * busdma_tag_destroy(PyObject *self, PyObject *args) { int error, tid; if (!PyArg_ParseTuple(args, "i", &tid)) return (NULL); error = bd_tag_destroy(tid); if (error) { PyErr_SetString(PyExc_IOError, strerror(error)); return (NULL); } Py_RETURN_NONE; } static PyObject * busdma_mem_alloc(PyObject *self, PyObject *args) { u_int flags; int mdid, tid; if (!PyArg_ParseTuple(args, "iI", &tid, &flags)) return (NULL); mdid = bd_mem_alloc(tid, flags); if (mdid == -1) { PyErr_SetString(PyExc_IOError, strerror(errno)); return (NULL); } return (Py_BuildValue("i", mdid)); } static PyObject * busdma_mem_free(PyObject *self, PyObject *args) { int error, mdid; if (!PyArg_ParseTuple(args, "i", &mdid)) return (NULL); error = bd_mem_free(mdid); if (error) { PyErr_SetString(PyExc_IOError, strerror(error)); return (NULL); } Py_RETURN_NONE; } -static PyMethodDef bus_space_methods[] = { +static PyMethodDef bus_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 } }; static PyMethodDef busdma_methods[] = { { "tag_create", busdma_tag_create, METH_VARARGS, "Create a root tag." }, { "tag_derive", busdma_tag_derive, METH_VARARGS, "Derive a child tag." }, { "tag_destroy", busdma_tag_destroy, METH_VARARGS, "Destroy a tag." }, { "mem_alloc", busdma_mem_alloc, METH_VARARGS, "Allocate memory according to the DMA constraints." }, { "mem_free", busdma_mem_free, METH_VARARGS, "Free allocated memory." }, { NULL, NULL, 0, NULL } }; PyMODINIT_FUNC -initbus_space(void) +initbus(void) { - Py_InitModule("bus_space", bus_space_methods); + Py_InitModule("bus", bus_methods); Py_InitModule("busdma", busdma_methods); } Index: head/tools/bus_space/bus.c =================================================================== --- head/tools/bus_space/bus.c (nonexistent) +++ head/tools/bus_space/bus.c (revision 284228) @@ -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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include "bus.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); +} Property changes on: head/tools/bus_space/bus.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/tools/bus_space/bus.h =================================================================== --- head/tools/bus_space/bus.h (nonexistent) +++ head/tools/bus_space/bus.h (revision 284228) @@ -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_ */ Property changes on: head/tools/bus_space/bus.h ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property