/*
* 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; // 在文件sched.c中定义
#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) // 软驱类型,2--1.2MB,7--1.44MB
#define DRIVE(x) ((x)&0x03) // 软驱序列号0-3对应A-D
/*
* 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()'
*/
/* 下面的全局变量仅供函数result使用 */
#define MAX_REPLIES 7 // FDC最多返回7个字节的结果
static unsigned char reply_buffer[MAX_REPLIES]; // 存放结果的buffer
#define ST0 (reply_buffer[0]) // 第一个char
#define ST1 (reply_buffer[1]) // 第二个char
#define ST2 (reply_buffer[2]) // 第三个char
#define ST3 (reply_buffer[3]) // 第四个char
/*
* 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, // 扇区数
unsigned int sect, // 每个磁道扇区数
unsigned int head, // 磁头数
unsigned int track, // 磁道数
unsigned int stretch, // 对磁道是否需要特殊处理
unsigned char gap, // 扇区间隙长度
unsigned char rate, // 数据传输率
unsigned char 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);
}
/* 指定的软驱更换软盘,如果软盘更换了的话,返回1,否则返回0 */
/*
* 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)
{
repeat:
floppy_on(nr); // 打开指定软驱
// 如果当前的软驱不是nr,或者说是没有选择的话,当前进程进入睡眠状态
while ((current_DOR & 3) != nr && selected)
interruptible_sleep_on(&wait_on_floppy_select);
// 当睡眠苏醒或者说是没有选择其他软驱,如果此时还不是选择的是nr
if ((current_DOR & 3) != nr)
goto repeat; // 继续等待
if (inb(FD_DIR) & 0x80) // 查看当前的状态,如果已经更换软盘的话
{
floppy_off(nr); // 关闭马达
return 1; // 返回1
}
floppy_off(nr); // 关闭马达
return 0; // 返回0
}
/* 复制内存 */
#define copy_buffer(from,to) \
__asm__("cld ; rep ; movsl" \
::"c" (BLOCK_SIZE/4),"S" ((long)(from)),"D" ((long)(to)) \
:"cx","di","si")
/* 初始化软盘的DMA通道 */
static void setup_DMA(void)
{
long addr = (long) CURRENT->buffer; // 当前请求项的缓冲区地址
cli(); // 关中断
if (addr >= 0x100000) // 如果地址大于0x100000
{
addr = (long) tmp_floppy_area; // 则将DMA的缓冲区设置为tmp_floppy_area
/* 主要是因为芯片的访存能力是有限的 */
if (command == FD_WRITE) // 如果是写盘的命令
copy_buffer(CURRENT->buffer,tmp_floppy_area); // 将内容复制到tmp_floppy_area
}
/* mask DMA 2 */
/* 屏蔽DAM通道2,DMA中存在不知一个DAM通道,每个DMA通道可以打开或者是关闭 */
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 */
/* 下面的汇编向DMA的端口的11,12写方式字 */
__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)));
/* 8 low bits of addr */
/* 向通道2写入低地址 */
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);
/* count的值写入寄存器 */
/* 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); // 开放DMA请求
sti(); // 开中断
}
/* 向软盘输出一个char字节流 */
static void output_byte(char byte)
{
int counter;
unsigned char status;
if (reset) // 如果需要重新置位
return;
/* 否则的话,在等待很长的时间之后,如果status还不正确的话,返回 */
for(counter = 0 ; counter < 10000 ; counter++) // 很长时间循环
{
status = inb_p(FD_STATUS) & (STATUS_READY | STATUS_DIR); // 读取状态
if (status == STATUS_READY) // ready?
{
outb(byte,FD_DATA); // 向DMA输入数据
return; // 返回
}
}
/* 函数还没返回,说明出现错误 */
reset = 1; // 需呀重新置位
printk("Unable to send byte to FDC\n\r");
}
/* 读取FDC执行的结果,函数如果正常执行的话,返回的是读取状态字数 */
/* ,如果返回-1,表示函数出错 */
static int result(void)
{
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++; // 更新errors值
if (CURRENT->errors > MAX_ERRORS) // 超过MAX_ERRORS
{
floppy_deselect(current_drive); // 取消选定软盘
end_request(0); // 停止请求
}
if (CURRENT->errors > MAX_ERRORS/2) // 如果大于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.
*/
/* 下面的函数是在DMA读或者是写成功时,调用的,于是需要检查结果,复制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置位驱动器
bad_flp_intr();
do_fd_request(); // 执行请求
return; // 返回
}
/* 函数还没返回 */
// 如果缓冲区地址大于1M,命令时”读“
if (command == FD_READ && (unsigned long)(CURRENT->buffer) >= 0x100000)
copy_buffer(tmp_floppy_area,CURRENT->buffer); // 复制buffer内容
floppy_deselect(current_drive); // 释放软盘
end_request(1); // 结束请求
do_fd_request(); // 继续执行其他请求
}
/* 设置DMA,并且输出软盘的命令和操作数 */
inline void setup_rw_floppy(void)
{
setup_DMA(); // 建立DMA
do_floppy = rw_interrupt; // 设置DMA中断调用函数
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)
{
/* sense drive status */
output_byte(FD_SENSEI);
/* 如果返回值不是2,或者ST0不是寻道结束,ST1不是当前选择的磁道 */.
if (result() != 2 || (ST0 & 0xF8) != 0x20 || ST1 != seek_track)
{
bad_flp_intr(); // 更新errors等的值
do_fd_request(); // 处理请求
return;
}
current_track = ST1; // 设置current_track为当前值
setup_rw_floppy(); // 重新建立DMA并设置软盘操作命令
}
/*
* 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) // 如果seek = 0,即是不需要寻道,调用setup_rw_floppy
{
setup_rw_floppy();
return;
}
do_floppy = seek_interrupt; // 设置软盘中断处理函数是寻道中断函数
if (seek_track) // 如果起始磁道号不等于0,则发送寻道命令和参数
{
/* 发送寻道命令和参数 */
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; // 重新校正
}
/* 重新校正处理函数 */
/*
* 该命令用来让磁头退回到0磁道.通常用于在软盘操作出错
* 时对磁头重新校正定位.其命令码是0x07,参数是指定的驱
* 动器号(0—3).
*/
static void recalibrate_floppy(void)
{
recalibrate = 0; // 重新校正标志置0
current_track = 0; // 当前磁道号0
do_floppy = recal_interrupt; // 置软盘中断函数为重新校正函数
output_byte(FD_RECALIBRATE); // 发送校正命令
output_byte(head<<2 | current_drive); // 参数
if (reset) // 如果需要复位(出错)
do_fd_request(); // 执行其他请求
}
/* FDC软盘复位中断处理程序 */
static void reset_interrupt(void)
{
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; // 复位标志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); // 对软盘FDC进行复位操作
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;
// 如果当前驱动器号与数字输出ODR不同,重置ODR为当前驱动器
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) // 当前驱动器不是请求项中指定的驱动器
seek = 1; // 重新寻道
current_drive = CURRENT_DEV; // 设置当前驱动器
block = CURRENT->sector; // 当前请求软盘其实扇区号
/* 因为每次处理时,是采用的2个扇区一起的,所以需要检查下面的语句 */
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++; // 磁盘上扇区是从1开始计数的
if (CURRENT->cmd == READ) // 命令时read
command = FD_READ; // 置“读”的命令码
else if (CURRENT->cmd == WRITE)
command = FD_WRITE;
else // 位置命令
panic("do_fd_request: unknown command");
/* 定时器到时,就调用函数floppy_on_interrupt */
add_timer(ticks_to_floppy_on(current_drive),&floppy_on_interrupt);
}
/* 软盘初始化 */
void floppy_init(void)
{
blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST; // 设置软盘中断函数floppy_on_interrupt
set_trap_gate(0x26,&floppy_interrupt); // 设置中断门
outb(inb_p(0x21)&~0x40,0x21); // 取消中断请求号屏蔽,即是允许中断请求
}
/*
* 该文件的核心作用是实现缓冲区和磁盘的数据交换。在主要函数的调用关系如下:
* do_fd_request ---> floppy_on_interrupt ---> transfer
* 其次是一些中断处理函数:
* bad_flp_intr,rw_interrupt,seek_interrupt,recal_interrupt,
* unexpected_floppy_interrupt,reset_interrupt。
*/
参考《linux内核完全注释》和网上相关资料