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