1264916Stychon/*-
2264916Stychon * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3264916Stychon * All rights reserved.
4264916Stychon *
5264916Stychon * Redistribution and use in source and binary forms, with or without
6264916Stychon * modification, are permitted provided that the following conditions
7264916Stychon * are met:
8264916Stychon * 1. Redistributions of source code must retain the above copyright
9264916Stychon *    notice, this list of conditions and the following disclaimer.
10264916Stychon * 2. Redistributions in binary form must reproduce the above copyright
11264916Stychon *    notice, this list of conditions and the following disclaimer in the
12264916Stychon *    documentation and/or other materials provided with the distribution.
13264916Stychon *
14264916Stychon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15264916Stychon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16264916Stychon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17264916Stychon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18264916Stychon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19264916Stychon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20264916Stychon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21264916Stychon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22264916Stychon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23264916Stychon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24264916Stychon * SUCH DAMAGE.
25264916Stychon */
26264916Stychon
27264916Stychon#include <sys/cdefs.h>
28264916Stychon__FBSDID("$FreeBSD$");
29264916Stychon
30264916Stychon#include <sys/types.h>
31264916Stychon
32264916Stychon#include <machine/vmm.h>
33264916Stychon
34270159Sgrehan#include <vmmapi.h>
35270159Sgrehan
36270159Sgrehan#include <assert.h>
37270159Sgrehan#include <errno.h>
38264916Stychon#include <stdio.h>
39264916Stychon
40264916Stychon#include "inout.h"
41264916Stychon#include "pci_lpc.h"
42264916Stychon
43264916Stychon#define	KBD_DATA_PORT		0x60
44264916Stychon
45264916Stychon#define	KBD_STS_CTL_PORT	0x64
46268934Sjhb#define	 KBD_SYS_FLAG		0x4
47264916Stychon
48264916Stychon#define	KBDC_RESET		0xfe
49264916Stychon
50264916Stychonstatic int
51264916Stychonatkbdc_data_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
52264916Stychon    uint32_t *eax, void *arg)
53264916Stychon{
54264916Stychon	if (bytes != 1)
55270159Sgrehan		return (-1);
56264916Stychon
57264916Stychon	*eax = 0;
58264916Stychon
59270159Sgrehan	return (0);
60264916Stychon}
61264916Stychon
62264916Stychonstatic int
63264916Stychonatkbdc_sts_ctl_handler(struct vmctx *ctx, int vcpu, int in, int port,
64264916Stychon    int bytes, uint32_t *eax, void *arg)
65264916Stychon{
66270159Sgrehan	int error, retval;
67264916Stychon
68264916Stychon	if (bytes != 1)
69270159Sgrehan		return (-1);
70264916Stychon
71270159Sgrehan	retval = 0;
72264916Stychon	if (in) {
73268934Sjhb		*eax = KBD_SYS_FLAG;	/* system passed POST */
74264916Stychon	} else {
75264916Stychon		switch (*eax) {
76264916Stychon		case KBDC_RESET:	/* Pulse "reset" line. */
77270159Sgrehan			error = vm_suspend(ctx, VM_SUSPEND_RESET);
78270159Sgrehan			assert(error == 0 || errno == EALREADY);
79264916Stychon			break;
80264916Stychon		}
81264916Stychon	}
82264916Stychon
83264916Stychon	return (retval);
84264916Stychon}
85264916Stychon
86264916StychonINOUT_PORT(atkdbc, KBD_DATA_PORT, IOPORT_F_INOUT, atkbdc_data_handler);
87264916StychonSYSRES_IO(KBD_DATA_PORT, 1);
88264916StychonINOUT_PORT(atkbdc, KBD_STS_CTL_PORT,  IOPORT_F_INOUT,
89264916Stychon    atkbdc_sts_ctl_handler);
90264916StychonSYSRES_IO(KBD_STS_CTL_PORT, 1);
91