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$");
30 #include <sys/ioctl.h>
41 #include "../../sys/dev/proto/proto_dev.h"
52 static struct resource *ridtbl = NULL;
61 for (rid = 0; rid < nrids; rid++) {
62 if (ridtbl[rid].fd == -1)
67 newtbl = realloc(ridtbl, sizeof(struct resource) * nrids);
74 ridtbl[rid].fd = INT_MAX;
78 static struct resource *
83 if (rid < 0 || rid >= nrids) {
96 bs_map(const char *dev, const char *res)
99 struct proto_ioc_region region;
103 len = snprintf(path, PATH_MAX, "/dev/proto/%s/%s", dev, res);
104 if (len >= PATH_MAX) {
114 r->fd = open(path, O_RDWR);
118 if (ioctl(r->fd, PROTO_IOC_REGION, ®ion) == -1) {
123 r->addr = region.address;
124 r->size = region.size;
126 r->ptr = mmap(NULL, r->size, PROT_READ | PROT_WRITE,
127 MAP_NOCORE | MAP_SHARED, r->fd, r->ofs);
132 bs_read(int rid, off_t ofs, void *buf, ssize_t bufsz)
142 if (ofs < 0 || ofs > r->size - bufsz) {
147 if (r->ptr != MAP_FAILED) {
151 *((uint8_t *)buf) = *((volatile uint8_t *)ptr);
154 *((uint16_t *)buf) = *((volatile uint16_t *)ptr);
157 *((uint32_t *)buf) = *((volatile uint32_t *)ptr);
164 o = lseek(r->fd, ofs, SEEK_SET);
167 s = read(r->fd, buf, bufsz);
175 bs_subregion(int rid0, long ofs, long sz)
182 r = rid_lookup(rid0);
185 if (ofs < 0 || sz < 1) {
189 if (ofs + sz > r->size) {
205 r->addr = addr0 + ofs;
221 if (r->ptr != MAP_FAILED)
222 munmap(r->ptr, r->size);
230 bs_write(int rid, off_t ofs, void *buf, ssize_t bufsz)
240 if (ofs < 0 || ofs > r->size - bufsz) {
245 if (r->ptr != MAP_FAILED) {
249 *((volatile uint8_t *)ptr) = *((uint8_t *)buf);
252 *((volatile uint16_t *)ptr) = *((uint16_t *)buf);
255 *((volatile uint32_t *)ptr) = *((uint32_t *)buf);
262 o = lseek(r->fd, ofs, SEEK_SET);
265 s = write(r->fd, buf, bufsz);