linux-floppy.c

 

/*
*  linux/kernel/floppy.c
*                                            //--软盘控制驱动程序
*  (C) 1991  Linus Torvalds
*/
/*
* 02.12.91 - Changed to static variables to indicate need for reset
* and recalibrate. This makes some things easier (output_byte reset
* checking etc), and means less interrupt jumping in case of errors,
* so the code is hopefully easier to understand.
*/
/*
* This file is certainly a mess. I've tried my best to get it working,
* but I don't like programming floppies, and I have only one anyway.
* Urgel. I should check for more errors, and do more graceful error
* recovery. Seems there are problems with several drives. I've tried to
* correct them. No promises.
*/
/*
* As with hd.c, all routines within this file can (and will) be called
* by interrupts, so extreme caution is needed. A hardware interrupt
* handler may not sleep, or a kernel panic will happen. Thus I cannot
* call "floppy-on" directly, but have to set a special timer interrupt
* etc.
*
* Also, I'm not certain this works on more than 1 floppy. Bugs may
* abund.
*/
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/fdreg.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/segment.h>
#define MAJOR_NR 2                                //--软驱主设备号
#include "blk.h"
static int recalibrate = 0;
static int reset = 0;
static int seek = 0;
extern unsigned char current_DOR;
#define immoutb_p(val,port) /                    //--字节直接数输出,把val输出到port端口
__asm__("outb %0,%1/n/tjmp 1f/n1:/tjmp 1f/n1:"::"a" ((char) (val)),"i" (port))
#define TYPE(x) ((x)>>2)
#define DRIVE(x) ((x)&0x03)
/*
* Note that MAX_ERRORS=8 doesn't imply that we retry every bad read
* max 8 times - some types of errors increase the errorcount by 2,
* so we might actually retry only 5-6 times before giving up.
*/
#define MAX_ERRORS 8
/*
* globals used by 'result()'
*/
#define MAX_REPLIES 7
static unsigned char reply_buffer[MAX_REPLIES];
#define ST0 (reply_buffer[0])
#define ST1 (reply_buffer[1])
#define ST2 (reply_buffer[2])
#define ST3 (reply_buffer[3])
/*
* This struct defines the different floppy types. Unlike minix
* linux doesn't have a "search for right type"-type, as the code
* for that is convoluted and weird. I've got enough problems with
* this driver as it is.
*
* The 'stretch' tells if the tracks need to be boubled for some
* types (ie 360kB diskette in 1.2MB drive etc). Others should
* be self-explanatory.
*/
static struct floppy_struct {                    //--定义软盘的结构
    unsigned int size, sect, head, track, stretch;    //--大小,扇区数,磁头数,磁道数,标志
    unsigned char gap,rate,spec1;                    //--扇区间隙,传输速率,参数
} floppy_type[] = {
    {    0, 0,0, 0,0,0x00,0x00,0x00 },    /* no testing */
    {  720, 9,2,40,0,0x2A,0x02,0xDF },    /* 360kB PC diskettes */
    { 2400,15,2,80,0,0x1B,0x00,0xDF },    /* 1.2 MB AT-diskettes */
    {  720, 9,2,40,1,0x2A,0x02,0xDF },    /* 360kB in 720kB drive */
    { 1440, 9,2,80,0,0x2A,0x02,0xDF },    /* 3.5" 720kB diskette */
    {  720, 9,2,40,1,0x23,0x01,0xDF },    /* 360kB in 1.2MB drive */
    { 1440, 9,2,80,0,0x23,0x01,0xDF },    /* 720kB in 1.2MB drive */
    { 2880,18,2,80,0,0x1B,0x00,0xCF },    /* 1.44MB diskette */
};
/*
* Rate is 0 for 500kb/s, 2 for 300kbps, 1 for 250kbps
* Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
* H is head unload time (1=16ms, 2=32ms, etc)
*
* Spec2 is (HLD<<1 | ND), where HLD is head load time (1=2ms, 2=4 ms etc)
* and ND is set means no DMA. Hardcoded to 6 (HLD=6ms, use DMA).
*/
extern void floppy_interrupt(void);
extern char tmp_floppy_area[1024];
/*
* These are global variables, as that's the easiest way to give
* information to interrupts. They are the data used for the current
* request.
*/
static int cur_spec1 = -1;
static int cur_rate = -1;
static struct floppy_struct * floppy = floppy_type;
static unsigned char current_drive = 0;
static unsigned char sector = 0;
static unsigned char head = 0;
static unsigned char track = 0;
static unsigned char seek_track = 0;
static unsigned char current_track = 255;
static unsigned char command = 0;
unsigned char selected = 0;
struct task_struct * wait_on_floppy_select = NULL;
void floppy_deselect(unsigned int nr)            //--取消选定软驱
{
    if (nr != (current_DOR & 3))
        printk("floppy_deselect: drive not selected/n/r");
    selected = 0;
    wake_up(&wait_on_floppy_select);
}
/*
* floppy-change is never called from an interrupt, so we can relax a bit
* here, sleep etc. Note that floppy-on tries to set current_DOR to point
* to the desired drive, but it will probably not survive the sleep if
* several floppies are used at the same time: thus the loop.
*/
int floppy_change(unsigned int nr)    //--检测指定软驱中软盘更换情况,由fs/buffer.c中check_disk_change()调用
{
repeat:
    floppy_on(nr);                    //--等待软驱启动
    while ((current_DOR & 3) != nr && selected)
        sleep_on(&wait_on_floppy_select);
    if ((current_DOR & 3) != nr)
        goto repeat;
    if (inb(FD_DIR) & 0x80) {
        floppy_off(nr);
        return 1;
    }
    floppy_off(nr);
    return 0;
}
#define copy_buffer(from,to) /                //--复制内存缓冲块
__asm__("cld ; rep ; movsl" /
    ::"c" (BLOCK_SIZE/4),"S" ((long)(from)),"D" ((long)(to)) /
    :"cx","di","si")
static void setup_DMA(void)                    //--设置软盘DMA通道
{
    long addr = (long) CURRENT->buffer;
    cli();
    if (addr >= 0x100000) {
        addr = (long) tmp_floppy_area;
        if (command == FD_WRITE)
            copy_buffer(CURRENT->buffer,tmp_floppy_area);
    }
/* mask DMA 2 */
    immoutb_p(4|2,10);
/* output command byte. I don't know why, but everyone (minix, */
/* sanches & canton) output this twice, first to 12 then to 11 */
     __asm__("outb %%al,$12/n/tjmp 1f/n1:/tjmp 1f/n1:/t"
    "outb %%al,$11/n/tjmp 1f/n1:/tjmp 1f/n1:"::
    "a" ((char) ((command == FD_READ)?DMA_READ:DMA_WRITE)));    //--向DMA控制器写入方式字
/* 8 low bits of addr */
    immoutb_p(addr,4);
    addr >>= 8;
/* bits 8-15 of addr */
    immoutb_p(addr,4);
    addr >>= 8;
/* bits 16-19 of addr */
    immoutb_p(addr,0x81);
/* low 8 bits of count-1 (1024-1=0x3ff) */
    immoutb_p(0xff,5);
/* high 8 bits of count-1 */
    immoutb_p(3,5);
/* activate DMA 2 */
    immoutb_p(0|2,10);
    sti();
}
static void output_byte(char byte)                //--向软驱控制器输出一个字节命令或参数
{
    int counter;
    unsigned char status;
    if (reset)
        return;
    for(counter = 0 ; counter < 10000 ; counter++) {
        status = inb_p(FD_STATUS) & (STATUS_READY | STATUS_DIR);
        if (status == STATUS_READY) {
            outb(byte,FD_DATA);
            return;
        }
    }
    reset = 1;
    printk("Unable to send byte to FDC/n/r");
}
static int result(void)                            //--读取FDC执行的结果信息
{
    int i = 0, counter, status;
    if (reset)
        return -1;
    for (counter = 0 ; counter < 10000 ; counter++) {
        status = inb_p(FD_STATUS)&(STATUS_DIR|STATUS_READY|STATUS_BUSY);
        if (status == STATUS_READY)
            return i;
        if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY)) {
            if (i >= MAX_REPLIES)
                break;
            reply_buffer[i++] = inb_p(FD_DATA);
        }
    }
    reset = 1;
    printk("Getstatus times out/n/r");
    return -1;
}
static void bad_flp_intr(void)                        //--软盘读写出错信息
{                                    //--该函数根据软盘读写出错次数来确定需要采取的进一步行动
    CURRENT->errors++;
    if (CURRENT->errors > MAX_ERRORS) {
        floppy_deselect(current_drive);
        end_request(0);
    }
    if (CURRENT->errors > MAX_ERRORS/2)
        reset = 1;
    else
        recalibrate = 1;
}    
/*
* Ok, this interrupt is called after a DMA read/write has succeeded,
* so we check the results, and copy any buffers.
*/
static void rw_interrupt(void)            //--软盘读写操作中断调用函数
{
    if (result() != 7 || (ST0 & 0xf8) || (ST1 & 0xbf) || (ST2 & 0x73)) {
        if (ST1 & 0x02) {
            printk("Drive %d is write protected/n/r",current_drive);
            floppy_deselect(current_drive);
            end_request(0);
        } else
            bad_flp_intr();
        do_fd_request();
        return;
    }
    if (command == FD_READ && (unsigned long)(CURRENT->buffer) >= 0x100000)
        copy_buffer(tmp_floppy_area,CURRENT->buffer);        //--复制软盘读操作的内容到当前请求的缓冲区
    floppy_deselect(current_drive);
    end_request(1);
    do_fd_request();
}
inline void setup_rw_floppy(void)            //--设置DMA通道2并向软盘控制器输出命令和参数
{
    setup_DMA();                            //--初始化软盘DMA通道
    do_floppy = rw_interrupt;
    output_byte(command);
    output_byte(head<<2 | current_drive);
    output_byte(track);
    output_byte(head);
    output_byte(sector);
    output_byte(2);        /* sector size = 512 */
    output_byte(floppy->sect);
    output_byte(floppy->gap);
    output_byte(0xFF);    /* sector size (0xff when n!=0 ?) */
    if (reset)
        do_fd_request();
}
/*
* This is the routine called after every seek (or recalibrate) interrupt
* from the floppy controller. Note that the "unexpected interrupt" routine
* also does a recalibrate, but doesn't come here.
*/
static void seek_interrupt(void)            //--寻道结束后中断过程中调用的C函数
{
/* sense drive status */
    output_byte(FD_SENSEI);
    if (result() != 2 || (ST0 & 0xF8) != 0x20 || ST1 != seek_track) {
        bad_flp_intr();
        do_fd_request();
        return;
    }
    current_track = ST1;            //--若寻道操作成功,则向软盘控制器发送命令和参数
    setup_rw_floppy();
}
/*
* This routine is called when everything should be correctly set up
* for the transfer (ie floppy motor is on and the correct floppy is
* selected).
*/
static void transfer(void)            //--在传输操作的所有信息都正确设置好后被调用的,读写数据传输函数
{
    if (cur_spec1 != floppy->spec1) {
        cur_spec1 = floppy->spec1;
        output_byte(FD_SPECIFY);
        output_byte(cur_spec1);        /* hut etc */
        output_byte(6);            /* Head load time =6ms, DMA */
    }
    if (cur_rate != floppy->rate)
        outb_p(cur_rate = floppy->rate,FD_DCR);
    if (reset) {
        do_fd_request();
        return;
    }
    if (!seek) {
        setup_rw_floppy();
        return;
    }
    do_floppy = seek_interrupt;
    if (seek_track) {
        output_byte(FD_SEEK);
        output_byte(head<<2 | current_drive);
        output_byte(seek_track);
    } else {
        output_byte(FD_RECALIBRATE);
        output_byte(head<<2 | current_drive);
    }
    if (reset)
        do_fd_request();
}
/*
* Special case - used after a unexpected interrupt (or reset)
*/
static void recal_interrupt(void)            //--用于意外中断处理
{                                            //--软驱重新校正中断调用函数
    output_byte(FD_SENSEI);
    if (result()!=2 || (ST0 & 0xE0) == 0x60)
        reset = 1;
    else
        recalibrate = 0;
    do_fd_request();
}
void unexpected_floppy_interrupt(void)        //--意外软盘中断请求引发的软盘中断处理程序中调用的函数
{
    output_byte(FD_SENSEI);
    if (result()!=2 || (ST0 & 0xE0) == 0x60)
        reset = 1;
    else
        recalibrate = 1;
}
static void recalibrate_floppy(void)        //--软盘重新校正处理函数
{
    recalibrate = 0;
    current_track = 0;
    do_floppy = recal_interrupt;
    output_byte(FD_RECALIBRATE);
    output_byte(head<<2 | current_drive);
    if (reset)
        do_fd_request();
}
static void reset_interrupt(void)            //--软盘控制器FDC复位中断调用函数
{
    output_byte(FD_SENSEI);
    (void) result();
    output_byte(FD_SPECIFY);
    output_byte(cur_spec1);        /* hut etc */
    output_byte(6);            /* Head load time =6ms, DMA */
    do_fd_request();
}
/*
* reset is done by pulling bit 2 of DOR low for a while.
*/
static void reset_floppy(void)                //--复位软盘控制器
{
    int i;
    reset = 0;
    cur_spec1 = -1;
    cur_rate = -1;
    recalibrate = 1;
    printk("Reset-floppy called/n/r");
    cli();
    do_floppy = reset_interrupt;
    outb_p(current_DOR & ~0x04,FD_DOR);
    for (i=0 ; i<100 ; i++)
        __asm__("nop");
    outb(current_DOR,FD_DOR);
    sti();
}
static void floppy_on_interrupt(void)        //--软驱启动定时中断调用函数
{
/* We cannot do a floppy-select, as that might sleep. We just force it */
    selected = 1;
    if (current_drive != (current_DOR & 3)) {
        current_DOR &= 0xFC;
        current_DOR |= current_drive;
        outb(current_DOR,FD_DOR);
        add_timer(2,&transfer);
    } else
        transfer();
}
void do_fd_request(void)                    //--软盘读写请求处理函数
{
    unsigned int block;
    seek = 0;
    if (reset) {
        reset_floppy();
        return;
    }
    if (recalibrate) {
        recalibrate_floppy();
        return;
    }
    INIT_REQUEST;                            //--检测请求的合法性
    floppy = (MINOR(CURRENT->dev)>>2) + floppy_type;        //--取得请求项指定软驱的参数
    if (current_drive != CURRENT_DEV)        //--下面设置112-122行的全局变量值
        seek = 1;
    current_drive = CURRENT_DEV;
    block = CURRENT->sector;                //--设置读写起始扇区block
    if (block+2 > floppy->size) {
        end_request(0);
        goto repeat;
    }
    sector = block % floppy->sect;            //--取扇区号,磁头号,磁道号...
    block /= floppy->sect;
    head = block % floppy->head;
    track = block / floppy->head;
    seek_track = track << floppy->stretch;
    if (seek_track != current_track)
        seek = 1;
    sector++;
    if (CURRENT->cmd == READ)
        command = FD_READ;
    else if (CURRENT->cmd == WRITE)
        command = FD_WRITE;
    else
        panic("do_fd_request: unknown command");
    add_timer(ticks_to_floppy_on(current_drive),&floppy_on_interrupt);
}                                        //--利用定时器启动操作floppy_on_interrupt
static int floppy_sizes[] ={
       0,   0,   0,   0,
     360, 360 ,360, 360,
    1200,1200,1200,1200,
     360, 360, 360, 360,
     720, 720, 720, 720,
     360, 360, 360, 360,
     720, 720, 720, 720,
    1440,1440,1440,1440
};
void floppy_init(void)                    //--软盘系统初始化
{
    blk_size[MAJOR_NR] = floppy_sizes;
    blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
    set_trap_gate(0x26,&floppy_interrupt);
    outb(inb_p(0x21)&~0x40,0x21);
}
|xGv00|fcc8d4de8197f69fde70263fb4d52380
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值