NetBSD/sys/dev/pckbport/pckbport.c

637 lines
15 KiB
C
Raw Normal View History

2004-03-19 00:05:19 +03:00
/* $NetBSD: pckbport.c,v 1.2 2004/03/18 21:05:19 bjh21 Exp $ */
/*
* Copyright (c) 2004 Ben Harris
* Copyright (c) 1998
* Matthias Drochner. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project
* by Matthias Drochner.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
2004-03-19 00:05:19 +03:00
__KERNEL_RCSID(0, "$NetBSD: pckbport.c,v 1.2 2004/03/18 21:05:19 bjh21 Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <dev/pckbport/pckbportvar.h>
#include "locators.h"
#ifdef __HAVE_NWSCONS /* XXX: this port uses sys/dev/pckbport */
#include "pckbd.h"
#else /* ie: only md drivers attach to pckbport */
#define NPCKBD 0
#endif
#if (NPCKBD > 0)
#include <dev/pckbport/pckbdvar.h>
#endif
/* descriptor for one device command */
struct pckbport_devcmd {
TAILQ_ENTRY(pckbport_devcmd) next;
int flags;
#define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
#define KBC_CMDFLAG_SLOW 2
u_char cmd[4];
int cmdlen, cmdidx, retries;
u_char response[4];
int status, responselen, responseidx;
};
/* data per slave device */
struct pckbport_slotdata {
int polling; /* don't process data in interrupt handler */
TAILQ_HEAD(, pckbport_devcmd) cmdqueue; /* active commands */
TAILQ_HEAD(, pckbport_devcmd) freequeue; /* free commands */
#define NCMD 5
struct pckbport_devcmd cmds[NCMD];
};
#define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
2004-03-19 00:05:19 +03:00
static void pckbport_init_slotdata(struct pckbport_slotdata *);
static int pckbport_submatch(struct device *, struct cfdata *, void *);
static int pckbportprint(void *, const char *);
static struct pckbport_slotdata pckbport_cons_slotdata;
2004-03-19 00:05:19 +03:00
static int pckbport_poll_data1(pckbport_tag_t, pckbport_slot_t);
static int pckbport_send_devcmd(struct pckbport_tag *, pckbport_slot_t,
u_char);
static void pckbport_poll_cmd1(struct pckbport_tag *, pckbport_slot_t,
struct pckbport_devcmd *);
2004-03-19 00:05:19 +03:00
static void pckbport_cleanqueue(struct pckbport_slotdata *);
static void pckbport_cleanup(void *);
static int pckbport_cmdresponse(struct pckbport_tag *, pckbport_slot_t,
u_char);
static void pckbport_start(struct pckbport_tag *, pckbport_slot_t);
static const char * const pckbport_slot_names[] = { "kbd", "aux" };
static struct pckbport_tag pckbport_cntag;
#define KBC_DEVCMD_ACK 0xfa
#define KBC_DEVCMD_RESEND 0xfe
#define KBD_DELAY DELAY(8)
static int
2004-03-19 00:05:19 +03:00
pckbport_poll_data1(pckbport_tag_t t, pckbport_slot_t slot)
{
return t->t_ops->t_poll_data1(t->t_cookie, slot);
}
static int
2004-03-19 00:05:19 +03:00
pckbport_send_devcmd(struct pckbport_tag *t, pckbport_slot_t slot, u_char val)
{
return t->t_ops->t_send_devcmd(t->t_cookie, slot, val);
}
static int
2004-03-19 00:05:19 +03:00
pckbport_submatch(struct device *parent, struct cfdata *cf, void *aux)
{
struct pckbport_attach_args *pa = aux;
if (cf->cf_loc[PCKBPORTCF_SLOT] != PCKBPORTCF_SLOT_DEFAULT &&
cf->cf_loc[PCKBPORTCF_SLOT] != pa->pa_slot)
2004-03-19 00:05:19 +03:00
return 0;
return config_match(parent, cf, aux);
}
pckbport_tag_t
pckbport_attach(void *cookie, struct pckbport_accessops const *ops)
{
pckbport_tag_t t;
if (cookie == pckbport_cntag.t_cookie &&
ops == pckbport_cntag.t_ops)
return &pckbport_cntag;
t = malloc(sizeof(struct pckbport_tag), M_DEVBUF, M_NOWAIT | M_ZERO);
if (t == NULL) return NULL;
t->t_cookie = cookie;
t->t_ops = ops;
return t;
}
struct device *
2004-03-19 00:05:19 +03:00
pckbport_attach_slot(struct device *dev, pckbport_tag_t t,
pckbport_slot_t slot)
{
struct pckbport_attach_args pa;
void *sdata;
struct device *found;
int alloced = 0;
pa.pa_tag = t;
pa.pa_slot = slot;
if (t->t_slotdata[slot] == NULL) {
sdata = malloc(sizeof(struct pckbport_slotdata),
M_DEVBUF, M_NOWAIT);
if (sdata == NULL) {
printf("%s: no memory\n", dev->dv_xname);
2004-03-19 00:05:19 +03:00
return 0;
}
t->t_slotdata[slot] = sdata;
pckbport_init_slotdata(t->t_slotdata[slot]);
alloced++;
}
found = config_found_sm(dev, &pa, pckbportprint, pckbport_submatch);
if (found == NULL && alloced) {
free(t->t_slotdata[slot], M_DEVBUF);
t->t_slotdata[slot] = NULL;
}
2004-03-19 00:05:19 +03:00
return found;
}
int
2004-03-19 00:05:19 +03:00
pckbportprint(void *aux, const char *pnp)
{
struct pckbport_attach_args *pa = aux;
if (!pnp)
aprint_normal(" (%s slot)", pckbport_slot_names[pa->pa_slot]);
2004-03-19 00:05:19 +03:00
return QUIET;
}
void
2004-03-19 00:05:19 +03:00
pckbport_init_slotdata(struct pckbport_slotdata *q)
{
int i;
2004-03-19 00:05:19 +03:00
TAILQ_INIT(&q->cmdqueue);
TAILQ_INIT(&q->freequeue);
2004-03-19 00:05:19 +03:00
for (i = 0; i < NCMD; i++)
TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
2004-03-19 00:05:19 +03:00
q->polling = 0;
}
void
2004-03-19 00:05:19 +03:00
pckbport_flush(pckbport_tag_t t, pckbport_slot_t slot)
{
2004-03-19 00:05:19 +03:00
(void)pckbport_poll_data1(t, slot);
}
int
2004-03-19 00:05:19 +03:00
pckbport_poll_data(pckbport_tag_t t, pckbport_slot_t slot)
{
struct pckbport_slotdata *q = t->t_slotdata[slot];
int c;
c = pckbport_poll_data1(t, slot);
2004-03-19 00:05:19 +03:00
if (c != -1 && q && CMD_IN_QUEUE(q))
/*
* we jumped into a running command - try to deliver
* the response
*/
if (pckbport_cmdresponse(t, slot, c))
2004-03-19 00:05:19 +03:00
return -1;
return c;
}
/*
* switch scancode translation on / off
* return nonzero on success
*/
int
2004-03-19 00:05:19 +03:00
pckbport_xt_translation(pckbport_tag_t t, pckbport_slot_t slot, int on)
{
return t->t_ops->t_xt_translation(t->t_cookie, slot, on);
}
void
2004-03-19 00:05:19 +03:00
pckbport_slot_enable(pckbport_tag_t t, pckbport_slot_t slot, int on)
{
t->t_ops->t_slot_enable(t->t_cookie, slot, on);
}
void
2004-03-19 00:05:19 +03:00
pckbport_set_poll(pckbport_tag_t t, pckbport_slot_t slot, int on)
{
t->t_slotdata[slot]->polling = on;
t->t_ops->t_set_poll(t->t_cookie, slot, on);
}
/*
* Pass command to device, poll for ACK and data.
* to be called at spltty()
*/
static void
2004-03-19 00:05:19 +03:00
pckbport_poll_cmd1(struct pckbport_tag *t, pckbport_slot_t slot,
struct pckbport_devcmd *cmd)
{
int i, c = 0;
while (cmd->cmdidx < cmd->cmdlen) {
if (!pckbport_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
printf("pckbport_cmd: send error\n");
cmd->status = EIO;
return;
}
for (i = 10; i; i--) { /* 1s ??? */
c = pckbport_poll_data1(t, slot);
if (c != -1)
break;
}
if (c == KBC_DEVCMD_ACK) {
cmd->cmdidx++;
continue;
}
if (c == KBC_DEVCMD_RESEND) {
#ifdef PCKBPORTDEBUG
printf("pckbport_cmd: RESEND\n");
#endif
if (cmd->retries++ < 5)
continue;
else {
#ifdef PCKBPORTDEBUG
printf("pckbport: cmd failed\n");
#endif
cmd->status = EIO;
return;
}
}
if (c == -1) {
#ifdef PCKBPORTDEBUG
printf("pckbport_cmd: timeout\n");
#endif
cmd->status = EIO;
return;
}
#ifdef PCKBPORTDEBUG
printf("pckbport_cmd: lost 0x%x\n", c);
#endif
}
while (cmd->responseidx < cmd->responselen) {
if (cmd->flags & KBC_CMDFLAG_SLOW)
i = 100; /* 10s ??? */
else
i = 10; /* 1s ??? */
while (i--) {
c = pckbport_poll_data1(t, slot);
if (c != -1)
break;
}
if (c == -1) {
#ifdef PCKBPORTDEBUG
printf("pckbport_cmd: no data\n");
#endif
cmd->status = ETIMEDOUT;
return;
} else
cmd->response[cmd->responseidx++] = c;
}
}
/* for use in autoconfiguration */
int
2004-03-19 00:05:19 +03:00
pckbport_poll_cmd(pckbport_tag_t t, pckbport_slot_t slot, u_char *cmd, int len,
int responselen, u_char *respbuf, int slow)
{
struct pckbport_devcmd nc;
if ((len > 4) || (responselen > 4))
return (EINVAL);
memset(&nc, 0, sizeof(nc));
memcpy(nc.cmd, cmd, len);
nc.cmdlen = len;
nc.responselen = responselen;
nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
pckbport_poll_cmd1(t, slot, &nc);
if (nc.status == 0 && respbuf)
memcpy(respbuf, nc.response, responselen);
2004-03-19 00:05:19 +03:00
return nc.status;
}
/*
* Clean up a command queue, throw away everything.
*/
void
2004-03-19 00:05:19 +03:00
pckbport_cleanqueue(struct pckbport_slotdata *q)
{
struct pckbport_devcmd *cmd;
#ifdef PCKBPORTDEBUG
int i;
#endif
while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
TAILQ_REMOVE(&q->cmdqueue, cmd, next);
#ifdef PCKBPORTDEBUG
printf("pckbport_cleanqueue: removing");
for (i = 0; i < cmd->cmdlen; i++)
printf(" %02x", cmd->cmd[i]);
printf("\n");
#endif
TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
}
}
/*
* Timeout error handler: clean queues and data port.
* XXX could be less invasive.
*/
void
2004-03-19 00:05:19 +03:00
pckbport_cleanup(void *self)
{
struct pckbport_tag *t = self;
int s;
printf("pckbport: command timeout\n");
s = spltty();
if (t->t_slotdata[PCKBPORT_KBD_SLOT])
pckbport_cleanqueue(t->t_slotdata[PCKBPORT_KBD_SLOT]);
if (t->t_slotdata[PCKBPORT_AUX_SLOT])
pckbport_cleanqueue(t->t_slotdata[PCKBPORT_AUX_SLOT]);
#if 0 /* XXXBJH Move to controller driver? */
while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
KBD_DELAY;
(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
}
#endif
/* reset KBC? */
splx(s);
}
/*
* Pass command to device during normal operation.
* to be called at spltty()
*/
void
2004-03-19 00:05:19 +03:00
pckbport_start(struct pckbport_tag *t, pckbport_slot_t slot)
{
struct pckbport_slotdata *q = t->t_slotdata[slot];
struct pckbport_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
if (q->polling) {
do {
pckbport_poll_cmd1(t, slot, cmd);
if (cmd->status)
printf("pckbport_start: command error\n");
TAILQ_REMOVE(&q->cmdqueue, cmd, next);
if (cmd->flags & KBC_CMDFLAG_SYNC)
wakeup(cmd);
else {
callout_stop(&t->t_cleanup);
TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
}
cmd = TAILQ_FIRST(&q->cmdqueue);
} while (cmd);
return;
}
if (!pckbport_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
printf("pckbport_start: send error\n");
/* XXX what now? */
return;
}
}
/*
* Handle command responses coming in asynchronously,
* return nonzero if valid response.
* to be called at spltty()
*/
int
2004-03-19 00:05:19 +03:00
pckbport_cmdresponse(struct pckbport_tag *t, pckbport_slot_t slot, u_char data)
{
struct pckbport_slotdata *q = t->t_slotdata[slot];
struct pckbport_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
2004-03-19 00:05:19 +03:00
#ifdef DIAGNOSTIC
if (!cmd)
panic("pckbport_cmdresponse: no active command");
#endif
if (cmd->cmdidx < cmd->cmdlen) {
if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
2004-03-19 00:05:19 +03:00
return 0;
if (data == KBC_DEVCMD_RESEND) {
2004-03-19 00:05:19 +03:00
if (cmd->retries++ < 5)
/* try again last command */
goto restart;
2004-03-19 00:05:19 +03:00
else {
#ifdef PCKBPORTDEBUG
printf("pckbport: cmd failed\n");
#endif
cmd->status = EIO;
/* dequeue */
}
} else {
if (++cmd->cmdidx < cmd->cmdlen)
goto restart;
if (cmd->responselen)
2004-03-19 00:05:19 +03:00
return 1;
/* else dequeue */
}
} else if (cmd->responseidx < cmd->responselen) {
cmd->response[cmd->responseidx++] = data;
if (cmd->responseidx < cmd->responselen)
2004-03-19 00:05:19 +03:00
return 1;
/* else dequeue */
} else
2004-03-19 00:05:19 +03:00
return 0;
/* dequeue: */
TAILQ_REMOVE(&q->cmdqueue, cmd, next);
if (cmd->flags & KBC_CMDFLAG_SYNC)
wakeup(cmd);
else {
callout_stop(&t->t_cleanup);
TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
}
if (!CMD_IN_QUEUE(q))
2004-03-19 00:05:19 +03:00
return 1;
restart:
pckbport_start(t, slot);
2004-03-19 00:05:19 +03:00
return 1;
}
/*
* Put command into the device's command queue, return zero or errno.
*/
int
2004-03-19 00:05:19 +03:00
pckbport_enqueue_cmd(pckbport_tag_t t, pckbport_slot_t slot, u_char *cmd,
int len, int responselen, int sync, u_char *respbuf)
{
struct pckbport_slotdata *q = t->t_slotdata[slot];
struct pckbport_devcmd *nc;
int s, isactive, res = 0;
if ((len > 4) || (responselen > 4))
2004-03-19 00:05:19 +03:00
return EINVAL;
s = spltty();
nc = TAILQ_FIRST(&q->freequeue);
2004-03-19 00:05:19 +03:00
if (nc)
TAILQ_REMOVE(&q->freequeue, nc, next);
splx(s);
if (!nc)
2004-03-19 00:05:19 +03:00
return ENOMEM;
memset(nc, 0, sizeof(*nc));
memcpy(nc->cmd, cmd, len);
nc->cmdlen = len;
nc->responselen = responselen;
nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
s = spltty();
2004-03-19 00:05:19 +03:00
if (q->polling && sync)
/*
* XXX We should poll until the queue is empty.
* But we don't come here normally, so make
* it simple and throw away everything.
*/
pckbport_cleanqueue(q);
isactive = CMD_IN_QUEUE(q);
TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
if (!isactive)
pckbport_start(t, slot);
if (q->polling)
res = (sync ? nc->status : 0);
else if (sync) {
if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
TAILQ_REMOVE(&q->cmdqueue, nc, next);
pckbport_cleanup(t);
} else
res = nc->status;
} else
callout_reset(&t->t_cleanup, hz, pckbport_cleanup, t);
if (sync) {
if (respbuf)
memcpy(respbuf, nc->response, responselen);
TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
}
splx(s);
2004-03-19 00:05:19 +03:00
return res;
}
void
2004-03-19 00:05:19 +03:00
pckbport_set_inputhandler(pckbport_tag_t t, pckbport_slot_t slot,
pckbport_inputfcn func, void *arg, char *name)
{
if (slot >= PCKBPORT_NSLOTS)
panic("pckbport_set_inputhandler: bad slot %d", slot);
t->t_ops->t_intr_establish(t->t_cookie, slot);
t->t_inputhandler[slot] = func;
t->t_inputarg[slot] = arg;
t->t_subname[slot] = name;
}
void
pckbportintr(pckbport_tag_t t, pckbport_slot_t slot, int data)
{
struct pckbport_slotdata *q;
q = t->t_slotdata[slot];
if (!q) {
/* XXX do something for live insertion? */
printf("pckbportintr: no dev for slot %d\n", slot);
return;
}
if (CMD_IN_QUEUE(q) && pckbport_cmdresponse(t, slot, data))
return;
if (t->t_inputhandler[slot])
(*t->t_inputhandler[slot])(t->t_inputarg[slot], data);
#ifdef PCKBPORTDEBUG
else
printf("pckbportintr: slot %d lost %d\n", slot, data);
#endif
}
int
pckbport_cnattach(void *cookie, struct pckbport_accessops const *ops,
pckbport_slot_t slot)
{
int res = 0;
pckbport_tag_t t = &pckbport_cntag;
t->t_cookie = cookie;
t->t_ops = ops;
/* flush */
pckbport_flush(t, slot);
#if (NPCKBD > 0)
res = pckbd_cnattach(t, slot);
#elif (NPCKBPORT_MACHDEP_CNATTACH > 0)
res = pckbport_machdep_cnattach(t, slot);
#else
res = ENXIO;
#endif /* NPCKBPORT_MACHDEP_CNATTACH > 0 */
if (res == 0) {
t->t_slotdata[slot] = &pckbport_cons_slotdata;
pckbport_init_slotdata(&pckbport_cons_slotdata);
}
return res;
}