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

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