⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floppy.c

📁 带有中文注释的linux内核源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/** 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.*//** 02.12.91 - 修改成静态变量,以适应复位和重新校正操作。这使得某些事情* 做起来较为方便(output_byte 复位检查等),并且意味着在出错时中断跳转* 要少一些,所以希望代码能更容易被理解。*//** 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.*//** 如同hd.c 文件一样,该文件中的所有子程序都能够被中断调用,所以需要特别* 地小心。硬件中断处理程序是不能睡眠的,否则内核就会傻掉(死机)?。因此不能* 直接调用"floppy-on",而只能设置一个特殊的时间中断等。** 另外,我不能保证该程序能在多于1 个软驱的系统上工作,有可能存在错误。*/#include <linux/sched.h>	// 调度程序头文件,定义了任务结构task_struct、初始任务0 的数据,// 还有一些有关描述符参数设置和获取的嵌入式汇编函数宏语句。#include <linux/fs.h>		// 文件系统头文件。定义文件表结构(file,buffer_head,m_inode 等)。#include <linux/kernel.h>	// 内核头文件。含有一些内核常用函数的原形定义。#include <linux/fdreg.h>	// 软驱头文件。含有软盘控制器参数的一些定义。#include <asm/system.h>		// 系统头文件。定义了设置或修改描述符/中断门等的嵌入式汇编宏。#include <asm/io.h>		// io 头文件。定义硬件端口输入/输出宏汇编语句。#include <asm/segment.h>	// 段操作头文件。定义了有关段寄存器操作的嵌入式汇编函数。#define MAJOR_NR 2		// 软驱的主设备号是2。#include "blk.h"		// 块设备头文件。定义请求数据结构、块设备数据结构和宏函数等信息。static int recalibrate = 0;	// 标志:需要重新校正。static int reset = 0;		// 标志:需要进行复位操作。static int seek = 0;		// 寻道。extern unsigned char current_DOR;	// 当前数字输出寄存器(Digital Output Register)。#define immoutb_p(val,port) \	// 字节直接输出(嵌入汇编语言宏)。__asm__ ("outb %0,%1\n\tjmp 1f\n1:\tjmp 1f\n1:"::"a" ((char) (val)),	 "i" (port))// 这两个定义用于计算软驱的设备号。次设备号 = TYPE*4 + DRIVE。计算方法参见列表后。#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.*//** 注意,下面定义MAX_ERRORS=8 并不表示对每次读错误尝试最多8 次 - 有些类型* 的错误将把出错计数值乘2,所以我们实际上在放弃操作之前只需尝试5-6 遍即可。*/#define MAX_ERRORS 8/** globals used by 'result()'*//* 下面是函数'result()'使用的全局变量 */// 这些状态字节中各比特位的含义请参见include/linux/fdreg.h 头文件。#define MAX_REPLIES 7		// FDC 最多返回7 字节的结果信息。     static unsigned char reply_buffer[MAX_REPLIES];	// 存放FDC 返回的结果信息。#define ST0 (reply_buffer[0])	// 返回结果状态字节0。#define ST1 (reply_buffer[1])	// 返回结果状态字节1。#define ST2 (reply_buffer[2])	// 返回结果状态字节2。#define ST3 (reply_buffer[3])	// 返回结果状态字节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.*//** 下面的软盘结构定义了不同的软盘类型。与minix 不同的是,linux 没有* "搜索正确的类型"-类型,因为对其处理的代码令人费解且怪怪的。本程序* 已经让我遇到了许多的问题了。** 对某些类型的软盘(例如在1.2MB 驱动器中的360kB 软盘等),'stretch'用于* 检测磁道是否需要特殊处理。其它参数应该是自明的。*/// 软盘参数有:// size 大小(扇区数);// sect 每磁道扇区数;// head 磁头数;// track 磁道数;// stretch 对磁道是否要特殊处理(标志);// gap 扇区间隙长度(字节数);// rate 数据传输速率;// spec1 参数(高4 位步进速率,低四位磁头卸载时间)。     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). *//** 上面速率rate:0 表示500kb/s,1 表示300kbps,2 表示250kbps。* 参数spec1 是0xSH,其中S 是步进速率(F-1 毫秒,E-2ms,D=3ms 等),* H 是磁头卸载时间(1=16ms,2=32ms 等)** spec2 是(HLD<<1 | ND),其中HLD 是磁头加载时间(1=2ms,2=4ms 等)* ND 置位表示不使用DMA(No DMA),在程序中硬编码成6(HLD=6ms,使用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;//// 释放(取消选定的)软盘(软驱)。// 数字输出寄存器(DOR)的低2 位用于指定选择的软驱(0-3 对应A-D)。voidfloppy_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.  *//** floppy-change()不是从中断程序中调用的,所以这里我们可以轻松一下,睡觉等。* 注意floppy-on()会尝试设置current_DOR 指向所需的驱动器,但当同时使用几个* 软盘时不能睡眠:因此此时只能使用循环方式。*///// 检测指定软驱中软盘更换情况。如果软盘更换了则返回1,否则返回0。intfloppy_change (unsigned int nr){repeat:  floppy_on (nr);		// 开启指定软驱nr(kernel/sched.c,251)。// 如果当前选择的软驱不是指定的软驱nr,并且已经选择其它了软驱,则让当前任务进入可中断// 等待状态。  while ((current_DOR & 3) != nr && selected)    interruptible_sleep_on (&wait_on_floppy_select);// 如果当前没有选择其它软驱或者当前任务被唤醒时,当前软驱仍然不是指定的软驱nr,则循环等待。  if ((current_DOR & 3) != nr)    goto repeat;// 取数字输入寄存器值,如果最高位(位7)置位,则表示软盘已更换,此时关闭马达并退出返回1。// 否则关闭马达退出返回0。  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")//// 设置(初始化)软盘DMA 通道。static voidsetup_DMA (void){  long addr = (long) CURRENT->buffer;	// 当前请求项缓冲区所处内存中位置(地址)。  cli ();// 如果缓冲区处于内存1M 以上的地方,则将DMA 缓冲区设在临时缓冲区域(tmp_floppy_area 数组)// (因为8237A 芯片只能在1M 地址范围内寻址)。如果是写盘命令,则还需将数据复制到该临时区域。  if (addr >= 0x100000)    {      addr = (long) tmp_floppy_area;      if (command == FD_WRITE)	copy_buffer (CURRENT->buffer, tmp_floppy_area);    }/* mask DMA 2 *//* 屏蔽DMA 通道2 */// 单通道屏蔽寄存器端口为0x10。位0-1 指定DMA 通道(0--3),位2:1 表示屏蔽,0 表示允许请求。  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 *//* 输出命令字节。我是不知道为什么,但是每个人(minix,*//* sanches 和canton)都输出两次,首先是12 口,然后是11 口 */// 下面嵌入汇编代码向DMA 控制器端口12 和11 写方式字(读盘0x46,写盘0x4A)。  __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 *//* 地址低0-7 位 */// 向DMA 通道2 写入基/当前地址寄存器(端口4)。  immoutb_p (addr, 4);  addr >>= 8;/* bits 8-15 of addr *//* 地址高8-15 位 */  immoutb_p (addr, 4);  addr >>= 8;/* bits 16-19 of addr *//* 地址16-19 位 */// DMA 只可以在1M 内存空间内寻址,其高16-19 位地址需放入页面寄存器(端口0x81)。  immoutb_p (addr, 0x81);/* low 8 bits of count-1 (1024-1=0x3ff) *//* 计数器低8 位(1024-1=0x3ff) */// 向DMA 通道2 写入基/当前字节计数器值(端口5)。  immoutb_p (0xff, 5);/* high 8 bits of count-1 *//* 计数器高8 位 */// 一次共传输1024 字节(两个扇区)。  immoutb_p (3, 5);/* activate DMA 2 *//* 开启DMA 通道2 的请求 */// 复位对DMA 通道2 的屏蔽,开放DMA2 请求DREQ 信号。  immoutb_p (0 | 2, 10);  sti ();}//// 向软盘控制器输出一个字节数据(命令或参数)。static voidoutput_byte (char byte){  int counter;  unsigned char status;  if (reset)    return;// 循环读取主状态控制器FD_STATUS(0x3f4)的状态。如果状态是STATUS_READY 并且STATUS_DIR=0// (CPU??FDC),则向数据端口输出指定字节。  for (counter = 0; counter < 10000; counter++)    {      status = inb_p (FD_STATUS) & (STATUS_READY | STATUS_DIR);      if (status == STATUS_READY)	{	  outb (byte, FD_DATA);	  return;	}    }// 如果到循环1 万次结束还不能发送,则置复位标志,并打印出错信息。  reset = 1;  printk ("Unable to send byte to FDC\n\r");}//// 读取FDC 执行的结果信息。// 结果信息最多7 个字节,存放在reply_buffer[]中。返回读入的结果字节数,若返回值=-1// 表示出错。static intresult (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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -