Home | History | Annotate | Line # | Download | only in pckbport
pckbport.c revision 1.7
      1 /* $NetBSD: pckbport.c,v 1.7 2005/12/11 12:23:22 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004 Ben Harris
      5  * Copyright (c) 1998
      6  *	Matthias Drochner.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: pckbport.c,v 1.7 2005/12/11 12:23:22 christos Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/callout.h>
     35 #include <sys/kernel.h>
     36 #include <sys/proc.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/errno.h>
     40 #include <sys/queue.h>
     41 #include <sys/lock.h>
     42 
     43 #include <dev/pckbport/pckbportvar.h>
     44 
     45 #include "locators.h"
     46 
     47 #ifdef __HAVE_NWSCONS /* XXX: this port uses sys/dev/pckbport */
     48 #include "pckbd.h"
     49 #else /* ie: only md drivers attach to pckbport */
     50 #define NPCKBD 0
     51 #endif
     52 #if (NPCKBD > 0)
     53 #include <dev/pckbport/pckbdvar.h>
     54 #endif
     55 
     56 /* descriptor for one device command */
     57 struct pckbport_devcmd {
     58 	TAILQ_ENTRY(pckbport_devcmd) next;
     59 	int flags;
     60 #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
     61 #define KBC_CMDFLAG_SLOW 2
     62 	u_char cmd[4];
     63 	int cmdlen, cmdidx, retries;
     64 	u_char response[4];
     65 	int status, responselen, responseidx;
     66 };
     67 
     68 /* data per slave device */
     69 struct pckbport_slotdata {
     70 	int polling;	/* don't process data in interrupt handler */
     71 	TAILQ_HEAD(, pckbport_devcmd) cmdqueue; /* active commands */
     72 	TAILQ_HEAD(, pckbport_devcmd) freequeue; /* free commands */
     73 #define NCMD 5
     74 	struct pckbport_devcmd cmds[NCMD];
     75 };
     76 
     77 #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
     78 
     79 static void pckbport_init_slotdata(struct pckbport_slotdata *);
     80 static int pckbportprint(void *, const char *);
     81 
     82 static struct pckbport_slotdata pckbport_cons_slotdata;
     83 
     84 static int pckbport_poll_data1(pckbport_tag_t, pckbport_slot_t);
     85 static int pckbport_send_devcmd(struct pckbport_tag *, pckbport_slot_t,
     86 				  u_char);
     87 static void pckbport_poll_cmd1(struct pckbport_tag *, pckbport_slot_t,
     88 				 struct pckbport_devcmd *);
     89 
     90 static void pckbport_cleanqueue(struct pckbport_slotdata *);
     91 static void pckbport_cleanup(void *);
     92 static int pckbport_cmdresponse(struct pckbport_tag *, pckbport_slot_t,
     93 					u_char);
     94 static void pckbport_start(struct pckbport_tag *, pckbport_slot_t);
     95 
     96 static const char * const pckbport_slot_names[] = { "kbd", "aux" };
     97 
     98 static struct pckbport_tag pckbport_cntag;
     99 
    100 #define KBC_DEVCMD_ACK 0xfa
    101 #define KBC_DEVCMD_RESEND 0xfe
    102 
    103 #define	KBD_DELAY	DELAY(8)
    104 
    105 static int
    106 pckbport_poll_data1(pckbport_tag_t t, pckbport_slot_t slot)
    107 {
    108 
    109 	return t->t_ops->t_poll_data1(t->t_cookie, slot);
    110 }
    111 
    112 static int
    113 pckbport_send_devcmd(struct pckbport_tag *t, pckbport_slot_t slot, u_char val)
    114 {
    115 
    116 	return t->t_ops->t_send_devcmd(t->t_cookie, slot, val);
    117 }
    118 
    119 pckbport_tag_t
    120 pckbport_attach(void *cookie, struct pckbport_accessops const *ops)
    121 {
    122 	pckbport_tag_t t;
    123 
    124 	if (cookie == pckbport_cntag.t_cookie &&
    125 	    ops == pckbport_cntag.t_ops)
    126 		return &pckbport_cntag;
    127 	t = malloc(sizeof(struct pckbport_tag), M_DEVBUF, M_NOWAIT | M_ZERO);
    128 	if (t == NULL) return NULL;
    129 	t->t_cookie = cookie;
    130 	t->t_ops = ops;
    131 	return t;
    132 }
    133 
    134 struct device *
    135 pckbport_attach_slot(struct device *dev, pckbport_tag_t t,
    136     pckbport_slot_t slot)
    137 {
    138 	struct pckbport_attach_args pa;
    139 	void *sdata;
    140 	struct device *found;
    141 	int alloced = 0;
    142 	int locs[PCKBPORTCF_NLOCS];
    143 
    144 	pa.pa_tag = t;
    145 	pa.pa_slot = slot;
    146 
    147 	if (t->t_slotdata[slot] == NULL) {
    148 		sdata = malloc(sizeof(struct pckbport_slotdata),
    149 		    M_DEVBUF, M_NOWAIT);
    150 		if (sdata == NULL) {
    151 			printf("%s: no memory\n", dev->dv_xname);
    152 			return 0;
    153 		}
    154 		t->t_slotdata[slot] = sdata;
    155 		pckbport_init_slotdata(t->t_slotdata[slot]);
    156 		alloced++;
    157 	}
    158 
    159 	locs[PCKBPORTCF_SLOT] = slot;
    160 
    161 	found = config_found_sm_loc(dev, "pckbport", locs, &pa,
    162 				    pckbportprint, config_stdsubmatch);
    163 
    164 	if (found == NULL && alloced) {
    165 		free(t->t_slotdata[slot], M_DEVBUF);
    166 		t->t_slotdata[slot] = NULL;
    167 	}
    168 
    169 	return found;
    170 }
    171 
    172 int
    173 pckbportprint(void *aux, const char *pnp)
    174 {
    175 	struct pckbport_attach_args *pa = aux;
    176 
    177 	if (!pnp)
    178 		aprint_normal(" (%s slot)", pckbport_slot_names[pa->pa_slot]);
    179 	return QUIET;
    180 }
    181 
    182 void
    183 pckbport_init_slotdata(struct pckbport_slotdata *q)
    184 {
    185 	int i;
    186 
    187 	TAILQ_INIT(&q->cmdqueue);
    188 	TAILQ_INIT(&q->freequeue);
    189 
    190 	for (i = 0; i < NCMD; i++)
    191 		TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
    192 
    193 	q->polling = 0;
    194 }
    195 
    196 void
    197 pckbport_flush(pckbport_tag_t t, pckbport_slot_t slot)
    198 {
    199 
    200 	(void)pckbport_poll_data1(t, slot);
    201 }
    202 
    203 int
    204 pckbport_poll_data(pckbport_tag_t t, pckbport_slot_t slot)
    205 {
    206 	struct pckbport_slotdata *q = t->t_slotdata[slot];
    207 	int c;
    208 
    209 	c = pckbport_poll_data1(t, slot);
    210 	if (c != -1 && q && CMD_IN_QUEUE(q))
    211 		/*
    212 		 * we jumped into a running command - try to deliver
    213 		 * the response
    214 		 */
    215 		if (pckbport_cmdresponse(t, slot, c))
    216 			return -1;
    217 	return c;
    218 }
    219 
    220 /*
    221  * switch scancode translation on / off
    222  * return nonzero on success
    223  */
    224 int
    225 pckbport_xt_translation(pckbport_tag_t t, pckbport_slot_t slot,	int on)
    226 {
    227 
    228 	return t->t_ops->t_xt_translation(t->t_cookie, slot, on);
    229 }
    230 
    231 void
    232 pckbport_slot_enable(pckbport_tag_t t, pckbport_slot_t slot, int on)
    233 {
    234 
    235 	t->t_ops->t_slot_enable(t->t_cookie, slot, on);
    236 }
    237 
    238 void
    239 pckbport_set_poll(pckbport_tag_t t, pckbport_slot_t slot, int on)
    240 {
    241 
    242 	t->t_slotdata[slot]->polling = on;
    243 	t->t_ops->t_set_poll(t->t_cookie, slot, on);
    244 }
    245 
    246 /*
    247  * Pass command to device, poll for ACK and data.
    248  * to be called at spltty()
    249  */
    250 static void
    251 pckbport_poll_cmd1(struct pckbport_tag *t, pckbport_slot_t slot,
    252     struct pckbport_devcmd *cmd)
    253 {
    254 	int i, c = 0;
    255 
    256 	while (cmd->cmdidx < cmd->cmdlen) {
    257 		if (!pckbport_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
    258 			printf("pckbport_cmd: send error\n");
    259 			cmd->status = EIO;
    260 			return;
    261 		}
    262 		for (i = 10; i; i--) { /* 1s ??? */
    263 			c = pckbport_poll_data1(t, slot);
    264 			if (c != -1)
    265 				break;
    266 		}
    267 
    268 		if (c == KBC_DEVCMD_ACK) {
    269 			cmd->cmdidx++;
    270 			continue;
    271 		}
    272 		if (c == KBC_DEVCMD_RESEND) {
    273 #ifdef PCKBPORTDEBUG
    274 			printf("pckbport_cmd: RESEND\n");
    275 #endif
    276 			if (cmd->retries++ < 5)
    277 				continue;
    278 			else {
    279 #ifdef PCKBPORTDEBUG
    280 				printf("pckbport: cmd failed\n");
    281 #endif
    282 				cmd->status = EIO;
    283 				return;
    284 			}
    285 		}
    286 		if (c == -1) {
    287 #ifdef PCKBPORTDEBUG
    288 			printf("pckbport_cmd: timeout\n");
    289 #endif
    290 			cmd->status = EIO;
    291 			return;
    292 		}
    293 #ifdef PCKBPORTDEBUG
    294 		printf("pckbport_cmd: lost 0x%x\n", c);
    295 #endif
    296 	}
    297 
    298 	while (cmd->responseidx < cmd->responselen) {
    299 		if (cmd->flags & KBC_CMDFLAG_SLOW)
    300 			i = 100; /* 10s ??? */
    301 		else
    302 			i = 10; /* 1s ??? */
    303 		while (i--) {
    304 			c = pckbport_poll_data1(t, slot);
    305 			if (c != -1)
    306 				break;
    307 		}
    308 		if (c == -1) {
    309 #ifdef PCKBPORTDEBUG
    310 			printf("pckbport_cmd: no data\n");
    311 #endif
    312 			cmd->status = ETIMEDOUT;
    313 			return;
    314 		} else
    315 			cmd->response[cmd->responseidx++] = c;
    316 	}
    317 }
    318 
    319 /* for use in autoconfiguration */
    320 int
    321 pckbport_poll_cmd(pckbport_tag_t t, pckbport_slot_t slot, u_char *cmd, int len,
    322     int responselen, u_char *respbuf, int slow)
    323 {
    324 	struct pckbport_devcmd nc;
    325 
    326 	if ((len > 4) || (responselen > 4))
    327 		return (EINVAL);
    328 
    329 	memset(&nc, 0, sizeof(nc));
    330 	memcpy(nc.cmd, cmd, len);
    331 	nc.cmdlen = len;
    332 	nc.responselen = responselen;
    333 	nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
    334 
    335 	pckbport_poll_cmd1(t, slot, &nc);
    336 
    337 	if (nc.status == 0 && respbuf)
    338 		memcpy(respbuf, nc.response, responselen);
    339 
    340 	return nc.status;
    341 }
    342 
    343 /*
    344  * Clean up a command queue, throw away everything.
    345  */
    346 void
    347 pckbport_cleanqueue(struct pckbport_slotdata *q)
    348 {
    349 	struct pckbport_devcmd *cmd;
    350 #ifdef PCKBPORTDEBUG
    351 	int i;
    352 #endif
    353 
    354 	while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
    355 		TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    356 #ifdef PCKBPORTDEBUG
    357 		printf("pckbport_cleanqueue: removing");
    358 		for (i = 0; i < cmd->cmdlen; i++)
    359 			printf(" %02x", cmd->cmd[i]);
    360 		printf("\n");
    361 #endif
    362 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    363 	}
    364 }
    365 
    366 /*
    367  * Timeout error handler: clean queues and data port.
    368  * XXX could be less invasive.
    369  */
    370 void
    371 pckbport_cleanup(void *self)
    372 {
    373 	struct pckbport_tag *t = self;
    374 	int s;
    375 
    376 	printf("pckbport: command timeout\n");
    377 
    378 	s = spltty();
    379 
    380 	if (t->t_slotdata[PCKBPORT_KBD_SLOT])
    381 		pckbport_cleanqueue(t->t_slotdata[PCKBPORT_KBD_SLOT]);
    382 	if (t->t_slotdata[PCKBPORT_AUX_SLOT])
    383 		pckbport_cleanqueue(t->t_slotdata[PCKBPORT_AUX_SLOT]);
    384 
    385 #if 0 /* XXXBJH Move to controller driver? */
    386 	while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
    387 		KBD_DELAY;
    388 		(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    389 	}
    390 #endif
    391 
    392 	/* reset KBC? */
    393 
    394 	splx(s);
    395 }
    396 
    397 /*
    398  * Pass command to device during normal operation.
    399  * to be called at spltty()
    400  */
    401 void
    402 pckbport_start(struct pckbport_tag *t, pckbport_slot_t slot)
    403 {
    404 	struct pckbport_slotdata *q = t->t_slotdata[slot];
    405 	struct pckbport_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
    406 
    407 	if (q->polling) {
    408 		do {
    409 			pckbport_poll_cmd1(t, slot, cmd);
    410 			if (cmd->status)
    411 				printf("pckbport_start: command error\n");
    412 
    413 			TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    414 			if (cmd->flags & KBC_CMDFLAG_SYNC)
    415 				wakeup(cmd);
    416 			else {
    417 				callout_stop(&t->t_cleanup);
    418 				TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    419 			}
    420 			cmd = TAILQ_FIRST(&q->cmdqueue);
    421 		} while (cmd);
    422 		return;
    423 	}
    424 
    425 	if (!pckbport_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
    426 		printf("pckbport_start: send error\n");
    427 		/* XXX what now? */
    428 		return;
    429 	}
    430 }
    431 
    432 /*
    433  * Handle command responses coming in asynchronously,
    434  * return nonzero if valid response.
    435  * to be called at spltty()
    436  */
    437 int
    438 pckbport_cmdresponse(struct pckbport_tag *t, pckbport_slot_t slot, u_char data)
    439 {
    440 	struct pckbport_slotdata *q = t->t_slotdata[slot];
    441 	struct pckbport_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
    442 
    443 #ifdef DIAGNOSTIC
    444 	if (!cmd)
    445 		panic("pckbport_cmdresponse: no active command");
    446 #endif
    447 	if (cmd->cmdidx < cmd->cmdlen) {
    448 		if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
    449 			return 0;
    450 
    451 		if (data == KBC_DEVCMD_RESEND) {
    452 			if (cmd->retries++ < 5)
    453 				/* try again last command */
    454 				goto restart;
    455 			else {
    456 #ifdef PCKBPORTDEBUG
    457 				printf("pckbport: cmd failed\n");
    458 #endif
    459 				cmd->status = EIO;
    460 				/* dequeue */
    461 			}
    462 		} else {
    463 			if (++cmd->cmdidx < cmd->cmdlen)
    464 				goto restart;
    465 			if (cmd->responselen)
    466 				return 1;
    467 			/* else dequeue */
    468 		}
    469 	} else if (cmd->responseidx < cmd->responselen) {
    470 		cmd->response[cmd->responseidx++] = data;
    471 		if (cmd->responseidx < cmd->responselen)
    472 			return 1;
    473 		/* else dequeue */
    474 	} else
    475 		return 0;
    476 
    477 	/* dequeue: */
    478 	TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    479 	if (cmd->flags & KBC_CMDFLAG_SYNC)
    480 		wakeup(cmd);
    481 	else {
    482 		callout_stop(&t->t_cleanup);
    483 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    484 	}
    485 	if (!CMD_IN_QUEUE(q))
    486 		return 1;
    487 restart:
    488 	pckbport_start(t, slot);
    489 	return 1;
    490 }
    491 
    492 /*
    493  * Put command into the device's command queue, return zero or errno.
    494  */
    495 int
    496 pckbport_enqueue_cmd(pckbport_tag_t t, pckbport_slot_t slot, u_char *cmd,
    497     int len, int responselen, int sync, u_char *respbuf)
    498 {
    499 	struct pckbport_slotdata *q = t->t_slotdata[slot];
    500 	struct pckbport_devcmd *nc;
    501 	int s, isactive, res = 0;
    502 
    503 	if ((len > 4) || (responselen > 4))
    504 		return EINVAL;
    505 	s = spltty();
    506 	nc = TAILQ_FIRST(&q->freequeue);
    507 	if (nc)
    508 		TAILQ_REMOVE(&q->freequeue, nc, next);
    509 	splx(s);
    510 	if (!nc)
    511 		return ENOMEM;
    512 
    513 	memset(nc, 0, sizeof(*nc));
    514 	memcpy(nc->cmd, cmd, len);
    515 	nc->cmdlen = len;
    516 	nc->responselen = responselen;
    517 	nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
    518 
    519 	s = spltty();
    520 
    521 	if (q->polling && sync)
    522 		/*
    523 		 * XXX We should poll until the queue is empty.
    524 		 * But we don't come here normally, so make
    525 		 * it simple and throw away everything.
    526 		 */
    527 		pckbport_cleanqueue(q);
    528 
    529 	isactive = CMD_IN_QUEUE(q);
    530 	TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
    531 	if (!isactive)
    532 		pckbport_start(t, slot);
    533 
    534 	if (q->polling)
    535 		res = (sync ? nc->status : 0);
    536 	else if (sync) {
    537 		if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
    538 			TAILQ_REMOVE(&q->cmdqueue, nc, next);
    539 			pckbport_cleanup(t);
    540 		} else
    541 			res = nc->status;
    542 	} else
    543 		callout_reset(&t->t_cleanup, hz, pckbport_cleanup, t);
    544 
    545 	if (sync) {
    546 		if (respbuf)
    547 			memcpy(respbuf, nc->response, responselen);
    548 		TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
    549 	}
    550 
    551 	splx(s);
    552 
    553 	return res;
    554 }
    555 
    556 void
    557 pckbport_set_inputhandler(pckbport_tag_t t, pckbport_slot_t slot,
    558     pckbport_inputfcn func, void *arg, char *name)
    559 {
    560 
    561 	if (slot >= PCKBPORT_NSLOTS)
    562 		panic("pckbport_set_inputhandler: bad slot %d", slot);
    563 
    564 	t->t_ops->t_intr_establish(t->t_cookie, slot);
    565 
    566 	t->t_inputhandler[slot] = func;
    567 	t->t_inputarg[slot] = arg;
    568 	t->t_subname[slot] = name;
    569 }
    570 
    571 void
    572 pckbportintr(pckbport_tag_t t, pckbport_slot_t slot, int data)
    573 {
    574 	struct pckbport_slotdata *q;
    575 
    576 	q = t->t_slotdata[slot];
    577 
    578 	if (!q) {
    579 		/* XXX do something for live insertion? */
    580 		printf("pckbportintr: no dev for slot %d\n", slot);
    581 		return;
    582 	}
    583 
    584 	if (CMD_IN_QUEUE(q) && pckbport_cmdresponse(t, slot, data))
    585 		return;
    586 
    587 	if (t->t_inputhandler[slot])
    588 		(*t->t_inputhandler[slot])(t->t_inputarg[slot], data);
    589 #ifdef PCKBPORTDEBUG
    590 	else
    591 		printf("pckbportintr: slot %d lost %d\n", slot, data);
    592 #endif
    593 }
    594 
    595 int
    596 pckbport_cnattach(void *cookie, struct pckbport_accessops const *ops,
    597     pckbport_slot_t slot)
    598 {
    599 	int res = 0;
    600 	pckbport_tag_t t = &pckbport_cntag;
    601 
    602 	t->t_cookie = cookie;
    603 	t->t_ops = ops;
    604 
    605 	/* flush */
    606 	pckbport_flush(t, slot);
    607 
    608 #if (NPCKBD > 0)
    609 	res = pckbd_cnattach(t, slot);
    610 #elif (NPCKBPORT_MACHDEP_CNATTACH > 0)
    611 	res = pckbport_machdep_cnattach(t, slot);
    612 #else
    613 	res = ENXIO;
    614 #endif /* NPCKBPORT_MACHDEP_CNATTACH > 0 */
    615 
    616 	if (res == 0) {
    617 		t->t_slotdata[slot] = &pckbport_cons_slotdata;
    618 		pckbport_init_slotdata(&pckbport_cons_slotdata);
    619 	}
    620 
    621 	return res;
    622 }
    623