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

unix.superglobalmegacorp.com

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