initial version
This commit is contained in:
71
Book-Lite/linux-0.12/kernel/blk_drv/Makefile
Normal file
71
Book-Lite/linux-0.12/kernel/blk_drv/Makefile
Normal file
@@ -0,0 +1,71 @@
|
||||
#
|
||||
# Makefile for the FREAX-kernel block device drivers.
|
||||
#
|
||||
# Note! Dependencies are done automagically by 'make dep', which also
|
||||
# removes any old dependencies. DON'T put your own dependencies here
|
||||
# unless it's something special (ie not a .c file).
|
||||
#
|
||||
|
||||
AR =gar
|
||||
AS =gas
|
||||
LD =gld
|
||||
LDFLAGS =-s -x
|
||||
CC =gcc
|
||||
CFLAGS =-Wall -O -fstrength-reduce -fomit-frame-pointer -fcombine-regs \
|
||||
-finline-functions -mstring-insns -nostdinc -I../../include
|
||||
CPP =gcc -E -nostdinc -I../../include
|
||||
|
||||
.c.s:
|
||||
$(CC) $(CFLAGS) \
|
||||
-S -o $*.s $<
|
||||
.s.o:
|
||||
$(AS) -c -o $*.o $<
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) \
|
||||
-c -o $*.o $<
|
||||
|
||||
OBJS = ll_rw_blk.o floppy.o hd.o ramdisk.o
|
||||
|
||||
blk_drv.a: $(OBJS)
|
||||
$(AR) rcs blk_drv.a $(OBJS)
|
||||
sync
|
||||
|
||||
clean:
|
||||
rm -f core *.o *.a tmp_make
|
||||
for i in *.c;do rm -f `basename $$i .c`.s;done
|
||||
|
||||
dep:
|
||||
sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
|
||||
(for i in *.c;do echo -n `echo $$i | sed 's,\.c,\.s,'`" "; \
|
||||
$(CPP) -M $$i;done) >> tmp_make
|
||||
cp tmp_make Makefile
|
||||
|
||||
### Dependencies:
|
||||
floppy.s floppy.o : floppy.c ../../include/linux/sched.h ../../include/linux/head.h \
|
||||
../../include/linux/fs.h ../../include/sys/types.h ../../include/linux/mm.h \
|
||||
../../include/linux/kernel.h ../../include/signal.h \
|
||||
../../include/sys/param.h ../../include/sys/time.h ../../include/time.h \
|
||||
../../include/sys/resource.h ../../include/linux/fdreg.h \
|
||||
../../include/asm/system.h ../../include/asm/io.h \
|
||||
../../include/asm/segment.h blk.h
|
||||
hd.s hd.o : hd.c ../../include/linux/config.h ../../include/linux/sched.h \
|
||||
../../include/linux/head.h ../../include/linux/fs.h \
|
||||
../../include/sys/types.h ../../include/linux/mm.h \
|
||||
../../include/linux/kernel.h ../../include/signal.h \
|
||||
../../include/sys/param.h ../../include/sys/time.h ../../include/time.h \
|
||||
../../include/sys/resource.h ../../include/linux/hdreg.h \
|
||||
../../include/asm/system.h ../../include/asm/io.h \
|
||||
../../include/asm/segment.h blk.h
|
||||
ll_rw_blk.s ll_rw_blk.o : ll_rw_blk.c ../../include/errno.h ../../include/linux/sched.h \
|
||||
../../include/linux/head.h ../../include/linux/fs.h \
|
||||
../../include/sys/types.h ../../include/linux/mm.h \
|
||||
../../include/linux/kernel.h ../../include/signal.h \
|
||||
../../include/sys/param.h ../../include/sys/time.h ../../include/time.h \
|
||||
../../include/sys/resource.h ../../include/asm/system.h blk.h
|
||||
ramdisk.s ramdisk.o : ramdisk.c ../../include/string.h ../../include/linux/config.h \
|
||||
../../include/linux/sched.h ../../include/linux/head.h \
|
||||
../../include/linux/fs.h ../../include/sys/types.h ../../include/linux/mm.h \
|
||||
../../include/linux/kernel.h ../../include/signal.h \
|
||||
../../include/sys/param.h ../../include/sys/time.h ../../include/time.h \
|
||||
../../include/sys/resource.h ../../include/asm/system.h \
|
||||
../../include/asm/segment.h ../../include/asm/memory.h blk.h
|
||||
164
Book-Lite/linux-0.12/kernel/blk_drv/blk.h
Normal file
164
Book-Lite/linux-0.12/kernel/blk_drv/blk.h
Normal file
@@ -0,0 +1,164 @@
|
||||
#ifndef _BLK_H
|
||||
#define _BLK_H
|
||||
|
||||
#define NR_BLK_DEV 7
|
||||
/*
|
||||
* NR_REQUEST is the number of entries in the request-queue.
|
||||
* NOTE that writes may use only the low 2/3 of these: reads
|
||||
* take precedence.
|
||||
*
|
||||
* 32 seems to be a reasonable number: enough to get some benefit
|
||||
* from the elevator-mechanism, but not so much as to lock a lot of
|
||||
* buffers when they are in the queue. 64 seems to be too many (easily
|
||||
* long pauses in reading when heavy writing/syncing is going on)
|
||||
*/
|
||||
#define NR_REQUEST 32
|
||||
|
||||
/*
|
||||
* Ok, this is an expanded form so that we can use the same
|
||||
* request for paging requests when that is implemented. In
|
||||
* paging, 'bh' is NULL, and 'waiting' is used to wait for
|
||||
* read/write completion.
|
||||
*/
|
||||
struct request {
|
||||
int dev; /* -1 if no request */
|
||||
int cmd; /* READ or WRITE */
|
||||
int errors;
|
||||
unsigned long sector;
|
||||
unsigned long nr_sectors;
|
||||
char * buffer;
|
||||
struct task_struct * waiting;
|
||||
struct buffer_head * bh;
|
||||
struct request * next;
|
||||
};
|
||||
|
||||
/*
|
||||
* This is used in the elevator algorithm: Note that
|
||||
* reads always go before writes. This is natural: reads
|
||||
* are much more time-critical than writes.
|
||||
*/
|
||||
#define IN_ORDER(s1,s2) \
|
||||
((s1)->cmd<(s2)->cmd || (s1)->cmd==(s2)->cmd && \
|
||||
((s1)->dev < (s2)->dev || ((s1)->dev == (s2)->dev && \
|
||||
(s1)->sector < (s2)->sector)))
|
||||
|
||||
struct blk_dev_struct {
|
||||
void (*request_fn)(void);
|
||||
struct request * current_request;
|
||||
};
|
||||
|
||||
extern struct blk_dev_struct blk_dev[NR_BLK_DEV];
|
||||
extern struct request request[NR_REQUEST];
|
||||
extern struct task_struct * wait_for_request;
|
||||
|
||||
extern int * blk_size[NR_BLK_DEV];
|
||||
|
||||
#ifdef MAJOR_NR
|
||||
|
||||
/*
|
||||
* Add entries as needed. Currently the only block devices
|
||||
* supported are hard-disks and floppies.
|
||||
*/
|
||||
|
||||
#if (MAJOR_NR == 1)
|
||||
/* ram disk */
|
||||
#define DEVICE_NAME "ramdisk"
|
||||
#define DEVICE_REQUEST do_rd_request
|
||||
#define DEVICE_NR(device) ((device) & 7)
|
||||
#define DEVICE_ON(device)
|
||||
#define DEVICE_OFF(device)
|
||||
|
||||
#elif (MAJOR_NR == 2)
|
||||
/* floppy */
|
||||
#define DEVICE_NAME "floppy"
|
||||
#define DEVICE_INTR do_floppy
|
||||
#define DEVICE_REQUEST do_fd_request
|
||||
#define DEVICE_NR(device) ((device) & 3)
|
||||
#define DEVICE_ON(device) floppy_on(DEVICE_NR(device))
|
||||
#define DEVICE_OFF(device) floppy_off(DEVICE_NR(device))
|
||||
|
||||
#elif (MAJOR_NR == 3)
|
||||
/* harddisk */
|
||||
#define DEVICE_NAME "harddisk"
|
||||
#define DEVICE_INTR do_hd
|
||||
#define DEVICE_TIMEOUT hd_timeout
|
||||
#define DEVICE_REQUEST do_hd_request
|
||||
#define DEVICE_NR(device) (MINOR(device)/5)
|
||||
#define DEVICE_ON(device)
|
||||
#define DEVICE_OFF(device)
|
||||
|
||||
#elif
|
||||
/* unknown blk device */
|
||||
#error "unknown blk device"
|
||||
|
||||
#endif
|
||||
|
||||
#define CURRENT (blk_dev[MAJOR_NR].current_request)
|
||||
#define CURRENT_DEV DEVICE_NR(CURRENT->dev)
|
||||
|
||||
#ifdef DEVICE_INTR
|
||||
void (*DEVICE_INTR)(void) = NULL;
|
||||
#endif
|
||||
#ifdef DEVICE_TIMEOUT
|
||||
int DEVICE_TIMEOUT = 0;
|
||||
#define SET_INTR(x) (DEVICE_INTR = (x),DEVICE_TIMEOUT = 200)
|
||||
#else
|
||||
#define SET_INTR(x) (DEVICE_INTR = (x))
|
||||
#endif
|
||||
static void (DEVICE_REQUEST)(void);
|
||||
|
||||
extern inline void unlock_buffer(struct buffer_head * bh)
|
||||
{
|
||||
if (!bh->b_lock)
|
||||
printk(DEVICE_NAME ": free buffer being unlocked\n");
|
||||
bh->b_lock=0;
|
||||
wake_up(&bh->b_wait);
|
||||
}
|
||||
|
||||
extern inline void end_request(int uptodate)
|
||||
{
|
||||
DEVICE_OFF(CURRENT->dev);
|
||||
if (CURRENT->bh) {
|
||||
CURRENT->bh->b_uptodate = uptodate;
|
||||
unlock_buffer(CURRENT->bh);
|
||||
}
|
||||
if (!uptodate) {
|
||||
printk(DEVICE_NAME " I/O error\n\r");
|
||||
printk("dev %04x, block %d\n\r",CURRENT->dev,
|
||||
CURRENT->bh->b_blocknr);
|
||||
}
|
||||
wake_up(&CURRENT->waiting);
|
||||
wake_up(&wait_for_request);
|
||||
CURRENT->dev = -1;
|
||||
CURRENT = CURRENT->next;
|
||||
}
|
||||
|
||||
#ifdef DEVICE_TIMEOUT
|
||||
#define CLEAR_DEVICE_TIMEOUT DEVICE_TIMEOUT = 0;
|
||||
#else
|
||||
#define CLEAR_DEVICE_TIMEOUT
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_INTR
|
||||
#define CLEAR_DEVICE_INTR DEVICE_INTR = 0;
|
||||
#else
|
||||
#define CLEAR_DEVICE_INTR
|
||||
#endif
|
||||
|
||||
#define INIT_REQUEST \
|
||||
repeat: \
|
||||
if (!CURRENT) {\
|
||||
CLEAR_DEVICE_INTR \
|
||||
CLEAR_DEVICE_TIMEOUT \
|
||||
return; \
|
||||
} \
|
||||
if (MAJOR(CURRENT->dev) != MAJOR_NR) \
|
||||
panic(DEVICE_NAME ": request list destroyed"); \
|
||||
if (CURRENT->bh) { \
|
||||
if (!CURRENT->bh->b_lock) \
|
||||
panic(DEVICE_NAME ": block not locked"); \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
475
Book-Lite/linux-0.12/kernel/blk_drv/floppy.c
Normal file
475
Book-Lite/linux-0.12/kernel/blk_drv/floppy.c
Normal file
@@ -0,0 +1,475 @@
|
||||
/*
|
||||
* 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) \
|
||||
__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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)));
|
||||
/* 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
setup_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)
|
||||
{
|
||||
/* 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)
|
||||
{
|
||||
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)
|
||||
seek = 1;
|
||||
current_drive = CURRENT_DEV;
|
||||
block = CURRENT->sector;
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
384
Book-Lite/linux-0.12/kernel/blk_drv/hd.c
Normal file
384
Book-Lite/linux-0.12/kernel/blk_drv/hd.c
Normal file
@@ -0,0 +1,384 @@
|
||||
/*
|
||||
* linux/kernel/hd.c
|
||||
*
|
||||
* (C) 1991 Linus Torvalds
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is the low-level hd interrupt support. It traverses the
|
||||
* request-list, using interrupts to jump between functions. As
|
||||
* all the functions are called within interrupts, we may not
|
||||
* sleep. Special care is recommended.
|
||||
*
|
||||
* modified by Drew Eckhardt to check nr of hd's from the CMOS.
|
||||
*/
|
||||
|
||||
#include <linux/config.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/hdreg.h>
|
||||
#include <asm/system.h>
|
||||
#include <asm/io.h>
|
||||
#include <asm/segment.h>
|
||||
|
||||
#define MAJOR_NR 3
|
||||
#include "blk.h"
|
||||
|
||||
#define CMOS_READ(addr) ({ \
|
||||
outb_p(0x80|addr,0x70); \
|
||||
inb_p(0x71); \
|
||||
})
|
||||
|
||||
/* Max read/write errors/sector */
|
||||
#define MAX_ERRORS 7
|
||||
#define MAX_HD 2
|
||||
|
||||
static void recal_intr(void);
|
||||
static void bad_rw_intr(void);
|
||||
|
||||
static int recalibrate = 0;
|
||||
static int reset = 0;
|
||||
|
||||
/*
|
||||
* This struct defines the HD's and their types.
|
||||
*/
|
||||
struct hd_i_struct {
|
||||
int head,sect,cyl,wpcom,lzone,ctl;
|
||||
};
|
||||
#ifdef HD_TYPE
|
||||
struct hd_i_struct hd_info[] = { HD_TYPE };
|
||||
#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct)))
|
||||
#else
|
||||
struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
|
||||
static int NR_HD = 0;
|
||||
#endif
|
||||
|
||||
static struct hd_struct {
|
||||
long start_sect;
|
||||
long nr_sects;
|
||||
} hd[5*MAX_HD]={{0,0},};
|
||||
|
||||
static int hd_sizes[5*MAX_HD] = {0, };
|
||||
|
||||
#define port_read(port,buf,nr) \
|
||||
__asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di")
|
||||
|
||||
#define port_write(port,buf,nr) \
|
||||
__asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si")
|
||||
|
||||
extern void hd_interrupt(void);
|
||||
extern void rd_load(void);
|
||||
|
||||
/* This may be used only once, enforced by 'static int callable' */
|
||||
int sys_setup(void * BIOS)
|
||||
{
|
||||
static int callable = 1;
|
||||
int i,drive;
|
||||
unsigned char cmos_disks;
|
||||
struct partition *p;
|
||||
struct buffer_head * bh;
|
||||
|
||||
if (!callable)
|
||||
return -1;
|
||||
callable = 0;
|
||||
#ifndef HD_TYPE
|
||||
for (drive=0 ; drive<2 ; drive++) {
|
||||
hd_info[drive].cyl = *(unsigned short *) BIOS;
|
||||
hd_info[drive].head = *(unsigned char *) (2+BIOS);
|
||||
hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);
|
||||
hd_info[drive].ctl = *(unsigned char *) (8+BIOS);
|
||||
hd_info[drive].lzone = *(unsigned short *) (12+BIOS);
|
||||
hd_info[drive].sect = *(unsigned char *) (14+BIOS);
|
||||
BIOS += 16;
|
||||
}
|
||||
if (hd_info[1].cyl)
|
||||
NR_HD=2;
|
||||
else
|
||||
NR_HD=1;
|
||||
#endif
|
||||
for (i=0 ; i<NR_HD ; i++) {
|
||||
hd[i*5].start_sect = 0;
|
||||
hd[i*5].nr_sects = hd_info[i].head*
|
||||
hd_info[i].sect*hd_info[i].cyl;
|
||||
}
|
||||
|
||||
/*
|
||||
We querry CMOS about hard disks : it could be that
|
||||
we have a SCSI/ESDI/etc controller that is BIOS
|
||||
compatable with ST-506, and thus showing up in our
|
||||
BIOS table, but not register compatable, and therefore
|
||||
not present in CMOS.
|
||||
|
||||
Furthurmore, we will assume that our ST-506 drives
|
||||
<if any> are the primary drives in the system, and
|
||||
the ones reflected as drive 1 or 2.
|
||||
|
||||
The first drive is stored in the high nibble of CMOS
|
||||
byte 0x12, the second in the low nibble. This will be
|
||||
either a 4 bit drive type or 0xf indicating use byte 0x19
|
||||
for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
|
||||
|
||||
Needless to say, a non-zero value means we have
|
||||
an AT controller hard disk for that drive.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
if ((cmos_disks = CMOS_READ(0x12)) & 0xf0)
|
||||
if (cmos_disks & 0x0f)
|
||||
NR_HD = 2;
|
||||
else
|
||||
NR_HD = 1;
|
||||
else
|
||||
NR_HD = 0;
|
||||
for (i = NR_HD ; i < 2 ; i++) {
|
||||
hd[i*5].start_sect = 0;
|
||||
hd[i*5].nr_sects = 0;
|
||||
}
|
||||
for (drive=0 ; drive<NR_HD ; drive++) {
|
||||
if (!(bh = bread(0x300 + drive*5,0))) {
|
||||
printk("Unable to read partition table of drive %d\n\r",
|
||||
drive);
|
||||
panic("");
|
||||
}
|
||||
if (bh->b_data[510] != 0x55 || (unsigned char)
|
||||
bh->b_data[511] != 0xAA) {
|
||||
printk("Bad partition table on drive %d\n\r",drive);
|
||||
panic("");
|
||||
}
|
||||
p = 0x1BE + (void *)bh->b_data;
|
||||
for (i=1;i<5;i++,p++) {
|
||||
hd[i+5*drive].start_sect = p->start_sect;
|
||||
hd[i+5*drive].nr_sects = p->nr_sects;
|
||||
}
|
||||
brelse(bh);
|
||||
}
|
||||
for (i=0 ; i<5*MAX_HD ; i++)
|
||||
hd_sizes[i] = hd[i].nr_sects>>1 ;
|
||||
blk_size[MAJOR_NR] = hd_sizes;
|
||||
if (NR_HD)
|
||||
printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":"");
|
||||
rd_load();
|
||||
init_swapping();
|
||||
mount_root();
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int controller_ready(void)
|
||||
{
|
||||
int retries = 100000;
|
||||
|
||||
while (--retries && (inb_p(HD_STATUS)&0xc0)!=0x40);
|
||||
return (retries);
|
||||
}
|
||||
|
||||
static int win_result(void)
|
||||
{
|
||||
int i=inb_p(HD_STATUS);
|
||||
|
||||
if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT))
|
||||
== (READY_STAT | SEEK_STAT))
|
||||
return(0); /* ok */
|
||||
if (i&1) i=inb(HD_ERROR);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
|
||||
unsigned int head,unsigned int cyl,unsigned int cmd,
|
||||
void (*intr_addr)(void))
|
||||
{
|
||||
register int port asm("dx");
|
||||
|
||||
if (drive>1 || head>15)
|
||||
panic("Trying to write bad sector");
|
||||
if (!controller_ready())
|
||||
panic("HD controller not ready");
|
||||
SET_INTR(intr_addr);
|
||||
outb_p(hd_info[drive].ctl,HD_CMD);
|
||||
port=HD_DATA;
|
||||
outb_p(hd_info[drive].wpcom>>2,++port);
|
||||
outb_p(nsect,++port);
|
||||
outb_p(sect,++port);
|
||||
outb_p(cyl,++port);
|
||||
outb_p(cyl>>8,++port);
|
||||
outb_p(0xA0|(drive<<4)|head,++port);
|
||||
outb(cmd,++port);
|
||||
}
|
||||
|
||||
static int drive_busy(void)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned char c;
|
||||
|
||||
for (i = 0; i < 50000; i++) {
|
||||
c = inb_p(HD_STATUS);
|
||||
c &= (BUSY_STAT | READY_STAT | SEEK_STAT);
|
||||
if (c == (READY_STAT | SEEK_STAT))
|
||||
return 0;
|
||||
}
|
||||
printk("HD controller times out\n\r");
|
||||
return(1);
|
||||
}
|
||||
|
||||
static void reset_controller(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
outb(4,HD_CMD);
|
||||
for(i = 0; i < 1000; i++) nop();
|
||||
outb(hd_info[0].ctl & 0x0f ,HD_CMD);
|
||||
if (drive_busy())
|
||||
printk("HD-controller still busy\n\r");
|
||||
if ((i = inb(HD_ERROR)) != 1)
|
||||
printk("HD-controller reset failed: %02x\n\r",i);
|
||||
}
|
||||
|
||||
static void reset_hd(void)
|
||||
{
|
||||
static int i;
|
||||
|
||||
repeat:
|
||||
if (reset) {
|
||||
reset = 0;
|
||||
i = -1;
|
||||
reset_controller();
|
||||
} else if (win_result()) {
|
||||
bad_rw_intr();
|
||||
if (reset)
|
||||
goto repeat;
|
||||
}
|
||||
i++;
|
||||
if (i < NR_HD) {
|
||||
hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
|
||||
hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
|
||||
} else
|
||||
do_hd_request();
|
||||
}
|
||||
|
||||
void unexpected_hd_interrupt(void)
|
||||
{
|
||||
printk("Unexpected HD interrupt\n\r");
|
||||
reset = 1;
|
||||
do_hd_request();
|
||||
}
|
||||
|
||||
static void bad_rw_intr(void)
|
||||
{
|
||||
if (++CURRENT->errors >= MAX_ERRORS)
|
||||
end_request(0);
|
||||
if (CURRENT->errors > MAX_ERRORS/2)
|
||||
reset = 1;
|
||||
}
|
||||
|
||||
static void read_intr(void)
|
||||
{
|
||||
if (win_result()) {
|
||||
bad_rw_intr();
|
||||
do_hd_request();
|
||||
return;
|
||||
}
|
||||
port_read(HD_DATA,CURRENT->buffer,256);
|
||||
CURRENT->errors = 0;
|
||||
CURRENT->buffer += 512;
|
||||
CURRENT->sector++;
|
||||
if (--CURRENT->nr_sectors) {
|
||||
SET_INTR(&read_intr);
|
||||
return;
|
||||
}
|
||||
end_request(1);
|
||||
do_hd_request();
|
||||
}
|
||||
|
||||
static void write_intr(void)
|
||||
{
|
||||
if (win_result()) {
|
||||
bad_rw_intr();
|
||||
do_hd_request();
|
||||
return;
|
||||
}
|
||||
if (--CURRENT->nr_sectors) {
|
||||
CURRENT->sector++;
|
||||
CURRENT->buffer += 512;
|
||||
SET_INTR(&write_intr);
|
||||
port_write(HD_DATA,CURRENT->buffer,256);
|
||||
return;
|
||||
}
|
||||
end_request(1);
|
||||
do_hd_request();
|
||||
}
|
||||
|
||||
static void recal_intr(void)
|
||||
{
|
||||
if (win_result())
|
||||
bad_rw_intr();
|
||||
do_hd_request();
|
||||
}
|
||||
|
||||
void hd_times_out(void)
|
||||
{
|
||||
if (!CURRENT)
|
||||
return;
|
||||
printk("HD timeout");
|
||||
if (++CURRENT->errors >= MAX_ERRORS)
|
||||
end_request(0);
|
||||
SET_INTR(NULL);
|
||||
reset = 1;
|
||||
do_hd_request();
|
||||
}
|
||||
|
||||
void do_hd_request(void)
|
||||
{
|
||||
int i,r;
|
||||
unsigned int block,dev;
|
||||
unsigned int sec,head,cyl;
|
||||
unsigned int nsect;
|
||||
|
||||
INIT_REQUEST;
|
||||
dev = MINOR(CURRENT->dev);
|
||||
block = CURRENT->sector;
|
||||
if (dev >= 5*NR_HD || block+2 > hd[dev].nr_sects) {
|
||||
end_request(0);
|
||||
goto repeat;
|
||||
}
|
||||
block += hd[dev].start_sect;
|
||||
dev /= 5;
|
||||
__asm__("divl %4":"=a" (block),"=d" (sec):"0" (block),"1" (0),
|
||||
"r" (hd_info[dev].sect));
|
||||
__asm__("divl %4":"=a" (cyl),"=d" (head):"0" (block),"1" (0),
|
||||
"r" (hd_info[dev].head));
|
||||
sec++;
|
||||
nsect = CURRENT->nr_sectors;
|
||||
if (reset) {
|
||||
recalibrate = 1;
|
||||
reset_hd();
|
||||
return;
|
||||
}
|
||||
if (recalibrate) {
|
||||
recalibrate = 0;
|
||||
hd_out(dev,hd_info[CURRENT_DEV].sect,0,0,0,
|
||||
WIN_RESTORE,&recal_intr);
|
||||
return;
|
||||
}
|
||||
if (CURRENT->cmd == WRITE) {
|
||||
hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
|
||||
for(i=0 ; i<10000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++)
|
||||
/* nothing */ ;
|
||||
if (!r) {
|
||||
bad_rw_intr();
|
||||
goto repeat;
|
||||
}
|
||||
port_write(HD_DATA,CURRENT->buffer,256);
|
||||
} else if (CURRENT->cmd == READ) {
|
||||
hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);
|
||||
} else
|
||||
panic("unknown hd-command");
|
||||
}
|
||||
|
||||
void hd_init(void)
|
||||
{
|
||||
blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
|
||||
set_intr_gate(0x2E,&hd_interrupt);
|
||||
outb_p(inb_p(0x21)&0xfb,0x21);
|
||||
outb(inb_p(0xA1)&0xbf,0xA1);
|
||||
}
|
||||
218
Book-Lite/linux-0.12/kernel/blk_drv/ll_rw_blk.c
Normal file
218
Book-Lite/linux-0.12/kernel/blk_drv/ll_rw_blk.c
Normal file
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* linux/kernel/blk_dev/ll_rw.c
|
||||
*
|
||||
* (C) 1991 Linus Torvalds
|
||||
*/
|
||||
|
||||
/*
|
||||
* This handles all read/write requests to block devices
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <asm/system.h>
|
||||
|
||||
#include "blk.h"
|
||||
|
||||
/*
|
||||
* The request-struct contains all necessary data
|
||||
* to load a nr of sectors into memory
|
||||
*/
|
||||
struct request request[NR_REQUEST];
|
||||
|
||||
/*
|
||||
* used to wait on when there are no free requests
|
||||
*/
|
||||
struct task_struct * wait_for_request = NULL;
|
||||
|
||||
/* blk_dev_struct is:
|
||||
* do_request-address
|
||||
* next-request
|
||||
*/
|
||||
struct blk_dev_struct blk_dev[NR_BLK_DEV] = {
|
||||
{ NULL, NULL }, /* no_dev */
|
||||
{ NULL, NULL }, /* dev mem */
|
||||
{ NULL, NULL }, /* dev fd */
|
||||
{ NULL, NULL }, /* dev hd */
|
||||
{ NULL, NULL }, /* dev ttyx */
|
||||
{ NULL, NULL }, /* dev tty */
|
||||
{ NULL, NULL } /* dev lp */
|
||||
};
|
||||
|
||||
/*
|
||||
* blk_size contains the size of all block-devices:
|
||||
*
|
||||
* blk_size[MAJOR][MINOR]
|
||||
*
|
||||
* if (!blk_size[MAJOR]) then no minor size checking is done.
|
||||
*/
|
||||
int * blk_size[NR_BLK_DEV] = { NULL, NULL, };
|
||||
|
||||
static inline void lock_buffer(struct buffer_head * bh)
|
||||
{
|
||||
cli();
|
||||
while (bh->b_lock)
|
||||
sleep_on(&bh->b_wait);
|
||||
bh->b_lock=1;
|
||||
sti();
|
||||
}
|
||||
|
||||
static inline void unlock_buffer(struct buffer_head * bh)
|
||||
{
|
||||
if (!bh->b_lock)
|
||||
printk("ll_rw_block.c: buffer not locked\n\r");
|
||||
bh->b_lock = 0;
|
||||
wake_up(&bh->b_wait);
|
||||
}
|
||||
|
||||
/*
|
||||
* add-request adds a request to the linked list.
|
||||
* It disables interrupts so that it can muck with the
|
||||
* request-lists in peace.
|
||||
*
|
||||
* Note that swapping requests always go before other requests,
|
||||
* and are done in the order they appear.
|
||||
*/
|
||||
static void add_request(struct blk_dev_struct * dev, struct request * req)
|
||||
{
|
||||
struct request * tmp;
|
||||
|
||||
req->next = NULL;
|
||||
cli();
|
||||
if (req->bh)
|
||||
req->bh->b_dirt = 0;
|
||||
if (!(tmp = dev->current_request)) {
|
||||
dev->current_request = req;
|
||||
sti();
|
||||
(dev->request_fn)();
|
||||
return;
|
||||
}
|
||||
for ( ; tmp->next ; tmp=tmp->next) {
|
||||
if (!req->bh)
|
||||
if (tmp->next->bh)
|
||||
break;
|
||||
else
|
||||
continue;
|
||||
if ((IN_ORDER(tmp,req) ||
|
||||
!IN_ORDER(tmp,tmp->next)) &&
|
||||
IN_ORDER(req,tmp->next))
|
||||
break;
|
||||
}
|
||||
req->next=tmp->next;
|
||||
tmp->next=req;
|
||||
sti();
|
||||
}
|
||||
|
||||
static void make_request(int major,int rw, struct buffer_head * bh)
|
||||
{
|
||||
struct request * req;
|
||||
int rw_ahead;
|
||||
|
||||
/* WRITEA/READA is special case - it is not really needed, so if the */
|
||||
/* buffer is locked, we just forget about it, else it's a normal read */
|
||||
if (rw_ahead = (rw == READA || rw == WRITEA)) {
|
||||
if (bh->b_lock)
|
||||
return;
|
||||
if (rw == READA)
|
||||
rw = READ;
|
||||
else
|
||||
rw = WRITE;
|
||||
}
|
||||
if (rw!=READ && rw!=WRITE)
|
||||
panic("Bad block dev command, must be R/W/RA/WA");
|
||||
lock_buffer(bh);
|
||||
if ((rw == WRITE && !bh->b_dirt) || (rw == READ && bh->b_uptodate)) {
|
||||
unlock_buffer(bh);
|
||||
return;
|
||||
}
|
||||
repeat:
|
||||
/* we don't allow the write-requests to fill up the queue completely:
|
||||
* we want some room for reads: they take precedence. The last third
|
||||
* of the requests are only for reads.
|
||||
*/
|
||||
if (rw == READ)
|
||||
req = request+NR_REQUEST;
|
||||
else
|
||||
req = request+((NR_REQUEST*2)/3);
|
||||
/* find an empty request */
|
||||
while (--req >= request)
|
||||
if (req->dev<0)
|
||||
break;
|
||||
/* if none found, sleep on new requests: check for rw_ahead */
|
||||
if (req < request) {
|
||||
if (rw_ahead) {
|
||||
unlock_buffer(bh);
|
||||
return;
|
||||
}
|
||||
sleep_on(&wait_for_request);
|
||||
goto repeat;
|
||||
}
|
||||
/* fill up the request-info, and add it to the queue */
|
||||
req->dev = bh->b_dev;
|
||||
req->cmd = rw;
|
||||
req->errors=0;
|
||||
req->sector = bh->b_blocknr<<1;
|
||||
req->nr_sectors = 2;
|
||||
req->buffer = bh->b_data;
|
||||
req->waiting = NULL;
|
||||
req->bh = bh;
|
||||
req->next = NULL;
|
||||
add_request(major+blk_dev,req);
|
||||
}
|
||||
|
||||
void ll_rw_page(int rw, int dev, int page, char * buffer)
|
||||
{
|
||||
struct request * req;
|
||||
unsigned int major = MAJOR(dev);
|
||||
|
||||
if (major >= NR_BLK_DEV || !(blk_dev[major].request_fn)) {
|
||||
printk("Trying to read nonexistent block-device\n\r");
|
||||
return;
|
||||
}
|
||||
if (rw!=READ && rw!=WRITE)
|
||||
panic("Bad block dev command, must be R/W");
|
||||
repeat:
|
||||
req = request+NR_REQUEST;
|
||||
while (--req >= request)
|
||||
if (req->dev<0)
|
||||
break;
|
||||
if (req < request) {
|
||||
sleep_on(&wait_for_request);
|
||||
goto repeat;
|
||||
}
|
||||
/* fill up the request-info, and add it to the queue */
|
||||
req->dev = dev;
|
||||
req->cmd = rw;
|
||||
req->errors = 0;
|
||||
req->sector = page<<3;
|
||||
req->nr_sectors = 8;
|
||||
req->buffer = buffer;
|
||||
req->waiting = current;
|
||||
req->bh = NULL;
|
||||
req->next = NULL;
|
||||
current->state = TASK_UNINTERRUPTIBLE;
|
||||
add_request(major+blk_dev,req);
|
||||
schedule();
|
||||
}
|
||||
|
||||
void ll_rw_block(int rw, struct buffer_head * bh)
|
||||
{
|
||||
unsigned int major;
|
||||
|
||||
if ((major=MAJOR(bh->b_dev)) >= NR_BLK_DEV ||
|
||||
!(blk_dev[major].request_fn)) {
|
||||
printk("Trying to read nonexistent block-device\n\r");
|
||||
return;
|
||||
}
|
||||
make_request(major,rw,bh);
|
||||
}
|
||||
|
||||
void blk_dev_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0 ; i<NR_REQUEST ; i++) {
|
||||
request[i].dev = -1;
|
||||
request[i].next = NULL;
|
||||
}
|
||||
}
|
||||
125
Book-Lite/linux-0.12/kernel/blk_drv/ramdisk.c
Normal file
125
Book-Lite/linux-0.12/kernel/blk_drv/ramdisk.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* linux/kernel/blk_drv/ramdisk.c
|
||||
*
|
||||
* Written by Theodore Ts'o, 12/2/91
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <linux/config.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <asm/system.h>
|
||||
#include <asm/segment.h>
|
||||
#include <asm/memory.h>
|
||||
|
||||
#define MAJOR_NR 1
|
||||
#include "blk.h"
|
||||
|
||||
char *rd_start;
|
||||
int rd_length = 0;
|
||||
|
||||
void do_rd_request(void)
|
||||
{
|
||||
int len;
|
||||
char *addr;
|
||||
|
||||
INIT_REQUEST;
|
||||
addr = rd_start + (CURRENT->sector << 9);
|
||||
len = CURRENT->nr_sectors << 9;
|
||||
if ((MINOR(CURRENT->dev) != 1) || (addr+len > rd_start+rd_length)) {
|
||||
end_request(0);
|
||||
goto repeat;
|
||||
}
|
||||
if (CURRENT-> cmd == WRITE) {
|
||||
(void ) memcpy(addr,
|
||||
CURRENT->buffer,
|
||||
len);
|
||||
} else if (CURRENT->cmd == READ) {
|
||||
(void) memcpy(CURRENT->buffer,
|
||||
addr,
|
||||
len);
|
||||
} else
|
||||
panic("unknown ramdisk-command");
|
||||
end_request(1);
|
||||
goto repeat;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns amount of memory which needs to be reserved.
|
||||
*/
|
||||
long rd_init(long mem_start, int length)
|
||||
{
|
||||
int i;
|
||||
char *cp;
|
||||
|
||||
blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
|
||||
rd_start = (char *) mem_start;
|
||||
rd_length = length;
|
||||
cp = rd_start;
|
||||
for (i=0; i < length; i++)
|
||||
*cp++ = '\0';
|
||||
return(length);
|
||||
}
|
||||
|
||||
/*
|
||||
* If the root device is the ram disk, try to load it.
|
||||
* In order to do this, the root device is originally set to the
|
||||
* floppy, and we later change it to be ram disk.
|
||||
*/
|
||||
void rd_load(void)
|
||||
{
|
||||
struct buffer_head *bh;
|
||||
struct super_block s;
|
||||
int block = 256; /* Start at block 256 */
|
||||
int i = 1;
|
||||
int nblocks;
|
||||
char *cp; /* Move pointer */
|
||||
|
||||
if (!rd_length)
|
||||
return;
|
||||
printk("Ram disk: %d bytes, starting at 0x%x\n", rd_length,
|
||||
(int) rd_start);
|
||||
if (MAJOR(ROOT_DEV) != 2)
|
||||
return;
|
||||
bh = breada(ROOT_DEV,block+1,block,block+2,-1);
|
||||
if (!bh) {
|
||||
printk("Disk error while looking for ramdisk!\n");
|
||||
return;
|
||||
}
|
||||
*((struct d_super_block *) &s) = *((struct d_super_block *) bh->b_data);
|
||||
brelse(bh);
|
||||
if (s.s_magic != SUPER_MAGIC)
|
||||
/* No ram disk image present, assume normal floppy boot */
|
||||
return;
|
||||
nblocks = s.s_nzones << s.s_log_zone_size;
|
||||
if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
|
||||
printk("Ram disk image too big! (%d blocks, %d avail)\n",
|
||||
nblocks, rd_length >> BLOCK_SIZE_BITS);
|
||||
return;
|
||||
}
|
||||
printk("Loading %d bytes into ram disk... 0000k",
|
||||
nblocks << BLOCK_SIZE_BITS);
|
||||
cp = rd_start;
|
||||
while (nblocks) {
|
||||
if (nblocks > 2)
|
||||
bh = breada(ROOT_DEV, block, block+1, block+2, -1);
|
||||
else
|
||||
bh = bread(ROOT_DEV, block);
|
||||
if (!bh) {
|
||||
printk("I/O error on block %d, aborting load\n",
|
||||
block);
|
||||
return;
|
||||
}
|
||||
(void) memcpy(cp, bh->b_data, BLOCK_SIZE);
|
||||
brelse(bh);
|
||||
printk("\010\010\010\010\010%4dk",i);
|
||||
cp += BLOCK_SIZE;
|
||||
block++;
|
||||
nblocks--;
|
||||
i++;
|
||||
}
|
||||
printk("\010\010\010\010\010done \n");
|
||||
ROOT_DEV=0x0101;
|
||||
}
|
||||
Reference in New Issue
Block a user