2 * Copyright (c) 2014 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "bus_space.h"
35 bus_read_1(PyObject *self, PyObject *args)
41 if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
43 if (!bs_read(rid, ofs, &val, sizeof(val))) {
44 PyErr_SetString(PyExc_IOError, strerror(errno));
47 return (Py_BuildValue("B", val));
51 bus_read_2(PyObject *self, PyObject *args)
57 if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
59 if (!bs_read(rid, ofs, &val, sizeof(val))) {
60 PyErr_SetString(PyExc_IOError, strerror(errno));
63 return (Py_BuildValue("H", val));
67 bus_read_4(PyObject *self, PyObject *args)
73 if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
75 if (!bs_read(rid, ofs, &val, sizeof(val))) {
76 PyErr_SetString(PyExc_IOError, strerror(errno));
79 return (Py_BuildValue("I", val));
83 bus_write_1(PyObject *self, PyObject *args)
89 if (!PyArg_ParseTuple(args, "ilB", &rid, &ofs, &val))
91 if (!bs_write(rid, ofs, &val, sizeof(val))) {
92 PyErr_SetString(PyExc_IOError, strerror(errno));
99 bus_write_2(PyObject *self, PyObject *args)
105 if (!PyArg_ParseTuple(args, "ilH", &rid, &ofs, &val))
107 if (!bs_write(rid, ofs, &val, sizeof(val))) {
108 PyErr_SetString(PyExc_IOError, strerror(errno));
115 bus_write_4(PyObject *self, PyObject *args)
121 if (!PyArg_ParseTuple(args, "ilI", &rid, &ofs, &val))
123 if (!bs_write(rid, ofs, &val, sizeof(val))) {
124 PyErr_SetString(PyExc_IOError, strerror(errno));
131 bus_map(PyObject *self, PyObject *args)
136 if (!PyArg_ParseTuple(args, "s", &dev))
140 PyErr_SetString(PyExc_IOError, strerror(errno));
143 return (Py_BuildValue("i", rid));
147 bus_unmap(PyObject *self, PyObject *args)
151 if (!PyArg_ParseTuple(args, "i", &rid))
153 if (!bs_unmap(rid)) {
154 PyErr_SetString(PyExc_IOError, strerror(errno));
161 bus_subregion(PyObject *self, PyObject *args)
166 if (!PyArg_ParseTuple(args, "ill", &rid0, &ofs, &sz))
168 rid = bs_subregion(rid0, ofs, sz);
170 PyErr_SetString(PyExc_IOError, strerror(errno));
173 return (Py_BuildValue("i", rid));
176 static PyMethodDef bus_space_methods[] = {
177 { "read_1", bus_read_1, METH_VARARGS, "Read a 1-byte data item." },
178 { "read_2", bus_read_2, METH_VARARGS, "Read a 2-byte data item." },
179 { "read_4", bus_read_4, METH_VARARGS, "Read a 4-byte data item." },
181 { "write_1", bus_write_1, METH_VARARGS, "Write a 1-byte data item." },
182 { "write_2", bus_write_2, METH_VARARGS, "Write a 2-byte data item." },
183 { "write_4", bus_write_4, METH_VARARGS, "Write a 4-byte data item." },
185 { "map", bus_map, METH_VARARGS,
186 "Return a resource ID for a device file created by proto(4)" },
187 { "unmap", bus_unmap, METH_VARARGS,
188 "Free a resource ID" },
189 { "subregion", bus_subregion, METH_VARARGS,
190 "Return a resource ID for a subregion of another resource ID" },
192 { NULL, NULL, 0, NULL }
199 Py_InitModule("bus_space", bus_space_methods);