2 * Copyright (c) 2014, 2015, 2019 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>
54 #include <dev/proto/proto_busdma.h>
56 CTASSERT(SYS_RES_IRQ != PROTO_RES_UNUSED &&
57 SYS_RES_DRQ != PROTO_RES_UNUSED &&
58 SYS_RES_MEMORY != PROTO_RES_UNUSED &&
59 SYS_RES_IOPORT != PROTO_RES_UNUSED);
60 CTASSERT(SYS_RES_IRQ != PROTO_RES_PCICFG &&
61 SYS_RES_DRQ != PROTO_RES_PCICFG &&
62 SYS_RES_MEMORY != PROTO_RES_PCICFG &&
63 SYS_RES_IOPORT != PROTO_RES_PCICFG);
64 CTASSERT(SYS_RES_IRQ != PROTO_RES_BUSDMA &&
65 SYS_RES_DRQ != PROTO_RES_BUSDMA &&
66 SYS_RES_MEMORY != PROTO_RES_BUSDMA &&
67 SYS_RES_IOPORT != PROTO_RES_BUSDMA);
69 char proto_driver_name[] = "proto";
71 static d_open_t proto_open;
72 static d_close_t proto_close;
73 static d_read_t proto_read;
74 static d_write_t proto_write;
75 static d_ioctl_t proto_ioctl;
76 static d_mmap_t proto_mmap;
78 struct cdevsw proto_devsw = {
79 .d_version = D_VERSION,
81 .d_name = proto_driver_name,
83 .d_close = proto_close,
85 .d_write = proto_write,
86 .d_ioctl = proto_ioctl,
90 static MALLOC_DEFINE(M_PROTO, "PROTO", "PROTO driver");
93 proto_add_resource(struct proto_softc *sc, int type, int rid,
98 if (type == PROTO_RES_UNUSED)
100 if (sc->sc_rescnt == PROTO_RES_MAX)
103 r = sc->sc_res + sc->sc_rescnt++;
112 proto_intr(void *arg)
114 struct proto_softc *sc = arg;
117 return (FILTER_HANDLED);
122 proto_probe(device_t dev, const char *prefix, char ***devnamesp)
124 char **devnames = *devnamesp;
125 const char *dn, *ep, *ev;
129 if (devnames == NULL) {
130 pfxlen = strlen(prefix);
131 names = 1; /* NULL pointer */
132 ev = kern_getenv("hw.proto.attach");
135 while (*dn != '\0') {
137 while (*ep != ',' && *ep != '\0')
139 if ((ep - dn) > pfxlen &&
140 strncmp(dn, prefix, pfxlen) == 0)
142 dn = (*ep == ',') ? ep + 1 : ep;
145 devnames = malloc(names * sizeof(caddr_t), M_DEVBUF,
147 *devnamesp = devnames;
151 while (*dn != '\0') {
153 while (*ep != ',' && *ep != '\0')
155 if ((ep - dn) > pfxlen &&
156 strncmp(dn, prefix, pfxlen) == 0) {
157 devnames[idx] = malloc(ep - dn + 1,
158 M_DEVBUF, M_WAITOK | M_ZERO);
159 memcpy(devnames[idx], dn, ep - dn);
162 dn = (*ep == ',') ? ep + 1 : ep;
164 freeenv(__DECONST(char *, ev));
168 dn = device_get_desc(dev);
169 while (*devnames != NULL) {
170 if (strcmp(dn, *devnames) == 0)
171 return (BUS_PROBE_SPECIFIC);
174 return (BUS_PROBE_HOOVER);
178 proto_attach(device_t dev)
180 struct proto_softc *sc;
184 sc = device_get_softc(dev);
186 mtx_init(&sc->sc_mtx, "proto-softc", NULL, MTX_DEF);
188 for (res = 0; res < sc->sc_rescnt; res++) {
189 r = sc->sc_res + res;
198 r->r_size = rman_get_size(r->r_d.res);
199 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600,
200 "proto/%s/%02x.%s", device_get_desc(dev), r->r_rid,
201 (r->r_type == SYS_RES_IOPORT) ? "io" : "mem");
202 r->r_u.cdev->si_drv1 = sc;
203 r->r_u.cdev->si_drv2 = r;
205 case PROTO_RES_PCICFG:
207 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600,
208 "proto/%s/pcicfg", device_get_desc(dev));
209 r->r_u.cdev->si_drv1 = sc;
210 r->r_u.cdev->si_drv2 = r;
212 case PROTO_RES_BUSDMA:
213 r->r_d.busdma = proto_busdma_attach(sc);
214 r->r_size = 0; /* no read(2) nor write(2) */
215 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600,
216 "proto/%s/busdma", device_get_desc(dev));
217 r->r_u.cdev->si_drv1 = sc;
218 r->r_u.cdev->si_drv2 = r;
226 proto_detach(device_t dev)
228 struct proto_softc *sc;
232 sc = device_get_softc(dev);
234 mtx_lock(&sc->sc_mtx);
235 if (sc->sc_opencnt == 0)
237 mtx_unlock(&sc->sc_mtx);
238 if (sc->sc_opencnt > 0)
241 for (res = 0; res < sc->sc_rescnt; res++) {
242 r = sc->sc_res + res;
247 bus_release_resource(dev, r->r_type, r->r_rid,
251 bus_release_resource(dev, r->r_type, r->r_rid,
256 destroy_dev(r->r_u.cdev);
257 bus_release_resource(dev, r->r_type, r->r_rid,
260 case PROTO_RES_PCICFG:
261 destroy_dev(r->r_u.cdev);
263 case PROTO_RES_BUSDMA:
264 destroy_dev(r->r_u.cdev);
265 proto_busdma_detach(sc, r->r_d.busdma);
268 r->r_type = PROTO_RES_UNUSED;
270 mtx_lock(&sc->sc_mtx);
273 mtx_unlock(&sc->sc_mtx);
274 mtx_destroy(&sc->sc_mtx);
283 proto_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
286 struct proto_softc *sc;
290 mtx_lock(&sc->sc_mtx);
291 if (sc->sc_opencnt >= 0) {
301 mtx_unlock(&sc->sc_mtx);
306 proto_close(struct cdev *cdev, int fflag, int devtype, struct thread *td)
309 struct proto_softc *sc;
313 mtx_lock(&sc->sc_mtx);
314 if (sc->sc_opencnt > 0) {
317 if (r->r_type == PROTO_RES_BUSDMA)
318 proto_busdma_cleanup(sc, r->r_d.busdma);
326 mtx_unlock(&sc->sc_mtx);
331 proto_read(struct cdev *cdev, struct uio *uio, int ioflag)
339 struct proto_softc *sc;
350 width = uio->uio_resid;
351 if (width < 1 || width > 8 || bitcount16(width) > 1)
353 ofs = uio->uio_offset;
354 if (ofs + width > r->r_size)
359 buf.x1[0] = (r->r_type == PROTO_RES_PCICFG) ?
360 pci_read_config(dev, ofs, 1) : bus_read_1(r->r_d.res, ofs);
363 buf.x2[0] = (r->r_type == PROTO_RES_PCICFG) ?
364 pci_read_config(dev, ofs, 2) : bus_read_2(r->r_d.res, ofs);
367 buf.x4[0] = (r->r_type == PROTO_RES_PCICFG) ?
368 pci_read_config(dev, ofs, 4) : bus_read_4(r->r_d.res, ofs);
372 if (r->r_type == PROTO_RES_PCICFG)
374 buf.x8[0] = bus_read_8(r->r_d.res, ofs);
381 error = uiomove(&buf, width, uio);
386 proto_write(struct cdev *cdev, struct uio *uio, int ioflag)
394 struct proto_softc *sc;
405 width = uio->uio_resid;
406 if (width < 1 || width > 8 || bitcount16(width) > 1)
408 ofs = uio->uio_offset;
409 if (ofs + width > r->r_size)
412 error = uiomove(&buf, width, uio);
418 if (r->r_type == PROTO_RES_PCICFG)
419 pci_write_config(dev, ofs, buf.x1[0], 1);
421 bus_write_1(r->r_d.res, ofs, buf.x1[0]);
424 if (r->r_type == PROTO_RES_PCICFG)
425 pci_write_config(dev, ofs, buf.x2[0], 2);
427 bus_write_2(r->r_d.res, ofs, buf.x2[0]);
430 if (r->r_type == PROTO_RES_PCICFG)
431 pci_write_config(dev, ofs, buf.x4[0], 4);
433 bus_write_4(r->r_d.res, ofs, buf.x4[0]);
437 if (r->r_type == PROTO_RES_PCICFG)
439 bus_write_8(r->r_d.res, ofs, buf.x8[0]);
450 proto_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
453 struct proto_ioc_region *region;
454 struct proto_ioc_busdma *busdma;
456 struct proto_softc *sc;
464 case PROTO_IOC_REGION:
465 if (r->r_type == PROTO_RES_BUSDMA) {
469 region = (struct proto_ioc_region *)data;
470 region->size = r->r_size;
471 if (r->r_type == PROTO_RES_PCICFG)
474 region->address = rman_get_start(r->r_d.res);
476 case PROTO_IOC_BUSDMA:
477 if (r->r_type != PROTO_RES_BUSDMA) {
481 busdma = (struct proto_ioc_busdma *)data;
482 error = proto_busdma_ioctl(sc, r->r_d.busdma, busdma, td);
492 proto_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
493 int prot, vm_memattr_t *memattr)
497 if (offset & PAGE_MASK)
499 if (prot & PROT_EXEC)
506 if (offset >= r->r_size)
508 *paddr = rman_get_start(r->r_d.res) + offset;
509 *memattr = VM_MEMATTR_UNCACHEABLE;
511 case PROTO_RES_BUSDMA:
512 if (!proto_busdma_mmap_allowed(r->r_d.busdma, offset))