?? floppy.c
字號(hào):
/** 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 - 修改成靜態(tài)變量,以適應(yīng)復(fù)位和重新校正操作。這使得某些事情* 做起來(lái)較為方便(output_byte 復(fù)位檢查等),并且意味著在出錯(cuò)時(shí)中斷跳轉(zhuǎn)* 要少一些,所以希望代碼能更容易被理解。*//** 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.*//** 這個(gè)文件當(dāng)然比較混亂。我已經(jīng)盡我所能使其能夠工作,但我不喜歡軟驅(qū)編程,* 而且我也只有一個(gè)軟驅(qū)。另外,我應(yīng)該做更多的查錯(cuò)工作,以及改正更多的錯(cuò)誤。* 對(duì)于某些軟盤(pán)驅(qū)動(dòng)器好象還存在一些問(wèn)題。我已經(jīng)嘗試著進(jìn)行糾正了,但不能保證* 問(wèn)題已消失。*//** 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 文件一樣,該文件中的所有子程序都能夠被中斷調(diào)用,所以需要特別* 地小心。硬件中斷處理程序是不能睡眠的,否則內(nèi)核就會(huì)傻掉(死機(jī))?。因此不能* 直接調(diào)用"floppy-on",而只能設(shè)置一個(gè)特殊的時(shí)間中斷等。** 另外,我不能保證該程序能在多于1 個(gè)軟驅(qū)的系統(tǒng)上工作,有可能存在錯(cuò)誤。*/#include <linux/sched.h> // 調(diào)度程序頭文件,定義了任務(wù)結(jié)構(gòu)task_struct、初始任務(wù)0 的數(shù)據(jù),// 還有一些有關(guān)描述符參數(shù)設(shè)置和獲取的嵌入式匯編函數(shù)宏語(yǔ)句。#include <linux/fs.h> // 文件系統(tǒng)頭文件。定義文件表結(jié)構(gòu)(file,buffer_head,m_inode 等)。#include <linux/kernel.h> // 內(nèi)核頭文件。含有一些內(nèi)核常用函數(shù)的原形定義。#include <linux/fdreg.h> // 軟驅(qū)頭文件。含有軟盤(pán)控制器參數(shù)的一些定義。#include <asm/system.h> // 系統(tǒng)頭文件。定義了設(shè)置或修改描述符/中斷門(mén)等的嵌入式匯編宏。#include <asm/io.h> // io 頭文件。定義硬件端口輸入/輸出宏匯編語(yǔ)句。#include <asm/segment.h> // 段操作頭文件。定義了有關(guān)段寄存器操作的嵌入式匯編函數(shù)。#define MAJOR_NR 2 // 軟驅(qū)的主設(shè)備號(hào)是2。#include "blk.h" // 塊設(shè)備頭文件。定義請(qǐng)求數(shù)據(jù)結(jié)構(gòu)、塊設(shè)備數(shù)據(jù)結(jié)構(gòu)和宏函數(shù)等信息。static int recalibrate = 0; // 標(biāo)志:需要重新校正。static int reset = 0; // 標(biāo)志:需要進(jìn)行復(fù)位操作。static int seek = 0; // 尋道。extern unsigned char current_DOR; // 當(dāng)前數(shù)字輸出寄存器(Digital Output Register)。#define immoutb_p(val,port) \ // 字節(jié)直接輸出(嵌入?yún)R編語(yǔ)言宏)。__asm__ ("outb %0,%1\n\tjmp 1f\n1:\tjmp 1f\n1:"::"a" ((char) (val)), "i" (port))// 這兩個(gè)定義用于計(jì)算軟驅(qū)的設(shè)備號(hào)。次設(shè)備號(hào) = TYPE*4 + DRIVE。計(jì)算方法參見(jiàn)列表后。#define TYPE(x) ((x)>>2) // 軟驅(qū)類(lèi)型(2--1.2Mb,7--1.44Mb)。#define DRIVE(x) ((x)&0x03) // 軟驅(qū)序號(hào)(0--3 對(duì)應(yīng)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 并不表示對(duì)每次讀錯(cuò)誤嘗試最多8 次 - 有些類(lèi)型* 的錯(cuò)誤將把出錯(cuò)計(jì)數(shù)值乘2,所以我們實(shí)際上在放棄操作之前只需嘗試5-6 遍即可。*/#define MAX_ERRORS 8/** globals used by 'result()'*//* 下面是函數(shù)'result()'使用的全局變量 */// 這些狀態(tài)字節(jié)中各比特位的含義請(qǐng)參見(jiàn)include/linux/fdreg.h 頭文件。#define MAX_REPLIES 7 // FDC 最多返回7 字節(jié)的結(jié)果信息。 static unsigned char reply_buffer[MAX_REPLIES]; // 存放FDC 返回的結(jié)果信息。#define ST0 (reply_buffer[0]) // 返回結(jié)果狀態(tài)字節(jié)0。#define ST1 (reply_buffer[1]) // 返回結(jié)果狀態(tài)字節(jié)1。#define ST2 (reply_buffer[2]) // 返回結(jié)果狀態(tài)字節(jié)2。#define ST3 (reply_buffer[3]) // 返回結(jié)果狀態(tài)字節(jié)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.*//** 下面的軟盤(pán)結(jié)構(gòu)定義了不同的軟盤(pán)類(lèi)型。與minix 不同的是,linux 沒(méi)有* "搜索正確的類(lèi)型"-類(lèi)型,因?yàn)閷?duì)其處理的代碼令人費(fèi)解且怪怪的。本程序* 已經(jīng)讓我遇到了許多的問(wèn)題了。** 對(duì)某些類(lèi)型的軟盤(pán)(例如在1.2MB 驅(qū)動(dòng)器中的360kB 軟盤(pán)等),'stretch'用于* 檢測(cè)磁道是否需要特殊處理。其它參數(shù)應(yīng)該是自明的。*/// 軟盤(pán)參數(shù)有:// size 大小(扇區(qū)數(shù));// sect 每磁道扇區(qū)數(shù);// head 磁頭數(shù);// track 磁道數(shù);// stretch 對(duì)磁道是否要特殊處理(標(biāo)志);// gap 扇區(qū)間隙長(zhǎng)度(字節(jié)數(shù));// rate 數(shù)據(jù)傳輸速率;// spec1 參數(shù)(高4 位步進(jìn)速率,低四位磁頭卸載時(shí)間)。 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。* 參數(shù)spec1 是0xSH,其中S 是步進(jìn)速率(F-1 毫秒,E-2ms,D=3ms 等),* H 是磁頭卸載時(shí)間(1=16ms,2=32ms 等)** spec2 是(HLD<<1 | ND),其中HLD 是磁頭加載時(shí)間(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. *//** 下面是一些全局變量,因?yàn)檫@是將信息傳給中斷程序最簡(jiǎn)單的方式。它們是* 用于當(dāng)前請(qǐng)求的數(shù)據(jù)。*/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;//// 釋放(取消選定的)軟盤(pán)(軟驅(qū))。// 數(shù)字輸出寄存器(DOR)的低2 位用于指定選擇的軟驅(qū)(0-3 對(duì)應(yīng)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()不是從中斷程序中調(diào)用的,所以這里我們可以輕松一下,睡覺(jué)等。* 注意floppy-on()會(huì)嘗試設(shè)置current_DOR 指向所需的驅(qū)動(dòng)器,但當(dāng)同時(shí)使用幾個(gè)* 軟盤(pán)時(shí)不能睡眠:因此此時(shí)只能使用循環(huán)方式。*///// 檢測(cè)指定軟驅(qū)中軟盤(pán)更換情況。如果軟盤(pán)更換了則返回1,否則返回0。intfloppy_change (unsigned int nr){repeat: floppy_on (nr); // 開(kāi)啟指定軟驅(qū)nr(kernel/sched.c,251)。// 如果當(dāng)前選擇的軟驅(qū)不是指定的軟驅(qū)nr,并且已經(jīng)選擇其它了軟驅(qū),則讓當(dāng)前任務(wù)進(jìn)入可中斷// 等待狀態(tài)。 while ((current_DOR & 3) != nr && selected) interruptible_sleep_on (&wait_on_floppy_select);// 如果當(dāng)前沒(méi)有選擇其它軟驅(qū)或者當(dāng)前任務(wù)被喚醒時(shí),當(dāng)前軟驅(qū)仍然不是指定的軟驅(qū)nr,則循環(huán)等待。 if ((current_DOR & 3) != nr) goto repeat;// 取數(shù)字輸入寄存器值,如果最高位(位7)置位,則表示軟盤(pán)已更換,此時(shí)關(guān)閉馬達(dá)并退出返回1。// 否則關(guān)閉馬達(dá)退出返回0。 if (inb (FD_DIR) & 0x80) { floppy_off (nr); return 1; } floppy_off (nr); return 0;}//// 復(fù)制內(nèi)存塊。#define copy_buffer(from,to) \ __asm__( "cld ; rep ; movsl" \ :: "c" (BLOCK_SIZE/4), "S" ((long)(from)), "D" ((long)(to)) \ : "cx", "di", "si")//// 設(shè)置(初始化)軟盤(pán)DMA 通道。static voidsetup_DMA (void){ long addr = (long) CURRENT->buffer; // 當(dāng)前請(qǐng)求項(xiàng)緩沖區(qū)所處內(nèi)存中位置(地址)。 cli ();// 如果緩沖區(qū)處于內(nèi)存1M 以上的地方,則將DMA 緩沖區(qū)設(shè)在臨時(shí)緩沖區(qū)域(tmp_floppy_area 數(shù)組)// (因?yàn)?237A 芯片只能在1M 地址范圍內(nèi)尋址)。如果是寫(xiě)盤(pán)命令,則還需將數(shù)據(jù)復(fù)制到該臨時(shí)區(qū)域。 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 表示允許請(qǐng)求。 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 *//* 輸出命令字節(jié)。我是不知道為什么,但是每個(gè)人(minix,*//* sanches 和canton)都輸出兩次,首先是12 口,然后是11 口 */// 下面嵌入?yún)R編代碼向DMA 控制器端口12 和11 寫(xiě)方式字(讀盤(pán)0x46,寫(xiě)盤(pán)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 寫(xiě)入基/當(dāng)前地址寄存器(端口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 內(nèi)存空間內(nèi)尋址,其高16-19 位地址需放入頁(yè)面寄存器(端口0x81)。 immoutb_p (addr, 0x81);/* low 8 bits of count-1 (1024-1=0x3ff) *//* 計(jì)數(shù)器低8 位(1024-1=0x3ff) */// 向DMA 通道2 寫(xiě)入基/當(dāng)前字節(jié)計(jì)數(shù)器值(端口5)。 immoutb_p (0xff, 5);/* high 8 bits of count-1 *//* 計(jì)數(shù)器高8 位 */// 一次共傳輸1024 字節(jié)(兩個(gè)扇區(qū))。 immoutb_p (3, 5);/* activate DMA 2 *//* 開(kāi)啟DMA 通道2 的請(qǐng)求 */// 復(fù)位對(duì)DMA 通道2 的屏蔽,開(kāi)放DMA2 請(qǐng)求DREQ 信號(hào)。 immoutb_p (0 | 2, 10); sti ();}//// 向軟盤(pán)控制器輸出一個(gè)字節(jié)數(shù)據(jù)(命令或參數(shù))。static voidoutput_byte (char byte){ int counter; unsigned char status; if (reset) return;// 循環(huán)讀取主狀態(tài)控制器FD_STATUS(0x3f4)的狀態(tài)。如果狀態(tài)是STATUS_READY 并且STATUS_DIR=0// (CPU??FDC),則向數(shù)據(jù)端口輸出指定字節(jié)。 for (counter = 0; counter < 10000; counter++) { status = inb_p (FD_STATUS) & (STATUS_READY | STATUS_DIR); if (status == STATUS_READY) { outb (byte, FD_DATA); return; } }// 如果到循環(huán)1 萬(wàn)次結(jié)束還不能發(fā)送,則置復(fù)位標(biāo)志,并打印出錯(cuò)信息。 reset = 1; printk ("Unable to send byte to FDC\n\r");}//// 讀取FDC 執(zhí)行的結(jié)果信息。// 結(jié)果信息最多7 個(gè)字節(jié),存放在reply_buffer[]中。返回讀入的結(jié)果字節(jié)數(shù),若返回值=-1// 表示出錯(cuò)。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;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -