2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000-2004
5 * Diomidis D. Spinellis, Athens, Greece
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY Diomidis D. Spinellis ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Diomidis D. Spinellis BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $Id: pbio.c,v 1.12 2003/10/11 13:05:08 dds Exp dds $
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h> /* SYSINIT stuff */
38 #include <sys/resource.h>
39 #include <sys/syslog.h>
40 #include <sys/sysctl.h>
41 #include <sys/conf.h> /* cdevsw stuff */
42 #include <sys/malloc.h> /* malloc region definitions */
43 #include <sys/module.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
47 #include <dev/pbio/pbioio.h> /* pbio IOCTL definitions */
49 #include <sys/fcntl.h>
51 #include <isa/isavar.h>
53 /* Function prototypes (these should all be static) */
54 static d_open_t pbioopen;
55 static d_read_t pbioread;
56 static d_write_t pbiowrite;
57 static d_ioctl_t pbioioctl;
58 static d_poll_t pbiopoll;
59 static int pbioprobe(device_t);
60 static int pbioattach(device_t);
62 /* Device registers */
69 /* Per-port buffer size */
70 #define PBIO_BUFSIZ 64
72 /* Number of /dev entries */
78 static char *port_names[] = {"a", "b", "ch", "cl"};
80 #define PBIO_PNAME(n) (port_names[(n)])
82 #define PORT(dev) (dev2unit(dev))
84 #define pbio_addr(dev) ((dev)->si_drv1)
86 static struct cdevsw pbio_cdevsw = {
87 .d_version = D_VERSION,
97 * Data specific to each I/O port
101 int diff; /* When true read only differences */
102 int ipace; /* Input pace */
103 int opace; /* Output pace */
104 char oldval; /* Last value read */
105 char buff[PBIO_BUFSIZ]; /* Per-port data buffer */
109 * One of these per allocated device
112 struct portdata pd[PBIO_NPORTS];/* Per port data */
113 int iomode; /* Virtualized I/O mode port value */
114 /* The real port is write-only */
115 struct resource *res;
119 typedef struct pbio_softc *sc_p;
121 static device_method_t pbio_methods[] = {
122 /* Device interface */
123 DEVMETHOD(device_probe, pbioprobe),
124 DEVMETHOD(device_attach, pbioattach),
128 static char driver_name[] = "pbio";
130 static driver_t pbio_driver = {
133 sizeof(struct pbio_softc),
136 DRIVER_MODULE(pbio, isa, pbio_driver, 0, 0);
138 static __inline uint8_t
139 pbinb(struct pbio_softc *scp, int off)
142 return (bus_read_1(scp->res, off));
146 pboutb(struct pbio_softc *scp, int off, uint8_t val)
149 bus_write_1(scp->res, off, val);
153 pbioprobe(device_t dev)
156 struct pbio_softc *scp = device_get_softc(dev);
157 #ifdef GENERIC_PBIO_PROBE
161 if (isa_get_logicalid(dev)) /* skip PnP probes */
164 scp->res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
165 IO_PBIOSIZE, RF_ACTIVE);
166 if (scp->res == NULL)
169 #ifdef GENERIC_PBIO_PROBE
171 * try see if the device is there.
172 * This probe works only if the device has no I/O attached to it
173 * XXX Better allow flags to abort testing
175 /* Set all ports to output */
176 pboutb(scp, PBIO_CFG, 0x80);
177 printf("pbio val(CFG: 0x%03x)=0x%02x (should be 0x80)\n",
178 rman_get_start(scp->res), pbinb(scp, PBIO_CFG));
179 pboutb(scp, PBIO_PORTA, 0xa5);
180 val = pbinb(scp, PBIO_PORTA);
181 printf("pbio val=0x%02x (should be 0xa5)\n", val);
183 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->res);
186 pboutb(scp, PBIO_PORTA, 0x5a);
187 val = pbinb(scp, PBIO_PORTA);
188 printf("pbio val=0x%02x (should be 0x5a)\n", val);
190 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->res);
194 device_set_desc(dev, "Intel 8255 PPI (basic mode)");
195 /* Set all ports to input */
196 /* pboutb(scp, PBIO_CFG, 0x9b); */
197 bus_release_resource(dev, SYS_RES_IOPORT, rid, scp->res);
198 return (BUS_PROBE_DEFAULT);
202 * Called if the probe succeeded.
203 * We can be destructive here as we know we have the device.
204 * we can also trust the unit number.
207 pbioattach (device_t dev)
209 struct make_dev_args args;
213 struct pbio_softc *sc;
215 sc = device_get_softc(dev);
216 unit = device_get_unit(dev);
218 sc->res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
219 IO_PBIOSIZE, RF_ACTIVE);
224 * Store whatever seems wise.
226 sc->iomode = 0x9b; /* All ports to input */
228 sx_init(&sc->lock, "pbio");
229 for (i = 0; i < PBIO_NPORTS; i++) {
230 make_dev_args_init(&args);
231 args.mda_devsw = &pbio_cdevsw;
234 args.mda_mode = 0600;
236 args.mda_si_drv1 = sc;
237 (void)make_dev_s(&args, &sc->pd[i].port, "pbio%d%s", unit,
244 pbioioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag,
247 struct pbio_softc *scp;
252 scp = pbio_addr(dev);
253 sx_xlock(&scp->lock);
256 scp->pd[port].diff = *(int *)data;
259 scp->pd[port].ipace = *(int *)data;
262 scp->pd[port].opace = *(int *)data;
265 *(int *)data = scp->pd[port].diff;
268 *(int *)data = scp->pd[port].ipace;
271 *(int *)data = scp->pd[port].opace;
276 sx_xunlock(&scp->lock);
281 pbioopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
283 struct pbio_softc *scp;
284 int error, ocfg, port;
285 int portbit; /* Port configuration bit */
288 scp = pbio_addr(dev);
291 case 0: portbit = 0x10; break; /* Port A */
292 case 1: portbit = 0x02; break; /* Port B */
293 case 2: portbit = 0x08; break; /* Port CH */
294 case 3: portbit = 0x01; break; /* Port CL */
295 default: return (ENODEV);
300 sx_xlock(&scp->lock);
302 /* Writing == output; zero the bit */
303 pboutb(scp, PBIO_CFG, scp->iomode = (ocfg & (~portbit)));
304 else if (oflags & FREAD)
305 /* Reading == input; set the bit */
306 pboutb(scp, PBIO_CFG, scp->iomode = (ocfg | portbit));
309 sx_xunlock(&scp->lock);
315 * Return the value of a given port on a given I/O base address
316 * Handles the split C port nibbles and blocking
319 portval(int port, struct pbio_softc *scp, char *val)
326 *val = pbinb(scp, PBIO_PORTA);
329 *val = pbinb(scp, PBIO_PORTB);
332 *val = (pbinb(scp, PBIO_PORTC) >> 4) & 0xf;
335 *val = pbinb(scp, PBIO_PORTC) & 0xf;
341 if (scp->pd[port].diff) {
342 if (*val != scp->pd[port].oldval) {
343 scp->pd[port].oldval = *val;
346 err = pause_sig("pbiopl", max(1, scp->pd[port].ipace));
355 pbioread(struct cdev *dev, struct uio *uio, int ioflag)
357 struct pbio_softc *scp;
358 int err, i, port, toread;
362 scp = pbio_addr(dev);
365 sx_xlock(&scp->lock);
366 while (uio->uio_resid > 0) {
367 toread = min(uio->uio_resid, PBIO_BUFSIZ);
368 if ((err = uiomove(scp->pd[port].buff, toread, uio)) != 0)
370 for (i = 0; i < toread; i++) {
371 if ((err = portval(port, scp, &val)) != 0)
373 scp->pd[port].buff[i] = val;
374 if (!scp->pd[port].diff && scp->pd[port].ipace)
375 pause_sig("pbioip", scp->pd[port].ipace);
378 sx_xunlock(&scp->lock);
383 pbiowrite(struct cdev *dev, struct uio *uio, int ioflag)
385 struct pbio_softc *scp;
386 int i, port, ret, towrite;
390 scp = pbio_addr(dev);
393 sx_xlock(&scp->lock);
394 while (uio->uio_resid > 0) {
395 towrite = min(uio->uio_resid, PBIO_BUFSIZ);
396 if ((ret = uiomove(scp->pd[port].buff, towrite, uio)) != 0)
398 for (i = 0; i < towrite; i++) {
399 val = scp->pd[port].buff[i];
402 pboutb(scp, PBIO_PORTA, val);
405 pboutb(scp, PBIO_PORTB, val);
408 oval = pbinb(scp, PBIO_PORTC);
411 pboutb(scp, PBIO_PORTC, val | oval);
414 oval = pbinb(scp, PBIO_PORTC);
417 pboutb(scp, PBIO_PORTC, oval | val);
420 if (scp->pd[port].opace)
421 pause_sig("pbioop", scp->pd[port].opace);
424 sx_xunlock(&scp->lock);
429 pbiopoll(struct cdev *dev, int which, struct thread *td)
434 return (0); /* this is the wrong value I'm sure */