sa1111_kbc.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: sa1111_kbc.c,v 1.1.2.2 2002/12/29 19:20:08 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*
4 1.1.2.2 thorpej * Copyright (c) 2002 Genetec Corporation. All rights reserved.
5 1.1.2.2 thorpej * Written by Hiroyuki Bessho for Genetec Corporation.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej * 3. The name of Genetec Corporation may not be used to endorse or
16 1.1.2.2 thorpej * promote products derived from this software without specific prior
17 1.1.2.2 thorpej * written permission.
18 1.1.2.2 thorpej *
19 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
20 1.1.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
23 1.1.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 thorpej *
31 1.1.2.2 thorpej * Driver for keyboard controller in SA-1111 companion chip.
32 1.1.2.2 thorpej *
33 1.1.2.2 thorpej * PC keyboard driver (sys/dev/pckbc/pckbd.c) works only with 8042
34 1.1.2.2 thorpej * keyboard controller driver (sys/dev/ic/pckbc.c). This file
35 1.1.2.2 thorpej * provides same functions as those of 8042 driver.
36 1.1.2.2 thorpej *
37 1.1.2.2 thorpej * XXX: we need cleaner interface between the keyboard driver and
38 1.1.2.2 thorpej * keyboard controller drivers.
39 1.1.2.2 thorpej */
40 1.1.2.2 thorpej /*
41 1.1.2.2 thorpej * Copyright (c) 1998
42 1.1.2.2 thorpej * Matthias Drochner. All rights reserved.
43 1.1.2.2 thorpej *
44 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
45 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
46 1.1.2.2 thorpej * are met:
47 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
48 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
49 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
50 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
51 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
52 1.1.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
53 1.1.2.2 thorpej * must display the following acknowledgement:
54 1.1.2.2 thorpej * This product includes software developed for the NetBSD Project
55 1.1.2.2 thorpej * by Matthias Drochner.
56 1.1.2.2 thorpej * 4. The name of the author may not be used to endorse or promote products
57 1.1.2.2 thorpej * derived from this software without specific prior written permission.
58 1.1.2.2 thorpej *
59 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 1.1.2.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 1.1.2.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 1.1.2.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 1.1.2.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 1.1.2.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 1.1.2.2 thorpej */
70 1.1.2.2 thorpej
71 1.1.2.2 thorpej #include <sys/param.h>
72 1.1.2.2 thorpej #include <sys/systm.h>
73 1.1.2.2 thorpej #include <sys/types.h>
74 1.1.2.2 thorpej #include <sys/callout.h>
75 1.1.2.2 thorpej #include <sys/kernel.h>
76 1.1.2.2 thorpej #include <sys/proc.h>
77 1.1.2.2 thorpej #include <sys/conf.h>
78 1.1.2.2 thorpej #include <sys/device.h>
79 1.1.2.2 thorpej #include <sys/malloc.h>
80 1.1.2.2 thorpej #include <sys/errno.h>
81 1.1.2.2 thorpej #include <sys/queue.h>
82 1.1.2.2 thorpej #include <sys/lock.h>
83 1.1.2.2 thorpej
84 1.1.2.2 thorpej #include <machine/bus.h>
85 1.1.2.2 thorpej #include <arm/sa11x0/sa1111_reg.h>
86 1.1.2.2 thorpej #include <arm/sa11x0/sa1111_var.h>
87 1.1.2.2 thorpej
88 1.1.2.2 thorpej #include <dev/ic/pckbcvar.h> /* for prototypes */
89 1.1.2.2 thorpej
90 1.1.2.2 thorpej #include "pckbd.h"
91 1.1.2.2 thorpej #include "rnd.h"
92 1.1.2.2 thorpej #include "locators.h"
93 1.1.2.2 thorpej
94 1.1.2.2 thorpej /* descriptor for one device command */
95 1.1.2.2 thorpej struct pckbc_devcmd {
96 1.1.2.2 thorpej TAILQ_ENTRY(pckbc_devcmd) next;
97 1.1.2.2 thorpej int flags;
98 1.1.2.2 thorpej #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
99 1.1.2.2 thorpej #define KBC_CMDFLAG_SLOW 2
100 1.1.2.2 thorpej u_char cmd[4];
101 1.1.2.2 thorpej int cmdlen, cmdidx, retries;
102 1.1.2.2 thorpej u_char response[4];
103 1.1.2.2 thorpej int status, responselen, responseidx;
104 1.1.2.2 thorpej };
105 1.1.2.2 thorpej
106 1.1.2.2 thorpej struct sackbc_softc {
107 1.1.2.2 thorpej struct device dev;
108 1.1.2.2 thorpej
109 1.1.2.2 thorpej bus_space_tag_t iot;
110 1.1.2.2 thorpej bus_space_handle_t ioh;
111 1.1.2.2 thorpej
112 1.1.2.2 thorpej void *ih_rx; /* receive interrupt */
113 1.1.2.2 thorpej int intr; /* interrupt number */
114 1.1.2.2 thorpej
115 1.1.2.2 thorpej int polling; /* don't process data in interrupt handler */
116 1.1.2.2 thorpej int poll_stat; /* data read from inr handler if polling */
117 1.1.2.2 thorpej int poll_data; /* status read from intr handler if polling */
118 1.1.2.2 thorpej
119 1.1.2.2 thorpej TAILQ_HEAD(, pckbc_devcmd) cmdqueue; /* active commands */
120 1.1.2.2 thorpej TAILQ_HEAD(, pckbc_devcmd) freequeue; /* free commands */
121 1.1.2.2 thorpej #define NCMD 5
122 1.1.2.2 thorpej struct pckbc_devcmd cmd[NCMD];
123 1.1.2.2 thorpej
124 1.1.2.2 thorpej struct callout t_cleanup;
125 1.1.2.2 thorpej pckbc_inputfcn inputhandler;
126 1.1.2.2 thorpej void *inputarg;
127 1.1.2.2 thorpej const char *subname;
128 1.1.2.2 thorpej
129 1.1.2.2 thorpej };
130 1.1.2.2 thorpej
131 1.1.2.2 thorpej #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
132 1.1.2.2 thorpej
133 1.1.2.2 thorpej #define N_KBC_SLOTS 2
134 1.1.2.2 thorpej /*static struct sackbc_softc *sackbc_slot[N_KBC_SLOTS] = { NULL, NULL };*/
135 1.1.2.2 thorpej
136 1.1.2.2 thorpej static int sackbc_match(struct device *, struct cfdata *, void *);
137 1.1.2.2 thorpej static void sackbc_attach(struct device *, struct device *, void *);
138 1.1.2.2 thorpej static int sackbc_cmdresponse( struct sackbc_softc *, int );
139 1.1.2.2 thorpej
140 1.1.2.2 thorpej CFATTACH_DECL(sackbc, sizeof(struct sackbc_softc), sackbc_match,
141 1.1.2.2 thorpej sackbc_attach, NULL, NULL);
142 1.1.2.2 thorpej
143 1.1.2.2 thorpej /* XXX should not be here */
144 1.1.2.2 thorpej #define KBC_DEVCMD_ACK 0xfa
145 1.1.2.2 thorpej #define KBC_DEVCMD_RESEND 0xfe
146 1.1.2.2 thorpej
147 1.1.2.2 thorpej #define KBD_DELAY DELAY(8)
148 1.1.2.2 thorpej
149 1.1.2.2 thorpej /*#define SACKBCDEBUG*/
150 1.1.2.2 thorpej
151 1.1.2.2 thorpej #ifdef SACKBCDEBUG
152 1.1.2.2 thorpej #define DPRINTF(arg) printf arg
153 1.1.2.2 thorpej #else
154 1.1.2.2 thorpej #define DPRINTF(arg)
155 1.1.2.2 thorpej #endif
156 1.1.2.2 thorpej
157 1.1.2.2 thorpej static void sackbc_poll_cmd1( struct sackbc_softc *, struct pckbc_devcmd * );
158 1.1.2.2 thorpej
159 1.1.2.2 thorpej
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej static int
162 1.1.2.2 thorpej sackbc_match(struct device *parent, struct cfdata *cf, void *aux)
163 1.1.2.2 thorpej {
164 1.1.2.2 thorpej struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
165 1.1.2.2 thorpej
166 1.1.2.2 thorpej switch( aa->sa_addr ){
167 1.1.2.2 thorpej case SACC_KBD0: case SACC_KBD1:
168 1.1.2.2 thorpej return 1;
169 1.1.2.2 thorpej }
170 1.1.2.2 thorpej return 0;
171 1.1.2.2 thorpej }
172 1.1.2.2 thorpej
173 1.1.2.2 thorpej #if 0
174 1.1.2.2 thorpej static int
175 1.1.2.2 thorpej sackbc_txint( void *cookie )
176 1.1.2.2 thorpej {
177 1.1.2.2 thorpej struct sackbc_softc *sc = cookie;
178 1.1.2.2 thorpej
179 1.1.2.2 thorpej bus_space_read_4( sc->iot, sc->ioh, SACCKBD_STAT );
180 1.1.2.2 thorpej
181 1.1.2.2 thorpej return 0;
182 1.1.2.2 thorpej }
183 1.1.2.2 thorpej #endif
184 1.1.2.2 thorpej
185 1.1.2.2 thorpej static int
186 1.1.2.2 thorpej sackbc_rxint( void *cookie )
187 1.1.2.2 thorpej {
188 1.1.2.2 thorpej struct sackbc_softc *sc = cookie;
189 1.1.2.2 thorpej int stat, code=-1;
190 1.1.2.2 thorpej
191 1.1.2.2 thorpej stat = bus_space_read_4( sc->iot, sc->ioh, SACCKBD_STAT );
192 1.1.2.2 thorpej DPRINTF(( "sackbc_rxint stat=%x\n", stat ));
193 1.1.2.2 thorpej if( stat & KBDSTAT_RXF ){
194 1.1.2.2 thorpej code = bus_space_read_4( sc->iot, sc->ioh, SACCKBD_DATA );
195 1.1.2.2 thorpej
196 1.1.2.2 thorpej if( sc->polling ){
197 1.1.2.2 thorpej sc->poll_data = code;
198 1.1.2.2 thorpej sc->poll_stat = stat;
199 1.1.2.2 thorpej }
200 1.1.2.2 thorpej else if (CMD_IN_QUEUE(sc) && sackbc_cmdresponse(sc, code))
201 1.1.2.2 thorpej ;
202 1.1.2.2 thorpej else if( sc->inputhandler ){
203 1.1.2.2 thorpej (* sc->inputhandler)( sc->inputarg, code );
204 1.1.2.2 thorpej }
205 1.1.2.2 thorpej return 1;
206 1.1.2.2 thorpej }
207 1.1.2.2 thorpej
208 1.1.2.2 thorpej return 0;
209 1.1.2.2 thorpej }
210 1.1.2.2 thorpej
211 1.1.2.2 thorpej static int
212 1.1.2.2 thorpej sackbcprint(void *aux, const char *pnp)
213 1.1.2.2 thorpej {
214 1.1.2.2 thorpej return (QUIET);
215 1.1.2.2 thorpej }
216 1.1.2.2 thorpej
217 1.1.2.2 thorpej static void
218 1.1.2.2 thorpej sackbc_setup_intrhandler(struct sackbc_softc *sc)
219 1.1.2.2 thorpej {
220 1.1.2.2 thorpej if( !(sc->polling) && sc->ih_rx==NULL ){
221 1.1.2.2 thorpej sc->ih_rx = sacc_intr_establish(
222 1.1.2.2 thorpej (sacc_chipset_tag_t *)(sc->dev.dv_parent),
223 1.1.2.2 thorpej sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc );
224 1.1.2.2 thorpej if( sc->ih_rx == NULL ){
225 1.1.2.2 thorpej printf( "%s: can't establish interrupt\n",
226 1.1.2.2 thorpej sc->dev.dv_xname );
227 1.1.2.2 thorpej }
228 1.1.2.2 thorpej }
229 1.1.2.2 thorpej }
230 1.1.2.2 thorpej
231 1.1.2.2 thorpej static void
232 1.1.2.2 thorpej sackbc_disable_intrhandler( struct sackbc_softc *sc )
233 1.1.2.2 thorpej {
234 1.1.2.2 thorpej if( sc->polling && sc->ih_rx ){
235 1.1.2.2 thorpej sacc_intr_disestablish(
236 1.1.2.2 thorpej (sacc_chipset_tag_t *)(sc->dev.dv_parent),
237 1.1.2.2 thorpej sc->ih_rx );
238 1.1.2.2 thorpej sc->ih_rx = NULL;
239 1.1.2.2 thorpej }
240 1.1.2.2 thorpej }
241 1.1.2.2 thorpej
242 1.1.2.2 thorpej static int
243 1.1.2.2 thorpej sackbc_submatch(struct device *parent, struct cfdata *cf, void *aux)
244 1.1.2.2 thorpej {
245 1.1.2.2 thorpej struct pckbc_attach_args *pa = aux;
246 1.1.2.2 thorpej
247 1.1.2.2 thorpej DPRINTF(( "slot = %d ", cf->cf_loc[SACKBCCF_SLOT] ));
248 1.1.2.2 thorpej
249 1.1.2.2 thorpej if( pa->pa_slot == PCKBCCF_SLOT_DEFAULT )
250 1.1.2.2 thorpej pa->pa_slot = cf->cf_loc[SACKBCCF_SLOT];
251 1.1.2.2 thorpej
252 1.1.2.2 thorpej return config_match(parent, cf, aux);
253 1.1.2.2 thorpej }
254 1.1.2.2 thorpej
255 1.1.2.2 thorpej static void
256 1.1.2.2 thorpej sackbc_attach(struct device *parent, struct device *self, void *aux)
257 1.1.2.2 thorpej {
258 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *)self;
259 1.1.2.2 thorpej struct sacc_softc *psc = (struct sacc_softc *)parent;
260 1.1.2.2 thorpej struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
261 1.1.2.2 thorpej uint32_t tmp, clock_bit;
262 1.1.2.2 thorpej int i, found, intr;
263 1.1.2.2 thorpej
264 1.1.2.2 thorpej switch( aa->sa_addr ){
265 1.1.2.2 thorpej case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
266 1.1.2.2 thorpej case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
267 1.1.2.2 thorpej default:
268 1.1.2.2 thorpej return;
269 1.1.2.2 thorpej }
270 1.1.2.2 thorpej
271 1.1.2.2 thorpej if( aa->sa_size <= 0 )
272 1.1.2.2 thorpej aa->sa_size = SACCKBD_SIZE;
273 1.1.2.2 thorpej if( aa->sa_intr == SACCCF_INTR_DEFAULT )
274 1.1.2.2 thorpej aa->sa_intr = intr;
275 1.1.2.2 thorpej
276 1.1.2.2 thorpej sc->iot = psc->sc_iot;
277 1.1.2.2 thorpej if( bus_space_subregion( psc->sc_iot, psc->sc_ioh,
278 1.1.2.2 thorpej aa->sa_addr, aa->sa_size, &sc->ioh ) ){
279 1.1.2.2 thorpej printf( ": can't map subregion\n" );
280 1.1.2.2 thorpej return;
281 1.1.2.2 thorpej }
282 1.1.2.2 thorpej
283 1.1.2.2 thorpej /* enable clock for PS/2 kbd or mouse */
284 1.1.2.2 thorpej tmp = bus_space_read_4( psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR );
285 1.1.2.2 thorpej bus_space_write_4( psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
286 1.1.2.2 thorpej tmp | clock_bit );
287 1.1.2.2 thorpej
288 1.1.2.2 thorpej sc->ih_rx = NULL;
289 1.1.2.2 thorpej sc->intr = aa->sa_intr;
290 1.1.2.2 thorpej sc->inputhandler = NULL;
291 1.1.2.2 thorpej sc->subname = sc->dev.dv_xname;
292 1.1.2.2 thorpej
293 1.1.2.2 thorpej TAILQ_INIT(&sc->cmdqueue);
294 1.1.2.2 thorpej TAILQ_INIT(&sc->freequeue);
295 1.1.2.2 thorpej
296 1.1.2.2 thorpej for (i = 0; i < NCMD; i++) {
297 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&sc->freequeue, &(sc->cmd[i]), next);
298 1.1.2.2 thorpej }
299 1.1.2.2 thorpej sc->polling = 0;
300 1.1.2.2 thorpej
301 1.1.2.2 thorpej tmp = bus_space_read_4( sc->iot, sc->ioh, SACCKBD_CR );
302 1.1.2.2 thorpej bus_space_write_4( sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA );
303 1.1.2.2 thorpej
304 1.1.2.2 thorpej /* XXX: this is necessary to get keyboard working. but I don't know why */
305 1.1.2.2 thorpej bus_space_write_4( sc->iot, sc->ioh, SACCKBD_CLKDIV, 2 );
306 1.1.2.2 thorpej
307 1.1.2.2 thorpej tmp = bus_space_read_4( sc->iot, sc->ioh, SACCKBD_STAT );
308 1.1.2.2 thorpej if( (tmp & KBDSTAT_ENA) == 0 ){
309 1.1.2.2 thorpej printf("??? can't enable KBD controller\n");
310 1.1.2.2 thorpej return;
311 1.1.2.2 thorpej }
312 1.1.2.2 thorpej
313 1.1.2.2 thorpej printf("\n");
314 1.1.2.2 thorpej
315 1.1.2.2 thorpej {
316 1.1.2.2 thorpej struct pckbc_attach_args pa;
317 1.1.2.2 thorpej
318 1.1.2.2 thorpej pa.pa_tag = sc;
319 1.1.2.2 thorpej pa.pa_slot = PCKBCCF_SLOT_DEFAULT; /* Bogus */
320 1.1.2.2 thorpej
321 1.1.2.2 thorpej found = (config_found_sm(self, &pa,
322 1.1.2.2 thorpej sackbcprint, sackbc_submatch) != NULL);
323 1.1.2.2 thorpej
324 1.1.2.2 thorpej #if 0 && NRND > 0 /* XXX: not yet */
325 1.1.2.2 thorpej if (found && (t->t_slotdata[slot] != NULL))
326 1.1.2.2 thorpej rnd_attach_source(&t->t_slotdata[slot]->rnd_source,
327 1.1.2.2 thorpej sc->subname[slot], RND_TYPE_TTY, 0);
328 1.1.2.2 thorpej #endif
329 1.1.2.2 thorpej }
330 1.1.2.2 thorpej
331 1.1.2.2 thorpej }
332 1.1.2.2 thorpej
333 1.1.2.2 thorpej
334 1.1.2.2 thorpej static inline int
335 1.1.2.2 thorpej sackbc_wait_output( struct sackbc_softc *sc )
336 1.1.2.2 thorpej {
337 1.1.2.2 thorpej u_int i, stat;
338 1.1.2.2 thorpej
339 1.1.2.2 thorpej for (i = 100000; i; i--){
340 1.1.2.2 thorpej stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
341 1.1.2.2 thorpej delay(100);
342 1.1.2.2 thorpej if( stat & KBDSTAT_TXE)
343 1.1.2.2 thorpej return 1;
344 1.1.2.2 thorpej }
345 1.1.2.2 thorpej return 0;
346 1.1.2.2 thorpej }
347 1.1.2.2 thorpej
348 1.1.2.2 thorpej static int
349 1.1.2.2 thorpej sackbc_poll_data1( struct sackbc_softc *sc )
350 1.1.2.2 thorpej {
351 1.1.2.2 thorpej int i, s, stat, c = -1;
352 1.1.2.2 thorpej
353 1.1.2.2 thorpej s = spltty();
354 1.1.2.2 thorpej
355 1.1.2.2 thorpej if (sc->polling){
356 1.1.2.2 thorpej stat = sc->poll_stat;
357 1.1.2.2 thorpej c = sc->poll_data;
358 1.1.2.2 thorpej sc->poll_data = -1;
359 1.1.2.2 thorpej sc->poll_stat = -1;
360 1.1.2.2 thorpej if( stat >= 0 &&
361 1.1.2.2 thorpej (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF ){
362 1.1.2.2 thorpej splx(s);
363 1.1.2.2 thorpej return c;
364 1.1.2.2 thorpej }
365 1.1.2.2 thorpej }
366 1.1.2.2 thorpej
367 1.1.2.2 thorpej /* if 1 port read takes 1us (?), this polls for 100ms */
368 1.1.2.2 thorpej for (i = 100000; i; i--) {
369 1.1.2.2 thorpej stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
370 1.1.2.2 thorpej if( (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF ){
371 1.1.2.2 thorpej KBD_DELAY;
372 1.1.2.2 thorpej c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
373 1.1.2.2 thorpej break;
374 1.1.2.2 thorpej }
375 1.1.2.2 thorpej }
376 1.1.2.2 thorpej
377 1.1.2.2 thorpej splx(s);
378 1.1.2.2 thorpej return (c);
379 1.1.2.2 thorpej }
380 1.1.2.2 thorpej
381 1.1.2.2 thorpej static int
382 1.1.2.2 thorpej sackbc_send_cmd( struct sackbc_softc *sc, int val )
383 1.1.2.2 thorpej {
384 1.1.2.2 thorpej if ( !sackbc_wait_output(sc) )
385 1.1.2.2 thorpej return (0);
386 1.1.2.2 thorpej bus_space_write_1( sc->iot, sc->ioh, SACCKBD_DATA, val );
387 1.1.2.2 thorpej return (1);
388 1.1.2.2 thorpej }
389 1.1.2.2 thorpej
390 1.1.2.2 thorpej #define sackbc_send_devcmd sackbc_send_cmd
391 1.1.2.2 thorpej
392 1.1.2.2 thorpej /*
393 1.1.2.2 thorpej * Clean up a command queue, throw away everything.
394 1.1.2.2 thorpej */
395 1.1.2.2 thorpej static void
396 1.1.2.2 thorpej sackbc_cleanqueue( struct sackbc_softc *sc )
397 1.1.2.2 thorpej {
398 1.1.2.2 thorpej struct pckbc_devcmd *cmd;
399 1.1.2.2 thorpej #ifdef SACKBCDEBUG
400 1.1.2.2 thorpej int i;
401 1.1.2.2 thorpej #endif
402 1.1.2.2 thorpej
403 1.1.2.2 thorpej while ((cmd = TAILQ_FIRST(&sc->cmdqueue))) {
404 1.1.2.2 thorpej TAILQ_REMOVE(&sc->cmdqueue, cmd, next);
405 1.1.2.2 thorpej #ifdef SACKBCDEBUG
406 1.1.2.2 thorpej printf("sackbc_cleanqueue: removing");
407 1.1.2.2 thorpej for (i = 0; i < cmd->cmdlen; i++)
408 1.1.2.2 thorpej printf(" %02x", cmd->cmd[i]);
409 1.1.2.2 thorpej printf("\n");
410 1.1.2.2 thorpej #endif
411 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&sc->freequeue, cmd, next);
412 1.1.2.2 thorpej }
413 1.1.2.2 thorpej }
414 1.1.2.2 thorpej
415 1.1.2.2 thorpej /*
416 1.1.2.2 thorpej * Timeout error handler: clean queues and data port.
417 1.1.2.2 thorpej * XXX could be less invasive.
418 1.1.2.2 thorpej */
419 1.1.2.2 thorpej static void
420 1.1.2.2 thorpej sackbc_cleanup(void *self)
421 1.1.2.2 thorpej {
422 1.1.2.2 thorpej struct sackbc_softc *sc = self;
423 1.1.2.2 thorpej int s;
424 1.1.2.2 thorpej
425 1.1.2.2 thorpej printf("sackbc: command timeout\n");
426 1.1.2.2 thorpej
427 1.1.2.2 thorpej s = spltty();
428 1.1.2.2 thorpej
429 1.1.2.2 thorpej sackbc_cleanqueue(sc);
430 1.1.2.2 thorpej
431 1.1.2.2 thorpej while (bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT) & KBDSTAT_RXF) {
432 1.1.2.2 thorpej KBD_DELAY;
433 1.1.2.2 thorpej (void) bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
434 1.1.2.2 thorpej }
435 1.1.2.2 thorpej
436 1.1.2.2 thorpej /* reset KBC? */
437 1.1.2.2 thorpej
438 1.1.2.2 thorpej splx(s);
439 1.1.2.2 thorpej }
440 1.1.2.2 thorpej
441 1.1.2.2 thorpej
442 1.1.2.2 thorpej /*
443 1.1.2.2 thorpej * Pass command to device during normal operation.
444 1.1.2.2 thorpej * to be called at spltty()
445 1.1.2.2 thorpej */
446 1.1.2.2 thorpej static void
447 1.1.2.2 thorpej sackbc_start( struct sackbc_softc *sc )
448 1.1.2.2 thorpej {
449 1.1.2.2 thorpej struct pckbc_devcmd *cmd = TAILQ_FIRST(&sc->cmdqueue);
450 1.1.2.2 thorpej
451 1.1.2.2 thorpej if (sc->polling) {
452 1.1.2.2 thorpej while(cmd){
453 1.1.2.2 thorpej sackbc_poll_cmd1(sc, cmd);
454 1.1.2.2 thorpej if (cmd->status)
455 1.1.2.2 thorpej printf("sackbc_start: command error\n");
456 1.1.2.2 thorpej
457 1.1.2.2 thorpej TAILQ_REMOVE(&sc->cmdqueue, cmd, next);
458 1.1.2.2 thorpej if (cmd->flags & KBC_CMDFLAG_SYNC)
459 1.1.2.2 thorpej wakeup(cmd);
460 1.1.2.2 thorpej else {
461 1.1.2.2 thorpej callout_stop(&sc->t_cleanup);
462 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&sc->freequeue, cmd, next);
463 1.1.2.2 thorpej }
464 1.1.2.2 thorpej cmd = TAILQ_FIRST(&sc->cmdqueue);
465 1.1.2.2 thorpej }
466 1.1.2.2 thorpej return;
467 1.1.2.2 thorpej }
468 1.1.2.2 thorpej
469 1.1.2.2 thorpej if (!sackbc_send_devcmd(sc, cmd->cmd[cmd->cmdidx])) {
470 1.1.2.2 thorpej printf("sackbc_start: send error\n");
471 1.1.2.2 thorpej /* XXX what now? */
472 1.1.2.2 thorpej return;
473 1.1.2.2 thorpej }
474 1.1.2.2 thorpej }
475 1.1.2.2 thorpej
476 1.1.2.2 thorpej /*
477 1.1.2.2 thorpej * Handle command responses coming in asynchonously,
478 1.1.2.2 thorpej * return nonzero if valid response.
479 1.1.2.2 thorpej * to be called at spltty()
480 1.1.2.2 thorpej */
481 1.1.2.2 thorpej static int
482 1.1.2.2 thorpej sackbc_cmdresponse( struct sackbc_softc *sc, int data)
483 1.1.2.2 thorpej {
484 1.1.2.2 thorpej struct pckbc_devcmd *cmd = TAILQ_FIRST(&sc->cmdqueue);
485 1.1.2.2 thorpej #ifdef DIAGNOSTIC
486 1.1.2.2 thorpej if (!cmd)
487 1.1.2.2 thorpej panic("sackbc_cmdresponse: no active command");
488 1.1.2.2 thorpej #endif
489 1.1.2.2 thorpej if (cmd->cmdidx < cmd->cmdlen) {
490 1.1.2.2 thorpej if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
491 1.1.2.2 thorpej return (0);
492 1.1.2.2 thorpej
493 1.1.2.2 thorpej if (data == KBC_DEVCMD_RESEND) {
494 1.1.2.2 thorpej if (cmd->retries++ < 5) {
495 1.1.2.2 thorpej /* try again last command */
496 1.1.2.2 thorpej goto restart;
497 1.1.2.2 thorpej } else {
498 1.1.2.2 thorpej printf("pckbc: cmd failed\n");
499 1.1.2.2 thorpej cmd->status = EIO;
500 1.1.2.2 thorpej /* dequeue */
501 1.1.2.2 thorpej }
502 1.1.2.2 thorpej } else {
503 1.1.2.2 thorpej if (++cmd->cmdidx < cmd->cmdlen)
504 1.1.2.2 thorpej goto restart;
505 1.1.2.2 thorpej if (cmd->responselen)
506 1.1.2.2 thorpej return (1);
507 1.1.2.2 thorpej /* else dequeue */
508 1.1.2.2 thorpej }
509 1.1.2.2 thorpej } else if (cmd->responseidx < cmd->responselen) {
510 1.1.2.2 thorpej cmd->response[cmd->responseidx++] = data;
511 1.1.2.2 thorpej if (cmd->responseidx < cmd->responselen)
512 1.1.2.2 thorpej return (1);
513 1.1.2.2 thorpej /* else dequeue */
514 1.1.2.2 thorpej } else
515 1.1.2.2 thorpej return (0);
516 1.1.2.2 thorpej
517 1.1.2.2 thorpej /* dequeue: */
518 1.1.2.2 thorpej TAILQ_REMOVE(&sc->cmdqueue, cmd, next);
519 1.1.2.2 thorpej if (cmd->flags & KBC_CMDFLAG_SYNC)
520 1.1.2.2 thorpej wakeup(cmd);
521 1.1.2.2 thorpej else {
522 1.1.2.2 thorpej callout_stop(&sc->t_cleanup);
523 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&sc->freequeue, cmd, next);
524 1.1.2.2 thorpej }
525 1.1.2.2 thorpej if (!CMD_IN_QUEUE(sc))
526 1.1.2.2 thorpej return (1);
527 1.1.2.2 thorpej restart:
528 1.1.2.2 thorpej sackbc_start(sc);
529 1.1.2.2 thorpej return (1);
530 1.1.2.2 thorpej }
531 1.1.2.2 thorpej
532 1.1.2.2 thorpej /*
533 1.1.2.2 thorpej * Pass command to device, poll for ACK and data.
534 1.1.2.2 thorpej * to be called at spltty()
535 1.1.2.2 thorpej */
536 1.1.2.2 thorpej static void
537 1.1.2.2 thorpej sackbc_poll_cmd1( struct sackbc_softc *sc, struct pckbc_devcmd *cmd )
538 1.1.2.2 thorpej {
539 1.1.2.2 thorpej int i, c = 0;
540 1.1.2.2 thorpej
541 1.1.2.2 thorpej while (cmd->cmdidx < cmd->cmdlen) {
542 1.1.2.2 thorpej DPRINTF((" tx: %x ", cmd->cmd[cmd->cmdidx]));
543 1.1.2.2 thorpej if (!sackbc_send_devcmd(sc, cmd->cmd[cmd->cmdidx])) {
544 1.1.2.2 thorpej printf("sackbc_cmd: send error\n");
545 1.1.2.2 thorpej cmd->status = EIO;
546 1.1.2.2 thorpej return;
547 1.1.2.2 thorpej }
548 1.1.2.2 thorpej delay(1000);
549 1.1.2.2 thorpej for (i = 10; i; i--) { /* 1s ??? */
550 1.1.2.2 thorpej c = sackbc_poll_data1(sc);
551 1.1.2.2 thorpej if (c != -1){
552 1.1.2.2 thorpej DPRINTF((" rx: %x", c ));
553 1.1.2.2 thorpej break;
554 1.1.2.2 thorpej }
555 1.1.2.2 thorpej }
556 1.1.2.2 thorpej
557 1.1.2.2 thorpej if (c == KBC_DEVCMD_ACK) {
558 1.1.2.2 thorpej cmd->cmdidx++;
559 1.1.2.2 thorpej continue;
560 1.1.2.2 thorpej }
561 1.1.2.2 thorpej if (c == KBC_DEVCMD_RESEND) {
562 1.1.2.2 thorpej DPRINTF(("sackbc_cmd: RESEND\n"));
563 1.1.2.2 thorpej
564 1.1.2.2 thorpej if (cmd->retries++ < 5)
565 1.1.2.2 thorpej continue;
566 1.1.2.2 thorpej else {
567 1.1.2.2 thorpej DPRINTF(("sackbc: cmd failed\n"));
568 1.1.2.2 thorpej
569 1.1.2.2 thorpej cmd->status = EIO;
570 1.1.2.2 thorpej return;
571 1.1.2.2 thorpej }
572 1.1.2.2 thorpej }
573 1.1.2.2 thorpej if (c == -1) {
574 1.1.2.2 thorpej DPRINTF(("pckbc_cmd: timeout\n"));
575 1.1.2.2 thorpej
576 1.1.2.2 thorpej cmd->status = EIO;
577 1.1.2.2 thorpej return;
578 1.1.2.2 thorpej }
579 1.1.2.2 thorpej DPRINTF(("pckbc_cmd: lost 0x%x\n", c));
580 1.1.2.2 thorpej
581 1.1.2.2 thorpej }
582 1.1.2.2 thorpej
583 1.1.2.2 thorpej while (cmd->responseidx < cmd->responselen) {
584 1.1.2.2 thorpej if (cmd->flags & KBC_CMDFLAG_SLOW)
585 1.1.2.2 thorpej i = 100; /* 10s ??? */
586 1.1.2.2 thorpej else
587 1.1.2.2 thorpej i = 10; /* 1s ??? */
588 1.1.2.2 thorpej while (i--) {
589 1.1.2.2 thorpej c = sackbc_poll_data1(sc);
590 1.1.2.2 thorpej if (c != -1){
591 1.1.2.2 thorpej DPRINTF((" resp: %x", c));
592 1.1.2.2 thorpej break;
593 1.1.2.2 thorpej }
594 1.1.2.2 thorpej }
595 1.1.2.2 thorpej if (c == -1) {
596 1.1.2.2 thorpej DPRINTF(("pckbc_cmd: no response"));
597 1.1.2.2 thorpej
598 1.1.2.2 thorpej cmd->status = ETIMEDOUT;
599 1.1.2.2 thorpej return;
600 1.1.2.2 thorpej } else
601 1.1.2.2 thorpej cmd->response[cmd->responseidx++] = c;
602 1.1.2.2 thorpej }
603 1.1.2.2 thorpej DPRINTF(("\n"));
604 1.1.2.2 thorpej }
605 1.1.2.2 thorpej
606 1.1.2.2 thorpej
607 1.1.2.2 thorpej /*
608 1.1.2.2 thorpej * Glue functions for pckbd on sackbc.
609 1.1.2.2 thorpej * These functions emulate those in dev/ic/pckbc.c.
610 1.1.2.2 thorpej *
611 1.1.2.2 thorpej */
612 1.1.2.2 thorpej
613 1.1.2.2 thorpej void
614 1.1.2.2 thorpej pckbc_set_inputhandler( pckbc_tag_t self, pckbc_slot_t slot,
615 1.1.2.2 thorpej pckbc_inputfcn func, void *arg, char *name)
616 1.1.2.2 thorpej {
617 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *) self;
618 1.1.2.2 thorpej
619 1.1.2.2 thorpej if( sc == NULL )
620 1.1.2.2 thorpej return;
621 1.1.2.2 thorpej
622 1.1.2.2 thorpej DPRINTF(( "set_inputhandler %p %p\n", func, arg ));
623 1.1.2.2 thorpej
624 1.1.2.2 thorpej sc->inputhandler = func;
625 1.1.2.2 thorpej sc->inputarg = arg;
626 1.1.2.2 thorpej sc->subname = name;
627 1.1.2.2 thorpej
628 1.1.2.2 thorpej sackbc_setup_intrhandler(sc);
629 1.1.2.2 thorpej }
630 1.1.2.2 thorpej
631 1.1.2.2 thorpej
632 1.1.2.2 thorpej /* for use in autoconfiguration */
633 1.1.2.2 thorpej int
634 1.1.2.2 thorpej pckbc_poll_cmd(pckbc_tag_t self, pckbc_slot_t slot,
635 1.1.2.2 thorpej u_char *cmd, int len, int responselen, u_char *respbuf, int slow)
636 1.1.2.2 thorpej {
637 1.1.2.2 thorpej struct pckbc_devcmd nc;
638 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *) self;
639 1.1.2.2 thorpej
640 1.1.2.2 thorpej if( sc == NULL )
641 1.1.2.2 thorpej return EINVAL;
642 1.1.2.2 thorpej
643 1.1.2.2 thorpej if ((len > 4) || (responselen > 4))
644 1.1.2.2 thorpej return EINVAL;
645 1.1.2.2 thorpej
646 1.1.2.2 thorpej memset(&nc, 0, sizeof(nc));
647 1.1.2.2 thorpej memcpy(nc.cmd, cmd, len);
648 1.1.2.2 thorpej nc.cmdlen = len;
649 1.1.2.2 thorpej nc.responselen = responselen;
650 1.1.2.2 thorpej nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
651 1.1.2.2 thorpej
652 1.1.2.2 thorpej sackbc_poll_cmd1(sc, &nc);
653 1.1.2.2 thorpej
654 1.1.2.2 thorpej if (nc.status == 0 && respbuf)
655 1.1.2.2 thorpej memcpy(respbuf, nc.response, responselen);
656 1.1.2.2 thorpej
657 1.1.2.2 thorpej return (nc.status);
658 1.1.2.2 thorpej }
659 1.1.2.2 thorpej
660 1.1.2.2 thorpej
661 1.1.2.2 thorpej /*
662 1.1.2.2 thorpej * switch scancode translation on / off
663 1.1.2.2 thorpej * return nonzero on success
664 1.1.2.2 thorpej */
665 1.1.2.2 thorpej int
666 1.1.2.2 thorpej pckbc_xt_translation(pckbc_tag_t self, pckbc_slot_t slot, int on)
667 1.1.2.2 thorpej {
668 1.1.2.2 thorpej /* KBD/Mouse controller doesn't have scancode translation */
669 1.1.2.2 thorpej return !on;
670 1.1.2.2 thorpej }
671 1.1.2.2 thorpej
672 1.1.2.2 thorpej void
673 1.1.2.2 thorpej pckbc_slot_enable(pckbc_tag_t self, pckbc_slot_t slot, int on)
674 1.1.2.2 thorpej {
675 1.1.2.2 thorpej #if 0
676 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *) self;
677 1.1.2.2 thorpej int cmd;
678 1.1.2.2 thorpej
679 1.1.2.2 thorpej cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
680 1.1.2.2 thorpej if ( !sackbc_send_cmd(sc, cmd ) )
681 1.1.2.2 thorpej printf("sackbc_slot_enable(%d) failed\n", on);
682 1.1.2.2 thorpej #endif
683 1.1.2.2 thorpej }
684 1.1.2.2 thorpej
685 1.1.2.2 thorpej
686 1.1.2.2 thorpej void
687 1.1.2.2 thorpej pckbc_flush(pckbc_tag_t self, pckbc_slot_t slot)
688 1.1.2.2 thorpej {
689 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *)self;
690 1.1.2.2 thorpej
691 1.1.2.2 thorpej (void) sackbc_poll_data1(sc);
692 1.1.2.2 thorpej }
693 1.1.2.2 thorpej
694 1.1.2.2 thorpej #if 0
695 1.1.2.2 thorpej int
696 1.1.2.2 thorpej sackbc_poll_data( struct sackbc_softc *sc )
697 1.1.2.2 thorpej {
698 1.1.2.2 thorpej struct pckbc_internal *t = self;
699 1.1.2.2 thorpej struct pckbc_slotdata *q = t->t_slotdata[slot];
700 1.1.2.2 thorpej int c;
701 1.1.2.2 thorpej
702 1.1.2.2 thorpej c = pckbc_poll_data1(t, slot, t->t_haveaux);
703 1.1.2.2 thorpej if (c != -1 && q && CMD_IN_QUEUE(q)) {
704 1.1.2.2 thorpej /* we jumped into a running command - try to
705 1.1.2.2 thorpej deliver the response */
706 1.1.2.2 thorpej if (pckbc_cmdresponse(t, slot, c))
707 1.1.2.2 thorpej return (-1);
708 1.1.2.2 thorpej }
709 1.1.2.2 thorpej return (c);
710 1.1.2.2 thorpej }
711 1.1.2.2 thorpej #endif
712 1.1.2.2 thorpej
713 1.1.2.2 thorpej void
714 1.1.2.2 thorpej pckbc_set_poll(pckbc_tag_t self, pckbc_slot_t slot, int on)
715 1.1.2.2 thorpej {
716 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *)self;
717 1.1.2.2 thorpej int s;
718 1.1.2.2 thorpej
719 1.1.2.2 thorpej s = spltty();
720 1.1.2.2 thorpej
721 1.1.2.2 thorpej if( sc->polling != on ){
722 1.1.2.2 thorpej
723 1.1.2.2 thorpej sc->polling = on;
724 1.1.2.2 thorpej
725 1.1.2.2 thorpej if( on ){
726 1.1.2.2 thorpej sc->poll_data = sc->poll_stat = -1;
727 1.1.2.2 thorpej sackbc_disable_intrhandler(sc);
728 1.1.2.2 thorpej }
729 1.1.2.2 thorpej else {
730 1.1.2.2 thorpej /*
731 1.1.2.2 thorpej * If disabling polling on a device that's
732 1.1.2.2 thorpej * been configured, make sure there are no
733 1.1.2.2 thorpej * bytes left in the FIFO, holding up the
734 1.1.2.2 thorpej * interrupt line. Otherwise we won't get any
735 1.1.2.2 thorpej * further interrupts.
736 1.1.2.2 thorpej */
737 1.1.2.2 thorpej sackbc_rxint(sc);
738 1.1.2.2 thorpej sackbc_setup_intrhandler(sc);
739 1.1.2.2 thorpej }
740 1.1.2.2 thorpej }
741 1.1.2.2 thorpej splx(s);
742 1.1.2.2 thorpej }
743 1.1.2.2 thorpej
744 1.1.2.2 thorpej /*
745 1.1.2.2 thorpej * Put command into the device's command queue, return zero or errno.
746 1.1.2.2 thorpej */
747 1.1.2.2 thorpej int
748 1.1.2.2 thorpej pckbc_enqueue_cmd( pckbc_tag_t self, pckbc_slot_t slot, u_char *cmd,
749 1.1.2.2 thorpej int len, int responselen, int sync, u_char *respbuf)
750 1.1.2.2 thorpej {
751 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *)self;
752 1.1.2.2 thorpej struct pckbc_devcmd *nc;
753 1.1.2.2 thorpej int s, isactive, res = 0;
754 1.1.2.2 thorpej
755 1.1.2.2 thorpej if ( sc == NULL || (len > 4) || (responselen > 4) )
756 1.1.2.2 thorpej return (EINVAL);
757 1.1.2.2 thorpej
758 1.1.2.2 thorpej s = spltty();
759 1.1.2.2 thorpej nc = TAILQ_FIRST(&sc->freequeue);
760 1.1.2.2 thorpej if (nc) {
761 1.1.2.2 thorpej TAILQ_REMOVE(&sc->freequeue, nc, next);
762 1.1.2.2 thorpej }
763 1.1.2.2 thorpej splx(s);
764 1.1.2.2 thorpej if (!nc)
765 1.1.2.2 thorpej return (ENOMEM);
766 1.1.2.2 thorpej
767 1.1.2.2 thorpej memset(nc, 0, sizeof(*nc));
768 1.1.2.2 thorpej memcpy(nc->cmd, cmd, len);
769 1.1.2.2 thorpej nc->cmdlen = len;
770 1.1.2.2 thorpej nc->responselen = responselen;
771 1.1.2.2 thorpej nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
772 1.1.2.2 thorpej
773 1.1.2.2 thorpej s = spltty();
774 1.1.2.2 thorpej
775 1.1.2.2 thorpej if (sc->polling && sync) {
776 1.1.2.2 thorpej /*
777 1.1.2.2 thorpej * XXX We should poll until the queue is empty.
778 1.1.2.2 thorpej * But we don't come here normally, so make
779 1.1.2.2 thorpej * it simple and throw away everything.
780 1.1.2.2 thorpej */
781 1.1.2.2 thorpej sackbc_cleanqueue(sc);
782 1.1.2.2 thorpej }
783 1.1.2.2 thorpej
784 1.1.2.2 thorpej isactive = CMD_IN_QUEUE(sc);
785 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&sc->cmdqueue, nc, next);
786 1.1.2.2 thorpej if (!isactive)
787 1.1.2.2 thorpej sackbc_start(sc);
788 1.1.2.2 thorpej
789 1.1.2.2 thorpej if (sc->polling)
790 1.1.2.2 thorpej res = (sync ? nc->status : 0);
791 1.1.2.2 thorpej else if (sync) {
792 1.1.2.2 thorpej if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
793 1.1.2.2 thorpej TAILQ_REMOVE(&sc->cmdqueue, nc, next);
794 1.1.2.2 thorpej sackbc_cleanup(sc);
795 1.1.2.2 thorpej } else
796 1.1.2.2 thorpej res = nc->status;
797 1.1.2.2 thorpej } else
798 1.1.2.2 thorpej callout_reset(&sc->t_cleanup, hz, sackbc_cleanup, sc);
799 1.1.2.2 thorpej
800 1.1.2.2 thorpej if (sync) {
801 1.1.2.2 thorpej if (respbuf)
802 1.1.2.2 thorpej memcpy(respbuf, nc->response, responselen);
803 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&sc->freequeue, nc, next);
804 1.1.2.2 thorpej }
805 1.1.2.2 thorpej
806 1.1.2.2 thorpej splx(s);
807 1.1.2.2 thorpej
808 1.1.2.2 thorpej return (res);
809 1.1.2.2 thorpej }
810 1.1.2.2 thorpej
811 1.1.2.2 thorpej int
812 1.1.2.2 thorpej pckbc_poll_data(pckbc_tag_t self, pckbc_slot_t slot)
813 1.1.2.2 thorpej {
814 1.1.2.2 thorpej struct sackbc_softc *sc = (struct sackbc_softc *)self;
815 1.1.2.2 thorpej int c;
816 1.1.2.2 thorpej
817 1.1.2.2 thorpej c = sackbc_poll_data1(sc);
818 1.1.2.2 thorpej if (c != -1 && CMD_IN_QUEUE(sc)) {
819 1.1.2.2 thorpej /* we jumped into a running command - try to
820 1.1.2.2 thorpej deliver the response */
821 1.1.2.2 thorpej if (sackbc_cmdresponse(sc, c))
822 1.1.2.2 thorpej return -1;
823 1.1.2.2 thorpej }
824 1.1.2.2 thorpej return (c);
825 1.1.2.2 thorpej }
826