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

unix.superglobalmegacorp.com

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