OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [block/] [floppy.c] - Diff between revs 1765 and 1782

Only display areas with differences | Details | Blame | View Log

Rev 1765 Rev 1782
/*
/*
 *  linux/arch/arm/drivers/block/floppy.c
 *  linux/arch/arm/drivers/block/floppy.c
 *  [ was linux/drivers/block/floppy.c ]
 *  [ was linux/drivers/block/floppy.c ]
 *
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Copyright (C) 1993, 1994  Alain Knaff
 *  Copyright (C) 1993, 1994  Alain Knaff
 *  Modifications Copyright (C) 1995 Russell King
 *  Modifications Copyright (C) 1995 Russell King
 */
 */
/*
/*
 * 02.12.91 - Changed to static variables to indicate need for reset
 * 02.12.91 - Changed to static variables to indicate need for reset
 * and recalibrate. This makes some things easier (output_byte reset
 * and recalibrate. This makes some things easier (output_byte reset
 * checking etc), and means less interrupt jumping in case of errors,
 * checking etc), and means less interrupt jumping in case of errors,
 * so the code is hopefully easier to understand.
 * so the code is hopefully easier to understand.
 */
 */
 
 
/*
/*
 * This file is certainly a mess. I've tried my best to get it working,
 * This file is certainly a mess. I've tried my best to get it working,
 * but I don't like programming floppies, and I have only one anyway.
 * but I don't like programming floppies, and I have only one anyway.
 * Urgel. I should check for more errors, and do more graceful error
 * Urgel. I should check for more errors, and do more graceful error
 * recovery. Seems there are problems with several drives. I've tried to
 * recovery. Seems there are problems with several drives. I've tried to
 * correct them. No promises.
 * correct them. No promises.
 */
 */
 
 
/*
/*
 * As with hd.c, all routines within this file can (and will) be called
 * As with hd.c, all routines within this file can (and will) be called
 * by interrupts, so extreme caution is needed. A hardware interrupt
 * by interrupts, so extreme caution is needed. A hardware interrupt
 * handler may not sleep, or a kernel panic will happen. Thus I cannot
 * handler may not sleep, or a kernel panic will happen. Thus I cannot
 * call "floppy-on" directly, but have to set a special timer interrupt
 * call "floppy-on" directly, but have to set a special timer interrupt
 * etc.
 * etc.
 */
 */
 
 
/*
/*
 * 28.02.92 - made track-buffering routines, based on the routines written
 * 28.02.92 - made track-buffering routines, based on the routines written
 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
 */
 */
 
 
/*
/*
 * Automatic floppy-detection and formatting written by Werner Almesberger
 * Automatic floppy-detection and formatting written by Werner Almesberger
 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
 * the floppy-change signal detection.
 * the floppy-change signal detection.
 */
 */
 
 
/*
/*
 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
 * FDC data overrun bug, added some preliminary stuff for vertical
 * FDC data overrun bug, added some preliminary stuff for vertical
 * recording support.
 * recording support.
 *
 *
 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
 *
 *
 * TODO: Errors are still not counted properly.
 * TODO: Errors are still not counted properly.
 */
 */
 
 
/* 1992/9/20
/* 1992/9/20
 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
 * Christoph H. Hochst\"atter.
 * Christoph H. Hochst\"atter.
 * I have fixed the shift values to the ones I always use. Maybe a new
 * I have fixed the shift values to the ones I always use. Maybe a new
 * ioctl() should be created to be able to modify them.
 * ioctl() should be created to be able to modify them.
 * There is a bug in the driver that makes it impossible to format a
 * There is a bug in the driver that makes it impossible to format a
 * floppy as the first thing after bootup.
 * floppy as the first thing after bootup.
 */
 */
 
 
/*
/*
 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
 * this helped the floppy driver as well. Much cleaner, and still seems to
 * this helped the floppy driver as well. Much cleaner, and still seems to
 * work.
 * work.
 */
 */
 
 
/* 1994/6/24 --bbroad-- added the floppy table entries and made
/* 1994/6/24 --bbroad-- added the floppy table entries and made
 * minor modifications to allow 2.88 floppies to be run.
 * minor modifications to allow 2.88 floppies to be run.
 */
 */
 
 
/* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
/* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
 * disk types.
 * disk types.
 */
 */
 
 
/*
/*
 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
 * format bug fixes, but unfortunately some new bugs too...
 * format bug fixes, but unfortunately some new bugs too...
 */
 */
 
 
/* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
/* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
 * errors to allow safe writing by specialized programs.
 * errors to allow safe writing by specialized programs.
 */
 */
 
 
/* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
/* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
 * drives are "upside-down").
 * drives are "upside-down").
 */
 */
 
 
/*
/*
 * 1995/8/26 -- Andreas Busse -- added Mips support.
 * 1995/8/26 -- Andreas Busse -- added Mips support.
 */
 */
 
 
/*
/*
 * 1995/8/16 -- Russell King -- added ARM support.
 * 1995/8/16 -- Russell King -- added ARM support.
 */
 */
 
 
/*
/*
 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
 * features to asm/floppy.h.
 * features to asm/floppy.h.
 */
 */
 
 
 
 
#define FLOPPY_SANITY_CHECK
#define FLOPPY_SANITY_CHECK
#undef  FLOPPY_SILENT_DCL_CLEAR
#undef  FLOPPY_SILENT_DCL_CLEAR
 
 
#define REALLY_SLOW_IO
#define REALLY_SLOW_IO
 
 
#define DEBUGT 2
#define DEBUGT 2
#define DCL_DEBUG /* debug disk change line */
#define DCL_DEBUG /* debug disk change line */
 
 
/* do print messages for unexpected interrupts */
/* do print messages for unexpected interrupts */
static int print_unex=1;
static int print_unex=1;
#include <linux/utsname.h>
#include <linux/utsname.h>
#include <linux/module.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/timer.h>
#include <linux/tqueue.h>
#include <linux/tqueue.h>
#define FDPATCHES
#define FDPATCHES
#include <linux/fdreg.h>
#include <linux/fdreg.h>
#include <linux/fd.h>
#include <linux/fd.h>
 
 
#define OLDFDRAWCMD 0x020d /* send a raw command to the FDC */
#define OLDFDRAWCMD 0x020d /* send a raw command to the FDC */
 
 
struct old_floppy_raw_cmd {
struct old_floppy_raw_cmd {
  void *data;
  void *data;
  long length;
  long length;
 
 
  unsigned char rate;
  unsigned char rate;
  unsigned char flags;
  unsigned char flags;
  unsigned char cmd_count;
  unsigned char cmd_count;
  unsigned char cmd[9];
  unsigned char cmd[9];
  unsigned char reply_count;
  unsigned char reply_count;
  unsigned char reply[7];
  unsigned char reply[7];
  int track;
  int track;
};
};
 
 
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/malloc.h>
#include <linux/malloc.h>
#include <linux/mm.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/delay.h>
#ifndef CONFIG_ARM
#ifndef CONFIG_ARM
#include <linux/mc146818rtc.h> /* CMOS defines */
#include <linux/mc146818rtc.h> /* CMOS defines */
#endif
#endif
#include <linux/ioport.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/interrupt.h>
 
 
#include <asm/dma.h>
#include <asm/dma.h>
#include <asm/irq.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <asm/segment.h>
 
 
/* the following is the mask of allowed drives. By default units 2 and
/* the following is the mask of allowed drives. By default units 2 and
 * 3 of both floppy controllers are disabled, because switching on the
 * 3 of both floppy controllers are disabled, because switching on the
 * motor of these drives causes system hangs on some PCI computers. drive
 * motor of these drives causes system hangs on some PCI computers. drive
 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
 * a drive is allowed. */
 * a drive is allowed. */
static int FLOPPY_IRQ=IRQ_FLOPPYDISK;
static int FLOPPY_IRQ=IRQ_FLOPPYDISK;
static int FLOPPY_DMA=DMA_FLOPPY;
static int FLOPPY_DMA=DMA_FLOPPY;
static int allowed_drive_mask = 0x33;
static int allowed_drive_mask = 0x33;
 
 
static int use_virtual_dma=0; /* virtual DMA for Intel */
static int use_virtual_dma=0; /* virtual DMA for Intel */
static unsigned short virtual_dma_port=0x3f0;
static unsigned short virtual_dma_port=0x3f0;
void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
static int set_dor(int fdc, char mask, char data);
static int set_dor(int fdc, char mask, char data);
static inline int __get_order(unsigned long size);
static inline int __get_order(unsigned long size);
#include <asm/floppy.h>
#include <asm/floppy.h>
 
 
 
 
#define MAJOR_NR FLOPPY_MAJOR
#define MAJOR_NR FLOPPY_MAJOR
 
 
#include <linux/blk.h>
#include <linux/blk.h>
#include <linux/cdrom.h> /* for the compatibility eject ioctl */
#include <linux/cdrom.h> /* for the compatibility eject ioctl */
 
 
 
 
#ifndef FLOPPY_MOTOR_MASK
#ifndef FLOPPY_MOTOR_MASK
#define FLOPPY_MOTOR_MASK 0xf0
#define FLOPPY_MOTOR_MASK 0xf0
#endif
#endif
 
 
#ifndef fd_get_dma_residue
#ifndef fd_get_dma_residue
#define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
#define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
#endif
#endif
 
 
/* Dma Memory related stuff */
/* Dma Memory related stuff */
 
 
/* Pure 2^n version of get_order */
/* Pure 2^n version of get_order */
static inline int __get_order(unsigned long size)
static inline int __get_order(unsigned long size)
{
{
        int order;
        int order;
 
 
        size = (size-1) >> (PAGE_SHIFT-1);
        size = (size-1) >> (PAGE_SHIFT-1);
        order = -1;
        order = -1;
        do {
        do {
                size >>= 1;
                size >>= 1;
                order++;
                order++;
        } while (size);
        } while (size);
        return order;
        return order;
}
}
 
 
#ifndef fd_dma_mem_free
#ifndef fd_dma_mem_free
#define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
#define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
#endif
#endif
 
 
#ifndef fd_dma_mem_alloc
#ifndef fd_dma_mem_alloc
#define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
#define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
#endif
#endif
 
 
/* End dma memory related stuff */
/* End dma memory related stuff */
 
 
static unsigned int fake_change = 0;
static unsigned int fake_change = 0;
static int initialising=1;
static int initialising=1;
 
 
static inline int TYPE(kdev_t x) {
static inline int TYPE(kdev_t x) {
        return  (MINOR(x)>>2) & 0x1f;
        return  (MINOR(x)>>2) & 0x1f;
}
}
static inline int DRIVE(kdev_t x) {
static inline int DRIVE(kdev_t x) {
        return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
        return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
}
}
#define ITYPE(x) (((x)>>2) & 0x1f)
#define ITYPE(x) (((x)>>2) & 0x1f)
#define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
#define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
#define UNIT(x) ((x) & 0x03)            /* drive on fdc */
#define UNIT(x) ((x) & 0x03)            /* drive on fdc */
#define FDC(x) (((x) & 0x04) >> 2)  /* fdc of drive */
#define FDC(x) (((x) & 0x04) >> 2)  /* fdc of drive */
#define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
#define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
                                /* reverse mapping from unit and fdc to drive */
                                /* reverse mapping from unit and fdc to drive */
#define DP (&drive_params[current_drive])
#define DP (&drive_params[current_drive])
#define DRS (&drive_state[current_drive])
#define DRS (&drive_state[current_drive])
#define DRWE (&write_errors[current_drive])
#define DRWE (&write_errors[current_drive])
#define FDCS (&fdc_state[fdc])
#define FDCS (&fdc_state[fdc])
#define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
#define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
#define SETF(x) (set_bit(x##_BIT, &DRS->flags))
#define SETF(x) (set_bit(x##_BIT, &DRS->flags))
#define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
#define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
 
 
#define UDP (&drive_params[drive])
#define UDP (&drive_params[drive])
#define UDRS (&drive_state[drive])
#define UDRS (&drive_state[drive])
#define UDRWE (&write_errors[drive])
#define UDRWE (&write_errors[drive])
#define UFDCS (&fdc_state[FDC(drive)])
#define UFDCS (&fdc_state[FDC(drive)])
#define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
#define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
#define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
#define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
#define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
#define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
 
 
#define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
#define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
 
 
#define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
#define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
#define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
#define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
 
 
#define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
#define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
 
 
#define INT_OFF save_flags(flags); cli()
#define INT_OFF save_flags(flags); cli()
#define INT_ON  restore_flags(flags)
#define INT_ON  restore_flags(flags)
 
 
/* read/write */
/* read/write */
#define COMMAND raw_cmd->cmd[0]
#define COMMAND raw_cmd->cmd[0]
#define DR_SELECT raw_cmd->cmd[1]
#define DR_SELECT raw_cmd->cmd[1]
#define TRACK raw_cmd->cmd[2]
#define TRACK raw_cmd->cmd[2]
#define HEAD raw_cmd->cmd[3]
#define HEAD raw_cmd->cmd[3]
#define SECTOR raw_cmd->cmd[4]
#define SECTOR raw_cmd->cmd[4]
#define SIZECODE raw_cmd->cmd[5]
#define SIZECODE raw_cmd->cmd[5]
#define SECT_PER_TRACK raw_cmd->cmd[6]
#define SECT_PER_TRACK raw_cmd->cmd[6]
#define GAP raw_cmd->cmd[7]
#define GAP raw_cmd->cmd[7]
#define SIZECODE2 raw_cmd->cmd[8]
#define SIZECODE2 raw_cmd->cmd[8]
#define NR_RW 9
#define NR_RW 9
 
 
/* format */
/* format */
#define F_SIZECODE raw_cmd->cmd[2]
#define F_SIZECODE raw_cmd->cmd[2]
#define F_SECT_PER_TRACK raw_cmd->cmd[3]
#define F_SECT_PER_TRACK raw_cmd->cmd[3]
#define F_GAP raw_cmd->cmd[4]
#define F_GAP raw_cmd->cmd[4]
#define F_FILL raw_cmd->cmd[5]
#define F_FILL raw_cmd->cmd[5]
#define NR_F 6
#define NR_F 6
 
 
/*
/*
 * Maximum disk size (in kilobytes). This default is used whenever the
 * Maximum disk size (in kilobytes). This default is used whenever the
 * current disk size is unknown.
 * current disk size is unknown.
 * [Now it is rather a minimum]
 * [Now it is rather a minimum]
 */
 */
#define MAX_DISK_SIZE 4 /* 3984*/
#define MAX_DISK_SIZE 4 /* 3984*/
 
 
#define K_64    0x10000         /* 64KB */
#define K_64    0x10000         /* 64KB */
 
 
/*
/*
 * globals used by 'result()'
 * globals used by 'result()'
 */
 */
#define MAX_REPLIES 16
#define MAX_REPLIES 16
static unsigned char reply_buffer[MAX_REPLIES];
static unsigned char reply_buffer[MAX_REPLIES];
static int inr; /* size of reply buffer, when called from interrupt */
static int inr; /* size of reply buffer, when called from interrupt */
#define ST0 (reply_buffer[0])
#define ST0 (reply_buffer[0])
#define ST1 (reply_buffer[1])
#define ST1 (reply_buffer[1])
#define ST2 (reply_buffer[2])
#define ST2 (reply_buffer[2])
#define ST3 (reply_buffer[0]) /* result of GETSTATUS */
#define ST3 (reply_buffer[0]) /* result of GETSTATUS */
#define R_TRACK (reply_buffer[3])
#define R_TRACK (reply_buffer[3])
#define R_HEAD (reply_buffer[4])
#define R_HEAD (reply_buffer[4])
#define R_SECTOR (reply_buffer[5])
#define R_SECTOR (reply_buffer[5])
#define R_SIZECODE (reply_buffer[6])
#define R_SIZECODE (reply_buffer[6])
 
 
#define SEL_DLY (2*HZ/100)
#define SEL_DLY (2*HZ/100)
 
 
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/*
/*
 * this struct defines the different floppy drive types.
 * this struct defines the different floppy drive types.
 */
 */
static struct {
static struct {
        struct floppy_drive_params params;
        struct floppy_drive_params params;
        const char *name; /* name printed while booting */
        const char *name; /* name printed while booting */
} default_drive_params[]= {
} default_drive_params[]= {
/* NOTE: the time values in jiffies should be in msec!
/* NOTE: the time values in jiffies should be in msec!
 CMOS drive type
 CMOS drive type
  |     Maximum data rate supported by drive type
  |     Maximum data rate supported by drive type
  |     |   Head load time, msec
  |     |   Head load time, msec
  |     |   |   Head unload time, msec (not used)
  |     |   |   Head unload time, msec (not used)
  |     |   |   |     Step rate interval, usec
  |     |   |   |     Step rate interval, usec
  |     |   |   |     |       Time needed for spinup time (jiffies)
  |     |   |   |     |       Time needed for spinup time (jiffies)
  |     |   |   |     |       |      Timeout for spinning down (jiffies)
  |     |   |   |     |       |      Timeout for spinning down (jiffies)
  |     |   |   |     |       |      |   Spindown offset (where disk stops)
  |     |   |   |     |       |      |   Spindown offset (where disk stops)
  |     |   |   |     |       |      |   |     Select delay
  |     |   |   |     |       |      |   |     Select delay
  |     |   |   |     |       |      |   |     |     RPS
  |     |   |   |     |       |      |   |     |     RPS
  |     |   |   |     |       |      |   |     |     |    Max number of tracks
  |     |   |   |     |       |      |   |     |     |    Max number of tracks
  |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
  |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
  |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
  |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
  |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
  |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
{{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
{{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
      0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
      0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
 
 
{{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
{{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
      0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
      0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
 
 
{{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
{{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
      0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
      0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
 
 
{{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
{{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
      0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
      0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
 
 
{{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
{{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
      0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
      0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
 
 
{{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
{{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
      0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
      0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
 
 
{{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
{{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
      0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
      0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
/*    |  --autodetected formats---    |      |      |
/*    |  --autodetected formats---    |      |      |
 *    read_track                      |      |    Name printed when booting
 *    read_track                      |      |    Name printed when booting
 *                                    |     Native format
 *                                    |     Native format
 *                  Frequency of disk change checks */
 *                  Frequency of disk change checks */
};
};
 
 
static struct floppy_drive_params drive_params[N_DRIVE];
static struct floppy_drive_params drive_params[N_DRIVE];
static struct floppy_drive_struct drive_state[N_DRIVE];
static struct floppy_drive_struct drive_state[N_DRIVE];
static struct floppy_write_errors write_errors[N_DRIVE];
static struct floppy_write_errors write_errors[N_DRIVE];
static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
 
 
/*
/*
 * This struct defines the different floppy types.
 * This struct defines the different floppy types.
 *
 *
 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
 * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
 * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
 * side 0 is on physical side 0 (but with the misnamed sector IDs).
 * side 0 is on physical side 0 (but with the misnamed sector IDs).
 * 'stretch' should probably be renamed to something more general, like
 * 'stretch' should probably be renamed to something more general, like
 * 'options'.  Other parameters should be self-explanatory (see also
 * 'options'.  Other parameters should be self-explanatory (see also
 * setfdprm(8)).
 * setfdprm(8)).
 */
 */
/*
/*
            Size
            Size
             |  Sectors per track
             |  Sectors per track
             |  | Head
             |  | Head
             |  | |  Tracks
             |  | |  Tracks
             |  | |  | Stretch
             |  | |  | Stretch
             |  | |  | |  Gap 1 size
             |  | |  | |  Gap 1 size
             |  | |  | |    |  Data rate, | 0x40 for perp
             |  | |  | |    |  Data rate, | 0x40 for perp
             |  | |  | |    |    |  Spec1 (stepping rate, head unload
             |  | |  | |    |    |  Spec1 (stepping rate, head unload
             |  | |  | |    |    |    |    /fmt gap (gap2) */
             |  | |  | |    |    |    |    /fmt gap (gap2) */
static struct floppy_struct floppy_type[32] = {
static struct floppy_struct floppy_type[32] = {
        {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    },      /*  0 no testing    */
        {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    },      /*  0 no testing    */
        {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
        {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
        { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" },  /*  2 1.2MB AT      */
        { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" },  /*  2 1.2MB AT      */
        {  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  },  /*  3 360KB SS 3.5" */
        {  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  },  /*  3 360KB SS 3.5" */
        { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  },  /*  4 720KB 3.5"    */
        { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  },  /*  4 720KB 3.5"    */
        {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  }, /*  5 360KB AT      */
        {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  }, /*  5 360KB AT      */
        { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  },  /*  6 720KB AT      */
        { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  },  /*  6 720KB AT      */
        { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" },  /*  7 1.44MB 3.5"   */
        { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" },  /*  7 1.44MB 3.5"   */
        { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" },  /*  8 2.88MB 3.5"   */
        { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" },  /*  8 2.88MB 3.5"   */
        { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" },  /*  9 3.12MB 3.5"   */
        { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" },  /*  9 3.12MB 3.5"   */
 
 
        { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
        { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
        { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
        { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
        {  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  }, /* 12 410KB 5.25"   */
        {  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  }, /* 12 410KB 5.25"   */
        { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  },  /* 13 820KB 3.5"    */
        { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  },  /* 13 820KB 3.5"    */
        { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" },  /* 14 1.48MB 5.25"  */
        { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" },  /* 14 1.48MB 5.25"  */
        { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" },  /* 15 1.72MB 3.5"   */
        { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" },  /* 15 1.72MB 3.5"   */
        {  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  }, /* 16 420KB 5.25"   */
        {  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  }, /* 16 420KB 5.25"   */
        { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  },  /* 17 830KB 3.5"    */
        { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  },  /* 17 830KB 3.5"    */
        { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" },  /* 18 1.49MB 5.25"  */
        { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" },  /* 18 1.49MB 5.25"  */
        { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
        { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
 
 
        { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
        { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
        { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
        { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
        { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
        { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
        { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
        { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
        { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
        { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
        { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
        { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
        { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
        { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
        { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
        { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
        { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
        { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
 
 
        { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
        { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
        { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  },  /* 30 800KB 3.5"    */
        { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  },  /* 30 800KB 3.5"    */
        { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
        { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
};
};
 
 
#define NUMBER(x)       (sizeof(x) / sizeof(*(x)))
#define NUMBER(x)       (sizeof(x) / sizeof(*(x)))
#define SECTSIZE (_FD_SECTSIZE(*floppy))
#define SECTSIZE (_FD_SECTSIZE(*floppy))
 
 
/* Auto-detection: Disk type used until the next media change occurs. */
/* Auto-detection: Disk type used until the next media change occurs. */
static struct floppy_struct *current_type[N_DRIVE] = {
static struct floppy_struct *current_type[N_DRIVE] = {
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL
        NULL, NULL, NULL, NULL
};
};
 
 
/*
/*
 * User-provided type information. current_type points to
 * User-provided type information. current_type points to
 * the respective entry of this array.
 * the respective entry of this array.
 */
 */
static struct floppy_struct user_params[N_DRIVE];
static struct floppy_struct user_params[N_DRIVE];
 
 
static int floppy_sizes[256];
static int floppy_sizes[256];
static int floppy_blocksizes[256] = { 0, };
static int floppy_blocksizes[256] = { 0, };
 
 
/*
/*
 * The driver is trying to determine the correct media format
 * The driver is trying to determine the correct media format
 * while probing is set. rw_interrupt() clears it after a
 * while probing is set. rw_interrupt() clears it after a
 * successful access.
 * successful access.
 */
 */
static int probing = 0;
static int probing = 0;
 
 
/* Synchronization of FDC access. */
/* Synchronization of FDC access. */
#define FD_COMMAND_NONE -1
#define FD_COMMAND_NONE -1
#define FD_COMMAND_ERROR 2
#define FD_COMMAND_ERROR 2
#define FD_COMMAND_OKAY 3
#define FD_COMMAND_OKAY 3
 
 
static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
static struct wait_queue *fdc_wait = NULL, *command_done = NULL;
static struct wait_queue *fdc_wait = NULL, *command_done = NULL;
#define NO_SIGNAL (!(current->signal & ~current->blocked) || !interruptible)
#define NO_SIGNAL (!(current->signal & ~current->blocked) || !interruptible)
#define CALL(x) if ((x) == -EINTR) return -EINTR
#define CALL(x) if ((x) == -EINTR) return -EINTR
#define ECALL(x) if ((ret = (x))) return ret;
#define ECALL(x) if ((ret = (x))) return ret;
#define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
#define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
#define WAIT(x) _WAIT((x),interruptible)
#define WAIT(x) _WAIT((x),interruptible)
#define IWAIT(x) _WAIT((x),1)
#define IWAIT(x) _WAIT((x),1)
 
 
/* Errors during formatting are counted here. */
/* Errors during formatting are counted here. */
static int format_errors;
static int format_errors;
 
 
/* Format request descriptor. */
/* Format request descriptor. */
static struct format_descr format_req;
static struct format_descr format_req;
 
 
/*
/*
 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
 * H is head unload time (1=16ms, 2=32ms, etc)
 * H is head unload time (1=16ms, 2=32ms, etc)
 */
 */
 
 
/*
/*
 * Track buffer
 * Track buffer
 * Because these are written to by the DMA controller, they must
 * Because these are written to by the DMA controller, they must
 * not contain a 64k byte boundary crossing, or data will be
 * not contain a 64k byte boundary crossing, or data will be
 * corrupted/lost.
 * corrupted/lost.
 */
 */
static char *floppy_track_buffer=NULL;
static char *floppy_track_buffer=NULL;
static int max_buffer_sectors=0;
static int max_buffer_sectors=0;
 
 
static int *errors;
static int *errors;
typedef void (*done_f)(int);
typedef void (*done_f)(int);
static struct cont_t {
static struct cont_t {
        void (*interrupt)(void); /* this is called after the interrupt of the
        void (*interrupt)(void); /* this is called after the interrupt of the
                                  * main command */
                                  * main command */
        void (*redo)(void); /* this is called to retry the operation */
        void (*redo)(void); /* this is called to retry the operation */
        void (*error)(void); /* this is called to tally an error */
        void (*error)(void); /* this is called to tally an error */
        done_f done; /* this is called to say if the operation has
        done_f done; /* this is called to say if the operation has
                      * succeeded/failed */
                      * succeeded/failed */
} *cont=NULL;
} *cont=NULL;
 
 
static void floppy_ready(void);
static void floppy_ready(void);
static void floppy_start(void);
static void floppy_start(void);
static void process_fd_request(void);
static void process_fd_request(void);
static void recalibrate_floppy(void);
static void recalibrate_floppy(void);
static void floppy_shutdown(void);
static void floppy_shutdown(void);
 
 
static int floppy_grab_irq_and_dma(void);
static int floppy_grab_irq_and_dma(void);
static void floppy_release_irq_and_dma(void);
static void floppy_release_irq_and_dma(void);
 
 
/*
/*
 * The "reset" variable should be tested whenever an interrupt is scheduled,
 * The "reset" variable should be tested whenever an interrupt is scheduled,
 * after the commands have been sent. This is to ensure that the driver doesn't
 * after the commands have been sent. This is to ensure that the driver doesn't
 * get wedged when the interrupt doesn't come because of a failed command.
 * get wedged when the interrupt doesn't come because of a failed command.
 * reset doesn't need to be tested before sending commands, because
 * reset doesn't need to be tested before sending commands, because
 * output_byte is automatically disabled when reset is set.
 * output_byte is automatically disabled when reset is set.
 */
 */
#define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
#define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
static void reset_fdc(void);
static void reset_fdc(void);
 
 
/*
/*
 * These are global variables, as that's the easiest way to give
 * These are global variables, as that's the easiest way to give
 * information to interrupts. They are the data used for the current
 * information to interrupts. They are the data used for the current
 * request.
 * request.
 */
 */
#define NO_TRACK -1
#define NO_TRACK -1
#define NEED_1_RECAL -2
#define NEED_1_RECAL -2
#define NEED_2_RECAL -3
#define NEED_2_RECAL -3
 
 
/* */
/* */
static int usage_count = 0;
static int usage_count = 0;
 
 
 
 
/* buffer related variables */
/* buffer related variables */
static int buffer_track = -1;
static int buffer_track = -1;
static int buffer_drive = -1;
static int buffer_drive = -1;
static int buffer_min = -1;
static int buffer_min = -1;
static int buffer_max = -1;
static int buffer_max = -1;
 
 
/* fdc related variables, should end up in a struct */
/* fdc related variables, should end up in a struct */
static struct floppy_fdc_state fdc_state[N_FDC];
static struct floppy_fdc_state fdc_state[N_FDC];
static int fdc; /* current fdc */
static int fdc; /* current fdc */
 
 
static struct floppy_struct *_floppy = floppy_type;
static struct floppy_struct *_floppy = floppy_type;
static unsigned char current_drive = 0;
static unsigned char current_drive = 0;
static long current_count_sectors = 0;
static long current_count_sectors = 0;
static unsigned char sector_t; /* sector in track */
static unsigned char sector_t; /* sector in track */
 
 
#ifndef fd_eject
#ifndef fd_eject
#define fd_eject(x) -EINVAL
#define fd_eject(x) -EINVAL
#endif
#endif
 
 
#ifdef DEBUGT
#ifdef DEBUGT
static long unsigned debugtimer;
static long unsigned debugtimer;
#endif
#endif
 
 
/*
/*
 * Debugging
 * Debugging
 * =========
 * =========
 */
 */
static inline void set_debugt(void)
static inline void set_debugt(void)
{
{
#ifdef DEBUGT
#ifdef DEBUGT
        debugtimer = jiffies;
        debugtimer = jiffies;
#endif
#endif
}
}
 
 
static inline void debugt(const char *message)
static inline void debugt(const char *message)
{
{
#ifdef DEBUGT
#ifdef DEBUGT
        if (DP->flags & DEBUGT)
        if (DP->flags & DEBUGT)
                printk("%s dtime=%lu\n", message, jiffies-debugtimer);
                printk("%s dtime=%lu\n", message, jiffies-debugtimer);
#endif
#endif
}
}
 
 
typedef void (*timeout_fn)(unsigned long);
typedef void (*timeout_fn)(unsigned long);
static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
                                       (timeout_fn) floppy_shutdown };
                                       (timeout_fn) floppy_shutdown };
 
 
static const char *timeout_message;
static const char *timeout_message;
 
 
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
static void is_alive(const char *message)
static void is_alive(const char *message)
{
{
        /* this routine checks whether the floppy driver is "alive" */
        /* this routine checks whether the floppy driver is "alive" */
        if (fdc_busy && command_status < 2 && !fd_timeout.prev){
        if (fdc_busy && command_status < 2 && !fd_timeout.prev){
                DPRINT("timeout handler died: %s\n",message);
                DPRINT("timeout handler died: %s\n",message);
        }
        }
}
}
#endif
#endif
 
 
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
 
 
#define OLOGSIZE 20
#define OLOGSIZE 20
 
 
static void (*lasthandler)(void) = NULL;
static void (*lasthandler)(void) = NULL;
static int interruptjiffies=0;
static int interruptjiffies=0;
static int resultjiffies=0;
static int resultjiffies=0;
static int resultsize=0;
static int resultsize=0;
static int lastredo=0;
static int lastredo=0;
 
 
static struct output_log {
static struct output_log {
        unsigned char data;
        unsigned char data;
        unsigned char status;
        unsigned char status;
        unsigned long jiffies;
        unsigned long jiffies;
} output_log[OLOGSIZE];
} output_log[OLOGSIZE];
 
 
static int output_log_pos=0;
static int output_log_pos=0;
#endif
#endif
 
 
#define CURRENTD -1
#define CURRENTD -1
#define MAXTIMEOUT -2
#define MAXTIMEOUT -2
 
 
static void reschedule_timeout(int drive, const char *message, int marg)
static void reschedule_timeout(int drive, const char *message, int marg)
{
{
        if (drive == CURRENTD)
        if (drive == CURRENTD)
                drive = current_drive;
                drive = current_drive;
        del_timer(&fd_timeout);
        del_timer(&fd_timeout);
        if (drive < 0 || drive > N_DRIVE) {
        if (drive < 0 || drive > N_DRIVE) {
                fd_timeout.expires = jiffies + 20*HZ;
                fd_timeout.expires = jiffies + 20*HZ;
                drive=0;
                drive=0;
        } else
        } else
                fd_timeout.expires = jiffies + UDP->timeout;
                fd_timeout.expires = jiffies + UDP->timeout;
        add_timer(&fd_timeout);
        add_timer(&fd_timeout);
        if (UDP->flags & FD_DEBUG){
        if (UDP->flags & FD_DEBUG){
                DPRINT("reschedule timeout ");
                DPRINT("reschedule timeout ");
                printk(message, marg);
                printk(message, marg);
                printk("\n");
                printk("\n");
        }
        }
        timeout_message = message;
        timeout_message = message;
}
}
 
 
static int maximum(int a, int b)
static int maximum(int a, int b)
{
{
        if(a > b)
        if(a > b)
                return a;
                return a;
        else
        else
                return b;
                return b;
}
}
#define INFBOUND(a,b) (a)=maximum((a),(b));
#define INFBOUND(a,b) (a)=maximum((a),(b));
 
 
static int minimum(int a, int b)
static int minimum(int a, int b)
{
{
        if(a < b)
        if(a < b)
                return a;
                return a;
        else
        else
                return b;
                return b;
}
}
#define SUPBOUND(a,b) (a)=minimum((a),(b));
#define SUPBOUND(a,b) (a)=minimum((a),(b));
 
 
 
 
/*
/*
 * Bottom half floppy driver.
 * Bottom half floppy driver.
 * ==========================
 * ==========================
 *
 *
 * This part of the file contains the code talking directly to the hardware,
 * This part of the file contains the code talking directly to the hardware,
 * and also the main service loop (seek-configure-spinup-command)
 * and also the main service loop (seek-configure-spinup-command)
 */
 */
 
 
/*
/*
 * disk change.
 * disk change.
 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
 * and the last_checked date.
 * and the last_checked date.
 *
 *
 * last_checked is the date of the last check which showed 'no disk change'
 * last_checked is the date of the last check which showed 'no disk change'
 * FD_DISK_CHANGE is set under two conditions:
 * FD_DISK_CHANGE is set under two conditions:
 * 1. The floppy has been changed after some i/o to that floppy already
 * 1. The floppy has been changed after some i/o to that floppy already
 *    took place.
 *    took place.
 * 2. No floppy disk is in the drive. This is done in order to ensure that
 * 2. No floppy disk is in the drive. This is done in order to ensure that
 *    requests are quickly flushed in case there is no disk in the drive. It
 *    requests are quickly flushed in case there is no disk in the drive. It
 *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
 *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
 *    the drive.
 *    the drive.
 *
 *
 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
 *  each seek. If a disk is present, the disk change line should also be
 *  each seek. If a disk is present, the disk change line should also be
 *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
 *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
 *  change line is set, this means either that no disk is in the drive, or
 *  change line is set, this means either that no disk is in the drive, or
 *  that it has been removed since the last seek.
 *  that it has been removed since the last seek.
 *
 *
 * This means that we really have a third possibility too:
 * This means that we really have a third possibility too:
 *  The floppy has been changed after the last seek.
 *  The floppy has been changed after the last seek.
 */
 */
 
 
static int disk_change(int drive)
static int disk_change(int drive)
{
{
        int fdc=FDC(drive);
        int fdc=FDC(drive);
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        if (jiffies - UDRS->select_date < UDP->select_delay)
        if (jiffies - UDRS->select_date < UDP->select_delay)
                DPRINT("WARNING disk change called early\n");
                DPRINT("WARNING disk change called early\n");
        if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
        if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
           (FDCS->dor & 3) != UNIT(drive) ||
           (FDCS->dor & 3) != UNIT(drive) ||
           fdc != FDC(drive)){
           fdc != FDC(drive)){
                DPRINT("probing disk change on unselected drive\n");
                DPRINT("probing disk change on unselected drive\n");
                DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
                DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
                        (unsigned int)FDCS->dor);
                        (unsigned int)FDCS->dor);
        }
        }
#endif
#endif
 
 
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (UDP->flags & FD_DEBUG){
        if (UDP->flags & FD_DEBUG){
                DPRINT("checking disk change line for drive %d\n",drive);
                DPRINT("checking disk change line for drive %d\n",drive);
                DPRINT("jiffies=%ld\n", jiffies);
                DPRINT("jiffies=%ld\n", jiffies);
                DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
                DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
                DPRINT("flags=%x\n",UDRS->flags);
                DPRINT("flags=%x\n",UDRS->flags);
        }
        }
#endif
#endif
        if (UDP->flags & FD_BROKEN_DCL)
        if (UDP->flags & FD_BROKEN_DCL)
                return UTESTF(FD_DISK_CHANGED);
                return UTESTF(FD_DISK_CHANGED);
        if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
        if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
                USETF(FD_VERIFY); /* verify write protection */
                USETF(FD_VERIFY); /* verify write protection */
                if (UDRS->maxblock){
                if (UDRS->maxblock){
                        /* mark it changed */
                        /* mark it changed */
                        USETF(FD_DISK_CHANGED);
                        USETF(FD_DISK_CHANGED);
                }
                }
 
 
                /* invalidate its geometry */
                /* invalidate its geometry */
                if (UDRS->keep_data >= 0) {
                if (UDRS->keep_data >= 0) {
                        if ((UDP->flags & FTD_MSG) &&
                        if ((UDP->flags & FTD_MSG) &&
                            current_type[drive] != NULL)
                            current_type[drive] != NULL)
                                DPRINT("Disk type is undefined after "
                                DPRINT("Disk type is undefined after "
                                       "disk change\n");
                                       "disk change\n");
                        current_type[drive] = NULL;
                        current_type[drive] = NULL;
                        floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
                        floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
                }
                }
 
 
                /*USETF(FD_DISK_NEWCHANGE);*/
                /*USETF(FD_DISK_NEWCHANGE);*/
                return 1;
                return 1;
        } else {
        } else {
                UDRS->last_checked=jiffies;
                UDRS->last_checked=jiffies;
                UCLEARF(FD_DISK_NEWCHANGE);
                UCLEARF(FD_DISK_NEWCHANGE);
        }
        }
        return 0;
        return 0;
}
}
 
 
static inline int is_selected(int dor, int unit)
static inline int is_selected(int dor, int unit)
{
{
        return ((dor  & (0x10 << unit)) && (dor &3) == unit);
        return ((dor  & (0x10 << unit)) && (dor &3) == unit);
}
}
 
 
static int set_dor(int fdc, char mask, char data)
static int set_dor(int fdc, char mask, char data)
{
{
        register unsigned char drive, unit, newdor,olddor;
        register unsigned char drive, unit, newdor,olddor;
 
 
        if (FDCS->address == -1)
        if (FDCS->address == -1)
                return -1;
                return -1;
 
 
        olddor = FDCS->dor;
        olddor = FDCS->dor;
        newdor =  (olddor & mask) | data;
        newdor =  (olddor & mask) | data;
        if (newdor != olddor){
        if (newdor != olddor){
                unit = olddor & 0x3;
                unit = olddor & 0x3;
                if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
                if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
                        drive = REVDRIVE(fdc,unit);
                        drive = REVDRIVE(fdc,unit);
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
                        if (UDP->flags & FD_DEBUG){
                        if (UDP->flags & FD_DEBUG){
                                DPRINT("calling disk change from set_dor\n");
                                DPRINT("calling disk change from set_dor\n");
                        }
                        }
#endif
#endif
                        disk_change(drive);
                        disk_change(drive);
                }
                }
                FDCS->dor = newdor;
                FDCS->dor = newdor;
                fd_setdor(newdor);
                fd_setdor(newdor);
 
 
                unit = newdor & 0x3;
                unit = newdor & 0x3;
                if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
                if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
                        drive = REVDRIVE(fdc,unit);
                        drive = REVDRIVE(fdc,unit);
                        UDRS->select_date = jiffies;
                        UDRS->select_date = jiffies;
                }
                }
        }
        }
        if (newdor & FLOPPY_MOTOR_MASK)
        if (newdor & FLOPPY_MOTOR_MASK)
                floppy_grab_irq_and_dma();
                floppy_grab_irq_and_dma();
        if (olddor & FLOPPY_MOTOR_MASK)
        if (olddor & FLOPPY_MOTOR_MASK)
                floppy_release_irq_and_dma();
                floppy_release_irq_and_dma();
        return olddor;
        return olddor;
}
}
 
 
static void twaddle(void)
static void twaddle(void)
{
{
        if (DP->select_delay)
        if (DP->select_delay)
                return;
                return;
        fd_setdor(FDCS->dor & ~(0x10<<UNIT(current_drive)));
        fd_setdor(FDCS->dor & ~(0x10<<UNIT(current_drive)));
        fd_setdor(FDCS->dor);
        fd_setdor(FDCS->dor);
        DRS->select_date = jiffies;
        DRS->select_date = jiffies;
}
}
 
 
/* reset all driver information about the current fdc. This is needed after
/* reset all driver information about the current fdc. This is needed after
 * a reset, and after a raw command. */
 * a reset, and after a raw command. */
static void reset_fdc_info(int mode)
static void reset_fdc_info(int mode)
{
{
        int drive;
        int drive;
 
 
        FDCS->spec1 = FDCS->spec2 = -1;
        FDCS->spec1 = FDCS->spec2 = -1;
        FDCS->need_configure = 1;
        FDCS->need_configure = 1;
        FDCS->perp_mode = 1;
        FDCS->perp_mode = 1;
        FDCS->rawcmd = 0;
        FDCS->rawcmd = 0;
        for (drive = 0; drive < N_DRIVE; drive++)
        for (drive = 0; drive < N_DRIVE; drive++)
                if (FDC(drive) == fdc &&
                if (FDC(drive) == fdc &&
                    (mode || UDRS->track != NEED_1_RECAL))
                    (mode || UDRS->track != NEED_1_RECAL))
                        UDRS->track = NEED_2_RECAL;
                        UDRS->track = NEED_2_RECAL;
}
}
 
 
/* selects the fdc and drive, and enables the fdc's input/dma. */
/* selects the fdc and drive, and enables the fdc's input/dma. */
static void set_fdc(int drive)
static void set_fdc(int drive)
{
{
        if (drive >= 0 && drive < N_DRIVE){
        if (drive >= 0 && drive < N_DRIVE){
                fdc = FDC(drive);
                fdc = FDC(drive);
                current_drive = drive;
                current_drive = drive;
        }
        }
        if (fdc != 1 && fdc != 0) {
        if (fdc != 1 && fdc != 0) {
                printk("bad fdc value\n");
                printk("bad fdc value\n");
                return;
                return;
        }
        }
        set_dor(fdc,~0,8);
        set_dor(fdc,~0,8);
#if N_FDC > 1
#if N_FDC > 1
        set_dor(1-fdc, ~8, 0);
        set_dor(1-fdc, ~8, 0);
#endif
#endif
        if (FDCS->rawcmd == 2)
        if (FDCS->rawcmd == 2)
                reset_fdc_info(1);
                reset_fdc_info(1);
        if (fd_inb(FD_STATUS) != STATUS_READY)
        if (fd_inb(FD_STATUS) != STATUS_READY)
                FDCS->reset = 1;
                FDCS->reset = 1;
}
}
 
 
/* locks the driver */
/* locks the driver */
static int lock_fdc(int drive, int interruptible)
static int lock_fdc(int drive, int interruptible)
{
{
        unsigned long flags;
        unsigned long flags;
 
 
        if (!usage_count){
        if (!usage_count){
                printk("trying to lock fdc while usage count=0\n");
                printk("trying to lock fdc while usage count=0\n");
                return -1;
                return -1;
        }
        }
        floppy_grab_irq_and_dma();
        floppy_grab_irq_and_dma();
        INT_OFF;
        INT_OFF;
        while (fdc_busy && NO_SIGNAL)
        while (fdc_busy && NO_SIGNAL)
                interruptible_sleep_on(&fdc_wait);
                interruptible_sleep_on(&fdc_wait);
        if (fdc_busy){
        if (fdc_busy){
                INT_ON;
                INT_ON;
                return -EINTR;
                return -EINTR;
        }
        }
        fdc_busy = 1;
        fdc_busy = 1;
        INT_ON;
        INT_ON;
        command_status = FD_COMMAND_NONE;
        command_status = FD_COMMAND_NONE;
        reschedule_timeout(drive, "lock fdc", 0);
        reschedule_timeout(drive, "lock fdc", 0);
        set_fdc(drive);
        set_fdc(drive);
        return 0;
        return 0;
}
}
 
 
#define LOCK_FDC(drive,interruptible) \
#define LOCK_FDC(drive,interruptible) \
if (lock_fdc(drive,interruptible)) return -EINTR;
if (lock_fdc(drive,interruptible)) return -EINTR;
 
 
 
 
/* unlocks the driver */
/* unlocks the driver */
static inline void unlock_fdc(void)
static inline void unlock_fdc(void)
{
{
        raw_cmd = 0;
        raw_cmd = 0;
        if (!fdc_busy)
        if (!fdc_busy)
                DPRINT("FDC access conflict!\n");
                DPRINT("FDC access conflict!\n");
 
 
        if (DEVICE_INTR)
        if (DEVICE_INTR)
                DPRINT("device interrupt still active at FDC release: %p!\n",
                DPRINT("device interrupt still active at FDC release: %p!\n",
                        DEVICE_INTR);
                        DEVICE_INTR);
        command_status = FD_COMMAND_NONE;
        command_status = FD_COMMAND_NONE;
        del_timer(&fd_timeout);
        del_timer(&fd_timeout);
        cont = NULL;
        cont = NULL;
        fdc_busy = 0;
        fdc_busy = 0;
        floppy_release_irq_and_dma();
        floppy_release_irq_and_dma();
        wake_up(&fdc_wait);
        wake_up(&fdc_wait);
}
}
 
 
/* switches the motor off after a given timeout */
/* switches the motor off after a given timeout */
static void motor_off_callback(unsigned long nr)
static void motor_off_callback(unsigned long nr)
{
{
        unsigned char mask = ~(0x10 << UNIT(nr));
        unsigned char mask = ~(0x10 << UNIT(nr));
 
 
        set_dor(FDC(nr), mask, 0);
        set_dor(FDC(nr), mask, 0);
}
}
 
 
static struct timer_list motor_off_timer[N_DRIVE] = {
static struct timer_list motor_off_timer[N_DRIVE] = {
        { NULL, NULL, 0, 0, motor_off_callback },
        { NULL, NULL, 0, 0, motor_off_callback },
        { NULL, NULL, 0, 1, motor_off_callback },
        { NULL, NULL, 0, 1, motor_off_callback },
        { NULL, NULL, 0, 2, motor_off_callback },
        { NULL, NULL, 0, 2, motor_off_callback },
        { NULL, NULL, 0, 3, motor_off_callback },
        { NULL, NULL, 0, 3, motor_off_callback },
        { NULL, NULL, 0, 4, motor_off_callback },
        { NULL, NULL, 0, 4, motor_off_callback },
        { NULL, NULL, 0, 5, motor_off_callback },
        { NULL, NULL, 0, 5, motor_off_callback },
        { NULL, NULL, 0, 6, motor_off_callback },
        { NULL, NULL, 0, 6, motor_off_callback },
        { NULL, NULL, 0, 7, motor_off_callback }
        { NULL, NULL, 0, 7, motor_off_callback }
};
};
 
 
/* schedules motor off */
/* schedules motor off */
static void floppy_off(unsigned int drive)
static void floppy_off(unsigned int drive)
{
{
        unsigned long volatile delta;
        unsigned long volatile delta;
        register int fdc=FDC(drive);
        register int fdc=FDC(drive);
 
 
        if (!(FDCS->dor & (0x10 << UNIT(drive))))
        if (!(FDCS->dor & (0x10 << UNIT(drive))))
                return;
                return;
 
 
        del_timer(motor_off_timer+drive);
        del_timer(motor_off_timer+drive);
 
 
        /* make spindle stop in a position which minimizes spinup time
        /* make spindle stop in a position which minimizes spinup time
         * next time */
         * next time */
        if (UDP->rps){
        if (UDP->rps){
                delta = jiffies - UDRS->first_read_date + HZ -
                delta = jiffies - UDRS->first_read_date + HZ -
                        UDP->spindown_offset;
                        UDP->spindown_offset;
                delta = ((delta * UDP->rps) % HZ) / UDP->rps;
                delta = ((delta * UDP->rps) % HZ) / UDP->rps;
                motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
                motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
        }
        }
        add_timer(motor_off_timer+drive);
        add_timer(motor_off_timer+drive);
}
}
 
 
/*
/*
 * cycle through all N_DRIVE floppy drives, for disk change testing.
 * cycle through all N_DRIVE floppy drives, for disk change testing.
 * stopping at current drive. This is done before any long operation, to
 * stopping at current drive. This is done before any long operation, to
 * be sure to have up to date disk change information.
 * be sure to have up to date disk change information.
 */
 */
static void scandrives(void)
static void scandrives(void)
{
{
        int i, drive, saved_drive;
        int i, drive, saved_drive;
 
 
        if (DP->select_delay)
        if (DP->select_delay)
                return;
                return;
 
 
        saved_drive = current_drive;
        saved_drive = current_drive;
        for (i=0; i < N_DRIVE; i++){
        for (i=0; i < N_DRIVE; i++){
                drive = (saved_drive + i + 1) % N_DRIVE;
                drive = (saved_drive + i + 1) % N_DRIVE;
                if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
                if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
                        continue; /* skip closed drives */
                        continue; /* skip closed drives */
                set_fdc(drive);
                set_fdc(drive);
                if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
                if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
                      (0x10 << UNIT(drive))))
                      (0x10 << UNIT(drive))))
                        /* switch the motor off again, if it was off to
                        /* switch the motor off again, if it was off to
                         * begin with */
                         * begin with */
                        set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
                        set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
        }
        }
        set_fdc(saved_drive);
        set_fdc(saved_drive);
}
}
 
 
static void empty(void)
static void empty(void)
{
{
}
}
 
 
static struct tq_struct floppy_tq =
static struct tq_struct floppy_tq =
{ 0, 0, 0, 0 };
{ 0, 0, 0, 0 };
 
 
static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
 
 
static void cancel_activity(void)
static void cancel_activity(void)
{
{
        CLEAR_INTR;
        CLEAR_INTR;
        floppy_tq.routine = (void *)(void *) empty;
        floppy_tq.routine = (void *)(void *) empty;
        del_timer(&fd_timer);
        del_timer(&fd_timer);
}
}
 
 
/* this function makes sure that the disk stays in the drive during the
/* this function makes sure that the disk stays in the drive during the
 * transfer */
 * transfer */
static void fd_watchdog(void)
static void fd_watchdog(void)
{
{
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (DP->flags & FD_DEBUG){
        if (DP->flags & FD_DEBUG){
                DPRINT("calling disk change from watchdog\n");
                DPRINT("calling disk change from watchdog\n");
        }
        }
#endif
#endif
 
 
        if (disk_change(current_drive)){
        if (disk_change(current_drive)){
                DPRINT("disk removed during i/o\n");
                DPRINT("disk removed during i/o\n");
                cancel_activity();
                cancel_activity();
                cont->done(0);
                cont->done(0);
                reset_fdc();
                reset_fdc();
        } else {
        } else {
                del_timer(&fd_timer);
                del_timer(&fd_timer);
                fd_timer.function = (timeout_fn) fd_watchdog;
                fd_timer.function = (timeout_fn) fd_watchdog;
                fd_timer.expires = jiffies + HZ / 10;
                fd_timer.expires = jiffies + HZ / 10;
                add_timer(&fd_timer);
                add_timer(&fd_timer);
        }
        }
}
}
 
 
static void main_command_interrupt(void)
static void main_command_interrupt(void)
{
{
        del_timer(&fd_timer);
        del_timer(&fd_timer);
        cont->interrupt();
        cont->interrupt();
}
}
 
 
/* waits for a delay (spinup or select) to pass */
/* waits for a delay (spinup or select) to pass */
static int wait_for_completion(int delay, timeout_fn function)
static int wait_for_completion(int delay, timeout_fn function)
{
{
        if (FDCS->reset){
        if (FDCS->reset){
                reset_fdc(); /* do the reset during sleep to win time
                reset_fdc(); /* do the reset during sleep to win time
                              * if we don't need to sleep, it's a good
                              * if we don't need to sleep, it's a good
                              * occasion anyways */
                              * occasion anyways */
                return 1;
                return 1;
        }
        }
 
 
        if ((signed) (jiffies - delay) < 0){
        if ((signed) (jiffies - delay) < 0){
                del_timer(&fd_timer);
                del_timer(&fd_timer);
                fd_timer.function = function;
                fd_timer.function = function;
                fd_timer.expires = delay;
                fd_timer.expires = delay;
                add_timer(&fd_timer);
                add_timer(&fd_timer);
                return 1;
                return 1;
        }
        }
        return 0;
        return 0;
}
}
 
 
static int hlt_disabled=0;
static int hlt_disabled=0;
static void floppy_disable_hlt(void)
static void floppy_disable_hlt(void)
{
{
        unsigned long flags;
        unsigned long flags;
 
 
        INT_OFF;
        INT_OFF;
        if (!hlt_disabled){
        if (!hlt_disabled){
                hlt_disabled=1;
                hlt_disabled=1;
#ifdef HAVE_DISABLE_HLT
#ifdef HAVE_DISABLE_HLT
                disable_hlt();
                disable_hlt();
#endif
#endif
        }
        }
        INT_ON;
        INT_ON;
}
}
 
 
static void floppy_enable_hlt(void)
static void floppy_enable_hlt(void)
{
{
        unsigned long flags;
        unsigned long flags;
 
 
        INT_OFF;
        INT_OFF;
        if (hlt_disabled){
        if (hlt_disabled){
                hlt_disabled=0;
                hlt_disabled=0;
#ifdef HAVE_DISABLE_HLT
#ifdef HAVE_DISABLE_HLT
                enable_hlt();
                enable_hlt();
#endif
#endif
        }
        }
        INT_ON;
        INT_ON;
}
}
 
 
 
 
static void setup_DMA(void)
static void setup_DMA(void)
{
{
        unsigned long flags;
        unsigned long flags;
 
 
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        if (raw_cmd->length == 0){
        if (raw_cmd->length == 0){
                int i;
                int i;
 
 
                printk("zero dma transfer size:");
                printk("zero dma transfer size:");
                for (i=0; i < raw_cmd->cmd_count; i++)
                for (i=0; i < raw_cmd->cmd_count; i++)
                        printk("%x,", raw_cmd->cmd[i]);
                        printk("%x,", raw_cmd->cmd[i]);
                printk("\n");
                printk("\n");
                cont->done(0);
                cont->done(0);
                FDCS->reset = 1;
                FDCS->reset = 1;
                return;
                return;
        }
        }
#if 0
#if 0
        if ((long) raw_cmd->kernel_data % 512){
        if ((long) raw_cmd->kernel_data % 512){
                printk("non aligned address: %p\n", raw_cmd->kernel_data);
                printk("non aligned address: %p\n", raw_cmd->kernel_data);
                cont->done(0);
                cont->done(0);
                FDCS->reset=1;
                FDCS->reset=1;
                return;
                return;
        }
        }
        if (CROSS_64KB(raw_cmd->kernel_data, raw_cmd->length)) {
        if (CROSS_64KB(raw_cmd->kernel_data, raw_cmd->length)) {
                printk("DMA crossing 64-K boundary %p-%p\n",
                printk("DMA crossing 64-K boundary %p-%p\n",
                       raw_cmd->kernel_data,
                       raw_cmd->kernel_data,
                       raw_cmd->kernel_data + raw_cmd->length);
                       raw_cmd->kernel_data + raw_cmd->length);
                cont->done(0);
                cont->done(0);
                FDCS->reset=1;
                FDCS->reset=1;
                return;
                return;
        }
        }
#endif
#endif
#endif
#endif
        INT_OFF;
        INT_OFF;
        fd_disable_dma();
        fd_disable_dma();
        fd_clear_dma_ff();
        fd_clear_dma_ff();
        fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
        fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
        fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
        fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
                        DMA_MODE_READ : DMA_MODE_WRITE);
                        DMA_MODE_READ : DMA_MODE_WRITE);
        fd_set_dma_addr(virt_to_bus(raw_cmd->kernel_data));
        fd_set_dma_addr(virt_to_bus(raw_cmd->kernel_data));
        fd_set_dma_count(raw_cmd->length);
        fd_set_dma_count(raw_cmd->length);
        virtual_dma_port = FDCS->address;
        virtual_dma_port = FDCS->address;
        fd_enable_dma();
        fd_enable_dma();
        INT_ON;
        INT_ON;
        floppy_disable_hlt();
        floppy_disable_hlt();
}
}
 
 
void show_floppy(void);
void show_floppy(void);
 
 
/* waits until the fdc becomes ready */
/* waits until the fdc becomes ready */
static int wait_til_ready(void)
static int wait_til_ready(void)
{
{
        int counter, status;
        int counter, status;
        if(FDCS->reset)
        if(FDCS->reset)
                return -1;
                return -1;
        for (counter = 0; counter < 10000; counter++) {
        for (counter = 0; counter < 10000; counter++) {
                status = fd_inb(FD_STATUS);
                status = fd_inb(FD_STATUS);
                if (status & STATUS_READY)
                if (status & STATUS_READY)
                        return status;
                        return status;
        }
        }
        if (!initialising) {
        if (!initialising) {
                DPRINT("Getstatus times out (%x) on fdc %d\n",
                DPRINT("Getstatus times out (%x) on fdc %d\n",
                        status, fdc);
                        status, fdc);
                show_floppy();
                show_floppy();
        }
        }
        FDCS->reset = 1;
        FDCS->reset = 1;
        return -1;
        return -1;
}
}
 
 
/* sends a command byte to the fdc */
/* sends a command byte to the fdc */
static int output_byte(char byte)
static int output_byte(char byte)
{
{
        int status;
        int status;
 
 
        if ((status = wait_til_ready()) < 0)
        if ((status = wait_til_ready()) < 0)
                return -1;
                return -1;
        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
                fd_outb(byte,FD_DATA);
                fd_outb(byte,FD_DATA);
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
                output_log[output_log_pos].data = byte;
                output_log[output_log_pos].data = byte;
                output_log[output_log_pos].status = status;
                output_log[output_log_pos].status = status;
                output_log[output_log_pos].jiffies = jiffies;
                output_log[output_log_pos].jiffies = jiffies;
                output_log_pos = (output_log_pos + 1) % OLOGSIZE;
                output_log_pos = (output_log_pos + 1) % OLOGSIZE;
#endif
#endif
                return 0;
                return 0;
        }
        }
        FDCS->reset = 1;
        FDCS->reset = 1;
        if (!initialising) {
        if (!initialising) {
                DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
                DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
                       byte, fdc, status);
                       byte, fdc, status);
                show_floppy();
                show_floppy();
        }
        }
        return -1;
        return -1;
}
}
#define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
#define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
 
 
/* gets the response from the fdc */
/* gets the response from the fdc */
static int result(void)
static int result(void)
{
{
        int i, status;
        int i, status;
 
 
        for(i=0; i < MAX_REPLIES; i++) {
        for(i=0; i < MAX_REPLIES; i++) {
                if ((status = wait_til_ready()) < 0)
                if ((status = wait_til_ready()) < 0)
                        break;
                        break;
                status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
                status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
                if ((status & ~STATUS_BUSY) == STATUS_READY){
                if ((status & ~STATUS_BUSY) == STATUS_READY){
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
                        resultjiffies = jiffies;
                        resultjiffies = jiffies;
                        resultsize = i;
                        resultsize = i;
#endif
#endif
                        return i;
                        return i;
                }
                }
                if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
                if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
                        reply_buffer[i] = fd_inb(FD_DATA);
                        reply_buffer[i] = fd_inb(FD_DATA);
                else
                else
                        break;
                        break;
        }
        }
        if(!initialising) {
        if(!initialising) {
                DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
                DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
                       fdc, status, i);
                       fdc, status, i);
                show_floppy();
                show_floppy();
        }
        }
        FDCS->reset = 1;
        FDCS->reset = 1;
        return -1;
        return -1;
}
}
 
 
#define MORE_OUTPUT -2
#define MORE_OUTPUT -2
/* does the fdc need more output? */
/* does the fdc need more output? */
static int need_more_output(void)
static int need_more_output(void)
{
{
        int status;
        int status;
        if( (status = wait_til_ready()) < 0)
        if( (status = wait_til_ready()) < 0)
                return -1;
                return -1;
        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
                return MORE_OUTPUT;
                return MORE_OUTPUT;
        return result();
        return result();
}
}
 
 
/* Set perpendicular mode as required, based on data rate, if supported.
/* Set perpendicular mode as required, based on data rate, if supported.
 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
 */
 */
static inline void perpendicular_mode(void)
static inline void perpendicular_mode(void)
{
{
        unsigned char perp_mode;
        unsigned char perp_mode;
 
 
        if (raw_cmd->rate & 0x40){
        if (raw_cmd->rate & 0x40){
                switch(raw_cmd->rate & 3){
                switch(raw_cmd->rate & 3){
                        case 0:
                        case 0:
                                perp_mode=2;
                                perp_mode=2;
                                break;
                                break;
                        case 3:
                        case 3:
                                perp_mode=3;
                                perp_mode=3;
                                break;
                                break;
                        default:
                        default:
                                DPRINT("Invalid data rate for perpendicular mode!\n");
                                DPRINT("Invalid data rate for perpendicular mode!\n");
                                cont->done(0);
                                cont->done(0);
                                FDCS->reset = 1; /* convenient way to return to
                                FDCS->reset = 1; /* convenient way to return to
                                                  * redo without to much hassle (deep
                                                  * redo without to much hassle (deep
                                                  * stack et al. */
                                                  * stack et al. */
                                return;
                                return;
                }
                }
        } else
        } else
                perp_mode = 0;
                perp_mode = 0;
 
 
        if (FDCS->perp_mode == perp_mode)
        if (FDCS->perp_mode == perp_mode)
                return;
                return;
        if (FDCS->version >= FDC_82077_ORIG) {
        if (FDCS->version >= FDC_82077_ORIG) {
                output_byte(FD_PERPENDICULAR);
                output_byte(FD_PERPENDICULAR);
                output_byte(perp_mode);
                output_byte(perp_mode);
                FDCS->perp_mode = perp_mode;
                FDCS->perp_mode = perp_mode;
        } else if (perp_mode) {
        } else if (perp_mode) {
                DPRINT("perpendicular mode not supported by this FDC.\n");
                DPRINT("perpendicular mode not supported by this FDC.\n");
        }
        }
} /* perpendicular_mode */
} /* perpendicular_mode */
 
 
static int fifo_depth = 0xa;
static int fifo_depth = 0xa;
static int no_fifo = 0;
static int no_fifo = 0;
 
 
static int fdc_configure(void)
static int fdc_configure(void)
{
{
        /* Turn on FIFO */
        /* Turn on FIFO */
        output_byte(FD_CONFIGURE);
        output_byte(FD_CONFIGURE);
        if(need_more_output() != MORE_OUTPUT)
        if(need_more_output() != MORE_OUTPUT)
                return 0;
                return 0;
        output_byte(0);
        output_byte(0);
        output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
        output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
        output_byte(0);  /* pre-compensation from track
        output_byte(0);  /* pre-compensation from track
                           0 upwards */
                           0 upwards */
        return 1;
        return 1;
}
}
 
 
#define NOMINAL_DTR 500
#define NOMINAL_DTR 500
 
 
/* Issue a "SPECIFY" command to set the step rate time, head unload time,
/* Issue a "SPECIFY" command to set the step rate time, head unload time,
 * head load time, and DMA disable flag to values needed by floppy.
 * head load time, and DMA disable flag to values needed by floppy.
 *
 *
 * The value "dtr" is the data transfer rate in Kbps.  It is needed
 * The value "dtr" is the data transfer rate in Kbps.  It is needed
 * to account for the data rate-based scaling done by the 82072 and 82077
 * to account for the data rate-based scaling done by the 82072 and 82077
 * FDC types.  This parameter is ignored for other types of FDCs (i.e.
 * FDC types.  This parameter is ignored for other types of FDCs (i.e.
 * 8272a).
 * 8272a).
 *
 *
 * Note that changing the data transfer rate has a (probably deleterious)
 * Note that changing the data transfer rate has a (probably deleterious)
 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
 * fdc_specify is called again after each data transfer rate
 * fdc_specify is called again after each data transfer rate
 * change.
 * change.
 *
 *
 * srt: 1000 to 16000 in microseconds
 * srt: 1000 to 16000 in microseconds
 * hut: 16 to 240 milliseconds
 * hut: 16 to 240 milliseconds
 * hlt: 2 to 254 milliseconds
 * hlt: 2 to 254 milliseconds
 *
 *
 * These values are rounded up to the next highest available delay time.
 * These values are rounded up to the next highest available delay time.
 */
 */
static void fdc_specify(void)
static void fdc_specify(void)
{
{
        unsigned char spec1, spec2;
        unsigned char spec1, spec2;
        int srt, hlt, hut;
        int srt, hlt, hut;
        unsigned long dtr = NOMINAL_DTR;
        unsigned long dtr = NOMINAL_DTR;
        unsigned long scale_dtr = NOMINAL_DTR;
        unsigned long scale_dtr = NOMINAL_DTR;
        int hlt_max_code = 0x7f;
        int hlt_max_code = 0x7f;
        int hut_max_code = 0xf;
        int hut_max_code = 0xf;
 
 
        if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
        if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
                fdc_configure();
                fdc_configure();
                FDCS->need_configure = 0;
                FDCS->need_configure = 0;
                /*DPRINT("FIFO enabled\n");*/
                /*DPRINT("FIFO enabled\n");*/
        }
        }
 
 
        switch (raw_cmd->rate & 0x03) {
        switch (raw_cmd->rate & 0x03) {
                case 3:
                case 3:
                        dtr = 1000;
                        dtr = 1000;
                        break;
                        break;
                case 1:
                case 1:
                        dtr = 300;
                        dtr = 300;
                        if (FDCS->version >= FDC_82078) {
                        if (FDCS->version >= FDC_82078) {
                                /* chose the default rate table, not the one
                                /* chose the default rate table, not the one
                                 * where 1 = 2 Mbps */
                                 * where 1 = 2 Mbps */
                                output_byte(FD_DRIVESPEC);
                                output_byte(FD_DRIVESPEC);
                                if(need_more_output() == MORE_OUTPUT) {
                                if(need_more_output() == MORE_OUTPUT) {
                                        output_byte(UNIT(current_drive));
                                        output_byte(UNIT(current_drive));
                                        output_byte(0xc0);
                                        output_byte(0xc0);
                                }
                                }
                        }
                        }
                        break;
                        break;
                case 2:
                case 2:
                        dtr = 250;
                        dtr = 250;
                        break;
                        break;
        }
        }
 
 
        if (FDCS->version >= FDC_82072) {
        if (FDCS->version >= FDC_82072) {
                scale_dtr = dtr;
                scale_dtr = dtr;
                hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
                hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
                hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
                hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
        }
        }
 
 
        /* Convert step rate from microseconds to milliseconds and 4 bits */
        /* Convert step rate from microseconds to milliseconds and 4 bits */
        srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
        srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
        SUPBOUND(srt, 0xf);
        SUPBOUND(srt, 0xf);
        INFBOUND(srt, 0);
        INFBOUND(srt, 0);
 
 
        hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
        hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
        if (hlt < 0x01)
        if (hlt < 0x01)
                hlt = 0x01;
                hlt = 0x01;
        else if (hlt > 0x7f)
        else if (hlt > 0x7f)
                hlt = hlt_max_code;
                hlt = hlt_max_code;
 
 
        hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
        hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
        if (hut < 0x1)
        if (hut < 0x1)
                hut = 0x1;
                hut = 0x1;
        else if (hut > 0xf)
        else if (hut > 0xf)
                hut = hut_max_code;
                hut = hut_max_code;
 
 
        spec1 = (srt << 4) | hut;
        spec1 = (srt << 4) | hut;
        spec2 = (hlt << 1) | (use_virtual_dma & 1);
        spec2 = (hlt << 1) | (use_virtual_dma & 1);
 
 
        /* If these parameters did not change, just return with success */
        /* If these parameters did not change, just return with success */
        if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
        if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
                /* Go ahead and set spec1 and spec2 */
                /* Go ahead and set spec1 and spec2 */
                output_byte(FD_SPECIFY);
                output_byte(FD_SPECIFY);
                output_byte(FDCS->spec1 = spec1);
                output_byte(FDCS->spec1 = spec1);
                output_byte(FDCS->spec2 = spec2);
                output_byte(FDCS->spec2 = spec2);
        }
        }
} /* fdc_specify */
} /* fdc_specify */
 
 
/* Set the FDC's data transfer rate on behalf of the specified drive.
/* Set the FDC's data transfer rate on behalf of the specified drive.
 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
 * of the specify command (i.e. using the fdc_specify function).
 * of the specify command (i.e. using the fdc_specify function).
 */
 */
static int fdc_dtr(void)
static int fdc_dtr(void)
{
{
        /* If data rate not already set to desired value, set it. */
        /* If data rate not already set to desired value, set it. */
        if ((raw_cmd->rate & 3) == FDCS->dtr)
        if ((raw_cmd->rate & 3) == FDCS->dtr)
                return 0;
                return 0;
 
 
        /* Set dtr */
        /* Set dtr */
        fd_outb(raw_cmd->rate & 3, FD_DCR);
        fd_outb(raw_cmd->rate & 3, FD_DCR);
 
 
        /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
        /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
         * need a stabilization period of several milliseconds to be
         * need a stabilization period of several milliseconds to be
         * enforced after data rate changes before R/W operations.
         * enforced after data rate changes before R/W operations.
         * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
         * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
         */
         */
        FDCS->dtr = raw_cmd->rate & 3;
        FDCS->dtr = raw_cmd->rate & 3;
        return(wait_for_completion(jiffies+2*HZ/100,
        return(wait_for_completion(jiffies+2*HZ/100,
                                   (timeout_fn) floppy_ready));
                                   (timeout_fn) floppy_ready));
} /* fdc_dtr */
} /* fdc_dtr */
 
 
static void tell_sector(void)
static void tell_sector(void)
{
{
        printk(": track %d, head %d, sector %d, size %d",
        printk(": track %d, head %d, sector %d, size %d",
               R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
               R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
} /* tell_sector */
} /* tell_sector */
 
 
 
 
/*
/*
 * OK, this error interpreting routine is called after a
 * OK, this error interpreting routine is called after a
 * DMA read/write has succeeded
 * DMA read/write has succeeded
 * or failed, so we check the results, and copy any buffers.
 * or failed, so we check the results, and copy any buffers.
 * hhb: Added better error reporting.
 * hhb: Added better error reporting.
 * ak: Made this into a separate routine.
 * ak: Made this into a separate routine.
 */
 */
static int interpret_errors(void)
static int interpret_errors(void)
{
{
        char bad;
        char bad;
int res = get_dma_residue(FLOPPY_DMA);
int res = get_dma_residue(FLOPPY_DMA);
if(res) {printk("\n-- DMA residue (%d)",res); tell_sector(); printk("\n");}
if(res) {printk("\n-- DMA residue (%d)",res); tell_sector(); printk("\n");}
 
 
        if (inr!=7) {
        if (inr!=7) {
                DPRINT("-- FDC reply error");
                DPRINT("-- FDC reply error");
                FDCS->reset = 1;
                FDCS->reset = 1;
                return 1;
                return 1;
        }
        }
 
 
        /* check IC to find cause of interrupt */
        /* check IC to find cause of interrupt */
        switch (ST0 & ST0_INTR) {
        switch (ST0 & ST0_INTR) {
                case 0x40:      /* error occurred during command execution */
                case 0x40:      /* error occurred during command execution */
                        if (ST1 & ST1_EOC)
                        if (ST1 & ST1_EOC)
                                return 0; /* occurs with pseudo-DMA */
                                return 0; /* occurs with pseudo-DMA */
                        bad = 1;
                        bad = 1;
                        if (ST1 & ST1_WP) {
                        if (ST1 & ST1_WP) {
                                DPRINT("Drive is write protected\n");
                                DPRINT("Drive is write protected\n");
                                CLEARF(FD_DISK_WRITABLE);
                                CLEARF(FD_DISK_WRITABLE);
                                cont->done(0);
                                cont->done(0);
                                bad = 2;
                                bad = 2;
                        } else if (ST1 & ST1_ND) {
                        } else if (ST1 & ST1_ND) {
                                SETF(FD_NEED_TWADDLE);
                                SETF(FD_NEED_TWADDLE);
                        } else if (ST1 & ST1_OR) {
                        } else if (ST1 & ST1_OR) {
                                if (DP->flags & FTD_MSG)
                                if (DP->flags & FTD_MSG)
                                        DPRINT("Over/Underrun - retrying\n");
                                        DPRINT("Over/Underrun - retrying\n");
                                bad = 0;
                                bad = 0;
                        }else if (*errors >= DP->max_errors.reporting){
                        }else if (*errors >= DP->max_errors.reporting){
                                DPRINT("");
                                DPRINT("");
                                if (ST0 & ST0_ECE) {
                                if (ST0 & ST0_ECE) {
                                        printk("Recalibrate failed!");
                                        printk("Recalibrate failed!");
                                } else if (ST2 & ST2_CRC) {
                                } else if (ST2 & ST2_CRC) {
                                        printk("data CRC error");
                                        printk("data CRC error");
                                        tell_sector();
                                        tell_sector();
                                } else if (ST1 & ST1_CRC) {
                                } else if (ST1 & ST1_CRC) {
                                        printk("CRC error");
                                        printk("CRC error");
                                        tell_sector();
                                        tell_sector();
                                } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
                                } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
                                        if (!probing) {
                                        if (!probing) {
                                                printk("sector not found");
                                                printk("sector not found");
                                                tell_sector();
                                                tell_sector();
                                        } else
                                        } else
                                                printk("probe failed...");
                                                printk("probe failed...");
                                } else if (ST2 & ST2_WC) {      /* seek error */
                                } else if (ST2 & ST2_WC) {      /* seek error */
                                        printk("wrong cylinder");
                                        printk("wrong cylinder");
                                } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
                                } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
                                        printk("bad cylinder");
                                        printk("bad cylinder");
                                } else {
                                } else {
                                        printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
                                        printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
                                        tell_sector();
                                        tell_sector();
                                }
                                }
                                printk("\n");
                                printk("\n");
 
 
                        }
                        }
                        if (ST2 & ST2_WC || ST2 & ST2_BC)
                        if (ST2 & ST2_WC || ST2 & ST2_BC)
                                /* wrong cylinder => recal */
                                /* wrong cylinder => recal */
                                DRS->track = NEED_2_RECAL;
                                DRS->track = NEED_2_RECAL;
                        return bad;
                        return bad;
                case 0x80: /* invalid command given */
                case 0x80: /* invalid command given */
                        DPRINT("Invalid FDC command given!\n");
                        DPRINT("Invalid FDC command given!\n");
                        cont->done(0);
                        cont->done(0);
                        return 2;
                        return 2;
                case 0xc0:
                case 0xc0:
                        DPRINT("Abnormal termination caused by polling\n");
                        DPRINT("Abnormal termination caused by polling\n");
                        cont->error();
                        cont->error();
                        return 2;
                        return 2;
                default: /* (0) Normal command termination */
                default: /* (0) Normal command termination */
                        return 0;
                        return 0;
        }
        }
}
}
 
 
/*
/*
 * This routine is called when everything should be correctly set up
 * This routine is called when everything should be correctly set up
 * for the transfer (i.e. floppy motor is on, the correct floppy is
 * for the transfer (i.e. floppy motor is on, the correct floppy is
 * selected, and the head is sitting on the right track).
 * selected, and the head is sitting on the right track).
 */
 */
static void setup_rw_floppy(void)
static void setup_rw_floppy(void)
{
{
        int i,ready_date,r, flags,dflags;
        int i,ready_date,r, flags,dflags;
        timeout_fn function;
        timeout_fn function;
 
 
        flags = raw_cmd->flags;
        flags = raw_cmd->flags;
        if (flags & (FD_RAW_READ | FD_RAW_WRITE))
        if (flags & (FD_RAW_READ | FD_RAW_WRITE))
                flags |= FD_RAW_INTR;
                flags |= FD_RAW_INTR;
 
 
        if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
        if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
                ready_date = DRS->spinup_date + DP->spinup;
                ready_date = DRS->spinup_date + DP->spinup;
                /* If spinup will take a long time, rerun scandrives
                /* If spinup will take a long time, rerun scandrives
                 * again just before spinup completion. Beware that
                 * again just before spinup completion. Beware that
                 * after scandrives, we must again wait for selection.
                 * after scandrives, we must again wait for selection.
                 */
                 */
                if ((signed) (ready_date - jiffies) > DP->select_delay){
                if ((signed) (ready_date - jiffies) > DP->select_delay){
                        ready_date -= DP->select_delay;
                        ready_date -= DP->select_delay;
                        function = (timeout_fn) floppy_start;
                        function = (timeout_fn) floppy_start;
                } else
                } else
                        function = (timeout_fn) setup_rw_floppy;
                        function = (timeout_fn) setup_rw_floppy;
 
 
                /* wait until the floppy is spinning fast enough */
                /* wait until the floppy is spinning fast enough */
                if (wait_for_completion(ready_date,function))
                if (wait_for_completion(ready_date,function))
                        return;
                        return;
        }
        }
        dflags = DRS->flags;
        dflags = DRS->flags;
 
 
        if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
        if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
                setup_DMA();
                setup_DMA();
 
 
        if (flags & FD_RAW_INTR)
        if (flags & FD_RAW_INTR)
                SET_INTR(main_command_interrupt);
                SET_INTR(main_command_interrupt);
 
 
        r=0;
        r=0;
        for (i=0; i< raw_cmd->cmd_count; i++)
        for (i=0; i< raw_cmd->cmd_count; i++)
                r|=output_byte(raw_cmd->cmd[i]);
                r|=output_byte(raw_cmd->cmd[i]);
 
 
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("rw_command: ");
        debugt("rw_command: ");
#endif
#endif
        if (r){
        if (r){
                cont->error();
                cont->error();
                reset_fdc();
                reset_fdc();
                return;
                return;
        }
        }
 
 
        if (!(flags & FD_RAW_INTR)){
        if (!(flags & FD_RAW_INTR)){
                inr = result();
                inr = result();
                cont->interrupt();
                cont->interrupt();
        } else if (flags & FD_RAW_NEED_DISK)
        } else if (flags & FD_RAW_NEED_DISK)
                fd_watchdog();
                fd_watchdog();
}
}
 
 
static int blind_seek;
static int blind_seek;
 
 
/*
/*
 * This is the routine called after every seek (or recalibrate) interrupt
 * This is the routine called after every seek (or recalibrate) interrupt
 * from the floppy controller.
 * from the floppy controller.
 */
 */
static void seek_interrupt(void)
static void seek_interrupt(void)
{
{
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("seek interrupt:");
        debugt("seek interrupt:");
#endif
#endif
        if (inr != 2 || (ST0 & 0xF8) != 0x20) {
        if (inr != 2 || (ST0 & 0xF8) != 0x20) {
                DPRINT("seek failed\n");
                DPRINT("seek failed\n");
                DRS->track = NEED_2_RECAL;
                DRS->track = NEED_2_RECAL;
                cont->error();
                cont->error();
                cont->redo();
                cont->redo();
                return;
                return;
        }
        }
        if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
        if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
                if (DP->flags & FD_DEBUG){
                if (DP->flags & FD_DEBUG){
                        DPRINT("clearing NEWCHANGE flag because of effective seek\n");
                        DPRINT("clearing NEWCHANGE flag because of effective seek\n");
                        DPRINT("jiffies=%ld\n", jiffies);
                        DPRINT("jiffies=%ld\n", jiffies);
                }
                }
#endif
#endif
                CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
                CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
                DRS->select_date = jiffies;
                DRS->select_date = jiffies;
        }
        }
        DRS->track = ST1;
        DRS->track = ST1;
        floppy_ready();
        floppy_ready();
}
}
 
 
static void check_wp(void)
static void check_wp(void)
{
{
        if (TESTF(FD_VERIFY)) {
        if (TESTF(FD_VERIFY)) {
                /* check write protection */
                /* check write protection */
                output_byte(FD_GETSTATUS);
                output_byte(FD_GETSTATUS);
                output_byte(UNIT(current_drive));
                output_byte(UNIT(current_drive));
                if (result() != 1){
                if (result() != 1){
                        FDCS->reset = 1;
                        FDCS->reset = 1;
                        return;
                        return;
                }
                }
                CLEARF(FD_VERIFY);
                CLEARF(FD_VERIFY);
                CLEARF(FD_NEED_TWADDLE);
                CLEARF(FD_NEED_TWADDLE);
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
                if (DP->flags & FD_DEBUG){
                if (DP->flags & FD_DEBUG){
                        DPRINT("checking whether disk is write protected\n");
                        DPRINT("checking whether disk is write protected\n");
                        DPRINT("wp=%x\n",ST3 & 0x40);
                        DPRINT("wp=%x\n",ST3 & 0x40);
                }
                }
#endif
#endif
                if (!(ST3  & 0x40))
                if (!(ST3  & 0x40))
                        SETF(FD_DISK_WRITABLE);
                        SETF(FD_DISK_WRITABLE);
                else
                else
                        CLEARF(FD_DISK_WRITABLE);
                        CLEARF(FD_DISK_WRITABLE);
        }
        }
}
}
 
 
static void seek_floppy(void)
static void seek_floppy(void)
{
{
        int track;
        int track;
 
 
        blind_seek=0;
        blind_seek=0;
 
 
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (DP->flags & FD_DEBUG){
        if (DP->flags & FD_DEBUG){
                DPRINT("calling disk change from seek\n");
                DPRINT("calling disk change from seek\n");
        }
        }
#endif
#endif
 
 
        if (!TESTF(FD_DISK_NEWCHANGE) &&
        if (!TESTF(FD_DISK_NEWCHANGE) &&
            disk_change(current_drive) &&
            disk_change(current_drive) &&
            (raw_cmd->flags & FD_RAW_NEED_DISK)){
            (raw_cmd->flags & FD_RAW_NEED_DISK)){
                /* the media changed flag should be cleared after the seek.
                /* the media changed flag should be cleared after the seek.
                 * If it isn't, this means that there is really no disk in
                 * If it isn't, this means that there is really no disk in
                 * the drive.
                 * the drive.
                 */
                 */
                SETF(FD_DISK_CHANGED);
                SETF(FD_DISK_CHANGED);
                cont->done(0);
                cont->done(0);
                cont->redo();
                cont->redo();
                return;
                return;
        }
        }
        if (DRS->track <= NEED_1_RECAL){
        if (DRS->track <= NEED_1_RECAL){
                recalibrate_floppy();
                recalibrate_floppy();
                return;
                return;
        } else if (TESTF(FD_DISK_NEWCHANGE) &&
        } else if (TESTF(FD_DISK_NEWCHANGE) &&
                   (raw_cmd->flags & FD_RAW_NEED_DISK) &&
                   (raw_cmd->flags & FD_RAW_NEED_DISK) &&
                   (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
                   (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
                /* we seek to clear the media-changed condition. Does anybody
                /* we seek to clear the media-changed condition. Does anybody
                 * know a more elegant way, which works on all drives? */
                 * know a more elegant way, which works on all drives? */
                if (raw_cmd->track)
                if (raw_cmd->track)
                        track = raw_cmd->track - 1;
                        track = raw_cmd->track - 1;
                else {
                else {
                        if (DP->flags & FD_SILENT_DCL_CLEAR){
                        if (DP->flags & FD_SILENT_DCL_CLEAR){
                                set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
                                set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
                                blind_seek = 1;
                                blind_seek = 1;
                                raw_cmd->flags |= FD_RAW_NEED_SEEK;
                                raw_cmd->flags |= FD_RAW_NEED_SEEK;
                        }
                        }
                        track = 1;
                        track = 1;
                }
                }
        } else {
        } else {
                check_wp();
                check_wp();
                if (raw_cmd->track != DRS->track &&
                if (raw_cmd->track != DRS->track &&
                    (raw_cmd->flags & FD_RAW_NEED_SEEK))
                    (raw_cmd->flags & FD_RAW_NEED_SEEK))
                        track = raw_cmd->track;
                        track = raw_cmd->track;
                else {
                else {
                        setup_rw_floppy();
                        setup_rw_floppy();
                        return;
                        return;
                }
                }
        }
        }
 
 
        SET_INTR(seek_interrupt);
        SET_INTR(seek_interrupt);
        output_byte(FD_SEEK);
        output_byte(FD_SEEK);
        output_byte(UNIT(current_drive));
        output_byte(UNIT(current_drive));
        LAST_OUT(track);
        LAST_OUT(track);
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("seek command:");
        debugt("seek command:");
#endif
#endif
}
}
 
 
static void recal_interrupt(void)
static void recal_interrupt(void)
{
{
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("recal interrupt:");
        debugt("recal interrupt:");
#endif
#endif
        if (inr !=2)
        if (inr !=2)
                FDCS->reset = 1;
                FDCS->reset = 1;
        else if (ST0 & ST0_ECE) {
        else if (ST0 & ST0_ECE) {
                switch(DRS->track){
                switch(DRS->track){
                        case NEED_1_RECAL:
                        case NEED_1_RECAL:
#ifdef DEBUGT
#ifdef DEBUGT
                                debugt("recal interrupt need 1 recal:");
                                debugt("recal interrupt need 1 recal:");
#endif
#endif
                                /* after a second recalibrate, we still haven't
                                /* after a second recalibrate, we still haven't
                                 * reached track 0. Probably no drive. Raise an
                                 * reached track 0. Probably no drive. Raise an
                                 * error, as failing immediately might upset
                                 * error, as failing immediately might upset
                                 * computers possessed by the Devil :-) */
                                 * computers possessed by the Devil :-) */
                                cont->error();
                                cont->error();
                                cont->redo();
                                cont->redo();
                                return;
                                return;
                        case NEED_2_RECAL:
                        case NEED_2_RECAL:
#ifdef DEBUGT
#ifdef DEBUGT
                                debugt("recal interrupt need 2 recal:");
                                debugt("recal interrupt need 2 recal:");
#endif
#endif
                                /* If we already did a recalibrate,
                                /* If we already did a recalibrate,
                                 * and we are not at track 0, this
                                 * and we are not at track 0, this
                                 * means we have moved. (The only way
                                 * means we have moved. (The only way
                                 * not to move at recalibration is to
                                 * not to move at recalibration is to
                                 * be already at track 0.) Clear the
                                 * be already at track 0.) Clear the
                                 * new change flag */
                                 * new change flag */
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
                                if (DP->flags & FD_DEBUG){
                                if (DP->flags & FD_DEBUG){
                                        DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
                                        DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
                                }
                                }
#endif
#endif
 
 
                                CLEARF(FD_DISK_NEWCHANGE);
                                CLEARF(FD_DISK_NEWCHANGE);
                                DRS->select_date = jiffies;
                                DRS->select_date = jiffies;
                                /* fall through */
                                /* fall through */
                        default:
                        default:
#ifdef DEBUGT
#ifdef DEBUGT
                                debugt("recal interrupt default:");
                                debugt("recal interrupt default:");
#endif
#endif
                                /* Recalibrate moves the head by at
                                /* Recalibrate moves the head by at
                                 * most 80 steps. If after one
                                 * most 80 steps. If after one
                                 * recalibrate we don't have reached
                                 * recalibrate we don't have reached
                                 * track 0, this might mean that we
                                 * track 0, this might mean that we
                                 * started beyond track 80.  Try
                                 * started beyond track 80.  Try
                                 * again.  */
                                 * again.  */
                                DRS->track = NEED_1_RECAL;
                                DRS->track = NEED_1_RECAL;
                                break;
                                break;
                }
                }
        } else
        } else
                DRS->track = ST1;
                DRS->track = ST1;
        floppy_ready();
        floppy_ready();
}
}
 
 
static void print_result(char *message, int inr)
static void print_result(char *message, int inr)
{
{
        int i;
        int i;
 
 
        DPRINT("%s ", message);
        DPRINT("%s ", message);
        if (inr >= 0)
        if (inr >= 0)
                for (i=0; i<inr; i++)
                for (i=0; i<inr; i++)
                        printk("repl[%d]=%x ", i, reply_buffer[i]);
                        printk("repl[%d]=%x ", i, reply_buffer[i]);
        printk("\n");
        printk("\n");
}
}
 
 
/* interrupt handler */
/* interrupt handler */
void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
{
{
        void (*handler)(void) = DEVICE_INTR;
        void (*handler)(void) = DEVICE_INTR;
        int do_print;
        int do_print;
 
 
        lasthandler = handler;
        lasthandler = handler;
        interruptjiffies = jiffies;
        interruptjiffies = jiffies;
 
 
        fd_disable_dma();
        fd_disable_dma();
        floppy_enable_hlt();
        floppy_enable_hlt();
        CLEAR_INTR;
        CLEAR_INTR;
        if (fdc >= N_FDC || FDCS->address == -1){
        if (fdc >= N_FDC || FDCS->address == -1){
                /* we don't even know which FDC is the culprit */
                /* we don't even know which FDC is the culprit */
                printk("DOR0=%x\n", (unsigned int)fdc_state[0].dor);
                printk("DOR0=%x\n", (unsigned int)fdc_state[0].dor);
                printk("floppy interrupt on bizarre fdc %d\n",fdc);
                printk("floppy interrupt on bizarre fdc %d\n",fdc);
                printk("handler=%p\n", handler);
                printk("handler=%p\n", handler);
                is_alive("bizarre fdc");
                is_alive("bizarre fdc");
                return;
                return;
        }
        }
 
 
        FDCS->reset = 0;
        FDCS->reset = 0;
        /* We have to clear the reset flag here, because apparently on boxes
        /* We have to clear the reset flag here, because apparently on boxes
         * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
         * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
         * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
         * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
         * emission of the SENSEI's.
         * emission of the SENSEI's.
         * It is OK to emit floppy commands because we are in an interrupt
         * It is OK to emit floppy commands because we are in an interrupt
         * handler here, and thus we have to fear no interference of other
         * handler here, and thus we have to fear no interference of other
         * activity.
         * activity.
         */
         */
 
 
        do_print = !handler && print_unex && !initialising;
        do_print = !handler && print_unex && !initialising;
 
 
        inr = result();
        inr = result();
        if(do_print)
        if(do_print)
                print_result("unexpected interrupt", inr);
                print_result("unexpected interrupt", inr);
        if (inr == 0){
        if (inr == 0){
                do {
                do {
                        output_byte(FD_SENSEI);
                        output_byte(FD_SENSEI);
                        inr = result();
                        inr = result();
                        if(do_print)
                        if(do_print)
                                print_result("sensei", inr);
                                print_result("sensei", inr);
                } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2);
                } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2);
        }
        }
        if (handler) {
        if (handler) {
                if(intr_count >= 2) {
                if(intr_count >= 2) {
                        /* expected interrupt */
                        /* expected interrupt */
                        floppy_tq.routine = (void *)(void *) handler;
                        floppy_tq.routine = (void *)(void *) handler;
                        queue_task_irq(&floppy_tq, &tq_immediate);
                        queue_task_irq(&floppy_tq, &tq_immediate);
                        mark_bh(IMMEDIATE_BH);
                        mark_bh(IMMEDIATE_BH);
                } else
                } else
                        handler();
                        handler();
        } else
        } else
                FDCS->reset = 1;
                FDCS->reset = 1;
        is_alive("normal interrupt end");
        is_alive("normal interrupt end");
}
}
 
 
static void recalibrate_floppy(void)
static void recalibrate_floppy(void)
{
{
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("recalibrate floppy:");
        debugt("recalibrate floppy:");
#endif
#endif
        SET_INTR(recal_interrupt);
        SET_INTR(recal_interrupt);
        output_byte(FD_RECALIBRATE);
        output_byte(FD_RECALIBRATE);
        LAST_OUT(UNIT(current_drive));
        LAST_OUT(UNIT(current_drive));
}
}
 
 
/*
/*
 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
 */
 */
static void reset_interrupt(void)
static void reset_interrupt(void)
{
{
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("reset interrupt:");
        debugt("reset interrupt:");
#endif
#endif
        result();               /* get the status ready for set_fdc */
        result();               /* get the status ready for set_fdc */
        if (FDCS->reset) {
        if (FDCS->reset) {
                printk("reset set in interrupt, calling %p\n", cont->error);
                printk("reset set in interrupt, calling %p\n", cont->error);
                cont->error(); /* a reset just after a reset. BAD! */
                cont->error(); /* a reset just after a reset. BAD! */
        }
        }
        cont->redo();
        cont->redo();
}
}
 
 
/*
/*
 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
 */
 */
static void reset_fdc(void)
static void reset_fdc(void)
{
{
        SET_INTR(reset_interrupt);
        SET_INTR(reset_interrupt);
        FDCS->reset = 0;
        FDCS->reset = 0;
        reset_fdc_info(0);
        reset_fdc_info(0);
 
 
        /* Pseudo-DMA may intercept 'reset finished' interrupt.  */
        /* Pseudo-DMA may intercept 'reset finished' interrupt.  */
        /* Irrelevant for systems with true DMA (i386).          */
        /* Irrelevant for systems with true DMA (i386).          */
        fd_disable_dma();
        fd_disable_dma();
 
 
        if (FDCS->version >= FDC_82072A)
        if (FDCS->version >= FDC_82072A)
                fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
                fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
        else {
        else {
                fd_setdor(FDCS->dor & ~0x04);
                fd_setdor(FDCS->dor & ~0x04);
                udelay(FD_RESET_DELAY);
                udelay(FD_RESET_DELAY);
                fd_setdor(FDCS->dor);
                fd_setdor(FDCS->dor);
        }
        }
}
}
 
 
void show_floppy(void)
void show_floppy(void)
{
{
        int i;
        int i;
 
 
        printk("\n");
        printk("\n");
        printk("floppy driver state\n");
        printk("floppy driver state\n");
        printk("-------------------\n");
        printk("-------------------\n");
        printk("now=%ld last interrupt=%d last called handler=%p\n",
        printk("now=%ld last interrupt=%d last called handler=%p\n",
               jiffies, interruptjiffies, lasthandler);
               jiffies, interruptjiffies, lasthandler);
 
 
 
 
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        printk("timeout_message=%s\n", timeout_message);
        printk("timeout_message=%s\n", timeout_message);
        printk("last output bytes:\n");
        printk("last output bytes:\n");
        for (i=0; i < OLOGSIZE; i++)
        for (i=0; i < OLOGSIZE; i++)
                printk("%2x %2x %ld\n",
                printk("%2x %2x %ld\n",
                       output_log[(i+output_log_pos) % OLOGSIZE].data,
                       output_log[(i+output_log_pos) % OLOGSIZE].data,
                       output_log[(i+output_log_pos) % OLOGSIZE].status,
                       output_log[(i+output_log_pos) % OLOGSIZE].status,
                       output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
                       output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
        printk("last result at %d\n", resultjiffies);
        printk("last result at %d\n", resultjiffies);
        printk("last redo_fd_request at %d\n", lastredo);
        printk("last redo_fd_request at %d\n", lastredo);
        for (i=0; i<resultsize; i++){
        for (i=0; i<resultsize; i++){
                printk("%2x ", reply_buffer[i]);
                printk("%2x ", reply_buffer[i]);
        }
        }
        printk("\n");
        printk("\n");
#endif
#endif
 
 
        printk("status=%x\n", fd_inb(FD_STATUS));
        printk("status=%x\n", fd_inb(FD_STATUS));
        printk("fdc_busy=%d\n", fdc_busy);
        printk("fdc_busy=%d\n", fdc_busy);
        if (DEVICE_INTR)
        if (DEVICE_INTR)
                printk("DEVICE_INTR=%p\n", DEVICE_INTR);
                printk("DEVICE_INTR=%p\n", DEVICE_INTR);
        if (floppy_tq.sync)
        if (floppy_tq.sync)
                printk("floppy_tq.routine=%p\n", floppy_tq.routine);
                printk("floppy_tq.routine=%p\n", floppy_tq.routine);
        if (fd_timer.prev)
        if (fd_timer.prev)
                printk("fd_timer.function=%p\n", fd_timer.function);
                printk("fd_timer.function=%p\n", fd_timer.function);
        if (fd_timeout.prev){
        if (fd_timeout.prev){
                printk("timer_table=%p\n",fd_timeout.function);
                printk("timer_table=%p\n",fd_timeout.function);
                printk("expires=%ld\n",fd_timeout.expires-jiffies);
                printk("expires=%ld\n",fd_timeout.expires-jiffies);
                printk("now=%ld\n",jiffies);
                printk("now=%ld\n",jiffies);
        }
        }
        printk("cont=%p\n", cont);
        printk("cont=%p\n", cont);
        printk("CURRENT=%p\n", CURRENT);
        printk("CURRENT=%p\n", CURRENT);
        printk("command_status=%d\n", command_status);
        printk("command_status=%d\n", command_status);
        printk("\n");
        printk("\n");
}
}
 
 
static void floppy_shutdown(void)
static void floppy_shutdown(void)
{
{
        if (!initialising)
        if (!initialising)
                show_floppy();
                show_floppy();
        cancel_activity();
        cancel_activity();
        sti();
        sti();
 
 
        floppy_enable_hlt();
        floppy_enable_hlt();
        fd_disable_dma();
        fd_disable_dma();
        /* avoid dma going to a random drive after shutdown */
        /* avoid dma going to a random drive after shutdown */
 
 
        if (!initialising)
        if (!initialising)
                DPRINT("floppy timeout called\n");
                DPRINT("floppy timeout called\n");
        FDCS->reset = 1;
        FDCS->reset = 1;
        if (cont){
        if (cont){
                cont->done(0);
                cont->done(0);
                cont->redo(); /* this will recall reset when needed */
                cont->redo(); /* this will recall reset when needed */
        } else {
        } else {
                printk("no cont in shutdown!\n");
                printk("no cont in shutdown!\n");
                process_fd_request();
                process_fd_request();
        }
        }
        is_alive("floppy shutdown");
        is_alive("floppy shutdown");
}
}
/*typedef void (*timeout_fn)(unsigned long);*/
/*typedef void (*timeout_fn)(unsigned long);*/
 
 
/* start motor, check media-changed condition and write protection */
/* start motor, check media-changed condition and write protection */
static int start_motor(void (*function)(void) )
static int start_motor(void (*function)(void) )
{
{
        int mask, data;
        int mask, data;
 
 
        mask = 0xfc;
        mask = 0xfc;
        data = UNIT(current_drive);
        data = UNIT(current_drive);
        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
                if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
                if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
                        set_debugt();
                        set_debugt();
                        /* no read since this drive is running */
                        /* no read since this drive is running */
                        DRS->first_read_date = 0;
                        DRS->first_read_date = 0;
                        /* note motor start time if motor is not yet running */
                        /* note motor start time if motor is not yet running */
                        DRS->spinup_date = jiffies;
                        DRS->spinup_date = jiffies;
                        data |= (0x10 << UNIT(current_drive));
                        data |= (0x10 << UNIT(current_drive));
                }
                }
        } else
        } else
                if (FDCS->dor & (0x10 << UNIT(current_drive)))
                if (FDCS->dor & (0x10 << UNIT(current_drive)))
                        mask &= ~(0x10 << UNIT(current_drive));
                        mask &= ~(0x10 << UNIT(current_drive));
 
 
        /* starts motor and selects floppy */
        /* starts motor and selects floppy */
        del_timer(motor_off_timer + current_drive);
        del_timer(motor_off_timer + current_drive);
        set_dor(fdc, mask, data);
        set_dor(fdc, mask, data);
 
 
        /* wait_for_completion also schedules reset if needed. */
        /* wait_for_completion also schedules reset if needed. */
        return(wait_for_completion(DRS->select_date+DP->select_delay,
        return(wait_for_completion(DRS->select_date+DP->select_delay,
                                   (timeout_fn) function));
                                   (timeout_fn) function));
}
}
 
 
static void floppy_ready(void)
static void floppy_ready(void)
{
{
        CHECK_RESET;
        CHECK_RESET;
        if (start_motor(floppy_ready)) return;
        if (start_motor(floppy_ready)) return;
        if (fdc_dtr()) return;
        if (fdc_dtr()) return;
 
 
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (DP->flags & FD_DEBUG){
        if (DP->flags & FD_DEBUG){
                DPRINT("calling disk change from floppy_ready\n");
                DPRINT("calling disk change from floppy_ready\n");
        }
        }
#endif
#endif
 
 
        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
           disk_change(current_drive) &&
           disk_change(current_drive) &&
           !DP->select_delay)
           !DP->select_delay)
                twaddle(); /* this clears the dcl on certain drive/controller
                twaddle(); /* this clears the dcl on certain drive/controller
                            * combinations */
                            * combinations */
 
 
        if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
        if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
                perpendicular_mode();
                perpendicular_mode();
                fdc_specify(); /* must be done here because of hut, hlt ... */
                fdc_specify(); /* must be done here because of hut, hlt ... */
                seek_floppy();
                seek_floppy();
        } else
        } else
                setup_rw_floppy();
                setup_rw_floppy();
}
}
 
 
static void floppy_start(void)
static void floppy_start(void)
{
{
        reschedule_timeout(CURRENTD, "floppy start", 0);
        reschedule_timeout(CURRENTD, "floppy start", 0);
 
 
        scandrives();
        scandrives();
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (DP->flags & FD_DEBUG){
        if (DP->flags & FD_DEBUG){
                DPRINT("setting NEWCHANGE in floppy_start\n");
                DPRINT("setting NEWCHANGE in floppy_start\n");
        }
        }
#endif
#endif
        SETF(FD_DISK_NEWCHANGE);
        SETF(FD_DISK_NEWCHANGE);
        floppy_ready();
        floppy_ready();
}
}
 
 
/*
/*
 * ========================================================================
 * ========================================================================
 * here ends the bottom half. Exported routines are:
 * here ends the bottom half. Exported routines are:
 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
 * and set_dor.
 * and set_dor.
 * ========================================================================
 * ========================================================================
 */
 */
/*
/*
 * General purpose continuations.
 * General purpose continuations.
 * ==============================
 * ==============================
 */
 */
 
 
static void do_wakeup(void)
static void do_wakeup(void)
{
{
        reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
        reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
        cont = 0;
        cont = 0;
        command_status += 2;
        command_status += 2;
        wake_up(&command_done);
        wake_up(&command_done);
}
}
 
 
static struct cont_t wakeup_cont={
static struct cont_t wakeup_cont={
        empty,
        empty,
        do_wakeup,
        do_wakeup,
        empty,
        empty,
        (done_f)empty
        (done_f)empty
};
};
 
 
 
 
static struct cont_t intr_cont={
static struct cont_t intr_cont={
        empty,
        empty,
        process_fd_request,
        process_fd_request,
        empty,
        empty,
        (done_f) empty
        (done_f) empty
};
};
 
 
static int wait_til_done(void (*handler)(void), int interruptible)
static int wait_til_done(void (*handler)(void), int interruptible)
{
{
        int ret;
        int ret;
        unsigned long flags;
        unsigned long flags;
 
 
        floppy_tq.routine = (void *)(void *) handler;
        floppy_tq.routine = (void *)(void *) handler;
        queue_task(&floppy_tq, &tq_immediate);
        queue_task(&floppy_tq, &tq_immediate);
        mark_bh(IMMEDIATE_BH);
        mark_bh(IMMEDIATE_BH);
        INT_OFF;
        INT_OFF;
        while(command_status < 2 && NO_SIGNAL){
        while(command_status < 2 && NO_SIGNAL){
                is_alive("wait_til_done");
                is_alive("wait_til_done");
                if (interruptible)
                if (interruptible)
                        interruptible_sleep_on(&command_done);
                        interruptible_sleep_on(&command_done);
                else
                else
                        sleep_on(&command_done);
                        sleep_on(&command_done);
        }
        }
        if (command_status < 2){
        if (command_status < 2){
                cancel_activity();
                cancel_activity();
                cont = &intr_cont;
                cont = &intr_cont;
                reset_fdc();
                reset_fdc();
                INT_ON;
                INT_ON;
                return -EINTR;
                return -EINTR;
        }
        }
        INT_ON;
        INT_ON;
 
 
        if (FDCS->reset)
        if (FDCS->reset)
                command_status = FD_COMMAND_ERROR;
                command_status = FD_COMMAND_ERROR;
        if (command_status == FD_COMMAND_OKAY)
        if (command_status == FD_COMMAND_OKAY)
                ret=0;
                ret=0;
        else
        else
                ret=-EIO;
                ret=-EIO;
        command_status = FD_COMMAND_NONE;
        command_status = FD_COMMAND_NONE;
        return ret;
        return ret;
}
}
 
 
static void generic_done(int result)
static void generic_done(int result)
{
{
        command_status = result;
        command_status = result;
        cont = &wakeup_cont;
        cont = &wakeup_cont;
}
}
 
 
static void generic_success(void)
static void generic_success(void)
{
{
        cont->done(1);
        cont->done(1);
}
}
 
 
static void generic_failure(void)
static void generic_failure(void)
{
{
        cont->done(0);
        cont->done(0);
}
}
 
 
static void success_and_wakeup(void)
static void success_and_wakeup(void)
{
{
        generic_success();
        generic_success();
        cont->redo();
        cont->redo();
}
}
 
 
 
 
/*
/*
 * formatting and rw support.
 * formatting and rw support.
 * ==========================
 * ==========================
 */
 */
 
 
static int next_valid_format(void)
static int next_valid_format(void)
{
{
        int probed_format;
        int probed_format;
 
 
        probed_format = DRS->probed_format;
        probed_format = DRS->probed_format;
        while(1){
        while(1){
                if (probed_format >= 8 ||
                if (probed_format >= 8 ||
                     !DP->autodetect[probed_format]){
                     !DP->autodetect[probed_format]){
                        DRS->probed_format = 0;
                        DRS->probed_format = 0;
                        return 1;
                        return 1;
                }
                }
                if (floppy_type[DP->autodetect[probed_format]].sect){
                if (floppy_type[DP->autodetect[probed_format]].sect){
                        DRS->probed_format = probed_format;
                        DRS->probed_format = probed_format;
                        return 0;
                        return 0;
                }
                }
                probed_format++;
                probed_format++;
        }
        }
}
}
 
 
static void bad_flp_intr(void)
static void bad_flp_intr(void)
{
{
        if (probing){
        if (probing){
                DRS->probed_format++;
                DRS->probed_format++;
                if (!next_valid_format())
                if (!next_valid_format())
                        return;
                        return;
        }
        }
        (*errors)++;
        (*errors)++;
        INFBOUND(DRWE->badness, *errors);
        INFBOUND(DRWE->badness, *errors);
        if (*errors > DP->max_errors.abort)
        if (*errors > DP->max_errors.abort)
                cont->done(0);
                cont->done(0);
        if (*errors > DP->max_errors.reset)
        if (*errors > DP->max_errors.reset)
                FDCS->reset = 1;
                FDCS->reset = 1;
        else if (*errors > DP->max_errors.recal)
        else if (*errors > DP->max_errors.recal)
                DRS->track = NEED_2_RECAL;
                DRS->track = NEED_2_RECAL;
}
}
 
 
static void set_floppy(kdev_t device)
static void set_floppy(kdev_t device)
{
{
        if (TYPE(device))
        if (TYPE(device))
                _floppy = TYPE(device) + floppy_type;
                _floppy = TYPE(device) + floppy_type;
        else
        else
                _floppy = current_type[ DRIVE(device) ];
                _floppy = current_type[ DRIVE(device) ];
}
}
 
 
/*
/*
 * formatting support.
 * formatting support.
 * ===================
 * ===================
 */
 */
static void format_interrupt(void)
static void format_interrupt(void)
{
{
        switch (interpret_errors()){
        switch (interpret_errors()){
                case 1:
                case 1:
                        cont->error();
                        cont->error();
                case 2:
                case 2:
                        break;
                        break;
                case 0:
                case 0:
                        cont->done(1);
                        cont->done(1);
        }
        }
        cont->redo();
        cont->redo();
}
}
 
 
#define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
#define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
#define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
#define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
#define CT(x) ((x) | 0x40)
#define CT(x) ((x) | 0x40)
static void setup_format_params(int track)
static void setup_format_params(int track)
{
{
        struct fparm {
        struct fparm {
                unsigned char track,head,sect,size;
                unsigned char track,head,sect,size;
        } *here = (struct fparm *)floppy_track_buffer;
        } *here = (struct fparm *)floppy_track_buffer;
        int il,n;
        int il,n;
        int count,head_shift,track_shift;
        int count,head_shift,track_shift;
 
 
        raw_cmd = &default_raw_cmd;
        raw_cmd = &default_raw_cmd;
        raw_cmd->track = track;
        raw_cmd->track = track;
 
 
        raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
        raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
                FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
                FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
        raw_cmd->rate = _floppy->rate & 0x43;
        raw_cmd->rate = _floppy->rate & 0x43;
        raw_cmd->cmd_count = NR_F;
        raw_cmd->cmd_count = NR_F;
        COMMAND = FM_MODE(_floppy,FD_FORMAT);
        COMMAND = FM_MODE(_floppy,FD_FORMAT);
        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
        F_SIZECODE = FD_SIZECODE(_floppy);
        F_SIZECODE = FD_SIZECODE(_floppy);
        F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
        F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
        F_GAP = _floppy->fmt_gap;
        F_GAP = _floppy->fmt_gap;
        F_FILL = FD_FILL_BYTE;
        F_FILL = FD_FILL_BYTE;
 
 
        raw_cmd->kernel_data = floppy_track_buffer;
        raw_cmd->kernel_data = floppy_track_buffer;
        raw_cmd->length = 4 * F_SECT_PER_TRACK;
        raw_cmd->length = 4 * F_SECT_PER_TRACK;
 
 
        /* allow for about 30ms for data transport per track */
        /* allow for about 30ms for data transport per track */
        head_shift  = (F_SECT_PER_TRACK + 5) / 6;
        head_shift  = (F_SECT_PER_TRACK + 5) / 6;
 
 
        /* a ``cylinder'' is two tracks plus a little stepping time */
        /* a ``cylinder'' is two tracks plus a little stepping time */
        track_shift = 2 * head_shift + 3;
        track_shift = 2 * head_shift + 3;
 
 
        /* position of logical sector 1 on this track */
        /* position of logical sector 1 on this track */
        n = (track_shift * format_req.track + head_shift * format_req.head)
        n = (track_shift * format_req.track + head_shift * format_req.head)
                % F_SECT_PER_TRACK;
                % F_SECT_PER_TRACK;
 
 
        /* determine interleave */
        /* determine interleave */
        il = 1;
        il = 1;
        if (_floppy->fmt_gap < 0x22)
        if (_floppy->fmt_gap < 0x22)
                il++;
                il++;
 
 
        /* initialize field */
        /* initialize field */
        for (count = 0; count < F_SECT_PER_TRACK; ++count) {
        for (count = 0; count < F_SECT_PER_TRACK; ++count) {
                here[count].track = format_req.track;
                here[count].track = format_req.track;
                here[count].head = format_req.head;
                here[count].head = format_req.head;
                here[count].sect = 0;
                here[count].sect = 0;
                here[count].size = F_SIZECODE;
                here[count].size = F_SIZECODE;
        }
        }
        /* place logical sectors */
        /* place logical sectors */
        for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
        for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
                here[n].sect = count;
                here[n].sect = count;
                n = (n+il) % F_SECT_PER_TRACK;
                n = (n+il) % F_SECT_PER_TRACK;
                if (here[n].sect) { /* sector busy, find next free sector */
                if (here[n].sect) { /* sector busy, find next free sector */
                        ++n;
                        ++n;
                        if (n>= F_SECT_PER_TRACK) {
                        if (n>= F_SECT_PER_TRACK) {
                                n-=F_SECT_PER_TRACK;
                                n-=F_SECT_PER_TRACK;
                                while (here[n].sect) ++n;
                                while (here[n].sect) ++n;
                        }
                        }
                }
                }
        }
        }
}
}
 
 
static void redo_format(void)
static void redo_format(void)
{
{
        buffer_track = -1;
        buffer_track = -1;
        setup_format_params(format_req.track << STRETCH(_floppy));
        setup_format_params(format_req.track << STRETCH(_floppy));
        floppy_start();
        floppy_start();
#ifdef DEBUGT
#ifdef DEBUGT
        debugt("queue format request");
        debugt("queue format request");
#endif
#endif
}
}
 
 
static struct cont_t format_cont={
static struct cont_t format_cont={
        format_interrupt,
        format_interrupt,
        redo_format,
        redo_format,
        bad_flp_intr,
        bad_flp_intr,
        generic_done };
        generic_done };
 
 
static int do_format(kdev_t device, struct format_descr *tmp_format_req)
static int do_format(kdev_t device, struct format_descr *tmp_format_req)
{
{
        int ret;
        int ret;
        int drive=DRIVE(device);
        int drive=DRIVE(device);
 
 
        LOCK_FDC(drive,1);
        LOCK_FDC(drive,1);
        set_floppy(device);
        set_floppy(device);
        if (!_floppy ||
        if (!_floppy ||
            _floppy->track > DP->tracks ||
            _floppy->track > DP->tracks ||
            tmp_format_req->track >= _floppy->track ||
            tmp_format_req->track >= _floppy->track ||
            tmp_format_req->head >= _floppy->head ||
            tmp_format_req->head >= _floppy->head ||
            (_floppy->sect << 2) % (1 <<  FD_SIZECODE(_floppy)) ||
            (_floppy->sect << 2) % (1 <<  FD_SIZECODE(_floppy)) ||
            !_floppy->fmt_gap) {
            !_floppy->fmt_gap) {
                process_fd_request();
                process_fd_request();
                return -EINVAL;
                return -EINVAL;
        }
        }
        format_req = *tmp_format_req;
        format_req = *tmp_format_req;
        format_errors = 0;
        format_errors = 0;
        cont = &format_cont;
        cont = &format_cont;
        errors = &format_errors;
        errors = &format_errors;
        IWAIT(redo_format);
        IWAIT(redo_format);
        process_fd_request();
        process_fd_request();
        return ret;
        return ret;
}
}
 
 
/*
/*
 * Buffer read/write and support
 * Buffer read/write and support
 * =============================
 * =============================
 */
 */
 
 
/* new request_done. Can handle physical sectors which are smaller than a
/* new request_done. Can handle physical sectors which are smaller than a
 * logical buffer */
 * logical buffer */
static void request_done(int uptodate)
static void request_done(int uptodate)
{
{
        int block;
        int block;
 
 
        probing = 0;
        probing = 0;
        reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
        reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
 
 
        if (!CURRENT){
        if (!CURRENT){
                DPRINT("request list destroyed in floppy request done\n");
                DPRINT("request list destroyed in floppy request done\n");
                return;
                return;
        }
        }
 
 
        if (uptodate){
        if (uptodate){
                /* maintain values for invalidation on geometry
                /* maintain values for invalidation on geometry
                 * change */
                 * change */
                block = current_count_sectors + CURRENT->sector;
                block = current_count_sectors + CURRENT->sector;
                INFBOUND(DRS->maxblock, block);
                INFBOUND(DRS->maxblock, block);
                if (block > _floppy->sect)
                if (block > _floppy->sect)
                        DRS->maxtrack = 1;
                        DRS->maxtrack = 1;
 
 
                /* unlock chained buffers */
                /* unlock chained buffers */
                while (current_count_sectors && CURRENT &&
                while (current_count_sectors && CURRENT &&
                       current_count_sectors >= CURRENT->current_nr_sectors){
                       current_count_sectors >= CURRENT->current_nr_sectors){
                        current_count_sectors -= CURRENT->current_nr_sectors;
                        current_count_sectors -= CURRENT->current_nr_sectors;
                        CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
                        CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
                        CURRENT->sector += CURRENT->current_nr_sectors;
                        CURRENT->sector += CURRENT->current_nr_sectors;
                        end_request(1);
                        end_request(1);
                }
                }
                if (current_count_sectors && CURRENT){
                if (current_count_sectors && CURRENT){
                        /* "unlock" last subsector */
                        /* "unlock" last subsector */
                        CURRENT->buffer += current_count_sectors <<9;
                        CURRENT->buffer += current_count_sectors <<9;
                        CURRENT->current_nr_sectors -= current_count_sectors;
                        CURRENT->current_nr_sectors -= current_count_sectors;
                        CURRENT->nr_sectors -= current_count_sectors;
                        CURRENT->nr_sectors -= current_count_sectors;
                        CURRENT->sector += current_count_sectors;
                        CURRENT->sector += current_count_sectors;
                        return;
                        return;
                }
                }
 
 
                if (current_count_sectors && !CURRENT)
                if (current_count_sectors && !CURRENT)
                        DPRINT("request list destroyed in floppy request done\n");
                        DPRINT("request list destroyed in floppy request done\n");
 
 
        } else {
        } else {
                if (CURRENT->cmd == WRITE) {
                if (CURRENT->cmd == WRITE) {
                        /* record write error information */
                        /* record write error information */
                        DRWE->write_errors++;
                        DRWE->write_errors++;
                        if (DRWE->write_errors == 1) {
                        if (DRWE->write_errors == 1) {
                                DRWE->first_error_sector = CURRENT->sector;
                                DRWE->first_error_sector = CURRENT->sector;
                                DRWE->first_error_generation = DRS->generation;
                                DRWE->first_error_generation = DRS->generation;
                        }
                        }
                        DRWE->last_error_sector = CURRENT->sector;
                        DRWE->last_error_sector = CURRENT->sector;
                        DRWE->last_error_generation = DRS->generation;
                        DRWE->last_error_generation = DRS->generation;
                }
                }
                end_request(0);
                end_request(0);
        }
        }
}
}
 
 
/* Interrupt handler evaluating the result of the r/w operation */
/* Interrupt handler evaluating the result of the r/w operation */
static void rw_interrupt(void)
static void rw_interrupt(void)
{
{
        int nr_sectors, ssize, eoc;
        int nr_sectors, ssize, eoc;
 
 
        if (!DRS->first_read_date)
        if (!DRS->first_read_date)
                DRS->first_read_date = jiffies;
                DRS->first_read_date = jiffies;
 
 
        nr_sectors = 0;
        nr_sectors = 0;
        CODE2SIZE;
        CODE2SIZE;
 
 
        if(ST1 & ST1_EOC)
        if(ST1 & ST1_EOC)
                eoc = 1;
                eoc = 1;
        else
        else
                eoc = 0;
                eoc = 0;
        nr_sectors = ((R_TRACK-TRACK)*_floppy->head+R_HEAD-HEAD) *
        nr_sectors = ((R_TRACK-TRACK)*_floppy->head+R_HEAD-HEAD) *
                _floppy->sect + ((R_SECTOR-SECTOR+eoc) <<  SIZECODE >> 2) -
                _floppy->sect + ((R_SECTOR-SECTOR+eoc) <<  SIZECODE >> 2) -
                (sector_t % _floppy->sect) % ssize;
                (sector_t % _floppy->sect) % ssize;
 
 
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        if (nr_sectors > current_count_sectors + ssize -
        if (nr_sectors > current_count_sectors + ssize -
             (current_count_sectors + sector_t) % ssize +
             (current_count_sectors + sector_t) % ssize +
             sector_t % ssize){
             sector_t % ssize){
                DPRINT("long rw: %x instead of %lx\n",
                DPRINT("long rw: %x instead of %lx\n",
                        nr_sectors, current_count_sectors);
                        nr_sectors, current_count_sectors);
                printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
                printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
                printk("rh=%d h=%d\n", R_HEAD, HEAD);
                printk("rh=%d h=%d\n", R_HEAD, HEAD);
                printk("rt=%d t=%d\n", R_TRACK, TRACK);
                printk("rt=%d t=%d\n", R_TRACK, TRACK);
                printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
                printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
                       sector_t, ssize);
                       sector_t, ssize);
        }
        }
#endif
#endif
        INFBOUND(nr_sectors,0);
        INFBOUND(nr_sectors,0);
        SUPBOUND(current_count_sectors, nr_sectors);
        SUPBOUND(current_count_sectors, nr_sectors);
 
 
        switch (interpret_errors()){
        switch (interpret_errors()){
                case 2:
                case 2:
                        cont->redo();
                        cont->redo();
                        return;
                        return;
                case 1:
                case 1:
                        if (!current_count_sectors){
                        if (!current_count_sectors){
                                cont->error();
                                cont->error();
                                cont->redo();
                                cont->redo();
                                return;
                                return;
                        }
                        }
                        break;
                        break;
                case 0:
                case 0:
                        if (!current_count_sectors){
                        if (!current_count_sectors){
                                cont->redo();
                                cont->redo();
                                return;
                                return;
                        }
                        }
                        current_type[current_drive] = _floppy;
                        current_type[current_drive] = _floppy;
                        floppy_sizes[TOMINOR(current_drive) ]= _floppy->size>>1;
                        floppy_sizes[TOMINOR(current_drive) ]= _floppy->size>>1;
                        break;
                        break;
        }
        }
 
 
        if (probing) {
        if (probing) {
                if (DP->flags & FTD_MSG)
                if (DP->flags & FTD_MSG)
                        DPRINT("Auto-detected floppy type %s in fd%d\n",
                        DPRINT("Auto-detected floppy type %s in fd%d\n",
                                _floppy->name,current_drive);
                                _floppy->name,current_drive);
                current_type[current_drive] = _floppy;
                current_type[current_drive] = _floppy;
                floppy_sizes[TOMINOR(current_drive)] = _floppy->size >> 1;
                floppy_sizes[TOMINOR(current_drive)] = _floppy->size >> 1;
                probing = 0;
                probing = 0;
        }
        }
 
 
        if (CT(COMMAND) != FD_READ ||
        if (CT(COMMAND) != FD_READ ||
             raw_cmd->kernel_data == CURRENT->buffer){
             raw_cmd->kernel_data == CURRENT->buffer){
                /* transfer directly from buffer */
                /* transfer directly from buffer */
                cont->done(1);
                cont->done(1);
        } else if (CT(COMMAND) == FD_READ){
        } else if (CT(COMMAND) == FD_READ){
                buffer_track = raw_cmd->track;
                buffer_track = raw_cmd->track;
                buffer_drive = current_drive;
                buffer_drive = current_drive;
                INFBOUND(buffer_max, nr_sectors + sector_t);
                INFBOUND(buffer_max, nr_sectors + sector_t);
        }
        }
        cont->redo();
        cont->redo();
}
}
 
 
/* Compute maximal contiguous buffer size. */
/* Compute maximal contiguous buffer size. */
static int buffer_chain_size(void)
static int buffer_chain_size(void)
{
{
        struct buffer_head *bh;
        struct buffer_head *bh;
        int size;
        int size;
        char *base;
        char *base;
 
 
        base = CURRENT->buffer;
        base = CURRENT->buffer;
        size = CURRENT->current_nr_sectors << 9;
        size = CURRENT->current_nr_sectors << 9;
        bh = CURRENT->bh;
        bh = CURRENT->bh;
 
 
        if (bh){
        if (bh){
                bh = bh->b_reqnext;
                bh = bh->b_reqnext;
                while (bh && bh->b_data == base + size){
                while (bh && bh->b_data == base + size){
                        size += bh->b_size;
                        size += bh->b_size;
                        bh = bh->b_reqnext;
                        bh = bh->b_reqnext;
                }
                }
        }
        }
        return size >> 9;
        return size >> 9;
}
}
 
 
/* Compute the maximal transfer size */
/* Compute the maximal transfer size */
static int transfer_size(int ssize, int max_sector, int max_size)
static int transfer_size(int ssize, int max_sector, int max_size)
{
{
        SUPBOUND(max_sector, sector_t + max_size);
        SUPBOUND(max_sector, sector_t + max_size);
 
 
        /* alignment */
        /* alignment */
        max_sector -= (max_sector % _floppy->sect) % ssize;
        max_sector -= (max_sector % _floppy->sect) % ssize;
 
 
        /* transfer size, beginning not aligned */
        /* transfer size, beginning not aligned */
        current_count_sectors = max_sector - sector_t ;
        current_count_sectors = max_sector - sector_t ;
 
 
        return max_sector;
        return max_sector;
}
}
 
 
/*
/*
 * Move data from/to the track buffer to/from the buffer cache.
 * Move data from/to the track buffer to/from the buffer cache.
 */
 */
static void copy_buffer(int ssize, int max_sector, int max_sector_2)
static void copy_buffer(int ssize, int max_sector, int max_sector_2)
{
{
        int remaining; /* number of transferred 512-byte sectors */
        int remaining; /* number of transferred 512-byte sectors */
        struct buffer_head *bh;
        struct buffer_head *bh;
        char *buffer, *dma_buffer;
        char *buffer, *dma_buffer;
        int size;
        int size;
 
 
        max_sector = transfer_size(ssize,
        max_sector = transfer_size(ssize,
                                   minimum(max_sector, max_sector_2),
                                   minimum(max_sector, max_sector_2),
                                   CURRENT->nr_sectors);
                                   CURRENT->nr_sectors);
 
 
        if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
        if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
            buffer_max > sector_t + CURRENT->nr_sectors)
            buffer_max > sector_t + CURRENT->nr_sectors)
                current_count_sectors = minimum(buffer_max - sector_t,
                current_count_sectors = minimum(buffer_max - sector_t,
                                                CURRENT->nr_sectors);
                                                CURRENT->nr_sectors);
 
 
        remaining = current_count_sectors << 9;
        remaining = current_count_sectors << 9;
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        if ((remaining >> 9) > CURRENT->nr_sectors  &&
        if ((remaining >> 9) > CURRENT->nr_sectors  &&
            CT(COMMAND) == FD_WRITE){
            CT(COMMAND) == FD_WRITE){
                DPRINT("in copy buffer\n");
                DPRINT("in copy buffer\n");
                printk("current_count_sectors=%ld\n", current_count_sectors);
                printk("current_count_sectors=%ld\n", current_count_sectors);
                printk("remaining=%d\n", remaining >> 9);
                printk("remaining=%d\n", remaining >> 9);
                printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
                printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
                printk("CURRENT->current_nr_sectors=%ld\n",
                printk("CURRENT->current_nr_sectors=%ld\n",
                       CURRENT->current_nr_sectors);
                       CURRENT->current_nr_sectors);
                printk("max_sector=%d\n", max_sector);
                printk("max_sector=%d\n", max_sector);
                printk("ssize=%d\n", ssize);
                printk("ssize=%d\n", ssize);
        }
        }
#endif
#endif
 
 
        buffer_max = maximum(max_sector, buffer_max);
        buffer_max = maximum(max_sector, buffer_max);
 
 
        dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
        dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
 
 
        bh = CURRENT->bh;
        bh = CURRENT->bh;
        size = CURRENT->current_nr_sectors << 9;
        size = CURRENT->current_nr_sectors << 9;
        buffer = CURRENT->buffer;
        buffer = CURRENT->buffer;
 
 
        while (remaining > 0){
        while (remaining > 0){
                SUPBOUND(size, remaining);
                SUPBOUND(size, remaining);
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
                if (dma_buffer + size >
                if (dma_buffer + size >
                    floppy_track_buffer + (max_buffer_sectors << 10) ||
                    floppy_track_buffer + (max_buffer_sectors << 10) ||
                    dma_buffer < floppy_track_buffer){
                    dma_buffer < floppy_track_buffer){
                        DPRINT("buffer overrun in copy buffer %d\n",
                        DPRINT("buffer overrun in copy buffer %d\n",
                                (int) ((floppy_track_buffer - dma_buffer) >>9));
                                (int) ((floppy_track_buffer - dma_buffer) >>9));
                        printk("sector_t=%d buffer_min=%d\n",
                        printk("sector_t=%d buffer_min=%d\n",
                               sector_t, buffer_min);
                               sector_t, buffer_min);
                        printk("current_count_sectors=%ld\n",
                        printk("current_count_sectors=%ld\n",
                               current_count_sectors);
                               current_count_sectors);
                        if (CT(COMMAND) == FD_READ)
                        if (CT(COMMAND) == FD_READ)
                                printk("read\n");
                                printk("read\n");
                        if (CT(COMMAND) == FD_READ)
                        if (CT(COMMAND) == FD_READ)
                                printk("write\n");
                                printk("write\n");
                        break;
                        break;
                }
                }
                if (((unsigned long)buffer) % 512)
                if (((unsigned long)buffer) % 512)
                        DPRINT("%p buffer not aligned\n", buffer);
                        DPRINT("%p buffer not aligned\n", buffer);
#endif
#endif
                if (CT(COMMAND) == FD_READ)
                if (CT(COMMAND) == FD_READ)
                        memcpy(buffer, dma_buffer, size);
                        memcpy(buffer, dma_buffer, size);
                else
                else
                        memcpy(dma_buffer, buffer, size);
                        memcpy(dma_buffer, buffer, size);
                remaining -= size;
                remaining -= size;
                if (!remaining)
                if (!remaining)
                        break;
                        break;
 
 
                dma_buffer += size;
                dma_buffer += size;
                bh = bh->b_reqnext;
                bh = bh->b_reqnext;
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
                if (!bh){
                if (!bh){
                        DPRINT("bh=null in copy buffer after copy\n");
                        DPRINT("bh=null in copy buffer after copy\n");
                        break;
                        break;
                }
                }
#endif
#endif
                size = bh->b_size;
                size = bh->b_size;
                buffer = bh->b_data;
                buffer = bh->b_data;
        }
        }
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        if (remaining){
        if (remaining){
                if (remaining > 0)
                if (remaining > 0)
                        max_sector -= remaining >> 9;
                        max_sector -= remaining >> 9;
                DPRINT("weirdness: remaining %d\n", remaining>>9);
                DPRINT("weirdness: remaining %d\n", remaining>>9);
        }
        }
#endif
#endif
}
}
 
 
/*
/*
 * Formulate a read/write request.
 * Formulate a read/write request.
 * this routine decides where to load the data (directly to buffer, or to
 * this routine decides where to load the data (directly to buffer, or to
 * tmp floppy area), how much data to load (the size of the buffer, the whole
 * tmp floppy area), how much data to load (the size of the buffer, the whole
 * track, or a single sector)
 * track, or a single sector)
 * All floppy_track_buffer handling goes in here. If we ever add track buffer
 * All floppy_track_buffer handling goes in here. If we ever add track buffer
 * allocation on the fly, it should be done here. No other part should need
 * allocation on the fly, it should be done here. No other part should need
 * modification.
 * modification.
 */
 */
 
 
static int make_raw_rw_request(void)
static int make_raw_rw_request(void)
{
{
        int aligned_sector_t;
        int aligned_sector_t;
        int max_sector, max_size, tracksize, ssize;
        int max_sector, max_size, tracksize, ssize;
 
 
        set_fdc(DRIVE(CURRENT->rq_dev));
        set_fdc(DRIVE(CURRENT->rq_dev));
 
 
        raw_cmd = &default_raw_cmd;
        raw_cmd = &default_raw_cmd;
        raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
        raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
                FD_RAW_NEED_SEEK;
                FD_RAW_NEED_SEEK;
        raw_cmd->cmd_count = NR_RW;
        raw_cmd->cmd_count = NR_RW;
        if (CURRENT->cmd == READ){
        if (CURRENT->cmd == READ){
                raw_cmd->flags |= FD_RAW_READ;
                raw_cmd->flags |= FD_RAW_READ;
                COMMAND = FM_MODE(_floppy,FD_READ);
                COMMAND = FM_MODE(_floppy,FD_READ);
        } else if (CURRENT->cmd == WRITE){
        } else if (CURRENT->cmd == WRITE){
                raw_cmd->flags |= FD_RAW_WRITE;
                raw_cmd->flags |= FD_RAW_WRITE;
                COMMAND = FM_MODE(_floppy,FD_WRITE);
                COMMAND = FM_MODE(_floppy,FD_WRITE);
        } else {
        } else {
                DPRINT("make_raw_rw_request: unknown command\n");
                DPRINT("make_raw_rw_request: unknown command\n");
                return 0;
                return 0;
        }
        }
 
 
        max_sector = _floppy->sect * _floppy->head;
        max_sector = _floppy->sect * _floppy->head;
 
 
        TRACK = CURRENT->sector / max_sector;
        TRACK = CURRENT->sector / max_sector;
        sector_t = CURRENT->sector % max_sector;
        sector_t = CURRENT->sector % max_sector;
        if (_floppy->track && TRACK >= _floppy->track)
        if (_floppy->track && TRACK >= _floppy->track)
                return 0;
                return 0;
        HEAD = sector_t / _floppy->sect;
        HEAD = sector_t / _floppy->sect;
 
 
        if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
        if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
            sector_t < _floppy->sect)
            sector_t < _floppy->sect)
                max_sector = _floppy->sect;
                max_sector = _floppy->sect;
 
 
        /* 2M disks have phantom sectors on the first track */
        /* 2M disks have phantom sectors on the first track */
        if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
        if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
                max_sector = 2 * _floppy->sect / 3;
                max_sector = 2 * _floppy->sect / 3;
                if (sector_t >= max_sector){
                if (sector_t >= max_sector){
                        current_count_sectors = minimum(_floppy->sect - sector_t,
                        current_count_sectors = minimum(_floppy->sect - sector_t,
                                                        CURRENT->nr_sectors);
                                                        CURRENT->nr_sectors);
                        return 1;
                        return 1;
                }
                }
                SIZECODE = 2;
                SIZECODE = 2;
        } else
        } else
                SIZECODE = FD_SIZECODE(_floppy);
                SIZECODE = FD_SIZECODE(_floppy);
        raw_cmd->rate = _floppy->rate & 0x43;
        raw_cmd->rate = _floppy->rate & 0x43;
        if ((_floppy->rate & FD_2M) &&
        if ((_floppy->rate & FD_2M) &&
            (TRACK || HEAD) &&
            (TRACK || HEAD) &&
            raw_cmd->rate == 2)
            raw_cmd->rate == 2)
                raw_cmd->rate = 1;
                raw_cmd->rate = 1;
 
 
        if (SIZECODE)
        if (SIZECODE)
                SIZECODE2 = 0xff;
                SIZECODE2 = 0xff;
        else
        else
                SIZECODE2 = 0x80;
                SIZECODE2 = 0x80;
        raw_cmd->track = TRACK << STRETCH(_floppy);
        raw_cmd->track = TRACK << STRETCH(_floppy);
        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
        GAP = _floppy->gap;
        GAP = _floppy->gap;
        CODE2SIZE;
        CODE2SIZE;
        SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
        SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
        SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
        SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
        tracksize = _floppy->sect - _floppy->sect % ssize;
        tracksize = _floppy->sect - _floppy->sect % ssize;
        if (tracksize < _floppy->sect){
        if (tracksize < _floppy->sect){
                SECT_PER_TRACK ++;
                SECT_PER_TRACK ++;
                if (tracksize <= sector_t % _floppy->sect)
                if (tracksize <= sector_t % _floppy->sect)
                        SECTOR--;
                        SECTOR--;
                while (tracksize <= sector_t % _floppy->sect){
                while (tracksize <= sector_t % _floppy->sect){
                        while(tracksize + ssize > _floppy->sect){
                        while(tracksize + ssize > _floppy->sect){
                                SIZECODE--;
                                SIZECODE--;
                                ssize >>= 1;
                                ssize >>= 1;
                        }
                        }
                        SECTOR++; SECT_PER_TRACK ++;
                        SECTOR++; SECT_PER_TRACK ++;
                        tracksize += ssize;
                        tracksize += ssize;
                }
                }
                max_sector = HEAD * _floppy->sect + tracksize;
                max_sector = HEAD * _floppy->sect + tracksize;
        } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing)
        } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing)
                max_sector = _floppy->sect;
                max_sector = _floppy->sect;
 
 
        aligned_sector_t = sector_t - (sector_t % _floppy->sect) % ssize;
        aligned_sector_t = sector_t - (sector_t % _floppy->sect) % ssize;
        max_size = CURRENT->nr_sectors;
        max_size = CURRENT->nr_sectors;
        if ((raw_cmd->track == buffer_track) &&
        if ((raw_cmd->track == buffer_track) &&
            (current_drive == buffer_drive) &&
            (current_drive == buffer_drive) &&
            (sector_t >= buffer_min) && (sector_t < buffer_max)) {
            (sector_t >= buffer_min) && (sector_t < buffer_max)) {
                /* data already in track buffer */
                /* data already in track buffer */
                if (CT(COMMAND) == FD_READ) {
                if (CT(COMMAND) == FD_READ) {
                        copy_buffer(1, max_sector, buffer_max);
                        copy_buffer(1, max_sector, buffer_max);
                        return 1;
                        return 1;
                }
                }
        } else if (aligned_sector_t != sector_t || CURRENT->nr_sectors < ssize){
        } else if (aligned_sector_t != sector_t || CURRENT->nr_sectors < ssize){
                if (CT(COMMAND) == FD_WRITE){
                if (CT(COMMAND) == FD_WRITE){
                        if (sector_t + CURRENT->nr_sectors > ssize &&
                        if (sector_t + CURRENT->nr_sectors > ssize &&
                            sector_t + CURRENT->nr_sectors < ssize + ssize)
                            sector_t + CURRENT->nr_sectors < ssize + ssize)
                                max_size = ssize + ssize;
                                max_size = ssize + ssize;
                        else
                        else
                                max_size = ssize;
                                max_size = ssize;
                }
                }
                raw_cmd->flags &= ~FD_RAW_WRITE;
                raw_cmd->flags &= ~FD_RAW_WRITE;
                raw_cmd->flags |= FD_RAW_READ;
                raw_cmd->flags |= FD_RAW_READ;
                COMMAND = FM_MODE(_floppy,FD_READ);
                COMMAND = FM_MODE(_floppy,FD_READ);
        } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
        } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
                unsigned long dma_limit;
                unsigned long dma_limit;
                int direct, indirect;
                int direct, indirect;
 
 
                indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
                indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
                        sector_t;
                        sector_t;
 
 
                /*
                /*
                 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
                 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
                 * on a 64 bit machine!
                 * on a 64 bit machine!
                 */
                 */
                max_size = buffer_chain_size();
                max_size = buffer_chain_size();
#ifndef CONFIG_ARM
#ifndef CONFIG_ARM
                dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
                dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
                if ((unsigned long) max_size > dma_limit) {
                if ((unsigned long) max_size > dma_limit) {
                        max_size = dma_limit;
                        max_size = dma_limit;
                }
                }
                /* 64 kb boundaries */
                /* 64 kb boundaries */
                if (CROSS_64KB(CURRENT->buffer, max_size << 9))
                if (CROSS_64KB(CURRENT->buffer, max_size << 9))
                        max_size = (K_64 - ((long) CURRENT->buffer) % K_64)>>9;
                        max_size = (K_64 - ((long) CURRENT->buffer) % K_64)>>9;
#endif
#endif
                direct = transfer_size(ssize,max_sector,max_size) - sector_t;
                direct = transfer_size(ssize,max_sector,max_size) - sector_t;
                /*
                /*
                 * We try to read tracks, but if we get too many errors, we
                 * We try to read tracks, but if we get too many errors, we
                 * go back to reading just one sector at a time.
                 * go back to reading just one sector at a time.
                 *
                 *
                 * This means we should be able to read a sector even if there
                 * This means we should be able to read a sector even if there
                 * are other bad sectors on this track.
                 * are other bad sectors on this track.
                 */
                 */
                if (!direct ||
                if (!direct ||
                    (indirect * 2 > direct * 3 &&
                    (indirect * 2 > direct * 3 &&
                     *errors < DP->max_errors.read_track &&
                     *errors < DP->max_errors.read_track &&
                     /*!TESTF(FD_NEED_TWADDLE) &&*/
                     /*!TESTF(FD_NEED_TWADDLE) &&*/
                     ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
                     ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
                        max_size = CURRENT->nr_sectors;
                        max_size = CURRENT->nr_sectors;
                } else {
                } else {
                        raw_cmd->kernel_data = CURRENT->buffer;
                        raw_cmd->kernel_data = CURRENT->buffer;
                        raw_cmd->length = current_count_sectors << 9;
                        raw_cmd->length = current_count_sectors << 9;
                        if (raw_cmd->length == 0){
                        if (raw_cmd->length == 0){
                                DPRINT("zero dma transfer attempted from make_raw_request\n");
                                DPRINT("zero dma transfer attempted from make_raw_request\n");
                                DPRINT("indirect=%d direct=%d sector_t=%d",
                                DPRINT("indirect=%d direct=%d sector_t=%d",
                                        indirect, direct, sector_t);
                                        indirect, direct, sector_t);
                                return 0;
                                return 0;
                        }
                        }
                        return 2;
                        return 2;
                }
                }
        }
        }
 
 
        if (CT(COMMAND) == FD_READ)
        if (CT(COMMAND) == FD_READ)
                max_size = max_sector; /* unbounded */
                max_size = max_sector; /* unbounded */
 
 
        /* claim buffer track if needed */
        /* claim buffer track if needed */
        if (buffer_track != raw_cmd->track ||  /* bad track */
        if (buffer_track != raw_cmd->track ||  /* bad track */
            buffer_drive !=current_drive || /* bad drive */
            buffer_drive !=current_drive || /* bad drive */
            sector_t > buffer_max ||
            sector_t > buffer_max ||
            sector_t < buffer_min ||
            sector_t < buffer_min ||
            ((CT(COMMAND) == FD_READ ||
            ((CT(COMMAND) == FD_READ ||
              (aligned_sector_t == sector_t && CURRENT->nr_sectors >= ssize))&&
              (aligned_sector_t == sector_t && CURRENT->nr_sectors >= ssize))&&
             max_sector > 2 * max_buffer_sectors + buffer_min &&
             max_sector > 2 * max_buffer_sectors + buffer_min &&
             max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
             max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
            /* not enough space */){
            /* not enough space */){
                buffer_track = -1;
                buffer_track = -1;
                buffer_drive = current_drive;
                buffer_drive = current_drive;
                buffer_max = buffer_min = aligned_sector_t;
                buffer_max = buffer_min = aligned_sector_t;
        }
        }
        raw_cmd->kernel_data = floppy_track_buffer +
        raw_cmd->kernel_data = floppy_track_buffer +
                ((aligned_sector_t-buffer_min)<<9);
                ((aligned_sector_t-buffer_min)<<9);
 
 
        if (CT(COMMAND) == FD_WRITE){
        if (CT(COMMAND) == FD_WRITE){
                /* copy write buffer to track buffer.
                /* copy write buffer to track buffer.
                 * if we get here, we know that the write
                 * if we get here, we know that the write
                 * is either aligned or the data already in the buffer
                 * is either aligned or the data already in the buffer
                 * (buffer will be overwritten) */
                 * (buffer will be overwritten) */
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
                if (sector_t != aligned_sector_t && buffer_track == -1)
                if (sector_t != aligned_sector_t && buffer_track == -1)
                        DPRINT("internal error offset !=0 on write\n");
                        DPRINT("internal error offset !=0 on write\n");
#endif
#endif
                buffer_track = raw_cmd->track;
                buffer_track = raw_cmd->track;
                buffer_drive = current_drive;
                buffer_drive = current_drive;
                copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
                copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
        } else
        } else
                transfer_size(ssize, max_sector,
                transfer_size(ssize, max_sector,
                              2*max_buffer_sectors+buffer_min-aligned_sector_t);
                              2*max_buffer_sectors+buffer_min-aligned_sector_t);
 
 
        /* round up current_count_sectors to get dma xfer size */
        /* round up current_count_sectors to get dma xfer size */
        raw_cmd->length = sector_t+current_count_sectors-aligned_sector_t;
        raw_cmd->length = sector_t+current_count_sectors-aligned_sector_t;
        raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
        raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
        raw_cmd->length <<= 9;
        raw_cmd->length <<= 9;
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        if ((raw_cmd->length < current_count_sectors << 9) ||
        if ((raw_cmd->length < current_count_sectors << 9) ||
            (raw_cmd->kernel_data != CURRENT->buffer &&
            (raw_cmd->kernel_data != CURRENT->buffer &&
             CT(COMMAND) == FD_WRITE &&
             CT(COMMAND) == FD_WRITE &&
             (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
             (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
              aligned_sector_t < buffer_min)) ||
              aligned_sector_t < buffer_min)) ||
            raw_cmd->length % (128 << SIZECODE) ||
            raw_cmd->length % (128 << SIZECODE) ||
            raw_cmd->length <= 0 || current_count_sectors <= 0){
            raw_cmd->length <= 0 || current_count_sectors <= 0){
                DPRINT("fractionary current count b=%lx s=%lx\n",
                DPRINT("fractionary current count b=%lx s=%lx\n",
                        raw_cmd->length, current_count_sectors);
                        raw_cmd->length, current_count_sectors);
                if (raw_cmd->kernel_data != CURRENT->buffer)
                if (raw_cmd->kernel_data != CURRENT->buffer)
                        printk("addr=%d, length=%ld\n",
                        printk("addr=%d, length=%ld\n",
                               (int) ((raw_cmd->kernel_data -
                               (int) ((raw_cmd->kernel_data -
                                       floppy_track_buffer) >> 9),
                                       floppy_track_buffer) >> 9),
                               current_count_sectors);
                               current_count_sectors);
                printk("st=%d ast=%d mse=%d msi=%d\n",
                printk("st=%d ast=%d mse=%d msi=%d\n",
                       sector_t, aligned_sector_t, max_sector, max_size);
                       sector_t, aligned_sector_t, max_sector, max_size);
                printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
                printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
                printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
                printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
                       COMMAND, SECTOR, HEAD, TRACK);
                       COMMAND, SECTOR, HEAD, TRACK);
                printk("buffer drive=%d\n", buffer_drive);
                printk("buffer drive=%d\n", buffer_drive);
                printk("buffer track=%d\n", buffer_track);
                printk("buffer track=%d\n", buffer_track);
                printk("buffer_min=%d\n", buffer_min);
                printk("buffer_min=%d\n", buffer_min);
                printk("buffer_max=%d\n", buffer_max);
                printk("buffer_max=%d\n", buffer_max);
                return 0;
                return 0;
        }
        }
 
 
        if (raw_cmd->kernel_data != CURRENT->buffer){
        if (raw_cmd->kernel_data != CURRENT->buffer){
                if (raw_cmd->kernel_data < floppy_track_buffer ||
                if (raw_cmd->kernel_data < floppy_track_buffer ||
                    current_count_sectors < 0 ||
                    current_count_sectors < 0 ||
                    raw_cmd->length < 0 ||
                    raw_cmd->length < 0 ||
                    raw_cmd->kernel_data + raw_cmd->length >
                    raw_cmd->kernel_data + raw_cmd->length >
                    floppy_track_buffer + (max_buffer_sectors  << 10)){
                    floppy_track_buffer + (max_buffer_sectors  << 10)){
                        DPRINT("buffer overrun in schedule dma\n");
                        DPRINT("buffer overrun in schedule dma\n");
                        printk("sector_t=%d buffer_min=%d current_count=%ld\n",
                        printk("sector_t=%d buffer_min=%d current_count=%ld\n",
                               sector_t, buffer_min,
                               sector_t, buffer_min,
                               raw_cmd->length >> 9);
                               raw_cmd->length >> 9);
                        printk("current_count_sectors=%ld\n",
                        printk("current_count_sectors=%ld\n",
                               current_count_sectors);
                               current_count_sectors);
                        if (CT(COMMAND) == FD_READ)
                        if (CT(COMMAND) == FD_READ)
                                printk("read\n");
                                printk("read\n");
                        if (CT(COMMAND) == FD_READ)
                        if (CT(COMMAND) == FD_READ)
                                printk("write\n");
                                printk("write\n");
                        return 0;
                        return 0;
                }
                }
        } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
        } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
                   current_count_sectors > CURRENT->nr_sectors){
                   current_count_sectors > CURRENT->nr_sectors){
                DPRINT("buffer overrun in direct transfer\n");
                DPRINT("buffer overrun in direct transfer\n");
                return 0;
                return 0;
        } else if (raw_cmd->length < current_count_sectors << 9){
        } else if (raw_cmd->length < current_count_sectors << 9){
                DPRINT("more sectors than bytes\n");
                DPRINT("more sectors than bytes\n");
                printk("bytes=%ld\n", raw_cmd->length >> 9);
                printk("bytes=%ld\n", raw_cmd->length >> 9);
                printk("sectors=%ld\n", current_count_sectors);
                printk("sectors=%ld\n", current_count_sectors);
        }
        }
        if (raw_cmd->length == 0){
        if (raw_cmd->length == 0){
                DPRINT("zero dma transfer attempted from make_raw_request\n");
                DPRINT("zero dma transfer attempted from make_raw_request\n");
                return 0;
                return 0;
        }
        }
#endif
#endif
        return 2;
        return 2;
}
}
 
 
static void redo_fd_request(void)
static void redo_fd_request(void)
{
{
#define REPEAT {request_done(0); continue; }
#define REPEAT {request_done(0); continue; }
        kdev_t device;
        kdev_t device;
        int tmp;
        int tmp;
 
 
        lastredo = jiffies;
        lastredo = jiffies;
        if (current_drive < N_DRIVE)
        if (current_drive < N_DRIVE)
                floppy_off(current_drive);
                floppy_off(current_drive);
 
 
        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
                CLEAR_INTR;
                CLEAR_INTR;
                unlock_fdc();
                unlock_fdc();
                return;
                return;
        }
        }
 
 
        while(1){
        while(1){
                if (!CURRENT) {
                if (!CURRENT) {
                        CLEAR_INTR;
                        CLEAR_INTR;
                        unlock_fdc();
                        unlock_fdc();
                        return;
                        return;
                }
                }
                if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
                if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
                        panic(DEVICE_NAME ": request list destroyed");
                        panic(DEVICE_NAME ": request list destroyed");
                if (CURRENT->bh && !buffer_locked(CURRENT->bh))
                if (CURRENT->bh && !buffer_locked(CURRENT->bh))
                        panic(DEVICE_NAME ": block not locked");
                        panic(DEVICE_NAME ": block not locked");
 
 
                device = CURRENT->rq_dev;
                device = CURRENT->rq_dev;
                set_fdc(DRIVE(device));
                set_fdc(DRIVE(device));
                reschedule_timeout(CURRENTD, "redo fd request", 0);
                reschedule_timeout(CURRENTD, "redo fd request", 0);
 
 
                set_floppy(device);
                set_floppy(device);
                raw_cmd = & default_raw_cmd;
                raw_cmd = & default_raw_cmd;
                raw_cmd->flags = 0;
                raw_cmd->flags = 0;
                if (start_motor(redo_fd_request)) return;
                if (start_motor(redo_fd_request)) return;
                disk_change(current_drive);
                disk_change(current_drive);
                if (test_bit(current_drive, &fake_change) ||
                if (test_bit(current_drive, &fake_change) ||
                   TESTF(FD_DISK_CHANGED)){
                   TESTF(FD_DISK_CHANGED)){
                        DPRINT("disk absent or changed during operation\n");
                        DPRINT("disk absent or changed during operation\n");
                        REPEAT;
                        REPEAT;
                }
                }
                if (!_floppy) { /* Autodetection */
                if (!_floppy) { /* Autodetection */
                        if (!probing){
                        if (!probing){
                                DRS->probed_format = 0;
                                DRS->probed_format = 0;
                                if (next_valid_format()){
                                if (next_valid_format()){
                                        DPRINT("no autodetectable formats\n");
                                        DPRINT("no autodetectable formats\n");
                                        _floppy = NULL;
                                        _floppy = NULL;
                                        REPEAT;
                                        REPEAT;
                                }
                                }
                        }
                        }
                        probing = 1;
                        probing = 1;
                        _floppy = floppy_type+DP->autodetect[DRS->probed_format];
                        _floppy = floppy_type+DP->autodetect[DRS->probed_format];
                } else
                } else
                        probing = 0;
                        probing = 0;
                errors = & (CURRENT->errors);
                errors = & (CURRENT->errors);
                tmp = make_raw_rw_request();
                tmp = make_raw_rw_request();
                if (tmp < 2){
                if (tmp < 2){
                        request_done(tmp);
                        request_done(tmp);
                        continue;
                        continue;
                }
                }
 
 
                if (TESTF(FD_NEED_TWADDLE))
                if (TESTF(FD_NEED_TWADDLE))
                        twaddle();
                        twaddle();
                floppy_tq.routine = (void *)(void *) floppy_start;
                floppy_tq.routine = (void *)(void *) floppy_start;
                queue_task(&floppy_tq, &tq_immediate);
                queue_task(&floppy_tq, &tq_immediate);
                mark_bh(IMMEDIATE_BH);
                mark_bh(IMMEDIATE_BH);
#ifdef DEBUGT
#ifdef DEBUGT
                debugt("queue fd request");
                debugt("queue fd request");
#endif
#endif
                return;
                return;
        }
        }
#undef REPEAT
#undef REPEAT
}
}
 
 
static struct cont_t rw_cont={
static struct cont_t rw_cont={
        rw_interrupt,
        rw_interrupt,
        redo_fd_request,
        redo_fd_request,
        bad_flp_intr,
        bad_flp_intr,
        request_done };
        request_done };
 
 
static struct tq_struct request_tq =
static struct tq_struct request_tq =
{ 0, 0, (void *) (void *) redo_fd_request, 0 };
{ 0, 0, (void *) (void *) redo_fd_request, 0 };
 
 
static void process_fd_request(void)
static void process_fd_request(void)
{
{
        cont = &rw_cont;
        cont = &rw_cont;
        queue_task(&request_tq, &tq_immediate);
        queue_task(&request_tq, &tq_immediate);
        mark_bh(IMMEDIATE_BH);
        mark_bh(IMMEDIATE_BH);
}
}
 
 
static void do_fd_request(void)
static void do_fd_request(void)
{
{
        sti();
        sti();
        if (fdc_busy){
        if (fdc_busy){
                /* fdc busy, this new request will be treated when the
                /* fdc busy, this new request will be treated when the
                   current one is done */
                   current one is done */
                is_alive("do fd request, old request running");
                is_alive("do fd request, old request running");
                return;
                return;
        }
        }
        lock_fdc(MAXTIMEOUT,0);
        lock_fdc(MAXTIMEOUT,0);
        process_fd_request();
        process_fd_request();
        is_alive("do fd request");
        is_alive("do fd request");
}
}
 
 
static struct cont_t poll_cont={
static struct cont_t poll_cont={
        success_and_wakeup,
        success_and_wakeup,
        floppy_ready,
        floppy_ready,
        generic_failure,
        generic_failure,
        generic_done };
        generic_done };
 
 
static int poll_drive(int interruptible, int flag)
static int poll_drive(int interruptible, int flag)
{
{
        int ret;
        int ret;
        /* no auto-sense, just clear dcl */
        /* no auto-sense, just clear dcl */
        raw_cmd = &default_raw_cmd;
        raw_cmd = &default_raw_cmd;
        raw_cmd->flags= flag;
        raw_cmd->flags= flag;
        raw_cmd->track=0;
        raw_cmd->track=0;
        raw_cmd->cmd_count=0;
        raw_cmd->cmd_count=0;
        cont = &poll_cont;
        cont = &poll_cont;
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (DP->flags & FD_DEBUG){
        if (DP->flags & FD_DEBUG){
                DPRINT("setting NEWCHANGE in poll_drive\n");
                DPRINT("setting NEWCHANGE in poll_drive\n");
        }
        }
#endif
#endif
        SETF(FD_DISK_NEWCHANGE);
        SETF(FD_DISK_NEWCHANGE);
        WAIT(floppy_ready);
        WAIT(floppy_ready);
        return ret;
        return ret;
}
}
 
 
/*
/*
 * User triggered reset
 * User triggered reset
 * ====================
 * ====================
 */
 */
 
 
static void reset_intr(void)
static void reset_intr(void)
{
{
        printk("weird, reset interrupt called\n");
        printk("weird, reset interrupt called\n");
}
}
 
 
static struct cont_t reset_cont={
static struct cont_t reset_cont={
        reset_intr,
        reset_intr,
        success_and_wakeup,
        success_and_wakeup,
        generic_failure,
        generic_failure,
        generic_done };
        generic_done };
 
 
static int user_reset_fdc(int drive, int arg, int interruptible)
static int user_reset_fdc(int drive, int arg, int interruptible)
{
{
        int ret;
        int ret;
 
 
        ret=0;
        ret=0;
        LOCK_FDC(drive,interruptible);
        LOCK_FDC(drive,interruptible);
        if (arg == FD_RESET_ALWAYS)
        if (arg == FD_RESET_ALWAYS)
                FDCS->reset=1;
                FDCS->reset=1;
        if (FDCS->reset){
        if (FDCS->reset){
                cont = &reset_cont;
                cont = &reset_cont;
                WAIT(reset_fdc);
                WAIT(reset_fdc);
        }
        }
        process_fd_request();
        process_fd_request();
        return ret;
        return ret;
}
}
 
 
/*
/*
 * Misc Ioctl's and support
 * Misc Ioctl's and support
 * ========================
 * ========================
 */
 */
static int fd_copyout(void *param, const void *address, int size)
static int fd_copyout(void *param, const void *address, int size)
{
{
        int ret;
        int ret;
 
 
        ECALL(verify_area(VERIFY_WRITE,param,size));
        ECALL(verify_area(VERIFY_WRITE,param,size));
        memcpy_tofs(param,(void *) address, size);
        memcpy_tofs(param,(void *) address, size);
        return 0;
        return 0;
}
}
 
 
static int fd_copyin(void *param, void *address, int size)
static int fd_copyin(void *param, void *address, int size)
{
{
        int ret;
        int ret;
 
 
        ECALL(verify_area(VERIFY_READ,param,size));
        ECALL(verify_area(VERIFY_READ,param,size));
        memcpy_fromfs((void *) address, param, size);
        memcpy_fromfs((void *) address, param, size);
        return 0;
        return 0;
}
}
 
 
#define COPYOUT(x) ECALL(fd_copyout((void *)param, &(x), sizeof(x)))
#define COPYOUT(x) ECALL(fd_copyout((void *)param, &(x), sizeof(x)))
#define COPYIN(x) ECALL(fd_copyin((void *)param, &(x), sizeof(x)))
#define COPYIN(x) ECALL(fd_copyin((void *)param, &(x), sizeof(x)))
 
 
static inline const char *drive_name(int type, int drive)
static inline const char *drive_name(int type, int drive)
{
{
        struct floppy_struct *floppy;
        struct floppy_struct *floppy;
 
 
        if (type)
        if (type)
                floppy = floppy_type + type;
                floppy = floppy_type + type;
        else {
        else {
                if (UDP->native_format)
                if (UDP->native_format)
                        floppy = floppy_type + UDP->native_format;
                        floppy = floppy_type + UDP->native_format;
                else
                else
                        return "(null)";
                        return "(null)";
        }
        }
        if (floppy->name)
        if (floppy->name)
                return floppy->name;
                return floppy->name;
        else
        else
                return "(null)";
                return "(null)";
}
}
 
 
 
 
/* raw commands */
/* raw commands */
static void raw_cmd_done(int flag)
static void raw_cmd_done(int flag)
{
{
        int i;
        int i;
 
 
        if (!flag) {
        if (!flag) {
                raw_cmd->flags |= FD_RAW_FAILURE;
                raw_cmd->flags |= FD_RAW_FAILURE;
                raw_cmd->flags |= FD_RAW_HARDFAILURE;
                raw_cmd->flags |= FD_RAW_HARDFAILURE;
        } else {
        } else {
                raw_cmd->reply_count = inr;
                raw_cmd->reply_count = inr;
                for (i=0; i< raw_cmd->reply_count; i++)
                for (i=0; i< raw_cmd->reply_count; i++)
                        raw_cmd->reply[i] = reply_buffer[i];
                        raw_cmd->reply[i] = reply_buffer[i];
 
 
                if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
                if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
                        raw_cmd->length = fd_get_dma_residue();
                        raw_cmd->length = fd_get_dma_residue();
 
 
                if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
                if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
                    (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
                    (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
                        raw_cmd->flags |= FD_RAW_FAILURE;
                        raw_cmd->flags |= FD_RAW_FAILURE;
 
 
                if (disk_change(current_drive))
                if (disk_change(current_drive))
                        raw_cmd->flags |= FD_RAW_DISK_CHANGE;
                        raw_cmd->flags |= FD_RAW_DISK_CHANGE;
                else
                else
                        raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
                        raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
                if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
                if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
                        motor_off_callback(current_drive);
                        motor_off_callback(current_drive);
 
 
                if (raw_cmd->next &&
                if (raw_cmd->next &&
                   (!(raw_cmd->flags & FD_RAW_FAILURE) ||
                   (!(raw_cmd->flags & FD_RAW_FAILURE) ||
                    !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
                    !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
                   ((raw_cmd->flags & FD_RAW_FAILURE) ||
                   ((raw_cmd->flags & FD_RAW_FAILURE) ||
                    !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
                    !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
                        raw_cmd = raw_cmd->next;
                        raw_cmd = raw_cmd->next;
                        return;
                        return;
                }
                }
        }
        }
        generic_done(flag);
        generic_done(flag);
}
}
 
 
 
 
static struct cont_t raw_cmd_cont={
static struct cont_t raw_cmd_cont={
        success_and_wakeup,
        success_and_wakeup,
        floppy_start,
        floppy_start,
        generic_failure,
        generic_failure,
        raw_cmd_done
        raw_cmd_done
};
};
 
 
static inline int raw_cmd_copyout(int cmd, char *param,
static inline int raw_cmd_copyout(int cmd, char *param,
                                  struct floppy_raw_cmd *ptr)
                                  struct floppy_raw_cmd *ptr)
{
{
        struct old_floppy_raw_cmd old_raw_cmd;
        struct old_floppy_raw_cmd old_raw_cmd;
        int ret;
        int ret;
 
 
        while(ptr) {
        while(ptr) {
                if (cmd == OLDFDRAWCMD) {
                if (cmd == OLDFDRAWCMD) {
                        old_raw_cmd.flags = ptr->flags;
                        old_raw_cmd.flags = ptr->flags;
                        old_raw_cmd.data = ptr->data;
                        old_raw_cmd.data = ptr->data;
                        old_raw_cmd.length = ptr->length;
                        old_raw_cmd.length = ptr->length;
                        old_raw_cmd.rate = ptr->rate;
                        old_raw_cmd.rate = ptr->rate;
                        old_raw_cmd.reply_count = ptr->reply_count;
                        old_raw_cmd.reply_count = ptr->reply_count;
                        memcpy(old_raw_cmd.reply, ptr->reply, 7);
                        memcpy(old_raw_cmd.reply, ptr->reply, 7);
                        COPYOUT(old_raw_cmd);
                        COPYOUT(old_raw_cmd);
                        param += sizeof(old_raw_cmd);
                        param += sizeof(old_raw_cmd);
                } else {
                } else {
                        COPYOUT(*ptr);
                        COPYOUT(*ptr);
                        param += sizeof(struct floppy_raw_cmd);
                        param += sizeof(struct floppy_raw_cmd);
                }
                }
 
 
                if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
                if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
                        if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
                        if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
                                ECALL(fd_copyout(ptr->data,
                                ECALL(fd_copyout(ptr->data,
                                                 ptr->kernel_data,
                                                 ptr->kernel_data,
                                                 ptr->buffer_length -
                                                 ptr->buffer_length -
                                                 ptr->length));
                                                 ptr->length));
                }
                }
                ptr = ptr->next;
                ptr = ptr->next;
        }
        }
        return 0;
        return 0;
}
}
 
 
 
 
static void raw_cmd_free(struct floppy_raw_cmd **ptr)
static void raw_cmd_free(struct floppy_raw_cmd **ptr)
{
{
        struct floppy_raw_cmd *next,*this;
        struct floppy_raw_cmd *next,*this;
 
 
        this = *ptr;
        this = *ptr;
        *ptr = 0;
        *ptr = 0;
        while(this) {
        while(this) {
                if (this->buffer_length) {
                if (this->buffer_length) {
                        fd_dma_mem_free((unsigned long)this->kernel_data,
                        fd_dma_mem_free((unsigned long)this->kernel_data,
                                        this->buffer_length);
                                        this->buffer_length);
                        this->buffer_length = 0;
                        this->buffer_length = 0;
                }
                }
                next = this->next;
                next = this->next;
                kfree(this);
                kfree(this);
                this = next;
                this = next;
        }
        }
}
}
 
 
 
 
static inline int raw_cmd_copyin(int cmd, char *param,
static inline int raw_cmd_copyin(int cmd, char *param,
                                 struct floppy_raw_cmd **rcmd)
                                 struct floppy_raw_cmd **rcmd)
{
{
        struct floppy_raw_cmd *ptr;
        struct floppy_raw_cmd *ptr;
        struct old_floppy_raw_cmd old_raw_cmd;
        struct old_floppy_raw_cmd old_raw_cmd;
        int ret;
        int ret;
        int i;
        int i;
 
 
        *rcmd = 0;
        *rcmd = 0;
        while(1) {
        while(1) {
                ptr = (struct floppy_raw_cmd *)
                ptr = (struct floppy_raw_cmd *)
                        kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
                        kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
                if (!ptr)
                if (!ptr)
                        return -ENOMEM;
                        return -ENOMEM;
                *rcmd = ptr;
                *rcmd = ptr;
                if (cmd == OLDFDRAWCMD){
                if (cmd == OLDFDRAWCMD){
                        COPYIN(old_raw_cmd);
                        COPYIN(old_raw_cmd);
                        ptr->flags = old_raw_cmd.flags;
                        ptr->flags = old_raw_cmd.flags;
                        ptr->data = old_raw_cmd.data;
                        ptr->data = old_raw_cmd.data;
                        ptr->length = old_raw_cmd.length;
                        ptr->length = old_raw_cmd.length;
                        ptr->rate = old_raw_cmd.rate;
                        ptr->rate = old_raw_cmd.rate;
                        ptr->cmd_count = old_raw_cmd.cmd_count;
                        ptr->cmd_count = old_raw_cmd.cmd_count;
                        ptr->track = old_raw_cmd.track;
                        ptr->track = old_raw_cmd.track;
                        ptr->phys_length = 0;
                        ptr->phys_length = 0;
                        ptr->next = 0;
                        ptr->next = 0;
                        ptr->buffer_length = 0;
                        ptr->buffer_length = 0;
                        memcpy(ptr->cmd, old_raw_cmd.cmd, 9);
                        memcpy(ptr->cmd, old_raw_cmd.cmd, 9);
                        param += sizeof(struct old_floppy_raw_cmd);
                        param += sizeof(struct old_floppy_raw_cmd);
                        if (ptr->cmd_count > 9)
                        if (ptr->cmd_count > 9)
                                return -EINVAL;
                                return -EINVAL;
                } else {
                } else {
                        COPYIN(*ptr);
                        COPYIN(*ptr);
                        ptr->next = 0;
                        ptr->next = 0;
                        ptr->buffer_length = 0;
                        ptr->buffer_length = 0;
                        param += sizeof(struct floppy_raw_cmd);
                        param += sizeof(struct floppy_raw_cmd);
                        if (ptr->cmd_count > 33)
                        if (ptr->cmd_count > 33)
                                /* the command may now also take up the space
                                /* the command may now also take up the space
                                 * initially intended for the reply & the
                                 * initially intended for the reply & the
                                 * reply count. Needed for long 82078 commands
                                 * reply count. Needed for long 82078 commands
                                 * such as RESTORE, which takes ... 17 command
                                 * such as RESTORE, which takes ... 17 command
                                 * bytes. Murphy's law #137: When you reserve
                                 * bytes. Murphy's law #137: When you reserve
                                 * 16 bytes for a structure, you'll one day
                                 * 16 bytes for a structure, you'll one day
                                 * discover that you really need 17...
                                 * discover that you really need 17...
                                 */
                                 */
                                return -EINVAL;
                                return -EINVAL;
                }
                }
 
 
                for (i=0; i< 16; i++)
                for (i=0; i< 16; i++)
                        ptr->reply[i] = 0;
                        ptr->reply[i] = 0;
                ptr->resultcode = 0;
                ptr->resultcode = 0;
                ptr->kernel_data = 0;
                ptr->kernel_data = 0;
 
 
                if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
                if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
                        if (ptr->length <= 0)
                        if (ptr->length <= 0)
                                return -EINVAL;
                                return -EINVAL;
                        ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
                        ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
                        if (!ptr->kernel_data)
                        if (!ptr->kernel_data)
                                return -ENOMEM;
                                return -ENOMEM;
                        ptr->buffer_length = ptr->length;
                        ptr->buffer_length = ptr->length;
                }
                }
                if ( ptr->flags & FD_RAW_READ )
                if ( ptr->flags & FD_RAW_READ )
                    ECALL( verify_area( VERIFY_WRITE, ptr->data,
                    ECALL( verify_area( VERIFY_WRITE, ptr->data,
                                        ptr->length ));
                                        ptr->length ));
                if (ptr->flags & FD_RAW_WRITE)
                if (ptr->flags & FD_RAW_WRITE)
                        ECALL(fd_copyin(ptr->data, ptr->kernel_data,
                        ECALL(fd_copyin(ptr->data, ptr->kernel_data,
                                        ptr->length));
                                        ptr->length));
                rcmd = & (ptr->next);
                rcmd = & (ptr->next);
                if (!(ptr->flags & FD_RAW_MORE))
                if (!(ptr->flags & FD_RAW_MORE))
                        return 0;
                        return 0;
                ptr->rate &= 0x43;
                ptr->rate &= 0x43;
        }
        }
}
}
 
 
 
 
static int raw_cmd_ioctl(int cmd, void *param)
static int raw_cmd_ioctl(int cmd, void *param)
{
{
        int drive, ret, ret2;
        int drive, ret, ret2;
        struct floppy_raw_cmd *my_raw_cmd;
        struct floppy_raw_cmd *my_raw_cmd;
 
 
        if (FDCS->rawcmd <= 1)
        if (FDCS->rawcmd <= 1)
                FDCS->rawcmd = 1;
                FDCS->rawcmd = 1;
        for (drive= 0; drive < N_DRIVE; drive++){
        for (drive= 0; drive < N_DRIVE; drive++){
                if (FDC(drive) != fdc)
                if (FDC(drive) != fdc)
                        continue;
                        continue;
                if (drive == current_drive){
                if (drive == current_drive){
                        if (UDRS->fd_ref > 1){
                        if (UDRS->fd_ref > 1){
                                FDCS->rawcmd = 2;
                                FDCS->rawcmd = 2;
                                break;
                                break;
                        }
                        }
                } else if (UDRS->fd_ref){
                } else if (UDRS->fd_ref){
                        FDCS->rawcmd = 2;
                        FDCS->rawcmd = 2;
                        break;
                        break;
                }
                }
        }
        }
 
 
        if (FDCS->reset)
        if (FDCS->reset)
                return -EIO;
                return -EIO;
 
 
        ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
        ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
        if (ret) {
        if (ret) {
                raw_cmd_free(&my_raw_cmd);
                raw_cmd_free(&my_raw_cmd);
                return ret;
                return ret;
        }
        }
 
 
        raw_cmd = my_raw_cmd;
        raw_cmd = my_raw_cmd;
        cont = &raw_cmd_cont;
        cont = &raw_cmd_cont;
        ret=wait_til_done(floppy_start,1);
        ret=wait_til_done(floppy_start,1);
#ifdef DCL_DEBUG
#ifdef DCL_DEBUG
        if (DP->flags & FD_DEBUG){
        if (DP->flags & FD_DEBUG){
                DPRINT("calling disk change from raw_cmd ioctl\n");
                DPRINT("calling disk change from raw_cmd ioctl\n");
        }
        }
#endif
#endif
 
 
        if (ret != -EINTR && FDCS->reset)
        if (ret != -EINTR && FDCS->reset)
                ret = -EIO;
                ret = -EIO;
 
 
        DRS->track = NO_TRACK;
        DRS->track = NO_TRACK;
 
 
        ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
        ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
        if (!ret)
        if (!ret)
                ret = ret2;
                ret = ret2;
        raw_cmd_free(&my_raw_cmd);
        raw_cmd_free(&my_raw_cmd);
        return ret;
        return ret;
}
}
 
 
static int invalidate_drive(kdev_t rdev)
static int invalidate_drive(kdev_t rdev)
{
{
        /* invalidate the buffer track to force a reread */
        /* invalidate the buffer track to force a reread */
        set_bit(DRIVE(rdev), &fake_change);
        set_bit(DRIVE(rdev), &fake_change);
        process_fd_request();
        process_fd_request();
        check_disk_change(rdev);
        check_disk_change(rdev);
        return 0;
        return 0;
}
}
 
 
 
 
static inline void clear_write_error(int drive)
static inline void clear_write_error(int drive)
{
{
        CLEARSTRUCT(UDRWE);
        CLEARSTRUCT(UDRWE);
}
}
 
 
static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
                               int drive, int type, kdev_t device)
                               int drive, int type, kdev_t device)
{
{
        int cnt;
        int cnt;
 
 
        /* sanity checking for parameters.*/
        /* sanity checking for parameters.*/
        if (g->sect <= 0 ||
        if (g->sect <= 0 ||
            g->head <= 0 ||
            g->head <= 0 ||
            g->track <= 0 ||
            g->track <= 0 ||
            g->track > UDP->tracks>>STRETCH(g) ||
            g->track > UDP->tracks>>STRETCH(g) ||
            /* check if reserved bits are set */
            /* check if reserved bits are set */
            (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
            (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
                return -EINVAL;
                return -EINVAL;
        if (type){
        if (type){
                if (!suser())
                if (!suser())
                        return -EPERM;
                        return -EPERM;
                LOCK_FDC(drive,1);
                LOCK_FDC(drive,1);
                for (cnt = 0; cnt < N_DRIVE; cnt++){
                for (cnt = 0; cnt < N_DRIVE; cnt++){
                        if (ITYPE(drive_state[cnt].fd_device) == type &&
                        if (ITYPE(drive_state[cnt].fd_device) == type &&
                            drive_state[cnt].fd_ref)
                            drive_state[cnt].fd_ref)
                                set_bit(drive, &fake_change);
                                set_bit(drive, &fake_change);
                }
                }
                floppy_type[type] = *g;
                floppy_type[type] = *g;
                floppy_type[type].name="user format";
                floppy_type[type].name="user format";
                for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
                for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
                        floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
                        floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
                                floppy_type[type].size>>1;
                                floppy_type[type].size>>1;
                process_fd_request();
                process_fd_request();
                for (cnt = 0; cnt < N_DRIVE; cnt++){
                for (cnt = 0; cnt < N_DRIVE; cnt++){
                        if (ITYPE(drive_state[cnt].fd_device) == type &&
                        if (ITYPE(drive_state[cnt].fd_device) == type &&
                            drive_state[cnt].fd_ref)
                            drive_state[cnt].fd_ref)
                                check_disk_change(
                                check_disk_change(
                                        MKDEV(FLOPPY_MAJOR,
                                        MKDEV(FLOPPY_MAJOR,
                                              drive_state[cnt].fd_device));
                                              drive_state[cnt].fd_device));
                }
                }
        } else {
        } else {
                LOCK_FDC(drive,1);
                LOCK_FDC(drive,1);
                if (cmd != FDDEFPRM)
                if (cmd != FDDEFPRM)
                        /* notice a disk change immediately, else
                        /* notice a disk change immediately, else
                         * we loose our settings immediately*/
                         * we loose our settings immediately*/
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                user_params[drive] = *g;
                user_params[drive] = *g;
                if (buffer_drive == drive)
                if (buffer_drive == drive)
                        SUPBOUND(buffer_max, user_params[drive].sect);
                        SUPBOUND(buffer_max, user_params[drive].sect);
                current_type[drive] = &user_params[drive];
                current_type[drive] = &user_params[drive];
                floppy_sizes[drive] = user_params[drive].size >> 1;
                floppy_sizes[drive] = user_params[drive].size >> 1;
                if (cmd == FDDEFPRM)
                if (cmd == FDDEFPRM)
                        DRS->keep_data = -1;
                        DRS->keep_data = -1;
                else
                else
                        DRS->keep_data = 1;
                        DRS->keep_data = 1;
                /* invalidation. Invalidate only when needed, i.e.
                /* invalidation. Invalidate only when needed, i.e.
                 * when there are already sectors in the buffer cache
                 * when there are already sectors in the buffer cache
                 * whose number will change. This is useful, because
                 * whose number will change. This is useful, because
                 * mtools often changes the geometry of the disk after
                 * mtools often changes the geometry of the disk after
                 * looking at the boot block */
                 * looking at the boot block */
                if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
                if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
                        invalidate_drive(device);
                        invalidate_drive(device);
                else
                else
                        process_fd_request();
                        process_fd_request();
        }
        }
        return 0;
        return 0;
}
}
 
 
/* handle obsolete ioctl's */
/* handle obsolete ioctl's */
static struct translation_entry {
static struct translation_entry {
    int newcmd;
    int newcmd;
    int oldcmd;
    int oldcmd;
    int oldsize; /* size of 0x00xx-style ioctl. Reflects old structures, thus
    int oldsize; /* size of 0x00xx-style ioctl. Reflects old structures, thus
                  * use numeric values. NO SIZEOFS */
                  * use numeric values. NO SIZEOFS */
} translation_table[]= {
} translation_table[]= {
    {FDCLRPRM,           0,  0},
    {FDCLRPRM,           0,  0},
    {FDSETPRM,           1, 28},
    {FDSETPRM,           1, 28},
    {FDDEFPRM,           2, 28},
    {FDDEFPRM,           2, 28},
    {FDGETPRM,           3, 28},
    {FDGETPRM,           3, 28},
    {FDMSGON,            4,  0},
    {FDMSGON,            4,  0},
    {FDMSGOFF,           5,  0},
    {FDMSGOFF,           5,  0},
    {FDFMTBEG,           6,  0},
    {FDFMTBEG,           6,  0},
    {FDFMTTRK,           7, 12},
    {FDFMTTRK,           7, 12},
    {FDFMTEND,           8,  0},
    {FDFMTEND,           8,  0},
    {FDSETEMSGTRESH,    10,  0},
    {FDSETEMSGTRESH,    10,  0},
    {FDFLUSH,           11,  0},
    {FDFLUSH,           11,  0},
    {FDSETMAXERRS,      12, 20},
    {FDSETMAXERRS,      12, 20},
    {OLDFDRAWCMD,       30,  0},
    {OLDFDRAWCMD,       30,  0},
    {FDGETMAXERRS,      14, 20},
    {FDGETMAXERRS,      14, 20},
    {FDGETDRVTYP,       16, 16},
    {FDGETDRVTYP,       16, 16},
    {FDSETDRVPRM,       20, 88},
    {FDSETDRVPRM,       20, 88},
    {FDGETDRVPRM,       21, 88},
    {FDGETDRVPRM,       21, 88},
    {FDGETDRVSTAT,      22, 52},
    {FDGETDRVSTAT,      22, 52},
    {FDPOLLDRVSTAT,     23, 52},
    {FDPOLLDRVSTAT,     23, 52},
    {FDRESET,           24,  0},
    {FDRESET,           24,  0},
    {FDGETFDCSTAT,      25, 40},
    {FDGETFDCSTAT,      25, 40},
    {FDWERRORCLR,       27,  0},
    {FDWERRORCLR,       27,  0},
    {FDWERRORGET,       28, 24},
    {FDWERRORGET,       28, 24},
    {FDRAWCMD,           0,  0},
    {FDRAWCMD,           0,  0},
    {FDEJECT,            0,  0},
    {FDEJECT,            0,  0},
    {FDTWADDLE,         40,  0} };
    {FDTWADDLE,         40,  0} };
 
 
static inline int normalize_0x02xx_ioctl(int *cmd, int *size)
static inline int normalize_0x02xx_ioctl(int *cmd, int *size)
{
{
        int i;
        int i;
 
 
        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
                if ((*cmd & 0xffff) == (translation_table[i].newcmd & 0xffff)){
                if ((*cmd & 0xffff) == (translation_table[i].newcmd & 0xffff)){
                        *size = _IOC_SIZE(*cmd);
                        *size = _IOC_SIZE(*cmd);
                        *cmd = translation_table[i].newcmd;
                        *cmd = translation_table[i].newcmd;
                        if (*size > _IOC_SIZE(*cmd)) {
                        if (*size > _IOC_SIZE(*cmd)) {
                                printk("ioctl not yet supported\n");
                                printk("ioctl not yet supported\n");
                                return -EFAULT;
                                return -EFAULT;
                        }
                        }
                        return 0;
                        return 0;
                }
                }
        }
        }
        return -EINVAL;
        return -EINVAL;
}
}
 
 
static inline int xlate_0x00xx_ioctl(int *cmd, int *size)
static inline int xlate_0x00xx_ioctl(int *cmd, int *size)
{
{
        int i;
        int i;
        /* old ioctls' for kernels <= 1.3.33 */
        /* old ioctls' for kernels <= 1.3.33 */
        /* When the next even release will come around, we'll start
        /* When the next even release will come around, we'll start
         * warning against these.
         * warning against these.
         * When the next odd release will come around, we'll fail with
         * When the next odd release will come around, we'll fail with
         * -EINVAL */
         * -EINVAL */
        if(strcmp(system_utsname.version, "1.4.0") >= 0)
        if(strcmp(system_utsname.version, "1.4.0") >= 0)
                printk("obsolete floppy ioctl %x\n", *cmd);
                printk("obsolete floppy ioctl %x\n", *cmd);
        if((system_utsname.version[0] == '1' &&
        if((system_utsname.version[0] == '1' &&
            strcmp(system_utsname.version, "1.5.0") >= 0) ||
            strcmp(system_utsname.version, "1.5.0") >= 0) ||
           (system_utsname.version[0] >= '2' &&
           (system_utsname.version[0] >= '2' &&
            strcmp(system_utsname.version, "2.1.0") >= 0))
            strcmp(system_utsname.version, "2.1.0") >= 0))
                return -EINVAL;
                return -EINVAL;
        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
                if (*cmd == translation_table[i].oldcmd) {
                if (*cmd == translation_table[i].oldcmd) {
                        *size = translation_table[i].oldsize;
                        *size = translation_table[i].oldsize;
                        *cmd = translation_table[i].newcmd;
                        *cmd = translation_table[i].newcmd;
                        return 0;
                        return 0;
                }
                }
        }
        }
        return -EINVAL;
        return -EINVAL;
}
}
 
 
static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                    unsigned long param)
                    unsigned long param)
{
{
#define IOCTL_MODE_BIT 8
#define IOCTL_MODE_BIT 8
#define OPEN_WRITE_BIT 16
#define OPEN_WRITE_BIT 16
#define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
#define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
#define OUT(c,x) case c: outparam = (const char *) (x); break
#define OUT(c,x) case c: outparam = (const char *) (x); break
#define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
#define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
 
 
        int i,drive,type;
        int i,drive,type;
        kdev_t device;
        kdev_t device;
        int ret;
        int ret;
        int size;
        int size;
        union inparam {
        union inparam {
                struct floppy_struct g; /* geometry */
                struct floppy_struct g; /* geometry */
                struct format_descr f;
                struct format_descr f;
                struct floppy_max_errors max_errors;
                struct floppy_max_errors max_errors;
                struct floppy_drive_params dp;
                struct floppy_drive_params dp;
        } inparam; /* parameters coming from user space */
        } inparam; /* parameters coming from user space */
        const char *outparam; /* parameters passed back to user space */
        const char *outparam; /* parameters passed back to user space */
 
 
        device = inode->i_rdev;
        device = inode->i_rdev;
        switch (cmd) {
        switch (cmd) {
                RO_IOCTLS(device,param);
                RO_IOCTLS(device,param);
        }
        }
        type = TYPE(device);
        type = TYPE(device);
        drive = DRIVE(device);
        drive = DRIVE(device);
 
 
        /* convert compatibility eject ioctls into floppy eject ioctl.
        /* convert compatibility eject ioctls into floppy eject ioctl.
         * We do this in order to provide a means to eject floppy disks before
         * We do this in order to provide a means to eject floppy disks before
         * installing the new fdutils package */
         * installing the new fdutils package */
        if(cmd == CDROMEJECT || /* CD-ROM eject */
        if(cmd == CDROMEJECT || /* CD-ROM eject */
           cmd == 0x6470 /* SunOS floppy eject */) {
           cmd == 0x6470 /* SunOS floppy eject */) {
                DPRINT("obsolete eject ioctl\n");
                DPRINT("obsolete eject ioctl\n");
                DPRINT("please use floppycontrol --eject\n");
                DPRINT("please use floppycontrol --eject\n");
                cmd = FDEJECT;
                cmd = FDEJECT;
        }
        }
 
 
        /* convert the old style command into a new style command */
        /* convert the old style command into a new style command */
        if ((cmd & 0xff00) == 0x0200) {
        if ((cmd & 0xff00) == 0x0200) {
                ECALL(normalize_0x02xx_ioctl(&cmd, &size));
                ECALL(normalize_0x02xx_ioctl(&cmd, &size));
        } else if ((cmd & 0xff00) == 0x0000) {
        } else if ((cmd & 0xff00) == 0x0000) {
                ECALL(xlate_0x00xx_ioctl(&cmd, &size));
                ECALL(xlate_0x00xx_ioctl(&cmd, &size));
        } else
        } else
                return -EINVAL;
                return -EINVAL;
 
 
        /* permission checks */
        /* permission checks */
        if (((cmd & 0x80) && !suser()) ||
        if (((cmd & 0x80) && !suser()) ||
             ((cmd & 0x40) && !IOCTL_ALLOWED))
             ((cmd & 0x40) && !IOCTL_ALLOWED))
                return -EPERM;
                return -EPERM;
 
 
        /* verify writability of result, and fail early */
        /* verify writability of result, and fail early */
        if (_IOC_DIR(cmd) & _IOC_READ)
        if (_IOC_DIR(cmd) & _IOC_READ)
                ECALL(verify_area(VERIFY_WRITE,(void *) param, size));
                ECALL(verify_area(VERIFY_WRITE,(void *) param, size));
 
 
        /* copyin */
        /* copyin */
        CLEARSTRUCT(&inparam);
        CLEARSTRUCT(&inparam);
        if (_IOC_DIR(cmd) & _IOC_WRITE)
        if (_IOC_DIR(cmd) & _IOC_WRITE)
                ECALL(fd_copyin((void *)param, &inparam, size))
                ECALL(fd_copyin((void *)param, &inparam, size))
 
 
        switch (cmd) {
        switch (cmd) {
                case FDEJECT:
                case FDEJECT:
                        if(UDRS->fd_ref != 1)
                        if(UDRS->fd_ref != 1)
                                /* somebody else has this drive open */
                                /* somebody else has this drive open */
                                return -EBUSY;
                                return -EBUSY;
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
 
 
                        /* do the actual eject. Fails on
                        /* do the actual eject. Fails on
                         * non-Sparc architectures */
                         * non-Sparc architectures */
                        ret=fd_eject(UNIT(drive));
                        ret=fd_eject(UNIT(drive));
 
 
                        USETF(FD_DISK_CHANGED);
                        USETF(FD_DISK_CHANGED);
                        USETF(FD_VERIFY);
                        USETF(FD_VERIFY);
                        process_fd_request();
                        process_fd_request();
                        return ret;
                        return ret;
                case FDCLRPRM:
                case FDCLRPRM:
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        current_type[drive] = NULL;
                        current_type[drive] = NULL;
                        floppy_sizes[drive] = MAX_DISK_SIZE;
                        floppy_sizes[drive] = MAX_DISK_SIZE;
                        UDRS->keep_data = 0;
                        UDRS->keep_data = 0;
                        return invalidate_drive(device);
                        return invalidate_drive(device);
                case FDSETPRM:
                case FDSETPRM:
                case FDDEFPRM:
                case FDDEFPRM:
                        return set_geometry(cmd, & inparam.g,
                        return set_geometry(cmd, & inparam.g,
                                            drive, type, device);
                                            drive, type, device);
                case FDGETPRM:
                case FDGETPRM:
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        CALL(poll_drive(1,0));
                        CALL(poll_drive(1,0));
                        process_fd_request();
                        process_fd_request();
                        if (type)
                        if (type)
                                outparam = (char *) &floppy_type[type];
                                outparam = (char *) &floppy_type[type];
                        else
                        else
                                outparam = (char *) current_type[drive];
                                outparam = (char *) current_type[drive];
                        if(!outparam)
                        if(!outparam)
                                return -ENODEV;
                                return -ENODEV;
                        break;
                        break;
 
 
                case FDMSGON:
                case FDMSGON:
                        UDP->flags |= FTD_MSG;
                        UDP->flags |= FTD_MSG;
                        return 0;
                        return 0;
                case FDMSGOFF:
                case FDMSGOFF:
                        UDP->flags &= ~FTD_MSG;
                        UDP->flags &= ~FTD_MSG;
                        return 0;
                        return 0;
 
 
                case FDFMTBEG:
                case FDFMTBEG:
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                        ret = UDRS->flags;
                        ret = UDRS->flags;
                        process_fd_request();
                        process_fd_request();
                        if(ret & FD_VERIFY)
                        if(ret & FD_VERIFY)
                                return -ENODEV;
                                return -ENODEV;
                        if(!(ret & FD_DISK_WRITABLE))
                        if(!(ret & FD_DISK_WRITABLE))
                                return -EROFS;
                                return -EROFS;
                        return 0;
                        return 0;
                case FDFMTTRK:
                case FDFMTTRK:
                        if (UDRS->fd_ref != 1)
                        if (UDRS->fd_ref != 1)
                                return -EBUSY;
                                return -EBUSY;
                        return do_format(device, &inparam.f);
                        return do_format(device, &inparam.f);
                case FDFMTEND:
                case FDFMTEND:
                case FDFLUSH:
                case FDFLUSH:
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        return invalidate_drive(device);
                        return invalidate_drive(device);
 
 
                case FDSETEMSGTRESH:
                case FDSETEMSGTRESH:
                        UDP->max_errors.reporting =
                        UDP->max_errors.reporting =
                                (unsigned short) (param & 0x0f);
                                (unsigned short) (param & 0x0f);
                        return 0;
                        return 0;
                OUT(FDGETMAXERRS, &UDP->max_errors);
                OUT(FDGETMAXERRS, &UDP->max_errors);
                IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
                IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
 
 
                case FDGETDRVTYP:
                case FDGETDRVTYP:
                        outparam = drive_name(type,drive);
                        outparam = drive_name(type,drive);
                        SUPBOUND(size,strlen(outparam)+1);
                        SUPBOUND(size,strlen(outparam)+1);
                        break;
                        break;
 
 
                IN(FDSETDRVPRM, UDP, dp);
                IN(FDSETDRVPRM, UDP, dp);
                OUT(FDGETDRVPRM, UDP);
                OUT(FDGETDRVPRM, UDP);
 
 
                case FDPOLLDRVSTAT:
                case FDPOLLDRVSTAT:
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
                        process_fd_request();
                        process_fd_request();
                        /* fall through */
                        /* fall through */
                OUT(FDGETDRVSTAT, UDRS);
                OUT(FDGETDRVSTAT, UDRS);
 
 
                case FDRESET:
                case FDRESET:
                        return user_reset_fdc(drive, (int)param, 1);
                        return user_reset_fdc(drive, (int)param, 1);
 
 
                OUT(FDGETFDCSTAT,UFDCS);
                OUT(FDGETFDCSTAT,UFDCS);
 
 
                case FDWERRORCLR:
                case FDWERRORCLR:
                        CLEARSTRUCT(UDRWE);
                        CLEARSTRUCT(UDRWE);
                        return 0;
                        return 0;
                OUT(FDWERRORGET,UDRWE);
                OUT(FDWERRORGET,UDRWE);
 
 
                case OLDFDRAWCMD:
                case OLDFDRAWCMD:
                case FDRAWCMD:
                case FDRAWCMD:
                        if (type)
                        if (type)
                                return -EINVAL;
                                return -EINVAL;
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        set_floppy(device);
                        set_floppy(device);
                        CALL(i = raw_cmd_ioctl(cmd,(void *) param));
                        CALL(i = raw_cmd_ioctl(cmd,(void *) param));
                        process_fd_request();
                        process_fd_request();
                        return i;
                        return i;
 
 
                case FDTWADDLE:
                case FDTWADDLE:
                        LOCK_FDC(drive,1);
                        LOCK_FDC(drive,1);
                        twaddle();
                        twaddle();
                        process_fd_request();
                        process_fd_request();
                        return 0;
                        return 0;
 
 
                default:
                default:
                        return -EINVAL;
                        return -EINVAL;
        }
        }
 
 
        if (_IOC_DIR(cmd) & _IOC_READ)
        if (_IOC_DIR(cmd) & _IOC_READ)
                return fd_copyout((void *)param, outparam, size);
                return fd_copyout((void *)param, outparam, size);
        else
        else
                return 0;
                return 0;
#undef IOCTL_ALLOWED
#undef IOCTL_ALLOWED
#undef OUT
#undef OUT
#undef IN
#undef IN
}
}
 
 
static void config_types(void)
static void config_types(void)
{
{
        int first=1;
        int first=1;
        int drive;
        int drive;
 
 
        /* read drive info out of physical CMOS */
        /* read drive info out of physical CMOS */
        drive=0;
        drive=0;
        if (!UDP->cmos)
        if (!UDP->cmos)
                UDP->cmos= FLOPPY0_TYPE;
                UDP->cmos= FLOPPY0_TYPE;
        drive=1;
        drive=1;
        if (!UDP->cmos && FLOPPY1_TYPE)
        if (!UDP->cmos && FLOPPY1_TYPE)
                UDP->cmos = FLOPPY1_TYPE;
                UDP->cmos = FLOPPY1_TYPE;
 
 
        /* XXX */
        /* XXX */
        /* additional physical CMOS drive detection should go here */
        /* additional physical CMOS drive detection should go here */
 
 
        for (drive=0; drive < N_DRIVE; drive++){
        for (drive=0; drive < N_DRIVE; drive++){
                if (UDP->cmos >= 16)
                if (UDP->cmos >= 16)
                        UDP->cmos = 0;
                        UDP->cmos = 0;
                if (UDP->cmos >= 0 && UDP->cmos <= NUMBER(default_drive_params))
                if (UDP->cmos >= 0 && UDP->cmos <= NUMBER(default_drive_params))
                        memcpy((char *) UDP,
                        memcpy((char *) UDP,
                               (char *) (&default_drive_params[(int)UDP->cmos].params),
                               (char *) (&default_drive_params[(int)UDP->cmos].params),
                               sizeof(struct floppy_drive_params));
                               sizeof(struct floppy_drive_params));
                if (UDP->cmos){
                if (UDP->cmos){
                        if (first)
                        if (first)
                                printk(KERN_INFO "Floppy drive(s): ");
                                printk(KERN_INFO "Floppy drive(s): ");
                        else
                        else
                                printk(", ");
                                printk(", ");
                        first=0;
                        first=0;
                        if (UDP->cmos > 0){
                        if (UDP->cmos > 0){
                                allowed_drive_mask |= 1 << drive;
                                allowed_drive_mask |= 1 << drive;
                                printk("fd%d is %s", drive,
                                printk("fd%d is %s", drive,
                                       default_drive_params[(int)UDP->cmos].name);
                                       default_drive_params[(int)UDP->cmos].name);
                        } else
                        } else
                                printk("fd%d is unknown type %d",drive,
                                printk("fd%d is unknown type %d",drive,
                                       (unsigned int)UDP->cmos);
                                       (unsigned int)UDP->cmos);
                }
                }
        }
        }
        if (!first)
        if (!first)
                printk("\n");
                printk("\n");
}
}
 
 
static int floppy_read(struct inode * inode, struct file * filp,
static int floppy_read(struct inode * inode, struct file * filp,
                       char * buf, int count)
                       char * buf, int count)
{
{
        int drive = DRIVE(inode->i_rdev);
        int drive = DRIVE(inode->i_rdev);
 
 
        check_disk_change(inode->i_rdev);
        check_disk_change(inode->i_rdev);
        if (UTESTF(FD_DISK_CHANGED))
        if (UTESTF(FD_DISK_CHANGED))
                return -ENXIO;
                return -ENXIO;
        return block_read(inode, filp, buf, count);
        return block_read(inode, filp, buf, count);
}
}
 
 
static int floppy_write(struct inode * inode, struct file * filp,
static int floppy_write(struct inode * inode, struct file * filp,
                        const char * buf, int count)
                        const char * buf, int count)
{
{
        int block;
        int block;
        int ret;
        int ret;
        int drive = DRIVE(inode->i_rdev);
        int drive = DRIVE(inode->i_rdev);
 
 
        if (!UDRS->maxblock)
        if (!UDRS->maxblock)
                UDRS->maxblock=1;/* make change detectable */
                UDRS->maxblock=1;/* make change detectable */
        check_disk_change(inode->i_rdev);
        check_disk_change(inode->i_rdev);
        if (UTESTF(FD_DISK_CHANGED))
        if (UTESTF(FD_DISK_CHANGED))
                return -ENXIO;
                return -ENXIO;
        if (!UTESTF(FD_DISK_WRITABLE))
        if (!UTESTF(FD_DISK_WRITABLE))
                return -EROFS;
                return -EROFS;
        block = (filp->f_pos + count) >> 9;
        block = (filp->f_pos + count) >> 9;
        INFBOUND(UDRS->maxblock, block);
        INFBOUND(UDRS->maxblock, block);
        ret= block_write(inode, filp, buf, count);
        ret= block_write(inode, filp, buf, count);
        return ret;
        return ret;
}
}
 
 
static void floppy_release(struct inode * inode, struct file * filp)
static void floppy_release(struct inode * inode, struct file * filp)
{
{
        int drive;
        int drive;
 
 
        drive = DRIVE(inode->i_rdev);
        drive = DRIVE(inode->i_rdev);
 
 
        if (!filp || (filp->f_mode & (2 | OPEN_WRITE_BIT)))
        if (!filp || (filp->f_mode & (2 | OPEN_WRITE_BIT)))
                /* if the file is mounted OR (writable now AND writable at
                /* if the file is mounted OR (writable now AND writable at
                 * open time) Linus: Does this cover all cases? */
                 * open time) Linus: Does this cover all cases? */
                block_fsync(inode,filp);
                block_fsync(inode,filp);
 
 
        if (UDRS->fd_ref < 0)
        if (UDRS->fd_ref < 0)
                UDRS->fd_ref=0;
                UDRS->fd_ref=0;
        else if (!UDRS->fd_ref--) {
        else if (!UDRS->fd_ref--) {
                DPRINT("floppy_release with fd_ref == 0");
                DPRINT("floppy_release with fd_ref == 0");
                UDRS->fd_ref = 0;
                UDRS->fd_ref = 0;
        }
        }
        floppy_release_irq_and_dma();
        floppy_release_irq_and_dma();
}
}
 
 
/*
/*
 * floppy_open check for aliasing (/dev/fd0 can be the same as
 * floppy_open check for aliasing (/dev/fd0 can be the same as
 * /dev/PS0 etc), and disallows simultaneous access to the same
 * /dev/PS0 etc), and disallows simultaneous access to the same
 * drive with different device numbers.
 * drive with different device numbers.
 */
 */
#define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
#define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
 
 
static int floppy_open(struct inode * inode, struct file * filp)
static int floppy_open(struct inode * inode, struct file * filp)
{
{
        int drive;
        int drive;
        int old_dev;
        int old_dev;
        int try;
        int try;
        char *tmp;
        char *tmp;
 
 
        if (!filp) {
        if (!filp) {
                DPRINT("Weird, open called with filp=0\n");
                DPRINT("Weird, open called with filp=0\n");
                return -EIO;
                return -EIO;
        }
        }
 
 
        drive = DRIVE(inode->i_rdev);
        drive = DRIVE(inode->i_rdev);
        if (drive >= N_DRIVE ||
        if (drive >= N_DRIVE ||
            !(allowed_drive_mask & (1 << drive)) ||
            !(allowed_drive_mask & (1 << drive)) ||
            fdc_state[FDC(drive)].version == FDC_NONE)
            fdc_state[FDC(drive)].version == FDC_NONE)
                return -ENXIO;
                return -ENXIO;
 
 
        if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
        if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
                return -ENXIO;
                return -ENXIO;
        old_dev = UDRS->fd_device;
        old_dev = UDRS->fd_device;
        if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
        if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
                return -EBUSY;
                return -EBUSY;
 
 
        if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
        if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
                USETF(FD_DISK_CHANGED);
                USETF(FD_DISK_CHANGED);
                USETF(FD_VERIFY);
                USETF(FD_VERIFY);
        }
        }
 
 
        if (UDRS->fd_ref == -1 ||
        if (UDRS->fd_ref == -1 ||
           (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
           (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
                return -EBUSY;
                return -EBUSY;
 
 
        if (floppy_grab_irq_and_dma())
        if (floppy_grab_irq_and_dma())
                return -EBUSY;
                return -EBUSY;
 
 
        if (filp->f_flags & O_EXCL)
        if (filp->f_flags & O_EXCL)
                UDRS->fd_ref = -1;
                UDRS->fd_ref = -1;
        else
        else
                UDRS->fd_ref++;
                UDRS->fd_ref++;
 
 
        if (!floppy_track_buffer){
        if (!floppy_track_buffer){
                /* if opening an ED drive, reserve a big buffer,
                /* if opening an ED drive, reserve a big buffer,
                 * else reserve a small one */
                 * else reserve a small one */
                if ((UDP->cmos == 6) || (UDP->cmos == 5))
                if ((UDP->cmos == 6) || (UDP->cmos == 5))
                        try = 64; /* Only 48 actually useful */
                        try = 64; /* Only 48 actually useful */
                else
                else
                        try = 32; /* Only 24 actually useful */
                        try = 32; /* Only 24 actually useful */
 
 
                tmp=(char *)fd_dma_mem_alloc(1024 * try);
                tmp=(char *)fd_dma_mem_alloc(1024 * try);
                if (!tmp) {
                if (!tmp) {
                        try >>= 1; /* buffer only one side */
                        try >>= 1; /* buffer only one side */
                        INFBOUND(try, 16);
                        INFBOUND(try, 16);
                        tmp= (char *)fd_dma_mem_alloc(1024*try);
                        tmp= (char *)fd_dma_mem_alloc(1024*try);
                }
                }
                if (!tmp) {
                if (!tmp) {
                        DPRINT("Unable to allocate DMA memory\n");
                        DPRINT("Unable to allocate DMA memory\n");
                        RETERR(ENXIO);
                        RETERR(ENXIO);
                }
                }
                if (floppy_track_buffer)
                if (floppy_track_buffer)
                        fd_dma_mem_free((unsigned long)tmp,try*1024);
                        fd_dma_mem_free((unsigned long)tmp,try*1024);
                else {
                else {
                        buffer_min = buffer_max = -1;
                        buffer_min = buffer_max = -1;
                        floppy_track_buffer = tmp;
                        floppy_track_buffer = tmp;
                        max_buffer_sectors = try;
                        max_buffer_sectors = try;
                }
                }
        }
        }
 
 
        UDRS->fd_device = MINOR(inode->i_rdev);
        UDRS->fd_device = MINOR(inode->i_rdev);
        if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
        if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
                if (buffer_drive == drive)
                if (buffer_drive == drive)
                        buffer_track = -1;
                        buffer_track = -1;
                invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
                invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
        }
        }
 
 
        /* Allow ioctls if we have write-permissions even if read-only open */
        /* Allow ioctls if we have write-permissions even if read-only open */
        if ((filp->f_mode & 2) || (permission(inode,2) == 0))
        if ((filp->f_mode & 2) || (permission(inode,2) == 0))
                filp->f_mode |= IOCTL_MODE_BIT;
                filp->f_mode |= IOCTL_MODE_BIT;
        if (filp->f_mode & 2)
        if (filp->f_mode & 2)
                filp->f_mode |= OPEN_WRITE_BIT;
                filp->f_mode |= OPEN_WRITE_BIT;
 
 
        if (UFDCS->rawcmd == 1)
        if (UFDCS->rawcmd == 1)
                UFDCS->rawcmd = 2;
                UFDCS->rawcmd = 2;
 
 
        if (filp->f_flags & O_NDELAY)
        if (filp->f_flags & O_NDELAY)
                return 0;
                return 0;
        if (filp->f_mode & 3) {
        if (filp->f_mode & 3) {
                UDRS->last_checked = 0;
                UDRS->last_checked = 0;
                check_disk_change(inode->i_rdev);
                check_disk_change(inode->i_rdev);
                if (UTESTF(FD_DISK_CHANGED))
                if (UTESTF(FD_DISK_CHANGED))
                        RETERR(ENXIO);
                        RETERR(ENXIO);
        }
        }
        if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
        if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
                RETERR(EROFS);
                RETERR(EROFS);
        return 0;
        return 0;
#undef RETERR
#undef RETERR
}
}
 
 
/*
/*
 * Check if the disk has been changed or if a change has been faked.
 * Check if the disk has been changed or if a change has been faked.
 */
 */
static int check_floppy_change(kdev_t dev)
static int check_floppy_change(kdev_t dev)
{
{
        int drive = DRIVE(dev);
        int drive = DRIVE(dev);
 
 
        if (MAJOR(dev) != MAJOR_NR) {
        if (MAJOR(dev) != MAJOR_NR) {
                DPRINT("check_floppy_change: not a floppy\n");
                DPRINT("check_floppy_change: not a floppy\n");
                return 0;
                return 0;
        }
        }
 
 
        if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
        if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
                return 1;
                return 1;
 
 
        if (UDP->checkfreq < jiffies - UDRS->last_checked){
        if (UDP->checkfreq < jiffies - UDRS->last_checked){
                lock_fdc(drive,0);
                lock_fdc(drive,0);
                poll_drive(0,0);
                poll_drive(0,0);
                process_fd_request();
                process_fd_request();
        }
        }
 
 
        if (UTESTF(FD_DISK_CHANGED) ||
        if (UTESTF(FD_DISK_CHANGED) ||
           UTESTF(FD_VERIFY) ||
           UTESTF(FD_VERIFY) ||
           test_bit(drive, &fake_change) ||
           test_bit(drive, &fake_change) ||
           (!TYPE(dev) && !current_type[drive]))
           (!TYPE(dev) && !current_type[drive]))
                return 1;
                return 1;
        return 0;
        return 0;
}
}
 
 
/* revalidate the floppy disk, i.e. trigger format autodetection by reading
/* revalidate the floppy disk, i.e. trigger format autodetection by reading
 * the bootblock (block 0). "Autodetection" is also needed to check whether
 * the bootblock (block 0). "Autodetection" is also needed to check whether
 * there is a disk in the drive at all... Thus we also do it for fixed
 * there is a disk in the drive at all... Thus we also do it for fixed
 * geometry formats */
 * geometry formats */
static int floppy_revalidate(kdev_t dev)
static int floppy_revalidate(kdev_t dev)
{
{
#define NO_GEOM (!current_type[drive] && !TYPE(dev))
#define NO_GEOM (!current_type[drive] && !TYPE(dev))
        struct buffer_head * bh;
        struct buffer_head * bh;
        int drive=DRIVE(dev);
        int drive=DRIVE(dev);
        int cf;
        int cf;
 
 
        if (UTESTF(FD_DISK_CHANGED) ||
        if (UTESTF(FD_DISK_CHANGED) ||
           UTESTF(FD_VERIFY) ||
           UTESTF(FD_VERIFY) ||
           test_bit(drive, &fake_change) ||
           test_bit(drive, &fake_change) ||
           NO_GEOM){
           NO_GEOM){
                lock_fdc(drive,0);
                lock_fdc(drive,0);
                cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
                cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
                if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
                if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
                        process_fd_request(); /*already done by another thread*/
                        process_fd_request(); /*already done by another thread*/
                        return 0;
                        return 0;
                }
                }
                UDRS->maxblock = 0;
                UDRS->maxblock = 0;
                UDRS->maxtrack = 0;
                UDRS->maxtrack = 0;
                if (buffer_drive == drive)
                if (buffer_drive == drive)
                        buffer_track = -1;
                        buffer_track = -1;
                clear_bit(drive, &fake_change);
                clear_bit(drive, &fake_change);
                UCLEARF(FD_DISK_CHANGED);
                UCLEARF(FD_DISK_CHANGED);
                if (cf)
                if (cf)
                        UDRS->generation++;
                        UDRS->generation++;
                if (NO_GEOM){
                if (NO_GEOM){
                        /* auto-sensing */
                        /* auto-sensing */
                        int size = floppy_blocksizes[MINOR(dev)];
                        int size = floppy_blocksizes[MINOR(dev)];
                        if (!size)
                        if (!size)
                                size = 1024;
                                size = 1024;
                        if (!(bh = getblk(dev,0,size))){
                        if (!(bh = getblk(dev,0,size))){
                                process_fd_request();
                                process_fd_request();
                                return 1;
                                return 1;
                        }
                        }
                        if (bh && !buffer_uptodate(bh))
                        if (bh && !buffer_uptodate(bh))
                                ll_rw_block(READ, 1, &bh);
                                ll_rw_block(READ, 1, &bh);
                        process_fd_request();
                        process_fd_request();
                        wait_on_buffer(bh);
                        wait_on_buffer(bh);
                        brelse(bh);
                        brelse(bh);
                        return 0;
                        return 0;
                }
                }
                if (cf)
                if (cf)
                        poll_drive(0, FD_RAW_NEED_DISK);
                        poll_drive(0, FD_RAW_NEED_DISK);
                process_fd_request();
                process_fd_request();
        }
        }
        return 0;
        return 0;
}
}
 
 
static struct file_operations floppy_fops = {
static struct file_operations floppy_fops = {
        NULL,                   /* lseek - default */
        NULL,                   /* lseek - default */
        floppy_read,            /* read - general block-dev read */
        floppy_read,            /* read - general block-dev read */
        floppy_write,           /* write - general block-dev write */
        floppy_write,           /* write - general block-dev write */
        NULL,                   /* readdir - bad */
        NULL,                   /* readdir - bad */
        NULL,                   /* select */
        NULL,                   /* select */
        fd_ioctl,               /* ioctl */
        fd_ioctl,               /* ioctl */
        NULL,                   /* mmap */
        NULL,                   /* mmap */
        floppy_open,            /* open */
        floppy_open,            /* open */
        floppy_release,         /* release */
        floppy_release,         /* release */
        block_fsync,            /* fsync */
        block_fsync,            /* fsync */
        NULL,                   /* fasync */
        NULL,                   /* fasync */
        check_floppy_change,    /* media_change */
        check_floppy_change,    /* media_change */
        floppy_revalidate,      /* revalidate */
        floppy_revalidate,      /* revalidate */
};
};
 
 
/*
/*
 * Floppy Driver initialization
 * Floppy Driver initialization
 * =============================
 * =============================
 */
 */
 
 
/* Determine the floppy disk controller type */
/* Determine the floppy disk controller type */
/* This routine was written by David C. Niemi */
/* This routine was written by David C. Niemi */
static char get_fdc_version(void)
static char get_fdc_version(void)
{
{
        int r;
        int r;
 
 
        output_byte(FD_DUMPREGS);       /* 82072 and better know DUMPREGS */
        output_byte(FD_DUMPREGS);       /* 82072 and better know DUMPREGS */
        if (FDCS->reset)
        if (FDCS->reset)
                return FDC_NONE;
                return FDC_NONE;
        if ((r = result()) <= 0x00)
        if ((r = result()) <= 0x00)
                return FDC_NONE;        /* No FDC present ??? */
                return FDC_NONE;        /* No FDC present ??? */
        if ((r==1) && (reply_buffer[0] == 0x80)){
        if ((r==1) && (reply_buffer[0] == 0x80)){
                printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
                printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
                return FDC_8272A;       /* 8272a/765 don't know DUMPREGS */
                return FDC_8272A;       /* 8272a/765 don't know DUMPREGS */
        }
        }
        if (r != 10) {
        if (r != 10) {
                printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
                printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
                       fdc, r);
                       fdc, r);
                return FDC_UNKNOWN;
                return FDC_UNKNOWN;
        }
        }
 
 
        if(!fdc_configure()) {
        if(!fdc_configure()) {
                printk(KERN_INFO "FDC %d is an 82072\n",fdc);
                printk(KERN_INFO "FDC %d is an 82072\n",fdc);
                return FDC_82072;       /* 82072 doesn't know CONFIGURE */
                return FDC_82072;       /* 82072 doesn't know CONFIGURE */
        }
        }
 
 
        output_byte(FD_PERPENDICULAR);
        output_byte(FD_PERPENDICULAR);
        if(need_more_output() == MORE_OUTPUT) {
        if(need_more_output() == MORE_OUTPUT) {
                output_byte(0);
                output_byte(0);
        } else {
        } else {
                printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
                printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
                return FDC_82072A;      /* 82072A as found on Sparcs. */
                return FDC_82072A;      /* 82072A as found on Sparcs. */
        }
        }
 
 
        output_byte(FD_UNLOCK);
        output_byte(FD_UNLOCK);
        r = result();
        r = result();
        if ((r == 1) && (reply_buffer[0] == 0x80)){
        if ((r == 1) && (reply_buffer[0] == 0x80)){
                printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
                printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
                return FDC_82077_ORIG;  /* Pre-1991 82077, doesn't know
                return FDC_82077_ORIG;  /* Pre-1991 82077, doesn't know
                                         * LOCK/UNLOCK */
                                         * LOCK/UNLOCK */
        }
        }
        if ((r != 1) || (reply_buffer[0] != 0x00)) {
        if ((r != 1) || (reply_buffer[0] != 0x00)) {
                printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
                printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
                       fdc, r);
                       fdc, r);
                return FDC_UNKNOWN;
                return FDC_UNKNOWN;
        }
        }
        output_byte(FD_PARTID);
        output_byte(FD_PARTID);
        r = result();
        r = result();
        if (r != 1) {
        if (r != 1) {
                printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
                printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
                       fdc, r);
                       fdc, r);
                return FDC_UNKNOWN;
                return FDC_UNKNOWN;
        }
        }
        if (reply_buffer[0] == 0x80) {
        if (reply_buffer[0] == 0x80) {
                printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
                printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
                return FDC_82077;       /* Revised 82077AA passes all the tests */
                return FDC_82077;       /* Revised 82077AA passes all the tests */
        }
        }
        switch (reply_buffer[0] >> 5) {
        switch (reply_buffer[0] >> 5) {
                case 0x0:
                case 0x0:
                        /* Either a 82078-1 or a 82078SL running at 5Volt */
                        /* Either a 82078-1 or a 82078SL running at 5Volt */
                        printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
                        printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
                        return FDC_82078;
                        return FDC_82078;
                case 0x1:
                case 0x1:
                        printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
                        printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
                        return FDC_82078;
                        return FDC_82078;
                case 0x2:
                case 0x2:
                        printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
                        printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
                        return FDC_S82078B;
                        return FDC_S82078B;
                case 0x3:
                case 0x3:
                        printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
                        printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
                        return FDC_87306;
                        return FDC_87306;
                default:
                default:
                        printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
                        printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
                               fdc, reply_buffer[0] >> 5);
                               fdc, reply_buffer[0] >> 5);
                        return FDC_82078_UNKN;
                        return FDC_82078_UNKN;
        }
        }
} /* get_fdc_version */
} /* get_fdc_version */
 
 
/* lilo configuration */
/* lilo configuration */
 
 
/* we make the invert_dcl function global. One day, somebody might
/* we make the invert_dcl function global. One day, somebody might
 * want to centralize all thinkpad related options into one lilo option,
 * want to centralize all thinkpad related options into one lilo option,
 * there are just so many thinkpad related quirks! */
 * there are just so many thinkpad related quirks! */
void floppy_invert_dcl(int *ints,int param)
void floppy_invert_dcl(int *ints,int param)
{
{
        int i;
        int i;
 
 
        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
                if (param)
                if (param)
                        default_drive_params[i].params.flags |= 0x80;
                        default_drive_params[i].params.flags |= 0x80;
                else
                else
                        default_drive_params[i].params.flags &= ~0x80;
                        default_drive_params[i].params.flags &= ~0x80;
        }
        }
        DPRINT("Configuring drives for inverted dcl\n");
        DPRINT("Configuring drives for inverted dcl\n");
}
}
 
 
static void daring(int *ints,int param)
static void daring(int *ints,int param)
{
{
        int i;
        int i;
 
 
        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
                if (param){
                if (param){
                        default_drive_params[i].params.select_delay = 0;
                        default_drive_params[i].params.select_delay = 0;
                        default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
                        default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
                } else {
                } else {
                        default_drive_params[i].params.select_delay = 2*HZ/100;
                        default_drive_params[i].params.select_delay = 2*HZ/100;
                        default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
                        default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
                }
                }
        }
        }
        DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
        DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
}
}
 
 
static void set_cmos(int *ints, int dummy)
static void set_cmos(int *ints, int dummy)
{
{
        int current_drive=0;
        int current_drive=0;
 
 
        if (ints[0] != 2){
        if (ints[0] != 2){
                DPRINT("wrong number of parameter for cmos\n");
                DPRINT("wrong number of parameter for cmos\n");
                return;
                return;
        }
        }
        current_drive = ints[1];
        current_drive = ints[1];
        if (current_drive < 0 || current_drive >= 8){
        if (current_drive < 0 || current_drive >= 8){
                DPRINT("bad drive for set_cmos\n");
                DPRINT("bad drive for set_cmos\n");
                return;
                return;
        }
        }
        if (current_drive >= 4 && !FDC2)
        if (current_drive >= 4 && !FDC2)
                FDC2 = 0x370;
                FDC2 = 0x370;
        if (ints[2] <= 0 ||
        if (ints[2] <= 0 ||
            (ints[2] >= NUMBER(default_drive_params) && ints[2] != 16)){
            (ints[2] >= NUMBER(default_drive_params) && ints[2] != 16)){
                DPRINT("bad cmos code %d\n", ints[2]);
                DPRINT("bad cmos code %d\n", ints[2]);
                return;
                return;
        }
        }
        DP->cmos = ints[2];
        DP->cmos = ints[2];
        DPRINT("setting cmos code to %d\n", ints[2]);
        DPRINT("setting cmos code to %d\n", ints[2]);
}
}
 
 
static struct param_table {
static struct param_table {
        const char *name;
        const char *name;
        void (*fn)(int *ints, int param);
        void (*fn)(int *ints, int param);
        int *var;
        int *var;
        int def_param;
        int def_param;
} config_params[]={
} config_params[]={
        { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff },
        { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff },
        { "all_drives", 0, &allowed_drive_mask, 0xff },
        { "all_drives", 0, &allowed_drive_mask, 0xff },
        { "asus_pci", 0, &allowed_drive_mask, 0x33 },
        { "asus_pci", 0, &allowed_drive_mask, 0x33 },
 
 
        { "daring", daring, 0, 1},
        { "daring", daring, 0, 1},
 
 
        { "two_fdc",  0, &FDC2, 0x370 },
        { "two_fdc",  0, &FDC2, 0x370 },
        { "one_fdc", 0, &FDC2, 0 },
        { "one_fdc", 0, &FDC2, 0 },
 
 
        { "thinkpad", floppy_invert_dcl, 0, 1 },
        { "thinkpad", floppy_invert_dcl, 0, 1 },
 
 
        { "nodma", 0, &use_virtual_dma, 1 },
        { "nodma", 0, &use_virtual_dma, 1 },
        { "omnibook", 0, &use_virtual_dma, 1 },
        { "omnibook", 0, &use_virtual_dma, 1 },
        { "dma", 0, &use_virtual_dma, 0 },
        { "dma", 0, &use_virtual_dma, 0 },
 
 
        { "fifo_depth", 0, &fifo_depth, 0xa },
        { "fifo_depth", 0, &fifo_depth, 0xa },
        { "nofifo", 0, &no_fifo, 0x20 },
        { "nofifo", 0, &no_fifo, 0x20 },
        { "usefifo", 0, &no_fifo, 0 },
        { "usefifo", 0, &no_fifo, 0 },
 
 
        { "cmos", set_cmos, 0, 0 },
        { "cmos", set_cmos, 0, 0 },
 
 
        { "unexpected_interrupts", 0, &print_unex, 1 },
        { "unexpected_interrupts", 0, &print_unex, 1 },
        { "no_unexpected_interrupts", 0, &print_unex, 0 },
        { "no_unexpected_interrupts", 0, &print_unex, 0 },
        { "L40SX", 0, &print_unex, 0 } };
        { "L40SX", 0, &print_unex, 0 } };
 
 
#define FLOPPY_SETUP
#define FLOPPY_SETUP
void floppy_setup(char *str, int *ints)
void floppy_setup(char *str, int *ints)
{
{
        int i;
        int i;
        int param;
        int param;
        if (str)
        if (str)
                for (i=0; i< ARRAY_SIZE(config_params); i++){
                for (i=0; i< ARRAY_SIZE(config_params); i++){
                        if (strcmp(str,config_params[i].name) == 0){
                        if (strcmp(str,config_params[i].name) == 0){
                                if (ints[0])
                                if (ints[0])
                                        param = ints[1];
                                        param = ints[1];
                                else
                                else
                                        param = config_params[i].def_param;
                                        param = config_params[i].def_param;
                                if(config_params[i].fn)
                                if(config_params[i].fn)
                                        config_params[i].fn(ints,param);
                                        config_params[i].fn(ints,param);
                                if(config_params[i].var) {
                                if(config_params[i].var) {
                                        DPRINT("%s=%d\n", str, param);
                                        DPRINT("%s=%d\n", str, param);
                                        *config_params[i].var = param;
                                        *config_params[i].var = param;
                                }
                                }
                                return;
                                return;
                        }
                        }
                }
                }
        if (str) {
        if (str) {
                DPRINT("unknown floppy option [%s]\n", str);
                DPRINT("unknown floppy option [%s]\n", str);
 
 
                DPRINT("allowed options are:");
                DPRINT("allowed options are:");
                for (i=0; i< ARRAY_SIZE(config_params); i++)
                for (i=0; i< ARRAY_SIZE(config_params); i++)
                        printk(" %s",config_params[i].name);
                        printk(" %s",config_params[i].name);
                printk("\n");
                printk("\n");
        } else
        } else
                DPRINT("botched floppy option\n");
                DPRINT("botched floppy option\n");
        DPRINT("Read linux/arch/arm/drivers/block/README.fd\n");
        DPRINT("Read linux/arch/arm/drivers/block/README.fd\n");
}
}
 
 
int floppy_init(void)
int floppy_init(void)
{
{
        int i,unit,drive;
        int i,unit,drive;
        int have_no_fdc= -EIO;
        int have_no_fdc= -EIO;
 
 
        raw_cmd = NULL;
        raw_cmd = NULL;
 
 
        if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
        if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
                printk("Unable to get major %d for floppy\n",MAJOR_NR);
                printk("Unable to get major %d for floppy\n",MAJOR_NR);
                return -EBUSY;
                return -EBUSY;
        }
        }
 
 
        for (i=0; i<256; i++)
        for (i=0; i<256; i++)
                if (ITYPE(i))
                if (ITYPE(i))
                        floppy_sizes[i] = floppy_type[ITYPE(i)].size >> 1;
                        floppy_sizes[i] = floppy_type[ITYPE(i)].size >> 1;
                else
                else
                        floppy_sizes[i] = MAX_DISK_SIZE;
                        floppy_sizes[i] = MAX_DISK_SIZE;
 
 
        blk_size[MAJOR_NR] = floppy_sizes;
        blk_size[MAJOR_NR] = floppy_sizes;
        blksize_size[MAJOR_NR] = floppy_blocksizes;
        blksize_size[MAJOR_NR] = floppy_blocksizes;
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
        reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
        reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
        config_types();
        config_types();
 
 
        for (i = 0; i < N_FDC; i++) {
        for (i = 0; i < N_FDC; i++) {
                fdc = i;
                fdc = i;
                CLEARSTRUCT(FDCS);
                CLEARSTRUCT(FDCS);
                FDCS->dtr = -1;
                FDCS->dtr = -1;
                FDCS->dor = 0x4;
                FDCS->dor = 0x4;
#ifdef __sparc__
#ifdef __sparc__
                /*sparcs don't have a DOR reset which we can fall back on to*/
                /*sparcs don't have a DOR reset which we can fall back on to*/
                FDCS->version = FDC_82072A;
                FDCS->version = FDC_82072A;
#endif
#endif
        }
        }
 
 
        fdc_state[0].address = FDC1;
        fdc_state[0].address = FDC1;
#if N_FDC > 1
#if N_FDC > 1
        fdc_state[1].address = FDC2;
        fdc_state[1].address = FDC2;
#endif
#endif
 
 
        if (floppy_grab_irq_and_dma()){
        if (floppy_grab_irq_and_dma()){
                unregister_blkdev(MAJOR_NR,"fd");
                unregister_blkdev(MAJOR_NR,"fd");
                return -EBUSY;
                return -EBUSY;
        }
        }
 
 
        /* initialise drive state */
        /* initialise drive state */
        for (drive = 0; drive < N_DRIVE; drive++) {
        for (drive = 0; drive < N_DRIVE; drive++) {
                CLEARSTRUCT(UDRS);
                CLEARSTRUCT(UDRS);
                CLEARSTRUCT(UDRWE);
                CLEARSTRUCT(UDRWE);
                UDRS->flags = FD_VERIFY | FD_DISK_NEWCHANGE | FD_DISK_CHANGED;
                UDRS->flags = FD_VERIFY | FD_DISK_NEWCHANGE | FD_DISK_CHANGED;
                UDRS->fd_device = -1;
                UDRS->fd_device = -1;
                floppy_track_buffer = NULL;
                floppy_track_buffer = NULL;
                max_buffer_sectors = 0;
                max_buffer_sectors = 0;
        }
        }
 
 
        for (i = 0; i < N_FDC; i++) {
        for (i = 0; i < N_FDC; i++) {
                fdc = i;
                fdc = i;
                FDCS->driver_version = FD_DRIVER_VERSION;
                FDCS->driver_version = FD_DRIVER_VERSION;
                for (unit=0; unit<4; unit++)
                for (unit=0; unit<4; unit++)
                        FDCS->track[unit] = 0;
                        FDCS->track[unit] = 0;
                if (FDCS->address == -1)
                if (FDCS->address == -1)
                        continue;
                        continue;
                FDCS->rawcmd = 2;
                FDCS->rawcmd = 2;
                if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
                if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
                        FDCS->address = -1;
                        FDCS->address = -1;
                        FDCS->version = FDC_NONE;
                        FDCS->version = FDC_NONE;
                        continue;
                        continue;
                }
                }
                /* Try to determine the floppy controller type */
                /* Try to determine the floppy controller type */
                FDCS->version = get_fdc_version();
                FDCS->version = get_fdc_version();
                if (FDCS->version == FDC_NONE){
                if (FDCS->version == FDC_NONE){
                        FDCS->address = -1;
                        FDCS->address = -1;
                        continue;
                        continue;
                }
                }
 
 
                request_region(FDCS->address, 6, "floppy");
                request_region(FDCS->address, 6, "floppy");
                request_region(FDCS->address+7, 1, "floppy DIR");
                request_region(FDCS->address+7, 1, "floppy DIR");
                /* address + 6 is reserved, and may be taken by IDE.
                /* address + 6 is reserved, and may be taken by IDE.
                 * Unfortunately, Adaptec doesn't know this :-(, */
                 * Unfortunately, Adaptec doesn't know this :-(, */
 
 
                have_no_fdc = 0;
                have_no_fdc = 0;
                /* Not all FDCs seem to be able to handle the version command
                /* Not all FDCs seem to be able to handle the version command
                 * properly, so force a reset for the standard FDC clones,
                 * properly, so force a reset for the standard FDC clones,
                 * to avoid interrupt garbage.
                 * to avoid interrupt garbage.
                 */
                 */
                user_reset_fdc(-1,FD_RESET_ALWAYS,0);
                user_reset_fdc(-1,FD_RESET_ALWAYS,0);
#ifdef CONFIG_ARM
#ifdef CONFIG_ARM
                fd_scandrives();
                fd_scandrives();
#endif
#endif
        }
        }
        fdc=0;
        fdc=0;
        del_timer(&fd_timeout);
        del_timer(&fd_timeout);
        current_drive = 0;
        current_drive = 0;
        floppy_release_irq_and_dma();
        floppy_release_irq_and_dma();
        initialising=0;
        initialising=0;
        if (have_no_fdc) {
        if (have_no_fdc) {
                DPRINT("no floppy controllers found\n");
                DPRINT("no floppy controllers found\n");
                unregister_blkdev(MAJOR_NR,"fd");
                unregister_blkdev(MAJOR_NR,"fd");
        }
        }
        return have_no_fdc;
        return have_no_fdc;
}
}
 
 
static int floppy_grab_irq_and_dma(void)
static int floppy_grab_irq_and_dma(void)
{
{
        int i;
        int i;
        unsigned long flags;
        unsigned long flags;
 
 
        INT_OFF;
        INT_OFF;
        if (usage_count++){
        if (usage_count++){
                INT_ON;
                INT_ON;
                return 0;
                return 0;
        }
        }
        INT_ON;
        INT_ON;
        MOD_INC_USE_COUNT;
        MOD_INC_USE_COUNT;
        for (i=0; i< N_FDC; i++){
        for (i=0; i< N_FDC; i++){
                if (fdc_state[i].address != -1){
                if (fdc_state[i].address != -1){
                        fdc = i;
                        fdc = i;
                        reset_fdc_info(1);
                        reset_fdc_info(1);
                        fd_setdor(FDCS->dor);
                        fd_setdor(FDCS->dor);
                }
                }
        }
        }
        fdc = 0;
        fdc = 0;
        set_dor(0, ~0, 8);  /* avoid immediate interrupt */
        set_dor(0, ~0, 8);  /* avoid immediate interrupt */
 
 
        if (fd_request_irq()) {
        if (fd_request_irq()) {
                DPRINT("Unable to grab IRQ%d for the floppy driver\n",
                DPRINT("Unable to grab IRQ%d for the floppy driver\n",
                        FLOPPY_IRQ);
                        FLOPPY_IRQ);
                MOD_DEC_USE_COUNT;
                MOD_DEC_USE_COUNT;
                usage_count--;
                usage_count--;
                return -1;
                return -1;
        }
        }
        if (fd_request_dma()) {
        if (fd_request_dma()) {
                DPRINT("Unable to grab DMA%d for the floppy driver\n",
                DPRINT("Unable to grab DMA%d for the floppy driver\n",
                        FLOPPY_DMA);
                        FLOPPY_DMA);
                fd_free_irq();
                fd_free_irq();
                MOD_DEC_USE_COUNT;
                MOD_DEC_USE_COUNT;
                usage_count--;
                usage_count--;
                return -1;
                return -1;
        }
        }
        for (fdc = 0; fdc < N_FDC; fdc++)
        for (fdc = 0; fdc < N_FDC; fdc++)
                if (FDCS->address != -1)
                if (FDCS->address != -1)
                        fd_setdor(FDCS->dor);
                        fd_setdor(FDCS->dor);
        fdc = 0;
        fdc = 0;
        fd_enable_irq();
        fd_enable_irq();
        return 0;
        return 0;
}
}
 
 
static void floppy_release_irq_and_dma(void)
static void floppy_release_irq_and_dma(void)
{
{
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        int drive;
        int drive;
#endif
#endif
        long tmpsize;
        long tmpsize;
        unsigned long tmpaddr;
        unsigned long tmpaddr;
        unsigned long flags;
        unsigned long flags;
 
 
        INT_OFF;
        INT_OFF;
        if (--usage_count){
        if (--usage_count){
                INT_ON;
                INT_ON;
                return;
                return;
        }
        }
        INT_ON;
        INT_ON;
        fd_disable_dma();
        fd_disable_dma();
        fd_free_dma();
        fd_free_dma();
        fd_disable_irq();
        fd_disable_irq();
        fd_free_irq();
        fd_free_irq();
 
 
        set_dor(0, ~0, 8);
        set_dor(0, ~0, 8);
#if N_FDC > 1
#if N_FDC > 1
        set_dor(1, ~8, 0);
        set_dor(1, ~8, 0);
#endif
#endif
        floppy_enable_hlt();
        floppy_enable_hlt();
 
 
        if (floppy_track_buffer && max_buffer_sectors) {
        if (floppy_track_buffer && max_buffer_sectors) {
                tmpsize = max_buffer_sectors*1024;
                tmpsize = max_buffer_sectors*1024;
                tmpaddr = (unsigned long)floppy_track_buffer;
                tmpaddr = (unsigned long)floppy_track_buffer;
                floppy_track_buffer = NULL;
                floppy_track_buffer = NULL;
                max_buffer_sectors = 0;
                max_buffer_sectors = 0;
                buffer_min = buffer_max = -1;
                buffer_min = buffer_max = -1;
                fd_dma_mem_free(tmpaddr, tmpsize);
                fd_dma_mem_free(tmpaddr, tmpsize);
        }
        }
 
 
#ifdef FLOPPY_SANITY_CHECK
#ifdef FLOPPY_SANITY_CHECK
        for (drive=0; drive < N_FDC * 4; drive++)
        for (drive=0; drive < N_FDC * 4; drive++)
                if (motor_off_timer[drive].next)
                if (motor_off_timer[drive].next)
                        printk("motor off timer %d still active\n", drive);
                        printk("motor off timer %d still active\n", drive);
 
 
        if (fd_timeout.next)
        if (fd_timeout.next)
                printk("floppy timer still active:%s\n", timeout_message);
                printk("floppy timer still active:%s\n", timeout_message);
        if (fd_timer.next)
        if (fd_timer.next)
                printk("auxiliary floppy timer still active\n");
                printk("auxiliary floppy timer still active\n");
        if (floppy_tq.sync)
        if (floppy_tq.sync)
                printk("task queue still active\n");
                printk("task queue still active\n");
#endif
#endif
        MOD_DEC_USE_COUNT;
        MOD_DEC_USE_COUNT;
}
}
 
 
 
 
#ifdef MODULE
#ifdef MODULE
 
 
extern char *get_options(char *str, int *ints);
extern char *get_options(char *str, int *ints);
 
 
char *floppy=NULL;
char *floppy=NULL;
 
 
static void parse_floppy_cfg_string(char *cfg)
static void parse_floppy_cfg_string(char *cfg)
{
{
        char *ptr;
        char *ptr;
        int ints[11];
        int ints[11];
 
 
        while(*cfg) {
        while(*cfg) {
                for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
                for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
                if(*cfg) {
                if(*cfg) {
                        *cfg = '\0';
                        *cfg = '\0';
                        cfg++;
                        cfg++;
                }
                }
                if(*ptr)
                if(*ptr)
                        floppy_setup(get_options(ptr,ints),ints);
                        floppy_setup(get_options(ptr,ints),ints);
        }
        }
}
}
 
 
static void mod_setup(char *pattern, void (*setup)(char *, int *))
static void mod_setup(char *pattern, void (*setup)(char *, int *))
{
{
        unsigned long i;
        unsigned long i;
        char c;
        char c;
        int j;
        int j;
        int match;
        int match;
        char buffer[100];
        char buffer[100];
        int ints[11];
        int ints[11];
        int length = strlen(pattern)+1;
        int length = strlen(pattern)+1;
 
 
        match=0;
        match=0;
        j=1;
        j=1;
 
 
        for (i=current->mm->env_start; i< current->mm->env_end; i ++){
        for (i=current->mm->env_start; i< current->mm->env_end; i ++){
                c= get_fs_byte(i);
                c= get_fs_byte(i);
                if (match){
                if (match){
                        if (j==99)
                        if (j==99)
                                c='\0';
                                c='\0';
                        buffer[j] = c;
                        buffer[j] = c;
                        if (!c || c == ' ' || c == '\t'){
                        if (!c || c == ' ' || c == '\t'){
                                if (j){
                                if (j){
                                        buffer[j] = '\0';
                                        buffer[j] = '\0';
                                        setup(get_options(buffer,ints),ints);
                                        setup(get_options(buffer,ints),ints);
                                }
                                }
                                j=0;
                                j=0;
                        } else
                        } else
                                j++;
                                j++;
                        if (!c)
                        if (!c)
                                break;
                                break;
                        continue;
                        continue;
                }
                }
                if ((!j && !c) || (j && c == pattern[j-1]))
                if ((!j && !c) || (j && c == pattern[j-1]))
                        j++;
                        j++;
                else
                else
                        j=0;
                        j=0;
                if (j==length){
                if (j==length){
                        match=1;
                        match=1;
                        j=0;
                        j=0;
                }
                }
        }
        }
}
}
 
 
 
 
#ifdef __cplusplus
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
#endif
int init_module(void)
int init_module(void)
{
{
        printk(KERN_INFO "inserting floppy driver for %s\n", kernel_version);
        printk(KERN_INFO "inserting floppy driver for %s\n", kernel_version);
 
 
        if(floppy)
        if(floppy)
                parse_floppy_cfg_string(floppy);
                parse_floppy_cfg_string(floppy);
        else
        else
                mod_setup("floppy=", floppy_setup);
                mod_setup("floppy=", floppy_setup);
 
 
        return floppy_init();
        return floppy_init();
}
}
 
 
void cleanup_module(void)
void cleanup_module(void)
{
{
        int fdc, dummy;
        int fdc, dummy;
 
 
        for (fdc=0; fdc<2; fdc++)
        for (fdc=0; fdc<2; fdc++)
                if (FDCS->address != -1){
                if (FDCS->address != -1){
                        release_region(FDCS->address, 6);
                        release_region(FDCS->address, 6);
                        release_region(FDCS->address+7, 1);
                        release_region(FDCS->address+7, 1);
                }
                }
 
 
        unregister_blkdev(MAJOR_NR, "fd");
        unregister_blkdev(MAJOR_NR, "fd");
 
 
        blk_dev[MAJOR_NR].request_fn = 0;
        blk_dev[MAJOR_NR].request_fn = 0;
        /* eject disk, if any */
        /* eject disk, if any */
        dummy = fd_eject(0);
        dummy = fd_eject(0);
}
}
 
 
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
 
 
#else
#else
/* eject the boot floppy (if we need the drive for a different root floppy) */
/* eject the boot floppy (if we need the drive for a different root floppy) */
/* This should only be called at boot time when we're sure that there's no
/* This should only be called at boot time when we're sure that there's no
 * resource contention. */
 * resource contention. */
void floppy_eject(void)
void floppy_eject(void)
{
{
        int dummy;
        int dummy;
        floppy_grab_irq_and_dma();
        floppy_grab_irq_and_dma();
        lock_fdc(MAXTIMEOUT,0);
        lock_fdc(MAXTIMEOUT,0);
        dummy=fd_eject(0);
        dummy=fd_eject(0);
        process_fd_request();
        process_fd_request();
        floppy_release_irq_and_dma();
        floppy_release_irq_and_dma();
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.