]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/bus_space/Python/lang.c
MFV r267565:
[FreeBSD/FreeBSD.git] / tools / bus_space / Python / lang.c
1 /*-
2  * Copyright (c) 2014 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <Python.h>
31
32 #include "bus_space.h"
33
34 static PyObject *
35 bus_read_1(PyObject *self, PyObject *args)
36 {
37         long ofs;
38         int rid;
39         uint8_t val;
40
41         if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
42                 return (NULL);
43         if (!bs_read(rid, ofs, &val, sizeof(val))) {
44                 PyErr_SetString(PyExc_IOError, strerror(errno));
45                 return (NULL);
46         }
47         return (Py_BuildValue("B", val));
48 }
49
50 static PyObject *
51 bus_read_2(PyObject *self, PyObject *args)
52 {
53         long ofs;
54         int rid;
55         uint16_t val;
56
57         if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
58                 return (NULL);
59         if (!bs_read(rid, ofs, &val, sizeof(val))) {
60                 PyErr_SetString(PyExc_IOError, strerror(errno));
61                 return (NULL);
62         }
63         return (Py_BuildValue("H", val));
64 }
65
66 static PyObject *
67 bus_read_4(PyObject *self, PyObject *args)
68 {
69         long ofs;
70         int rid;
71         uint32_t val;
72
73         if (!PyArg_ParseTuple(args, "il", &rid, &ofs))
74                 return (NULL);
75         if (!bs_read(rid, ofs, &val, sizeof(val))) {
76                 PyErr_SetString(PyExc_IOError, strerror(errno));
77                 return (NULL);
78         }
79         return (Py_BuildValue("I", val));
80 }
81
82 static PyObject *
83 bus_write_1(PyObject *self, PyObject *args)
84 {
85         long ofs;
86         int rid;
87         uint8_t val;
88
89         if (!PyArg_ParseTuple(args, "ilB", &rid, &ofs, &val))
90                 return (NULL);
91         if (!bs_write(rid, ofs, &val, sizeof(val))) {
92                 PyErr_SetString(PyExc_IOError, strerror(errno));
93                 return (NULL);
94         }
95         Py_RETURN_NONE;
96 }
97
98 static PyObject *
99 bus_write_2(PyObject *self, PyObject *args)
100 {
101         long ofs;
102         int rid;
103         uint16_t val;
104
105         if (!PyArg_ParseTuple(args, "ilH", &rid, &ofs, &val))
106                 return (NULL);
107         if (!bs_write(rid, ofs, &val, sizeof(val))) {
108                 PyErr_SetString(PyExc_IOError, strerror(errno));
109                 return (NULL);
110         }
111         Py_RETURN_NONE;
112 }
113
114 static PyObject *
115 bus_write_4(PyObject *self, PyObject *args)
116 {
117         long ofs;
118         int rid;
119         uint32_t val;
120
121         if (!PyArg_ParseTuple(args, "ilI", &rid, &ofs, &val))
122                 return (NULL);
123         if (!bs_write(rid, ofs, &val, sizeof(val))) {
124                 PyErr_SetString(PyExc_IOError, strerror(errno));
125                 return (NULL);
126         }
127         Py_RETURN_NONE;
128 }
129
130 static PyObject *
131 bus_map(PyObject *self, PyObject *args)
132 {
133         char *dev;
134         int rid;
135
136         if (!PyArg_ParseTuple(args, "s", &dev))
137                 return (NULL);
138         rid = bs_map(dev);
139         if (rid == -1) {
140                 PyErr_SetString(PyExc_IOError, strerror(errno));
141                 return (NULL);
142         }
143         return (Py_BuildValue("i", rid));
144 }
145
146 static PyObject *
147 bus_unmap(PyObject *self, PyObject *args)
148 {
149         int rid;
150
151         if (!PyArg_ParseTuple(args, "i", &rid))
152                 return (NULL);
153         if (!bs_unmap(rid)) {
154                 PyErr_SetString(PyExc_IOError, strerror(errno));
155                 return (NULL);
156         }
157         Py_RETURN_NONE;
158 }
159
160 static PyObject *
161 bus_subregion(PyObject *self, PyObject *args)
162 {
163         long ofs, sz;
164         int rid0, rid;
165
166         if (!PyArg_ParseTuple(args, "ill", &rid0, &ofs, &sz))
167                 return (NULL);
168         rid = bs_subregion(rid0, ofs, sz);
169         if (rid == -1) {
170                 PyErr_SetString(PyExc_IOError, strerror(errno));
171                 return (NULL);
172         }
173         return (Py_BuildValue("i", rid));
174 }
175
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." },
180
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." },
184
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" },
191
192     { NULL, NULL, 0, NULL }
193 };
194
195 PyMODINIT_FUNC
196 initbus_space(void)
197 {
198
199         Py_InitModule("bus_space", bus_space_methods);
200 }