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