/*
 *  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

int first;
int second;
int current_track = -1;
short base_used;
volatile int irq6_state = 0;
int motor_on = 0;
int sr0 = 0;

char *floppy_types[] = 
{
    "None.",
    "360kB 5.25: Unsupported.",
    "1.2MB 5.25: Unsupported.",
    "720KB 3.5: Unsupported.",
    "1.44MB 3.5.",
    "2.88MB 3.5: Unsupported."
};

void get_floppy_type()
{
    unsigned char data;
    outportb(0x70, 0x10);
    data = inportb(0x71);
    first = data >> 4;
    second = data & 0xF;
}

struct stack_data * irq6_handler(struct stack_data *regs)
{
    irq6_state = 1;
    return regs;
}

void wait_irq6()
{
    while(irq6_state == 0);
    irq6_state = 0;
}

void start_motor()
{
    outportb((base_used + 0x2), 0x1C);
//    delay(500);
    motor_on = 1;
}    

void stop_motor()
{
    outportb((base_used + 0x2), 0x00);
//    delay(500);
    motor_on = 0;
}

/* from intel manual */
void sendbyte(char byte)
{
    volatile int msr;
    int tmo;
    for(tmo = 0; tmo < 128; tmo++) 
    {
        msr = inportb((base_used + 0x04));
        if ((msr & 0xC0) == 0x80) 
        {
            outportb((base_used + 0x05), byte);
            return;
        }
        inportb(0x80);
    }   
}

/* from intel manual */
int getbyte()
{
    volatile int msr;
    int tmo;
    for (tmo = 0; tmo < 128; tmo++) 
    {
        msr = inportb((base_used + 0x04));
        if ((msr & 0xd0) == 0xd0) 
	        return inportb((base_used + 0x05));
        inportb(0x80);
    }
    return -1;
}

void waitfdc()
{
    wait_irq6();
    sendbyte(0x08);
    sr0 = getbyte();
    (void)getbyte();
} 

int fdc_seek(char track)
{
    if(current_track == track)
        return 0;
    sendbyte(0x0F);
    sendbyte(0x00);
    sendbyte(track);
//    delay(500);
    waitfdc();
    if(sr0 != 0x20)
    {
        puts("Track seek error\n");
        return error;
    }
    current_track = track;
    return 0;
}

void fdc_recalibrate()
{
    start_motor();
    sendbyte(0x07);
    sendbyte(0x00);
    waitfdc();
    stop_motor();
}

void fdc_reset()
{
   outportb((base_used + 0x02), 0);    // stop everything
   motor_on = 0;
   outportb((base_used + 0x04), 0);     // data rate (500K/s)
   outportb((base_used + 0x02), 0x0C);  // restart ints
//   delay(500);
   waitfdc();
//   delay(500);
   sendbyte(0x03);                     // timing
   sendbyte(0xDF);                     // timing
   sendbyte(0x02);                     // timing
//   delay(500);
   while(fdc_seek(1) == error);        // set track
   fdc_recalibrate();
}

void init_floppy()
{
    base_used = 0x3F0;
    get_floppy_type();
    irq_setter(6, irq6_handler);
    fdc_reset();
}

void floppy_ident()
{
    puts("    First Floppy: ");
    puts(floppy_types[first]);
    putchar('\n');
    puts("    Second Floppy: ");
    puts(floppy_types[second]);
    putchar('\n');
}

void floppy_read_sectors(unsigned char *buf, int pos, int count)
{
    int sec, cyl, head, max_pos = pos + count;
    start_motor();
    for(; pos < max_pos; pos++)
    {
        sec = (pos % 18) + 1;
        cyl = pos / 36;
        head = (pos / 18) % 2;
        outportb((base_used + 0x7), 0);
        while(fdc_seek((char)cyl) == error);
        set_dma(0x00000000, 512, 0, 2);
//        delay(500);
//        if(inportb(base_used + 0x4) != 0x80)
//        {
//            puts("The DMA bit wasn't set\n");
////            stop_motor();
//            return;
//        }    
        sendbyte(0xE6);
        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();
        memcpyi((unsigned int *)buf, (unsigned int *)0x00000000, 128);
        buf += 0x200;
    }    
    stop_motor();
}

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();
}

