#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from ioctl_opt import IOW, IOR, IOWR, IO
import ctypes


class video_dmac_region(ctypes.Structure):
    _fields_ = [
        ('gpu_offset', ctypes.c_uint),
        ('size', ctypes.c_uint),
        ('handle', ctypes.c_uint),
    ]


class video_dmac_descriptor(ctypes.Structure):
    _fields_ = [
        ('port', ctypes.c_uint),
        ('handle', ctypes.c_uint),
        ('ch', ctypes.c_uint),
        ('strip', ctypes.c_uint),
        ('direction', ctypes.c_uint),
        ('kind', ctypes.c_uint),
    ]


class video_dmac_channel_setting(ctypes.Structure):
    _fields_ = [
        ('ch', ctypes.c_uint),
        ('write_port', ctypes.c_uint),
        ('read_port', ctypes.c_uint),
    ]


class gpu_mem_info(ctypes.Structure):
    _fields_ = [
        ('address', ctypes.c_void_p),
        ('size', ctypes.c_uint),
    ]


class fpga_reg_write_arg(ctypes.Structure):
    _fields_ = [
        ('dst_address', ctypes.c_uint),
        ('reg_top', ctypes.POINTER(ctypes.c_uint)),
        ('size', ctypes.c_uint),
    ]


class multicast_info(ctypes.Structure):
    _fields_ = [
        ('number', ctypes.c_uint),
        ('address', ctypes.c_ulong),
    ]


class ctrl_dmac_ignore_list(ctypes.Structure):
    _fields_ = [
        ('list_addr', ctypes.POINTER(ctypes.c_uint)),
        ('size', ctypes.c_uint),
    ]


class video_file_info(ctypes.Structure):
    _fields_ = [
        ('format', ctypes.c_uint),
        ('dst_address', ctypes.c_ulong),
        ('buffer_address', ctypes.POINTER(ctypes.c_uint)),
        ('buffer_size', ctypes.c_uint),
    ]


class audio_header(ctypes.Structure):
    _fields_ = [
        ('sample_status', ctypes.c_ubyte),
        ('endian', ctypes.c_ubyte),
        ('ch_assign', ctypes.c_ushort),
        ('payload_size', ctypes.c_uint),
        ('sample_size', ctypes.c_uint),
    ]


class audio_file_info(ctypes.Structure):
    _fields_ = [
        ('head', audio_header),
        ('target_addr', ctypes.c_ulong),
        ('next_read', ctypes.c_uint),
        ('buffer_address', ctypes.POINTER(ctypes.c_uint)),
        ('buffer_size', ctypes.c_uint),
    ]


class slavedata_write_info(ctypes.Structure):
    _fields_ = [
        ('block_num', ctypes.c_uint),
        ('buffer_address', ctypes.POINTER(ctypes.c_uint)),
    ]


class slavedata_start_param(ctypes.Structure):
    _fields_ = [
        ('file_target', ctypes.c_uint),
        ('block_count', ctypes.c_uint),
    ]


class aurora_status_param(ctypes.Structure):
    _fields_ = [
        ('qsfp_number', ctypes.c_uint),
        ('status', ctypes.c_uint),
    ]


class i2c_param(ctypes.Structure):
    _fields_ = [
        ('i2c_bus', ctypes.c_uint),
        ('pci_addr', ctypes.c_uint),
        ('slave_addr', ctypes.c_uint),
        ('i2c_addr', ctypes.c_uint),
        ('buff', ctypes.c_ubyte * 256),
        ('size', ctypes.c_uint),
    ]


class qsfp_thresholds(ctypes.Structure):
    _fields_ = [
        ('high_alarm', ctypes.c_ushort),
        ('low_alarm', ctypes.c_ushort),
        ('high_warning', ctypes.c_ushort),
        ('low_warning', ctypes.c_ushort),
    ]


class qsfp_status(ctypes.Structure):
    _fields_ = [
        ('mod_prs_l', ctypes.c_uint),
        ('initialized', ctypes.c_bool),
        ('rx_signal_loss', ctypes.c_ubyte),
        ('rx_cdr_unlock', ctypes.c_ubyte),
        ('temperature', ctypes.c_short),
        ('rx_power_ch1', ctypes.c_ushort),
        ('rx_power_ch2', ctypes.c_ushort),
        ('rx_power_ch3', ctypes.c_ushort),
        ('rx_power_ch4', ctypes.c_ushort),
        ('tx_power_ch1', ctypes.c_ushort),
        ('tx_power_ch2', ctypes.c_ushort),
        ('tx_power_ch3', ctypes.c_ushort),
        ('tx_power_ch4', ctypes.c_ushort),
        ('rx_power_mon', ctypes.c_ubyte * 2),
        ('tx_power_mon', ctypes.c_ubyte * 2),
        ('rx_power_threshold', qsfp_thresholds),
        ('tx_power_threshold', qsfp_thresholds),
    ]


class qsfp_status_param(ctypes.Structure):
    _fields_ = [
        ('qsfp_number', ctypes.c_uint),
        ('status', qsfp_status),
    ]


class qsfp_led_setting(ctypes.Structure):
    _fields_ = [
        ('qsfp_number', ctypes.c_uint),
        ('led_status', ctypes.c_uint),
    ]


class qsfp_led_param(ctypes.Structure):
    _fields_ = [
        ('num', ctypes.c_uint),
        ('settings', ctypes.POINTER(qsfp_led_setting)),
    ]


class aurora_test_result(ctypes.Structure):
    _fields_ = [
        ('connect_result_pri_0', ctypes.c_uint),
        ('connect_result_pri_1', ctypes.c_uint),
        ('connect_result_meo', ctypes.c_uint),
        ('compare_result_pri_0', ctypes.c_uint),
        ('compare_result_pri_1', ctypes.c_uint),
        ('compare_result_meo', ctypes.c_uint),
    ]


class aurora_test_result_param(ctypes.Structure):
    _fields_ = [
        ('num', ctypes.c_uint),
        ('result', ctypes.POINTER(aurora_test_result)),
    ]


SONY_FPGA_IOC_MAGIC = (0x73)
SONY_FPGA_PIN_GPU_MEMORY = IOW(SONY_FPGA_IOC_MAGIC, 1, gpu_mem_info)
SONY_FPGA_WAIT_VD_INFO = IOR(SONY_FPGA_IOC_MAGIC, 2, ctypes.c_uint)
SONY_FPGA_VD_INT_ENABLE = IOW(SONY_FPGA_IOC_MAGIC, 3, ctypes.c_uint)
SONY_FPGA_REG_WRITE = IOW(SONY_FPGA_IOC_MAGIC, 4, fpga_reg_write_arg)
SONY_FPGA_REG_UPDATE_COMPLETED = IO(SONY_FPGA_IOC_MAGIC, 5)
SONY_FPGA_REG_UPDATE_OK = IO(SONY_FPGA_IOC_MAGIC, 6)
SONY_FPGA_MAKE_VIDEO_DESCRIPTOR = IOWR(SONY_FPGA_IOC_MAGIC, 7, video_dmac_region)
SONY_FPGA_RESET_VIDEO_DESCRIPTOR = IO(SONY_FPGA_IOC_MAGIC, 8)
SONY_FPGA_SET_VIDEO_DESCRIPTOR = IOW(SONY_FPGA_IOC_MAGIC, 9, video_dmac_descriptor)
SONY_FPGA_SET_VIDEO_DMAC = IOW(SONY_FPGA_IOC_MAGIC, 10, ctypes.c_uint)
SONY_FPGA_SET_MASTER_MODE = IOW(SONY_FPGA_IOC_MAGIC, 11, ctypes.c_uint)
SONY_FPGA_SET_CTRL_DMAC = IOW(SONY_FPGA_IOC_MAGIC, 12, ctypes.c_uint)
SONY_FPGA_SET_MULTICAST = IOW(SONY_FPGA_IOC_MAGIC, 13, ctypes.c_uint)
SONY_FPGA_SET_MULTICAST_ADDRESS = IOW(SONY_FPGA_IOC_MAGIC, 14, multicast_info)
SONY_FPGA_CTRL_DMAC_IGNORE_ADDRESS = IOW(SONY_FPGA_IOC_MAGIC, 15, ctrl_dmac_ignore_list)
SONY_FPGA_CTRL_DMAC_IGNORE_LIST_CLEAR = IO(SONY_FPGA_IOC_MAGIC, 16)
SONY_FPGA_GET_DMAC_FILE_STATUS = IOR(SONY_FPGA_IOC_MAGIC, 17, ctypes.c_uint)
SONY_FPGA_VIDEO_FILE_WRITE = IOWR(SONY_FPGA_IOC_MAGIC, 18, video_file_info)
SONY_FPGA_VIDEO_FILE_READ = IOWR(SONY_FPGA_IOC_MAGIC, 19, video_file_info)
SONY_FPGA_SET_SDI_OUT_CH = IOW(SONY_FPGA_IOC_MAGIC, 20, ctypes.c_uint)
SONY_FPGA_REF_LOCK = IO(SONY_FPGA_IOC_MAGIC, 21)
SONY_FPGA_AUDIO_MUTE = IOW(SONY_FPGA_IOC_MAGIC, 22, ctypes.c_uint)
SONY_FPGA_AUDIO_FILE_WRITE = IOWR(SONY_FPGA_IOC_MAGIC, 23, audio_file_info)
SONY_FPGA_AUDIO_FILE_READ = IOWR(SONY_FPGA_IOC_MAGIC, 24, audio_file_info)
SONY_FPGA_SLAVEDATA_FILE_WRITE = IOWR(SONY_FPGA_IOC_MAGIC, 25, slavedata_write_info)
SONY_FPGA_SLAVEDATA_UPDATE_PROGRESS = IOR(SONY_FPGA_IOC_MAGIC, 26, ctypes.c_uint)
SONY_FPGA_SLAVEDATA_UPDATE_START = IOW(SONY_FPGA_IOC_MAGIC, 27, slavedata_start_param)
SONY_FPGA_SLAVEDATA_UPDATE_FINISH = IOR(SONY_FPGA_IOC_MAGIC, 28, ctypes.c_uint)
SONY_FPGA_SLAVEDATA_UPDATE_ERROR_CLEAR = IOR(SONY_FPGA_IOC_MAGIC, 35, ctypes.c_uint)
SONY_FPGA_WAIT_VD = IO(SONY_FPGA_IOC_MAGIC, 29)
SONY_FPGA_READ_FRAME_CNT = IOR(SONY_FPGA_IOC_MAGIC, 30, ctypes.c_uint)
SONY_FPGA_CMP_TEST_CTRL = IOW(SONY_FPGA_IOC_MAGIC, 31, ctypes.c_uint)
SONY_FPGA_REFSYNC_MON_CLEAR = IO(SONY_FPGA_IOC_MAGIC, 32)
SONY_FPGA_SET_VIDEO_FORMAT = IOW(SONY_FPGA_IOC_MAGIC, 33, ctypes.c_uint)
SONY_FPGA_SET_VIDEO_TRANSFER = IOW(SONY_FPGA_IOC_MAGIC, 34, ctypes.c_uint)
SONY_FPGA_PAUSE_VIDEO_DMAC = IO(SONY_FPGA_IOC_MAGIC, 35)
SONY_FPGA_RESTART_VIDEO_DMAC = IO(SONY_FPGA_IOC_MAGIC, 36)
SONY_FPGA_VIDEO_FORMAT_INTR = IO(SONY_FPGA_IOC_MAGIC, 37)
SONY_FPGA_RTP_AURORA_RESET = IO(SONY_FPGA_IOC_MAGIC, 38)
SONY_FPGA_SWR_AURORA_RESET = IO(SONY_FPGA_IOC_MAGIC, 39)
SONY_FPGA_XPT_AURORA_RESET = IO(SONY_FPGA_IOC_MAGIC, 40)
SONY_FPGA_BIM_AURORA_RESET = IO(SONY_FPGA_IOC_MAGIC, 41)
SONY_FPGA_GET_AURORA_STATUS = IOR(SONY_FPGA_IOC_MAGIC, 42, aurora_status_param)
SONY_FPGA_XPT_I2C_TX = IOWR(SONY_FPGA_IOC_MAGIC, 43, i2c_param)
SONY_FPGA_XPT_I2C_RX = IOWR(SONY_FPGA_IOC_MAGIC, 44, i2c_param)
SONY_FPGA_GET_QSFP_STATUS = IOWR(SONY_FPGA_IOC_MAGIC, 45, qsfp_status_param)
SONY_FPGA_SET_BOX_MODE = IOW(SONY_FPGA_IOC_MAGIC, 46, ctypes.c_uint)
SONY_FPGA_SET_QSFP_LED = IOW(SONY_FPGA_IOC_MAGIC, 47, qsfp_led_param)
SONY_FPGA_QSFP_AURORA_RESET = IOW(SONY_FPGA_IOC_MAGIC, 48, ctypes.c_uint)
SONY_FPGA_QSFP_INITIALIZE = IOW(SONY_FPGA_IOC_MAGIC, 49, ctypes.c_uint)
SONY_FPGA_QSFP_PAUSE_THREAD = IOW(SONY_FPGA_IOC_MAGIC, 50, ctypes.c_uint)
SONY_FPGA_QSFP_RESUME_THREAD = IOW(SONY_FPGA_IOC_MAGIC, 51, ctypes.c_uint)
SONY_FPGA_C2C_AURORA_RESET = IO(SONY_FPGA_IOC_MAGIC, 52)
SONY_FPGA_QSFP_CDR_RESET = IOW(SONY_FPGA_IOC_MAGIC, 53, ctypes.c_uint)
SONY_FPGA_SET_VIDEO_DMAC_CHANNEL = IOW(SONY_FPGA_IOC_MAGIC, 54, video_dmac_channel_setting)
SONY_FPGA_ENABLE_GIF_DELAY_ADJUSTMENT = IO(SONY_FPGA_IOC_MAGIC, 55)
SONY_FPGA_ENTER_AURORA_TESTMODE = IOW(SONY_FPGA_IOC_MAGIC, 56, ctypes.c_uint)
SONY_FPGA_START_AURORA_TESTMODE = IOR(SONY_FPGA_IOC_MAGIC, 57, aurora_test_result_param)
SONY_FPGA_EXIT_AURORA_TESTMODE = IO(SONY_FPGA_IOC_MAGIC, 58)
SONY_FPGA_RTP_AURORA_CHECK = IO(SONY_FPGA_IOC_MAGIC, 59)
SONY_FPGA_AURORA_NSTOP_RELEASE = IO(SONY_FPGA_IOC_MAGIC, 60)
SONY_FPGA_WRITE_SERIAL_NUMBER = IOW(SONY_FPGA_IOC_MAGIC, 61, ctypes.c_uint)
SONY_FPGA_SET_AXI_FIREWALL_WAIT = IO(SONY_FPGA_IOC_MAGIC, 62)
SONY_FPGA_AURORA_INIT = IO(SONY_FPGA_IOC_MAGIC, 63)
SONY_FPGA_SET_SDI_MUTE = IOW(SONY_FPGA_IOC_MAGIC, 64, ctypes.c_uint)
SONY_FPGA_SET_SDI_AUTO_RESET = IOW(SONY_FPGA_IOC_MAGIC, 65, ctypes.c_uint)
SONY_FPGA_WAIT_VD_INFO_TIMEOUT = IOWR(SONY_FPGA_IOC_MAGIC, 66, ctypes.c_uint)
SONY_FPGA_MIB_START = IO(SONY_FPGA_IOC_MAGIC, 67)
SONY_FPGA_RESET_MIB_COUNTER = IO(SONY_FPGA_IOC_MAGIC, 68)
