root/kernel/blk_drv/floppy.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. select_callback
  2. floppy_select
  3. motor_on_callback
  4. motor_off_callback
  5. floppy_on
  6. floppy_off
  7. request_done
  8. floppy_change
  9. setup_DMA
  10. output_byte
  11. result
  12. bad_flp_intr
  13. perpendicular_mode
  14. configure_fdc_mode
  15. tell_sector
  16. rw_interrupt
  17. setup_rw_floppy
  18. seek_interrupt
  19. transfer
  20. recal_interrupt
  21. unexpected_floppy_interrupt
  22. recalibrate_floppy
  23. reset_interrupt
  24. reset_floppy
  25. floppy_shutdown
  26. shake_done
  27. retry_recal
  28. shake_zero
  29. shake_one
  30. floppy_ready
  31. setup_format_params
  32. redo_fd_request
  33. do_fd_request
  34. fd_ioctl
  35. find_base
  36. config_types
  37. floppy_open
  38. floppy_release
  39. ignore_interrupt
  40. floppy_interrupt
  41. floppy_init

   1 /*
   2  *  linux/kernel/floppy.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * 02.12.91 - Changed to static variables to indicate need for reset
   9  * and recalibrate. This makes some things easier (output_byte reset
  10  * checking etc), and means less interrupt jumping in case of errors,
  11  * so the code is hopefully easier to understand.
  12  */
  13 
  14 /*
  15  * This file is certainly a mess. I've tried my best to get it working,
  16  * but I don't like programming floppies, and I have only one anyway.
  17  * Urgel. I should check for more errors, and do more graceful error
  18  * recovery. Seems there are problems with several drives. I've tried to
  19  * correct them. No promises. 
  20  */
  21 
  22 /*
  23  * As with hd.c, all routines within this file can (and will) be called
  24  * by interrupts, so extreme caution is needed. A hardware interrupt
  25  * handler may not sleep, or a kernel panic will happen. Thus I cannot
  26  * call "floppy-on" directly, but have to set a special timer interrupt
  27  * etc.
  28  */
  29 
  30 /*
  31  * 28.02.92 - made track-buffering routines, based on the routines written
  32  * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
  33  */
  34 
  35 /*
  36  * Automatic floppy-detection and formatting written by Werner Almesberger
  37  * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
  38  * the floppy-change signal detection.
  39  */
  40 
  41 /*
  42  * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed 
  43  * FDC data overrun bug, added some preliminary stuff for vertical
  44  * recording support.
  45  *
  46  * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
  47  *
  48  * TODO: Errors are still not counted properly.
  49  */
  50 
  51 /* 1992/9/20
  52  * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
  53  * modelled after the freeware MS/DOS program fdformat/88 V1.8 by 
  54  * Christoph H. Hochst\"atter.
  55  * I have fixed the shift values to the ones I always use. Maybe a new
  56  * ioctl() should be created to be able to modify them.
  57  * There is a bug in the driver that makes it impossible to format a
  58  * floppy as the first thing after bootup.
  59  */
  60 
  61 /*
  62  * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
  63  * this helped the floppy driver as well. Much cleaner, and still seems to
  64  * work.
  65  */
  66 
  67 #define REALLY_SLOW_IO
  68 #define FLOPPY_IRQ 6
  69 #define FLOPPY_DMA 2
  70 
  71 #include <linux/sched.h>
  72 #include <linux/fs.h>
  73 #include <linux/kernel.h>
  74 #include <linux/timer.h>
  75 #include <linux/fdreg.h>
  76 #include <linux/fd.h>
  77 #include <linux/errno.h>
  78 
  79 #include <asm/dma.h>
  80 #include <asm/system.h>
  81 #include <asm/io.h>
  82 #include <asm/segment.h>
  83 
  84 #define MAJOR_NR 2
  85 #include "blk.h"
  86 
  87 static unsigned int changed_floppies = 0, fake_change = 0;
  88 
  89 static int initial_reset_flag = 0;
  90 static int need_configure = 1;          /* for 82077 */
  91 static int recalibrate = 0;
  92 static int reset = 0;
  93 static int recover = 0; /* recalibrate immediately after resetting */
  94 static int seek = 0;
  95 
  96 static unsigned char current_DOR = 0x0C;
  97 static unsigned char running = 0;
  98 
  99 #define TYPE(x) ((x)>>2)
 100 #define DRIVE(x) ((x)&0x03)
 101 
 102 /*
 103  * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
 104  * max X times - some types of errors increase the errorcount by 2 or
 105  * even 3, so we might actually retry only X/2 times before giving up.
 106  */
 107 #define MAX_ERRORS 12
 108 
 109 /*
 110  * Maximum disk size (in kilobytes). This default is used whenever the
 111  * current disk size is unknown.
 112  */
 113 #define MAX_DISK_SIZE 1440
 114 
 115 /*
 116  * Maximum number of sectors in a track buffer. Track buffering is disabled
 117  * if tracks are bigger.
 118  */
 119 #define MAX_BUFFER_SECTORS 18
 120 
 121 /*
 122  * The DMA channel used by the floppy controller cannot access data at
 123  * addresses >= 16MB
 124  *
 125  * Went back to the 1MB limit, as some people had problems with the floppy
 126  * driver otherwise. It doesn't matter much for performance anyway, as most
 127  * floppy accesses go through the track buffer.
 128  */
 129 #define LAST_DMA_ADDR   (0x100000 - BLOCK_SIZE)
 130 
 131 /*
 132  * globals used by 'result()'
 133  */
 134 #define MAX_REPLIES 7
 135 static unsigned char reply_buffer[MAX_REPLIES];
 136 #define ST0 (reply_buffer[0])
 137 #define ST1 (reply_buffer[1])
 138 #define ST2 (reply_buffer[2])
 139 #define ST3 (reply_buffer[3])
 140 
 141 /*
 142  * This struct defines the different floppy types.
 143  *
 144  * The 'stretch' tells if the tracks need to be doubled for some
 145  * types (ie 360kB diskette in 1.2MB drive etc). Others should
 146  * be self-explanatory.
 147  */
 148 static struct floppy_struct floppy_type[] = {
 149         {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL },    /* no testing */
 150         {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,NULL },    /* 360kB PC diskettes */
 151         { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,NULL },    /* 1.2 MB AT-diskettes */
 152         {  720, 9,2,40,1,0x2A,0x02,0xDF,0x50,NULL },    /* 360kB in 720kB drive */
 153         { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,NULL },    /* 3.5" 720kB diskette */
 154         {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,NULL },    /* 360kB in 1.2MB drive */
 155         { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,NULL },    /* 720kB in 1.2MB drive */
 156         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL },    /* 1.44MB diskette */
 157 };
 158 
 159 /*
 160  * Auto-detection. Each drive type has a pair of formats which are
 161  * used in succession to try to read the disk. If the FDC cannot lock onto
 162  * the disk, the next format is tried. This uses the variable 'probing'.
 163  */
 164 static struct floppy_struct floppy_types[] = {
 165         {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" }, /* 360kB PC diskettes */
 166         {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" }, /* 360kB PC diskettes */
 167         { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"1.2M" },    /* 1.2 MB AT-diskettes */
 168         {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"360k/AT" }, /* 360kB in 1.2MB drive */
 169         { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" },    /* 3.5" 720kB diskette */
 170         { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" },    /* 3.5" 720kB diskette */
 171         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"1.44M" },   /* 1.44MB diskette */
 172         { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k/AT" }, /* 3.5" 720kB diskette */
 173 };
 174 
 175 /* Auto-detection: Disk type used until the next media change occurs. */
 176 struct floppy_struct *current_type[4] = { NULL, NULL, NULL, NULL };
 177 
 178 /* This type is tried first. */
 179 struct floppy_struct *base_type[4];
 180 
 181 /*
 182  * User-provided type information. current_type points to
 183  * the respective entry of this array.
 184  */
 185 struct floppy_struct user_params[4];
 186 
 187 static int floppy_sizes[] ={
 188         MAX_DISK_SIZE, MAX_DISK_SIZE, MAX_DISK_SIZE, MAX_DISK_SIZE,
 189          360, 360 ,360, 360,
 190         1200,1200,1200,1200,
 191          360, 360, 360, 360,
 192          720, 720, 720, 720,
 193          360, 360, 360, 360,
 194          720, 720, 720, 720,
 195         1440,1440,1440,1440
 196 };
 197 
 198 /*
 199  * The driver is trying to determine the correct media format
 200  * while probing is set. rw_interrupt() clears it after a
 201  * successful access.
 202  */
 203 static int probing = 0;
 204 
 205 /*
 206  * (User-provided) media information is _not_ discarded after a media change
 207  * if the corresponding keep_data flag is non-zero. Positive values are
 208  * decremented after each probe.
 209  */
 210 static int keep_data[4] = { 0,0,0,0 };
 211 
 212 /*
 213  * Announce successful media type detection and media information loss after
 214  * disk changes.
 215  * Also used to enable/disable printing of overrun warnings.
 216  */
 217 static ftd_msg[4] = { 0,0,0,0 };
 218 
 219 /* Prevent "aliased" accesses. */
 220 
 221 static fd_ref[4] = { 0,0,0,0 };
 222 static fd_device[4] = { 0,0,0,0 };
 223 
 224 /* Synchronization of FDC access. */
 225 static volatile int format_status = FORMAT_NONE, fdc_busy = 0;
 226 static struct wait_queue *fdc_wait = NULL, *format_done = NULL;
 227 
 228 /* Errors during formatting are counted here. */
 229 static int format_errors;
 230 
 231 /* Format request descriptor. */
 232 static struct format_descr format_req;
 233 
 234 /*
 235  * Current device number. Taken either from the block header or from the
 236  * format request descriptor.
 237  */
 238 #define CURRENT_DEVICE (format_status == FORMAT_BUSY ? format_req.device : \
 239    (CURRENT->dev))
 240 
 241 /* Current error count. */
 242 #define CURRENT_ERRORS (format_status == FORMAT_BUSY ? format_errors : \
 243     (CURRENT->errors))
 244 
 245 /*
 246  * Threshold for reporting FDC errors to the console.
 247  * Setting this to zero may flood your screen when using
 248  * ultra cheap floppies ;-)
 249  */
 250 static unsigned short min_report_error_cnt[4] = {2, 2, 2, 2};
 251 
 252 /*
 253  * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
 254  * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
 255  * H is head unload time (1=16ms, 2=32ms, etc)
 256  *
 257  * Spec2 is (HLD<<1 | ND), where HLD is head load time (1=2ms, 2=4 ms etc)
 258  * and ND is set means no DMA. Hardcoded to 6 (HLD=6ms, use DMA).
 259  */
 260 
 261 /*
 262  * Track buffer and block buffer (in case track buffering doesn't work).
 263  * Because these are written to by the DMA controller, they must
 264  * not contain a 64k byte boundary crossing, or data will be
 265  * corrupted/lost. Alignment of these is enforced in boot/head.s.
 266  * Note that you must not change the sizes below without updating head.s.
 267  */
 268 extern char tmp_floppy_area[BLOCK_SIZE];
 269 extern char floppy_track_buffer[512*2*MAX_BUFFER_SECTORS];
 270 
 271 static void redo_fd_request(void);
 272 
 273 /*
 274  * These are global variables, as that's the easiest way to give
 275  * information to interrupts. They are the data used for the current
 276  * request.
 277  */
 278 #define NO_TRACK 255
 279 
 280 static int read_track = 0;      /* flag to indicate if we want to read entire track */
 281 static int buffer_track = -1;
 282 static int buffer_drive = -1;
 283 static int cur_spec1 = -1;
 284 static int cur_rate = -1;
 285 static struct floppy_struct * floppy = floppy_type;
 286 static unsigned char current_drive = 255;
 287 static unsigned char sector = 0;
 288 static unsigned char head = 0;
 289 static unsigned char track = 0;
 290 static unsigned char seek_track = 0;
 291 static unsigned char current_track = NO_TRACK;
 292 static unsigned char command = 0;
 293 static unsigned char fdc_version = FDC_TYPE_STD;        /* FDC version code */
 294 
 295 static void floppy_ready(void);
 296 
 297 static void select_callback(unsigned long unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299         floppy_ready();
 300 }
 301 
 302 static void floppy_select(unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {
 304         static struct timer_list select = { NULL, 0, 0, select_callback };
 305 
 306         if (current_drive == (current_DOR & 3)) {
 307                 floppy_ready();
 308                 return;
 309         }
 310         seek = 1;
 311         current_track = NO_TRACK;
 312         current_DOR &= 0xFC;
 313         current_DOR |= current_drive;
 314         outb(current_DOR,FD_DOR);
 315         del_timer(&select);
 316         select.expires = 2;
 317         add_timer(&select);
 318 }
 319 
 320 static void motor_on_callback(unsigned long nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         running |= 0x10 << nr;
 323         floppy_select(nr);
 324 }
 325 
 326 static struct timer_list motor_on_timer[4] = {
 327         { NULL, 0, 0, motor_on_callback },
 328         { NULL, 0, 1, motor_on_callback },
 329         { NULL, 0, 2, motor_on_callback },
 330         { NULL, 0, 3, motor_on_callback }
 331 };
 332 
 333 static void motor_off_callback(unsigned long nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 334 {
 335         unsigned char mask = ~(0x10 << nr);
 336         cli();
 337         running &= mask;
 338         current_DOR &= mask;
 339         outb(current_DOR,FD_DOR);
 340         sti();
 341 }
 342 
 343 static struct timer_list motor_off_timer[4] = {
 344         { NULL, 0, 0, motor_off_callback },
 345         { NULL, 0, 1, motor_off_callback },
 346         { NULL, 0, 2, motor_off_callback },
 347         { NULL, 0, 3, motor_off_callback }
 348 };
 349 
 350 static void floppy_on(unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 351 {
 352         unsigned char mask = 0x10 << nr;
 353 
 354         del_timer(motor_off_timer + nr);
 355         if (mask & running)
 356                 floppy_select(nr);
 357         if (!(mask & current_DOR)) {
 358                 del_timer(motor_on_timer + nr);
 359                 motor_on_timer[nr].expires = HZ;
 360                 add_timer(motor_on_timer + nr);
 361         }
 362         current_DOR &= 0xFC;
 363         current_DOR |= mask;
 364         current_DOR |= nr;
 365         outb(current_DOR,FD_DOR);
 366 }
 367 
 368 static void floppy_off(unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370         del_timer(motor_off_timer+nr);
 371         motor_off_timer[nr].expires = 3*HZ;
 372         add_timer(motor_off_timer+nr);
 373 }
 374 
 375 void request_done(int uptodate)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377         timer_active &= ~(1 << FLOPPY_TIMER);
 378         if (format_status != FORMAT_BUSY)
 379                 end_request(uptodate);
 380         else {
 381                 format_status = uptodate ? FORMAT_OKAY : FORMAT_ERROR;
 382                 wake_up(&format_done);
 383         }
 384 }
 385 
 386 /*
 387  * floppy-change is never called from an interrupt, so we can relax a bit
 388  * here, sleep etc. Note that floppy-on tries to set current_DOR to point
 389  * to the desired drive, but it will probably not survive the sleep if
 390  * several floppies are used at the same time: thus the loop.
 391  */
 392 int floppy_change(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 393 {
 394         unsigned int mask = 1 << (bh->b_dev & 0x03);
 395 
 396         if (MAJOR(bh->b_dev) != 2) {
 397                 printk("floppy_changed: not a floppy\n");
 398                 return 0;
 399         }
 400         if (fake_change & mask) {
 401                 fake_change &= ~mask;
 402 /* omitting the next line breaks formatting in a horrible way ... */
 403                 changed_floppies &= ~mask;
 404                 return 1;
 405         }
 406         if (changed_floppies & mask) {
 407                 changed_floppies &= ~mask;
 408                 recalibrate = 1;
 409                 return 1;
 410         }
 411         if (!bh)
 412                 return 0;
 413         if (bh->b_dirt)
 414                 ll_rw_block(WRITE, 1, &bh);
 415         else {
 416                 buffer_track = -1;
 417                 bh->b_uptodate = 0;
 418                 ll_rw_block(READ, 1, &bh);
 419         }
 420         cli();
 421         while (bh->b_lock)
 422                 sleep_on(&bh->b_wait);
 423         sti();
 424         if (changed_floppies & mask) {
 425                 changed_floppies &= ~mask;
 426                 recalibrate = 1;
 427                 return 1;
 428         }
 429         return 0;
 430 }
 431 
 432 #define copy_buffer(from,to) \
 433 __asm__("cld ; rep ; movsl" \
 434         : \
 435         :"c" (BLOCK_SIZE/4),"S" ((long)(from)),"D" ((long)(to)) \
 436         :"cx","di","si")
 437 
 438 static void setup_DMA(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 439 {
 440         unsigned long addr,count;
 441         unsigned char dma_code;
 442 
 443         dma_code = DMA_WRITE;
 444         if (command == FD_READ)
 445                 dma_code = DMA_READ;
 446         if (command == FD_FORMAT) {
 447                 addr = (long) tmp_floppy_area;
 448                 count = floppy->sect*4;
 449         } else {
 450                 addr = (long) CURRENT->buffer;
 451                 count = 1024;
 452         }
 453         if (read_track) {
 454 /* mark buffer-track bad, in case all this fails.. */
 455                 buffer_drive = buffer_track = -1;
 456                 count = floppy->sect*2*512;
 457                 addr = (long) floppy_track_buffer;
 458         } else if (addr >= LAST_DMA_ADDR) {
 459                 addr = (long) tmp_floppy_area;
 460                 if (command == FD_WRITE)
 461                         copy_buffer(CURRENT->buffer,tmp_floppy_area);
 462         }
 463         cli();
 464         disable_dma(FLOPPY_DMA);
 465         clear_dma_ff(FLOPPY_DMA);
 466         set_dma_mode(FLOPPY_DMA, (command == FD_READ)? DMA_MODE_READ : DMA_MODE_WRITE);
 467         set_dma_addr(FLOPPY_DMA, addr);
 468         set_dma_count(FLOPPY_DMA, count);
 469         enable_dma(FLOPPY_DMA);
 470         sti();
 471 }
 472 
 473 static void output_byte(char byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 474 {
 475         int counter;
 476         unsigned char status;
 477 
 478         if (reset)
 479                 return;
 480         for(counter = 0 ; counter < 10000 ; counter++) {
 481                 status = inb_p(FD_STATUS) & (STATUS_READY | STATUS_DIR);
 482                 if (status == STATUS_READY) {
 483                         outb(byte,FD_DATA);
 484                         return;
 485                 }
 486         }
 487         current_track = NO_TRACK;
 488         reset = 1;
 489         printk("Unable to send byte to FDC\n");
 490 }
 491 
 492 static int result(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 493 {
 494         int i = 0, counter, status;
 495 
 496         if (reset)
 497                 return -1;
 498         for (counter = 0 ; counter < 10000 ; counter++) {
 499                 status = inb_p(FD_STATUS)&(STATUS_DIR|STATUS_READY|STATUS_BUSY);
 500                 if (status == STATUS_READY) {
 501                         return i;
 502                 }
 503                 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY)) {
 504                         if (i >= MAX_REPLIES) {
 505                                 printk("floppy_stat reply overrun\n");
 506                                 break;
 507                         }
 508                         reply_buffer[i++] = inb_p(FD_DATA);
 509                 }
 510         }
 511         reset = 1;
 512         current_track = NO_TRACK;
 513         printk("Getstatus times out\n");
 514         return -1;
 515 }
 516 
 517 static void bad_flp_intr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519         int errors;
 520 
 521         current_track = NO_TRACK;
 522         if (format_status == FORMAT_BUSY)
 523                 errors = ++format_errors;
 524         else if (!CURRENT) {
 525                 printk(DEVICE_NAME ": no current request\n");
 526                 reset = recalibrate = 1;
 527                 return;
 528         } else
 529                 errors = ++CURRENT->errors;
 530         if (errors > MAX_ERRORS) {
 531                 request_done(0);
 532         }
 533         if (errors > MAX_ERRORS/2)
 534                 reset = 1;
 535         else
 536                 recalibrate = 1;
 537 }       
 538 
 539 
 540 /* Set perpendicular mode as required, based on data rate, if supported.
 541  * 82077 Untested! 1Mbps data rate only possible with 82077-1.
 542  * TODO: increase MAX_BUFFER_SECTORS, add floppy_type entries.
 543  */
 544 static inline void perpendicular_mode(unsigned char rate)
     /* [previous][next][first][last][top][bottom][index][help] */
 545 {
 546         if (fdc_version == FDC_TYPE_82077) {
 547                 output_byte(FD_PERPENDICULAR);
 548                 if (rate & 0x40) {
 549                         unsigned char r = rate & 0x03;
 550                         if (r == 0)
 551                                 output_byte(2); /* perpendicular, 500 kbps */
 552                         else if (r == 3)
 553                                 output_byte(3); /* perpendicular, 1Mbps */
 554                         else {
 555                                 printk(DEVICE_NAME ": Invalid data rate for perpendicular mode!\n");
 556                                 reset = 1;
 557                         }
 558                 } else
 559                         output_byte(0);         /* conventional mode */
 560         } else {
 561                 if (rate & 0x40) {
 562                         printk(DEVICE_NAME ": perpendicular mode not supported by this FDC.\n");
 563                         reset = 1;
 564                 }
 565         }
 566 } /* perpendicular_mode */
 567 
 568 
 569 /*
 570  * This has only been tested for the case fdc_version == FDC_TYPE_STD.
 571  * In case you have a 82077 and want to test it, you'll have to compile
 572  * with `FDC_FIFO_UNTESTED' defined. You may also want to add support for
 573  * recognizing drives with vertical recording support.
 574  */
 575 static void configure_fdc_mode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 576 {
 577         if (need_configure && (fdc_version == FDC_TYPE_82077)) {
 578                 /* Enhanced version with FIFO & vertical recording. */
 579                 output_byte(FD_CONFIGURE);
 580                 output_byte(0);
 581                 output_byte(0x1A);      /* FIFO on, polling off, 10 byte threshold */
 582                 output_byte(0);         /* precompensation from track 0 upwards */
 583                 need_configure = 0;
 584                 printk(DEVICE_NAME ": FIFO enabled\n");
 585         }
 586         if (cur_spec1 != floppy->spec1) {
 587                 cur_spec1 = floppy->spec1;
 588                 output_byte(FD_SPECIFY);
 589                 output_byte(cur_spec1);         /* hut etc */
 590                 output_byte(6);                 /* Head load time =6ms, DMA */
 591         }
 592         if (cur_rate != floppy->rate) {
 593                 /* use bit 6 of floppy->rate to indicate perpendicular mode */
 594                 perpendicular_mode(floppy->rate);
 595                 outb_p((cur_rate = (floppy->rate)) & ~0x40, FD_DCR);
 596         }
 597 } /* configure_fdc_mode */
 598 
 599 
 600 static void tell_sector(int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 601 {
 602         if (nr!=7) {
 603                 printk(" -- FDC reply errror");
 604                 reset = 1;
 605         } else
 606                 printk(": track %d, head %d, sector %d", reply_buffer[3],
 607                         reply_buffer[4], reply_buffer[5]);
 608 } /* tell_sector */
 609 
 610 
 611 /*
 612  * Ok, this interrupt is called after a DMA read/write has succeeded
 613  * or failed, so we check the results, and copy any buffers.
 614  * hhb: Added better error reporting.
 615  */
 616 static void rw_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 617 {
 618         char * buffer_area;
 619         int nr;
 620         char bad;
 621 
 622         nr = result();
 623         /* check IC to find cause of interrupt */
 624         switch ((ST0 & ST0_INTR)>>6) {
 625                 case 1: /* error occured during command execution */
 626                         bad = 1;
 627                         if (ST1 & ST1_WP) {
 628                                 printk(DEVICE_NAME ": Drive %d is write protected\n", current_drive);
 629                                 request_done(0);
 630                                 bad = 0;
 631                         } else if (ST1 & ST1_OR) {
 632                                 if (ftd_msg[ST0 & ST0_DS])
 633                                         printk(DEVICE_NAME ": Over/Underrun - retrying\n");
 634                                 /* could continue from where we stopped, but ... */
 635                                 bad = 0;
 636                         } else if (CURRENT_ERRORS > min_report_error_cnt[ST0 & ST0_DS]) {
 637                                 printk(DEVICE_NAME " %d: ", ST0 & ST0_DS);
 638                                 if (ST0 & ST0_ECE) {
 639                                         printk("Recalibrate failed!");
 640                                 } else if (ST2 & ST2_CRC) {
 641                                         printk("data CRC error");
 642                                         tell_sector(nr);
 643                                 } else if (ST1 & ST1_CRC) {
 644                                         printk("CRC error");
 645                                         tell_sector(nr);
 646                                 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
 647                                         if (!probing) {
 648                                                 printk("sector not found");
 649                                                 tell_sector(nr);
 650                                         } else
 651                                                 printk("probe failed...");
 652                                 } else if (ST2 & ST2_WC) {      /* seek error */
 653                                         printk("wrong cylinder");
 654                                 } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
 655                                         printk("bad cylinder");
 656                                 } else {
 657                                         printk("unknown error. ST[0..3] are: 0x%x 0x%x 0x%x 0x%x\n", ST0, ST1, ST2, ST3);
 658                                 }
 659                                 printk("\n");
 660 
 661                         }
 662                         if (bad)
 663                                 bad_flp_intr();
 664                         redo_fd_request();
 665                         return;
 666                 case 2: /* invalid command given */
 667                         printk(DEVICE_NAME ": Invalid FDC command given!\n");
 668                         request_done(0);
 669                         return;
 670                 case 3:
 671                         printk(DEVICE_NAME ": Abnormal termination caused by polling\n");
 672                         bad_flp_intr();
 673                         redo_fd_request();
 674                         return;
 675                 default: /* (0) Normal command termination */
 676                         break;
 677         }
 678 
 679         if (probing) {
 680                 int drive = MINOR(CURRENT->dev);
 681 
 682                 if (ftd_msg[drive])
 683                         printk("Auto-detected floppy type %s in fd%d\n",
 684                             floppy->name,drive);
 685                 current_type[drive] = floppy;
 686                 floppy_sizes[drive] = floppy->size >> 1;
 687                 probing = 0;
 688         }
 689         if (read_track) {
 690                 buffer_track = seek_track;
 691                 buffer_drive = current_drive;
 692                 buffer_area = floppy_track_buffer +
 693                         ((sector-1 + head*floppy->sect)<<9);
 694                 copy_buffer(buffer_area,CURRENT->buffer);
 695         } else if (command == FD_READ &&
 696                 (unsigned long)(CURRENT->buffer) >= LAST_DMA_ADDR)
 697                 copy_buffer(tmp_floppy_area,CURRENT->buffer);
 698         request_done(1);
 699         redo_fd_request();
 700 }
 701 
 702 /*
 703  * We try to read tracks, but if we get too many errors, we
 704  * go back to reading just one sector at a time.
 705  *
 706  * This means we should be able to read a sector even if there
 707  * are other bad sectors on this track.
 708  */
 709 inline void setup_rw_floppy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711         setup_DMA();
 712         do_floppy = rw_interrupt;
 713         output_byte(command);
 714         if (command != FD_FORMAT) {
 715                 if (read_track) {
 716                         output_byte(current_drive);
 717                         output_byte(track);
 718                         output_byte(0);
 719                         output_byte(1);
 720                 } else {
 721                         output_byte(head<<2 | current_drive);
 722                         output_byte(track);
 723                         output_byte(head);
 724                         output_byte(sector);
 725                 }
 726                 output_byte(2);         /* sector size = 512 */
 727                 output_byte(floppy->sect);
 728                 output_byte(floppy->gap);
 729                 output_byte(0xFF);      /* sector size (0xff when n!=0 ?) */
 730         } else {
 731                 output_byte(head<<2 | current_drive);
 732                 output_byte(2);
 733                 output_byte(floppy->sect);
 734                 output_byte(floppy->fmt_gap);
 735                 output_byte(FD_FILL_BYTE);
 736         }
 737         if (reset)
 738                 redo_fd_request();
 739 }
 740 
 741 /*
 742  * This is the routine called after every seek (or recalibrate) interrupt
 743  * from the floppy controller. Note that the "unexpected interrupt" routine
 744  * also does a recalibrate, but doesn't come here.
 745  */
 746 static void seek_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 747 {
 748 /* sense drive status */
 749         output_byte(FD_SENSEI);
 750         if (result() != 2 || (ST0 & 0xF8) != 0x20 || ST1 != seek_track) {
 751                 printk(DEVICE_NAME ": seek failed\n");
 752                 recalibrate = 1;
 753                 bad_flp_intr();
 754                 redo_fd_request();
 755                 return;
 756         }
 757         current_track = ST1;
 758         setup_rw_floppy();
 759 }
 760 
 761 
 762 /*
 763  * This routine is called when everything should be correctly set up
 764  * for the transfer (ie floppy motor is on and the correct floppy is
 765  * selected).
 766  */
 767 static void transfer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 768 {
 769         read_track = (command == FD_READ) && (CURRENT_ERRORS < 4) &&
 770             (floppy->sect <= MAX_BUFFER_SECTORS);
 771 
 772         configure_fdc_mode();
 773 
 774         if (reset) {
 775                 redo_fd_request();
 776                 return;
 777         }
 778         if (!seek) {
 779                 setup_rw_floppy();
 780                 return;
 781         }
 782 
 783         do_floppy = seek_interrupt;
 784         output_byte(FD_SEEK);
 785         if (read_track)
 786                 output_byte(current_drive);
 787         else
 788                 output_byte((head<<2) | current_drive);
 789         output_byte(seek_track);
 790         if (reset)
 791                 redo_fd_request();
 792 }
 793 
 794 /*
 795  * Special case - used after a unexpected interrupt (or reset)
 796  */
 797 
 798 static void recalibrate_floppy(void);
 799 
 800 static void recal_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 801 {
 802         output_byte(FD_SENSEI);
 803         current_track = NO_TRACK;
 804         if (result()!=2 || (ST0 & 0xE0) == 0x60)
 805                 reset = 1;
 806 /* Recalibrate until track 0 is reached. Might help on some errors. */
 807         if ((ST0 & 0x10) == 0x10)
 808                 recalibrate_floppy();   /* FIXME: should limit nr of recalibrates */
 809         else
 810                 redo_fd_request();
 811 }
 812 
 813 static void unexpected_floppy_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 814 {
 815         current_track = NO_TRACK;
 816         output_byte(FD_SENSEI);
 817         printk(DEVICE_NAME ": unexpected interrupt\n");
 818         if (result()!=2 || (ST0 & 0xE0) == 0x60)
 819                 reset = 1;
 820         else
 821                 recalibrate = 1;
 822 }
 823 
 824 static void recalibrate_floppy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 825 {
 826         recalibrate = 0;
 827         current_track = 0;
 828         do_floppy = recal_interrupt;
 829         output_byte(FD_RECALIBRATE);
 830         output_byte(head<<2 | current_drive);
 831         if (reset)
 832                 redo_fd_request();
 833 }
 834 
 835 /*
 836  * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
 837  */
 838 static void reset_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 839 {
 840         short i;
 841 
 842         for (i=0; i<4; i++) {
 843                 output_byte(FD_SENSEI);
 844                 (void) result();
 845         }
 846         output_byte(FD_SPECIFY);
 847         output_byte(cur_spec1);         /* hut etc */
 848         output_byte(6);                 /* Head load time =6ms, DMA */
 849         configure_fdc_mode();           /* reprogram fdc */
 850         if (initial_reset_flag) {
 851                 initial_reset_flag = 0;
 852                 recalibrate = 1;
 853                 reset = 0;
 854                 return;
 855         }
 856         if (!recover)
 857                 redo_fd_request();
 858         else {
 859                 recalibrate_floppy();
 860                 recover = 0;
 861         }
 862 }
 863 
 864 /*
 865  * reset is done by pulling bit 2 of DOR low for a while.
 866  */
 867 static void reset_floppy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 868 {
 869         int i;
 870 
 871         do_floppy = reset_interrupt;
 872         reset = 0;
 873         current_track = NO_TRACK;
 874         cur_spec1 = -1;
 875         cur_rate = -1;
 876         recalibrate = 1;
 877         need_configure = 1;
 878         if (!initial_reset_flag)
 879                 printk("Reset-floppy called\n");
 880         cli();
 881         outb_p(current_DOR & ~0x04, FD_DOR);
 882         for (i=0 ; i<1000 ; i++)
 883                 __asm__("nop");
 884         outb(current_DOR, FD_DOR);
 885         sti();
 886 }
 887 
 888 static void floppy_shutdown(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 889 {
 890         cli();
 891         do_floppy = NULL;
 892         request_done(0);
 893         recover = 1;
 894         reset_floppy();
 895         sti();
 896         redo_fd_request();
 897 }
 898 
 899 static void shake_done(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 900 {
 901         current_track = NO_TRACK;
 902         if (inb(FD_DIR) & 0x80)
 903                 request_done(0);
 904         redo_fd_request();
 905 }
 906 
 907 static int retry_recal(void (*proc)(void))
     /* [previous][next][first][last][top][bottom][index][help] */
 908 {
 909         output_byte(FD_SENSEI);
 910         if (result() == 2 && (ST0 & 0x10) != 0x10) return 0;
 911         do_floppy = proc;
 912         output_byte(FD_RECALIBRATE);
 913         output_byte(head<<2 | current_drive);
 914         return 1;
 915 }
 916 
 917 static void shake_zero(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 918 {
 919         if (!retry_recal(shake_zero)) shake_done();
 920 }
 921 
 922 static void shake_one(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 923 {
 924         if (retry_recal(shake_one)) return;
 925         do_floppy = shake_done;
 926         output_byte(FD_SEEK);
 927         output_byte(head << 2 | current_drive);
 928         output_byte(1);
 929 }
 930 
 931 static void floppy_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 932 {
 933         if (inb(FD_DIR) & 0x80) {
 934                 changed_floppies |= 1<<current_drive;
 935                 buffer_track = -1;
 936                 if (keep_data[current_drive]) {
 937                         if (keep_data[current_drive] > 0)
 938                                 keep_data[current_drive]--;
 939                 } else {
 940                         if (ftd_msg[current_drive] && current_type[current_drive] != NULL)
 941                                 printk("Disk type is undefined after disk "
 942                                     "change in fd%d\n",current_drive);
 943                         current_type[current_drive] = NULL;
 944                         floppy_sizes[current_drive] = MAX_DISK_SIZE;
 945                 }
 946 /* Forcing the drive to seek makes the "media changed" condition go away.
 947  * There should be a cleaner solution for that ...
 948  */
 949                 if (!reset && !recalibrate) {
 950                         if (current_track && current_track != NO_TRACK)
 951                                 do_floppy = shake_zero;
 952                         else
 953                                 do_floppy = shake_one;
 954                         output_byte(FD_RECALIBRATE);
 955                         output_byte(head<<2 | current_drive);
 956                         return;
 957                 }
 958         }
 959         if (reset) {
 960                 reset_floppy();
 961                 return;
 962         }
 963         if (recalibrate) {
 964                 recalibrate_floppy();
 965                 return;
 966         }
 967         transfer();
 968 }
 969 
 970 static void setup_format_params(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 971 {
 972     unsigned char *here = (unsigned char *) tmp_floppy_area;
 973     int count,head_shift,track_shift,total_shift;
 974 
 975     /* allow for about 30ms for data transport per track */
 976     head_shift  = floppy->sect / 6;
 977     /* a ``cylinder'' is two tracks plus a little stepping time */
 978     track_shift = 2 * head_shift + 1; 
 979     /* count backwards */
 980     total_shift = floppy->sect - 
 981         ((track_shift * track + head_shift * head) % floppy->sect);
 982 
 983     /* XXX: should do a check to see this fits in tmp_floppy_area!! */
 984     for (count = 0; count < floppy->sect; count++) {
 985         *here++ = track;
 986         *here++ = head;
 987         *here++ = 1 + (( count + total_shift ) % floppy->sect);
 988         *here++ = 2; /* 512 bytes */
 989     }
 990 }
 991 
 992 static void redo_fd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 993 {
 994         unsigned int block;
 995         char * buffer_area;
 996         int device;
 997 
 998         if (CURRENT && CURRENT->dev < 0) return;
 999 
1000 repeat:
1001         if (format_status == FORMAT_WAIT)
1002                 format_status = FORMAT_BUSY;
1003         if (format_status != FORMAT_BUSY) {
1004                 if (!CURRENT) {
1005                         if (!fdc_busy)
1006                                 printk("FDC access conflict!");
1007                         fdc_busy = 0;
1008                         wake_up(&fdc_wait);
1009                         CLEAR_INTR;
1010                         return;
1011                 }
1012                 if (MAJOR(CURRENT->dev) != MAJOR_NR)
1013                         panic(DEVICE_NAME ": request list destroyed"); \
1014                 if (CURRENT->bh) {
1015                         if (!CURRENT->bh->b_lock)
1016                                 panic(DEVICE_NAME ": block not locked");
1017                 }
1018         }
1019         seek = 0;
1020         probing = 0;
1021         device = MINOR(CURRENT_DEVICE);
1022         if (device > 3)
1023                 floppy = (device >> 2) + floppy_type;
1024         else { /* Auto-detection */
1025                 floppy = current_type[device & 3];
1026                 if (!floppy) {
1027                         probing = 1;
1028                         floppy = base_type[device & 3];
1029                         if (!floppy) {
1030                                 request_done(0);
1031                                 goto repeat;
1032                         }
1033                         if (CURRENT_ERRORS & 1)
1034                                 floppy++;
1035                 }
1036         }
1037         if (format_status != FORMAT_BUSY) {
1038                 if (current_drive != CURRENT_DEV) {
1039                         current_track = NO_TRACK;
1040                         current_drive = CURRENT_DEV;
1041                 }
1042                 block = CURRENT->sector;
1043                 if (block+2 > floppy->size) {
1044                         request_done(0);
1045                         goto repeat;
1046                 }
1047                 sector = block % floppy->sect;
1048                 block /= floppy->sect;
1049                 head = block % floppy->head;
1050                 track = block / floppy->head;
1051                 seek_track = track << floppy->stretch;
1052                 if (CURRENT->cmd == READ)
1053                         command = FD_READ;
1054                 else if (CURRENT->cmd == WRITE)
1055                         command = FD_WRITE;
1056                 else {
1057                         printk("do_fd_request: unknown command\n");
1058                         request_done(0);
1059                         goto repeat;
1060                 }
1061         } else {
1062                 if (current_drive != (format_req.device & 3))
1063                         current_track = NO_TRACK;
1064                 current_drive = format_req.device & 3;
1065                 if (((unsigned) format_req.track) >= floppy->track ||
1066                     (format_req.head & 0xfffe) || probing) {
1067                         request_done(0);
1068                         goto repeat;
1069                 }
1070                 head = format_req.head;
1071                 track = format_req.track;
1072                 seek_track = track << floppy->stretch;
1073                 if (seek_track == buffer_track) buffer_track = -1;
1074                 command = FD_FORMAT;
1075                 setup_format_params();
1076         }
1077         timer_table[FLOPPY_TIMER].expires = jiffies+10*HZ;
1078         timer_active |= 1 << FLOPPY_TIMER;
1079         if ((seek_track == buffer_track) &&
1080          (current_drive == buffer_drive)) {
1081                 buffer_area = floppy_track_buffer +
1082                         ((sector + head*floppy->sect)<<9);
1083                 if (command == FD_READ) {
1084                         copy_buffer(buffer_area,CURRENT->buffer);
1085                         request_done(1);
1086                         goto repeat;
1087                 } else if (command == FD_WRITE)
1088                         copy_buffer(CURRENT->buffer,buffer_area);
1089         }
1090         if (seek_track != current_track)
1091                 seek = 1;
1092         sector++;
1093         del_timer(motor_off_timer + current_drive);
1094         floppy_on(current_drive);
1095 }
1096 
1097 void do_fd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1098 {
1099         cli();
1100         while (fdc_busy) sleep_on(&fdc_wait);
1101         fdc_busy = 1;
1102         sti();
1103         redo_fd_request();
1104 }
1105 
1106 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1107     unsigned long param)
1108 {
1109         int i,drive,cnt,okay;
1110         struct floppy_struct *this_floppy;
1111 
1112         switch (cmd) {
1113                 RO_IOCTLS(inode->i_rdev,param);
1114         }
1115         drive = MINOR(inode->i_rdev);
1116         switch (cmd) {
1117                 case FDFMTBEG:
1118                         if (!suser())
1119                                 return -EPERM;
1120                         return 0;
1121                 case FDFMTEND:
1122                         if (!suser())
1123                                 return -EPERM;
1124                         cli();
1125                         fake_change |= 1 << (drive & 3);
1126                         sti();
1127                         drive &= 3;
1128                         cmd = FDCLRPRM;
1129                         break;
1130                 case FDGETPRM:
1131                         if (drive > 3) this_floppy = &floppy_type[drive >> 2];
1132                         else if ((this_floppy = current_type[drive & 3]) == NULL)
1133                                     return -ENODEV;
1134                         i = verify_area(VERIFY_WRITE,(void *) param,sizeof(struct floppy_struct));
1135                         if (i)
1136                                 return i;
1137                         for (cnt = 0; cnt < sizeof(struct floppy_struct); cnt++)
1138                                 put_fs_byte(((char *) this_floppy)[cnt],
1139                                     (char *) param+cnt);
1140                         return 0;
1141                 case FDFMTTRK:
1142                         if (!suser())
1143                                 return -EPERM;
1144                         if (fd_ref[drive & 3] != 1)
1145                                 return -EBUSY;
1146                         cli();
1147                         while (format_status != FORMAT_NONE)
1148                                 sleep_on(&format_done);
1149                         for (cnt = 0; cnt < sizeof(struct format_descr); cnt++)
1150                                 ((char *) &format_req)[cnt] = get_fs_byte(
1151                                     (char *) param+cnt);
1152                         format_req.device = drive;
1153                         format_status = FORMAT_WAIT;
1154                         format_errors = 0;
1155                         while (format_status != FORMAT_OKAY && format_status !=
1156                             FORMAT_ERROR) {
1157                                 if (fdc_busy) sleep_on(&fdc_wait);
1158                                 else {
1159                                         fdc_busy = 1;
1160                                         redo_fd_request();
1161                                 }
1162                         }
1163                         while (format_status != FORMAT_OKAY && format_status !=
1164                             FORMAT_ERROR)
1165                                 sleep_on(&format_done);
1166                         sti();
1167                         okay = format_status == FORMAT_OKAY;
1168                         format_status = FORMAT_NONE;
1169                         wake_up(&format_done);
1170                         return okay ? 0 : -EIO;
1171                 case FDFLUSH:
1172                         if (!permission(inode, 2))
1173                                 return -EPERM;
1174                         cli();
1175                         fake_change |= 1 << (drive & 3);
1176                         sti();
1177                         check_disk_change(inode->i_rdev);
1178                         return 0;
1179         }
1180         if (!suser())
1181                 return -EPERM;
1182         if (drive < 0 || drive > 3)
1183                 return -EINVAL;
1184         switch (cmd) {
1185                 case FDCLRPRM:
1186                         current_type[drive] = NULL;
1187                         floppy_sizes[drive] = MAX_DISK_SIZE;
1188                         keep_data[drive] = 0;
1189                         break;
1190                 case FDSETPRM:
1191                 case FDDEFPRM:
1192                         memcpy_fromfs(user_params+drive,
1193                                 (void *) param,
1194                                 sizeof(struct floppy_struct));
1195                         current_type[drive] = &user_params[drive];
1196                         floppy_sizes[drive] = user_params[drive].size >> 1;
1197                         if (cmd == FDDEFPRM)
1198                                 keep_data[drive] = -1;
1199                         else {
1200                                 cli();
1201                                 while (fdc_busy) sleep_on(&fdc_wait);
1202                                 fdc_busy = 1;
1203                                 sti();
1204                                 outb_p((current_DOR & 0xfc) | drive |
1205                                     (0x10 << drive),FD_DOR);
1206                                 for (cnt = 0; cnt < 1000; cnt++) __asm__("nop");
1207                                 if (inb(FD_DIR) & 0x80)
1208                                         keep_data[drive] = 1;
1209                                 else
1210                                         keep_data[drive] = 0;
1211                                 outb_p(current_DOR,FD_DOR);
1212                                 fdc_busy = 0;
1213                                 wake_up(&fdc_wait);
1214                         }
1215                         break;
1216                 case FDMSGON:
1217                         ftd_msg[drive] = 1;
1218                         break;
1219                 case FDMSGOFF:
1220                         ftd_msg[drive] = 0;
1221                         break;
1222                 case FDSETEMSGTRESH:
1223                         min_report_error_cnt[drive] = (unsigned short) (param & 0x0f);
1224                         break;
1225                 default:
1226                         return -EINVAL;
1227         }
1228         return 0;
1229 }
1230 
1231 #define CMOS_READ(addr) ({ \
1232 outb_p(addr,0x70); \
1233 inb_p(0x71); \
1234 })
1235 
1236 static struct floppy_struct *find_base(int drive,int code)
     /* [previous][next][first][last][top][bottom][index][help] */
1237 {
1238         struct floppy_struct *base;
1239 
1240         if (code > 0 && code < 5) {
1241                 base = &floppy_types[(code-1)*2];
1242                 printk("fd%d is %s",drive,base->name);
1243                 return base;
1244         }
1245         printk("fd%d is unknown type %d",drive,code);
1246         return NULL;
1247 }
1248 
1249 static void config_types(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1250 {
1251         printk("Floppy drive(s): ");
1252         base_type[0] = find_base(0,(CMOS_READ(0x10) >> 4) & 15);
1253         if (((CMOS_READ(0x14) >> 6) & 1) == 0)
1254                 base_type[1] = NULL;
1255         else {
1256                 printk(", ");
1257                 base_type[1] = find_base(1,CMOS_READ(0x10) & 15);
1258         }
1259         base_type[2] = base_type[3] = NULL;
1260         printk("\n");
1261 }
1262 
1263 /*
1264  * floppy_open check for aliasing (/dev/fd0 can be the same as
1265  * /dev/PS0 etc), and disallows simultaneous access to the same
1266  * drive with different device numbers.
1267  */
1268 static int floppy_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1269 {
1270         int drive;
1271         int old_dev;
1272 
1273         drive = inode->i_rdev & 3;
1274         old_dev = fd_device[drive];
1275         if (fd_ref[drive])
1276                 if (old_dev != inode->i_rdev)
1277                         return -EBUSY;
1278         fd_ref[drive]++;
1279         fd_device[drive] = inode->i_rdev;
1280         buffer_drive = buffer_track = -1;
1281         if (old_dev && old_dev != inode->i_rdev)
1282                 invalidate_buffers(old_dev);
1283         if (filp && filp->f_mode)
1284                 check_disk_change(inode->i_rdev);
1285         return 0;
1286 }
1287 
1288 static void floppy_release(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1289 {
1290         sync_dev(inode->i_rdev);
1291         if (!fd_ref[inode->i_rdev & 3]--) {
1292                 printk("floppy_release with fd_ref == 0");
1293                 fd_ref[inode->i_rdev & 3] = 0;
1294         }
1295 }
1296 
1297 static struct file_operations floppy_fops = {
1298         NULL,                   /* lseek - default */
1299         block_read,             /* read - general block-dev read */
1300         block_write,            /* write - general block-dev write */
1301         NULL,                   /* readdir - bad */
1302         NULL,                   /* select */
1303         fd_ioctl,               /* ioctl */
1304         NULL,                   /* mmap */
1305         floppy_open,            /* open */
1306         floppy_release,         /* release */
1307         block_fsync             /* fsync */
1308 };
1309 
1310 
1311 /*
1312  * The version command is not supposed to generate an interrupt, but
1313  * my FDC does, except when booting in SVGA screen mode.
1314  * When it does generate an interrupt, it doesn't return any status bytes.
1315  * It appears to have something to do with the version command...
1316  *
1317  * This should never be called, because of the reset after the version check.
1318  */
1319 static void ignore_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1320 {
1321         printk(DEVICE_NAME ": weird interrupt ignored (%d)\n", result());
1322         reset = 1;
1323         CLEAR_INTR;     /* ignore only once */
1324 }
1325 
1326 
1327 static void floppy_interrupt(int unused)
     /* [previous][next][first][last][top][bottom][index][help] */
1328 {
1329         void (*handler)(void) = DEVICE_INTR;
1330 
1331         DEVICE_INTR = NULL;
1332         if (!handler)
1333                 handler = unexpected_floppy_interrupt;
1334         handler();
1335 }
1336 
1337 /*
1338  * This is the floppy IRQ description. The SA_INTERRUPT in sa_flags
1339  * means we run the IRQ-handler with interrupts disabled.
1340  */
1341 static struct sigaction floppy_sigaction = {
1342         floppy_interrupt,
1343         0,
1344         SA_INTERRUPT,
1345         NULL
1346 };
1347 
1348 void floppy_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1349 {
1350         outb(current_DOR,FD_DOR);
1351         if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
1352                 printk("Unable to get major %d for floppy\n",MAJOR_NR);
1353                 return;
1354         }
1355         blk_size[MAJOR_NR] = floppy_sizes;
1356         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1357         timer_table[FLOPPY_TIMER].fn = floppy_shutdown;
1358         timer_active &= ~(1 << FLOPPY_TIMER);
1359         config_types();
1360         if (irqaction(FLOPPY_IRQ,&floppy_sigaction))
1361                 printk("Unable to grab IRQ%d for the floppy driver\n", FLOPPY_IRQ);
1362         if (request_dma(FLOPPY_DMA))
1363                 printk("Unable to grab DMA%d for the floppy driver\n", FLOPPY_DMA);
1364         /* Try to determine the floppy controller type */
1365         DEVICE_INTR = ignore_interrupt; /* don't ask ... */
1366         output_byte(FD_VERSION);        /* get FDC version code */
1367         if (result() != 1) {
1368                 printk(DEVICE_NAME ": FDC failed to return version byte\n");
1369                 fdc_version = FDC_TYPE_STD;
1370         } else
1371                 fdc_version = reply_buffer[0];
1372         if (fdc_version != FDC_TYPE_STD) 
1373                 printk(DEVICE_NAME ": FDC version 0x%x\n", fdc_version);
1374 #ifndef FDC_FIFO_UNTESTED
1375         fdc_version = FDC_TYPE_STD;     /* force std fdc type; can't test other. */
1376 #endif
1377 
1378         /* Not all FDCs seem to be able to handle the version command
1379          * properly, so force a reset for the standard FDC clones,
1380          * to avoid interrupt garbage.
1381          */
1382 
1383         if (fdc_version == FDC_TYPE_STD) {
1384                 initial_reset_flag = 1;
1385                 reset_floppy();
1386         }
1387 }

/* [previous][next][first][last][top][bottom][index][help] */