Annotation of Gnu-Mach/linux/src/drivers/block/floppy.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *  linux/kernel/floppy.c
                      3:  *
                      4:  *  Copyright (C) 1991, 1992  Linus Torvalds
                      5:  *  Copyright (C) 1993, 1994  Alain Knaff
                      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 [email protected] (Lawrence Foard). Linus.
                     33:  */
                     34: 
                     35: /*
                     36:  * Automatic floppy-detection and formatting written by Werner Almesberger
                     37:  * ([email protected]), 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 ([email protected])
                     53:  * modeled 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: /* 1994/6/24 --bbroad-- added the floppy table entries and made
                     68:  * minor modifications to allow 2.88 floppies to be run.
                     69:  */
                     70: 
                     71: /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
                     72:  * disk types.
                     73:  */
                     74: 
                     75: /*
                     76:  * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
                     77:  * format bug fixes, but unfortunately some new bugs too...
                     78:  */
                     79: 
                     80: /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
                     81:  * errors to allow safe writing by specialized programs.
                     82:  */
                     83: 
                     84: /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
                     85:  * by defining bit 1 of the "stretch" parameter to mean put sectors on the
                     86:  * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
                     87:  * drives are "upside-down").
                     88:  */
                     89: 
                     90: /*
                     91:  * 1995/8/26 -- Andreas Busse -- added Mips support.
                     92:  */
                     93: 
                     94: /*
                     95:  * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
                     96:  * features to asm/floppy.h.
                     97:  */
                     98: 
                     99: 
                    100: #define FLOPPY_SANITY_CHECK
                    101: #undef  FLOPPY_SILENT_DCL_CLEAR
                    102: 
                    103: #define REALLY_SLOW_IO
                    104: 
                    105: #define DEBUGT 2
                    106: #define DCL_DEBUG /* debug disk change line */
                    107: 
                    108: /* do print messages for unexpected interrupts */
                    109: static int print_unex=1;
                    110: #include <linux/utsname.h>
                    111: #include <linux/module.h>
                    112: 
                    113: /* the following is the mask of allowed drives. By default units 2 and
                    114:  * 3 of both floppy controllers are disabled, because switching on the
                    115:  * motor of these drives causes system hangs on some PCI computers. drive
                    116:  * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
                    117:  * a drive is allowed. */
                    118: static int FLOPPY_IRQ=6;
                    119: static int FLOPPY_DMA=2;
                    120: static int allowed_drive_mask = 0x33;
                    121: 
                    122: static int irqdma_allocated = 0; 
                    123: 
                    124: #include <linux/sched.h>
                    125: #include <linux/fs.h>
                    126: #include <linux/kernel.h>
                    127: #include <linux/timer.h>
                    128: #include <linux/tqueue.h>
                    129: #define FDPATCHES
                    130: #include <linux/fdreg.h>
                    131: 
                    132: 
                    133: #include <linux/fd.h>
                    134: 
                    135: 
                    136: #define OLDFDRAWCMD 0x020d /* send a raw command to the FDC */
                    137: 
                    138: struct old_floppy_raw_cmd {
                    139:   void *data;
                    140:   long length;
                    141: 
                    142:   unsigned char rate;
                    143:   unsigned char flags;
                    144:   unsigned char cmd_count;
                    145:   unsigned char cmd[9];
                    146:   unsigned char reply_count;
                    147:   unsigned char reply[7];
                    148:   int track;
                    149: };
                    150: 
                    151: #include <linux/errno.h>
                    152: #include <linux/malloc.h>
                    153: #include <linux/mm.h>
                    154: #include <linux/string.h>
                    155: #include <linux/fcntl.h>
                    156: #include <linux/delay.h>
                    157: #include <linux/mc146818rtc.h> /* CMOS defines */
                    158: #include <linux/ioport.h>
                    159: #include <linux/interrupt.h>
                    160: 
                    161: #include <asm/dma.h>
                    162: #include <asm/irq.h>
                    163: #include <asm/system.h>
                    164: #include <asm/io.h>
                    165: #include <asm/segment.h>
                    166: 
                    167: static int use_virtual_dma=0; /* virtual DMA for Intel */
                    168: static unsigned short virtual_dma_port=0x3f0;
                    169: void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
                    170: static int set_dor(int fdc, char mask, char data);
                    171: static inline int __get_order(unsigned long size);
                    172: #include <asm/floppy.h>
                    173: 
                    174: 
                    175: #define MAJOR_NR FLOPPY_MAJOR
                    176: 
                    177: #include <linux/blk.h>
                    178: #include <linux/cdrom.h> /* for the compatibility eject ioctl */
                    179: 
                    180: 
                    181: #ifndef FLOPPY_MOTOR_MASK
                    182: #define FLOPPY_MOTOR_MASK 0xf0
                    183: #endif
                    184: 
                    185: #ifndef fd_get_dma_residue
                    186: #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
                    187: #endif
                    188: 
                    189: /* Dma Memory related stuff */
                    190: 
                    191: /* Pure 2^n version of get_order */
                    192: static inline int __get_order(unsigned long size)
                    193: {
                    194:        int order;
                    195: 
                    196:        size = (size-1) >> (PAGE_SHIFT-1);
                    197:        order = -1;
                    198:        do {
                    199:                size >>= 1;
                    200:                order++;
                    201:        } while (size);
                    202:        return order;
                    203: }
                    204: 
                    205: #ifndef fd_dma_mem_free
                    206: #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
                    207: #endif
                    208: 
                    209: #ifndef fd_dma_mem_alloc
                    210: #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
                    211: #endif
                    212: 
                    213: /* End dma memory related stuff */
                    214: 
                    215: static unsigned int fake_change = 0;
                    216: static int initialising=1;
                    217: 
                    218: static inline int TYPE(kdev_t x) {
                    219:        return  (MINOR(x)>>2) & 0x1f;
                    220: }
                    221: static inline int DRIVE(kdev_t x) {
                    222:        return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
                    223: }
                    224: #define ITYPE(x) (((x)>>2) & 0x1f)
                    225: #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
                    226: #define UNIT(x) ((x) & 0x03)           /* drive on fdc */
                    227: #define FDC(x) (((x) & 0x04) >> 2)  /* fdc of drive */
                    228: #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
                    229:                                /* reverse mapping from unit and fdc to drive */
                    230: #define DP (&drive_params[current_drive])
                    231: #define DRS (&drive_state[current_drive])
                    232: #define DRWE (&write_errors[current_drive])
                    233: #define FDCS (&fdc_state[fdc])
                    234: #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
                    235: #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
                    236: #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
                    237: 
                    238: #define UDP (&drive_params[drive])
                    239: #define UDRS (&drive_state[drive])
                    240: #define UDRWE (&write_errors[drive])
                    241: #define UFDCS (&fdc_state[FDC(drive)])
                    242: #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
                    243: #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
                    244: #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
                    245: 
                    246: #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
                    247: 
                    248: #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
                    249: #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
                    250: 
                    251: #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
                    252: 
                    253: #define INT_OFF save_flags(flags); cli()
                    254: #define INT_ON  restore_flags(flags)
                    255: 
                    256: /* read/write */
                    257: #define COMMAND raw_cmd->cmd[0]
                    258: #define DR_SELECT raw_cmd->cmd[1]
                    259: #define TRACK raw_cmd->cmd[2]
                    260: #define HEAD raw_cmd->cmd[3]
                    261: #define SECTOR raw_cmd->cmd[4]
                    262: #define SIZECODE raw_cmd->cmd[5]
                    263: #define SECT_PER_TRACK raw_cmd->cmd[6]
                    264: #define GAP raw_cmd->cmd[7]
                    265: #define SIZECODE2 raw_cmd->cmd[8]
                    266: #define NR_RW 9
                    267: 
                    268: /* format */
                    269: #define F_SIZECODE raw_cmd->cmd[2]
                    270: #define F_SECT_PER_TRACK raw_cmd->cmd[3]
                    271: #define F_GAP raw_cmd->cmd[4]
                    272: #define F_FILL raw_cmd->cmd[5]
                    273: #define NR_F 6
                    274: 
                    275: /*
                    276:  * Maximum disk size (in kilobytes). This default is used whenever the
                    277:  * current disk size is unknown.
                    278:  * [Now it is rather a minimum]
                    279:  */
                    280: #define MAX_DISK_SIZE 4 /* 3984*/
                    281: 
                    282: #define K_64   0x10000         /* 64KB */
                    283: 
                    284: /*
                    285:  * globals used by 'result()'
                    286:  */
                    287: #define MAX_REPLIES 16
                    288: static unsigned char reply_buffer[MAX_REPLIES];
                    289: static int inr; /* size of reply buffer, when called from interrupt */
                    290: #define ST0 (reply_buffer[0])
                    291: #define ST1 (reply_buffer[1])
                    292: #define ST2 (reply_buffer[2])
                    293: #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
                    294: #define R_TRACK (reply_buffer[3])
                    295: #define R_HEAD (reply_buffer[4])
                    296: #define R_SECTOR (reply_buffer[5])
                    297: #define R_SIZECODE (reply_buffer[6])
                    298: 
                    299: #define SEL_DLY (2*HZ/100)
                    300: 
                    301: #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
                    302: /*
                    303:  * this struct defines the different floppy drive types.
                    304:  */
                    305: static struct {
                    306:        struct floppy_drive_params params;
                    307:        const char *name; /* name printed while booting */
                    308: } default_drive_params[]= {
                    309: /* NOTE: the time values in jiffies should be in msec!
                    310:  CMOS drive type
                    311:   |     Maximum data rate supported by drive type
                    312:   |     |   Head load time, msec
                    313:   |     |   |   Head unload time, msec (not used)
                    314:   |     |   |   |     Step rate interval, usec
                    315:   |     |   |   |     |       Time needed for spinup time (jiffies)
                    316:   |     |   |   |     |       |      Timeout for spinning down (jiffies)
                    317:   |     |   |   |     |       |      |   Spindown offset (where disk stops)
                    318:   |     |   |   |     |       |      |   |     Select delay
                    319:   |     |   |   |     |       |      |   |     |     RPS
                    320:   |     |   |   |     |       |      |   |     |     |    Max number of tracks
                    321:   |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
                    322:   |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
                    323:   |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
                    324: {{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
                    325:       0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
                    326: 
                    327: {{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
                    328:       0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
                    329: 
                    330: {{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
                    331:       0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
                    332: 
                    333: {{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
                    334:       0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
                    335: 
                    336: {{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
                    337:       0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
                    338: 
                    339: {{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
                    340:       0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
                    341: 
                    342: {{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
                    343:       0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
                    344: /*    |  --autodetected formats---    |      |      |
                    345:  *    read_track                      |      |    Name printed when booting
                    346:  *                                   |     Native format
                    347:  *                 Frequency of disk change checks */
                    348: };
                    349: 
                    350: static struct floppy_drive_params drive_params[N_DRIVE];
                    351: static struct floppy_drive_struct drive_state[N_DRIVE];
                    352: static struct floppy_write_errors write_errors[N_DRIVE];
                    353: static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
                    354: 
                    355: /*
                    356:  * This struct defines the different floppy types.
                    357:  *
                    358:  * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
                    359:  * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
                    360:  * tells if the disk is in Commodore 1581 format, which means side 0 sectors
                    361:  * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
                    362:  * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
                    363:  * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
                    364:  * side 0 is on physical side 0 (but with the misnamed sector IDs).
                    365:  * 'stretch' should probably be renamed to something more general, like
                    366:  * 'options'.  Other parameters should be self-explanatory (see also
                    367:  * setfdprm(8)).
                    368:  */
                    369: static struct floppy_struct floppy_type[32] = {
                    370:        {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    }, /*  0 no testing    */
                    371:        {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
                    372:        { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /*  2 1.2MB AT      */
                    373:        {  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  }, /*  3 360KB SS 3.5" */
                    374:        { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  }, /*  4 720KB 3.5"    */
                    375:        {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  }, /*  5 360KB AT      */
                    376:        { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  }, /*  6 720KB AT      */
                    377:        { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /*  7 1.44MB 3.5"   */
                    378:        { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /*  8 2.88MB 3.5"   */
                    379:        { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"},  /*  9 3.12MB 3.5"   */
                    380: 
                    381:        { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
                    382:        { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
                    383:        {  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  }, /* 12 410KB 5.25"   */
                    384:        { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  }, /* 13 820KB 3.5"    */
                    385:        { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25"  */
                    386:        { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5"   */
                    387:        {  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  }, /* 16 420KB 5.25"   */
                    388:        { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  }, /* 17 830KB 3.5"    */
                    389:        { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25"  */
                    390:        { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
                    391: 
                    392:        { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
                    393:        { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
                    394:        { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
                    395:        { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
                    396:        { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
                    397:        { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
                    398:        { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
                    399:        { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
                    400:        { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
                    401: 
                    402:        { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
                    403:        { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  }, /* 30 800KB 3.5"    */
                    404:        { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
                    405: };
                    406: 
                    407: #define        NUMBER(x)       (sizeof(x) / sizeof(*(x)))
                    408: #define SECTSIZE (_FD_SECTSIZE(*floppy))
                    409: 
                    410: /* Auto-detection: Disk type used until the next media change occurs. */
                    411: static struct floppy_struct *current_type[N_DRIVE] = {
                    412:        NULL, NULL, NULL, NULL,
                    413:        NULL, NULL, NULL, NULL
                    414: };
                    415: 
                    416: /*
                    417:  * User-provided type information. current_type points to
                    418:  * the respective entry of this array.
                    419:  */
                    420: static struct floppy_struct user_params[N_DRIVE];
                    421: 
                    422: static int floppy_sizes[256];
                    423: static int floppy_blocksizes[256] = { 0, };
                    424: 
                    425: /*
                    426:  * The driver is trying to determine the correct media format
                    427:  * while probing is set. rw_interrupt() clears it after a
                    428:  * successful access.
                    429:  */
                    430: static int probing = 0;
                    431: 
                    432: /* Synchronization of FDC access. */
                    433: #define FD_COMMAND_NONE -1
                    434: #define FD_COMMAND_ERROR 2
                    435: #define FD_COMMAND_OKAY 3
                    436: 
                    437: static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
                    438: static struct wait_queue *fdc_wait = NULL, *command_done = NULL;
                    439: #define NO_SIGNAL (!(current->signal & ~current->blocked) || !interruptible)
                    440: #define CALL(x) if ((x) == -EINTR) return -EINTR
                    441: #define ECALL(x) if ((ret = (x))) return ret;
                    442: #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
                    443: #define WAIT(x) _WAIT((x),interruptible)
                    444: #define IWAIT(x) _WAIT((x),1)
                    445: 
                    446: /* Errors during formatting are counted here. */
                    447: static int format_errors;
                    448: 
                    449: /* Format request descriptor. */
                    450: static struct format_descr format_req;
                    451: 
                    452: /*
                    453:  * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
                    454:  * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
                    455:  * H is head unload time (1=16ms, 2=32ms, etc)
                    456:  */
                    457: 
                    458: /*
                    459:  * Track buffer
                    460:  * Because these are written to by the DMA controller, they must
                    461:  * not contain a 64k byte boundary crossing, or data will be
                    462:  * corrupted/lost.
                    463:  */
                    464: static char *floppy_track_buffer=0;
                    465: static int max_buffer_sectors=0;
                    466: 
                    467: static int *errors;
                    468: typedef void (*done_f)(int);
                    469: static struct cont_t {
                    470:        void (*interrupt)(void); /* this is called after the interrupt of the
                    471:                                  * main command */
                    472:        void (*redo)(void); /* this is called to retry the operation */
                    473:        void (*error)(void); /* this is called to tally an error */
                    474:        done_f done; /* this is called to say if the operation has 
                    475:                      * succeeded/failed */
                    476: } *cont=NULL;
                    477: 
                    478: static void floppy_ready(void);
                    479: static void floppy_start(void);
                    480: static void process_fd_request(void);
                    481: static void recalibrate_floppy(void);
                    482: static void floppy_shutdown(void);
                    483: 
                    484: static int floppy_grab_irq_and_dma(void);
                    485: static void floppy_release_irq_and_dma(void);
                    486: 
                    487: /*
                    488:  * The "reset" variable should be tested whenever an interrupt is scheduled,
                    489:  * after the commands have been sent. This is to ensure that the driver doesn't
                    490:  * get wedged when the interrupt doesn't come because of a failed command.
                    491:  * reset doesn't need to be tested before sending commands, because
                    492:  * output_byte is automatically disabled when reset is set.
                    493:  */
                    494: #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
                    495: static void reset_fdc(void);
                    496: 
                    497: /*
                    498:  * These are global variables, as that's the easiest way to give
                    499:  * information to interrupts. They are the data used for the current
                    500:  * request.
                    501:  */
                    502: #define NO_TRACK -1
                    503: #define NEED_1_RECAL -2
                    504: #define NEED_2_RECAL -3
                    505: 
                    506: /* */
                    507: static int usage_count = 0;
                    508: 
                    509: 
                    510: /* buffer related variables */
                    511: static int buffer_track = -1;
                    512: static int buffer_drive = -1;
                    513: static int buffer_min = -1;
                    514: static int buffer_max = -1;
                    515: 
                    516: /* fdc related variables, should end up in a struct */
                    517: static struct floppy_fdc_state fdc_state[N_FDC];
                    518: static int fdc; /* current fdc */
                    519: 
                    520: static struct floppy_struct *_floppy = floppy_type;
                    521: static unsigned char current_drive = 0;
                    522: static long current_count_sectors = 0;
                    523: static unsigned char sector_t; /* sector in track */
                    524: 
                    525: #ifndef fd_eject
                    526: #define fd_eject(x) -EINVAL
                    527: #endif
                    528: 
                    529: 
                    530: #ifdef DEBUGT
                    531: static long unsigned debugtimer;
                    532: #endif
                    533: 
                    534: /*
                    535:  * Debugging
                    536:  * =========
                    537:  */
                    538: static inline void set_debugt(void)
                    539: {
                    540: #ifdef DEBUGT
                    541:        debugtimer = jiffies;
                    542: #endif
                    543: }
                    544: 
                    545: static inline void debugt(const char *message)
                    546: {
                    547: #ifdef DEBUGT
                    548:        if (DP->flags & DEBUGT)
                    549:                printk("%s dtime=%lu\n", message, jiffies-debugtimer);
                    550: #endif
                    551: }
                    552: 
                    553: typedef void (*timeout_fn)(unsigned long);
                    554: static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
                    555:                                       (timeout_fn) floppy_shutdown };
                    556: 
                    557: static const char *timeout_message;
                    558: 
                    559: #ifdef FLOPPY_SANITY_CHECK
                    560: static void is_alive(const char *message)
                    561: {
                    562:        /* this routine checks whether the floppy driver is "alive" */
                    563:        if (fdc_busy && command_status < 2 && !fd_timeout.prev){
                    564:                DPRINT("timeout handler died: %s\n",message);
                    565:        }
                    566: }
                    567: #endif
                    568: 
                    569: #ifdef FLOPPY_SANITY_CHECK
                    570: 
                    571: #define OLOGSIZE 20
                    572: 
                    573: static void (*lasthandler)(void) = NULL;
                    574: static int interruptjiffies=0;
                    575: static int resultjiffies=0;
                    576: static int resultsize=0;
                    577: static int lastredo=0;
                    578: 
                    579: static struct output_log {
                    580:        unsigned char data;
                    581:        unsigned char status;
                    582:        unsigned long jiffies;
                    583: } output_log[OLOGSIZE];
                    584: 
                    585: static int output_log_pos=0;
                    586: #endif
                    587: 
                    588: #define CURRENTD -1
                    589: #define MAXTIMEOUT -2
                    590: 
                    591: static void reschedule_timeout(int drive, const char *message, int marg)
                    592: {
                    593:        if (drive == CURRENTD)
                    594:                drive = current_drive;
                    595:        del_timer(&fd_timeout);
                    596:        if (drive < 0 || drive > N_DRIVE) {
                    597:                fd_timeout.expires = jiffies + 20*HZ;
                    598:                drive=0;
                    599:        } else
                    600:                fd_timeout.expires = jiffies + UDP->timeout;
                    601:        add_timer(&fd_timeout);
                    602:        if (UDP->flags & FD_DEBUG){
                    603:                DPRINT("reschedule timeout ");
                    604:                printk(message, marg);
                    605:                printk("\n");
                    606:        }
                    607:        timeout_message = message;
                    608: }
                    609: 
                    610: static int maximum(int a, int b)
                    611: {
                    612:        if(a > b)
                    613:                return a;
                    614:        else
                    615:                return b;
                    616: }
                    617: #define INFBOUND(a,b) (a)=maximum((a),(b));
                    618: 
                    619: static int minimum(int a, int b)
                    620: {
                    621:        if(a < b)
                    622:                return a;
                    623:        else
                    624:                return b;
                    625: }
                    626: #define SUPBOUND(a,b) (a)=minimum((a),(b));
                    627: 
                    628: 
                    629: /*
                    630:  * Bottom half floppy driver.
                    631:  * ==========================
                    632:  *
                    633:  * This part of the file contains the code talking directly to the hardware,
                    634:  * and also the main service loop (seek-configure-spinup-command)
                    635:  */
                    636: 
                    637: /*
                    638:  * disk change.
                    639:  * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
                    640:  * and the last_checked date.
                    641:  *
                    642:  * last_checked is the date of the last check which showed 'no disk change'
                    643:  * FD_DISK_CHANGE is set under two conditions:
                    644:  * 1. The floppy has been changed after some i/o to that floppy already
                    645:  *    took place.
                    646:  * 2. No floppy disk is in the drive. This is done in order to ensure that
                    647:  *    requests are quickly flushed in case there is no disk in the drive. It
                    648:  *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
                    649:  *    the drive.
                    650:  *
                    651:  * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
                    652:  * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
                    653:  *  each seek. If a disk is present, the disk change line should also be
                    654:  *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
                    655:  *  change line is set, this means either that no disk is in the drive, or
                    656:  *  that it has been removed since the last seek.
                    657:  *
                    658:  * This means that we really have a third possibility too:
                    659:  *  The floppy has been changed after the last seek.
                    660:  */
                    661: 
                    662: static int disk_change(int drive)
                    663: {
                    664:        int fdc=FDC(drive);
                    665: #ifdef FLOPPY_SANITY_CHECK
                    666:        if (jiffies - UDRS->select_date < UDP->select_delay)
                    667:                DPRINT("WARNING disk change called early\n");
                    668:        if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
                    669:           (FDCS->dor & 3) != UNIT(drive) ||
                    670:           fdc != FDC(drive)){
                    671:                DPRINT("probing disk change on unselected drive\n");
                    672:                DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
                    673:                        FDCS->dor);
                    674:        }
                    675: #endif
                    676: 
                    677: #ifdef DCL_DEBUG
                    678:        if (UDP->flags & FD_DEBUG){
                    679:                DPRINT("checking disk change line for drive %d\n",drive);
                    680:                DPRINT("jiffies=%ld\n", jiffies);
                    681:                DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
                    682:                DPRINT("flags=%x\n",UDRS->flags);
                    683:        }
                    684: #endif
                    685:        if (UDP->flags & FD_BROKEN_DCL)
                    686:                return UTESTF(FD_DISK_CHANGED);
                    687:        if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
                    688:                USETF(FD_VERIFY); /* verify write protection */
                    689:                if (UDRS->maxblock){
                    690:                        /* mark it changed */
                    691:                        USETF(FD_DISK_CHANGED);
                    692:                }
                    693: 
                    694:                /* invalidate its geometry */
                    695:                if (UDRS->keep_data >= 0) {
                    696:                        if ((UDP->flags & FTD_MSG) &&
                    697:                            current_type[drive] != NULL)
                    698:                                DPRINT("Disk type is undefined after "
                    699:                                       "disk change\n");
                    700:                        current_type[drive] = NULL;
                    701:                        floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
                    702:                }
                    703: 
                    704:                /*USETF(FD_DISK_NEWCHANGE);*/
                    705:                return 1;
                    706:        } else {
                    707:                UDRS->last_checked=jiffies;
                    708:                UCLEARF(FD_DISK_NEWCHANGE);
                    709:        }
                    710:        return 0;
                    711: }
                    712: 
                    713: static inline int is_selected(int dor, int unit)
                    714: {
                    715:        return ((dor  & (0x10 << unit)) && (dor &3) == unit);
                    716: }
                    717: 
                    718: static int set_dor(int fdc, char mask, char data)
                    719: {
                    720:        register unsigned char drive, unit, newdor,olddor;
                    721: 
                    722:        if (FDCS->address == -1)
                    723:                return -1;
                    724: 
                    725:        olddor = FDCS->dor;
                    726:        newdor =  (olddor & mask) | data;
                    727:        if (newdor != olddor){
                    728:                unit = olddor & 0x3;
                    729:                if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
                    730:                        drive = REVDRIVE(fdc,unit);
                    731: #ifdef DCL_DEBUG
                    732:                        if (UDP->flags & FD_DEBUG){
                    733:                                DPRINT("calling disk change from set_dor\n");
                    734:                        }
                    735: #endif
                    736:                        disk_change(drive);
                    737:                }
                    738:                FDCS->dor = newdor;
                    739:                fd_outb(newdor, FD_DOR);
                    740: 
                    741:                unit = newdor & 0x3;
                    742:                if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
                    743:                        drive = REVDRIVE(fdc,unit);
                    744:                        UDRS->select_date = jiffies;
                    745:                }
                    746:        }
                    747:        
                    748:        /* FIXME: we should be more graceful here */
                    749:        
                    750:        if (newdor & FLOPPY_MOTOR_MASK)
                    751:                floppy_grab_irq_and_dma();
                    752:        if (olddor & FLOPPY_MOTOR_MASK)
                    753:                floppy_release_irq_and_dma();
                    754:        return olddor;
                    755: }
                    756: 
                    757: static void twaddle(void)
                    758: {
                    759:        if (DP->select_delay)
                    760:                return;
                    761:        fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)),FD_DOR);
                    762:        fd_outb(FDCS->dor, FD_DOR);
                    763:        DRS->select_date = jiffies;
                    764: }
                    765: 
                    766: /* reset all driver information about the current fdc. This is needed after
                    767:  * a reset, and after a raw command. */
                    768: static void reset_fdc_info(int mode)
                    769: {
                    770:        int drive;
                    771: 
                    772:        FDCS->spec1 = FDCS->spec2 = -1;
                    773:        FDCS->need_configure = 1;
                    774:        FDCS->perp_mode = 1;
                    775:        FDCS->rawcmd = 0;
                    776:        for (drive = 0; drive < N_DRIVE; drive++)
                    777:                if (FDC(drive) == fdc &&
                    778:                    (mode || UDRS->track != NEED_1_RECAL))
                    779:                        UDRS->track = NEED_2_RECAL;
                    780: }
                    781: 
                    782: /* selects the fdc and drive, and enables the fdc's input/dma. */
                    783: static void set_fdc(int drive)
                    784: {
                    785:        if (drive >= 0 && drive < N_DRIVE){
                    786:                fdc = FDC(drive);
                    787:                current_drive = drive;
                    788:        }
                    789:        if (fdc != 1 && fdc != 0) {
                    790:                printk("bad fdc value\n");
                    791:                return;
                    792:        }
                    793:        set_dor(fdc,~0,8);
                    794: #if N_FDC > 1
                    795:        set_dor(1-fdc, ~8, 0);
                    796: #endif
                    797:        if (FDCS->rawcmd == 2)
                    798:                reset_fdc_info(1);
                    799:        if (fd_inb(FD_STATUS) != STATUS_READY)
                    800:                FDCS->reset = 1;
                    801: }
                    802: 
                    803: /* locks the driver */
                    804: static int lock_fdc(int drive, int interruptible)
                    805: {
                    806:        unsigned long flags;
                    807: 
                    808:        if (!usage_count){
                    809:                printk(KERN_ERR "trying to lock fdc while usage count=0\n");
                    810:                return -1;
                    811:        }
                    812:        if(floppy_grab_irq_and_dma()==-1)
                    813:                return -EBUSY;
                    814:        INT_OFF;
                    815:        while (fdc_busy && NO_SIGNAL)
                    816:                interruptible_sleep_on(&fdc_wait);
                    817:        if (fdc_busy){
                    818:                INT_ON;
                    819:                return -EINTR;
                    820:        }
                    821:        fdc_busy = 1;
                    822:        INT_ON;
                    823:        command_status = FD_COMMAND_NONE;
                    824:        reschedule_timeout(drive, "lock fdc", 0);
                    825:        set_fdc(drive);
                    826:        return 0;
                    827: }
                    828: 
                    829: #define LOCK_FDC(drive,interruptible) \
                    830: if (lock_fdc(drive,interruptible)) return -EINTR;
                    831: 
                    832: 
                    833: /* unlocks the driver */
                    834: static inline void unlock_fdc(void)
                    835: {
                    836:        raw_cmd = 0;
                    837:        if (!fdc_busy)
                    838:                DPRINT("FDC access conflict!\n");
                    839: 
                    840:        if (DEVICE_INTR)
                    841:                DPRINT("device interrupt still active at FDC release: %p!\n",
                    842:                        DEVICE_INTR);
                    843:        command_status = FD_COMMAND_NONE;
                    844:        del_timer(&fd_timeout);
                    845:        cont = NULL;
                    846:        fdc_busy = 0;
                    847:        floppy_release_irq_and_dma();
                    848:        wake_up(&fdc_wait);
                    849: }
                    850: 
                    851: /* switches the motor off after a given timeout */
                    852: static void motor_off_callback(unsigned long nr)
                    853: {
                    854:        unsigned char mask = ~(0x10 << UNIT(nr));
                    855: 
                    856:        set_dor(FDC(nr), mask, 0);
                    857: }
                    858: 
                    859: static struct timer_list motor_off_timer[N_DRIVE] = {
                    860:        { NULL, NULL, 0, 0, motor_off_callback },
                    861:        { NULL, NULL, 0, 1, motor_off_callback },
                    862:        { NULL, NULL, 0, 2, motor_off_callback },
                    863:        { NULL, NULL, 0, 3, motor_off_callback },
                    864:        { NULL, NULL, 0, 4, motor_off_callback },
                    865:        { NULL, NULL, 0, 5, motor_off_callback },
                    866:        { NULL, NULL, 0, 6, motor_off_callback },
                    867:        { NULL, NULL, 0, 7, motor_off_callback }
                    868: };
                    869: 
                    870: /* schedules motor off */
                    871: static void floppy_off(unsigned int drive)
                    872: {
                    873:        unsigned long volatile delta;
                    874:        register int fdc=FDC(drive);
                    875: 
                    876:        if (!(FDCS->dor & (0x10 << UNIT(drive))))
                    877:                return;
                    878: 
                    879:        del_timer(motor_off_timer+drive);
                    880: 
                    881:        /* make spindle stop in a position which minimizes spinup time
                    882:         * next time */
                    883:        if (UDP->rps){
                    884:                delta = jiffies - UDRS->first_read_date + HZ -
                    885:                        UDP->spindown_offset;
                    886:                delta = ((delta * UDP->rps) % HZ) / UDP->rps;
                    887:                motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
                    888:        }
                    889:        add_timer(motor_off_timer+drive);
                    890: }
                    891: 
                    892: /*
                    893:  * cycle through all N_DRIVE floppy drives, for disk change testing.
                    894:  * stopping at current drive. This is done before any long operation, to
                    895:  * be sure to have up to date disk change information.
                    896:  */
                    897: static void scandrives(void)
                    898: {
                    899:        int i, drive, saved_drive;
                    900: 
                    901:        if (DP->select_delay)
                    902:                return;
                    903: 
                    904:        saved_drive = current_drive;
                    905:        for (i=0; i < N_DRIVE; i++){
                    906:                drive = (saved_drive + i + 1) % N_DRIVE;
                    907:                if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
                    908:                        continue; /* skip closed drives */
                    909:                set_fdc(drive);
                    910:                if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
                    911:                      (0x10 << UNIT(drive))))
                    912:                        /* switch the motor off again, if it was off to
                    913:                         * begin with */
                    914:                        set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
                    915:        }
                    916:        set_fdc(saved_drive);
                    917: }
                    918: 
                    919: static void empty(void)
                    920: {
                    921: }
                    922: 
                    923: static struct tq_struct floppy_tq =
                    924: { 0, 0, 0, 0 };
                    925: 
                    926: static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
                    927: 
                    928: static void cancel_activity(void)
                    929: {
                    930:        CLEAR_INTR;
                    931:        floppy_tq.routine = (void *)(void *) empty;
                    932:        del_timer(&fd_timer);
                    933: }
                    934: 
                    935: /* this function makes sure that the disk stays in the drive during the
                    936:  * transfer */
                    937: static void fd_watchdog(void)
                    938: {
                    939: #ifdef DCL_DEBUG
                    940:        if (DP->flags & FD_DEBUG){
                    941:                DPRINT("calling disk change from watchdog\n");
                    942:        }
                    943: #endif
                    944: 
                    945:        if (disk_change(current_drive)){
                    946:                DPRINT("disk removed during i/o\n");
                    947:                cancel_activity();
                    948:                cont->done(0);
                    949:                reset_fdc();
                    950:        } else {
                    951:                del_timer(&fd_timer);
                    952:                fd_timer.function = (timeout_fn) fd_watchdog;
                    953:                fd_timer.expires = jiffies + HZ / 10;
                    954:                add_timer(&fd_timer);
                    955:        }
                    956: }
                    957: 
                    958: static void main_command_interrupt(void)
                    959: {
                    960:        del_timer(&fd_timer);
                    961:        cont->interrupt();
                    962: }
                    963: 
                    964: /* waits for a delay (spinup or select) to pass */
                    965: static int wait_for_completion(int delay, timeout_fn function)
                    966: {
                    967:        if (FDCS->reset){
                    968:                reset_fdc(); /* do the reset during sleep to win time
                    969:                              * if we don't need to sleep, it's a good
                    970:                              * occasion anyways */
                    971:                return 1;
                    972:        }
                    973: 
                    974:        if ((signed) (jiffies - delay) < 0){
                    975:                del_timer(&fd_timer);
                    976:                fd_timer.function = function;
                    977:                fd_timer.expires = delay;
                    978:                add_timer(&fd_timer);
                    979:                return 1;
                    980:        }
                    981:        return 0;
                    982: }
                    983: 
                    984: static int hlt_disabled=0;
                    985: static void floppy_disable_hlt(void)
                    986: {
                    987:        unsigned long flags;
                    988: 
                    989:        INT_OFF;
                    990:        if (!hlt_disabled){
                    991:                hlt_disabled=1;
                    992: #ifdef HAVE_DISABLE_HLT
                    993:                disable_hlt();
                    994: #endif
                    995:        }
                    996:        INT_ON;
                    997: }
                    998: 
                    999: static void floppy_enable_hlt(void)
                   1000: {
                   1001:        unsigned long flags;
                   1002: 
                   1003:        INT_OFF;
                   1004:        if (hlt_disabled){
                   1005:                hlt_disabled=0;
                   1006: #ifdef HAVE_DISABLE_HLT
                   1007:                enable_hlt();
                   1008: #endif
                   1009:        }
                   1010:        INT_ON;
                   1011: }
                   1012: 
                   1013: 
                   1014: static void setup_DMA(void)
                   1015: {
                   1016:        unsigned long flags;
                   1017: 
                   1018: #ifdef FLOPPY_SANITY_CHECK
                   1019:        if (raw_cmd->length == 0){
                   1020:                int i;
                   1021: 
                   1022:                printk("zero dma transfer size:");
                   1023:                for (i=0; i < raw_cmd->cmd_count; i++)
                   1024:                        printk("%x,", raw_cmd->cmd[i]);
                   1025:                printk("\n");
                   1026:                cont->done(0);
                   1027:                FDCS->reset = 1;
                   1028:                return;
                   1029:        }
                   1030:        if ((long) raw_cmd->kernel_data % 512){
                   1031:                printk("non aligned address: %p\n", raw_cmd->kernel_data);
                   1032:                cont->done(0);
                   1033:                FDCS->reset=1;
                   1034:                return;
                   1035:        }
                   1036:        if (CROSS_64KB(raw_cmd->kernel_data, raw_cmd->length)) {
                   1037:                printk("DMA crossing 64-K boundary %p-%p\n",
                   1038:                       raw_cmd->kernel_data,
                   1039:                       raw_cmd->kernel_data + raw_cmd->length);
                   1040:                cont->done(0);
                   1041:                FDCS->reset=1;
                   1042:                return;
                   1043:        }
                   1044: #endif
                   1045:        INT_OFF;
                   1046:        fd_disable_dma();
                   1047:        fd_clear_dma_ff();
                   1048:        fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
                   1049:        fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
                   1050:                        DMA_MODE_READ : DMA_MODE_WRITE);
                   1051:        fd_set_dma_addr(virt_to_bus(raw_cmd->kernel_data));
                   1052:        fd_set_dma_count(raw_cmd->length);
                   1053:        virtual_dma_port = FDCS->address;
                   1054:        fd_enable_dma();
                   1055:        INT_ON;
                   1056:        floppy_disable_hlt();
                   1057: }
                   1058: 
                   1059: void show_floppy(void);
                   1060: 
                   1061: /* waits until the fdc becomes ready */
                   1062: static int wait_til_ready(void)
                   1063: {
                   1064:        int counter, status;
                   1065:        if(FDCS->reset)
                   1066:                return -1;
                   1067:        for (counter = 0; counter < 10000; counter++) {
                   1068:                status = fd_inb(FD_STATUS);             
                   1069:                if (status & STATUS_READY)
                   1070:                        return status;
                   1071:        }
                   1072:        if (!initialising) {
                   1073:                DPRINT("Getstatus times out (%x) on fdc %d\n",
                   1074:                        status, fdc);
                   1075:                show_floppy();
                   1076:        }
                   1077:        FDCS->reset = 1;
                   1078:        return -1;
                   1079: }
                   1080: 
                   1081: /* sends a command byte to the fdc */
                   1082: static int output_byte(char byte)
                   1083: {
                   1084:        int status;
                   1085: 
                   1086:        if ((status = wait_til_ready()) < 0)
                   1087:                return -1;
                   1088:        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
                   1089:                fd_outb(byte,FD_DATA);
                   1090: #ifdef FLOPPY_SANITY_CHECK
                   1091:                output_log[output_log_pos].data = byte;
                   1092:                output_log[output_log_pos].status = status;
                   1093:                output_log[output_log_pos].jiffies = jiffies;
                   1094:                output_log_pos = (output_log_pos + 1) % OLOGSIZE;
                   1095: #endif
                   1096:                return 0;
                   1097:        }
                   1098:        FDCS->reset = 1;
                   1099:        if (!initialising) {
                   1100:                DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
                   1101:                       byte, fdc, status);
                   1102:                show_floppy();
                   1103:        }
                   1104:        return -1;
                   1105: }
                   1106: #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
                   1107: 
                   1108: /* gets the response from the fdc */
                   1109: static int result(void)
                   1110: {
                   1111:        int i, status;
                   1112: 
                   1113:        for(i=0; i < MAX_REPLIES; i++) {
                   1114:                if ((status = wait_til_ready()) < 0)
                   1115:                        break;
                   1116:                status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
                   1117:                if ((status & ~STATUS_BUSY) == STATUS_READY){
                   1118: #ifdef FLOPPY_SANITY_CHECK
                   1119:                        resultjiffies = jiffies;
                   1120:                        resultsize = i;
                   1121: #endif
                   1122:                        return i;
                   1123:                }
                   1124:                if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
                   1125:                        reply_buffer[i] = fd_inb(FD_DATA);
                   1126:                else
                   1127:                        break;
                   1128:        }
                   1129:        if(!initialising) {
                   1130:                DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
                   1131:                       fdc, status, i);
                   1132:                show_floppy();
                   1133:        }
                   1134:        FDCS->reset = 1;
                   1135:        return -1;
                   1136: }
                   1137: 
                   1138: #define MORE_OUTPUT -2
                   1139: /* does the fdc need more output? */
                   1140: static int need_more_output(void)
                   1141: {
                   1142:        int status;
                   1143:        if( (status = wait_til_ready()) < 0)
                   1144:                return -1;
                   1145:        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
                   1146:                return MORE_OUTPUT;
                   1147:        return result();
                   1148: }
                   1149: 
                   1150: /* Set perpendicular mode as required, based on data rate, if supported.
                   1151:  * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
                   1152:  */
                   1153: static inline void perpendicular_mode(void)
                   1154: {
                   1155:        unsigned char perp_mode;
                   1156: 
                   1157:        if (raw_cmd->rate & 0x40){
                   1158:                switch(raw_cmd->rate & 3){
                   1159:                        case 0:
                   1160:                                perp_mode=2;
                   1161:                                break;
                   1162:                        case 3:
                   1163:                                perp_mode=3;
                   1164:                                break;
                   1165:                        default:
                   1166:                                DPRINT("Invalid data rate for perpendicular mode!\n");
                   1167:                                cont->done(0);
                   1168:                                FDCS->reset = 1; /* convenient way to return to
                   1169:                                                  * redo without to much hassle (deep
                   1170:                                                  * stack et al. */
                   1171:                                return;
                   1172:                }
                   1173:        } else
                   1174:                perp_mode = 0;
                   1175: 
                   1176:        if (FDCS->perp_mode == perp_mode)
                   1177:                return;
                   1178:        if (FDCS->version >= FDC_82077_ORIG) {
                   1179:                output_byte(FD_PERPENDICULAR);
                   1180:                output_byte(perp_mode);
                   1181:                FDCS->perp_mode = perp_mode;
                   1182:        } else if (perp_mode) {
                   1183:                DPRINT("perpendicular mode not supported by this FDC.\n");
                   1184:        }
                   1185: } /* perpendicular_mode */
                   1186: 
                   1187: static int fifo_depth = 0xa;
                   1188: static int no_fifo = 0;
                   1189: 
                   1190: static int fdc_configure(void)
                   1191: {
                   1192:        /* Turn on FIFO */
                   1193:        output_byte(FD_CONFIGURE);
                   1194:        if(need_more_output() != MORE_OUTPUT)
                   1195:                return 0;
                   1196:        output_byte(0);
                   1197:        output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
                   1198:        output_byte(0); /* pre-compensation from track 
                   1199:                           0 upwards */
                   1200:        return 1;
                   1201: }      
                   1202: 
                   1203: #define NOMINAL_DTR 500
                   1204: 
                   1205: /* Issue a "SPECIFY" command to set the step rate time, head unload time,
                   1206:  * head load time, and DMA disable flag to values needed by floppy.
                   1207:  *
                   1208:  * The value "dtr" is the data transfer rate in Kbps.  It is needed
                   1209:  * to account for the data rate-based scaling done by the 82072 and 82077
                   1210:  * FDC types.  This parameter is ignored for other types of FDCs (i.e.
                   1211:  * 8272a).
                   1212:  *
                   1213:  * Note that changing the data transfer rate has a (probably deleterious)
                   1214:  * effect on the parameters subject to scaling for 82072/82077 FDCs, so
                   1215:  * fdc_specify is called again after each data transfer rate
                   1216:  * change.
                   1217:  *
                   1218:  * srt: 1000 to 16000 in microseconds
                   1219:  * hut: 16 to 240 milliseconds
                   1220:  * hlt: 2 to 254 milliseconds
                   1221:  *
                   1222:  * These values are rounded up to the next highest available delay time.
                   1223:  */
                   1224: static void fdc_specify(void)
                   1225: {
                   1226:        unsigned char spec1, spec2;
                   1227:        int srt, hlt, hut;
                   1228:        unsigned long dtr = NOMINAL_DTR;
                   1229:        unsigned long scale_dtr = NOMINAL_DTR;
                   1230:        int hlt_max_code = 0x7f;
                   1231:        int hut_max_code = 0xf;
                   1232: 
                   1233:        if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
                   1234:                fdc_configure();
                   1235:                FDCS->need_configure = 0;
                   1236:                /*DPRINT("FIFO enabled\n");*/
                   1237:        }
                   1238: 
                   1239:        switch (raw_cmd->rate & 0x03) {
                   1240:                case 3:
                   1241:                        dtr = 1000;
                   1242:                        break;
                   1243:                case 1:
                   1244:                        dtr = 300;
                   1245:                        if (FDCS->version >= FDC_82078) {
                   1246:                                /* chose the default rate table, not the one
                   1247:                                 * where 1 = 2 Mbps */
                   1248:                                output_byte(FD_DRIVESPEC);
                   1249:                                if(need_more_output() == MORE_OUTPUT) {
                   1250:                                        output_byte(UNIT(current_drive));
                   1251:                                        output_byte(0xc0);
                   1252:                                }
                   1253:                        }
                   1254:                        break;
                   1255:                case 2:
                   1256:                        dtr = 250;
                   1257:                        break;
                   1258:        }
                   1259: 
                   1260:        if (FDCS->version >= FDC_82072) {
                   1261:                scale_dtr = dtr;
                   1262:                hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
                   1263:                hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
                   1264:        }
                   1265: 
                   1266:        /* Convert step rate from microseconds to milliseconds and 4 bits */
                   1267:        srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
                   1268:        SUPBOUND(srt, 0xf);
                   1269:        INFBOUND(srt, 0);
                   1270: 
                   1271:        hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
                   1272:        if (hlt < 0x01)
                   1273:                hlt = 0x01;
                   1274:        else if (hlt > 0x7f)
                   1275:                hlt = hlt_max_code;
                   1276: 
                   1277:        hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
                   1278:        if (hut < 0x1)
                   1279:                hut = 0x1;
                   1280:        else if (hut > 0xf)
                   1281:                hut = hut_max_code;
                   1282: 
                   1283:        spec1 = (srt << 4) | hut;
                   1284:        spec2 = (hlt << 1) | (use_virtual_dma & 1);
                   1285: 
                   1286:        /* If these parameters did not change, just return with success */
                   1287:        if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
                   1288:                /* Go ahead and set spec1 and spec2 */
                   1289:                output_byte(FD_SPECIFY);
                   1290:                output_byte(FDCS->spec1 = spec1);
                   1291:                output_byte(FDCS->spec2 = spec2);
                   1292:        }
                   1293: } /* fdc_specify */
                   1294: 
                   1295: /* Set the FDC's data transfer rate on behalf of the specified drive.
                   1296:  * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
                   1297:  * of the specify command (i.e. using the fdc_specify function).
                   1298:  */
                   1299: static int fdc_dtr(void)
                   1300: {
                   1301:        /* If data rate not already set to desired value, set it. */
                   1302:        if ((raw_cmd->rate & 3) == FDCS->dtr)
                   1303:                return 0;
                   1304: 
                   1305:        /* Set dtr */
                   1306:        fd_outb(raw_cmd->rate & 3, FD_DCR);
                   1307: 
                   1308:        /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
                   1309:         * need a stabilization period of several milliseconds to be
                   1310:         * enforced after data rate changes before R/W operations.
                   1311:         * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
                   1312:         */
                   1313:        FDCS->dtr = raw_cmd->rate & 3;
                   1314:        return(wait_for_completion(jiffies+2*HZ/100,
                   1315:                                   (timeout_fn) floppy_ready));
                   1316: } /* fdc_dtr */
                   1317: 
                   1318: static void tell_sector(void)
                   1319: {
                   1320:        printk(": track %d, head %d, sector %d, size %d",
                   1321:               R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
                   1322: } /* tell_sector */
                   1323: 
                   1324: 
                   1325: /*
                   1326:  * OK, this error interpreting routine is called after a
                   1327:  * DMA read/write has succeeded
                   1328:  * or failed, so we check the results, and copy any buffers.
                   1329:  * hhb: Added better error reporting.
                   1330:  * ak: Made this into a separate routine.
                   1331:  */
                   1332: static int interpret_errors(void)
                   1333: {
                   1334:        char bad;
                   1335: 
                   1336:        if (inr!=7) {
                   1337:                DPRINT("-- FDC reply error");
                   1338:                FDCS->reset = 1;
                   1339:                return 1;
                   1340:        }
                   1341: 
                   1342:        /* check IC to find cause of interrupt */
                   1343:        switch (ST0 & ST0_INTR) {
                   1344:                case 0x40:      /* error occurred during command execution */
                   1345:                        if (ST1 & ST1_EOC)
                   1346:                                return 0; /* occurs with pseudo-DMA */
                   1347:                        bad = 1;
                   1348:                        if (ST1 & ST1_WP) {
                   1349:                                DPRINT("Drive is write protected\n");
                   1350:                                CLEARF(FD_DISK_WRITABLE);
                   1351:                                cont->done(0);
                   1352:                                bad = 2;
                   1353:                        } else if (ST1 & ST1_ND) {
                   1354:                                SETF(FD_NEED_TWADDLE);
                   1355:                        } else if (ST1 & ST1_OR) {
                   1356:                                if (DP->flags & FTD_MSG)
                   1357:                                        DPRINT("Over/Underrun - retrying\n");
                   1358:                                bad = 0;
                   1359:                        }else if (*errors >= DP->max_errors.reporting){
                   1360:                                DPRINT("");
                   1361:                                if (ST0 & ST0_ECE) {
                   1362:                                        printk("Recalibrate failed!");
                   1363:                                } else if (ST2 & ST2_CRC) {
                   1364:                                        printk("data CRC error");
                   1365:                                        tell_sector();
                   1366:                                } else if (ST1 & ST1_CRC) {
                   1367:                                        printk("CRC error");
                   1368:                                        tell_sector();
                   1369:                                } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
                   1370:                                        if (!probing) {
                   1371:                                                printk("sector not found");
                   1372:                                                tell_sector();
                   1373:                                        } else
                   1374:                                                printk("probe failed...");
                   1375:                                } else if (ST2 & ST2_WC) {      /* seek error */
                   1376:                                        printk("wrong cylinder");
                   1377:                                } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
                   1378:                                        printk("bad cylinder");
                   1379:                                } else {
                   1380:                                        printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
                   1381:                                        tell_sector();
                   1382:                                }
                   1383:                                printk("\n");
                   1384: 
                   1385:                        }
                   1386:                        if (ST2 & ST2_WC || ST2 & ST2_BC)
                   1387:                                /* wrong cylinder => recal */
                   1388:                                DRS->track = NEED_2_RECAL;
                   1389:                        return bad;
                   1390:                case 0x80: /* invalid command given */
                   1391:                        DPRINT("Invalid FDC command given!\n");
                   1392:                        cont->done(0);
                   1393:                        return 2;
                   1394:                case 0xc0:
                   1395:                        DPRINT("Abnormal termination caused by polling\n");
                   1396:                        cont->error();
                   1397:                        return 2;
                   1398:                default: /* (0) Normal command termination */
                   1399:                        return 0;
                   1400:        }
                   1401: }
                   1402: 
                   1403: /*
                   1404:  * This routine is called when everything should be correctly set up
                   1405:  * for the transfer (i.e. floppy motor is on, the correct floppy is
                   1406:  * selected, and the head is sitting on the right track).
                   1407:  */
                   1408: static void setup_rw_floppy(void)
                   1409: {
                   1410:        int i,ready_date,r, flags,dflags;
                   1411:        timeout_fn function;
                   1412: 
                   1413:        flags = raw_cmd->flags;
                   1414:        if (flags & (FD_RAW_READ | FD_RAW_WRITE))
                   1415:                flags |= FD_RAW_INTR;
                   1416: 
                   1417:        if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
                   1418:                ready_date = DRS->spinup_date + DP->spinup;
                   1419:                /* If spinup will take a long time, rerun scandrives
                   1420:                 * again just before spinup completion. Beware that
                   1421:                 * after scandrives, we must again wait for selection.
                   1422:                 */
                   1423:                if ((signed) (ready_date - jiffies) > DP->select_delay){
                   1424:                        ready_date -= DP->select_delay;
                   1425:                        function = (timeout_fn) floppy_start;
                   1426:                } else
                   1427:                        function = (timeout_fn) setup_rw_floppy;
                   1428: 
                   1429:                /* wait until the floppy is spinning fast enough */
                   1430:                if (wait_for_completion(ready_date,function))
                   1431:                        return;
                   1432:        }
                   1433:        dflags = DRS->flags;
                   1434: 
                   1435:        if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
                   1436:                setup_DMA();
                   1437: 
                   1438:        if (flags & FD_RAW_INTR)
                   1439:                SET_INTR(main_command_interrupt);
                   1440: 
                   1441:        r=0;
                   1442:        for (i=0; i< raw_cmd->cmd_count; i++)
                   1443:                r|=output_byte(raw_cmd->cmd[i]);
                   1444: 
                   1445: #ifdef DEBUGT
                   1446:        debugt("rw_command: ");
                   1447: #endif
                   1448:        if (r){
                   1449:                cont->error();
                   1450:                reset_fdc();
                   1451:                return;
                   1452:        }
                   1453: 
                   1454:        if (!(flags & FD_RAW_INTR)){
                   1455:                inr = result();
                   1456:                cont->interrupt();
                   1457:        } else if (flags & FD_RAW_NEED_DISK)
                   1458:                fd_watchdog();
                   1459: }
                   1460: 
                   1461: static int blind_seek;
                   1462: 
                   1463: /*
                   1464:  * This is the routine called after every seek (or recalibrate) interrupt
                   1465:  * from the floppy controller.
                   1466:  */
                   1467: static void seek_interrupt(void)
                   1468: {
                   1469: #ifdef DEBUGT
                   1470:        debugt("seek interrupt:");
                   1471: #endif
                   1472:        if (inr != 2 || (ST0 & 0xF8) != 0x20) {
                   1473:                DPRINT("seek failed\n");
                   1474:                DRS->track = NEED_2_RECAL;
                   1475:                cont->error();
                   1476:                cont->redo();
                   1477:                return;
                   1478:        }
                   1479:        if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
                   1480: #ifdef DCL_DEBUG
                   1481:                if (DP->flags & FD_DEBUG){
                   1482:                        DPRINT("clearing NEWCHANGE flag because of effective seek\n");
                   1483:                        DPRINT("jiffies=%ld\n", jiffies);
                   1484:                }
                   1485: #endif
                   1486:                CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
                   1487:                DRS->select_date = jiffies;
                   1488:        }
                   1489:        DRS->track = ST1;
                   1490:        floppy_ready();
                   1491: }
                   1492: 
                   1493: static void check_wp(void)
                   1494: {
                   1495:        if (TESTF(FD_VERIFY)) {
                   1496:                /* check write protection */
                   1497:                output_byte(FD_GETSTATUS);
                   1498:                output_byte(UNIT(current_drive));
                   1499:                if (result() != 1){
                   1500:                        FDCS->reset = 1;
                   1501:                        return;
                   1502:                }
                   1503:                CLEARF(FD_VERIFY);
                   1504:                CLEARF(FD_NEED_TWADDLE);
                   1505: #ifdef DCL_DEBUG
                   1506:                if (DP->flags & FD_DEBUG){
                   1507:                        DPRINT("checking whether disk is write protected\n");
                   1508:                        DPRINT("wp=%x\n",ST3 & 0x40);
                   1509:                }
                   1510: #endif
                   1511:                if (!(ST3  & 0x40))
                   1512:                        SETF(FD_DISK_WRITABLE);
                   1513:                else
                   1514:                        CLEARF(FD_DISK_WRITABLE);
                   1515:        }
                   1516: }
                   1517: 
                   1518: static void seek_floppy(void)
                   1519: {
                   1520:        int track;
                   1521: 
                   1522:        blind_seek=0;
                   1523: 
                   1524: #ifdef DCL_DEBUG
                   1525:        if (DP->flags & FD_DEBUG){
                   1526:                DPRINT("calling disk change from seek\n");
                   1527:        }
                   1528: #endif
                   1529: 
                   1530:        if (!TESTF(FD_DISK_NEWCHANGE) &&
                   1531:            disk_change(current_drive) &&
                   1532:            (raw_cmd->flags & FD_RAW_NEED_DISK)){
                   1533:                /* the media changed flag should be cleared after the seek.
                   1534:                 * If it isn't, this means that there is really no disk in
                   1535:                 * the drive.
                   1536:                 */
                   1537:                SETF(FD_DISK_CHANGED);
                   1538:                cont->done(0);
                   1539:                cont->redo();
                   1540:                return;
                   1541:        }
                   1542:        if (DRS->track <= NEED_1_RECAL){
                   1543:                recalibrate_floppy();
                   1544:                return;
                   1545:        } else if (TESTF(FD_DISK_NEWCHANGE) &&
                   1546:                   (raw_cmd->flags & FD_RAW_NEED_DISK) &&
                   1547:                   (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
                   1548:                /* we seek to clear the media-changed condition. Does anybody
                   1549:                 * know a more elegant way, which works on all drives? */
                   1550:                if (raw_cmd->track)
                   1551:                        track = raw_cmd->track - 1;
                   1552:                else {
                   1553:                        if (DP->flags & FD_SILENT_DCL_CLEAR){
                   1554:                                set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
                   1555:                                blind_seek = 1;
                   1556:                                raw_cmd->flags |= FD_RAW_NEED_SEEK;
                   1557:                        }
                   1558:                        track = 1;
                   1559:                }
                   1560:        } else {
                   1561:                check_wp();
                   1562:                if (raw_cmd->track != DRS->track &&
                   1563:                    (raw_cmd->flags & FD_RAW_NEED_SEEK))
                   1564:                        track = raw_cmd->track;
                   1565:                else {
                   1566:                        setup_rw_floppy();
                   1567:                        return;
                   1568:                }
                   1569:        }
                   1570: 
                   1571:        SET_INTR(seek_interrupt);
                   1572:        output_byte(FD_SEEK);
                   1573:        output_byte(UNIT(current_drive));
                   1574:        LAST_OUT(track);
                   1575: #ifdef DEBUGT
                   1576:        debugt("seek command:");
                   1577: #endif
                   1578: }
                   1579: 
                   1580: static void recal_interrupt(void)
                   1581: {
                   1582: #ifdef DEBUGT
                   1583:        debugt("recal interrupt:");
                   1584: #endif
                   1585:        if (inr !=2)
                   1586:                FDCS->reset = 1;
                   1587:        else if (ST0 & ST0_ECE) {
                   1588:                switch(DRS->track){
                   1589:                        case NEED_1_RECAL:
                   1590: #ifdef DEBUGT
                   1591:                                debugt("recal interrupt need 1 recal:");
                   1592: #endif
                   1593:                                /* after a second recalibrate, we still haven't
                   1594:                                 * reached track 0. Probably no drive. Raise an
                   1595:                                 * error, as failing immediately might upset
                   1596:                                 * computers possessed by the Devil :-) */
                   1597:                                cont->error();
                   1598:                                cont->redo();
                   1599:                                return;
                   1600:                        case NEED_2_RECAL:
                   1601: #ifdef DEBUGT
                   1602:                                debugt("recal interrupt need 2 recal:");
                   1603: #endif
                   1604:                                /* If we already did a recalibrate,
                   1605:                                 * and we are not at track 0, this
                   1606:                                 * means we have moved. (The only way
                   1607:                                 * not to move at recalibration is to
                   1608:                                 * be already at track 0.) Clear the
                   1609:                                 * new change flag */
                   1610: #ifdef DCL_DEBUG
                   1611:                                if (DP->flags & FD_DEBUG){
                   1612:                                        DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
                   1613:                                }
                   1614: #endif
                   1615: 
                   1616:                                CLEARF(FD_DISK_NEWCHANGE);
                   1617:                                DRS->select_date = jiffies;
                   1618:                                /* fall through */
                   1619:                        default:
                   1620: #ifdef DEBUGT
                   1621:                                debugt("recal interrupt default:");
                   1622: #endif
                   1623:                                /* Recalibrate moves the head by at
                   1624:                                 * most 80 steps. If after one
                   1625:                                 * recalibrate we don't have reached
                   1626:                                 * track 0, this might mean that we
                   1627:                                 * started beyond track 80.  Try
                   1628:                                 * again.  */
                   1629:                                DRS->track = NEED_1_RECAL;
                   1630:                                break;
                   1631:                }
                   1632:        } else
                   1633:                DRS->track = ST1;
                   1634:        floppy_ready();
                   1635: }
                   1636: 
                   1637: static void print_result(char *message, int inr)
                   1638: {
                   1639:        int i;
                   1640: 
                   1641:        DPRINT("%s ", message);
                   1642:        if (inr >= 0)
                   1643:                for (i=0; i<inr; i++)
                   1644:                        printk("repl[%d]=%x ", i, reply_buffer[i]);
                   1645:        printk("\n");
                   1646: }
                   1647: 
                   1648: /* interrupt handler */
                   1649: void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
                   1650: {
                   1651:        void (*handler)(void) = DEVICE_INTR;
                   1652:        int do_print;
                   1653: 
                   1654:        lasthandler = handler;
                   1655:        interruptjiffies = jiffies;
                   1656: 
                   1657:        fd_disable_dma();
                   1658:        floppy_enable_hlt();
                   1659:        CLEAR_INTR;
                   1660:        if (fdc >= N_FDC || FDCS->address == -1){
                   1661:                /* we don't even know which FDC is the culprit */
                   1662:                printk("DOR0=%x\n", fdc_state[0].dor);
                   1663:                printk("floppy interrupt on bizarre fdc %d\n",fdc);
                   1664:                printk("handler=%p\n", handler);
                   1665:                is_alive("bizarre fdc");
                   1666:                return;
                   1667:        }
                   1668: 
                   1669:        FDCS->reset = 0;
                   1670:        /* We have to clear the reset flag here, because apparently on boxes
                   1671:         * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
                   1672:         * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
                   1673:         * emission of the SENSEI's.
                   1674:         * It is OK to emit floppy commands because we are in an interrupt
                   1675:         * handler here, and thus we have to fear no interference of other
                   1676:         * activity.
                   1677:         */
                   1678: 
                   1679:        do_print = !handler && print_unex && !initialising;
                   1680: 
                   1681:        inr = result();
                   1682:        if(do_print)
                   1683:                print_result("unexpected interrupt", inr);
                   1684:        if (inr == 0){
                   1685:                int max_sensei = 4;
                   1686:                do {
                   1687:                        output_byte(FD_SENSEI);
                   1688:                        inr = result();
                   1689:                        if(do_print)
                   1690:                                print_result("sensei", inr);
                   1691:                        max_sensei--;
                   1692:                } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2 && max_sensei);
                   1693:        }
                   1694:        if (handler) {
                   1695:                if(intr_count >= 2) {
                   1696:                        /* expected interrupt */
                   1697:                        floppy_tq.routine = (void *)(void *) handler;
                   1698:                        queue_task_irq(&floppy_tq, &tq_immediate);
                   1699:                        mark_bh(IMMEDIATE_BH);
                   1700:                } else
                   1701:                        handler();
                   1702:        } else
                   1703:                FDCS->reset = 1;
                   1704:        is_alive("normal interrupt end");
                   1705: }
                   1706: 
                   1707: static void recalibrate_floppy(void)
                   1708: {
                   1709: #ifdef DEBUGT
                   1710:        debugt("recalibrate floppy:");
                   1711: #endif
                   1712:        SET_INTR(recal_interrupt);
                   1713:        output_byte(FD_RECALIBRATE);
                   1714:        LAST_OUT(UNIT(current_drive));
                   1715: }
                   1716: 
                   1717: /*
                   1718:  * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
                   1719:  */
                   1720: static void reset_interrupt(void)
                   1721: {
                   1722: #ifdef DEBUGT
                   1723:        debugt("reset interrupt:");
                   1724: #endif
                   1725:        result();               /* get the status ready for set_fdc */
                   1726:        if (FDCS->reset) {
                   1727:                printk("reset set in interrupt, calling %p\n", cont->error);
                   1728:                cont->error(); /* a reset just after a reset. BAD! */
                   1729:        }
                   1730:        cont->redo();
                   1731: }
                   1732: 
                   1733: /*
                   1734:  * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
                   1735:  * or by setting the self clearing bit 7 of STATUS (newer FDCs)
                   1736:  */
                   1737: static void reset_fdc(void)
                   1738: {
                   1739:        SET_INTR(reset_interrupt);
                   1740:        FDCS->reset = 0;
                   1741:        reset_fdc_info(0);
                   1742: 
                   1743:        /* Pseudo-DMA may intercept 'reset finished' interrupt.  */
                   1744:        /* Irrelevant for systems with true DMA (i386).          */
                   1745:        fd_disable_dma();
                   1746: 
                   1747:        if (FDCS->version >= FDC_82072A)
                   1748:                fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
                   1749:        else {
                   1750:                fd_outb(FDCS->dor & ~0x04, FD_DOR);
                   1751:                udelay(FD_RESET_DELAY);
                   1752:                fd_outb(FDCS->dor, FD_DOR);
                   1753:        }
                   1754: }
                   1755: 
                   1756: void show_floppy(void)
                   1757: {
                   1758:        int i;
                   1759: 
                   1760:        printk("\n");
                   1761:        printk("floppy driver state\n");
                   1762:        printk("-------------------\n");
                   1763:        printk("now=%ld last interrupt=%d last called handler=%p\n",
                   1764:               jiffies, interruptjiffies, lasthandler);
                   1765: 
                   1766: 
                   1767: #ifdef FLOPPY_SANITY_CHECK
                   1768:        printk("timeout_message=%s\n", timeout_message);
                   1769:        printk("last output bytes:\n");
                   1770:        for (i=0; i < OLOGSIZE; i++)
                   1771:                printk("%2x %2x %ld\n",
                   1772:                       output_log[(i+output_log_pos) % OLOGSIZE].data,
                   1773:                       output_log[(i+output_log_pos) % OLOGSIZE].status,
                   1774:                       output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
                   1775:        printk("last result at %d\n", resultjiffies);
                   1776:        printk("last redo_fd_request at %d\n", lastredo);
                   1777:        for (i=0; i<resultsize; i++){
                   1778:                printk("%2x ", reply_buffer[i]);
                   1779:        }
                   1780:        printk("\n");
                   1781: #endif
                   1782: 
                   1783:        printk("status=%x\n", fd_inb(FD_STATUS));
                   1784:        printk("fdc_busy=%d\n", fdc_busy);
                   1785:        if (DEVICE_INTR)
                   1786:                printk("DEVICE_INTR=%p\n", DEVICE_INTR);
                   1787:        if (floppy_tq.sync)
                   1788:                printk("floppy_tq.routine=%p\n", floppy_tq.routine);
                   1789:        if (fd_timer.prev)
                   1790:                printk("fd_timer.function=%p\n", fd_timer.function);
                   1791:        if (fd_timeout.prev){
                   1792:                printk("timer_table=%p\n",fd_timeout.function);
                   1793:                printk("expires=%ld\n",fd_timeout.expires-jiffies);
                   1794:                printk("now=%ld\n",jiffies);
                   1795:        }
                   1796:        printk("cont=%p\n", cont);
                   1797:        printk("CURRENT=%p\n", CURRENT);
                   1798:        printk("command_status=%d\n", command_status);
                   1799:        printk("\n");
                   1800: }
                   1801: 
                   1802: static void floppy_shutdown(void)
                   1803: {
                   1804:        if (!initialising)
                   1805:                show_floppy();
                   1806:        cancel_activity();
                   1807:        sti();
                   1808: 
                   1809:        floppy_enable_hlt();
                   1810:        fd_disable_dma();
                   1811:        /* avoid dma going to a random drive after shutdown */
                   1812: 
                   1813:        if (!initialising)
                   1814:                DPRINT("floppy timeout called\n");
                   1815:        FDCS->reset = 1;
                   1816:        if (cont){
                   1817:                cont->done(0);
                   1818:                cont->redo(); /* this will recall reset when needed */
                   1819:        } else {
                   1820:                printk("no cont in shutdown!\n");
                   1821:                process_fd_request();
                   1822:        }
                   1823:        is_alive("floppy shutdown");
                   1824: }
                   1825: /*typedef void (*timeout_fn)(unsigned long);*/
                   1826: 
                   1827: /* start motor, check media-changed condition and write protection */
                   1828: static int start_motor(void (*function)(void) )
                   1829: {
                   1830:        int mask, data;
                   1831: 
                   1832:        mask = 0xfc;
                   1833:        data = UNIT(current_drive);
                   1834:        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
                   1835:                if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
                   1836:                        set_debugt();
                   1837:                        /* no read since this drive is running */
                   1838:                        DRS->first_read_date = 0;
                   1839:                        /* note motor start time if motor is not yet running */
                   1840:                        DRS->spinup_date = jiffies;
                   1841:                        data |= (0x10 << UNIT(current_drive));
                   1842:                }
                   1843:        } else
                   1844:                if (FDCS->dor & (0x10 << UNIT(current_drive)))
                   1845:                        mask &= ~(0x10 << UNIT(current_drive));
                   1846: 
                   1847:        /* starts motor and selects floppy */
                   1848:        del_timer(motor_off_timer + current_drive);
                   1849:        set_dor(fdc, mask, data);
                   1850: 
                   1851:        /* wait_for_completion also schedules reset if needed. */
                   1852:        return(wait_for_completion(DRS->select_date+DP->select_delay,
                   1853:                                   (timeout_fn) function));
                   1854: }
                   1855: 
                   1856: static void floppy_ready(void)
                   1857: {
                   1858:        CHECK_RESET;
                   1859:        if (start_motor(floppy_ready)) return;
                   1860:        if (fdc_dtr()) return;
                   1861: 
                   1862: #ifdef DCL_DEBUG
                   1863:        if (DP->flags & FD_DEBUG){
                   1864:                DPRINT("calling disk change from floppy_ready\n");
                   1865:        }
                   1866: #endif
                   1867: 
                   1868:        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
                   1869:           disk_change(current_drive) &&
                   1870:           !DP->select_delay)
                   1871:                twaddle(); /* this clears the dcl on certain drive/controller
                   1872:                            * combinations */
                   1873: 
                   1874:        if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
                   1875:                perpendicular_mode();
                   1876:                fdc_specify(); /* must be done here because of hut, hlt ... */
                   1877:                seek_floppy();
                   1878:        } else
                   1879:                setup_rw_floppy();
                   1880: }
                   1881: 
                   1882: static void floppy_start(void)
                   1883: {
                   1884:        reschedule_timeout(CURRENTD, "floppy start", 0);
                   1885: 
                   1886:        scandrives();
                   1887: #ifdef DCL_DEBUG
                   1888:        if (DP->flags & FD_DEBUG){
                   1889:                DPRINT("setting NEWCHANGE in floppy_start\n");
                   1890:        }
                   1891: #endif
                   1892:        SETF(FD_DISK_NEWCHANGE);
                   1893:        floppy_ready();
                   1894: }
                   1895: 
                   1896: /*
                   1897:  * ========================================================================
                   1898:  * here ends the bottom half. Exported routines are:
                   1899:  * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
                   1900:  * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
                   1901:  * Initialization also uses output_byte, result, set_dor, floppy_interrupt
                   1902:  * and set_dor.
                   1903:  * ========================================================================
                   1904:  */
                   1905: /*
                   1906:  * General purpose continuations.
                   1907:  * ==============================
                   1908:  */
                   1909: 
                   1910: static void do_wakeup(void)
                   1911: {
                   1912:        reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
                   1913:        cont = 0;
                   1914:        command_status += 2;
                   1915:        wake_up(&command_done);
                   1916: }
                   1917: 
                   1918: static struct cont_t wakeup_cont={
                   1919:        empty,
                   1920:        do_wakeup,
                   1921:        empty,
                   1922:        (done_f)empty
                   1923: };
                   1924: 
                   1925: 
                   1926: static struct cont_t intr_cont={
                   1927:        empty,
                   1928:        process_fd_request,
                   1929:        empty,
                   1930:        (done_f) empty
                   1931: };
                   1932: 
                   1933: static int wait_til_done(void (*handler)(void), int interruptible)
                   1934: {
                   1935:        int ret;
                   1936:        unsigned long flags;
                   1937: 
                   1938:        floppy_tq.routine = (void *)(void *) handler;
                   1939:        queue_task(&floppy_tq, &tq_immediate);
                   1940:        mark_bh(IMMEDIATE_BH);
                   1941:        INT_OFF;
                   1942:        while(command_status < 2 && NO_SIGNAL){
                   1943:                is_alive("wait_til_done");
                   1944:                if (interruptible)
                   1945:                        interruptible_sleep_on(&command_done);
                   1946:                else
                   1947:                        sleep_on(&command_done);
                   1948:        }
                   1949:        if (command_status < 2){
                   1950:                cancel_activity();
                   1951:                cont = &intr_cont;
                   1952:                reset_fdc();
                   1953:                INT_ON;
                   1954:                return -EINTR;
                   1955:        }
                   1956:        INT_ON;
                   1957: 
                   1958:        if (FDCS->reset)
                   1959:                command_status = FD_COMMAND_ERROR;
                   1960:        if (command_status == FD_COMMAND_OKAY)
                   1961:                ret=0;
                   1962:        else
                   1963:                ret=-EIO;
                   1964:        command_status = FD_COMMAND_NONE;
                   1965:        return ret;
                   1966: }
                   1967: 
                   1968: static void generic_done(int result)
                   1969: {
                   1970:        command_status = result;
                   1971:        cont = &wakeup_cont;
                   1972: }
                   1973: 
                   1974: static void generic_success(void)
                   1975: {
                   1976:        cont->done(1);
                   1977: }
                   1978: 
                   1979: static void generic_failure(void)
                   1980: {
                   1981:        cont->done(0);
                   1982: }
                   1983: 
                   1984: static void success_and_wakeup(void)
                   1985: {
                   1986:        generic_success();
                   1987:        cont->redo();
                   1988: }
                   1989: 
                   1990: 
                   1991: /*
                   1992:  * formatting and rw support.
                   1993:  * ==========================
                   1994:  */
                   1995: 
                   1996: static int next_valid_format(void)
                   1997: {
                   1998:        int probed_format;
                   1999: 
                   2000:        probed_format = DRS->probed_format;
                   2001:        while(1){
                   2002:                if (probed_format >= 8 ||
                   2003:                     !DP->autodetect[probed_format]){
                   2004:                        DRS->probed_format = 0;
                   2005:                        return 1;
                   2006:                }
                   2007:                if (floppy_type[DP->autodetect[probed_format]].sect){
                   2008:                        DRS->probed_format = probed_format;
                   2009:                        return 0;
                   2010:                }
                   2011:                probed_format++;
                   2012:        }
                   2013: }
                   2014: 
                   2015: static void bad_flp_intr(void)
                   2016: {
                   2017:        if (probing){
                   2018:                DRS->probed_format++;
                   2019:                if (!next_valid_format())
                   2020:                        return;
                   2021:        }
                   2022:        (*errors)++;
                   2023:        INFBOUND(DRWE->badness, *errors);
                   2024:        if (*errors > DP->max_errors.abort)
                   2025:                cont->done(0);
                   2026:        if (*errors > DP->max_errors.reset)
                   2027:                FDCS->reset = 1;
                   2028:        else if (*errors > DP->max_errors.recal)
                   2029:                DRS->track = NEED_2_RECAL;
                   2030: }
                   2031: 
                   2032: static void set_floppy(kdev_t device)
                   2033: {
                   2034:        if (TYPE(device))
                   2035:                _floppy = TYPE(device) + floppy_type;
                   2036:        else
                   2037:                _floppy = current_type[ DRIVE(device) ];
                   2038: }
                   2039: 
                   2040: /*
                   2041:  * formatting support.
                   2042:  * ===================
                   2043:  */
                   2044: static void format_interrupt(void)
                   2045: {
                   2046:        switch (interpret_errors()){
                   2047:                case 1:
                   2048:                        cont->error();
                   2049:                case 2:
                   2050:                        break;
                   2051:                case 0:
                   2052:                        cont->done(1);
                   2053:        }
                   2054:        cont->redo();
                   2055: }
                   2056: 
                   2057: #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
                   2058: #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
                   2059: #define CT(x) ((x) | 0x40)
                   2060: static void setup_format_params(int track)
                   2061: {
                   2062:        struct fparm {
                   2063:                unsigned char track,head,sect,size;
                   2064:        } *here = (struct fparm *)floppy_track_buffer;
                   2065:        int il,n;
                   2066:        int count,head_shift,track_shift;
                   2067: 
                   2068:        raw_cmd = &default_raw_cmd;
                   2069:        raw_cmd->track = track;
                   2070: 
                   2071:        raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
                   2072:                FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
                   2073:        raw_cmd->rate = _floppy->rate & 0x43;
                   2074:        raw_cmd->cmd_count = NR_F;
                   2075:        COMMAND = FM_MODE(_floppy,FD_FORMAT);
                   2076:        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
                   2077:        F_SIZECODE = FD_SIZECODE(_floppy);
                   2078:        F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
                   2079:        F_GAP = _floppy->fmt_gap;
                   2080:        F_FILL = FD_FILL_BYTE;
                   2081: 
                   2082:        raw_cmd->kernel_data = floppy_track_buffer;
                   2083:        raw_cmd->length = 4 * F_SECT_PER_TRACK;
                   2084: 
                   2085:        /* allow for about 30ms for data transport per track */
                   2086:        head_shift  = (F_SECT_PER_TRACK + 5) / 6;
                   2087: 
                   2088:        /* a ``cylinder'' is two tracks plus a little stepping time */
                   2089:        track_shift = 2 * head_shift + 3;
                   2090: 
                   2091:        /* position of logical sector 1 on this track */
                   2092:        n = (track_shift * format_req.track + head_shift * format_req.head)
                   2093:                % F_SECT_PER_TRACK;
                   2094: 
                   2095:        /* determine interleave */
                   2096:        il = 1;
                   2097:        if (_floppy->fmt_gap < 0x22)
                   2098:                il++;
                   2099: 
                   2100:        /* initialize field */
                   2101:        for (count = 0; count < F_SECT_PER_TRACK; ++count) {
                   2102:                here[count].track = format_req.track;
                   2103:                here[count].head = format_req.head;
                   2104:                here[count].sect = 0;
                   2105:                here[count].size = F_SIZECODE;
                   2106:        }
                   2107:        /* place logical sectors */
                   2108:        for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
                   2109:                here[n].sect = count;
                   2110:                n = (n+il) % F_SECT_PER_TRACK;
                   2111:                if (here[n].sect) { /* sector busy, find next free sector */
                   2112:                        ++n;
                   2113:                        if (n>= F_SECT_PER_TRACK) {
                   2114:                                n-=F_SECT_PER_TRACK;
                   2115:                                while (here[n].sect) ++n;
                   2116:                        }
                   2117:                }
                   2118:        }
                   2119: }
                   2120: 
                   2121: static void redo_format(void)
                   2122: {
                   2123:        buffer_track = -1;
                   2124:        setup_format_params(format_req.track << STRETCH(_floppy));
                   2125:        floppy_start();
                   2126: #ifdef DEBUGT
                   2127:        debugt("queue format request");
                   2128: #endif
                   2129: }
                   2130: 
                   2131: static struct cont_t format_cont={
                   2132:        format_interrupt,
                   2133:        redo_format,
                   2134:        bad_flp_intr,
                   2135:        generic_done };
                   2136: 
                   2137: static int do_format(kdev_t device, struct format_descr *tmp_format_req)
                   2138: {
                   2139:        int ret;
                   2140:        int drive=DRIVE(device);
                   2141: 
                   2142:        LOCK_FDC(drive,1);
                   2143:        set_floppy(device);
                   2144:        if (!_floppy ||
                   2145:            _floppy->track > DP->tracks ||
                   2146:            tmp_format_req->track >= _floppy->track ||
                   2147:            tmp_format_req->head >= _floppy->head ||
                   2148:            (_floppy->sect << 2) % (1 <<  FD_SIZECODE(_floppy)) ||
                   2149:            !_floppy->fmt_gap) {
                   2150:                process_fd_request();
                   2151:                return -EINVAL;
                   2152:        }
                   2153:        format_req = *tmp_format_req;
                   2154:        format_errors = 0;
                   2155:        cont = &format_cont;
                   2156:        errors = &format_errors;
                   2157:        IWAIT(redo_format);
                   2158:        process_fd_request();
                   2159:        return ret;
                   2160: }
                   2161: 
                   2162: /*
                   2163:  * Buffer read/write and support
                   2164:  * =============================
                   2165:  */
                   2166: 
                   2167: /* new request_done. Can handle physical sectors which are smaller than a
                   2168:  * logical buffer */
                   2169: static void request_done(int uptodate)
                   2170: {
                   2171:        int block;
                   2172: 
                   2173:        probing = 0;
                   2174:        reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
                   2175: 
                   2176:        if (!CURRENT){
                   2177:                DPRINT("request list destroyed in floppy request done\n");
                   2178:                return;
                   2179:        }
                   2180: 
                   2181:        if (uptodate){
                   2182:                /* maintain values for invalidation on geometry
                   2183:                 * change */
                   2184:                block = current_count_sectors + CURRENT->sector;
                   2185:                INFBOUND(DRS->maxblock, block);
                   2186:                if (block > _floppy->sect)
                   2187:                        DRS->maxtrack = 1;
                   2188: 
                   2189:                /* unlock chained buffers */
                   2190:                while (current_count_sectors && CURRENT &&
                   2191:                       current_count_sectors >= CURRENT->current_nr_sectors){
                   2192:                        current_count_sectors -= CURRENT->current_nr_sectors;
                   2193:                        CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
                   2194:                        CURRENT->sector += CURRENT->current_nr_sectors;
                   2195:                        end_request(1);
                   2196:                }
                   2197:                if (current_count_sectors && CURRENT){
                   2198:                        /* "unlock" last subsector */
                   2199:                        CURRENT->buffer += current_count_sectors <<9;
                   2200:                        CURRENT->current_nr_sectors -= current_count_sectors;
                   2201:                        CURRENT->nr_sectors -= current_count_sectors;
                   2202:                        CURRENT->sector += current_count_sectors;
                   2203:                        return;
                   2204:                }
                   2205: 
                   2206:                if (current_count_sectors && !CURRENT)
                   2207:                        DPRINT("request list destroyed in floppy request done\n");
                   2208: 
                   2209:        } else {
                   2210:                if (CURRENT->cmd == WRITE) {
                   2211:                        /* record write error information */
                   2212:                        DRWE->write_errors++;
                   2213:                        if (DRWE->write_errors == 1) {
                   2214:                                DRWE->first_error_sector = CURRENT->sector;
                   2215:                                DRWE->first_error_generation = DRS->generation;
                   2216:                        }
                   2217:                        DRWE->last_error_sector = CURRENT->sector;
                   2218:                        DRWE->last_error_generation = DRS->generation;
                   2219:                }
                   2220:                end_request(0);
                   2221:        }
                   2222: }
                   2223: 
                   2224: /* Interrupt handler evaluating the result of the r/w operation */
                   2225: static void rw_interrupt(void)
                   2226: {
                   2227:        int nr_sectors, ssize, eoc;
                   2228: 
                   2229:        if (!DRS->first_read_date)
                   2230:                DRS->first_read_date = jiffies;
                   2231: 
                   2232:        nr_sectors = 0;
                   2233:        CODE2SIZE;
                   2234: 
                   2235:        if(ST1 & ST1_EOC)
                   2236:                eoc = 1;
                   2237:        else
                   2238:                eoc = 0;
                   2239:        nr_sectors = ((R_TRACK-TRACK)*_floppy->head+R_HEAD-HEAD) *
                   2240:                _floppy->sect + ((R_SECTOR-SECTOR+eoc) <<  SIZECODE >> 2) -
                   2241:                (sector_t % _floppy->sect) % ssize;
                   2242: 
                   2243: #ifdef FLOPPY_SANITY_CHECK
                   2244:        if (nr_sectors > current_count_sectors + ssize -
                   2245:             (current_count_sectors + sector_t) % ssize +
                   2246:             sector_t % ssize){
                   2247:                DPRINT("long rw: %x instead of %lx\n",
                   2248:                        nr_sectors, current_count_sectors);
                   2249:                printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
                   2250:                printk("rh=%d h=%d\n", R_HEAD, HEAD);
                   2251:                printk("rt=%d t=%d\n", R_TRACK, TRACK);
                   2252:                printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
                   2253:                       sector_t, ssize);
                   2254:        }
                   2255: #endif
                   2256:        INFBOUND(nr_sectors,0);
                   2257:        SUPBOUND(current_count_sectors, nr_sectors);
                   2258: 
                   2259:        switch (interpret_errors()){
                   2260:                case 2:
                   2261:                        cont->redo();
                   2262:                        return;
                   2263:                case 1:
                   2264:                        if (!current_count_sectors){
                   2265:                                cont->error();
                   2266:                                cont->redo();
                   2267:                                return;
                   2268:                        }
                   2269:                        break;
                   2270:                case 0:
                   2271:                        if (!current_count_sectors){
                   2272:                                cont->redo();
                   2273:                                return;
                   2274:                        }
                   2275:                        current_type[current_drive] = _floppy;
                   2276:                        floppy_sizes[TOMINOR(current_drive) ]= _floppy->size>>1;
                   2277:                        break;
                   2278:        }
                   2279: 
                   2280:        if (probing) {
                   2281:                if (DP->flags & FTD_MSG)
                   2282:                        DPRINT("Auto-detected floppy type %s in fd%d\n",
                   2283:                                _floppy->name,current_drive);
                   2284:                current_type[current_drive] = _floppy;
                   2285:                floppy_sizes[TOMINOR(current_drive)] = _floppy->size >> 1;
                   2286:                probing = 0;
                   2287:        }
                   2288: 
                   2289:        if (CT(COMMAND) != FD_READ || 
                   2290:             raw_cmd->kernel_data == CURRENT->buffer){
                   2291:                /* transfer directly from buffer */
                   2292:                cont->done(1);
                   2293:        } else if (CT(COMMAND) == FD_READ){
                   2294:                buffer_track = raw_cmd->track;
                   2295:                buffer_drive = current_drive;
                   2296:                INFBOUND(buffer_max, nr_sectors + sector_t);
                   2297:        }
                   2298:        cont->redo();
                   2299: }
                   2300: 
                   2301: /* Compute maximal contiguous buffer size. */
                   2302: static int buffer_chain_size(void)
                   2303: {
                   2304:        struct buffer_head *bh;
                   2305:        int size;
                   2306:        char *base;
                   2307: 
                   2308:        base = CURRENT->buffer;
                   2309:        size = CURRENT->current_nr_sectors << 9;
                   2310:        bh = CURRENT->bh;
                   2311: 
                   2312:        if (bh){
                   2313:                bh = bh->b_reqnext;
                   2314:                while (bh && bh->b_data == base + size){
                   2315:                        size += bh->b_size;
                   2316:                        bh = bh->b_reqnext;
                   2317:                }
                   2318:        }
                   2319:        return size >> 9;
                   2320: }
                   2321: 
                   2322: /* Compute the maximal transfer size */
                   2323: static int transfer_size(int ssize, int max_sector, int max_size)
                   2324: {
                   2325:        SUPBOUND(max_sector, sector_t + max_size);
                   2326: 
                   2327:        /* alignment */
                   2328:        max_sector -= (max_sector % _floppy->sect) % ssize;
                   2329: 
                   2330:        /* transfer size, beginning not aligned */
                   2331:        current_count_sectors = max_sector - sector_t ;
                   2332: 
                   2333:        return max_sector;
                   2334: }
                   2335: 
                   2336: /*
                   2337:  * Move data from/to the track buffer to/from the buffer cache.
                   2338:  */
                   2339: static void copy_buffer(int ssize, int max_sector, int max_sector_2)
                   2340: {
                   2341:        int remaining; /* number of transferred 512-byte sectors */
                   2342:        struct buffer_head *bh;
                   2343:        char *buffer, *dma_buffer;
                   2344:        int size;
                   2345: 
                   2346:        max_sector = transfer_size(ssize,
                   2347:                                   minimum(max_sector, max_sector_2),
                   2348:                                   CURRENT->nr_sectors);
                   2349: 
                   2350:        if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
                   2351:            buffer_max > sector_t + CURRENT->nr_sectors)
                   2352:                current_count_sectors = minimum(buffer_max - sector_t,
                   2353:                                                CURRENT->nr_sectors);
                   2354: 
                   2355:        remaining = current_count_sectors << 9;
                   2356: #ifdef FLOPPY_SANITY_CHECK
                   2357:        if ((remaining >> 9) > CURRENT->nr_sectors  &&
                   2358:            CT(COMMAND) == FD_WRITE){
                   2359:                DPRINT("in copy buffer\n");
                   2360:                printk("current_count_sectors=%ld\n", current_count_sectors);
                   2361:                printk("remaining=%d\n", remaining >> 9);
                   2362:                printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
                   2363:                printk("CURRENT->current_nr_sectors=%ld\n",
                   2364:                       CURRENT->current_nr_sectors);
                   2365:                printk("max_sector=%d\n", max_sector);
                   2366:                printk("ssize=%d\n", ssize);
                   2367:        }
                   2368: #endif
                   2369: 
                   2370:        buffer_max = maximum(max_sector, buffer_max);
                   2371: 
                   2372:        dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
                   2373: 
                   2374:        bh = CURRENT->bh;
                   2375:        size = CURRENT->current_nr_sectors << 9;
                   2376:        buffer = CURRENT->buffer;
                   2377: 
                   2378:        while (remaining > 0){
                   2379:                SUPBOUND(size, remaining);
                   2380: #ifdef FLOPPY_SANITY_CHECK
                   2381:                if (dma_buffer + size >
                   2382:                    floppy_track_buffer + (max_buffer_sectors << 10) ||
                   2383:                    dma_buffer < floppy_track_buffer){
                   2384:                        DPRINT("buffer overrun in copy buffer %d\n",
                   2385:                                (int) ((floppy_track_buffer - dma_buffer) >>9));
                   2386:                        printk("sector_t=%d buffer_min=%d\n",
                   2387:                               sector_t, buffer_min);
                   2388:                        printk("current_count_sectors=%ld\n",
                   2389:                               current_count_sectors);
                   2390:                        if (CT(COMMAND) == FD_READ)
                   2391:                                printk("read\n");
                   2392:                        if (CT(COMMAND) == FD_READ)
                   2393:                                printk("write\n");
                   2394:                        break;
                   2395:                }
                   2396:                if (((unsigned long)buffer) % 512)
                   2397:                        DPRINT("%p buffer not aligned\n", buffer);
                   2398: #endif
                   2399:                if (CT(COMMAND) == FD_READ)
                   2400:                        memcpy(buffer, dma_buffer, size);
                   2401:                else
                   2402:                        memcpy(dma_buffer, buffer, size);
                   2403:                remaining -= size;
                   2404:                if (!remaining)
                   2405:                        break;
                   2406: 
                   2407:                dma_buffer += size;
                   2408:                bh = bh->b_reqnext;
                   2409: #ifdef FLOPPY_SANITY_CHECK
                   2410:                if (!bh){
                   2411:                        DPRINT("bh=null in copy buffer after copy\n");
                   2412:                        break;
                   2413:                }
                   2414: #endif
                   2415:                size = bh->b_size;
                   2416:                buffer = bh->b_data;
                   2417:        }
                   2418: #ifdef FLOPPY_SANITY_CHECK
                   2419:        if (remaining){
                   2420:                if (remaining > 0)
                   2421:                        max_sector -= remaining >> 9;
                   2422:                DPRINT("weirdness: remaining %d\n", remaining>>9);
                   2423:        }
                   2424: #endif
                   2425: }
                   2426: 
                   2427: /*
                   2428:  * Formulate a read/write request.
                   2429:  * this routine decides where to load the data (directly to buffer, or to
                   2430:  * tmp floppy area), how much data to load (the size of the buffer, the whole
                   2431:  * track, or a single sector)
                   2432:  * All floppy_track_buffer handling goes in here. If we ever add track buffer
                   2433:  * allocation on the fly, it should be done here. No other part should need
                   2434:  * modification.
                   2435:  */
                   2436: 
                   2437: static int make_raw_rw_request(void)
                   2438: {
                   2439:        int aligned_sector_t;
                   2440:        int max_sector, max_size, tracksize, ssize;
                   2441: 
                   2442:        set_fdc(DRIVE(CURRENT->rq_dev));
                   2443: 
                   2444:        raw_cmd = &default_raw_cmd;
                   2445:        raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
                   2446:                FD_RAW_NEED_SEEK;
                   2447:        raw_cmd->cmd_count = NR_RW;
                   2448:        if (CURRENT->cmd == READ){
                   2449:                raw_cmd->flags |= FD_RAW_READ;
                   2450:                COMMAND = FM_MODE(_floppy,FD_READ);
                   2451:        } else if (CURRENT->cmd == WRITE){
                   2452:                raw_cmd->flags |= FD_RAW_WRITE;
                   2453:                COMMAND = FM_MODE(_floppy,FD_WRITE);
                   2454:        } else {
                   2455:                DPRINT("make_raw_rw_request: unknown command\n");
                   2456:                return 0;
                   2457:        }
                   2458: 
                   2459:        max_sector = _floppy->sect * _floppy->head;
                   2460: 
                   2461:        TRACK = CURRENT->sector / max_sector;
                   2462:        sector_t = CURRENT->sector % max_sector;
                   2463:        if (_floppy->track && TRACK >= _floppy->track)
                   2464:                return 0;
                   2465:        HEAD = sector_t / _floppy->sect;
                   2466: 
                   2467:        if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
                   2468:            sector_t < _floppy->sect)
                   2469:                max_sector = _floppy->sect;
                   2470: 
                   2471:        /* 2M disks have phantom sectors on the first track */
                   2472:        if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
                   2473:                max_sector = 2 * _floppy->sect / 3;
                   2474:                if (sector_t >= max_sector){
                   2475:                        current_count_sectors = minimum(_floppy->sect - sector_t,
                   2476:                                                        CURRENT->nr_sectors);
                   2477:                        return 1;
                   2478:                }
                   2479:                SIZECODE = 2;
                   2480:        } else
                   2481:                SIZECODE = FD_SIZECODE(_floppy);
                   2482:        raw_cmd->rate = _floppy->rate & 0x43;
                   2483:        if ((_floppy->rate & FD_2M) &&
                   2484:            (TRACK || HEAD) &&
                   2485:            raw_cmd->rate == 2)
                   2486:                raw_cmd->rate = 1;
                   2487: 
                   2488:        if (SIZECODE)
                   2489:                SIZECODE2 = 0xff;
                   2490:        else
                   2491:                SIZECODE2 = 0x80;
                   2492:        raw_cmd->track = TRACK << STRETCH(_floppy);
                   2493:        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
                   2494:        GAP = _floppy->gap;
                   2495:        CODE2SIZE;
                   2496:        SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
                   2497:        SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
                   2498:        tracksize = _floppy->sect - _floppy->sect % ssize;
                   2499:        if (tracksize < _floppy->sect){
                   2500:                SECT_PER_TRACK ++;
                   2501:                if (tracksize <= sector_t % _floppy->sect)
                   2502:                        SECTOR--;
                   2503:                while (tracksize <= sector_t % _floppy->sect){
                   2504:                        while(tracksize + ssize > _floppy->sect){
                   2505:                                SIZECODE--;
                   2506:                                ssize >>= 1;
                   2507:                        }
                   2508:                        SECTOR++; SECT_PER_TRACK ++;
                   2509:                        tracksize += ssize;
                   2510:                }
                   2511:                max_sector = HEAD * _floppy->sect + tracksize;
                   2512:        } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing)
                   2513:                max_sector = _floppy->sect;
                   2514: 
                   2515:        aligned_sector_t = sector_t - (sector_t % _floppy->sect) % ssize;
                   2516:        max_size = CURRENT->nr_sectors;
                   2517:        if ((raw_cmd->track == buffer_track) && 
                   2518:            (current_drive == buffer_drive) &&
                   2519:            (sector_t >= buffer_min) && (sector_t < buffer_max)) {
                   2520:                /* data already in track buffer */
                   2521:                if (CT(COMMAND) == FD_READ) {
                   2522:                        copy_buffer(1, max_sector, buffer_max);
                   2523:                        return 1;
                   2524:                }
                   2525:        } else if (aligned_sector_t != sector_t || CURRENT->nr_sectors < ssize){
                   2526:                if (CT(COMMAND) == FD_WRITE){
                   2527:                        if (sector_t + CURRENT->nr_sectors > ssize &&
                   2528:                            sector_t + CURRENT->nr_sectors < ssize + ssize)
                   2529:                                max_size = ssize + ssize;
                   2530:                        else
                   2531:                                max_size = ssize;
                   2532:                }
                   2533:                raw_cmd->flags &= ~FD_RAW_WRITE;
                   2534:                raw_cmd->flags |= FD_RAW_READ;
                   2535:                COMMAND = FM_MODE(_floppy,FD_READ);
                   2536:        } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
                   2537:                unsigned long dma_limit;
                   2538:                int direct, indirect;
                   2539: 
                   2540:                indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
                   2541:                        sector_t;
                   2542: 
                   2543:                /*
                   2544:                 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
                   2545:                 * on a 64 bit machine!
                   2546:                 */
                   2547:                max_size = buffer_chain_size();
                   2548:                dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
                   2549:                if ((unsigned long) max_size > dma_limit) {
                   2550:                        max_size = dma_limit;
                   2551:                }
                   2552:                /* 64 kb boundaries */
                   2553:                if (CROSS_64KB(CURRENT->buffer, max_size << 9))
                   2554:                        max_size = (K_64 - ((long) CURRENT->buffer) % K_64)>>9;
                   2555:                direct = transfer_size(ssize,max_sector,max_size) - sector_t;
                   2556:                /*
                   2557:                 * We try to read tracks, but if we get too many errors, we
                   2558:                 * go back to reading just one sector at a time.
                   2559:                 *
                   2560:                 * This means we should be able to read a sector even if there
                   2561:                 * are other bad sectors on this track.
                   2562:                 */
                   2563:                if (!direct ||
                   2564:                    (indirect * 2 > direct * 3 &&
                   2565:                     *errors < DP->max_errors.read_track &&
                   2566:                     /*!TESTF(FD_NEED_TWADDLE) &&*/
                   2567:                     ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
                   2568:                        max_size = CURRENT->nr_sectors;
                   2569:                } else {
                   2570:                        raw_cmd->kernel_data = CURRENT->buffer;
                   2571:                        raw_cmd->length = current_count_sectors << 9;
                   2572:                        if (raw_cmd->length == 0){
                   2573:                                DPRINT("zero dma transfer attempted from make_raw_request\n");
                   2574:                                DPRINT("indirect=%d direct=%d sector_t=%d",
                   2575:                                        indirect, direct, sector_t);
                   2576:                                return 0;
                   2577:                        }
                   2578:                        return 2;
                   2579:                }
                   2580:        }
                   2581: 
                   2582:        if (CT(COMMAND) == FD_READ)
                   2583:                max_size = max_sector; /* unbounded */
                   2584: 
                   2585:        /* claim buffer track if needed */
                   2586:        if (buffer_track != raw_cmd->track ||  /* bad track */
                   2587:            buffer_drive !=current_drive || /* bad drive */
                   2588:            sector_t > buffer_max ||
                   2589:            sector_t < buffer_min ||
                   2590:            ((CT(COMMAND) == FD_READ ||
                   2591:              (aligned_sector_t == sector_t && CURRENT->nr_sectors >= ssize))&&
                   2592:             max_sector > 2 * max_buffer_sectors + buffer_min &&
                   2593:             max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
                   2594:            /* not enough space */){
                   2595:                buffer_track = -1;
                   2596:                buffer_drive = current_drive;
                   2597:                buffer_max = buffer_min = aligned_sector_t;
                   2598:        }
                   2599:        raw_cmd->kernel_data = floppy_track_buffer + 
                   2600:                ((aligned_sector_t-buffer_min)<<9);
                   2601: 
                   2602:        if (CT(COMMAND) == FD_WRITE){
                   2603:                /* copy write buffer to track buffer.
                   2604:                 * if we get here, we know that the write
                   2605:                 * is either aligned or the data already in the buffer
                   2606:                 * (buffer will be overwritten) */
                   2607: #ifdef FLOPPY_SANITY_CHECK
                   2608:                if (sector_t != aligned_sector_t && buffer_track == -1)
                   2609:                        DPRINT("internal error offset !=0 on write\n");
                   2610: #endif
                   2611:                buffer_track = raw_cmd->track;
                   2612:                buffer_drive = current_drive;
                   2613:                copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
                   2614:        } else
                   2615:                transfer_size(ssize, max_sector,
                   2616:                              2*max_buffer_sectors+buffer_min-aligned_sector_t);
                   2617: 
                   2618:        /* round up current_count_sectors to get dma xfer size */
                   2619:        raw_cmd->length = sector_t+current_count_sectors-aligned_sector_t;
                   2620:        raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
                   2621:        raw_cmd->length <<= 9;
                   2622: #ifdef FLOPPY_SANITY_CHECK
                   2623:        if ((raw_cmd->length < current_count_sectors << 9) ||
                   2624:            (raw_cmd->kernel_data != CURRENT->buffer &&
                   2625:             CT(COMMAND) == FD_WRITE &&
                   2626:             (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
                   2627:              aligned_sector_t < buffer_min)) ||
                   2628:            raw_cmd->length % (128 << SIZECODE) ||
                   2629:            raw_cmd->length <= 0 || current_count_sectors <= 0){
                   2630:                DPRINT("fractionary current count b=%lx s=%lx\n",
                   2631:                        raw_cmd->length, current_count_sectors);
                   2632:                if (raw_cmd->kernel_data != CURRENT->buffer)
                   2633:                        printk("addr=%d, length=%ld\n",
                   2634:                               (int) ((raw_cmd->kernel_data - 
                   2635:                                       floppy_track_buffer) >> 9),
                   2636:                               current_count_sectors);
                   2637:                printk("st=%d ast=%d mse=%d msi=%d\n",
                   2638:                       sector_t, aligned_sector_t, max_sector, max_size);
                   2639:                printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
                   2640:                printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
                   2641:                       COMMAND, SECTOR, HEAD, TRACK);
                   2642:                printk("buffer drive=%d\n", buffer_drive);
                   2643:                printk("buffer track=%d\n", buffer_track);
                   2644:                printk("buffer_min=%d\n", buffer_min);
                   2645:                printk("buffer_max=%d\n", buffer_max);
                   2646:                return 0;
                   2647:        }
                   2648: 
                   2649:        if (raw_cmd->kernel_data != CURRENT->buffer){
                   2650:                if (raw_cmd->kernel_data < floppy_track_buffer ||
                   2651:                    current_count_sectors < 0 ||
                   2652:                    raw_cmd->length < 0 ||
                   2653:                    raw_cmd->kernel_data + raw_cmd->length >
                   2654:                    floppy_track_buffer + (max_buffer_sectors  << 10)){
                   2655:                        DPRINT("buffer overrun in schedule dma\n");
                   2656:                        printk("sector_t=%d buffer_min=%d current_count=%ld\n",
                   2657:                               sector_t, buffer_min,
                   2658:                               raw_cmd->length >> 9);
                   2659:                        printk("current_count_sectors=%ld\n",
                   2660:                               current_count_sectors);
                   2661:                        if (CT(COMMAND) == FD_READ)
                   2662:                                printk("read\n");
                   2663:                        if (CT(COMMAND) == FD_READ)
                   2664:                                printk("write\n");
                   2665:                        return 0;
                   2666:                }
                   2667:        } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
                   2668:                   current_count_sectors > CURRENT->nr_sectors){
                   2669:                DPRINT("buffer overrun in direct transfer\n");
                   2670:                return 0;
                   2671:        } else if (raw_cmd->length < current_count_sectors << 9){
                   2672:                DPRINT("more sectors than bytes\n");
                   2673:                printk("bytes=%ld\n", raw_cmd->length >> 9);
                   2674:                printk("sectors=%ld\n", current_count_sectors);
                   2675:        }
                   2676:        if (raw_cmd->length == 0){
                   2677:                DPRINT("zero dma transfer attempted from make_raw_request\n");
                   2678:                return 0;
                   2679:        }
                   2680: #endif
                   2681:        return 2;
                   2682: }
                   2683: 
                   2684: static void redo_fd_request(void)
                   2685: {
                   2686: #define REPEAT {request_done(0); continue; }
                   2687:        kdev_t device;
                   2688:        int tmp;
                   2689: 
                   2690:        lastredo = jiffies;
                   2691:        if (current_drive < N_DRIVE)
                   2692:                floppy_off(current_drive);
                   2693: 
                   2694:        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
                   2695:                CLEAR_INTR;
                   2696:                unlock_fdc();
                   2697:                return;
                   2698:        }
                   2699: 
                   2700:        while(1){
                   2701:                if (!CURRENT) {
                   2702:                        CLEAR_INTR;
                   2703:                        unlock_fdc();
                   2704:                        return;
                   2705:                }
                   2706:                if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
                   2707:                        panic(DEVICE_NAME ": request list destroyed");
                   2708:                if (CURRENT->bh && !buffer_locked(CURRENT->bh))
                   2709:                        panic(DEVICE_NAME ": block not locked");
                   2710: 
                   2711:                device = CURRENT->rq_dev;
                   2712:                set_fdc(DRIVE(device));
                   2713:                reschedule_timeout(CURRENTD, "redo fd request", 0);
                   2714: 
                   2715:                set_floppy(device);
                   2716:                raw_cmd = & default_raw_cmd;
                   2717:                raw_cmd->flags = 0;
                   2718:                if (start_motor(redo_fd_request)) return;
                   2719:                disk_change(current_drive);
                   2720:                if (test_bit(current_drive, &fake_change) ||
                   2721:                   TESTF(FD_DISK_CHANGED)){
                   2722:                        DPRINT("disk absent or changed during operation\n");
                   2723:                        REPEAT;
                   2724:                }
                   2725:                if (!_floppy) { /* Autodetection */
                   2726:                        if (!probing){
                   2727:                                DRS->probed_format = 0;
                   2728:                                if (next_valid_format()){
                   2729:                                        DPRINT("no autodetectable formats\n");
                   2730:                                        _floppy = NULL;
                   2731:                                        REPEAT;
                   2732:                                }
                   2733:                        }
                   2734:                        probing = 1;
                   2735:                        _floppy = floppy_type+DP->autodetect[DRS->probed_format];
                   2736:                } else
                   2737:                        probing = 0;
                   2738:                errors = & (CURRENT->errors);
                   2739:                tmp = make_raw_rw_request();
                   2740:                if (tmp < 2){
                   2741:                        request_done(tmp);
                   2742:                        continue;
                   2743:                }
                   2744: 
                   2745:                if (TESTF(FD_NEED_TWADDLE))
                   2746:                        twaddle();
                   2747:                floppy_tq.routine = (void *)(void *) floppy_start;
                   2748:                queue_task(&floppy_tq, &tq_immediate);
                   2749:                mark_bh(IMMEDIATE_BH);
                   2750: #ifdef DEBUGT
                   2751:                debugt("queue fd request");
                   2752: #endif
                   2753:                return;
                   2754:        }
                   2755: #undef REPEAT
                   2756: }
                   2757: 
                   2758: static struct cont_t rw_cont={
                   2759:        rw_interrupt,
                   2760:        redo_fd_request,
                   2761:        bad_flp_intr,
                   2762:        request_done };
                   2763: 
                   2764: static struct tq_struct request_tq =
                   2765: { 0, 0, (void *) (void *) redo_fd_request, 0 };
                   2766: 
                   2767: static void process_fd_request(void)
                   2768: {
                   2769:        cont = &rw_cont;
                   2770:        queue_task(&request_tq, &tq_immediate);
                   2771:        mark_bh(IMMEDIATE_BH);
                   2772: }
                   2773: 
                   2774: static void do_fd_request(void)
                   2775: {
                   2776:        sti();
                   2777:        if (fdc_busy){
                   2778:                /* fdc busy, this new request will be treated when the
                   2779:                   current one is done */
                   2780:                is_alive("do fd request, old request running");
                   2781:                return;
                   2782:        }
                   2783:        lock_fdc(MAXTIMEOUT,0);
                   2784:        process_fd_request();
                   2785:        is_alive("do fd request");
                   2786: }
                   2787: 
                   2788: static struct cont_t poll_cont={
                   2789:        success_and_wakeup,
                   2790:        floppy_ready,
                   2791:        generic_failure,
                   2792:        generic_done };
                   2793: 
                   2794: static int poll_drive(int interruptible, int flag)
                   2795: {
                   2796:        int ret;
                   2797:        /* no auto-sense, just clear dcl */
                   2798:        raw_cmd = &default_raw_cmd;
                   2799:        raw_cmd->flags= flag;
                   2800:        raw_cmd->track=0;
                   2801:        raw_cmd->cmd_count=0;
                   2802:        cont = &poll_cont;
                   2803: #ifdef DCL_DEBUG
                   2804:        if (DP->flags & FD_DEBUG){
                   2805:                DPRINT("setting NEWCHANGE in poll_drive\n");
                   2806:        }
                   2807: #endif
                   2808:        SETF(FD_DISK_NEWCHANGE);
                   2809:        WAIT(floppy_ready);
                   2810:        return ret;
                   2811: }
                   2812: 
                   2813: /*
                   2814:  * User triggered reset
                   2815:  * ====================
                   2816:  */
                   2817: 
                   2818: static void reset_intr(void)
                   2819: {
                   2820:        printk("weird, reset interrupt called\n");
                   2821: }
                   2822: 
                   2823: static struct cont_t reset_cont={
                   2824:        reset_intr,
                   2825:        success_and_wakeup,
                   2826:        generic_failure,
                   2827:        generic_done };
                   2828: 
                   2829: static int user_reset_fdc(int drive, int arg, int interruptible)
                   2830: {
                   2831:        int ret;
                   2832: 
                   2833:        ret=0;
                   2834:        LOCK_FDC(drive,interruptible);
                   2835:        if (arg == FD_RESET_ALWAYS)
                   2836:                FDCS->reset=1;
                   2837:        if (FDCS->reset){
                   2838:                cont = &reset_cont;
                   2839:                WAIT(reset_fdc);
                   2840:        }
                   2841:        process_fd_request();
                   2842:        return ret;
                   2843: }
                   2844: 
                   2845: /*
                   2846:  * Misc Ioctl's and support
                   2847:  * ========================
                   2848:  */
                   2849: static int fd_copyout(void *param, const void *address, int size)
                   2850: {
                   2851:        int ret;
                   2852: 
                   2853:        ECALL(verify_area(VERIFY_WRITE,param,size));
                   2854:        memcpy_tofs(param,(void *) address, size);
                   2855:        return 0;
                   2856: }
                   2857: 
                   2858: static int fd_copyin(void *param, void *address, int size)
                   2859: {
                   2860:        int ret;
                   2861: 
                   2862:        ECALL(verify_area(VERIFY_READ,param,size));
                   2863:        memcpy_fromfs((void *) address, param, size);
                   2864:        return 0;
                   2865: }
                   2866: 
                   2867: #define COPYOUT(x) ECALL(fd_copyout((void *)param, &(x), sizeof(x)))
                   2868: #define COPYIN(x) ECALL(fd_copyin((void *)param, &(x), sizeof(x)))
                   2869: 
                   2870: static inline const char *drive_name(int type, int drive)
                   2871: {
                   2872:        struct floppy_struct *floppy;
                   2873: 
                   2874:        if (type)
                   2875:                floppy = floppy_type + type;
                   2876:        else {
                   2877:                if (UDP->native_format)
                   2878:                        floppy = floppy_type + UDP->native_format;
                   2879:                else
                   2880:                        return "(null)";
                   2881:        }
                   2882:        if (floppy->name)
                   2883:                return floppy->name;
                   2884:        else
                   2885:                return "(null)";
                   2886: }
                   2887: 
                   2888: 
                   2889: /* raw commands */
                   2890: static void raw_cmd_done(int flag)
                   2891: {
                   2892:        int i;
                   2893: 
                   2894:        if (!flag) {
                   2895:                raw_cmd->flags |= FD_RAW_FAILURE;
                   2896:                raw_cmd->flags |= FD_RAW_HARDFAILURE;
                   2897:        } else {
                   2898:                raw_cmd->reply_count = inr;
                   2899:                for (i=0; i< raw_cmd->reply_count; i++)
                   2900:                        raw_cmd->reply[i] = reply_buffer[i];
                   2901: 
                   2902:                if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
                   2903:                        raw_cmd->length = fd_get_dma_residue();
                   2904:                
                   2905:                if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
                   2906:                    (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
                   2907:                        raw_cmd->flags |= FD_RAW_FAILURE;
                   2908: 
                   2909:                if (disk_change(current_drive))
                   2910:                        raw_cmd->flags |= FD_RAW_DISK_CHANGE;
                   2911:                else
                   2912:                        raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
                   2913:                if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
                   2914:                        motor_off_callback(current_drive);
                   2915: 
                   2916:                if (raw_cmd->next &&
                   2917:                   (!(raw_cmd->flags & FD_RAW_FAILURE) ||
                   2918:                    !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
                   2919:                   ((raw_cmd->flags & FD_RAW_FAILURE) ||
                   2920:                    !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
                   2921:                        raw_cmd = raw_cmd->next;
                   2922:                        return;
                   2923:                }
                   2924:        }
                   2925:        generic_done(flag);
                   2926: }
                   2927: 
                   2928: 
                   2929: static struct cont_t raw_cmd_cont={
                   2930:        success_and_wakeup,
                   2931:        floppy_start,
                   2932:        generic_failure,
                   2933:        raw_cmd_done
                   2934: };
                   2935: 
                   2936: static inline int raw_cmd_copyout(int cmd, char *param,
                   2937:                                  struct floppy_raw_cmd *ptr)
                   2938: {
                   2939:        struct old_floppy_raw_cmd old_raw_cmd;
                   2940:        int ret;
                   2941: 
                   2942:        while(ptr) {
                   2943:                if (cmd == OLDFDRAWCMD) {
                   2944:                        old_raw_cmd.flags = ptr->flags;
                   2945:                        old_raw_cmd.data = ptr->data;
                   2946:                        old_raw_cmd.length = ptr->length;
                   2947:                        old_raw_cmd.rate = ptr->rate;
                   2948:                        old_raw_cmd.reply_count = ptr->reply_count;
                   2949:                        memcpy(old_raw_cmd.reply, ptr->reply, 7);
                   2950:                        COPYOUT(old_raw_cmd);
                   2951:                        param += sizeof(old_raw_cmd);
                   2952:                } else {
                   2953:                        COPYOUT(*ptr);
                   2954:                        param += sizeof(struct floppy_raw_cmd);
                   2955:                }
                   2956: 
                   2957:                if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
                   2958:                        if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
                   2959:                                ECALL(fd_copyout(ptr->data, 
                   2960:                                                 ptr->kernel_data, 
                   2961:                                                 ptr->buffer_length - 
                   2962:                                                 ptr->length));
                   2963:                }
                   2964:                ptr = ptr->next;
                   2965:        }
                   2966:        return 0;
                   2967: }
                   2968: 
                   2969: 
                   2970: static void raw_cmd_free(struct floppy_raw_cmd **ptr)
                   2971: {
                   2972:        struct floppy_raw_cmd *next,*this;
                   2973: 
                   2974:        this = *ptr;
                   2975:        *ptr = 0;
                   2976:        while(this) {
                   2977:                if (this->buffer_length) {
                   2978:                        fd_dma_mem_free((unsigned long)this->kernel_data,
                   2979:                                        this->buffer_length);
                   2980:                        this->buffer_length = 0;
                   2981:                }
                   2982:                next = this->next;
                   2983:                kfree(this);
                   2984:                this = next;
                   2985:        }
                   2986: }
                   2987: 
                   2988: 
                   2989: static inline int raw_cmd_copyin(int cmd, char *param,
                   2990:                                 struct floppy_raw_cmd **rcmd)
                   2991: {
                   2992:        struct floppy_raw_cmd *ptr;
                   2993:        struct old_floppy_raw_cmd old_raw_cmd;
                   2994:        int ret;
                   2995:        int i;
                   2996:        
                   2997:        *rcmd = 0;
                   2998:        while(1) {
                   2999:                ptr = (struct floppy_raw_cmd *) 
                   3000:                        kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
                   3001:                if (!ptr)
                   3002:                        return -ENOMEM;
                   3003:                *rcmd = ptr;
                   3004:                if (cmd == OLDFDRAWCMD){
                   3005:                        COPYIN(old_raw_cmd);
                   3006:                        ptr->flags = old_raw_cmd.flags;
                   3007:                        ptr->data = old_raw_cmd.data;
                   3008:                        ptr->length = old_raw_cmd.length;
                   3009:                        ptr->rate = old_raw_cmd.rate;
                   3010:                        ptr->cmd_count = old_raw_cmd.cmd_count;
                   3011:                        ptr->track = old_raw_cmd.track;
                   3012:                        ptr->phys_length = 0;
                   3013:                        ptr->next = 0;
                   3014:                        ptr->buffer_length = 0;
                   3015:                        memcpy(ptr->cmd, old_raw_cmd.cmd, 9);
                   3016:                        param += sizeof(struct old_floppy_raw_cmd);
                   3017:                        if (ptr->cmd_count > 9)
                   3018:                                return -EINVAL;
                   3019:                } else {
                   3020:                        COPYIN(*ptr);
                   3021:                        ptr->next = 0;
                   3022:                        ptr->buffer_length = 0;
                   3023:                        param += sizeof(struct floppy_raw_cmd);
                   3024:                        if (ptr->cmd_count > 33)
                   3025:                                /* the command may now also take up the space
                   3026:                                 * initially intended for the reply & the
                   3027:                                 * reply count. Needed for long 82078 commands
                   3028:                                 * such as RESTORE, which takes ... 17 command
                   3029:                                 * bytes. Murphy's law #137: When you reserve
                   3030:                                 * 16 bytes for a structure, you'll one day
                   3031:                                 * discover that you really need 17...
                   3032:                                 */
                   3033:                                return -EINVAL;
                   3034:                }
                   3035: 
                   3036:                for (i=0; i< 16; i++)
                   3037:                        ptr->reply[i] = 0;
                   3038:                ptr->resultcode = 0;
                   3039:                ptr->kernel_data = 0;
                   3040: 
                   3041:                if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
                   3042:                        if (ptr->length <= 0)
                   3043:                                return -EINVAL;
                   3044:                        ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
                   3045:                        if (!ptr->kernel_data)
                   3046:                                return -ENOMEM;
                   3047:                        ptr->buffer_length = ptr->length;
                   3048:                }
                   3049:                if ( ptr->flags & FD_RAW_READ )
                   3050:                    ECALL( verify_area( VERIFY_WRITE, ptr->data, 
                   3051:                                        ptr->length ));
                   3052:                if (ptr->flags & FD_RAW_WRITE)
                   3053:                        ECALL(fd_copyin(ptr->data, ptr->kernel_data, 
                   3054:                                        ptr->length));
                   3055:                rcmd = & (ptr->next);
                   3056:                if (!(ptr->flags & FD_RAW_MORE))
                   3057:                        return 0;
                   3058:                ptr->rate &= 0x43;
                   3059:        }
                   3060: }
                   3061: 
                   3062: 
                   3063: static int raw_cmd_ioctl(int cmd, void *param)
                   3064: {
                   3065:        int drive, ret, ret2;
                   3066:        struct floppy_raw_cmd *my_raw_cmd;
                   3067: 
                   3068:        if (FDCS->rawcmd <= 1)
                   3069:                FDCS->rawcmd = 1;
                   3070:        for (drive= 0; drive < N_DRIVE; drive++){
                   3071:                if (FDC(drive) != fdc)
                   3072:                        continue;
                   3073:                if (drive == current_drive){
                   3074:                        if (UDRS->fd_ref > 1){
                   3075:                                FDCS->rawcmd = 2;
                   3076:                                break;
                   3077:                        }
                   3078:                } else if (UDRS->fd_ref){
                   3079:                        FDCS->rawcmd = 2;
                   3080:                        break;
                   3081:                }
                   3082:        }
                   3083: 
                   3084:        if (FDCS->reset)
                   3085:                return -EIO;
                   3086: 
                   3087:        ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
                   3088:        if (ret) {
                   3089:                raw_cmd_free(&my_raw_cmd);
                   3090:                return ret;
                   3091:        }
                   3092: 
                   3093:        raw_cmd = my_raw_cmd;
                   3094:        cont = &raw_cmd_cont;
                   3095:        ret=wait_til_done(floppy_start,1);
                   3096: #ifdef DCL_DEBUG
                   3097:        if (DP->flags & FD_DEBUG){
                   3098:                DPRINT("calling disk change from raw_cmd ioctl\n");
                   3099:        }
                   3100: #endif
                   3101: 
                   3102:        if (ret != -EINTR && FDCS->reset)
                   3103:                ret = -EIO;
                   3104: 
                   3105:        DRS->track = NO_TRACK;
                   3106: 
                   3107:        ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
                   3108:        if (!ret)
                   3109:                ret = ret2;
                   3110:        raw_cmd_free(&my_raw_cmd);
                   3111:        return ret;
                   3112: }
                   3113: 
                   3114: static int invalidate_drive(kdev_t rdev)
                   3115: {
                   3116:        /* invalidate the buffer track to force a reread */
                   3117:        set_bit(DRIVE(rdev), &fake_change);
                   3118:        process_fd_request();
                   3119:        check_disk_change(rdev);
                   3120:        return 0;
                   3121: }
                   3122: 
                   3123: 
                   3124: static inline void clear_write_error(int drive)
                   3125: {
                   3126:        CLEARSTRUCT(UDRWE);
                   3127: }
                   3128: 
                   3129: static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
                   3130:                               int drive, int type, kdev_t device)
                   3131: {
                   3132:        int cnt;
                   3133: 
                   3134:        /* sanity checking for parameters.*/
                   3135:        if (g->sect <= 0 ||
                   3136:            g->head <= 0 ||
                   3137:            g->track <= 0 ||
                   3138:            g->track > UDP->tracks>>STRETCH(g) ||
                   3139:            /* check if reserved bits are set */
                   3140:            (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
                   3141:                return -EINVAL;
                   3142:        if (type){
                   3143:                if (!suser())
                   3144:                        return -EPERM;
                   3145:                LOCK_FDC(drive,1);
                   3146:                for (cnt = 0; cnt < N_DRIVE; cnt++){
                   3147:                        if (ITYPE(drive_state[cnt].fd_device) == type &&
                   3148:                            drive_state[cnt].fd_ref)
                   3149:                                set_bit(drive, &fake_change);
                   3150:                }
                   3151:                floppy_type[type] = *g;
                   3152:                floppy_type[type].name="user format";
                   3153:                for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
                   3154:                        floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
                   3155:                                floppy_type[type].size>>1;
                   3156:                process_fd_request();
                   3157:                for (cnt = 0; cnt < N_DRIVE; cnt++){
                   3158:                        if (ITYPE(drive_state[cnt].fd_device) == type &&
                   3159:                            drive_state[cnt].fd_ref)
                   3160:                                check_disk_change(
                   3161:                                        MKDEV(FLOPPY_MAJOR,
                   3162:                                              drive_state[cnt].fd_device));
                   3163:                }
                   3164:        } else {
                   3165:                LOCK_FDC(drive,1);
                   3166:                if (cmd != FDDEFPRM)
                   3167:                        /* notice a disk change immediately, else
                   3168:                         * we loose our settings immediately*/
                   3169:                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                   3170:                user_params[drive] = *g;
                   3171:                if (buffer_drive == drive)
                   3172:                        SUPBOUND(buffer_max, user_params[drive].sect);
                   3173:                current_type[drive] = &user_params[drive];
                   3174:                floppy_sizes[drive] = user_params[drive].size >> 1;
                   3175:                if (cmd == FDDEFPRM)
                   3176:                        DRS->keep_data = -1;
                   3177:                else
                   3178:                        DRS->keep_data = 1;
                   3179:                /* invalidation. Invalidate only when needed, i.e.
                   3180:                 * when there are already sectors in the buffer cache
                   3181:                 * whose number will change. This is useful, because
                   3182:                 * mtools often changes the geometry of the disk after
                   3183:                 * looking at the boot block */
                   3184:                if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
                   3185:                        invalidate_drive(device);
                   3186:                else
                   3187:                        process_fd_request();
                   3188:        }
                   3189:        return 0;
                   3190: }
                   3191: 
                   3192: /* handle obsolete ioctl's */
                   3193: static struct translation_entry {
                   3194:     int newcmd;
                   3195:     int oldcmd;
                   3196:     int oldsize; /* size of 0x00xx-style ioctl. Reflects old structures, thus
                   3197:                  * use numeric values. NO SIZEOFS */
                   3198: } translation_table[]= {
                   3199:     {FDCLRPRM,          0,  0},
                   3200:     {FDSETPRM,          1, 28},
                   3201:     {FDDEFPRM,          2, 28},
                   3202:     {FDGETPRM,          3, 28},
                   3203:     {FDMSGON,           4,  0},
                   3204:     {FDMSGOFF,          5,  0},
                   3205:     {FDFMTBEG,          6,  0},
                   3206:     {FDFMTTRK,          7, 12},
                   3207:     {FDFMTEND,          8,  0},
                   3208:     {FDSETEMSGTRESH,   10,  0},
                   3209:     {FDFLUSH,          11,  0},
                   3210:     {FDSETMAXERRS,     12, 20},
                   3211:     {OLDFDRAWCMD,              30,  0},
                   3212:     {FDGETMAXERRS,     14, 20},
                   3213:     {FDGETDRVTYP,      16, 16},
                   3214:     {FDSETDRVPRM,      20, 88},
                   3215:     {FDGETDRVPRM,      21, 88},
                   3216:     {FDGETDRVSTAT,     22, 52},
                   3217:     {FDPOLLDRVSTAT,    23, 52},
                   3218:     {FDRESET,          24,  0},
                   3219:     {FDGETFDCSTAT,     25, 40},
                   3220:     {FDWERRORCLR,      27,  0},
                   3221:     {FDWERRORGET,      28, 24},
                   3222:     {FDRAWCMD,          0,  0},
                   3223:     {FDEJECT,           0,  0},
                   3224:     {FDTWADDLE,                40,  0} };
                   3225: 
                   3226: static inline int normalize_0x02xx_ioctl(int *cmd, int *size)
                   3227: {
                   3228:        int i;
                   3229: 
                   3230:        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
                   3231:                if ((*cmd & 0xffff) == (translation_table[i].newcmd & 0xffff)){
                   3232:                        *size = _IOC_SIZE(*cmd);
                   3233:                        *cmd = translation_table[i].newcmd;
                   3234:                        if (*size > _IOC_SIZE(*cmd)) {
                   3235:                                printk("ioctl not yet supported\n");
                   3236:                                return -EFAULT;
                   3237:                        }
                   3238:                        return 0;
                   3239:                }
                   3240:        }
                   3241:        return -EINVAL;
                   3242: }
                   3243: 
                   3244: static inline int xlate_0x00xx_ioctl(int *cmd, int *size)
                   3245: {
                   3246:        int i;
                   3247:        /* old ioctls' for kernels <= 1.3.33 */
                   3248:        /* When the next even release will come around, we'll start
                   3249:         * warning against these.
                   3250:         * When the next odd release will come around, we'll fail with
                   3251:         * -EINVAL */
                   3252:        if(strcmp(system_utsname.version, "1.4.0") >= 0)
                   3253:                printk("obsolete floppy ioctl %x\n", *cmd);
                   3254:        if((system_utsname.version[0] == '1' &&
                   3255:            strcmp(system_utsname.version, "1.5.0") >= 0) ||
                   3256:           (system_utsname.version[0] >= '2' &&
                   3257:            strcmp(system_utsname.version, "2.1.0") >= 0))
                   3258:                return -EINVAL;
                   3259:        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
                   3260:                if (*cmd == translation_table[i].oldcmd) {
                   3261:                        *size = translation_table[i].oldsize;
                   3262:                        *cmd = translation_table[i].newcmd;
                   3263:                        return 0;
                   3264:                }
                   3265:        }
                   3266:        return -EINVAL;
                   3267: }
                   3268: 
                   3269: static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                   3270:                    unsigned long param)
                   3271: {
                   3272: #define IOCTL_MODE_BIT 8
                   3273: #define OPEN_WRITE_BIT 16
                   3274: #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
                   3275: #define OUT(c,x) case c: outparam = (const char *) (x); break
                   3276: #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
                   3277: 
                   3278:        int i,drive,type;
                   3279:        kdev_t device;
                   3280:        int ret;
                   3281:        int size;
                   3282:        union inparam {
                   3283:                struct floppy_struct g; /* geometry */
                   3284:                struct format_descr f;
                   3285:                struct floppy_max_errors max_errors;
                   3286:                struct floppy_drive_params dp;
                   3287:        } inparam; /* parameters coming from user space */
                   3288:        const char *outparam; /* parameters passed back to user space */
                   3289: 
                   3290:        device = inode->i_rdev;
                   3291:        switch (cmd) {
                   3292:                RO_IOCTLS(device,param);
                   3293:        }
                   3294:        type = TYPE(device);
                   3295:        drive = DRIVE(device);
                   3296: 
                   3297:        /* convert compatibility eject ioctls into floppy eject ioctl.
                   3298:         * We do this in order to provide a means to eject floppy disks before
                   3299:         * installing the new fdutils package */
                   3300:        if(cmd == CDROMEJECT || /* CD-ROM eject */
                   3301:           cmd == 0x6470 /* SunOS floppy eject */) {
                   3302:                DPRINT("obsolete eject ioctl\n");
                   3303:                DPRINT("please use floppycontrol --eject\n");
                   3304:                cmd = FDEJECT;
                   3305:        }
                   3306: 
                   3307:        /* convert the old style command into a new style command */
                   3308:        if ((cmd & 0xff00) == 0x0200) {
                   3309:                ECALL(normalize_0x02xx_ioctl(&cmd, &size));
                   3310:        } else if ((cmd & 0xff00) == 0x0000) {
                   3311:                ECALL(xlate_0x00xx_ioctl(&cmd, &size));
                   3312:        } else
                   3313:                return -EINVAL;
                   3314: 
                   3315:        /* permission checks */
                   3316:        if (((cmd & 0x80) && !suser()) ||
                   3317:             ((cmd & 0x40) && !IOCTL_ALLOWED))
                   3318:                return -EPERM;
                   3319: 
                   3320:        /* verify writability of result, and fail early */
                   3321:        if (_IOC_DIR(cmd) & _IOC_READ)
                   3322:                ECALL(verify_area(VERIFY_WRITE,(void *) param, size));
                   3323:                
                   3324:        /* copyin */
                   3325:        CLEARSTRUCT(&inparam);
                   3326:        if (_IOC_DIR(cmd) & _IOC_WRITE)
                   3327:                ECALL(fd_copyin((void *)param, &inparam, size))
                   3328: 
                   3329:        switch (cmd) {
                   3330:                case FDEJECT:
                   3331:                        if(UDRS->fd_ref != 1)
                   3332:                                /* somebody else has this drive open */
                   3333:                                return -EBUSY;
                   3334:                        LOCK_FDC(drive,1);
                   3335: 
                   3336:                        /* do the actual eject. Fails on
                   3337:                         * non-Sparc architectures */
                   3338:                        ret=fd_eject(UNIT(drive));
                   3339: 
                   3340:                        USETF(FD_DISK_CHANGED);
                   3341:                        USETF(FD_VERIFY);
                   3342:                        process_fd_request();
                   3343:                        return ret;                     
                   3344:                case FDCLRPRM:
                   3345:                        LOCK_FDC(drive,1);
                   3346:                        current_type[drive] = NULL;
                   3347:                        floppy_sizes[drive] = MAX_DISK_SIZE;
                   3348:                        UDRS->keep_data = 0;
                   3349:                        return invalidate_drive(device);
                   3350:                case FDSETPRM:
                   3351:                case FDDEFPRM:
                   3352:                        return set_geometry(cmd, & inparam.g,
                   3353:                                            drive, type, device);
                   3354:                case FDGETPRM:
                   3355:                        LOCK_FDC(drive,1);
                   3356:                        CALL(poll_drive(1,0));
                   3357:                        process_fd_request();
                   3358:                        if (type)
                   3359:                                outparam = (char *) &floppy_type[type];
                   3360:                        else 
                   3361:                                outparam = (char *) current_type[drive];
                   3362:                        if(!outparam)
                   3363:                                return -ENODEV;
                   3364:                        break;
                   3365: 
                   3366:                case FDMSGON:
                   3367:                        UDP->flags |= FTD_MSG;
                   3368:                        return 0;
                   3369:                case FDMSGOFF:
                   3370:                        UDP->flags &= ~FTD_MSG;
                   3371:                        return 0;
                   3372: 
                   3373:                case FDFMTBEG:
                   3374:                        LOCK_FDC(drive,1);
                   3375:                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                   3376:                        ret = UDRS->flags;
                   3377:                        process_fd_request();
                   3378:                        if(ret & FD_VERIFY)
                   3379:                                return -ENODEV;
                   3380:                        if(!(ret & FD_DISK_WRITABLE))
                   3381:                                return -EROFS;
                   3382:                        return 0;
                   3383:                case FDFMTTRK:
                   3384:                        if (UDRS->fd_ref != 1)
                   3385:                                return -EBUSY;
                   3386:                        return do_format(device, &inparam.f);
                   3387:                case FDFMTEND:
                   3388:                case FDFLUSH:
                   3389:                        LOCK_FDC(drive,1);
                   3390:                        return invalidate_drive(device);
                   3391: 
                   3392:                case FDSETEMSGTRESH:
                   3393:                        UDP->max_errors.reporting =
                   3394:                                (unsigned short) (param & 0x0f);
                   3395:                        return 0;
                   3396:                OUT(FDGETMAXERRS, &UDP->max_errors);
                   3397:                IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
                   3398: 
                   3399:                case FDGETDRVTYP:
                   3400:                        outparam = drive_name(type,drive);
                   3401:                        SUPBOUND(size,strlen(outparam)+1);
                   3402:                        break;
                   3403: 
                   3404:                IN(FDSETDRVPRM, UDP, dp);
                   3405:                OUT(FDGETDRVPRM, UDP);
                   3406: 
                   3407:                case FDPOLLDRVSTAT:
                   3408:                        LOCK_FDC(drive,1);
                   3409:                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                   3410:                        process_fd_request();
                   3411:                        /* fall through */
                   3412:                OUT(FDGETDRVSTAT, UDRS);
                   3413: 
                   3414:                case FDRESET:
                   3415:                        return user_reset_fdc(drive, (int)param, 1);
                   3416: 
                   3417:                OUT(FDGETFDCSTAT,UFDCS);
                   3418: 
                   3419:                case FDWERRORCLR:
                   3420:                        CLEARSTRUCT(UDRWE);
                   3421:                        return 0;
                   3422:                OUT(FDWERRORGET,UDRWE);
                   3423: 
                   3424:                case OLDFDRAWCMD:
                   3425:                case FDRAWCMD:
                   3426:                        if (type)
                   3427:                                return -EINVAL;
                   3428:                        LOCK_FDC(drive,1);
                   3429:                        set_floppy(device);
                   3430:                        CALL(i = raw_cmd_ioctl(cmd,(void *) param));
                   3431:                        process_fd_request();
                   3432:                        return i;
                   3433: 
                   3434:                case FDTWADDLE:
                   3435:                        LOCK_FDC(drive,1);
                   3436:                        twaddle();
                   3437:                        process_fd_request();
                   3438:                        return 0;
                   3439: 
                   3440:                default:
                   3441:                        return -EINVAL;
                   3442:        }
                   3443: 
                   3444:        if (_IOC_DIR(cmd) & _IOC_READ)
                   3445:                return fd_copyout((void *)param, outparam, size);
                   3446:        else
                   3447:                return 0;
                   3448: #undef IOCTL_ALLOWED
                   3449: #undef OUT
                   3450: #undef IN
                   3451: }
                   3452: 
                   3453: static void config_types(void)
                   3454: {
                   3455:        int first=1;
                   3456:        int drive;
                   3457: 
                   3458:        /* read drive info out of physical CMOS */
                   3459:        drive=0;
                   3460:        if (!UDP->cmos)
                   3461:                UDP->cmos= FLOPPY0_TYPE;
                   3462:        drive=1;
                   3463:        if (!UDP->cmos && FLOPPY1_TYPE)
                   3464:                UDP->cmos = FLOPPY1_TYPE;
                   3465: 
                   3466:        /* XXX */
                   3467:        /* additional physical CMOS drive detection should go here */
                   3468: 
                   3469:        for (drive=0; drive < N_DRIVE; drive++){
                   3470:                if (UDP->cmos >= 16)
                   3471:                        UDP->cmos = 0;
                   3472:                if (UDP->cmos >= 0 && UDP->cmos <= NUMBER(default_drive_params))
                   3473:                        memcpy((char *) UDP,
                   3474:                               (char *) (&default_drive_params[(int)UDP->cmos].params),
                   3475:                               sizeof(struct floppy_drive_params));
                   3476:                if (UDP->cmos){
                   3477:                        if (first)
                   3478:                                printk(KERN_INFO "Floppy drive(s): ");
                   3479:                        else
                   3480:                                printk(", ");
                   3481:                        first=0;
                   3482:                        if (UDP->cmos > 0){
                   3483:                                allowed_drive_mask |= 1 << drive;
                   3484:                                printk("fd%d is %s", drive,
                   3485:                                       default_drive_params[(int)UDP->cmos].name);
                   3486:                        } else
                   3487:                                printk("fd%d is unknown type %d",drive,
                   3488:                                       UDP->cmos);
                   3489:                }
                   3490:        }
                   3491:        if (!first)
                   3492:                printk("\n");
                   3493: }
                   3494: 
                   3495: static int floppy_read(struct inode * inode, struct file * filp,
                   3496:                       char * buf, int count)
                   3497: {
                   3498:        int drive = DRIVE(inode->i_rdev);
                   3499: 
                   3500:        check_disk_change(inode->i_rdev);
                   3501:        if (UTESTF(FD_DISK_CHANGED))
                   3502:                return -ENXIO;
                   3503:        return block_read(inode, filp, buf, count);
                   3504: }
                   3505: 
                   3506: static int floppy_write(struct inode * inode, struct file * filp,
                   3507:                        const char * buf, int count)
                   3508: {
                   3509:        int block;
                   3510:        int ret;
                   3511:        int drive = DRIVE(inode->i_rdev);
                   3512: 
                   3513:        if (!UDRS->maxblock)
                   3514:                UDRS->maxblock=1;/* make change detectable */
                   3515:        check_disk_change(inode->i_rdev);
                   3516:        if (UTESTF(FD_DISK_CHANGED))
                   3517:                return -ENXIO;
                   3518:        if (!UTESTF(FD_DISK_WRITABLE))
                   3519:                return -EROFS;
                   3520:        block = (filp->f_pos + count) >> 9;
                   3521:        INFBOUND(UDRS->maxblock, block);
                   3522:        ret= block_write(inode, filp, buf, count);
                   3523:        return ret;
                   3524: }
                   3525: 
                   3526: static void floppy_release(struct inode * inode, struct file * filp)
                   3527: {
                   3528:        int drive;
                   3529: 
                   3530:        drive = DRIVE(inode->i_rdev);
                   3531: 
                   3532:        if (!filp || (filp->f_mode & (2 | OPEN_WRITE_BIT)))
                   3533:                /* if the file is mounted OR (writable now AND writable at
                   3534:                 * open time) Linus: Does this cover all cases? */
                   3535:                block_fsync(inode,filp);
                   3536: 
                   3537:        if (UDRS->fd_ref < 0)
                   3538:                UDRS->fd_ref=0;
                   3539:        else if (!UDRS->fd_ref--) {
                   3540:                DPRINT("floppy_release with fd_ref == 0");
                   3541:                UDRS->fd_ref = 0;
                   3542:        }
                   3543:        floppy_release_irq_and_dma();
                   3544: }
                   3545: 
                   3546: /*
                   3547:  * floppy_open check for aliasing (/dev/fd0 can be the same as
                   3548:  * /dev/PS0 etc), and disallows simultaneous access to the same
                   3549:  * drive with different device numbers.
                   3550:  */
                   3551: #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
                   3552: 
                   3553: static int floppy_open(struct inode * inode, struct file * filp)
                   3554: {
                   3555:        int drive;
                   3556:        int old_dev;
                   3557:        int try;
                   3558:        char *tmp;
                   3559: 
                   3560:        if (!filp) {
                   3561:                DPRINT("Weird, open called with filp=0\n");
                   3562:                return -EIO;
                   3563:        }
                   3564: 
                   3565:        drive = DRIVE(inode->i_rdev);
                   3566:        if (drive >= N_DRIVE ||
                   3567:            !(allowed_drive_mask & (1 << drive)) ||
                   3568:            fdc_state[FDC(drive)].version == FDC_NONE)
                   3569:                return -ENXIO;
                   3570: 
                   3571:        if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
                   3572:                return -ENXIO;
                   3573:        old_dev = UDRS->fd_device;
                   3574:        if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
                   3575:                return -EBUSY;
                   3576: 
                   3577:        if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
                   3578:                USETF(FD_DISK_CHANGED);
                   3579:                USETF(FD_VERIFY);
                   3580:        }
                   3581: 
                   3582:        if (UDRS->fd_ref == -1 ||
                   3583:           (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
                   3584:                return -EBUSY;
                   3585: 
                   3586:        if (floppy_grab_irq_and_dma())
                   3587:                return -EBUSY;
                   3588: 
                   3589:        if (filp->f_flags & O_EXCL)
                   3590:                UDRS->fd_ref = -1;
                   3591:        else
                   3592:                UDRS->fd_ref++;
                   3593: 
                   3594:        if (!floppy_track_buffer){
                   3595:                /* if opening an ED drive, reserve a big buffer,
                   3596:                 * else reserve a small one */
                   3597:                if ((UDP->cmos == 6) || (UDP->cmos == 5))
                   3598:                        try = 64; /* Only 48 actually useful */
                   3599:                else
                   3600:                        try = 32; /* Only 24 actually useful */
                   3601: 
                   3602:                tmp=(char *)fd_dma_mem_alloc(1024 * try);
                   3603:                if (!tmp) {
                   3604:                        try >>= 1; /* buffer only one side */
                   3605:                        INFBOUND(try, 16);
                   3606:                        tmp= (char *)fd_dma_mem_alloc(1024*try);
                   3607:                }
                   3608:                if (!tmp) {
                   3609:                        DPRINT("Unable to allocate DMA memory\n");
                   3610:                        RETERR(ENXIO);
                   3611:                }
                   3612:                if (floppy_track_buffer)
                   3613:                        fd_dma_mem_free((unsigned long)tmp,try*1024);
                   3614:                else {
                   3615:                        buffer_min = buffer_max = -1;
                   3616:                        floppy_track_buffer = tmp;
                   3617:                        max_buffer_sectors = try;
                   3618:                }
                   3619:        }
                   3620: 
                   3621:        UDRS->fd_device = MINOR(inode->i_rdev);
                   3622:        if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
                   3623:                if (buffer_drive == drive)
                   3624:                        buffer_track = -1;
                   3625:                invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
                   3626:        }
                   3627: 
                   3628:        /* Allow ioctls if we have write-permissions even if read-only open */
                   3629:        if ((filp->f_mode & 2) || (permission(inode,2) == 0))
                   3630:                filp->f_mode |= IOCTL_MODE_BIT;
                   3631:        if (filp->f_mode & 2)
                   3632:                filp->f_mode |= OPEN_WRITE_BIT;
                   3633: 
                   3634:        if (UFDCS->rawcmd == 1)
                   3635:                UFDCS->rawcmd = 2;
                   3636: 
                   3637:        if (filp->f_flags & O_NDELAY)
                   3638:                return 0;
                   3639:        if (filp->f_mode & 3) {
                   3640:                UDRS->last_checked = 0;
                   3641:                check_disk_change(inode->i_rdev);
                   3642:                if (UTESTF(FD_DISK_CHANGED))
                   3643:                        RETERR(ENXIO);
                   3644:        }
                   3645:        if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
                   3646:                RETERR(EROFS);
                   3647:        return 0;
                   3648: #undef RETERR
                   3649: }
                   3650: 
                   3651: /*
                   3652:  * Check if the disk has been changed or if a change has been faked.
                   3653:  */
                   3654: static int check_floppy_change(kdev_t dev)
                   3655: {
                   3656:        int drive = DRIVE(dev);
                   3657: 
                   3658:        if (MAJOR(dev) != MAJOR_NR) {
                   3659:                DPRINT("check_floppy_change: not a floppy\n");
                   3660:                return 0;
                   3661:        }
                   3662: 
                   3663:        if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
                   3664:                return 1;
                   3665: 
                   3666:        if (UDP->checkfreq < jiffies - UDRS->last_checked){
                   3667:                lock_fdc(drive,0);
                   3668:                poll_drive(0,0);
                   3669:                process_fd_request();
                   3670:        }
                   3671: 
                   3672:        if (UTESTF(FD_DISK_CHANGED) ||
                   3673:           UTESTF(FD_VERIFY) ||
                   3674:           test_bit(drive, &fake_change) ||
                   3675:           (!TYPE(dev) && !current_type[drive]))
                   3676:                return 1;
                   3677:        return 0;
                   3678: }
                   3679: 
                   3680: /* revalidate the floppy disk, i.e. trigger format autodetection by reading
                   3681:  * the bootblock (block 0). "Autodetection" is also needed to check whether
                   3682:  * there is a disk in the drive at all... Thus we also do it for fixed
                   3683:  * geometry formats */
                   3684: static int floppy_revalidate(kdev_t dev)
                   3685: {
                   3686: #define NO_GEOM (!current_type[drive] && !TYPE(dev))
                   3687:        struct buffer_head * bh;
                   3688:        int drive=DRIVE(dev);
                   3689:        int cf;
                   3690: 
                   3691:        if (UTESTF(FD_DISK_CHANGED) ||
                   3692:           UTESTF(FD_VERIFY) ||
                   3693:           test_bit(drive, &fake_change) ||
                   3694:           NO_GEOM){
                   3695:                lock_fdc(drive,0);
                   3696:                cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
                   3697:                if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
                   3698:                        process_fd_request(); /*already done by another thread*/
                   3699:                        return 0;
                   3700:                }
                   3701:                UDRS->maxblock = 0;
                   3702:                UDRS->maxtrack = 0;
                   3703:                if (buffer_drive == drive)
                   3704:                        buffer_track = -1;
                   3705:                clear_bit(drive, &fake_change);
                   3706:                UCLEARF(FD_DISK_CHANGED);
                   3707:                if (cf)
                   3708:                        UDRS->generation++;
                   3709:                if (NO_GEOM){
                   3710:                        /* auto-sensing */
                   3711:                        int size = floppy_blocksizes[MINOR(dev)];
                   3712:                        if (!size)
                   3713:                                size = 1024;
                   3714:                        if (!(bh = getblk(dev,0,size))){
                   3715:                                process_fd_request();
                   3716:                                return 1;
                   3717:                        }
                   3718:                        if (bh && !buffer_uptodate(bh))
                   3719:                                ll_rw_block(READ, 1, &bh);
                   3720:                        process_fd_request();
                   3721:                        wait_on_buffer(bh);
                   3722:                        brelse(bh);
                   3723:                        return 0;
                   3724:                }
                   3725:                if (cf)
                   3726:                        poll_drive(0, FD_RAW_NEED_DISK);
                   3727:                process_fd_request();
                   3728:        }
                   3729:        return 0;
                   3730: }
                   3731: 
                   3732: static struct file_operations floppy_fops = {
                   3733:        NULL,                   /* lseek - default */
                   3734:        floppy_read,            /* read - general block-dev read */
                   3735:        floppy_write,           /* write - general block-dev write */
                   3736:        NULL,                   /* readdir - bad */
                   3737:        NULL,                   /* select */
                   3738:        fd_ioctl,               /* ioctl */
                   3739:        NULL,                   /* mmap */
                   3740:        floppy_open,            /* open */
                   3741:        floppy_release,         /* release */
                   3742:        block_fsync,            /* fsync */
                   3743:        NULL,                   /* fasync */
                   3744:        check_floppy_change,    /* media_change */
                   3745:        floppy_revalidate,      /* revalidate */
                   3746: };
                   3747: 
                   3748: /*
                   3749:  * Floppy Driver initialization
                   3750:  * =============================
                   3751:  */
                   3752: 
                   3753: /* Determine the floppy disk controller type */
                   3754: /* This routine was written by David C. Niemi */
                   3755: static char get_fdc_version(void)
                   3756: {
                   3757:        int r;
                   3758: 
                   3759:        output_byte(FD_DUMPREGS);       /* 82072 and better know DUMPREGS */
                   3760:        if (FDCS->reset)
                   3761:                return FDC_NONE;
                   3762:        if ((r = result()) <= 0x00)
                   3763:                return FDC_NONE;        /* No FDC present ??? */
                   3764:        if ((r==1) && (reply_buffer[0] == 0x80)){
                   3765:                printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
                   3766:                return FDC_8272A;       /* 8272a/765 don't know DUMPREGS */
                   3767:        }
                   3768:        if (r != 10) {
                   3769:                printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
                   3770:                       fdc, r);
                   3771:                return FDC_UNKNOWN;
                   3772:        }
                   3773: 
                   3774:        if(!fdc_configure()) {
                   3775:                printk(KERN_INFO "FDC %d is an 82072\n",fdc);
                   3776:                return FDC_82072;       /* 82072 doesn't know CONFIGURE */
                   3777:        }
                   3778: 
                   3779:        output_byte(FD_PERPENDICULAR);
                   3780:        if(need_more_output() == MORE_OUTPUT) {
                   3781:                output_byte(0);
                   3782:        } else {
                   3783:                printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
                   3784:                return FDC_82072A;      /* 82072A as found on Sparcs. */
                   3785:        }
                   3786: 
                   3787:        output_byte(FD_UNLOCK);
                   3788:        r = result();
                   3789:        if ((r == 1) && (reply_buffer[0] == 0x80)){
                   3790:                printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
                   3791:                return FDC_82077_ORIG;  /* Pre-1991 82077, doesn't know 
                   3792:                                         * LOCK/UNLOCK */
                   3793:        }
                   3794:        if ((r != 1) || (reply_buffer[0] != 0x00)) {
                   3795:                printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
                   3796:                       fdc, r);
                   3797:                return FDC_UNKNOWN;
                   3798:        }
                   3799:        output_byte(FD_PARTID);
                   3800:        r = result();
                   3801:        if (r != 1) {
                   3802:                printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
                   3803:                       fdc, r);
                   3804:                return FDC_UNKNOWN;
                   3805:        }
                   3806:        if (reply_buffer[0] == 0x80) {
                   3807:                printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
                   3808:                return FDC_82077;       /* Revised 82077AA passes all the tests */
                   3809:        }
                   3810:        switch (reply_buffer[0] >> 5) {
                   3811:                case 0x0:
                   3812:                        /* Either a 82078-1 or a 82078SL running at 5Volt */
                   3813:                        printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
                   3814:                        return FDC_82078;
                   3815:                case 0x1:
                   3816:                        printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
                   3817:                        return FDC_82078;
                   3818:                case 0x2:
                   3819:                        printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
                   3820:                        return FDC_S82078B;
                   3821:                case 0x3:
                   3822:                        printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
                   3823:                        return FDC_87306;
                   3824:                default:
                   3825:                        printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
                   3826:                               fdc, reply_buffer[0] >> 5);
                   3827:                        return FDC_82078_UNKN;
                   3828:        }
                   3829: } /* get_fdc_version */
                   3830: 
                   3831: /* lilo configuration */
                   3832: 
                   3833: /* we make the invert_dcl function global. One day, somebody might
                   3834:  * want to centralize all thinkpad related options into one lilo option,
                   3835:  * there are just so many thinkpad related quirks! */
                   3836: void floppy_invert_dcl(int *ints,int param)
                   3837: {
                   3838:        int i;
                   3839: 
                   3840:        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
                   3841:                if (param)
                   3842:                        default_drive_params[i].params.flags |= 0x80;
                   3843:                else
                   3844:                        default_drive_params[i].params.flags &= ~0x80;
                   3845:        }
                   3846:        DPRINT("Configuring drives for inverted dcl\n");
                   3847: }
                   3848: 
                   3849: static void daring(int *ints,int param)
                   3850: {
                   3851:        int i;
                   3852: 
                   3853:        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
                   3854:                if (param){
                   3855:                        default_drive_params[i].params.select_delay = 0;
                   3856:                        default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
                   3857:                } else {
                   3858:                        default_drive_params[i].params.select_delay = 2*HZ/100;
                   3859:                        default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
                   3860:                }
                   3861:        }
                   3862:        DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
                   3863: }
                   3864: 
                   3865: static void set_cmos(int *ints, int dummy)
                   3866: {
                   3867:        int current_drive=0;
                   3868: 
                   3869:        if (ints[0] != 2){
                   3870:                DPRINT("wrong number of parameter for cmos\n");
                   3871:                return;
                   3872:        }
                   3873:        current_drive = ints[1];
                   3874:        if (current_drive < 0 || current_drive >= 8){
                   3875:                DPRINT("bad drive for set_cmos\n");
                   3876:                return;
                   3877:        }
                   3878:        if (current_drive >= 4 && !FDC2)
                   3879:                FDC2 = 0x370;
                   3880:        if (ints[2] <= 0 || 
                   3881:            (ints[2] >= NUMBER(default_drive_params) && ints[2] != 16)){
                   3882:                DPRINT("bad cmos code %d\n", ints[2]);
                   3883:                return;
                   3884:        }
                   3885:        DP->cmos = ints[2];
                   3886:        DPRINT("setting cmos code to %d\n", ints[2]);
                   3887: }
                   3888: 
                   3889: static struct param_table {
                   3890:        const char *name;
                   3891:        void (*fn)(int *ints, int param);
                   3892:        int *var;
                   3893:        int def_param;
                   3894: } config_params[]={
                   3895:        { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff },
                   3896:        { "all_drives", 0, &allowed_drive_mask, 0xff },
                   3897:        { "asus_pci", 0, &allowed_drive_mask, 0x33 },
                   3898: 
                   3899:        { "daring", daring, 0, 1},
                   3900: 
                   3901:        { "two_fdc",  0, &FDC2, 0x370 },
                   3902:        { "one_fdc", 0, &FDC2, 0 },
                   3903: 
                   3904:        { "thinkpad", floppy_invert_dcl, 0, 1 },
                   3905: 
                   3906:        { "nodma", 0, &use_virtual_dma, 1 },
                   3907:        { "omnibook", 0, &use_virtual_dma, 1 },
                   3908:        { "dma", 0, &use_virtual_dma, 0 },
                   3909: 
                   3910:        { "fifo_depth", 0, &fifo_depth, 0xa },
                   3911:        { "nofifo", 0, &no_fifo, 0x20 },
                   3912:        { "usefifo", 0, &no_fifo, 0 },
                   3913: 
                   3914:        { "cmos", set_cmos, 0, 0 },
                   3915: 
                   3916:        { "unexpected_interrupts", 0, &print_unex, 1 },
                   3917:        { "no_unexpected_interrupts", 0, &print_unex, 0 },
                   3918:        { "L40SX", 0, &print_unex, 0 } };
                   3919: 
                   3920: #define FLOPPY_SETUP
                   3921: void floppy_setup(char *str, int *ints)
                   3922: {
                   3923:        int i;
                   3924:        int param;
                   3925:        if (str)
                   3926:                for (i=0; i< ARRAY_SIZE(config_params); i++){
                   3927:                        if (strcmp(str,config_params[i].name) == 0){
                   3928:                                if (ints[0])
                   3929:                                        param = ints[1];
                   3930:                                else
                   3931:                                        param = config_params[i].def_param;
                   3932:                                if(config_params[i].fn)
                   3933:                                        config_params[i].fn(ints,param);
                   3934:                                if(config_params[i].var) {
                   3935:                                        DPRINT("%s=%d\n", str, param);
                   3936:                                        *config_params[i].var = param;
                   3937:                                }
                   3938:                                return;
                   3939:                        }
                   3940:                }
                   3941:        if (str) {
                   3942:                DPRINT("unknown floppy option [%s]\n", str);
                   3943:                
                   3944:                DPRINT("allowed options are:");
                   3945:                for (i=0; i< ARRAY_SIZE(config_params); i++)
                   3946:                        printk(" %s",config_params[i].name);
                   3947:                printk("\n");
                   3948:        } else
                   3949:                DPRINT("botched floppy option\n");
                   3950:        DPRINT("Read linux/drivers/block/README.fd\n");
                   3951: }
                   3952: 
                   3953: int floppy_init(void)
                   3954: {
                   3955:        int i,unit,drive;
                   3956:        int have_no_fdc= -EIO;
                   3957: 
                   3958:        raw_cmd = 0;
                   3959: 
                   3960:        if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
                   3961:                printk("Unable to get major %d for floppy\n",MAJOR_NR);
                   3962:                return -EBUSY;
                   3963:        }
                   3964: 
                   3965:        for (i=0; i<256; i++)
                   3966:                if (ITYPE(i))
                   3967:                        floppy_sizes[i] = floppy_type[ITYPE(i)].size >> 1;
                   3968:                else
                   3969:                        floppy_sizes[i] = MAX_DISK_SIZE;
                   3970: 
                   3971:        blk_size[MAJOR_NR] = floppy_sizes;
                   3972:        blksize_size[MAJOR_NR] = floppy_blocksizes;
                   3973:        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
                   3974:        reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
                   3975:        config_types();
                   3976: 
                   3977:        for (i = 0; i < N_FDC; i++) {
                   3978:                fdc = i;
                   3979:                CLEARSTRUCT(FDCS);
                   3980:                FDCS->dtr = -1;
                   3981:                FDCS->dor = 0x4;
                   3982: #ifdef __sparc__
                   3983:                /*sparcs don't have a DOR reset which we can fall back on to*/
                   3984:                FDCS->version = FDC_82072A;
                   3985: #endif
                   3986:        }
                   3987: 
                   3988:        fdc_state[0].address = FDC1;
                   3989: #if N_FDC > 1
                   3990:        fdc_state[1].address = FDC2;
                   3991: #endif
                   3992: 
                   3993:        if (floppy_grab_irq_and_dma()){
                   3994:                del_timer(&fd_timeout);
                   3995:                blk_dev[MAJOR_NR].request_fn = NULL;
                   3996:                unregister_blkdev(MAJOR_NR,"fd");
                   3997:                return -EBUSY;
                   3998:        }
                   3999: 
                   4000:        /* initialise drive state */
                   4001:        for (drive = 0; drive < N_DRIVE; drive++) {
                   4002:                CLEARSTRUCT(UDRS);
                   4003:                CLEARSTRUCT(UDRWE);
                   4004:                UDRS->flags = FD_VERIFY | FD_DISK_NEWCHANGE | FD_DISK_CHANGED;
                   4005:                UDRS->fd_device = -1;
                   4006:                floppy_track_buffer = NULL;
                   4007:                max_buffer_sectors = 0;
                   4008:        }
                   4009: 
                   4010:        for (i = 0; i < N_FDC; i++) {
                   4011:                fdc = i;
                   4012:                FDCS->driver_version = FD_DRIVER_VERSION;
                   4013:                for (unit=0; unit<4; unit++)
                   4014:                        FDCS->track[unit] = 0;
                   4015:                if (FDCS->address == -1)
                   4016:                        continue;
                   4017:                FDCS->rawcmd = 2;
                   4018:                if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
                   4019:                        FDCS->address = -1;
                   4020:                        FDCS->version = FDC_NONE;
                   4021:                        continue;
                   4022:                }
                   4023:                /* Try to determine the floppy controller type */
                   4024:                FDCS->version = get_fdc_version();
                   4025:                if (FDCS->version == FDC_NONE){
                   4026:                        FDCS->address = -1;
                   4027:                        continue;
                   4028:                }
                   4029: 
                   4030:                request_region(FDCS->address, 6, "floppy");
                   4031:                request_region(FDCS->address+7, 1, "floppy DIR");
                   4032:                /* address + 6 is reserved, and may be taken by IDE.
                   4033:                 * Unfortunately, Adaptec doesn't know this :-(, */
                   4034: 
                   4035:                have_no_fdc = 0;
                   4036:                /* Not all FDCs seem to be able to handle the version command
                   4037:                 * properly, so force a reset for the standard FDC clones,
                   4038:                 * to avoid interrupt garbage.
                   4039:                 */
                   4040:                user_reset_fdc(-1,FD_RESET_ALWAYS,0);
                   4041:        }
                   4042:        fdc=0;
                   4043:        del_timer(&fd_timeout);
                   4044:        current_drive = 0;
                   4045:        floppy_release_irq_and_dma();
                   4046:        initialising=0;
                   4047:        if (have_no_fdc) {
                   4048:                DPRINT("no floppy controllers found\n");
                   4049:                request_tq.routine = (void *)(void *) empty;
                   4050:                /*
                   4051:                 *      When we return we may be unloaded. This little
                   4052:                 *      trick forces the immediate_bh handler to have run
                   4053:                 *      before we unload it, lest we cause bad things.
                   4054:                 */
                   4055:                mark_bh(IMMEDIATE_BH);
                   4056:                schedule();
                   4057:                if (usage_count)
                   4058:                        floppy_release_irq_and_dma();
                   4059:                blk_dev[MAJOR_NR].request_fn = NULL;
                   4060:                unregister_blkdev(MAJOR_NR,"fd");
                   4061:        }
                   4062:        return have_no_fdc;
                   4063: }
                   4064: 
                   4065: static int floppy_grab_irq_and_dma(void)
                   4066: {
                   4067:        int i;
                   4068:        unsigned long flags;
                   4069: 
                   4070:        INT_OFF;
                   4071:        if (usage_count++){
                   4072:                INT_ON;
                   4073:                return 0;
                   4074:        }
                   4075:        INT_ON;
                   4076:        MOD_INC_USE_COUNT;
                   4077:        for (i=0; i< N_FDC; i++){
                   4078:                if (fdc_state[i].address != -1){
                   4079:                        fdc = i;
                   4080:                        reset_fdc_info(1);
                   4081:                        fd_outb(FDCS->dor, FD_DOR);
                   4082:                }
                   4083:        }
                   4084:        fdc = 0;
                   4085:        set_dor(0, ~0, 8);  /* avoid immediate interrupt */
                   4086: 
                   4087:        if (fd_request_irq()) {
                   4088:                DPRINT("Unable to grab IRQ%d for the floppy driver\n",
                   4089:                        FLOPPY_IRQ);
                   4090:                MOD_DEC_USE_COUNT;
                   4091:                usage_count--;
                   4092:                return -1;
                   4093:        }
                   4094:        if (fd_request_dma()) {
                   4095:                DPRINT("Unable to grab DMA%d for the floppy driver\n",
                   4096:                        FLOPPY_DMA);
                   4097:                fd_free_irq();
                   4098:                MOD_DEC_USE_COUNT;
                   4099:                usage_count--;
                   4100:                return -1;
                   4101:        }
                   4102:        for (fdc = 0; fdc < N_FDC; fdc++)
                   4103:                if (FDCS->address != -1)
                   4104:                        fd_outb(FDCS->dor, FD_DOR);
                   4105:        fdc = 0;
                   4106:        fd_enable_irq();
                   4107:        irqdma_allocated=1;
                   4108:        return 0;
                   4109: }
                   4110: 
                   4111: static void floppy_release_irq_and_dma(void)
                   4112: {
                   4113: #ifdef FLOPPY_SANITY_CHECK
                   4114:        int drive;
                   4115: #endif
                   4116:        long tmpsize;
                   4117:        unsigned long tmpaddr;
                   4118:        unsigned long flags;
                   4119: 
                   4120:        INT_OFF;
                   4121:        if (--usage_count){
                   4122:                INT_ON;
                   4123:                return;
                   4124:        }
                   4125:        INT_ON;
                   4126:        if(irqdma_allocated)
                   4127:        {
                   4128:                fd_disable_dma();
                   4129:                fd_free_dma();
                   4130:                fd_disable_irq();
                   4131:                fd_free_irq();
                   4132:                irqdma_allocated=0;
                   4133:        }
                   4134: 
                   4135:        set_dor(0, ~0, 8);
                   4136: #if N_FDC > 1
                   4137:        set_dor(1, ~8, 0);
                   4138: #endif
                   4139:        floppy_enable_hlt();
                   4140: 
                   4141:        if (floppy_track_buffer && max_buffer_sectors) {
                   4142:                tmpsize = max_buffer_sectors*1024;
                   4143:                tmpaddr = (unsigned long)floppy_track_buffer;
                   4144:                floppy_track_buffer = 0;
                   4145:                max_buffer_sectors = 0;
                   4146:                buffer_min = buffer_max = -1;
                   4147:                fd_dma_mem_free(tmpaddr, tmpsize);
                   4148:        }
                   4149: 
                   4150: #ifdef FLOPPY_SANITY_CHECK
                   4151: #ifndef __sparc__
                   4152:        for (drive=0; drive < N_FDC * 4; drive++)
                   4153:                if (motor_off_timer[drive].next)
                   4154:                        printk("motor off timer %d still active\n", drive);
                   4155: #endif
                   4156: 
                   4157:        if (fd_timeout.next)
                   4158:                printk("floppy timer still active:%s\n", timeout_message);
                   4159:        if (fd_timer.next)
                   4160:                printk("auxiliary floppy timer still active\n");
                   4161:        if (floppy_tq.sync)
                   4162:                printk("task queue still active\n");
                   4163: #endif
                   4164:        MOD_DEC_USE_COUNT;
                   4165: }
                   4166: 
                   4167: 
                   4168: #ifdef MODULE
                   4169: 
                   4170: extern char *get_options(char *str, int *ints);
                   4171: 
                   4172: char *floppy=NULL;
                   4173: 
                   4174: static void parse_floppy_cfg_string(char *cfg)
                   4175: {
                   4176:        char *ptr;
                   4177:        int ints[11];
                   4178: 
                   4179:        while(*cfg) {
                   4180:                for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
                   4181:                if(*cfg) {
                   4182:                        *cfg = '\0';
                   4183:                        cfg++;
                   4184:                }
                   4185:                if(*ptr)
                   4186:                        floppy_setup(get_options(ptr,ints),ints);
                   4187:        }
                   4188: }
                   4189: 
                   4190: static void mod_setup(char *pattern, void (*setup)(char *, int *))
                   4191: {
                   4192:        unsigned long i;
                   4193:        char c;
                   4194:        int j;
                   4195:        int match;
                   4196:        char buffer[100];
                   4197:        int ints[11];
                   4198:        int length = strlen(pattern)+1;
                   4199: 
                   4200:        match=0;
                   4201:        j=1;
                   4202: 
                   4203:        for (i=current->mm->env_start; i< current->mm->env_end; i ++){
                   4204:                c= get_fs_byte(i);
                   4205:                if (match){
                   4206:                        if (j==99)
                   4207:                                c='\0';
                   4208:                        buffer[j] = c;
                   4209:                        if (!c || c == ' ' || c == '\t'){
                   4210:                                if (j){
                   4211:                                        buffer[j] = '\0';
                   4212:                                        setup(get_options(buffer,ints),ints);
                   4213:                                }
                   4214:                                j=0;
                   4215:                        } else
                   4216:                                j++;
                   4217:                        if (!c)
                   4218:                                break;
                   4219:                        continue;
                   4220:                }
                   4221:                if ((!j && !c) || (j && c == pattern[j-1]))
                   4222:                        j++;
                   4223:                else
                   4224:                        j=0;
                   4225:                if (j==length){
                   4226:                        match=1;
                   4227:                        j=0;
                   4228:                }
                   4229:        }
                   4230: }
                   4231: 
                   4232: 
                   4233: #ifdef __cplusplus
                   4234: extern "C" {
                   4235: #endif
                   4236: int init_module(void)
                   4237: {
                   4238:        printk(KERN_INFO "inserting floppy driver for %s\n", kernel_version);
                   4239:                
                   4240:        if(floppy)
                   4241:                parse_floppy_cfg_string(floppy);
                   4242:        else
                   4243:                mod_setup("floppy=", floppy_setup);
                   4244:                
                   4245:        return floppy_init();
                   4246: }
                   4247: 
                   4248: void cleanup_module(void)
                   4249: {
                   4250:        int fdc, dummy;
                   4251:                
                   4252:        for (fdc=0; fdc<2; fdc++)
                   4253:                if (FDCS->address != -1){
                   4254:                        release_region(FDCS->address, 6);
                   4255:                        release_region(FDCS->address+7, 1);
                   4256:        }
                   4257:                
                   4258:        unregister_blkdev(MAJOR_NR, "fd");
                   4259: 
                   4260:        blk_dev[MAJOR_NR].request_fn = 0;
                   4261:        /* eject disk, if any */
                   4262:        dummy = fd_eject(0);
                   4263: }
                   4264: 
                   4265: #ifdef __cplusplus
                   4266: }
                   4267: #endif
                   4268: 
                   4269: #else
                   4270: /* eject the boot floppy (if we need the drive for a different root floppy) */
                   4271: /* This should only be called at boot time when we're sure that there's no
                   4272:  * resource contention. */
                   4273: void floppy_eject(void)
                   4274: {
                   4275:        int dummy;
                   4276:        if(floppy_grab_irq_and_dma()==0)
                   4277:        {
                   4278:                lock_fdc(MAXTIMEOUT,0);
                   4279:                dummy=fd_eject(0);
                   4280:                process_fd_request();
                   4281:                floppy_release_irq_and_dma();
                   4282:        }
                   4283: }
                   4284: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.