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