File Index
Files and directories
Sign in
FILE

ataflop.c

File preview with syntax highlighting.

linux-6.19.14/drivers/block/ataflop.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  drivers/block/ataflop.c
   4 *
   5 *  Copyright (C) 1993  Greg Harp
   6 *  Atari Support by Bjoern Brauel, Roman Hodek
   7 *
   8 *  Big cleanup Sep 11..14 1994 Roman Hodek:
   9 *   - Driver now works interrupt driven
  10 *   - Support for two drives; should work, but I cannot test that :-(
  11 *   - Reading is done in whole tracks and buffered to speed up things
  12 *   - Disk change detection and drive deselecting after motor-off
  13 *     similar to TOS
  14 *   - Autodetection of disk format (DD/HD); untested yet, because I
  15 *     don't have an HD drive :-(
  16 *
  17 *  Fixes Nov 13 1994 Martin Schaller:
  18 *   - Autodetection works now
  19 *   - Support for 5 1/4'' disks
  20 *   - Removed drive type (unknown on atari)
  21 *   - Do seeks with 8 Mhz
  22 *
  23 *  Changes by Andreas Schwab:
  24 *   - After errors in multiple read mode try again reading single sectors
  25 *  (Feb 1995):
  26 *   - Clean up error handling
  27 *   - Set blk_size for proper size checking
  28 *   - Initialize track register when testing presence of floppy
  29 *   - Implement some ioctl's
  30 *
  31 *  Changes by Torsten Lang:
  32 *   - When probing the floppies we should add the FDCCMDADD_H flag since
  33 *     the FDC will otherwise wait forever when no disk is inserted...
  34 *
  35 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
  36 *  - MFPDELAY() after each FDC access -> atari 
  37 *  - more/other disk formats
  38 *  - DMA to the block buffer directly if we have a 32bit DMA
  39 *  - for medusa, the step rate is always 3ms
  40 *  - on medusa, use only cache_push()
  41 * Roman:
  42 *  - Make disk format numbering independent from minors
  43 *  - Let user set max. supported drive type (speeds up format
  44 *    detection, saves buffer space)
  45 *
  46 * Roman 10/15/95:
  47 *  - implement some more ioctls
  48 *  - disk formatting
  49 *  
  50 * Andreas 95/12/12:
  51 *  - increase gap size at start of track for HD/ED disks
  52 *
  53 * Michael (MSch) 11/07/96:
  54 *  - implemented FDSETPRM and FDDEFPRM ioctl
  55 *
  56 * Andreas (97/03/19):
  57 *  - implemented missing BLK* ioctls
  58 *
  59 *  Things left to do:
  60 *   - Formatting
  61 *   - Maybe a better strategy for disk change detection (does anyone
  62 *     know one?)
  63 */
  64
  65#include <linux/module.h>
  66
  67#include <linux/fd.h>
  68#include <linux/delay.h>
  69#include <linux/init.h>
  70#include <linux/blk-mq.h>
  71#include <linux/major.h>
  72#include <linux/mutex.h>
  73#include <linux/completion.h>
  74#include <linux/wait.h>
  75
  76#include <asm/atariints.h>
  77#include <asm/atari_stdma.h>
  78#include <asm/atari_stram.h>
  79
  80#define	FD_MAX_UNITS 2
  81
  82#undef DEBUG
  83
  84static DEFINE_MUTEX(ataflop_mutex);
  85static struct request *fd_request;
  86
  87/*
  88 * WD1772 stuff
  89 */
  90
  91/* register codes */
  92
  93#define FDCSELREG_STP   (0x80)   /* command/status register */
  94#define FDCSELREG_TRA   (0x82)   /* track register */
  95#define FDCSELREG_SEC   (0x84)   /* sector register */
  96#define FDCSELREG_DTA   (0x86)   /* data register */
  97
  98/* register names for FDC_READ/WRITE macros */
  99
 100#define FDCREG_CMD		0
 101#define FDCREG_STATUS	0
 102#define FDCREG_TRACK	2
 103#define FDCREG_SECTOR	4
 104#define FDCREG_DATA		6
 105
 106/* command opcodes */
 107
 108#define FDCCMD_RESTORE  (0x00)   /*  -                   */
 109#define FDCCMD_SEEK     (0x10)   /*   |                  */
 110#define FDCCMD_STEP     (0x20)   /*   |  TYP 1 Commands  */
 111#define FDCCMD_STIN     (0x40)   /*   |                  */
 112#define FDCCMD_STOT     (0x60)   /*  -                   */
 113#define FDCCMD_RDSEC    (0x80)   /*  -   TYP 2 Commands  */
 114#define FDCCMD_WRSEC    (0xa0)   /*  -          "        */
 115#define FDCCMD_RDADR    (0xc0)   /*  -                   */
 116#define FDCCMD_RDTRA    (0xe0)   /*   |  TYP 3 Commands  */
 117#define FDCCMD_WRTRA    (0xf0)   /*  -                   */
 118#define FDCCMD_FORCI    (0xd0)   /*  -   TYP 4 Command   */
 119
 120/* command modifier bits */
 121
 122#define FDCCMDADD_SR6   (0x00)   /* step rate settings */
 123#define FDCCMDADD_SR12  (0x01)
 124#define FDCCMDADD_SR2   (0x02)
 125#define FDCCMDADD_SR3   (0x03)
 126#define FDCCMDADD_V     (0x04)   /* verify */
 127#define FDCCMDADD_H     (0x08)   /* wait for spin-up */
 128#define FDCCMDADD_U     (0x10)   /* update track register */
 129#define FDCCMDADD_M     (0x10)   /* multiple sector access */
 130#define FDCCMDADD_E     (0x04)   /* head settling flag */
 131#define FDCCMDADD_P     (0x02)   /* precompensation off */
 132#define FDCCMDADD_A0    (0x01)   /* DAM flag */
 133
 134/* status register bits */
 135
 136#define	FDCSTAT_MOTORON	(0x80)   /* motor on */
 137#define	FDCSTAT_WPROT	(0x40)   /* write protected (FDCCMD_WR*) */
 138#define	FDCSTAT_SPINUP	(0x20)   /* motor speed stable (Type I) */
 139#define	FDCSTAT_DELDAM	(0x20)   /* sector has deleted DAM (Type II+III) */
 140#define	FDCSTAT_RECNF	(0x10)   /* record not found */
 141#define	FDCSTAT_CRC		(0x08)   /* CRC error */
 142#define	FDCSTAT_TR00	(0x04)   /* Track 00 flag (Type I) */
 143#define	FDCSTAT_LOST	(0x04)   /* Lost Data (Type II+III) */
 144#define	FDCSTAT_IDX		(0x02)   /* Index status (Type I) */
 145#define	FDCSTAT_DRQ		(0x02)   /* DRQ status (Type II+III) */
 146#define	FDCSTAT_BUSY	(0x01)   /* FDC is busy */
 147
 148
 149/* PSG Port A Bit Nr 0 .. Side Sel .. 0 -> Side 1  1 -> Side 2 */
 150#define DSKSIDE     (0x01)
 151
 152#define DSKDRVNONE  (0x06)
 153#define DSKDRV0     (0x02)
 154#define DSKDRV1     (0x04)
 155
 156/* step rates */
 157#define	FDCSTEP_6	0x00
 158#define	FDCSTEP_12	0x01
 159#define	FDCSTEP_2	0x02
 160#define	FDCSTEP_3	0x03
 161
 162struct atari_format_descr {
 163	int track;		/* to be formatted */
 164	int head;		/*   ""     ""     */
 165	int sect_offset;	/* offset of first sector */
 166};
 167
 168/* Disk types: DD, HD, ED */
 169static struct atari_disk_type {
 170	const char	*name;
 171	unsigned	spt;		/* sectors per track */
 172	unsigned	blocks;		/* total number of blocks */
 173	unsigned	fdc_speed;	/* fdc_speed setting */
 174	unsigned 	stretch;	/* track doubling ? */
 175} atari_disk_type[] = {
 176	{ "d360",  9, 720, 0, 0},	/*  0: 360kB diskette */
 177	{ "D360",  9, 720, 0, 1},	/*  1: 360kb in 720k or 1.2MB drive */
 178	{ "D720",  9,1440, 0, 0},	/*  2: 720kb in 720k or 1.2MB drive */
 179	{ "D820", 10,1640, 0, 0},	/*  3: DD disk with 82 tracks/10 sectors */
 180/* formats above are probed for type DD */
 181#define	MAX_TYPE_DD 3
 182	{ "h1200",15,2400, 3, 0},	/*  4: 1.2MB diskette */
 183	{ "H1440",18,2880, 3, 0},	/*  5: 1.4 MB diskette (HD) */
 184	{ "H1640",20,3280, 3, 0},	/*  6: 1.64MB diskette (fat HD) 82 tr 20 sec */
 185/* formats above are probed for types DD and HD */
 186#define	MAX_TYPE_HD 6
 187	{ "E2880",36,5760, 3, 0},	/*  7: 2.8 MB diskette (ED) */
 188	{ "E3280",40,6560, 3, 0},	/*  8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
 189/* formats above are probed for types DD, HD and ED */
 190#define	MAX_TYPE_ED 8
 191/* types below are never autoprobed */
 192	{ "H1680",21,3360, 3, 0},	/*  9: 1.68MB diskette (fat HD) 80 tr 21 sec */
 193	{ "h410",10,820, 0, 1},		/* 10: 410k diskette 41 tr 10 sec, stretch */
 194	{ "h1476",18,2952, 3, 0},	/* 11: 1.48MB diskette 82 tr 18 sec */
 195	{ "H1722",21,3444, 3, 0},	/* 12: 1.72MB diskette 82 tr 21 sec */
 196	{ "h420",10,840, 0, 1},		/* 13: 420k diskette 42 tr 10 sec, stretch */
 197	{ "H830",10,1660, 0, 0},	/* 14: 820k diskette 83 tr 10 sec */
 198	{ "h1494",18,2952, 3, 0},	/* 15: 1.49MB diskette 83 tr 18 sec */
 199	{ "H1743",21,3486, 3, 0},	/* 16: 1.74MB diskette 83 tr 21 sec */
 200	{ "h880",11,1760, 0, 0},	/* 17: 880k diskette 80 tr 11 sec */
 201	{ "D1040",13,2080, 0, 0},	/* 18: 1.04MB diskette 80 tr 13 sec */
 202	{ "D1120",14,2240, 0, 0},	/* 19: 1.12MB diskette 80 tr 14 sec */
 203	{ "h1600",20,3200, 3, 0},	/* 20: 1.60MB diskette 80 tr 20 sec */
 204	{ "H1760",22,3520, 3, 0},	/* 21: 1.76MB diskette 80 tr 22 sec */
 205	{ "H1920",24,3840, 3, 0},	/* 22: 1.92MB diskette 80 tr 24 sec */
 206	{ "E3200",40,6400, 3, 0},	/* 23: 3.2MB diskette 80 tr 40 sec */
 207	{ "E3520",44,7040, 3, 0},	/* 24: 3.52MB diskette 80 tr 44 sec */
 208	{ "E3840",48,7680, 3, 0},	/* 25: 3.84MB diskette 80 tr 48 sec */
 209	{ "H1840",23,3680, 3, 0},	/* 26: 1.84MB diskette 80 tr 23 sec */
 210	{ "D800",10,1600, 0, 0},	/* 27: 800k diskette 80 tr 10 sec */
 211};
 212
 213static int StartDiskType[] = {
 214	MAX_TYPE_DD,
 215	MAX_TYPE_HD,
 216	MAX_TYPE_ED
 217};
 218
 219#define	TYPE_DD		0
 220#define	TYPE_HD		1
 221#define	TYPE_ED		2
 222
 223static int DriveType = TYPE_HD;
 224
 225static DEFINE_SPINLOCK(ataflop_lock);
 226
 227/* Array for translating minors into disk formats */
 228static struct {
 229	int 	 index;
 230	unsigned drive_types;
 231} minor2disktype[] = {
 232	{  0, TYPE_DD },	/*  1: d360 */
 233	{  4, TYPE_HD },	/*  2: h1200 */
 234	{  1, TYPE_DD },	/*  3: D360 */
 235	{  2, TYPE_DD },	/*  4: D720 */
 236	{  1, TYPE_DD },	/*  5: h360 = D360 */
 237	{  2, TYPE_DD },	/*  6: h720 = D720 */
 238	{  5, TYPE_HD },	/*  7: H1440 */
 239	{  7, TYPE_ED },	/*  8: E2880 */
 240/* some PC formats :-) */
 241	{  8, TYPE_ED },	/*  9: E3280    <- was "CompaQ" == E2880 for PC */
 242	{  5, TYPE_HD },	/* 10: h1440 = H1440 */
 243	{  9, TYPE_HD },	/* 11: H1680 */
 244	{ 10, TYPE_DD },	/* 12: h410  */
 245	{  3, TYPE_DD },	/* 13: H820     <- == D820, 82x10 */
 246	{ 11, TYPE_HD },	/* 14: h1476 */
 247	{ 12, TYPE_HD },	/* 15: H1722 */
 248	{ 13, TYPE_DD },	/* 16: h420  */
 249	{ 14, TYPE_DD },	/* 17: H830  */
 250	{ 15, TYPE_HD },	/* 18: h1494 */
 251	{ 16, TYPE_HD },	/* 19: H1743 */
 252	{ 17, TYPE_DD },	/* 20: h880  */
 253	{ 18, TYPE_DD },	/* 21: D1040 */
 254	{ 19, TYPE_DD },	/* 22: D1120 */
 255	{ 20, TYPE_HD },	/* 23: h1600 */
 256	{ 21, TYPE_HD },	/* 24: H1760 */
 257	{ 22, TYPE_HD },	/* 25: H1920 */
 258	{ 23, TYPE_ED },	/* 26: E3200 */
 259	{ 24, TYPE_ED },	/* 27: E3520 */
 260	{ 25, TYPE_ED },	/* 28: E3840 */
 261	{ 26, TYPE_HD },	/* 29: H1840 */
 262	{ 27, TYPE_DD },	/* 30: D800  */
 263	{  6, TYPE_HD },	/* 31: H1640    <- was H1600 == h1600 for PC */
 264};
 265
 266#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
 267
 268/*
 269 * Maximum disk size (in kilobytes). This default is used whenever the
 270 * current disk size is unknown.
 271 */
 272#define MAX_DISK_SIZE 3280
 273
 274/*
 275 * MSch: User-provided type information. 'drive' points to
 276 * the respective entry of this array. Set by FDSETPRM ioctls.
 277 */
 278static struct atari_disk_type user_params[FD_MAX_UNITS];
 279
 280/*
 281 * User-provided permanent type information. 'drive' points to
 282 * the respective entry of this array.  Set by FDDEFPRM ioctls, 
 283 * restored upon disk change by floppy_revalidate() if valid (as seen by
 284 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
 285 */
 286static struct atari_disk_type default_params[FD_MAX_UNITS];
 287
 288/* current info on each unit */
 289static struct atari_floppy_struct {
 290	int connected;				/* !=0 : drive is connected */
 291	int autoprobe;				/* !=0 : do autoprobe	    */
 292
 293	struct atari_disk_type	*disktype;	/* current type of disk */
 294
 295	int track;		/* current head position or -1 if
 296				   unknown */
 297	unsigned int steprate;	/* steprate setting */
 298	unsigned int wpstat;	/* current state of WP signal (for
 299				   disk change detection) */
 300	int flags;		/* flags */
 301	struct gendisk *disk[NUM_DISK_MINORS];
 302	bool registered[NUM_DISK_MINORS];
 303	int ref;
 304	int type;
 305	struct blk_mq_tag_set tag_set;
 306	int error_count;
 307} unit[FD_MAX_UNITS];
 308
 309#define	UD	unit[drive]
 310#define	UDT	unit[drive].disktype
 311#define	SUD	unit[SelectedDrive]
 312#define	SUDT	unit[SelectedDrive].disktype
 313
 314
 315#define FDC_READ(reg) ({			\
 316    /* unsigned long __flags; */		\
 317    unsigned short __val;			\
 318    /* local_irq_save(__flags); */		\
 319    dma_wd.dma_mode_status = 0x80 | (reg);	\
 320    udelay(25);					\
 321    __val = dma_wd.fdc_acces_seccount;		\
 322    MFPDELAY();					\
 323    /* local_irq_restore(__flags); */		\
 324    __val & 0xff;				\
 325})
 326
 327#define FDC_WRITE(reg,val)			\
 328    do {					\
 329	/* unsigned long __flags; */		\
 330	/* local_irq_save(__flags); */		\
 331	dma_wd.dma_mode_status = 0x80 | (reg);	\
 332	udelay(25);				\
 333	dma_wd.fdc_acces_seccount = (val);	\
 334	MFPDELAY();				\
 335        /* local_irq_restore(__flags); */	\
 336    } while(0)
 337
 338
 339/* Buffering variables:
 340 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
 341 * operations. Second, a track buffer is used to cache a whole track
 342 * of the disk to save read operations. These are two separate buffers
 343 * because that allows write operations without clearing the track buffer.
 344 */
 345
 346static int MaxSectors[] = {
 347	11, 22, 44
 348};
 349static int BufferSize[] = {
 350	15*512, 30*512, 60*512
 351};
 352
 353#define	BUFFER_SIZE	(BufferSize[DriveType])
 354
 355unsigned char *DMABuffer;			  /* buffer for writes */
 356static unsigned long PhysDMABuffer;   /* physical address */
 357
 358static int UseTrackbuffer = -1;		  /* Do track buffering? */
 359module_param(UseTrackbuffer, int, 0);
 360
 361unsigned char *TrackBuffer;			  /* buffer for reads */
 362static unsigned long PhysTrackBuffer; /* physical address */
 363static int BufferDrive, BufferSide, BufferTrack;
 364static int read_track;		/* non-zero if we are reading whole tracks */
 365
 366#define	SECTOR_BUFFER(sec)	(TrackBuffer + ((sec)-1)*512)
 367#define	IS_BUFFERED(drive,side,track) \
 368    (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
 369
 370/*
 371 * These are global variables, as that's the easiest way to give
 372 * information to interrupts. They are the data used for the current
 373 * request.
 374 */
 375static int SelectedDrive = 0;
 376static int ReqCmd, ReqBlock;
 377static int ReqSide, ReqTrack, ReqSector, ReqCnt;
 378static int HeadSettleFlag = 0;
 379static unsigned char *ReqData, *ReqBuffer;
 380static int MotorOn = 0, MotorOffTrys;
 381static int IsFormatting = 0, FormatError;
 382
 383static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
 384module_param_array(UserSteprate, int, NULL, 0);
 385
 386static DECLARE_COMPLETION(format_wait);
 387
 388static unsigned long changed_floppies = 0xff, fake_change = 0;
 389#define	CHECK_CHANGE_DELAY	HZ/2
 390
 391#define	FD_MOTOR_OFF_DELAY	(3*HZ)
 392#define	FD_MOTOR_OFF_MAXTRY	(10*20)
 393
 394#define FLOPPY_TIMEOUT		(6*HZ)
 395#define RECALIBRATE_ERRORS	4	/* After this many errors the drive
 396					 * will be recalibrated. */
 397#define MAX_ERRORS		8	/* After this many errors the driver
 398					 * will give up. */
 399
 400
 401/*
 402 * The driver is trying to determine the correct media format
 403 * while Probing is set. fd_rwsec_done() clears it after a
 404 * successful access.
 405 */
 406static int Probing = 0;
 407
 408/* This flag is set when a dummy seek is necessary to make the WP
 409 * status bit accessible.
 410 */
 411static int NeedSeek = 0;
 412
 413
 414#ifdef DEBUG
 415#define DPRINT(a)	printk a
 416#else
 417#define DPRINT(a)
 418#endif
 419
 420/***************************** Prototypes *****************************/
 421
 422static void fd_select_side( int side );
 423static void fd_select_drive( int drive );
 424static void fd_deselect( void );
 425static void fd_motor_off_timer(struct timer_list *unused);
 426static void check_change(struct timer_list *unused);
 427static irqreturn_t floppy_irq (int irq, void *dummy);
 428static void fd_error( void );
 429static int do_format(int drive, int type, struct atari_format_descr *desc);
 430static void do_fd_action( int drive );
 431static void fd_calibrate( void );
 432static void fd_calibrate_done( int status );
 433static void fd_seek( void );
 434static void fd_seek_done( int status );
 435static void fd_rwsec( void );
 436static void fd_readtrack_check(struct timer_list *unused);
 437static void fd_rwsec_done( int status );
 438static void fd_rwsec_done1(int status);
 439static void fd_writetrack( void );
 440static void fd_writetrack_done( int status );
 441static void fd_times_out(struct timer_list *unused);
 442static void finish_fdc( void );
 443static void finish_fdc_done( int dummy );
 444static void setup_req_params( int drive );
 445static int fd_locked_ioctl(struct block_device *bdev, blk_mode_t mode,
 446		unsigned int cmd, unsigned long param);
 447static void fd_probe( int drive );
 448static int fd_test_drive_present( int drive );
 449static void config_types( void );
 450static int floppy_open(struct gendisk *disk, blk_mode_t mode);
 451static void floppy_release(struct gendisk *disk);
 452
 453/************************* End of Prototypes **************************/
 454
 455static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer);
 456static DEFINE_TIMER(readtrack_timer, fd_readtrack_check);
 457static DEFINE_TIMER(timeout_timer, fd_times_out);
 458static DEFINE_TIMER(fd_timer, check_change);
 459	
 460static void fd_end_request_cur(blk_status_t err)
 461{
 462	DPRINT(("fd_end_request_cur(), bytes %d of %d\n",
 463		blk_rq_cur_bytes(fd_request),
 464		blk_rq_bytes(fd_request)));
 465
 466	if (!blk_update_request(fd_request, err,
 467				blk_rq_cur_bytes(fd_request))) {
 468		DPRINT(("calling __blk_mq_end_request()\n"));
 469		__blk_mq_end_request(fd_request, err);
 470		fd_request = NULL;
 471	} else {
 472		/* requeue rest of request */
 473		DPRINT(("calling blk_mq_requeue_request()\n"));
 474		blk_mq_requeue_request(fd_request, true);
 475		fd_request = NULL;
 476	}
 477}
 478
 479static inline void start_motor_off_timer(void)
 480{
 481	mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
 482	MotorOffTrys = 0;
 483}
 484
 485static inline void start_check_change_timer( void )
 486{
 487	mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
 488}
 489
 490static inline void start_timeout(void)
 491{
 492	mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
 493}
 494
 495static inline void stop_timeout(void)
 496{
 497	timer_delete(&timeout_timer);
 498}
 499
 500/* Select the side to use. */
 501
 502static void fd_select_side( int side )
 503{
 504	unsigned long flags;
 505
 506	/* protect against various other ints mucking around with the PSG */
 507	local_irq_save(flags);
 508  
 509	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
 510	sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
 511	                                 sound_ym.rd_data_reg_sel & 0xfe;
 512
 513	local_irq_restore(flags);
 514}
 515
 516
 517/* Select a drive, update the FDC's track register and set the correct
 518 * clock speed for this disk's type.
 519 */
 520
 521static void fd_select_drive( int drive )
 522{
 523	unsigned long flags;
 524	unsigned char tmp;
 525  
 526	if (drive == SelectedDrive)
 527	  return;
 528
 529	/* protect against various other ints mucking around with the PSG */
 530	local_irq_save(flags);
 531	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
 532	tmp = sound_ym.rd_data_reg_sel;
 533	sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
 534	atari_dont_touch_floppy_select = 1;
 535	local_irq_restore(flags);
 536
 537	/* restore track register to saved value */
 538	FDC_WRITE( FDCREG_TRACK, UD.track );
 539	udelay(25);
 540
 541	/* select 8/16 MHz */
 542	if (UDT)
 543		if (ATARIHW_PRESENT(FDCSPEED))
 544			dma_wd.fdc_speed = UDT->fdc_speed;
 545	
 546	SelectedDrive = drive;
 547}
 548
 549
 550/* Deselect both drives. */
 551
 552static void fd_deselect( void )
 553{
 554	unsigned long flags;
 555
 556	/* protect against various other ints mucking around with the PSG */
 557	local_irq_save(flags);
 558	atari_dont_touch_floppy_select = 0;
 559	sound_ym.rd_data_reg_sel=14;	/* Select PSG Port A */
 560	sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
 561			    (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
 562	/* On Falcon, the drive B select line is used on the printer port, so
 563	 * leave it alone... */
 564	SelectedDrive = -1;
 565	local_irq_restore(flags);
 566}
 567
 568
 569/* This timer function deselects the drives when the FDC switched the
 570 * motor off. The deselection cannot happen earlier because the FDC
 571 * counts the index signals, which arrive only if one drive is selected.
 572 */
 573
 574static void fd_motor_off_timer(struct timer_list *unused)
 575{
 576	unsigned char status;
 577
 578	if (SelectedDrive < 0)
 579		/* no drive selected, needn't deselect anyone */
 580		return;
 581
 582	if (stdma_islocked())
 583		goto retry;
 584
 585	status = FDC_READ( FDCREG_STATUS );
 586
 587	if (!(status & 0x80)) {
 588		/* motor already turned off by FDC -> deselect drives */
 589		MotorOn = 0;
 590		fd_deselect();
 591		return;
 592	}
 593	/* not yet off, try again */
 594
 595  retry:
 596	/* Test again later; if tested too often, it seems there is no disk
 597	 * in the drive and the FDC will leave the motor on forever (or,
 598	 * at least until a disk is inserted). So we'll test only twice
 599	 * per second from then on...
 600	 */
 601	mod_timer(&motor_off_timer,
 602		  jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
 603}
 604
 605
 606/* This function is repeatedly called to detect disk changes (as good
 607 * as possible) and keep track of the current state of the write protection.
 608 */
 609
 610static void check_change(struct timer_list *unused)
 611{
 612	static int    drive = 0;
 613
 614	unsigned long flags;
 615	unsigned char old_porta;
 616	int			  stat;
 617
 618	if (++drive > 1 || !UD.connected)
 619		drive = 0;
 620
 621	/* protect against various other ints mucking around with the PSG */
 622	local_irq_save(flags);
 623
 624	if (!stdma_islocked()) {
 625		sound_ym.rd_data_reg_sel = 14;
 626		old_porta = sound_ym.rd_data_reg_sel;
 627		sound_ym.wd_data = (old_porta | DSKDRVNONE) &
 628			               ~(drive == 0 ? DSKDRV0 : DSKDRV1);
 629		stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
 630		sound_ym.wd_data = old_porta;
 631
 632		if (stat != UD.wpstat) {
 633			DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
 634			UD.wpstat = stat;
 635			set_bit (drive, &changed_floppies);
 636		}
 637	}
 638	local_irq_restore(flags);
 639
 640	start_check_change_timer();
 641}
 642
 643 
 644/* Handling of the Head Settling Flag: This flag should be set after each
 645 * seek operation, because we don't use seeks with verify.
 646 */
 647
 648static inline void set_head_settle_flag(void)
 649{
 650	HeadSettleFlag = FDCCMDADD_E;
 651}
 652
 653static inline int get_head_settle_flag(void)
 654{
 655	int	tmp = HeadSettleFlag;
 656	HeadSettleFlag = 0;
 657	return( tmp );
 658}
 659
 660static inline void copy_buffer(void *from, void *to)
 661{
 662	ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
 663	int cnt;
 664
 665	for (cnt = 512/4; cnt; cnt--)
 666		*p2++ = *p1++;
 667}
 668
 669/* General Interrupt Handling */
 670
 671static void (*FloppyIRQHandler)( int status ) = NULL;
 672
 673static irqreturn_t floppy_irq (int irq, void *dummy)
 674{
 675	unsigned char status;
 676	void (*handler)( int );
 677
 678	handler = xchg(&FloppyIRQHandler, NULL);
 679
 680	if (handler) {
 681		nop();
 682		status = FDC_READ( FDCREG_STATUS );
 683		DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
 684		handler( status );
 685	}
 686	else {
 687		DPRINT(("FDC irq, no handler\n"));
 688	}
 689	return IRQ_HANDLED;
 690}
 691
 692
 693/* Error handling: If some error happened, retry some times, then
 694 * recalibrate, then try again, and fail after MAX_ERRORS.
 695 */
 696
 697static void fd_error( void )
 698{
 699	if (IsFormatting) {
 700		IsFormatting = 0;
 701		FormatError = 1;
 702		complete(&format_wait);
 703		return;
 704	}
 705
 706	if (!fd_request)
 707		return;
 708
 709	unit[SelectedDrive].error_count++;
 710	if (unit[SelectedDrive].error_count >= MAX_ERRORS) {
 711		printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
 712		fd_end_request_cur(BLK_STS_IOERR);
 713		finish_fdc();
 714		return;
 715	}
 716	else if (unit[SelectedDrive].error_count == RECALIBRATE_ERRORS) {
 717		printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
 718		if (SelectedDrive != -1)
 719			SUD.track = -1;
 720	}
 721	/* need to re-run request to recalibrate */
 722	atari_disable_irq( IRQ_MFP_FDC );
 723
 724	setup_req_params( SelectedDrive );
 725	do_fd_action( SelectedDrive );
 726
 727	atari_enable_irq( IRQ_MFP_FDC );
 728}
 729
 730
 731
 732#define	SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
 733
 734
 735/* ---------- Formatting ---------- */
 736
 737#define FILL(n,val)		\
 738    do {			\
 739	memset( p, val, n );	\
 740	p += n;			\
 741    } while(0)
 742
 743static int do_format(int drive, int type, struct atari_format_descr *desc)
 744{
 745	struct request_queue *q;
 746	unsigned char	*p;
 747	int sect, nsect;
 748	unsigned long	flags;
 749	unsigned int memflags;
 750	int ret;
 751
 752	if (type) {
 753		type--;
 754		if (type >= NUM_DISK_MINORS ||
 755		    minor2disktype[type].drive_types > DriveType) {
 756			finish_fdc();
 757			return -EINVAL;
 758		}
 759	}
 760
 761	q = unit[drive].disk[type]->queue;
 762	memflags = blk_mq_freeze_queue(q);
 763	blk_mq_quiesce_queue(q);
 764
 765	local_irq_save(flags);
 766	stdma_lock(floppy_irq, NULL);
 767	atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
 768	local_irq_restore(flags);
 769
 770	if (type) {
 771		type = minor2disktype[type].index;
 772		UDT = &atari_disk_type[type];
 773	}
 774
 775	if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
 776		finish_fdc();
 777		ret = -EINVAL;
 778		goto out;
 779	}
 780
 781	nsect = UDT->spt;
 782	p = TrackBuffer;
 783	/* The track buffer is used for the raw track data, so its
 784	   contents become invalid! */
 785	BufferDrive = -1;
 786	/* stop deselect timer */
 787	timer_delete(&motor_off_timer);
 788
 789	FILL( 60 * (nsect / 9), 0x4e );
 790	for( sect = 0; sect < nsect; ++sect ) {
 791		FILL( 12, 0 );
 792		FILL( 3, 0xf5 );
 793		*p++ = 0xfe;
 794		*p++ = desc->track;
 795		*p++ = desc->head;
 796		*p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
 797		*p++ = 2;
 798		*p++ = 0xf7;
 799		FILL( 22, 0x4e );
 800		FILL( 12, 0 );
 801		FILL( 3, 0xf5 );
 802		*p++ = 0xfb;
 803		FILL( 512, 0xe5 );
 804		*p++ = 0xf7;
 805		FILL( 40, 0x4e );
 806	}
 807	FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
 808
 809	IsFormatting = 1;
 810	FormatError = 0;
 811	ReqTrack = desc->track;
 812	ReqSide  = desc->head;
 813	do_fd_action( drive );
 814
 815	wait_for_completion(&format_wait);
 816
 817	finish_fdc();
 818	ret = FormatError ? -EIO : 0;
 819out:
 820	blk_mq_unquiesce_queue(q);
 821	blk_mq_unfreeze_queue(q, memflags);
 822	return ret;
 823}
 824
 825
 826/* do_fd_action() is the general procedure for a fd request: All
 827 * required parameter settings (drive select, side select, track
 828 * position) are checked and set if needed. For each of these
 829 * parameters and the actual reading or writing exist two functions:
 830 * one that starts the setting (or skips it if possible) and one
 831 * callback for the "done" interrupt. Each done func calls the next
 832 * set function to propagate the request down to fd_rwsec_done().
 833 */
 834
 835static void do_fd_action( int drive )
 836{
 837	DPRINT(("do_fd_action\n"));
 838	
 839	if (UseTrackbuffer && !IsFormatting) {
 840	repeat:
 841	    if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
 842		if (ReqCmd == READ) {
 843		    copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
 844		    if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
 845			/* read next sector */
 846			setup_req_params( drive );
 847			goto repeat;
 848		    }
 849		    else {
 850			/* all sectors finished */
 851			fd_end_request_cur(BLK_STS_OK);
 852			finish_fdc();
 853			return;
 854		    }
 855		}
 856		else {
 857		    /* cmd == WRITE, pay attention to track buffer
 858		     * consistency! */
 859		    copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
 860		}
 861	    }
 862	}
 863
 864	if (SelectedDrive != drive)
 865		fd_select_drive( drive );
 866    
 867	if (UD.track == -1)
 868		fd_calibrate();
 869	else if (UD.track != ReqTrack << UDT->stretch)
 870		fd_seek();
 871	else if (IsFormatting)
 872		fd_writetrack();
 873	else
 874		fd_rwsec();
 875}
 876
 877
 878/* Seek to track 0 if the current track is unknown */
 879
 880static void fd_calibrate( void )
 881{
 882	if (SUD.track >= 0) {
 883		fd_calibrate_done( 0 );
 884		return;
 885	}
 886
 887	if (ATARIHW_PRESENT(FDCSPEED))
 888		dma_wd.fdc_speed = 0;   /* always seek with 8 Mhz */
 889	DPRINT(("fd_calibrate\n"));
 890	SET_IRQ_HANDLER( fd_calibrate_done );
 891	/* we can't verify, since the speed may be incorrect */
 892	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
 893
 894	NeedSeek = 1;
 895	MotorOn = 1;
 896	start_timeout();
 897	/* wait for IRQ */
 898}
 899
 900
 901static void fd_calibrate_done( int status )
 902{
 903	DPRINT(("fd_calibrate_done()\n"));
 904	stop_timeout();
 905    
 906	/* set the correct speed now */
 907	if (ATARIHW_PRESENT(FDCSPEED))
 908		dma_wd.fdc_speed = SUDT->fdc_speed;
 909	if (status & FDCSTAT_RECNF) {
 910		printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
 911		fd_error();
 912	}
 913	else {
 914		SUD.track = 0;
 915		fd_seek();
 916	}
 917}
 918  
 919  
 920/* Seek the drive to the requested track. The drive must have been
 921 * calibrated at some point before this.
 922 */
 923  
 924static void fd_seek( void )
 925{
 926	if (SUD.track == ReqTrack << SUDT->stretch) {
 927		fd_seek_done( 0 );
 928		return;
 929	}
 930
 931	if (ATARIHW_PRESENT(FDCSPEED)) {
 932		dma_wd.fdc_speed = 0;	/* always seek witch 8 Mhz */
 933		MFPDELAY();
 934	}
 935
 936	DPRINT(("fd_seek() to track %d\n",ReqTrack));
 937	FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
 938	udelay(25);
 939	SET_IRQ_HANDLER( fd_seek_done );
 940	FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
 941
 942	MotorOn = 1;
 943	set_head_settle_flag();
 944	start_timeout();
 945	/* wait for IRQ */
 946}
 947
 948
 949static void fd_seek_done( int status )
 950{
 951	DPRINT(("fd_seek_done()\n"));
 952	stop_timeout();
 953	
 954	/* set the correct speed */
 955	if (ATARIHW_PRESENT(FDCSPEED))
 956		dma_wd.fdc_speed = SUDT->fdc_speed;
 957	if (status & FDCSTAT_RECNF) {
 958		printk(KERN_ERR "fd%d: seek error (to track %d)\n",
 959				SelectedDrive, ReqTrack );
 960		/* we don't know exactly which track we are on now! */
 961		SUD.track = -1;
 962		fd_error();
 963	}
 964	else {
 965		SUD.track = ReqTrack << SUDT->stretch;
 966		NeedSeek = 0;
 967		if (IsFormatting)
 968			fd_writetrack();
 969		else
 970			fd_rwsec();
 971	}
 972}
 973
 974
 975/* This does the actual reading/writing after positioning the head
 976 * over the correct track.
 977 */
 978
 979static int MultReadInProgress = 0;
 980
 981
 982static void fd_rwsec( void )
 983{
 984	unsigned long paddr, flags;
 985	unsigned int  rwflag, old_motoron;
 986	unsigned int track;
 987	
 988	DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
 989	if (ReqCmd == WRITE) {
 990		if (ATARIHW_PRESENT(EXTD_DMA)) {
 991			paddr = virt_to_phys(ReqData);
 992		}
 993		else {
 994			copy_buffer( ReqData, DMABuffer );
 995			paddr = PhysDMABuffer;
 996		}
 997		dma_cache_maintenance( paddr, 512, 1 );
 998		rwflag = 0x100;
 999	}
1000	else {
1001		if (read_track)
1002			paddr = PhysTrackBuffer;
1003		else
1004			paddr = ATARIHW_PRESENT(EXTD_DMA) ? 
1005				virt_to_phys(ReqData) : PhysDMABuffer;
1006		rwflag = 0;
1007	}
1008
1009	fd_select_side( ReqSide );
1010  
1011	/* Start sector of this operation */
1012	FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
1013	MFPDELAY();
1014	/* Cheat for track if stretch != 0 */
1015	if (SUDT->stretch) {
1016		track = FDC_READ( FDCREG_TRACK);
1017		MFPDELAY();
1018		FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
1019	}
1020	udelay(25);
1021  
1022	/* Setup DMA */
1023	local_irq_save(flags);
1024	dma_wd.dma_lo = (unsigned char)paddr;
1025	MFPDELAY();
1026	paddr >>= 8;
1027	dma_wd.dma_md = (unsigned char)paddr;
1028	MFPDELAY();
1029	paddr >>= 8;
1030	if (ATARIHW_PRESENT(EXTD_DMA))
1031		st_dma_ext_dmahi = (unsigned short)paddr;
1032	else
1033		dma_wd.dma_hi = (unsigned char)paddr;
1034	MFPDELAY();
1035	local_irq_restore(flags);
1036  
1037	/* Clear FIFO and switch DMA to correct mode */  
1038	dma_wd.dma_mode_status = 0x90 | rwflag;  
1039	MFPDELAY();
1040	dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);  
1041	MFPDELAY();
1042	dma_wd.dma_mode_status = 0x90 | rwflag;
1043	MFPDELAY();
1044  
1045	/* How many sectors for DMA */
1046	dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1047  
1048	udelay(25);  
1049  
1050	/* Start operation */
1051	dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1052	udelay(25);
1053	SET_IRQ_HANDLER( fd_rwsec_done );
1054	dma_wd.fdc_acces_seccount =
1055	  (get_head_settle_flag() |
1056	   (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1057
1058	old_motoron = MotorOn;
1059	MotorOn = 1;
1060	NeedSeek = 1;
1061	/* wait for interrupt */
1062
1063	if (read_track) {
1064		/* If reading a whole track, wait about one disk rotation and
1065		 * then check if all sectors are read. The FDC will even
1066		 * search for the first non-existent sector and need 1 sec to
1067		 * recognise that it isn't present :-(
1068		 */
1069		MultReadInProgress = 1;
1070		mod_timer(&readtrack_timer,
1071			  /* 1 rot. + 5 rot.s if motor was off  */
1072			  jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1073	}
1074	start_timeout();
1075}
1076
1077    
1078static void fd_readtrack_check(struct timer_list *unused)
1079{
1080	unsigned long flags, addr, addr2;
1081
1082	local_irq_save(flags);
1083
1084	if (!MultReadInProgress) {
1085		/* This prevents a race condition that could arise if the
1086		 * interrupt is triggered while the calling of this timer
1087		 * callback function takes place. The IRQ function then has
1088		 * already cleared 'MultReadInProgress'  when flow of control
1089		 * gets here.
1090		 */
1091		local_irq_restore(flags);
1092		return;
1093	}
1094
1095	/* get the current DMA address */
1096	/* ++ f.a. read twice to avoid being fooled by switcher */
1097	addr = 0;
1098	do {
1099		addr2 = addr;
1100		addr = dma_wd.dma_lo & 0xff;
1101		MFPDELAY();
1102		addr |= (dma_wd.dma_md & 0xff) << 8;
1103		MFPDELAY();
1104		if (ATARIHW_PRESENT( EXTD_DMA ))
1105			addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1106		else
1107			addr |= (dma_wd.dma_hi & 0xff) << 16;
1108		MFPDELAY();
1109	} while(addr != addr2);
1110  
1111	if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1112		/* already read enough data, force an FDC interrupt to stop
1113		 * the read operation
1114		 */
1115		SET_IRQ_HANDLER( NULL );
1116		MultReadInProgress = 0;
1117		local_irq_restore(flags);
1118		DPRINT(("fd_readtrack_check(): done\n"));
1119		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1120		udelay(25);
1121
1122		/* No error until now -- the FDC would have interrupted
1123		 * otherwise!
1124		 */
1125		fd_rwsec_done1(0);
1126	}
1127	else {
1128		/* not yet finished, wait another tenth rotation */
1129		local_irq_restore(flags);
1130		DPRINT(("fd_readtrack_check(): not yet finished\n"));
1131		mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1132	}
1133}
1134
1135
1136static void fd_rwsec_done( int status )
1137{
1138	DPRINT(("fd_rwsec_done()\n"));
1139
1140	if (read_track) {
1141		timer_delete(&readtrack_timer);
1142		if (!MultReadInProgress)
1143			return;
1144		MultReadInProgress = 0;
1145	}
1146	fd_rwsec_done1(status);
1147}
1148
1149static void fd_rwsec_done1(int status)
1150{
1151	unsigned int track;
1152
1153	stop_timeout();
1154	
1155	/* Correct the track if stretch != 0 */
1156	if (SUDT->stretch) {
1157		track = FDC_READ( FDCREG_TRACK);
1158		MFPDELAY();
1159		FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1160	}
1161
1162	if (!UseTrackbuffer) {
1163		dma_wd.dma_mode_status = 0x90;
1164		MFPDELAY();
1165		if (!(dma_wd.dma_mode_status & 0x01)) {
1166			printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1167			goto err_end;
1168		}
1169	}
1170	MFPDELAY();
1171
1172	if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1173		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1174		goto err_end;
1175	}	
1176	if ((status & FDCSTAT_RECNF) &&
1177	    /* RECNF is no error after a multiple read when the FDC
1178	       searched for a non-existent sector! */
1179	    !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1180		if (Probing) {
1181			if (SUDT > atari_disk_type) {
1182			    if (SUDT[-1].blocks > ReqBlock) {
1183				/* try another disk type */
1184				SUDT--;
1185				set_capacity(unit[SelectedDrive].disk[0],
1186							SUDT->blocks);
1187			    } else
1188				Probing = 0;
1189			}
1190			else {
1191				if (SUD.flags & FTD_MSG)
1192					printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1193					       SelectedDrive, SUDT->name );
1194				Probing=0;
1195			}
1196		} else {	
1197/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1198			if (SUD.autoprobe) {
1199				SUDT = atari_disk_type + StartDiskType[DriveType];
1200				set_capacity(unit[SelectedDrive].disk[0],
1201							SUDT->blocks);
1202				Probing = 1;
1203			}
1204		}
1205		if (Probing) {
1206			if (ATARIHW_PRESENT(FDCSPEED)) {
1207				dma_wd.fdc_speed = SUDT->fdc_speed;
1208				MFPDELAY();
1209			}
1210			setup_req_params( SelectedDrive );
1211			BufferDrive = -1;
1212			do_fd_action( SelectedDrive );
1213			return;
1214		}
1215
1216		printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1217		       SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1218		goto err_end;
1219	}
1220	if (status & FDCSTAT_CRC) {
1221		printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1222		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1223		goto err_end;
1224	}
1225	if (status & FDCSTAT_LOST) {
1226		printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1227		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1228		goto err_end;
1229	}
1230
1231	Probing = 0;
1232	
1233	if (ReqCmd == READ) {
1234		if (!read_track) {
1235			void *addr;
1236			addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1237			dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1238			if (!ATARIHW_PRESENT( EXTD_DMA ))
1239				copy_buffer (addr, ReqData);
1240		} else {
1241			dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1242			BufferDrive = SelectedDrive;
1243			BufferSide  = ReqSide;
1244			BufferTrack = ReqTrack;
1245			copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1246		}
1247	}
1248  
1249	if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1250		/* read next sector */
1251		setup_req_params( SelectedDrive );
1252		do_fd_action( SelectedDrive );
1253	}
1254	else {
1255		/* all sectors finished */
1256		fd_end_request_cur(BLK_STS_OK);
1257		finish_fdc();
1258	}
1259	return;
1260  
1261  err_end:
1262	BufferDrive = -1;
1263	fd_error();
1264}
1265
1266
1267static void fd_writetrack( void )
1268{
1269	unsigned long paddr, flags;
1270	unsigned int track;
1271	
1272	DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1273
1274	paddr = PhysTrackBuffer;
1275	dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1276
1277	fd_select_side( ReqSide );
1278  
1279	/* Cheat for track if stretch != 0 */
1280	if (SUDT->stretch) {
1281		track = FDC_READ( FDCREG_TRACK);
1282		MFPDELAY();
1283		FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1284	}
1285	udelay(40);
1286  
1287	/* Setup DMA */
1288	local_irq_save(flags);
1289	dma_wd.dma_lo = (unsigned char)paddr;
1290	MFPDELAY();
1291	paddr >>= 8;
1292	dma_wd.dma_md = (unsigned char)paddr;
1293	MFPDELAY();
1294	paddr >>= 8;
1295	if (ATARIHW_PRESENT( EXTD_DMA ))
1296		st_dma_ext_dmahi = (unsigned short)paddr;
1297	else
1298		dma_wd.dma_hi = (unsigned char)paddr;
1299	MFPDELAY();
1300	local_irq_restore(flags);
1301  
1302	/* Clear FIFO and switch DMA to correct mode */  
1303	dma_wd.dma_mode_status = 0x190;  
1304	MFPDELAY();
1305	dma_wd.dma_mode_status = 0x90;  
1306	MFPDELAY();
1307	dma_wd.dma_mode_status = 0x190;
1308	MFPDELAY();
1309  
1310	/* How many sectors for DMA */
1311	dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1312	udelay(40);  
1313  
1314	/* Start operation */
1315	dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1316	udelay(40);
1317	SET_IRQ_HANDLER( fd_writetrack_done );
1318	dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag(); 
1319
1320	MotorOn = 1;
1321	start_timeout();
1322	/* wait for interrupt */
1323}
1324
1325
1326static void fd_writetrack_done( int status )
1327{
1328	DPRINT(("fd_writetrack_done()\n"));
1329
1330	stop_timeout();
1331
1332	if (status & FDCSTAT_WPROT) {
1333		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1334		goto err_end;
1335	}	
1336	if (status & FDCSTAT_LOST) {
1337		printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1338				SelectedDrive, ReqSide, ReqTrack );
1339		goto err_end;
1340	}
1341
1342	complete(&format_wait);
1343	return;
1344
1345  err_end:
1346	fd_error();
1347}
1348
1349static void fd_times_out(struct timer_list *unused)
1350{
1351	atari_disable_irq( IRQ_MFP_FDC );
1352	if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1353					  * before we came here... */
1354
1355	SET_IRQ_HANDLER( NULL );
1356	/* If the timeout occurred while the readtrack_check timer was
1357	 * active, we need to cancel it, else bad things will happen */
1358	if (UseTrackbuffer)
1359		timer_delete(&readtrack_timer);
1360	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1361	udelay( 25 );
1362	
1363	printk(KERN_ERR "floppy timeout\n" );
1364	fd_error();
1365  end:
1366	atari_enable_irq( IRQ_MFP_FDC );
1367}
1368
1369
1370/* The (noop) seek operation here is needed to make the WP bit in the
1371 * FDC status register accessible for check_change. If the last disk
1372 * operation would have been a RDSEC, this bit would always read as 0
1373 * no matter what :-( To save time, the seek goes to the track we're
1374 * already on.
1375 */
1376
1377static void finish_fdc( void )
1378{
1379	if (!NeedSeek || !stdma_is_locked_by(floppy_irq)) {
1380		finish_fdc_done( 0 );
1381	}
1382	else {
1383		DPRINT(("finish_fdc: dummy seek started\n"));
1384		FDC_WRITE (FDCREG_DATA, SUD.track);
1385		SET_IRQ_HANDLER( finish_fdc_done );
1386		FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1387		MotorOn = 1;
1388		start_timeout();
1389		/* we must wait for the IRQ here, because the ST-DMA
1390		   is released immediately afterwards and the interrupt
1391		   may be delivered to the wrong driver. */
1392	  }
1393}
1394
1395
1396static void finish_fdc_done( int dummy )
1397{
1398	unsigned long flags;
1399
1400	DPRINT(("finish_fdc_done entered\n"));
1401	stop_timeout();
1402	NeedSeek = 0;
1403
1404	if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1405		/* If the check for a disk change is done too early after this
1406		 * last seek command, the WP bit still reads wrong :-((
1407		 */
1408		mod_timer(&fd_timer, jiffies + 5);
1409	else
1410		start_check_change_timer();
1411	start_motor_off_timer();
1412
1413	local_irq_save(flags);
1414	if (stdma_is_locked_by(floppy_irq))
1415		stdma_release();
1416	local_irq_restore(flags);
1417
1418	DPRINT(("finish_fdc() finished\n"));
1419}
1420
1421/* The detection of disk changes is a dark chapter in Atari history :-(
1422 * Because the "Drive ready" signal isn't present in the Atari
1423 * hardware, one has to rely on the "Write Protect". This works fine,
1424 * as long as no write protected disks are used. TOS solves this
1425 * problem by introducing tri-state logic ("maybe changed") and
1426 * looking at the serial number in block 0. This isn't possible for
1427 * Linux, since the floppy driver can't make assumptions about the
1428 * filesystem used on the disk and thus the contents of block 0. I've
1429 * chosen the method to always say "The disk was changed" if it is
1430 * unsure whether it was. This implies that every open or mount
1431 * invalidates the disk buffers if you work with write protected
1432 * disks. But at least this is better than working with incorrect data
1433 * due to unrecognised disk changes.
1434 */
1435
1436static unsigned int floppy_check_events(struct gendisk *disk,
1437					unsigned int clearing)
1438{
1439	struct atari_floppy_struct *p = disk->private_data;
1440	unsigned int drive = p - unit;
1441	if (test_bit (drive, &fake_change)) {
1442		/* simulated change (e.g. after formatting) */
1443		return DISK_EVENT_MEDIA_CHANGE;
1444	}
1445	if (test_bit (drive, &changed_floppies)) {
1446		/* surely changed (the WP signal changed at least once) */
1447		return DISK_EVENT_MEDIA_CHANGE;
1448	}
1449	if (UD.wpstat) {
1450		/* WP is on -> could be changed: to be sure, buffers should be
1451		 * invalidated...
1452		 */
1453		return DISK_EVENT_MEDIA_CHANGE;
1454	}
1455
1456	return 0;
1457}
1458
1459static int floppy_revalidate(struct gendisk *disk)
1460{
1461	struct atari_floppy_struct *p = disk->private_data;
1462	unsigned int drive = p - unit;
1463
1464	if (test_bit(drive, &changed_floppies) ||
1465	    test_bit(drive, &fake_change) || !p->disktype) {
1466		if (UD.flags & FTD_MSG)
1467			printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1468		BufferDrive = -1;
1469		clear_bit(drive, &fake_change);
1470		clear_bit(drive, &changed_floppies);
1471		/* MSch: clearing geometry makes sense only for autoprobe
1472		   formats, for 'permanent user-defined' parameter:
1473		   restore default_params[] here if flagged valid! */
1474		if (default_params[drive].blocks == 0)
1475			UDT = NULL;
1476		else
1477			UDT = &default_params[drive];
1478	}
1479	return 0;
1480}
1481
1482
1483/* This sets up the global variables describing the current request. */
1484
1485static void setup_req_params( int drive )
1486{
1487	int block = ReqBlock + ReqCnt;
1488
1489	ReqTrack = block / UDT->spt;
1490	ReqSector = block - ReqTrack * UDT->spt + 1;
1491	ReqSide = ReqTrack & 1;
1492	ReqTrack >>= 1;
1493	ReqData = ReqBuffer + 512 * ReqCnt;
1494
1495	if (UseTrackbuffer)
1496		read_track = (ReqCmd == READ && unit[drive].error_count == 0);
1497	else
1498		read_track = 0;
1499
1500	DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1501			ReqTrack, ReqSector, (unsigned long)ReqData ));
1502}
1503
1504static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
1505				     const struct blk_mq_queue_data *bd)
1506{
1507	struct atari_floppy_struct *floppy = bd->rq->q->disk->private_data;
1508	int drive = floppy - unit;
1509	int type = floppy->type;
1510
1511	DPRINT(("Queue request: drive %d type %d sectors %d of %d last %d\n",
1512		drive, type, blk_rq_cur_sectors(bd->rq),
1513		blk_rq_sectors(bd->rq), bd->last));
1514
1515	spin_lock_irq(&ataflop_lock);
1516	if (fd_request) {
1517		spin_unlock_irq(&ataflop_lock);
1518		return BLK_STS_DEV_RESOURCE;
1519	}
1520	if (!stdma_try_lock(floppy_irq, NULL))  {
1521		spin_unlock_irq(&ataflop_lock);
1522		return BLK_STS_RESOURCE;
1523	}
1524	fd_request = bd->rq;
1525	unit[drive].error_count = 0;
1526	blk_mq_start_request(fd_request);
1527
1528	atari_disable_irq( IRQ_MFP_FDC );
1529
1530	IsFormatting = 0;
1531
1532	if (!UD.connected) {
1533		/* drive not connected */
1534		printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1535		fd_end_request_cur(BLK_STS_IOERR);
1536		stdma_release();
1537		goto out;
1538	}
1539		
1540	if (type == 0) {
1541		if (!UDT) {
1542			Probing = 1;
1543			UDT = atari_disk_type + StartDiskType[DriveType];
1544			set_capacity(bd->rq->q->disk, UDT->blocks);
1545			UD.autoprobe = 1;
1546		}
1547	} 
1548	else {
1549		/* user supplied disk type */
1550		if (--type >= NUM_DISK_MINORS) {
1551			printk(KERN_WARNING "fd%d: invalid disk format", drive );
1552			fd_end_request_cur(BLK_STS_IOERR);
1553			stdma_release();
1554			goto out;
1555		}
1556		if (minor2disktype[type].drive_types > DriveType)  {
1557			printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1558			fd_end_request_cur(BLK_STS_IOERR);
1559			stdma_release();
1560			goto out;
1561		}
1562		type = minor2disktype[type].index;
1563		UDT = &atari_disk_type[type];
1564		set_capacity(bd->rq->q->disk, UDT->blocks);
1565		UD.autoprobe = 0;
1566	}
1567
1568	/* stop deselect timer */
1569	timer_delete(&motor_off_timer);
1570		
1571	ReqCnt = 0;
1572	ReqCmd = rq_data_dir(fd_request);
1573	ReqBlock = blk_rq_pos(fd_request);
1574	ReqBuffer = bio_data(fd_request->bio);
1575	setup_req_params( drive );
1576	do_fd_action( drive );
1577
1578	atari_enable_irq( IRQ_MFP_FDC );
1579
1580out:
1581	spin_unlock_irq(&ataflop_lock);
1582	return BLK_STS_OK;
1583}
1584
1585static int fd_locked_ioctl(struct block_device *bdev, blk_mode_t mode,
1586		    unsigned int cmd, unsigned long param)
1587{
1588	struct gendisk *disk = bdev->bd_disk;
1589	struct atari_floppy_struct *floppy = disk->private_data;
1590	int drive = floppy - unit;
1591	int type = floppy->type;
1592	struct atari_format_descr fmt_desc;
1593	struct atari_disk_type *dtp;
1594	struct floppy_struct getprm;
1595	int settype;
1596	struct floppy_struct setprm;
1597	void __user *argp = (void __user *)param;
1598
1599	switch (cmd) {
1600	case FDGETPRM:
1601		if (type) {
1602			if (--type >= NUM_DISK_MINORS)
1603				return -ENODEV;
1604			if (minor2disktype[type].drive_types > DriveType)
1605				return -ENODEV;
1606			type = minor2disktype[type].index;
1607			dtp = &atari_disk_type[type];
1608			if (UD.flags & FTD_MSG)
1609			    printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1610			        drive, dtp, dtp->name);
1611		}
1612		else {
1613			if (!UDT)
1614				return -ENXIO;
1615			else
1616				dtp = UDT;
1617		}
1618		memset((void *)&getprm, 0, sizeof(getprm));
1619		getprm.size = dtp->blocks;
1620		getprm.sect = dtp->spt;
1621		getprm.head = 2;
1622		getprm.track = dtp->blocks/dtp->spt/2;
1623		getprm.stretch = dtp->stretch;
1624		if (copy_to_user(argp, &getprm, sizeof(getprm)))
1625			return -EFAULT;
1626		return 0;
1627	}
1628	switch (cmd) {
1629	case FDSETPRM:
1630	case FDDEFPRM:
1631	        /* 
1632		 * MSch 7/96: simple 'set geometry' case: just set the
1633		 * 'default' device params (minor == 0).
1634		 * Currently, the drive geometry is cleared after each
1635		 * disk change and subsequent revalidate()! simple
1636		 * implementation of FDDEFPRM: save geometry from a
1637		 * FDDEFPRM call and restore it in floppy_revalidate() !
1638		 */
1639
1640		/* get the parameters from user space */
1641		if (floppy->ref != 1 && floppy->ref != -1)
1642			return -EBUSY;
1643		if (copy_from_user(&setprm, argp, sizeof(setprm)))
1644			return -EFAULT;
1645		/* 
1646		 * first of all: check for floppy change and revalidate, 
1647		 * or the next access will revalidate - and clear UDT :-(
1648		 */
1649
1650		if (floppy_check_events(disk, 0))
1651		        floppy_revalidate(disk);
1652
1653		if (UD.flags & FTD_MSG)
1654		    printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1655			drive, setprm.size, setprm.sect, setprm.stretch);
1656
1657		/* what if type > 0 here? Overwrite specified entry ? */
1658		if (type) {
1659		        /* refuse to re-set a predefined type for now */
1660			finish_fdc();
1661			return -EINVAL;
1662		}
1663
1664		/* 
1665		 * type == 0: first look for a matching entry in the type list,
1666		 * and set the UD.disktype field to use the perdefined entry.
1667		 * TODO: add user-defined format to head of autoprobe list ? 
1668		 * Useful to include the user-type for future autodetection!
1669		 */
1670
1671		for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1672			int setidx = 0;
1673			if (minor2disktype[settype].drive_types > DriveType) {
1674				/* skip this one, invalid for drive ... */
1675				continue;
1676			}
1677			setidx = minor2disktype[settype].index;
1678			dtp = &atari_disk_type[setidx];
1679
1680			/* found matching entry ?? */
1681			if (   dtp->blocks  == setprm.size 
1682			    && dtp->spt     == setprm.sect
1683			    && dtp->stretch == setprm.stretch ) {
1684				if (UD.flags & FTD_MSG)
1685				    printk (KERN_INFO "floppy%d: setting %s %p!\n",
1686				        drive, dtp->name, dtp);
1687				UDT = dtp;
1688				set_capacity(disk, UDT->blocks);
1689
1690				if (cmd == FDDEFPRM) {
1691				  /* save settings as permanent default type */
1692				  default_params[drive].name    = dtp->name;
1693				  default_params[drive].spt     = dtp->spt;
1694				  default_params[drive].blocks  = dtp->blocks;
1695				  default_params[drive].fdc_speed = dtp->fdc_speed;
1696				  default_params[drive].stretch = dtp->stretch;
1697				}
1698				
1699				return 0;
1700			}
1701
1702		}
1703
1704		/* no matching disk type found above - setting user_params */
1705
1706	       	if (cmd == FDDEFPRM) {
1707			/* set permanent type */
1708			dtp = &default_params[drive];
1709		} else
1710			/* set user type (reset by disk change!) */
1711			dtp = &user_params[drive];
1712
1713		dtp->name   = "user format";
1714		dtp->blocks = setprm.size;
1715		dtp->spt    = setprm.sect;
1716		if (setprm.sect > 14) 
1717			dtp->fdc_speed = 3;
1718		else
1719			dtp->fdc_speed = 0;
1720		dtp->stretch = setprm.stretch;
1721
1722		if (UD.flags & FTD_MSG)
1723			printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1724				drive, dtp->blocks, dtp->spt, dtp->stretch);
1725
1726		/* sanity check */
1727		if (setprm.track != dtp->blocks/dtp->spt/2 ||
1728		    setprm.head != 2) {
1729			finish_fdc();
1730			return -EINVAL;
1731		}
1732
1733		UDT = dtp;
1734		set_capacity(disk, UDT->blocks);
1735
1736		return 0;
1737	case FDMSGON:
1738		UD.flags |= FTD_MSG;
1739		return 0;
1740	case FDMSGOFF:
1741		UD.flags &= ~FTD_MSG;
1742		return 0;
1743	case FDSETEMSGTRESH:
1744		return -EINVAL;
1745	case FDFMTBEG:
1746		return 0;
1747	case FDFMTTRK:
1748		if (floppy->ref != 1 && floppy->ref != -1)
1749			return -EBUSY;
1750		if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1751			return -EFAULT;
1752		return do_format(drive, type, &fmt_desc);
1753	case FDCLRPRM:
1754		UDT = NULL;
1755		/* MSch: invalidate default_params */
1756		default_params[drive].blocks  = 0;
1757		set_capacity(disk, MAX_DISK_SIZE * 2);
1758		fallthrough;
1759	case FDFMTEND:
1760	case FDFLUSH:
1761		/* invalidate the buffer track to force a reread */
1762		BufferDrive = -1;
1763		set_bit(drive, &fake_change);
1764		if (disk_check_media_change(disk)) {
1765			bdev_mark_dead(disk->part0, true);
1766			floppy_revalidate(disk);
1767		}
1768		return 0;
1769	default:
1770		return -EINVAL;
1771	}
1772}
1773
1774static int fd_ioctl(struct block_device *bdev, blk_mode_t mode,
1775			     unsigned int cmd, unsigned long arg)
1776{
1777	int ret;
1778
1779	mutex_lock(&ataflop_mutex);
1780	ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1781	mutex_unlock(&ataflop_mutex);
1782
1783	return ret;
1784}
1785
1786/* Initialize the 'unit' variable for drive 'drive' */
1787
1788static void __init fd_probe( int drive )
1789{
1790	UD.connected = 0;
1791	UDT  = NULL;
1792
1793	if (!fd_test_drive_present( drive ))
1794		return;
1795
1796	UD.connected = 1;
1797	UD.track     = 0;
1798	switch( UserSteprate[drive] ) {
1799	case 2:
1800		UD.steprate = FDCSTEP_2;
1801		break;
1802	case 3:
1803		UD.steprate = FDCSTEP_3;
1804		break;
1805	case 6:
1806		UD.steprate = FDCSTEP_6;
1807		break;
1808	case 12:
1809		UD.steprate = FDCSTEP_12;
1810		break;
1811	default: /* should be -1 for "not set by user" */
1812		if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1813			UD.steprate = FDCSTEP_3;
1814		else
1815			UD.steprate = FDCSTEP_6;
1816		break;
1817	}
1818	MotorOn = 1;	/* from probe restore operation! */
1819}
1820
1821
1822/* This function tests the physical presence of a floppy drive (not
1823 * whether a disk is inserted). This is done by issuing a restore
1824 * command, waiting max. 2 seconds (that should be enough to move the
1825 * head across the whole disk) and looking at the state of the "TR00"
1826 * signal. This should now be raised if there is a drive connected
1827 * (and there is no hardware failure :-) Otherwise, the drive is
1828 * declared absent.
1829 */
1830
1831static int __init fd_test_drive_present( int drive )
1832{
1833	unsigned long timeout;
1834	unsigned char status;
1835	int ok;
1836	
1837	if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1838	fd_select_drive( drive );
1839
1840	/* disable interrupt temporarily */
1841	atari_turnoff_irq( IRQ_MFP_FDC );
1842	FDC_WRITE (FDCREG_TRACK, 0xff00);
1843	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1844
1845	timeout = jiffies + 2*HZ+HZ/2;
1846	while (time_before(jiffies, timeout))
1847		if (!(st_mfp.par_dt_reg & 0x20))
1848			break;
1849
1850	status = FDC_READ( FDCREG_STATUS );
1851	ok = (status & FDCSTAT_TR00) != 0;
1852
1853	/* force interrupt to abort restore operation (FDC would try
1854	 * about 50 seconds!) */
1855	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1856	udelay(500);
1857	status = FDC_READ( FDCREG_STATUS );
1858	udelay(20);
1859
1860	if (ok) {
1861		/* dummy seek command to make WP bit accessible */
1862		FDC_WRITE( FDCREG_DATA, 0 );
1863		FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1864		while( st_mfp.par_dt_reg & 0x20 )
1865			;
1866		status = FDC_READ( FDCREG_STATUS );
1867	}
1868
1869	atari_turnon_irq( IRQ_MFP_FDC );
1870	return( ok );
1871}
1872
1873
1874/* Look how many and which kind of drives are connected. If there are
1875 * floppies, additionally start the disk-change and motor-off timers.
1876 */
1877
1878static void __init config_types( void )
1879{
1880	int drive, cnt = 0;
1881
1882	/* for probing drives, set the FDC speed to 8 MHz */
1883	if (ATARIHW_PRESENT(FDCSPEED))
1884		dma_wd.fdc_speed = 0;
1885
1886	printk(KERN_INFO "Probing floppy drive(s):\n");
1887	for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1888		fd_probe( drive );
1889		if (UD.connected) {
1890			printk(KERN_INFO "fd%d\n", drive);
1891			++cnt;
1892		}
1893	}
1894
1895	if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1896		/* If FDC is still busy from probing, give it another FORCI
1897		 * command to abort the operation. If this isn't done, the FDC
1898		 * will interrupt later and its IRQ line stays low, because
1899		 * the status register isn't read. And this will block any
1900		 * interrupts on this IRQ line :-(
1901		 */
1902		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1903		udelay(500);
1904		FDC_READ( FDCREG_STATUS );
1905		udelay(20);
1906	}
1907	
1908	if (cnt > 0) {
1909		start_motor_off_timer();
1910		if (cnt == 1) fd_select_drive( 0 );
1911		start_check_change_timer();
1912	}
1913}
1914
1915/*
1916 * floppy_open check for aliasing (/dev/fd0 can be the same as
1917 * /dev/PS0 etc), and disallows simultaneous access to the same
1918 * drive with different device numbers.
1919 */
1920
1921static int floppy_open(struct gendisk *disk, blk_mode_t mode)
1922{
1923	struct atari_floppy_struct *p = disk->private_data;
1924	int type = disk->first_minor >> 2;
1925
1926	DPRINT(("fd_open: type=%d\n",type));
1927	if (p->ref && p->type != type)
1928		return -EBUSY;
1929
1930	if (p->ref == -1 || (p->ref && mode & BLK_OPEN_EXCL))
1931		return -EBUSY;
1932	if (mode & BLK_OPEN_EXCL)
1933		p->ref = -1;
1934	else
1935		p->ref++;
1936
1937	p->type = type;
1938
1939	if (mode & BLK_OPEN_NDELAY)
1940		return 0;
1941
1942	if (mode & (BLK_OPEN_READ | BLK_OPEN_WRITE)) {
1943		if (disk_check_media_change(disk))
1944			floppy_revalidate(disk);
1945		if (mode & BLK_OPEN_WRITE) {
1946			if (p->wpstat) {
1947				if (p->ref < 0)
1948					p->ref = 0;
1949				else
1950					p->ref--;
1951				return -EROFS;
1952			}
1953		}
1954	}
1955	return 0;
1956}
1957
1958static int floppy_unlocked_open(struct gendisk *disk, blk_mode_t mode)
1959{
1960	int ret;
1961
1962	mutex_lock(&ataflop_mutex);
1963	ret = floppy_open(disk, mode);
1964	mutex_unlock(&ataflop_mutex);
1965
1966	return ret;
1967}
1968
1969static void floppy_release(struct gendisk *disk)
1970{
1971	struct atari_floppy_struct *p = disk->private_data;
1972	mutex_lock(&ataflop_mutex);
1973	if (p->ref < 0)
1974		p->ref = 0;
1975	else if (!p->ref--) {
1976		printk(KERN_ERR "floppy_release with fd_ref == 0");
1977		p->ref = 0;
1978	}
1979	mutex_unlock(&ataflop_mutex);
1980}
1981
1982static const struct block_device_operations floppy_fops = {
1983	.owner		= THIS_MODULE,
1984	.open		= floppy_unlocked_open,
1985	.release	= floppy_release,
1986	.ioctl		= fd_ioctl,
1987	.check_events	= floppy_check_events,
1988};
1989
1990static const struct blk_mq_ops ataflop_mq_ops = {
1991	.queue_rq = ataflop_queue_rq,
1992};
1993
1994static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
1995{
1996	struct queue_limits lim = {
1997		.features		= BLK_FEAT_ROTATIONAL,
1998	};
1999	struct gendisk *disk;
2000
2001	disk = blk_mq_alloc_disk(&unit[drive].tag_set, &lim, NULL);
2002	if (IS_ERR(disk))
2003		return PTR_ERR(disk);
2004
2005	disk->major = FLOPPY_MAJOR;
2006	disk->first_minor = drive + (type << 2);
2007	disk->minors = 1;
2008	sprintf(disk->disk_name, "fd%d", drive);
2009	disk->fops = &floppy_fops;
2010	disk->flags |= GENHD_FL_NO_PART;
2011	disk->events = DISK_EVENT_MEDIA_CHANGE;
2012	disk->private_data = &unit[drive];
2013	set_capacity(disk, MAX_DISK_SIZE * 2);
2014
2015	unit[drive].disk[type] = disk;
2016	return 0;
2017}
2018
2019static void ataflop_probe(dev_t dev)
2020{
2021	int drive = MINOR(dev) & 3;
2022	int type  = MINOR(dev) >> 2;
2023
2024	if (type)
2025		type--;
2026
2027	if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
2028		return;
2029	if (unit[drive].disk[type])
2030		return;
2031	if (ataflop_alloc_disk(drive, type))
2032		return;
2033	if (add_disk(unit[drive].disk[type]))
2034		goto cleanup_disk;
2035	unit[drive].registered[type] = true;
2036	return;
2037
2038cleanup_disk:
2039	put_disk(unit[drive].disk[type]);
2040	unit[drive].disk[type] = NULL;
2041}
2042
2043static void atari_floppy_cleanup(void)
2044{
2045	int i;
2046	int type;
2047
2048	for (i = 0; i < FD_MAX_UNITS; i++) {
2049		for (type = 0; type < NUM_DISK_MINORS; type++) {
2050			if (!unit[i].disk[type])
2051				continue;
2052			del_gendisk(unit[i].disk[type]);
2053			put_disk(unit[i].disk[type]);
2054		}
2055		blk_mq_free_tag_set(&unit[i].tag_set);
2056	}
2057
2058	timer_delete_sync(&fd_timer);
2059	atari_stram_free(DMABuffer);
2060}
2061
2062static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs)
2063{
2064	int type;
2065
2066	for (type = 0; type < NUM_DISK_MINORS; type++) {
2067		if (!fs->disk[type])
2068			continue;
2069		if (fs->registered[type])
2070			del_gendisk(fs->disk[type]);
2071		put_disk(fs->disk[type]);
2072	}
2073	blk_mq_free_tag_set(&fs->tag_set);
2074}
2075
2076static int __init atari_floppy_init (void)
2077{
2078	int i;
2079	int ret;
2080
2081	if (!MACH_IS_ATARI)
2082		/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2083		return -ENODEV;
2084
2085	for (i = 0; i < FD_MAX_UNITS; i++) {
2086		memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
2087		unit[i].tag_set.ops = &ataflop_mq_ops;
2088		unit[i].tag_set.nr_hw_queues = 1;
2089		unit[i].tag_set.nr_maps = 1;
2090		unit[i].tag_set.queue_depth = 2;
2091		unit[i].tag_set.numa_node = NUMA_NO_NODE;
2092		ret = blk_mq_alloc_tag_set(&unit[i].tag_set);
2093		if (ret)
2094			goto err;
2095
2096		ret = ataflop_alloc_disk(i, 0);
2097		if (ret) {
2098			blk_mq_free_tag_set(&unit[i].tag_set);
2099			goto err;
2100		}
2101	}
2102
2103	if (UseTrackbuffer < 0)
2104		/* not set by user -> use default: for now, we turn
2105		   track buffering off for all Medusas, though it
2106		   could be used with ones that have a counter
2107		   card. But the test is too hard :-( */
2108		UseTrackbuffer = !MACH_IS_MEDUSA;
2109
2110	/* initialize variables */
2111	SelectedDrive = -1;
2112	BufferDrive = -1;
2113
2114	DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2115	if (!DMABuffer) {
2116		printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2117		ret = -ENOMEM;
2118		goto err;
2119	}
2120	TrackBuffer = DMABuffer + 512;
2121	PhysDMABuffer = atari_stram_to_phys(DMABuffer);
2122	PhysTrackBuffer = virt_to_phys(TrackBuffer);
2123	BufferDrive = BufferSide = BufferTrack = -1;
2124
2125	for (i = 0; i < FD_MAX_UNITS; i++) {
2126		unit[i].track = -1;
2127		unit[i].flags = 0;
2128		ret = add_disk(unit[i].disk[0]);
2129		if (ret)
2130			goto err_out_dma;
2131		unit[i].registered[0] = true;
2132	}
2133
2134	printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2135	       DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2136	       UseTrackbuffer ? "" : "no ");
2137	config_types();
2138
2139	ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2140	if (ret) {
2141		printk(KERN_ERR "atari_floppy_init: cannot register block device\n");
2142		atari_floppy_cleanup();
2143	}
2144	return ret;
2145
2146err_out_dma:
2147	atari_stram_free(DMABuffer);
2148err:
2149	while (--i >= 0)
2150		atari_cleanup_floppy_disk(&unit[i]);
2151
2152	return ret;
2153}
2154
2155#ifndef MODULE
2156static int __init atari_floppy_setup(char *str)
2157{
2158	int ints[3 + FD_MAX_UNITS];
2159	int i;
2160
2161	if (!MACH_IS_ATARI)
2162		return 0;
2163
2164	str = get_options(str, 3 + FD_MAX_UNITS, ints);
2165	
2166	if (ints[0] < 1) {
2167		printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2168		return 0;
2169	}
2170	else if (ints[0] > 2+FD_MAX_UNITS) {
2171		printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2172	}
2173
2174	if (ints[1] < 0 || ints[1] > 2)
2175		printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2176	else
2177		DriveType = ints[1];
2178
2179	if (ints[0] >= 2)
2180		UseTrackbuffer = (ints[2] > 0);
2181
2182	for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2183		if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2184			printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2185		else
2186			UserSteprate[i-3] = ints[i];
2187	}
2188	return 1;
2189}
2190
2191__setup("floppy=", atari_floppy_setup);
2192#endif
2193
2194static void __exit atari_floppy_exit(void)
2195{
2196	unregister_blkdev(FLOPPY_MAJOR, "fd");
2197	atari_floppy_cleanup();
2198}
2199
2200module_init(atari_floppy_init)
2201module_exit(atari_floppy_exit)
2202
2203MODULE_DESCRIPTION("Atari floppy driver");
2204MODULE_LICENSE("GPL");
~/ Read-only file preview.