ppb_base.c revision 38061
1/*-
2 * Copyright (c) 1997, 1998 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 *	$Id: ppb_base.c,v 1.3 1997/09/01 00:51:45 bde Exp $
27 *
28 */
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32
33#include <dev/ppbus/ppbconf.h>
34
35/*
36 * ppb_intr()
37 *
38 * Function called by ppcintr() when an intr occurs.
39 */
40void
41ppb_intr(struct ppb_link *pl)
42{
43	struct ppb_data *ppb = pl->ppbus;
44
45	/*
46	 * Call chipset dependent code.
47	 * Should be filled at chipset initialisation if needed.
48	 */
49	if (pl->adapter->intr_handler)
50		(*pl->adapter->intr_handler)(pl->adapter_unit);
51
52	/*
53	 * Call upper handler iff the bus is owned by a device and
54	 * this device has specified an interrupt handler.
55	 */
56	if (ppb->ppb_owner && ppb->ppb_owner->intr)
57		(*ppb->ppb_owner->intr)(ppb->ppb_owner->id_unit);
58
59	return;
60}
61
62/*
63 * ppb_poll_device()
64 *
65 * Polls the device
66 *
67 * max is a delay in 10-milliseconds
68 */
69int
70ppb_poll_device(struct ppb_device *dev, int max,
71		char mask, char status, int how)
72{
73	int i, error;
74
75	for (i = 0; i < max; i++) {
76		if ((ppb_rstr(dev) & mask) == status)
77			return (0);
78
79		switch (how) {
80		case PPB_NOINTR:
81			/* wait 10 ms */
82			if ((error = tsleep((caddr_t)dev, PPBPRI,
83						"ppbpoll", hz/100)))
84				return (error);
85			break;
86
87		case PPB_INTR:
88		default:
89			/* wait 10 ms */
90			if ((error = tsleep((caddr_t)dev, PPBPRI | PCATCH,
91						"ppbpoll", hz/100)))
92				return (error);
93			break;
94		}
95	}
96
97	return (EWOULDBLOCK);
98}
99
100/*
101 * ppb_set_mode()
102 *
103 * Set the operating mode of the chipset
104 */
105int
106ppb_set_mode(struct ppb_device *dev, int mode)
107{
108	struct ppb_data *ppb = dev->ppb;
109	int old_mode = ppb_get_mode(dev);
110
111	if ((*ppb->ppb_link->adapter->setmode)(dev->id_unit, mode))
112		return (-1);
113
114	/* XXX yet device mode = ppbus mode = chipset mode */
115	dev->mode = ppb->mode = mode;
116
117	return (old_mode);
118}
119
120/*
121 * ppb_reset_epp_timeout()
122 *
123 * Reset the EPP timeout bit in the status register
124 */
125int
126ppb_reset_epp_timeout(struct ppb_device *dev)
127{
128	struct ppb_data *ppb = dev->ppb;
129
130	if (ppb->ppb_owner != dev)
131		return (EACCES);
132
133	(*ppb->ppb_link->adapter->reset_epp_timeout)(dev->id_unit);
134
135	return (0);
136}
137
138/*
139 * ppb_ecp_sync()
140 *
141 * Wait for the ECP FIFO to be empty
142 */
143int
144ppb_ecp_sync(struct ppb_device *dev)
145{
146	struct ppb_data *ppb = dev->ppb;
147
148	if (ppb->ppb_owner != dev)
149		return (EACCES);
150
151	(*ppb->ppb_link->adapter->ecp_sync)(dev->id_unit);
152
153	return (0);
154}
155
156/*
157 * ppb_get_status()
158 *
159 * Read the status register and update the status info
160 */
161int
162ppb_get_status(struct ppb_device *dev, struct ppb_status *status)
163{
164	struct ppb_data *ppb = dev->ppb;
165	register char r;
166
167	if (ppb->ppb_owner != dev)
168		return (EACCES);
169
170	r = status->status = ppb_rstr(dev);
171
172	status->timeout	= r & TIMEOUT;
173	status->error	= !(r & nFAULT);
174	status->select	= r & SELECT;
175	status->paper_end = r & ERROR;
176	status->ack	= !(r & nACK);
177	status->busy	= !(r & nBUSY);
178
179	return (0);
180}
181