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