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