/*
 * Copyright 2010 Stefan Lankes, Chair for Operating Systems, 
 *                               RWTH Aachen University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 * 
 * This file is part of MetalSVM.
 */

#include <metalsvm/stdlib.h>
#include <metalsvm/stdio.h>
#include <metalsvm/string.h>
#include <metalsvm/stdarg.h>
#include <metalsvm/tasks.h>
#include <metalsvm/semaphore.h>
#include <asm/atomic.h>
#include <asm/processor.h>
#include <asm/io.h>
#ifdef CONFIG_VGA
#include <asm/vga.h>
#endif

#define NO_EARLY_PRINT		0
#define VGA_EARLY_PRINT		1
#define UART_EARLY_PRINT	2

#ifdef CONFIG_VGA
static uint32_t early_print = VGA_EARLY_PRINT;
#elif defined(CONFIG_UART)
static uint32_t early_print = UART_EARLY_PRINT;
#else
static uint32_t early_print = NO_EARLY_PRINT;
#endif
static atomic_int32_t kmsg_counter = ATOMIC_INIT(0);
static unsigned char kmessages[KMSG_SIZE] __attribute__ ((section(".kmsg"))) = {[0 ... KMSG_SIZE-1] = 0x00};

static ssize_t kmsg_read(fildes_t* file, uint8_t* buffer, size_t size)
{
	size_t start, i = 0;

	if (BUILTIN_EXPECT(!buffer, 0))
		return -EINVAL;
	if (BUILTIN_EXPECT(!size, 0))
		return 0;

	if (kmessages[(atomic_int32_read(&kmsg_counter) + 1) % KMSG_SIZE] == 0)
		start = 0;
	else
		start = (atomic_int32_read(&kmsg_counter) + 1) % KMSG_SIZE;

	if (((start + file->offset) % KMSG_SIZE) == atomic_int32_read(&kmsg_counter))
		return 0;
	if (file->offset >= KMSG_SIZE)
		return 0;

	for(i=0; i<size; i++, file->offset++) {
		buffer[i] = kmessages[(start + file->offset) % KMSG_SIZE];
		if (((start + file->offset) % KMSG_SIZE) == atomic_int32_read(&kmsg_counter))
			return i;
	}

	return size;
}

static int kmsg_open(fildes_t* file, const char *name)
{
	return 0;
}

static int kmsg_close(fildes_t* file)
{
	return 0;
}

/* Init Functions */
int kmsg_init(vfs_node_t * node, const char *name)
{
	uint32_t	i, j;
	vfs_node_t*	new_node;
	dir_block_t*	blockdir;
	dirent_t*	dirent;
	block_list_t*	blist;

	if (BUILTIN_EXPECT(!node || !name, 0))
		return -EINVAL;

	if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0))
		return -EINVAL;

	if (finddir_fs(node, name))
		return -EINVAL;

	new_node = kmalloc(sizeof(vfs_node_t));
	if (BUILTIN_EXPECT(!new_node, 0))
		return -ENOMEM;

	memset(new_node, 0x00, sizeof(vfs_node_t));
	new_node->type = FS_CHARDEVICE;
	new_node->open = &kmsg_open;
	new_node->close = &kmsg_close;
	new_node->read = &kmsg_read;
	new_node->write = NULL;
	spinlock_init(&new_node->lock);

	blist = &node->block_list;
	do {
		for (i = 0; i < MAX_DATABLOCKS; i++) {
			if (blist->data[i]) {
				blockdir = (dir_block_t *) blist->data[i];
				for (j = 0; j < MAX_DIRENTRIES; j++) {
					dirent = &blockdir->entries[j];
					if (!dirent->vfs_node) {
						dirent->vfs_node = new_node;
						strncpy(dirent->name, name, MAX_FNAME);
						return 0;
					}
				}
			}
		}

		if (!blist->next) {
			blist->next = (block_list_t *) kmalloc(sizeof(block_list_t));
			if (blist->next)
				memset(blist->next, 0x00, sizeof(block_list_t));
		}
	} while (blist);

	kfree(new_node, sizeof(vfs_node_t));

	return -ENOMEM;
}

int koutput_init(void)
{
#ifdef CONFIG_VGA
	vga_init();
#endif

	return 0;
}

int kputchar(int c)
{
	int pos;

	pos = atomic_int32_inc(&kmsg_counter);
	kmessages[pos % KMSG_SIZE] = (unsigned char) c;
#ifdef CONFIG_VGA
	if (early_print == VGA_EARLY_PRINT)
		vga_putchar(c);
#endif
#ifdef CONFIG_UART
	if (early_print == UART_EARLY_PRINT)
		uart_putchar(c);
#endif

	return 1;
}

int kputs(const char *str)
{
	int pos;
	int i;

	for(i=0; str[i] != '\0'; i++) {
		pos = atomic_int32_inc(&kmsg_counter);
		kmessages[pos % KMSG_SIZE] = str[i];
#ifdef CONFIG_VGA
		if (early_print == VGA_EARLY_PRINT)
			vga_putchar(str[i]);
#endif
#ifdef CONFIG_UART
		if (early_print == UART_EARLY_PRINT)
			uart_putchar(str[i]);
#endif
	}

	return i;
}

int kflush(void)
{
	flush_cache();

	return 0;
}