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 #include <sys/ioctl.h>
39 #include "../../sys/dev/proto/proto_dev.h"
50 static struct resource *ridtbl = NULL;
59 for (rid = 0; rid < nrids; rid++) {
60 if (ridtbl[rid].fd == -1)
65 newtbl = realloc(ridtbl, sizeof(struct resource) * nrids);
72 ridtbl[rid].fd = INT_MAX;
76 static struct resource *
81 if (rid < 0 || rid >= nrids) {
94 bs_map(const char *dev, const char *res)
97 struct proto_ioc_region region;
101 len = snprintf(path, PATH_MAX, "/dev/proto/%s/%s", dev, res);
102 if (len >= PATH_MAX) {
112 r->fd = open(path, O_RDWR);
116 if (ioctl(r->fd, PROTO_IOC_REGION, ®ion) == -1) {
121 r->addr = region.address;
122 r->size = region.size;
124 r->ptr = mmap(NULL, r->size, PROT_READ | PROT_WRITE,
125 MAP_NOCORE | MAP_SHARED, r->fd, r->ofs);
130 bs_read(int rid, off_t ofs, void *buf, ssize_t bufsz)
140 if (ofs < 0 || ofs > r->size - bufsz) {
145 if (r->ptr != MAP_FAILED) {
149 *((uint8_t *)buf) = *((volatile uint8_t *)ptr);
152 *((uint16_t *)buf) = *((volatile uint16_t *)ptr);
155 *((uint32_t *)buf) = *((volatile uint32_t *)ptr);
162 o = lseek(r->fd, ofs, SEEK_SET);
165 s = read(r->fd, buf, bufsz);
173 bs_subregion(int rid0, long ofs, long sz)
180 r = rid_lookup(rid0);
183 if (ofs < 0 || sz < 1) {
187 if (ofs + sz > r->size) {
203 r->addr = addr0 + ofs;
219 if (r->ptr != MAP_FAILED)
220 munmap(r->ptr, r->size);
228 bs_write(int rid, off_t ofs, void *buf, ssize_t bufsz)
238 if (ofs < 0 || ofs > r->size - bufsz) {
243 if (r->ptr != MAP_FAILED) {
247 *((volatile uint8_t *)ptr) = *((uint8_t *)buf);
250 *((volatile uint16_t *)ptr) = *((uint16_t *)buf);
253 *((volatile uint32_t *)ptr) = *((uint32_t *)buf);
260 o = lseek(r->fd, ofs, SEEK_SET);
263 s = write(r->fd, buf, bufsz);