/*
 * device.h
 *
 * Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __MONOLITHIUM_DEVICE_H__
#define __MONOLITHIUM_DEVICE_H__

#include "object.h"

#define MAX_DEVICE_NAME 32

typedef enum
{
    BLOCK_DEVICE,
    CHAR_DEVICE
} device_type_t;

/* Generic block device IOCTLs and related data */

#define IOCTL_BLOCK_DEV_INFO        0xB0000000
#define IOCTL_BLOCK_DEV_MEDIA_INFO  0xB0000001
#define IOCTL_BLOCK_DEV_MEDIA_LOAD  0xB0000002
#define IOCTL_BLOCK_DEV_MEDIA_EJECT 0xB0000003

#define BLOCK_DEVICE_REMOVABLE_MEDIA  (1 << 0)

/* Generic character device IOCTLs and related data */

#define IOCTL_CHAR_DEV_CHECK_INPUT  0xC0000000
#define IOCTL_CHAR_DEV_CHECK_OUTPUT 0xC0000001

/* Video IOCTLs and related data */

#define IOCTL_VIDEO_GET_MODE        0xC9000000
#define IOCTL_VIDEO_SET_MODE        0xC9000001
#define IOCTL_VIDEO_LIST_MODES      0xC9000002
#define IOCTL_VIDEO_QUERY_MODE      0xC9000003
#define IOCTL_VIDEO_MAP_FRAMEBUFFER 0xC9000004
#define IOCTL_VIDEO_BITBLT          0xC9000005
#define IOCTL_VIDEO_READ_PALETTE    0xC9000006
#define IOCTL_VIDEO_WRITE_PALETTE   0xC9000007
#define IOCTL_VIDEO_READ_FONT       0xC9000008
#define IOCTL_VIDEO_WRITE_FONT      0xC9000009
#define IOCTL_VIDEO_SET_TEXT_CURSOR 0xC900000A

#define VIDEO_MODE_ALPHANUMERIC     (1 << 0)
#define VIDEO_MODE_USES_PALETTE     (1 << 1)

#define IOCTL_VIDEO_GET_MODE        0xC9000000
#define IOCTL_VIDEO_SET_MODE        0xC9000001
#define IOCTL_VIDEO_LIST_MODES      0xC9000002
#define IOCTL_VIDEO_QUERY_MODE      0xC9000003
#define IOCTL_VIDEO_MAP_FRAMEBUFFER 0xC9000004
#define IOCTL_VIDEO_BITBLT          0xC9000005
#define IOCTL_VIDEO_READ_PALETTE    0xC9000006
#define IOCTL_VIDEO_WRITE_PALETTE   0xC9000007
#define IOCTL_VIDEO_READ_FONT       0xC9000008
#define IOCTL_VIDEO_WRITE_FONT      0xC9000009
#define IOCTL_VIDEO_SET_TEXT_CURSOR 0xC900000A

#define BITBLT_RESULT_INVERT        (1 << 2)
#define BITBLT_SOURCE_INVERT        (1 << 3)
#define BITBLT_DEST_INVERT          (1 << 4)
#define BITBLT_SOURCE_ENABLED       (1 << 30)
#define BITBLT_DEST_ENABLED         (1 << 31)

enum
{
    BITBLT_OPERATION_NONE,
    BITBLT_OPERATION_AND,
    BITBLT_OPERATION_OR,
    BITBLT_OPERATION_XOR,
};

typedef struct
{
    dword_t source_x;
    dword_t source_y;
    dword_t dest_x;
    dword_t dest_y;
    dword_t width;
    dword_t height;
    dword_t operation;
} video_bitblt_parameters_t;

typedef struct
{
    void *address;
    uintptr_t offset;
    size_t size;
} video_map_framebuffer_t;

typedef struct
{
    dword_t row;
    dword_t column;
} video_cursor_location_t;

typedef struct
{
    dword_t number;
    dword_t flags;
    dword_t width;
    dword_t height;
    dword_t bpp;
    dword_t scanline_size;
    uintptr_t framebuffer_phys;
    byte_t red_mask_size;
    byte_t red_field_pos;
    byte_t green_mask_size;
    byte_t green_field_pos;
    byte_t blue_mask_size;
    byte_t blue_field_pos;
    byte_t reserved_mask_size;
    byte_t reserved_field_pos;
} video_mode_t;

sysret_t syscall_device_ioctl(handle_t device, dword_t control_code, const void *in_buffer, size_t in_length, void *out_buffer, size_t out_length);

#endif
