/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#ifndef DRV_PS1_CD_TYPES_H
#define DRV_PS1_CD_TYPES_H

#include <drv/event.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

struct drv_ps1_cd
{
    bool available;
    struct drv_event ev;
};

struct cmd
{
    enum
    {
        CMD_SYNC,
        CMD_GETSTAT,
        CMD_SETLOC,
    } cmd;

    union cmd_param
    {
        struct cmd_setloc
        {
            uint8_t min, sec, sect;
        } setloc;
    } params;
};

struct cd_prv_getstat
{
    union
    {
        struct
        {
            uint8_t invalid :1, motor :1, seek_error :1, id_error :1,
                shell_open :1, reading :1, seeking :1, playing_cdda :1;
        } bits;

        uint8_t byte;
    } status;
};

struct cd_prv_read
{
    int endevent, errevent;
};

struct cd_req_read
{
    void *buf;
    size_t n;
    off_t offset;
};

struct cd_req
{
    union
    {
        struct cd_req_read read;
    } u;

    int (*f)(void);
    struct drv_event_done done;
    struct cd_req *next;
};

enum {CD_SECTOR_SZ = 2048};

struct cd_prv
{
    bool available, sector_read;
    int (*next)(void);
    int event;
    struct cmd cmd;
    struct cd_req *head, *tail;
    char sector[CD_SECTOR_SZ];
    off_t offset;

    union
    {
        struct cd_prv_getstat getstat;
        struct cd_prv_read read;
    } u;
};

extern struct cd_prv drv_ps1_cd_prv;

#endif
