2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/systm.h>
38 #include <dev/ppbus/ppbconf.h>
42 #include <dev/ppbus/ppbio.h>
44 MODULE_VERSION(ppbus, 1);
46 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
53 * max is a delay in 10-milliseconds
56 ppb_poll_bus(device_t bus, int max,
57 char mask, char status, int how)
59 struct ppb_data *ppb = DEVTOSOFTC(bus);
63 mtx_assert(ppb->ppc_lock, MA_OWNED);
65 /* try at least up to 10ms */
66 for (j = 0; j < ((how & PPB_POLL) ? max : 1); j++) {
67 for (i = 0; i < 10000; i++) {
70 if ((r & mask) == status)
75 if (!(how & PPB_POLL)) {
76 for (i = 0; max == PPB_FOREVER || i < max-1; i++) {
77 if ((ppb_rstr(bus) & mask) == status)
81 error = mtx_sleep((caddr_t)bus, ppb->ppc_lock, PPBPRI |
82 (how == PPB_NOINTR ? 0 : PCATCH), "ppbpoll", hz/100);
83 if (error != EWOULDBLOCK)
92 * ppb_get_epp_protocol()
94 * Return the chipset EPP protocol
97 ppb_get_epp_protocol(device_t bus)
100 struct ppb_data *ppb = DEVTOSOFTC(bus);
104 mtx_assert(ppb->ppc_lock, MA_OWNED);
105 BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_EPP_PROTO, &protocol);
115 ppb_get_mode(device_t bus)
117 struct ppb_data *ppb = DEVTOSOFTC(bus);
119 /* XXX yet device mode = ppbus mode = chipset mode */
120 mtx_assert(ppb->ppc_lock, MA_OWNED);
127 * Set the operating mode of the chipset, return the previous mode
130 ppb_set_mode(device_t bus, int mode)
132 struct ppb_data *ppb = DEVTOSOFTC(bus);
133 int old_mode = ppb_get_mode(bus);
135 mtx_assert(ppb->ppc_lock, MA_OWNED);
136 if (PPBUS_SETMODE(device_get_parent(bus), mode))
139 /* XXX yet device mode = ppbus mode = chipset mode */
140 ppb->mode = (mode & PPB_MASK);
148 * Write charaters to the port
151 ppb_write(device_t bus, char *buf, int len, int how)
154 struct ppb_data *ppb = DEVTOSOFTC(bus);
157 mtx_assert(ppb->ppc_lock, MA_OWNED);
158 return (PPBUS_WRITE(device_get_parent(bus), buf, len, how));
162 * ppb_reset_epp_timeout()
164 * Reset the EPP timeout bit in the status register
167 ppb_reset_epp_timeout(device_t bus)
170 struct ppb_data *ppb = DEVTOSOFTC(bus);
173 mtx_assert(ppb->ppc_lock, MA_OWNED);
174 return(PPBUS_RESET_EPP(device_get_parent(bus)));
180 * Wait for the ECP FIFO to be empty
183 ppb_ecp_sync(device_t bus)
186 struct ppb_data *ppb = DEVTOSOFTC(bus);
189 mtx_assert(ppb->ppc_lock, MA_OWNED);
190 return (PPBUS_ECP_SYNC(device_get_parent(bus)));
196 * Read the status register and update the status info
199 ppb_get_status(device_t bus, struct ppb_status *status)
202 struct ppb_data *ppb = DEVTOSOFTC(bus);
206 mtx_assert(ppb->ppc_lock, MA_OWNED);
208 r = status->status = ppb_rstr(bus);
210 status->timeout = r & TIMEOUT;
211 status->error = !(r & nFAULT);
212 status->select = r & SELECT;
213 status->paper_end = r & PERROR;
214 status->ack = !(r & nACK);
215 status->busy = !(r & nBUSY);
221 ppb_lock(device_t bus)
223 struct ppb_data *ppb = DEVTOSOFTC(bus);
225 mtx_lock(ppb->ppc_lock);
229 ppb_unlock(device_t bus)
231 struct ppb_data *ppb = DEVTOSOFTC(bus);
233 mtx_unlock(ppb->ppc_lock);
237 _ppb_assert_locked(device_t bus, const char *file, int line)
240 struct ppb_data *ppb = DEVTOSOFTC(bus);
242 _mtx_assert(ppb->ppc_lock, MA_OWNED, file, line);
247 ppb_init_callout(device_t bus, struct callout *c, int flags)
249 struct ppb_data *ppb = DEVTOSOFTC(bus);
251 callout_init_mtx(c, ppb->ppc_lock, flags);
255 ppb_sleep(device_t bus, void *wchan, int priority, const char *wmesg, int timo)
257 struct ppb_data *ppb = DEVTOSOFTC(bus);
259 return (mtx_sleep(wchan, ppb->ppc_lock, priority, wmesg, timo));