/*
 * Copyright (c) 2008 Pavel Rimsky
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - 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.
 * - 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.
 */

/** @addtogroup sparc64
 * @{
 */
/**
 * @file
 * @brief	Niagara input/output driver based on hypervisor calls.
 */

#include <arch/drivers/niagara.h>
#include <console/chardev.h>
#include <console/console.h>
#include <arch/drivers/kbd.h>
#include <arch/sun4v/hypercall.h>

/* functions referenced from definitions of I/O operations structures */
static void niagara_putchar(chardev_t *, const char);
static void niagara_noop(chardev_t *);
static char niagara_read(chardev_t *);

/** character device operations */
static chardev_operations_t niagara_ops = {
	.write = niagara_putchar,
	.suspend = niagara_noop,
	.resume = niagara_noop,
	.read = niagara_read
};

/** Niagara character device */
chardev_t niagara_io;

/** defined in drivers/kbd.c */
extern kbd_type_t kbd_type;

/** Writes a single character to the standard output. */
static inline void do_putchar(const char c) {
	/* repeat until the buffer is non-full */
	while (__hypercall_fast1(CONS_PUTCHAR, c) == EWOULDBLOCK)
		;
}

/** Writes a single character to the standard output. */
static void niagara_putchar(struct chardev * cd, const char c)
{
	do_putchar(c);
	if (c == '\n')
		do_putchar('\r');
}

/**
 * Grabs the input for kernel.
 */
void niagara_grab(void)
{
}

/**
 * Releases the input so that userspace can use it.
 */
void niagara_release(void)
{
}

/**
 * Default suspend/resume operation for the input device.
 */
static void niagara_noop(chardev_t *d)
{
}

/**
 * Called when actively reading the character. Not implemented yet.
 */
static char niagara_read(chardev_t *d)
{
	return (char) 0;
}

/**
 * Function regularly called by the keyboard polling thread. Asks the
 * hypervisor whether there is any unread character. If so, it picks it up
 * and sends it to the upper layers of HelenOS.
 */
void niagara_poll(void)
{
	uint64_t c = 50;

	if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == EOK) {
		chardev_push_character(&niagara_io, c);	
		if (c == '\r')
			chardev_push_character(&niagara_io, '\n');
	}

}

/**
 * Initializes the input/output subsystem so that the Niagara standard
 * input/output is used.
 */
void niagara_init(void)
{
	kbd_type = KBD_SUN4V;

	chardev_initialize("niagara_io", &niagara_io, &niagara_ops);
	stdin = &niagara_io;
	stdout = &niagara_io;
}

/** @}
 */
