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/param.h>
31 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <sys/reboot.h>
44 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/stdarg.h>
50 #include <dev/pci/pcivar.h>
52 #include <dev/proto/proto.h>
53 #include <dev/proto/proto_dev.h>
55 CTASSERT(SYS_RES_IRQ != PROTO_RES_UNUSED &&
56 SYS_RES_MEMORY != PROTO_RES_UNUSED &&
57 SYS_RES_IOPORT != PROTO_RES_UNUSED);
58 CTASSERT(SYS_RES_IRQ != PROTO_RES_PCICFG &&
59 SYS_RES_MEMORY != PROTO_RES_PCICFG &&
60 SYS_RES_IOPORT != PROTO_RES_PCICFG);
62 devclass_t proto_devclass;
63 char proto_driver_name[] = "proto";
65 static d_open_t proto_open;
66 static d_close_t proto_close;
67 static d_read_t proto_read;
68 static d_write_t proto_write;
69 static d_ioctl_t proto_ioctl;
70 static d_mmap_t proto_mmap;
72 struct cdevsw proto_devsw = {
73 .d_version = D_VERSION,
75 .d_name = proto_driver_name,
77 .d_close = proto_close,
79 .d_write = proto_write,
80 .d_ioctl = proto_ioctl,
84 static MALLOC_DEFINE(M_PROTO, "PROTO", "PROTO driver");
87 proto_add_resource(struct proto_softc *sc, int type, int rid,
92 if (type == PROTO_RES_UNUSED)
94 if (sc->sc_rescnt == PROTO_RES_MAX)
97 r = sc->sc_res + sc->sc_rescnt++;
106 proto_intr(void *arg)
108 struct proto_softc *sc = arg;
111 return (FILTER_HANDLED);
116 proto_attach(device_t dev)
118 struct proto_softc *sc;
122 sc = device_get_softc(dev);
125 for (res = 0; res < sc->sc_rescnt; res++) {
126 r = sc->sc_res + res;
133 r->r_size = rman_get_size(r->r_res);
134 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
135 "proto/%s/%02x.%s", device_get_desc(dev), r->r_rid,
136 (r->r_type == SYS_RES_IOPORT) ? "io" : "mem");
137 r->r_u.cdev->si_drv1 = sc;
138 r->r_u.cdev->si_drv2 = r;
140 case PROTO_RES_PCICFG:
142 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0666,
143 "proto/%s/pcicfg", device_get_desc(dev));
144 r->r_u.cdev->si_drv1 = sc;
145 r->r_u.cdev->si_drv2 = r;
153 proto_detach(device_t dev)
155 struct proto_softc *sc;
159 sc = device_get_softc(dev);
161 /* Don't detach if we have open device filess. */
162 for (res = 0; res < sc->sc_rescnt; res++) {
163 r = sc->sc_res + res;
168 for (res = 0; res < sc->sc_rescnt; res++) {
169 r = sc->sc_res + res;
176 case PROTO_RES_PCICFG:
177 destroy_dev(r->r_u.cdev);
180 if (r->r_res != NULL) {
181 bus_release_resource(dev, r->r_type, r->r_rid,
185 r->r_type = PROTO_RES_UNUSED;
196 proto_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
201 if (!atomic_cmpset_acq_ptr(&r->r_opened, 0UL, (uintptr_t)td->td_proc))
207 proto_close(struct cdev *cdev, int fflag, int devtype, struct thread *td)
212 if (!atomic_cmpset_acq_ptr(&r->r_opened, (uintptr_t)td->td_proc, 0UL))
218 proto_read(struct cdev *cdev, struct uio *uio, int ioflag)
226 struct proto_softc *sc;
237 width = uio->uio_resid;
238 if (width < 1 || width > 8 || bitcount16(width) > 1)
240 ofs = uio->uio_offset;
241 if (ofs + width > r->r_size)
246 buf.x1[0] = (r->r_type == PROTO_RES_PCICFG) ?
247 pci_read_config(dev, ofs, 1) : bus_read_1(r->r_res, ofs);
250 buf.x2[0] = (r->r_type == PROTO_RES_PCICFG) ?
251 pci_read_config(dev, ofs, 2) : bus_read_2(r->r_res, ofs);
254 buf.x4[0] = (r->r_type == PROTO_RES_PCICFG) ?
255 pci_read_config(dev, ofs, 4) : bus_read_4(r->r_res, ofs);
259 if (r->r_type == PROTO_RES_PCICFG)
261 buf.x8[0] = bus_read_8(r->r_res, ofs);
268 error = uiomove(&buf, width, uio);
273 proto_write(struct cdev *cdev, struct uio *uio, int ioflag)
281 struct proto_softc *sc;
292 width = uio->uio_resid;
293 if (width < 1 || width > 8 || bitcount16(width) > 1)
295 ofs = uio->uio_offset;
296 if (ofs + width > r->r_size)
299 error = uiomove(&buf, width, uio);
305 if (r->r_type == PROTO_RES_PCICFG)
306 pci_write_config(dev, ofs, buf.x1[0], 1);
308 bus_write_1(r->r_res, ofs, buf.x1[0]);
311 if (r->r_type == PROTO_RES_PCICFG)
312 pci_write_config(dev, ofs, buf.x2[0], 2);
314 bus_write_2(r->r_res, ofs, buf.x2[0]);
317 if (r->r_type == PROTO_RES_PCICFG)
318 pci_write_config(dev, ofs, buf.x4[0], 4);
320 bus_write_4(r->r_res, ofs, buf.x4[0]);
324 if (r->r_type == PROTO_RES_PCICFG)
326 bus_write_8(r->r_res, ofs, buf.x8[0]);
337 proto_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
340 struct proto_ioc_region *region;
348 case PROTO_IOC_REGION:
349 region = (struct proto_ioc_region *)data;
350 region->size = r->r_size;
351 if (r->r_type == PROTO_RES_PCICFG)
354 region->address = rman_get_start(r->r_res);
364 proto_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
365 int prot, vm_memattr_t *memattr)
371 if (r->r_type != SYS_RES_MEMORY)
373 if (offset & PAGE_MASK)
375 if (prot & PROT_EXEC)
377 if (offset >= r->r_size)
379 *paddr = rman_get_start(r->r_res) + offset;
381 *memattr = VM_MEMATTR_UNCACHEABLE;