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