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