2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/systm.h>
40 #include <dev/ppbus/ppbconf.h>
44 #include <dev/ppbus/ppbio.h>
46 MODULE_VERSION(ppbus, 1);
48 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
55 * max is a delay in 10-milliseconds
58 ppb_poll_bus(device_t bus, int max,
59 uint8_t mask, uint8_t status, int how)
61 struct ppb_data *ppb = DEVTOSOFTC(bus);
65 ppb_assert_locked(bus);
67 /* try at least up to 10ms */
68 for (j = 0; j < ((how & PPB_POLL) ? max : 1); j++) {
69 for (i = 0; i < 10000; i++) {
72 if ((r & mask) == status)
77 if (!(how & PPB_POLL)) {
78 for (i = 0; max == PPB_FOREVER || i < max-1; i++) {
79 if ((ppb_rstr(bus) & mask) == status)
83 error = mtx_sleep((caddr_t)bus, ppb->ppc_lock, PPBPRI |
84 (how == PPB_NOINTR ? 0 : PCATCH), "ppbpoll", hz/100);
85 if (error != EWOULDBLOCK)
94 * ppb_get_epp_protocol()
96 * Return the chipset EPP protocol
99 ppb_get_epp_protocol(device_t bus)
103 ppb_assert_locked(bus);
104 BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_EPP_PROTO, &protocol);
114 ppb_get_mode(device_t bus)
116 struct ppb_data *ppb = DEVTOSOFTC(bus);
118 /* XXX yet device mode = ppbus mode = chipset mode */
119 ppb_assert_locked(bus);
126 * Set the operating mode of the chipset, return the previous mode
129 ppb_set_mode(device_t bus, int mode)
131 struct ppb_data *ppb = DEVTOSOFTC(bus);
132 int old_mode = ppb_get_mode(bus);
134 ppb_assert_locked(bus);
135 if (PPBUS_SETMODE(device_get_parent(bus), mode))
138 /* XXX yet device mode = ppbus mode = chipset mode */
139 ppb->mode = (mode & PPB_MASK);
147 * Write charaters to the port
150 ppb_write(device_t bus, char *buf, int len, int how)
153 ppb_assert_locked(bus);
154 return (PPBUS_WRITE(device_get_parent(bus), buf, len, how));
158 * ppb_reset_epp_timeout()
160 * Reset the EPP timeout bit in the status register
163 ppb_reset_epp_timeout(device_t bus)
166 ppb_assert_locked(bus);
167 return(PPBUS_RESET_EPP(device_get_parent(bus)));
173 * Wait for the ECP FIFO to be empty
176 ppb_ecp_sync(device_t bus)
179 ppb_assert_locked(bus);
180 return (PPBUS_ECP_SYNC(device_get_parent(bus)));
186 * Read the status register and update the status info
189 ppb_get_status(device_t bus, struct ppb_status *status)
193 ppb_assert_locked(bus);
195 r = status->status = ppb_rstr(bus);
197 status->timeout = r & TIMEOUT;
198 status->error = !(r & nFAULT);
199 status->select = r & SELECT;
200 status->paper_end = r & PERROR;
201 status->ack = !(r & nACK);
202 status->busy = !(r & nBUSY);
208 ppb_lock(device_t bus)
210 struct ppb_data *ppb = DEVTOSOFTC(bus);
212 mtx_lock(ppb->ppc_lock);
216 ppb_unlock(device_t bus)
218 struct ppb_data *ppb = DEVTOSOFTC(bus);
220 mtx_unlock(ppb->ppc_lock);
224 _ppb_assert_locked(device_t bus, const char *file, int line)
227 mtx_assert_(DEVTOSOFTC(bus)->ppc_lock, MA_OWNED, file, line);
231 ppb_init_callout(device_t bus, struct callout *c, int flags)
233 struct ppb_data *ppb = DEVTOSOFTC(bus);
235 callout_init_mtx(c, ppb->ppc_lock, flags);
239 ppb_sleep(device_t bus, void *wchan, int priority, const char *wmesg, int timo)
241 struct ppb_data *ppb = DEVTOSOFTC(bus);
243 return (mtx_sleep(wchan, ppb->ppc_lock, priority, wmesg, timo));