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 #include <sys/param.h>
29 #include <sys/systm.h>
33 #include <sys/fcntl.h>
34 #include <sys/interrupt.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/reboot.h>
42 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <machine/stdarg.h>
48 #include <dev/pci/pcivar.h>
50 #include <dev/proto/proto.h>
51 #include <dev/proto/proto_dev.h>
52 #include <dev/proto/proto_busdma.h>
54 CTASSERT(SYS_RES_IRQ != PROTO_RES_UNUSED &&
55 SYS_RES_DRQ != 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_DRQ != PROTO_RES_PCICFG &&
60 SYS_RES_MEMORY != PROTO_RES_PCICFG &&
61 SYS_RES_IOPORT != PROTO_RES_PCICFG);
62 CTASSERT(SYS_RES_IRQ != PROTO_RES_BUSDMA &&
63 SYS_RES_DRQ != PROTO_RES_BUSDMA &&
64 SYS_RES_MEMORY != PROTO_RES_BUSDMA &&
65 SYS_RES_IOPORT != PROTO_RES_BUSDMA);
67 char proto_driver_name[] = "proto";
69 static d_open_t proto_open;
70 static d_close_t proto_close;
71 static d_read_t proto_read;
72 static d_write_t proto_write;
73 static d_ioctl_t proto_ioctl;
74 static d_mmap_t proto_mmap;
76 struct cdevsw proto_devsw = {
77 .d_version = D_VERSION,
79 .d_name = proto_driver_name,
81 .d_close = proto_close,
83 .d_write = proto_write,
84 .d_ioctl = proto_ioctl,
88 static MALLOC_DEFINE(M_PROTO, "PROTO", "PROTO driver");
91 proto_add_resource(struct proto_softc *sc, int type, int rid,
96 if (type == PROTO_RES_UNUSED)
98 if (sc->sc_rescnt == PROTO_RES_MAX)
101 r = sc->sc_res + sc->sc_rescnt++;
110 proto_intr(void *arg)
112 struct proto_softc *sc = arg;
115 return (FILTER_HANDLED);
120 proto_probe(device_t dev, const char *prefix, char ***devnamesp)
122 char **devnames = *devnamesp;
123 const char *dn, *ep, *ev;
127 if (devnames == NULL) {
128 pfxlen = strlen(prefix);
129 names = 1; /* NULL pointer */
130 ev = kern_getenv("hw.proto.attach");
133 while (*dn != '\0') {
135 while (*ep != ',' && *ep != '\0')
137 if ((ep - dn) > pfxlen &&
138 strncmp(dn, prefix, pfxlen) == 0)
140 dn = (*ep == ',') ? ep + 1 : ep;
143 devnames = malloc(names * sizeof(caddr_t), M_DEVBUF,
145 *devnamesp = devnames;
149 while (*dn != '\0') {
151 while (*ep != ',' && *ep != '\0')
153 if ((ep - dn) > pfxlen &&
154 strncmp(dn, prefix, pfxlen) == 0) {
155 devnames[idx] = malloc(ep - dn + 1,
156 M_DEVBUF, M_WAITOK | M_ZERO);
157 memcpy(devnames[idx], dn, ep - dn);
160 dn = (*ep == ',') ? ep + 1 : ep;
162 freeenv(__DECONST(char *, ev));
166 dn = device_get_desc(dev);
167 while (*devnames != NULL) {
168 if (strcmp(dn, *devnames) == 0)
169 return (BUS_PROBE_SPECIFIC);
172 return (BUS_PROBE_HOOVER);
176 proto_attach(device_t dev)
178 struct proto_softc *sc;
182 sc = device_get_softc(dev);
184 mtx_init(&sc->sc_mtx, "proto-softc", NULL, MTX_DEF);
186 for (res = 0; res < sc->sc_rescnt; res++) {
187 r = sc->sc_res + res;
196 r->r_size = rman_get_size(r->r_d.res);
197 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600,
198 "proto/%s/%02x.%s", device_get_desc(dev), r->r_rid,
199 (r->r_type == SYS_RES_IOPORT) ? "io" : "mem");
200 r->r_u.cdev->si_drv1 = sc;
201 r->r_u.cdev->si_drv2 = r;
203 case PROTO_RES_PCICFG:
205 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600,
206 "proto/%s/pcicfg", device_get_desc(dev));
207 r->r_u.cdev->si_drv1 = sc;
208 r->r_u.cdev->si_drv2 = r;
210 case PROTO_RES_BUSDMA:
211 r->r_d.busdma = proto_busdma_attach(sc);
212 r->r_size = 0; /* no read(2) nor write(2) */
213 r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600,
214 "proto/%s/busdma", device_get_desc(dev));
215 r->r_u.cdev->si_drv1 = sc;
216 r->r_u.cdev->si_drv2 = r;
224 proto_detach(device_t dev)
226 struct proto_softc *sc;
230 sc = device_get_softc(dev);
232 mtx_lock(&sc->sc_mtx);
233 if (sc->sc_opencnt == 0)
235 mtx_unlock(&sc->sc_mtx);
236 if (sc->sc_opencnt > 0)
239 for (res = 0; res < sc->sc_rescnt; res++) {
240 r = sc->sc_res + res;
245 bus_release_resource(dev, r->r_type, r->r_rid,
249 bus_release_resource(dev, r->r_type, r->r_rid,
254 destroy_dev(r->r_u.cdev);
255 bus_release_resource(dev, r->r_type, r->r_rid,
258 case PROTO_RES_PCICFG:
259 destroy_dev(r->r_u.cdev);
261 case PROTO_RES_BUSDMA:
262 destroy_dev(r->r_u.cdev);
263 proto_busdma_detach(sc, r->r_d.busdma);
266 r->r_type = PROTO_RES_UNUSED;
268 mtx_lock(&sc->sc_mtx);
271 mtx_unlock(&sc->sc_mtx);
272 mtx_destroy(&sc->sc_mtx);
281 proto_open(struct cdev *cdev, int oflags, int devtype, struct thread *td)
284 struct proto_softc *sc;
288 mtx_lock(&sc->sc_mtx);
289 if (sc->sc_opencnt >= 0) {
299 mtx_unlock(&sc->sc_mtx);
304 proto_close(struct cdev *cdev, int fflag, int devtype, struct thread *td)
307 struct proto_softc *sc;
311 mtx_lock(&sc->sc_mtx);
312 if (sc->sc_opencnt > 0) {
315 if (r->r_type == PROTO_RES_BUSDMA)
316 proto_busdma_cleanup(sc, r->r_d.busdma);
324 mtx_unlock(&sc->sc_mtx);
329 proto_read(struct cdev *cdev, struct uio *uio, int ioflag)
337 struct proto_softc *sc;
348 width = uio->uio_resid;
349 if (width < 1 || width > 8 || bitcount16(width) > 1)
351 ofs = uio->uio_offset;
352 if (ofs + width > r->r_size)
357 buf.x1[0] = (r->r_type == PROTO_RES_PCICFG) ?
358 pci_read_config(dev, ofs, 1) : bus_read_1(r->r_d.res, ofs);
361 buf.x2[0] = (r->r_type == PROTO_RES_PCICFG) ?
362 pci_read_config(dev, ofs, 2) : bus_read_2(r->r_d.res, ofs);
365 buf.x4[0] = (r->r_type == PROTO_RES_PCICFG) ?
366 pci_read_config(dev, ofs, 4) : bus_read_4(r->r_d.res, ofs);
370 if (r->r_type == PROTO_RES_PCICFG)
372 buf.x8[0] = bus_read_8(r->r_d.res, ofs);
379 error = uiomove(&buf, width, uio);
384 proto_write(struct cdev *cdev, struct uio *uio, int ioflag)
392 struct proto_softc *sc;
403 width = uio->uio_resid;
404 if (width < 1 || width > 8 || bitcount16(width) > 1)
406 ofs = uio->uio_offset;
407 if (ofs + width > r->r_size)
410 error = uiomove(&buf, width, uio);
416 if (r->r_type == PROTO_RES_PCICFG)
417 pci_write_config(dev, ofs, buf.x1[0], 1);
419 bus_write_1(r->r_d.res, ofs, buf.x1[0]);
422 if (r->r_type == PROTO_RES_PCICFG)
423 pci_write_config(dev, ofs, buf.x2[0], 2);
425 bus_write_2(r->r_d.res, ofs, buf.x2[0]);
428 if (r->r_type == PROTO_RES_PCICFG)
429 pci_write_config(dev, ofs, buf.x4[0], 4);
431 bus_write_4(r->r_d.res, ofs, buf.x4[0]);
435 if (r->r_type == PROTO_RES_PCICFG)
437 bus_write_8(r->r_d.res, ofs, buf.x8[0]);
448 proto_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
451 struct proto_ioc_region *region;
452 struct proto_ioc_busdma *busdma;
454 struct proto_softc *sc;
462 case PROTO_IOC_REGION:
463 if (r->r_type == PROTO_RES_BUSDMA) {
467 region = (struct proto_ioc_region *)data;
468 region->size = r->r_size;
469 if (r->r_type == PROTO_RES_PCICFG)
472 region->address = rman_get_start(r->r_d.res);
474 case PROTO_IOC_BUSDMA:
475 if (r->r_type != PROTO_RES_BUSDMA) {
479 busdma = (struct proto_ioc_busdma *)data;
480 error = proto_busdma_ioctl(sc, r->r_d.busdma, busdma, td);
490 proto_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
491 int prot, vm_memattr_t *memattr)
495 if (offset & PAGE_MASK)
497 if (prot & PROT_EXEC)
504 if (offset >= r->r_size)
506 *paddr = rman_get_start(r->r_d.res) + offset;
507 *memattr = VM_MEMATTR_UNCACHEABLE;
509 case PROTO_RES_BUSDMA:
510 if (!proto_busdma_mmap_allowed(r->r_d.busdma, offset))