--- linux/kernel/blk_drv/floppy.c 2018/04/24 18:02:30 1.1.1.2 +++ linux/kernel/blk_drv/floppy.c 2018/04/24 18:05:39 1.1.1.5 @@ -25,9 +25,11 @@ * 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. + */ + +/* + * 28.02.92 - made track-buffering routines, based on the routines written + * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus. */ #include @@ -41,6 +43,8 @@ #define MAJOR_NR 2 #include "blk.h" +unsigned int changed_floppies = 0; + static int recalibrate = 0; static int reset = 0; static int seek = 0; @@ -53,11 +57,11 @@ __asm__("outb %0,%1\n\tjmp 1f\n1:\tjmp 1 #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. + * Note that MAX_ERRORS=X doesn't imply that we retry every bad read + * max X times - some types of errors increase the errorcount by 2 or + * even 3, so we might actually retry only X/2 times before giving up. */ -#define MAX_ERRORS 8 +#define MAX_ERRORS 12 /* * globals used by 'result()' @@ -79,10 +83,12 @@ static unsigned char reply_buffer[MAX_RE * types (ie 360kB diskette in 1.2MB drive etc). Others should * be self-explanatory. */ -static struct floppy_struct { +struct floppy_struct { unsigned int size, sect, head, track, stretch; unsigned char gap,rate,spec1; -} floppy_type[] = { +}; + +static struct floppy_struct 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 */ @@ -92,6 +98,7 @@ static struct floppy_struct { { 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), @@ -103,21 +110,27 @@ static struct floppy_struct { extern void floppy_interrupt(void); extern char tmp_floppy_area[1024]; +extern char floppy_track_buffer[512*2*18]; /* * These are global variables, as that's the easiest way to give * information to interrupts. They are the data used for the current * request. */ +#define NO_TRACK 255 + +static int read_track = 0; /* flag to indicate if we want to read all track */ +static int buffer_track = -1; +static int buffer_drive = -1; 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 current_drive = 255; 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 current_track = NO_TRACK; static unsigned char command = 0; unsigned char selected = 0; struct task_struct * wait_on_floppy_select = NULL; @@ -136,19 +149,37 @@ void floppy_deselect(unsigned int nr) * 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) +int floppy_change(struct buffer_head * bh) { -repeat: - floppy_on(nr); - while ((current_DOR & 3) != nr && selected) - interruptible_sleep_on(&wait_on_floppy_select); - if ((current_DOR & 3) != nr) - goto repeat; - if (inb(FD_DIR) & 0x80) { - floppy_off(nr); + unsigned int mask = 1 << (bh->b_dev & 0x03); + + if (MAJOR(bh->b_dev) != 2) { + printk("floppy_changed: not a floppy\n"); + return 0; + } + if (changed_floppies & mask) { + changed_floppies &= ~mask; + recalibrate = 1; return 1; } - floppy_off(nr); + if (!bh) + return 0; + if (bh->b_dirt) + ll_rw_block(WRITE,bh); + else { + buffer_track = -1; + bh->b_uptodate = 0; + ll_rw_block(READ,bh); + } + cli(); + while (bh->b_lock) + sleep_on(&bh->b_wait); + sti(); + if (changed_floppies & mask) { + changed_floppies &= ~mask; + recalibrate = 1; + return 1; + } return 0; } @@ -159,10 +190,16 @@ __asm__("cld ; rep ; movsl" \ static void setup_DMA(void) { - long addr = (long) CURRENT->buffer; + unsigned long addr = (long) CURRENT->buffer; + unsigned long count = 1024; cli(); - if (addr >= 0x100000) { + if (read_track) { +/* mark buffer-track bad, in case all this fails.. */ + buffer_drive = buffer_track = -1; + count = floppy->sect*2*512; + addr = (long) floppy_track_buffer; + } else if (addr >= 0x100000) { addr = (long) tmp_floppy_area; if (command == FD_WRITE) copy_buffer(CURRENT->buffer,tmp_floppy_area); @@ -182,10 +219,12 @@ static void setup_DMA(void) addr >>= 8; /* bits 16-19 of addr */ immoutb_p(addr,0x81); -/* low 8 bits of count-1 (1024-1=0x3ff) */ - immoutb_p(0xff,5); +/* low 8 bits of count-1 */ + count--; + immoutb_p(count,5); + count >>= 8; /* high 8 bits of count-1 */ - immoutb_p(3,5); + immoutb_p(count,5); /* activate DMA 2 */ immoutb_p(0|2,10); sti(); @@ -205,6 +244,7 @@ static void output_byte(char byte) return; } } + current_track = NO_TRACK; reset = 1; printk("Unable to send byte to FDC\n\r"); } @@ -226,12 +266,14 @@ static int result(void) } } reset = 1; + current_track = NO_TRACK; printk("Getstatus times out\n\r"); return -1; } static void bad_flp_intr(void) { + current_track = NO_TRACK; CURRENT->errors++; if (CURRENT->errors > MAX_ERRORS) { floppy_deselect(current_drive); @@ -249,6 +291,8 @@ static void bad_flp_intr(void) */ static void rw_interrupt(void) { + char * buffer_area; + if (result() != 7 || (ST0 & 0xf8) || (ST1 & 0xbf) || (ST2 & 0x73)) { if (ST1 & 0x02) { printk("Drive %d is write protected\n\r",current_drive); @@ -259,22 +303,43 @@ static void rw_interrupt(void) do_fd_request(); return; } - if (command == FD_READ && (unsigned long)(CURRENT->buffer) >= 0x100000) + if (read_track) { + buffer_track = seek_track; + buffer_drive = current_drive; + buffer_area = floppy_track_buffer + + ((sector-1 + head*floppy->sect)<<9); + copy_buffer(buffer_area,CURRENT->buffer); + } else 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(); } +/* + * We try to read tracks, but if we get too many errors, we + * go back to reading just one sector at a time. + * + * This means we should be able to read a sector even if there + * are other bad sectors on this track. + */ 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); + if (read_track) { + output_byte(current_drive); + output_byte(track); + output_byte(0); + output_byte(1); + } else { + 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); @@ -293,6 +358,7 @@ static void seek_interrupt(void) /* sense drive status */ output_byte(FD_SENSEI); if (result() != 2 || (ST0 & 0xF8) != 0x20 || ST1 != seek_track) { + recalibrate = 1; bad_flp_intr(); do_fd_request(); return; @@ -308,6 +374,7 @@ static void seek_interrupt(void) */ static void transfer(void) { + read_track = (command == FD_READ) && (CURRENT->errors < 4); if (cur_spec1 != floppy->spec1) { cur_spec1 = floppy->spec1; output_byte(FD_SPECIFY); @@ -325,14 +392,12 @@ static void transfer(void) 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); - } + output_byte(FD_SEEK); + if (read_track) + output_byte(current_drive); + else + output_byte((head<<2) | current_drive); + output_byte(seek_track); if (reset) do_fd_request(); } @@ -343,15 +408,15 @@ static void transfer(void) static void recal_interrupt(void) { output_byte(FD_SENSEI); + current_track = NO_TRACK; if (result()!=2 || (ST0 & 0xE0) == 0x60) reset = 1; - else - recalibrate = 0; do_fd_request(); } void unexpected_floppy_interrupt(void) { + current_track = NO_TRACK; output_byte(FD_SENSEI); if (result()!=2 || (ST0 & 0xE0) == 0x60) reset = 1; @@ -387,15 +452,16 @@ static void reset_floppy(void) { int i; + do_floppy = reset_interrupt; reset = 0; + current_track = NO_TRACK; 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++) + for (i=0 ; i<1000 ; i++) __asm__("nop"); outb(current_DOR,FD_DOR); sti(); @@ -403,6 +469,18 @@ static void reset_floppy(void) static void floppy_on_interrupt(void) { + if (inb(FD_DIR) & 0x80) { + changed_floppies |= 1<dev)>>2) + floppy_type; if (current_drive != CURRENT_DEV) - seek = 1; + current_track = NO_TRACK; current_drive = CURRENT_DEV; block = CURRENT->sector; if (block+2 > floppy->size) { @@ -442,21 +513,48 @@ void do_fd_request(void) 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"); + else { + printk("do_fd_request: unknown command\n"); + end_request(0); + goto repeat; + } + if ((seek_track == buffer_track) && + (current_drive == buffer_drive)) { + buffer_area = floppy_track_buffer + + ((sector + head*floppy->sect)<<9); + if (command == FD_READ) { + copy_buffer(buffer_area,CURRENT->buffer); + end_request(1); + goto repeat; + } else + copy_buffer(CURRENT->buffer,buffer_area); + } + if (seek_track != current_track) + seek = 1; + sector++; 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) { + outb(current_DOR,FD_DOR); + blk_size[MAJOR_NR] = floppy_sizes; blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST; - set_trap_gate(0x26,&floppy_interrupt); + set_intr_gate(0x26,&floppy_interrupt); outb(inb_p(0x21)&~0x40,0x21); }