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 #include "busdma.h" 34 35 static PyObject * 36 bus_read_1(PyObject *self, PyObject *args) 37 { 38 long ofs; 39 int rid; 40 uint8_t val; 41 42 if (!PyArg_ParseTuple(args, "il", &rid, &ofs)) 43 return (NULL); 44 if (!bs_read(rid, ofs, &val, sizeof(val))) { 45 PyErr_SetString(PyExc_IOError, strerror(errno)); 46 return (NULL); 47 } 48 return (Py_BuildValue("B", val)); 49 } 50 51 static PyObject * 52 bus_read_2(PyObject *self, PyObject *args) 53 { 54 long ofs; 55 int rid; 56 uint16_t val; 57 58 if (!PyArg_ParseTuple(args, "il", &rid, &ofs)) 59 return (NULL); 60 if (!bs_read(rid, ofs, &val, sizeof(val))) { 61 PyErr_SetString(PyExc_IOError, strerror(errno)); 62 return (NULL); 63 } 64 return (Py_BuildValue("H", val)); 65 } 66 67 static PyObject * 68 bus_read_4(PyObject *self, PyObject *args) 69 { 70 long ofs; 71 int rid; 72 uint32_t val; 73 74 if (!PyArg_ParseTuple(args, "il", &rid, &ofs)) 75 return (NULL); 76 if (!bs_read(rid, ofs, &val, sizeof(val))) { 77 PyErr_SetString(PyExc_IOError, strerror(errno)); 78 return (NULL); 79 } 80 return (Py_BuildValue("I", val)); 81 } 82 83 static PyObject * 84 bus_write_1(PyObject *self, PyObject *args) 85 { 86 long ofs; 87 int rid; 88 uint8_t val; 89 90 if (!PyArg_ParseTuple(args, "ilB", &rid, &ofs, &val)) 91 return (NULL); 92 if (!bs_write(rid, ofs, &val, sizeof(val))) { 93 PyErr_SetString(PyExc_IOError, strerror(errno)); 94 return (NULL); 95 } 96 Py_RETURN_NONE; 97 } 98 99 static PyObject * 100 bus_write_2(PyObject *self, PyObject *args) 101 { 102 long ofs; 103 int rid; 104 uint16_t val; 105 106 if (!PyArg_ParseTuple(args, "ilH", &rid, &ofs, &val)) 107 return (NULL); 108 if (!bs_write(rid, ofs, &val, sizeof(val))) { 109 PyErr_SetString(PyExc_IOError, strerror(errno)); 110 return (NULL); 111 } 112 Py_RETURN_NONE; 113 } 114 115 static PyObject * 116 bus_write_4(PyObject *self, PyObject *args) 117 { 118 long ofs; 119 int rid; 120 uint32_t val; 121 122 if (!PyArg_ParseTuple(args, "ilI", &rid, &ofs, &val)) 123 return (NULL); 124 if (!bs_write(rid, ofs, &val, sizeof(val))) { 125 PyErr_SetString(PyExc_IOError, strerror(errno)); 126 return (NULL); 127 } 128 Py_RETURN_NONE; 129 } 130 131 static PyObject * 132 bus_map(PyObject *self, PyObject *args) 133 { 134 char *dev; 135 int rid; 136 137 if (!PyArg_ParseTuple(args, "s", &dev)) 138 return (NULL); 139 rid = bs_map(dev); 140 if (rid == -1) { 141 PyErr_SetString(PyExc_IOError, strerror(errno)); 142 return (NULL); 143 } 144 return (Py_BuildValue("i", rid)); 145 } 146 147 static PyObject * 148 bus_unmap(PyObject *self, PyObject *args) 149 { 150 int rid; 151 152 if (!PyArg_ParseTuple(args, "i", &rid)) 153 return (NULL); 154 if (!bs_unmap(rid)) { 155 PyErr_SetString(PyExc_IOError, strerror(errno)); 156 return (NULL); 157 } 158 Py_RETURN_NONE; 159 } 160 161 static PyObject * 162 bus_subregion(PyObject *self, PyObject *args) 163 { 164 long ofs, sz; 165 int rid0, rid; 166 167 if (!PyArg_ParseTuple(args, "ill", &rid0, &ofs, &sz)) 168 return (NULL); 169 rid = bs_subregion(rid0, ofs, sz); 170 if (rid == -1) { 171 PyErr_SetString(PyExc_IOError, strerror(errno)); 172 return (NULL); 173 } 174 return (Py_BuildValue("i", rid)); 175 } 176 177 static PyObject * 178 busdma_tag_create(PyObject *self, PyObject *args) 179 { 180 char *dev; 181 long align, bndry, maxaddr, maxsz, maxsegsz; 182 int tid, nsegs, datarate, flags; 183 184 if (!PyArg_ParseTuple(args, "sllllilii", &dev, &align, &bndry, 185 &maxaddr, &maxsz, &nsegs, &maxsegsz, &datarate, &flags)) 186 return (NULL); 187 tid = bd_tag_create(dev, align, bndry, maxaddr, maxsz, nsegs, 188 maxsegsz, datarate, flags); 189 if (tid == -1) { 190 PyErr_SetString(PyExc_IOError, strerror(errno)); 191 return (NULL); 192 } 193 return (Py_BuildValue("i", tid)); 194 } 195 196 static PyObject * 197 busdma_tag_derive(PyObject *self, PyObject *args) 198 { 199 long align, bndry, maxaddr, maxsz, maxsegsz; 200 int ptid, tid, nsegs, datarate, flags; 201 202 if (!PyArg_ParseTuple(args, "illllilii", &ptid, &align, &bndry, 203 &maxaddr, &maxsz, &nsegs, &maxsegsz, &datarate, &flags)) 204 return (NULL); 205 tid = bd_tag_derive(ptid, align, bndry, maxaddr, maxsz, nsegs, 206 maxsegsz, datarate, flags); 207 if (tid == -1) { 208 PyErr_SetString(PyExc_IOError, strerror(errno)); 209 return (NULL); 210 } 211 return (Py_BuildValue("i", tid)); 212 } 213 214 static PyObject * 215 busdma_tag_destroy(PyObject *self, PyObject *args) 216 { 217 int error, tid; 218 219 if (!PyArg_ParseTuple(args, "i", &tid)) 220 return (NULL); 221 error = bd_tag_destroy(tid); 222 if (error) { 223 PyErr_SetString(PyExc_IOError, strerror(error)); 224 return (NULL); 225 } 226 Py_RETURN_NONE; 227 } 228 229 static PyMethodDef bus_space_methods[] = { 230 { "read_1", bus_read_1, METH_VARARGS, "Read a 1-byte data item." }, 231 { "read_2", bus_read_2, METH_VARARGS, "Read a 2-byte data item." }, 232 { "read_4", bus_read_4, METH_VARARGS, "Read a 4-byte data item." }, 233 234 { "write_1", bus_write_1, METH_VARARGS, "Write a 1-byte data item." }, 235 { "write_2", bus_write_2, METH_VARARGS, "Write a 2-byte data item." }, 236 { "write_4", bus_write_4, METH_VARARGS, "Write a 4-byte data item." }, 237 238 { "map", bus_map, METH_VARARGS, 239 "Return a resource ID for a device file created by proto(4)" }, 240 { "unmap", bus_unmap, METH_VARARGS, 241 "Free a resource ID" }, 242 { "subregion", bus_subregion, METH_VARARGS, 243 "Return a resource ID for a subregion of another resource ID" }, 244 245 { NULL, NULL, 0, NULL } 246 }; 247 248 static PyMethodDef busdma_methods[] = { 249 { "tag_create", busdma_tag_create, METH_VARARGS, "Create a root tag." }, 250 { "tag_derive", busdma_tag_derive, METH_VARARGS, "Derive a child tag." }, 251 { "tag_destroy", busdma_tag_destroy, METH_VARARGS, "Destroy a tag." }, 252 { NULL, NULL, 0, NULL } 253 }; 254 255 PyMODINIT_FUNC 256 initbus_space(void) 257 { 258 259 Py_InitModule("bus_space", bus_space_methods); 260 Py_InitModule("busdma", busdma_methods); 261 } 262