/* vi: set sw=4 ts=4: */
/*
 *  Copyright 1997, 1998 by UCHIYAMA Yasushi
 *  $XFree86: xc/programs/Xserver/hw/xfree86/os-support/hurd/hurd_video.c,v 1.4 
 *  2000/11/14 18:20:37 dawes Exp $ 
 *
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * 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.
 *
 * 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 TERRENCE R. LAMBERT 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "defs.h"

#if defined(__Mach__)

#include <mach.h>
#include <device/device.h>

#define VIDEORAM_ADDR 0xA0000
#define VIDEORAM_SIZE 0x10000

int MachMmapVideoRAM(unsigned char **gMem)
{
    mach_port_t device,iopl_dev;
    memory_object_t iopl_mem;
    kern_return_t err;
    vm_address_t addr=(vm_address_t)0;

    err = get_privileged_ports (NULL, &device);
    if( err )
    {
		errno = err;
		return FAILURE;
    }

    err = device_open(device,D_READ|D_WRITE,"iopl",&iopl_dev);
    mach_port_deallocate (mach_task_self(), device);
    if( err )
    {
		errno = err;

		return FAILURE;
    }

    err = device_map(iopl_dev,VM_PROT_READ|VM_PROT_WRITE, 
					VIDEORAM_ADDR , VIDEORAM_SIZE ,&iopl_mem,0);
    if( err )
    {
		errno = err;
		return FAILURE;
    }
    err = vm_map(mach_task_self(),
		 &addr,
		 VIDEORAM_SIZE,
		 0,     /* mask */
		 TRUE,  /* anywhere */
		 iopl_mem,
		 (vm_offset_t)VIDEORAM_ADDR,
		 FALSE, /* copy on write */
		 VM_PROT_READ|VM_PROT_WRITE,
		 VM_PROT_READ|VM_PROT_WRITE,
		 VM_INHERIT_SHARE);
	
    mach_port_deallocate(mach_task_self(),iopl_mem);
    if( err )
    {
		errno = err;
		return FAILURE;
    }
    mach_port_deallocate(mach_task_self(),iopl_dev);
    if( err )
    {
		errno = err;
		return FAILURE;
    }

	*gMem = (unsigned char *)addr;
    return SUCCESS;
}

int MachMunmapVideoRAM(void)
{
    kern_return_t err = vm_deallocate(mach_task_self(), (int)VIDEORAM_ADDR, VIDEORAM_SIZE);
    if( err )
    {
		errno = err;
		return FAILURE;
    }
    return TRUE;
}

#endif

