/*
 *  los/drivers/floppy/floppy.c
 *
 *  Copyright (C) 2007  Andrew Davis
 */

/* I know, I know, this code is strategically uncommented so google
   code searchers can't find errors: HACK FIX ME EXPLOIT !!! */

#include <los/floppy.h>

#include <los/text.h>
#include <los/syslib.h>
#include <los/clock.h>
#include <los/setup.h>
#include <los/dma.h>
#include <los/device.h>
#include <los/memory.h>
#include <los/scheduler.h>

#define DISK_PARAMETER_ADDRESS 0x000FEFC7
#define error 1

fdisk_t * fd1 = NULL;
fdisk_t * fd2 = NULL;

volatile int irq6_state = 0;  
 
struct FLOPPY_GEOMETRY floppy_geometrys[5] = 
{
	{  9, 2, 40 },
	{ 15, 2, 80 },
	{  9, 2, 80 },
	{ 18, 2, 80 },
	{ 36, 2, 80 }
};

char *floppy_names[] = 
{
    "None.",
    "360kB 5.25: Unsupported.",
    "1.2MB 5.25: Unsupported.",
    "720KB 3.5: Unsupported.",
    "1.44MB 3.5.",
    "2.88MB 3.5: Unsupported."
};

struct stack_data * irq6_handler(struct stack_data *regs)
{
    irq6_state = 1;
    return regs;
}

void wait_irq6()
{
    int tmo = 10;
    while(!irq6_state)
    {
        yield(); // give up prossesor
        tmo--;
    }
    if( tmo == 0 )
        printf("Floppy disk: seek timeout!");    
    irq6_state = 0;
}

void start_motor( fdisk_t *d )
{
    outportb((d -> base + 0x2), 0x1C);
    delay(250);
    d -> motor_on = 1;
}    

void stop_motor( fdisk_t *d )
{
    outportb((d -> base + 0x2), 0x00);
    delay(250);
    d -> motor_on = 0;
}

void sendbyte( char byte, fdisk_t *d )
{
    volatile int msr;
    int tmo;
    for(tmo = 0; tmo < 128; tmo++) 
    {
        msr = inportb((d -> base + 0x04));
        if ((msr & 0xC0) == 0x80) 
        {
            outportb((d -> base + 0x05), byte);
            return;
        }
        (void)inportb(0x80);
    }   
}

int getbyte( fdisk_t *d )
{
    volatile int msr;
    int tmo;
    for (tmo = 0; tmo < 128; tmo++) 
    {
        msr = inportb((d -> base + 0x04));
        if ((msr & 0xd0) == 0xd0) 
	        return inportb((d -> base + 0x05));
        (void)inportb(0x80);
    }
    return -1;
}

void waitfdc( fdisk_t *d, int wait )
{
    wait_irq6();
    if( wait )
    {
		sendbyte( 0x08, d );
	    d -> sr0 = getbyte( d );
	    d -> track = getbyte( d );
    }
} 

int fdc_seek( char track, fdisk_t *d )
{
    if( d -> track == track )
        return 0;
    sendbyte( 0x0F, d );
    sendbyte( d->base == 0x03F0 ? 0 : 1 , d );
    sendbyte( track, d );
    delay(250);
    waitfdc( d, 1 );
    delay(250);
    if( d -> track != track )
    {
        puts( "Track seek error\n" );
        return error;
    }
    return 0;
}

void fdc_recalibrate( fdisk_t *d )
{
    start_motor( d );
    sendbyte( 0x07, d );
    sendbyte( d->base == 0x03F0 ? 0 : 1 , d);
    waitfdc( d , 1);
    stop_motor( d );
}

void fdc_reset( fdisk_t *d )
{
    int tmo = 10;
	outportb((d -> base + 0x02), 0);    // stop everything
    outportb((d -> base + 0x04), 0);     // data rate (500K/s)
    outportb((d -> base + 0x02), 0x0C);  // restart ints
    delay(250);
    waitfdc( d , 1 );
    delay(250);
	sendbyte(0x03, d);                     // timing
    sendbyte(0xDF, d);                     // timing
    sendbyte(0x02, d);                     // timing
    delay(250);
    while( fdc_seek( 1, d ) == error && tmo )
        tmo--;
    if( tmo == 0 )
        printf("Floppy disk: %s seek timeout!", (d -> base == 0x3F0)?"/dev/fda":"/dev/fdb");
    fdc_recalibrate( d );
}

void get_floppy_types()
{
    unsigned char data, floppy_type;
    outportb(0x70, 0x10);
    data = inportb(0x71);
    if((floppy_type = data >> 4))
    {
        fd1 = (fdisk_t *)malloc(sizeof(fdisk_t));
        fd1 -> floppy_type = floppy_type;
        fd1 -> name = floppy_names[(int)floppy_type];
        fd1 -> base = 0x3F0;
        fd1 -> geometry = &floppy_geometrys[floppy_type - 1];
        fd1 -> motor_on = 0;
        fd1 -> sr0 = 0;
        fd1 -> track = -1;
        fd1 -> locked = FALSE;
    }
    if((floppy_type = data & 0x0F))
    {
        fd2 = (fdisk_t *)malloc(sizeof(fdisk_t));
        fd2 -> floppy_type = floppy_type;
        fd2 -> name = floppy_names[(int)floppy_type];
        fd2 -> base = (short)0x370;
        fd2 -> geometry = &floppy_geometrys[floppy_type - 1];
        fd2 -> motor_on = 0;
        fd2 -> sr0 = 0;
        fd2 -> track = -1;
        fd2 -> locked = FALSE;
    }
}

struct DEVICE_HANDLE * fda_floppy_open(struct DEVICE_HANDLE * handle, char * buffer)
{
    return (struct DEVICE_HANDLE *)0;
}
int fda_floppy_close(struct DEVICE_HANDLE * handle)
{
    return 0;
}
int fda_floppy_read(struct DEVICE_HANDLE * handle, char * buffer, int size)
{
    return 0;
}
int fda_floppy_write(struct DEVICE_HANDLE * handle, char * buffer, int size)
{
    return 0;
}

struct DEVICE_HANDLE * fdb_floppy_open(struct DEVICE_HANDLE * handle, char * buffer)
{
    return (struct DEVICE_HANDLE *)0;
}
int fdb_floppy_close(struct DEVICE_HANDLE * handle)
{
    return 0;
}
int fdb_floppy_read(struct DEVICE_HANDLE * handle, char * buffer, int size)
{
    return 0;
}
int fdb_floppy_write(struct DEVICE_HANDLE * handle, char * buffer, int size)
{
    return 0;
}

void init_floppy()
{
//    struct DEVICE_CALLTABLE * fda_table;
//    struct DEVICE_CALLTABLE * fdb_table;
    get_floppy_types();
//    if(fd1 != NULL);
//    {
//        fda_table = (struct DEVICE_CALLTABLE *)malloc(sizeof(struct DEVICE_CALLTABLE));
//        fda_table -> open = fda_floppy_open;
//        fda_table -> close = fda_floppy_close;
//        fda_table -> read = fda_floppy_read;
//        fda_table -> write = fda_floppy_write;
//        dev_add("fda", fda_table, BLOCK_DEVICE);
//    }    
//    if(fd2 != NULL);
//    {
//        fdb_table = (struct DEVICE_CALLTABLE *)malloc(sizeof(struct DEVICE_CALLTABLE));
//        fdb_table -> open = fdb_floppy_open;
//        fdb_table -> close = fdb_floppy_close;
//        fdb_table -> read = fdb_floppy_read;
//        fdb_table -> write = fdb_floppy_write;
//        dev_add("fdb", fdb_table, BLOCK_DEVICE);
//    }    
    irq_setter(6, irq6_handler);
    if(fd1 != NULL);
        fdc_reset(fd1);
    if(fd2 != NULL);
        fdc_reset(fd2);
}

void floppy_ident()
{
    if(fd1 != NULL);
        printf("   Floppy Drive 1: %s\n", fd1 -> name);
    if(fd2 != NULL);
        printf("   Floppy Drive 2: %s\n", fd2 -> name);
}

void floppy_read_sector(unsigned char *buf, int pos, int count)
{
    int sec, cyl, head, max_pos = pos + count;
    start_motor(fd1);
    for(; pos < max_pos; pos++)
    {
        sec = (pos % 18) + 1;
        cyl = pos / 36;
        head = (pos / 18) % 2;
        outportb((fd1 -> base + 0x7), 0);
        while(fdc_seek((char)cyl, fd1) == error);
        set_dma(0x00000000, 512, 0, 2);
        delay(250);
        if(inportb(fd1 -> base + 0x4) != 0x80)
        {
            puts("The DMA bit wasn't set\n");
            stop_motor(fd1);
            return;
        }    
        sendbyte(0xE6, fd1);
        sendbyte((unsigned char)head << 2, fd1);
        sendbyte((unsigned char)cyl, fd1);
        sendbyte((unsigned char)head, fd1);
        sendbyte((unsigned char)sec, fd1);
        sendbyte(0x02, fd1);
        sendbyte(0x12, fd1);
        sendbyte(0x1B, fd1);
        sendbyte(0xFF, fd1);
        wait_irq6();
        (void)getbyte(fd1);
        (void)getbyte(fd1);
        (void)getbyte(fd1);
        (void)getbyte(fd1);
        (void)getbyte(fd1);
        (void)getbyte(fd1);
        (void)getbyte(fd1);
        memcpyi((unsigned int *)buf, (unsigned int *)0x00000000, 128);
        buf += 0x200;
    }    
    stop_motor(fd1);
}

void floppy_read_sectors( unsigned char *buf, int pos, int count )
{
}    
    

/*
void floppy_write_sector(unsigned char *buf, int pos, int read)
{
    int sec, cyl, head, stat = error;
    sec = (pos % 18) + 1;
    cyl = (pos / 18) / 2;
    head = cyl % 2;
    start_motor();
    outportb((base_used + 0x7), 0);
    while(stat == error)
        stat = fdc_seek((char)cyl);
    set_dma(buf, 512, 1, 2);
    if(inportb(base_used + 0x4) != 0x80)
    {
        puts("The DMA bit wasn't set\n");
        return;
    }
    sendbyte(0xC5);
    sendbyte((unsigned char)head << 2);
    sendbyte((unsigned char)cyl);
    sendbyte((unsigned char)head);
    sendbyte((unsigned char)sec);
    sendbyte(0x02);
    sendbyte(0x12);
    sendbyte(0x1B);
    sendbyte(0xFF);
    wait_irq6();
    (void)getbyte();
    (void)getbyte();
    (void)getbyte();
    (void)getbyte();
    (void)getbyte();
    (void)getbyte();
    (void)getbyte();
    stop_motor();
}
*/
