2 * drivers/block/ataflop.c
4 * Copyright (C) 1993 Greg Harp
5 * Atari Support by Bjoern Brauel, Roman Hodek
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
13 * - Autodetection of disk format (DD/HD); untested yet, because I
14 * don't have an HD drive :-(
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
22 * Changes by Andreas Schwab:
23 * - After errors in multiple read mode try again reading single sectors
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
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...
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()
41 * - Make disk format numbering independent from minors
42 * - Let user set max. supported drive type (speeds up format
43 * detection, saves buffer space)
46 * - implement some more ioctls
50 * - increase gap size at start of track for HD/ED disks
52 * Michael (MSch) 11/07/96:
53 * - implemented FDSETPRM and FDDEFPRM ioctl
56 * - implemented missing BLK* ioctls
60 * - Maybe a better strategy for disk change detection (does anyone
64 #include <linux/module.h>
66 #include <linux/sched.h>
67 #include <linux/string.h>
69 #include <linux/fcntl.h>
70 #include <linux/kernel.h>
71 #include <linux/timer.h>
73 #include <linux/errno.h>
74 #include <linux/types.h>
75 #include <linux/delay.h>
77 #include <linux/malloc.h>
78 #include <linux/init.h>
80 #include <asm/setup.h>
81 #include <asm/system.h>
82 #include <asm/bitops.h>
84 #include <asm/pgtable.h>
85 #include <asm/uaccess.h>
87 #include <asm/atafd.h>
88 #include <asm/atafdreg.h>
89 #include <asm/atarihw.h>
90 #include <asm/atariints.h>
91 #include <asm/atari_stdma.h>
92 #include <asm/atari_stram.h>
94 #define MAJOR_NR FLOPPY_MAJOR
95 #include <linux/blk.h>
97 #define FD_MAX_UNITS 2
101 /* Disk types: DD, HD, ED */
102 static struct atari_disk_type
{
104 unsigned spt
; /* sectors per track */
105 unsigned blocks
; /* total number of blocks */
106 unsigned fdc_speed
; /* fdc_speed setting */
107 unsigned stretch
; /* track doubling ? */
109 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
110 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
111 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
112 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
113 /* formats above are probed for type DD */
114 #define MAX_TYPE_DD 3
115 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
116 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
117 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
118 /* formats above are probed for types DD and HD */
119 #define MAX_TYPE_HD 6
120 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
121 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
122 /* formats above are probed for types DD, HD and ED */
123 #define MAX_TYPE_ED 8
124 /* types below are never autoprobed */
125 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
126 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
127 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
128 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
129 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
130 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
131 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
132 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
133 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
134 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
135 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
136 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
137 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
138 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
139 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
140 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
141 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
142 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
143 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
146 static int StartDiskType
[] = {
156 static int DriveType
= TYPE_HD
;
158 /* Array for translating minors into disk formats */
161 unsigned drive_types
;
162 } minor2disktype
[] = {
163 { 0, TYPE_DD
}, /* 1: d360 */
164 { 4, TYPE_HD
}, /* 2: h1200 */
165 { 1, TYPE_DD
}, /* 3: D360 */
166 { 2, TYPE_DD
}, /* 4: D720 */
167 { 1, TYPE_DD
}, /* 5: h360 = D360 */
168 { 2, TYPE_DD
}, /* 6: h720 = D720 */
169 { 5, TYPE_HD
}, /* 7: H1440 */
170 { 7, TYPE_ED
}, /* 8: E2880 */
171 /* some PC formats :-) */
172 { 8, TYPE_ED
}, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
173 { 5, TYPE_HD
}, /* 10: h1440 = H1440 */
174 { 9, TYPE_HD
}, /* 11: H1680 */
175 { 10, TYPE_DD
}, /* 12: h410 */
176 { 3, TYPE_DD
}, /* 13: H820 <- == D820, 82x10 */
177 { 11, TYPE_HD
}, /* 14: h1476 */
178 { 12, TYPE_HD
}, /* 15: H1722 */
179 { 13, TYPE_DD
}, /* 16: h420 */
180 { 14, TYPE_DD
}, /* 17: H830 */
181 { 15, TYPE_HD
}, /* 18: h1494 */
182 { 16, TYPE_HD
}, /* 19: H1743 */
183 { 17, TYPE_DD
}, /* 20: h880 */
184 { 18, TYPE_DD
}, /* 21: D1040 */
185 { 19, TYPE_DD
}, /* 22: D1120 */
186 { 20, TYPE_HD
}, /* 23: h1600 */
187 { 21, TYPE_HD
}, /* 24: H1760 */
188 { 22, TYPE_HD
}, /* 25: H1920 */
189 { 23, TYPE_ED
}, /* 26: E3200 */
190 { 24, TYPE_ED
}, /* 27: E3520 */
191 { 25, TYPE_ED
}, /* 28: E3840 */
192 { 26, TYPE_HD
}, /* 29: H1840 */
193 { 27, TYPE_DD
}, /* 30: D800 */
194 { 6, TYPE_HD
}, /* 31: H1640 <- was H1600 == h1600 for PC */
197 #define NUM_DISK_MINORS (sizeof(minor2disktype)/sizeof(*minor2disktype))
200 * Maximum disk size (in kilobytes). This default is used whenever the
201 * current disk size is unknown.
203 #define MAX_DISK_SIZE 3280
206 * MSch: User-provided type information. 'drive' points to
207 * the respective entry of this array. Set by FDSETPRM ioctls.
209 static struct atari_disk_type user_params
[FD_MAX_UNITS
];
212 * User-provided permanent type information. 'drive' points to
213 * the respective entry of this array. Set by FDDEFPRM ioctls,
214 * restored upon disk change by floppy_revalidate() if valid (as seen by
215 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
217 static struct atari_disk_type default_params
[FD_MAX_UNITS
] = {
218 { NULL
, 0, 0, 0, 0}, };
220 static int floppy_sizes
[256];
221 static int floppy_blocksizes
[256] = { 0, };
223 /* current info on each unit */
224 static struct atari_floppy_struct
{
225 int connected
; /* !=0 : drive is connected */
226 int autoprobe
; /* !=0 : do autoprobe */
228 struct atari_disk_type
*disktype
; /* current type of disk */
230 int track
; /* current head position or -1 if
232 unsigned int steprate
; /* steprate setting */
233 unsigned int wpstat
; /* current state of WP signal (for
234 disk change detection) */
235 int flags
; /* flags */
236 } unit
[FD_MAX_UNITS
];
238 #define UD unit[drive]
239 #define UDT unit[drive].disktype
240 #define SUD unit[SelectedDrive]
241 #define SUDT unit[SelectedDrive].disktype
244 #define FDC_READ(reg) ({ \
245 /* unsigned long __flags; */ \
246 unsigned short __val; \
247 /* save_flags(__flags); cli(); */ \
248 dma_wd.dma_mode_status = 0x80 | (reg); \
250 __val = dma_wd.fdc_acces_seccount; \
252 /* restore_flags(__flags); */ \
256 #define FDC_WRITE(reg,val) \
258 /* unsigned long __flags; */ \
259 /* save_flags(__flags); cli(); */ \
260 dma_wd.dma_mode_status = 0x80 | (reg); \
262 dma_wd.fdc_acces_seccount = (val); \
264 /* restore_flags(__flags); */ \
268 /* Buffering variables:
269 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
270 * operations. Second, a track buffer is used to cache a whole track
271 * of the disk to save read operations. These are two separate buffers
272 * because that allows write operations without clearing the track buffer.
275 static int MaxSectors
[] = {
278 static int BufferSize
[] = {
279 15*512, 30*512, 60*512
282 #define BUFFER_SIZE (BufferSize[DriveType])
284 unsigned char *DMABuffer
; /* buffer for writes */
285 static unsigned long PhysDMABuffer
; /* physical address */
287 static int UseTrackbuffer
= -1; /* Do track buffering? */
288 MODULE_PARM(UseTrackbuffer
, "i");
290 unsigned char *TrackBuffer
; /* buffer for reads */
291 static unsigned long PhysTrackBuffer
; /* physical address */
292 static int BufferDrive
, BufferSide
, BufferTrack
;
293 static int read_track
; /* non-zero if we are reading whole tracks */
295 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
296 #define IS_BUFFERED(drive,side,track) \
297 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
300 * These are global variables, as that's the easiest way to give
301 * information to interrupts. They are the data used for the current
304 static int SelectedDrive
= 0;
305 static int ReqCmd
, ReqBlock
;
306 static int ReqSide
, ReqTrack
, ReqSector
, ReqCnt
;
307 static int HeadSettleFlag
= 0;
308 static unsigned char *ReqData
, *ReqBuffer
;
309 static int MotorOn
= 0, MotorOffTrys
;
310 static int IsFormatting
= 0, FormatError
;
312 static int UserSteprate
[FD_MAX_UNITS
] = { -1, -1 };
313 MODULE_PARM(UserSteprate
, "1-" __MODULE_STRING(FD_MAX_UNITS
) "i");
315 /* Synchronization of FDC access. */
316 static volatile int fdc_busy
= 0;
317 static struct wait_queue
*fdc_wait
= NULL
;
318 static struct wait_queue
*format_wait
= NULL
;
320 static unsigned int changed_floppies
= 0xff, fake_change
= 0;
321 #define CHECK_CHANGE_DELAY HZ/2
323 #define FD_MOTOR_OFF_DELAY (3*HZ)
324 #define FD_MOTOR_OFF_MAXTRY (10*20)
326 #define FLOPPY_TIMEOUT (6*HZ)
327 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
328 * will be recalibrated. */
329 #define MAX_ERRORS 8 /* After this many errors the driver
333 #define START_MOTOR_OFF_TIMER(delay) \
335 motor_off_timer.expires = jiffies + (delay); \
336 add_timer( &motor_off_timer ); \
340 #define START_CHECK_CHANGE_TIMER(delay) \
342 timer_table[FLOPPY_TIMER].expires = jiffies + (delay); \
343 timer_active |= (1 << FLOPPY_TIMER); \
346 #define START_TIMEOUT() \
347 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT)
349 #define STOP_TIMEOUT() \
350 del_timer(&timeout_timer)
354 * The driver is trying to determine the correct media format
355 * while Probing is set. fd_rwsec_done() clears it after a
358 static int Probing
= 0;
360 /* This flag is set when a dummy seek is necessary to make the WP
361 * status bit accessible.
363 static int NeedSeek
= 0;
367 #define DPRINT(a) printk a
372 /***************************** Prototypes *****************************/
374 static void fd_select_side( int side
);
375 static void fd_select_drive( int drive
);
376 static void fd_deselect( void );
377 static void fd_motor_off_timer( unsigned long dummy
);
378 static void check_change( void );
379 static __inline__
void set_head_settle_flag( void );
380 static __inline__
int get_head_settle_flag( void );
381 static void floppy_irq (int irq
, void *dummy
, struct pt_regs
*fp
);
382 static void fd_error( void );
383 static int do_format(kdev_t drive
, struct atari_format_descr
*desc
);
384 static void do_fd_action( int drive
);
385 static void fd_calibrate( void );
386 static void fd_calibrate_done( int status
);
387 static void fd_seek( void );
388 static void fd_seek_done( int status
);
389 static void fd_rwsec( void );
390 static void fd_readtrack_check( unsigned long dummy
);
391 static void fd_rwsec_done( int status
);
392 static void fd_rwsec_done1(int status
);
393 static void fd_writetrack( void );
394 static void fd_writetrack_done( int status
);
395 static void fd_times_out( unsigned long dummy
);
396 static void finish_fdc( void );
397 static void finish_fdc_done( int dummy
);
398 static void floppy_off( unsigned int nr
);
399 static __inline__
void copy_buffer( void *from
, void *to
);
400 static void setup_req_params( int drive
);
401 static void redo_fd_request( void);
402 static int invalidate_drive(kdev_t rdev
);
403 static int fd_ioctl( struct inode
*inode
, struct file
*filp
, unsigned int
404 cmd
, unsigned long param
);
405 static void fd_probe( int drive
);
406 static int fd_test_drive_present( int drive
);
407 static void config_types( void );
408 static int floppy_open( struct inode
*inode
, struct file
*filp
);
409 static int floppy_release( struct inode
* inode
, struct file
* filp
);
411 /************************* End of Prototypes **************************/
413 static struct timer_list motor_off_timer
=
414 { NULL
, NULL
, 0, 0, fd_motor_off_timer
};
415 static struct timer_list readtrack_timer
=
416 { NULL
, NULL
, 0, 0, fd_readtrack_check
};
418 static struct timer_list timeout_timer
=
419 { NULL
, NULL
, 0, 0, fd_times_out
};
423 /* Select the side to use. */
425 static void fd_select_side( int side
)
430 cli(); /* protect against various other ints mucking around with the PSG */
432 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
433 sound_ym
.wd_data
= (side
== 0) ? sound_ym
.rd_data_reg_sel
| 0x01 :
434 sound_ym
.rd_data_reg_sel
& 0xfe;
436 restore_flags(flags
);
440 /* Select a drive, update the FDC's track register and set the correct
441 * clock speed for this disk's type.
444 static void fd_select_drive( int drive
)
449 if (drive
== SelectedDrive
)
453 cli(); /* protect against various other ints mucking around with the PSG */
454 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
455 tmp
= sound_ym
.rd_data_reg_sel
;
456 sound_ym
.wd_data
= (tmp
| DSKDRVNONE
) & ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
457 atari_dont_touch_floppy_select
= 1;
458 restore_flags(flags
);
460 /* restore track register to saved value */
461 FDC_WRITE( FDCREG_TRACK
, UD
.track
);
464 /* select 8/16 MHz */
466 if (ATARIHW_PRESENT(FDCSPEED
))
467 dma_wd
.fdc_speed
= UDT
->fdc_speed
;
469 SelectedDrive
= drive
;
473 /* Deselect both drives. */
475 static void fd_deselect( void )
480 cli(); /* protect against various other ints mucking around with the PSG */
481 atari_dont_touch_floppy_select
= 0;
482 sound_ym
.rd_data_reg_sel
=14; /* Select PSG Port A */
483 sound_ym
.wd_data
= (sound_ym
.rd_data_reg_sel
|
484 (MACH_IS_FALCON
? 3 : 7)); /* no drives selected */
485 /* On Falcon, the drive B select line is used on the printer port, so
486 * leave it alone... */
488 restore_flags(flags
);
492 /* This timer function deselects the drives when the FDC switched the
493 * motor off. The deselection cannot happen earlier because the FDC
494 * counts the index signals, which arrive only if one drive is selected.
497 static void fd_motor_off_timer( unsigned long dummy
)
499 /* unsigned long flags; */
500 unsigned char status
;
503 del_timer( &motor_off_timer
);
505 if (SelectedDrive
< 0)
506 /* no drive selected, needn't deselect anyone */
509 /* save_flags(flags);
512 if (stdma_islocked())
515 status
= FDC_READ( FDCREG_STATUS
);
517 if (!(status
& 0x80)) {
518 /* motor already turned off by FDC -> deselect drives */
521 /* restore_flags(flags); */
524 /* not yet off, try again */
527 /* restore_flags(flags); */
528 /* Test again later; if tested too often, it seems there is no disk
529 * in the drive and the FDC will leave the motor on forever (or,
530 * at least until a disk is inserted). So we'll test only twice
531 * per second from then on...
533 delay
= (MotorOffTrys
< FD_MOTOR_OFF_MAXTRY
) ?
534 (++MotorOffTrys
, HZ
/20) : HZ
/2;
535 START_MOTOR_OFF_TIMER( delay
);
539 /* This function is repeatedly called to detect disk changes (as good
540 * as possible) and keep track of the current state of the write protection.
543 static void check_change( void )
545 static int drive
= 0;
548 unsigned char old_porta
;
551 if (++drive
> 1 || !UD
.connected
)
555 cli(); /* protect against various other ints mucking around with the PSG */
557 if (!stdma_islocked()) {
558 sound_ym
.rd_data_reg_sel
= 14;
559 old_porta
= sound_ym
.rd_data_reg_sel
;
560 sound_ym
.wd_data
= (old_porta
| DSKDRVNONE
) &
561 ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
562 stat
= !!(FDC_READ( FDCREG_STATUS
) & FDCSTAT_WPROT
);
563 sound_ym
.wd_data
= old_porta
;
565 if (stat
!= UD
.wpstat
) {
566 DPRINT(( "wpstat[%d] = %d\n", drive
, stat
));
568 set_bit (drive
, &changed_floppies
);
571 restore_flags(flags
);
573 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY
);
577 /* Handling of the Head Settling Flag: This flag should be set after each
578 * seek operation, because we don't use seeks with verify.
581 static __inline__
void set_head_settle_flag( void )
583 HeadSettleFlag
= FDCCMDADD_E
;
586 static __inline__
int get_head_settle_flag( void )
588 int tmp
= HeadSettleFlag
;
596 /* General Interrupt Handling */
598 static void (*FloppyIRQHandler
)( int status
) = NULL
;
600 static void floppy_irq (int irq
, void *dummy
, struct pt_regs
*fp
)
602 unsigned char status
;
603 void (*handler
)( int );
605 handler
= FloppyIRQHandler
;
606 FloppyIRQHandler
= NULL
;
610 status
= FDC_READ( FDCREG_STATUS
);
611 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status
,(unsigned long)handler
));
615 DPRINT(("FDC irq, no handler\n"));
620 /* Error handling: If some error happened, retry some times, then
621 * recalibrate, then try again, and fail after MAX_ERRORS.
624 static void fd_error( void )
629 wake_up( &format_wait
);
633 if (!CURRENT
) return;
635 if (CURRENT
->errors
>= MAX_ERRORS
) {
636 printk(KERN_ERR
"fd%d: too many errors.\n", SelectedDrive
);
639 else if (CURRENT
->errors
== RECALIBRATE_ERRORS
) {
640 printk(KERN_WARNING
"fd%d: recalibrating\n", SelectedDrive
);
641 if (SelectedDrive
!= -1)
649 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
652 /* ---------- Formatting ---------- */
654 #define FILL(n,val) \
656 memset( p, val, n ); \
660 static int do_format(kdev_t device
, struct atari_format_descr
*desc
)
665 int type
, drive
= MINOR(device
) & 3;
667 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
668 drive
, desc
->track
, desc
->head
, desc
->sect_offset
));
672 while( fdc_busy
) sleep_on( &fdc_wait
);
674 stdma_lock(floppy_irq
, NULL
);
675 atari_turnon_irq( IRQ_MFP_FDC
); /* should be already, just to be sure */
676 restore_flags(flags
);
678 type
= MINOR(device
) >> 2;
680 if (--type
>= NUM_DISK_MINORS
||
681 minor2disktype
[type
].drive_types
> DriveType
) {
685 type
= minor2disktype
[type
].index
;
686 UDT
= &disk_type
[type
];
689 if (!UDT
|| desc
->track
>= UDT
->blocks
/UDT
->spt
/2 || desc
->head
>= 2) {
696 /* The track buffer is used for the raw track data, so its
697 contents become invalid! */
699 /* stop deselect timer */
700 del_timer( &motor_off_timer
);
702 FILL( 60 * (nsect
/ 9), 0x4e );
703 for( sect
= 0; sect
< nsect
; ++sect
) {
709 *p
++ = (nsect
+ sect
- desc
->sect_offset
) % nsect
+ 1;
720 FILL( TrackBuffer
+BUFFER_SIZE
-p
, 0x4e );
724 ReqTrack
= desc
->track
;
725 ReqSide
= desc
->head
;
726 do_fd_action( drive
);
728 sleep_on( &format_wait
);
731 return( FormatError
? -EIO
: 0 );
735 /* do_fd_action() is the general procedure for a fd request: All
736 * required parameter settings (drive select, side select, track
737 * position) are checked and set if needed. For each of these
738 * parameters and the actual reading or writing exist two functions:
739 * one that starts the setting (or skips it if possible) and one
740 * callback for the "done" interrupt. Each done func calls the next
741 * set function to propagate the request down to fd_rwsec_done().
744 static void do_fd_action( int drive
)
746 DPRINT(("do_fd_action\n"));
748 if (UseTrackbuffer
&& !IsFormatting
) {
750 if (IS_BUFFERED( drive
, ReqSide
, ReqTrack
)) {
751 if (ReqCmd
== READ
) {
752 copy_buffer( SECTOR_BUFFER(ReqSector
), ReqData
);
753 if (++ReqCnt
< CURRENT
->current_nr_sectors
) {
754 /* read next sector */
755 setup_req_params( drive
);
759 /* all sectors finished */
760 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
761 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
768 /* cmd == WRITE, pay attention to track buffer
770 copy_buffer( ReqData
, SECTOR_BUFFER(ReqSector
) );
775 if (SelectedDrive
!= drive
)
776 fd_select_drive( drive
);
780 else if (UD
.track
!= ReqTrack
<< UDT
->stretch
)
782 else if (IsFormatting
)
789 /* Seek to track 0 if the current track is unknown */
791 static void fd_calibrate( void )
793 if (SUD
.track
>= 0) {
794 fd_calibrate_done( 0 );
798 if (ATARIHW_PRESENT(FDCSPEED
))
799 dma_wd
.fdc_speed
= 0; /* always seek with 8 Mhz */;
800 DPRINT(("fd_calibrate\n"));
801 SET_IRQ_HANDLER( fd_calibrate_done
);
802 /* we can't verify, since the speed may be incorrect */
803 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| SUD
.steprate
);
812 static void fd_calibrate_done( int status
)
814 DPRINT(("fd_calibrate_done()\n"));
817 /* set the correct speed now */
818 if (ATARIHW_PRESENT(FDCSPEED
))
819 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
820 if (status
& FDCSTAT_RECNF
) {
821 printk(KERN_ERR
"fd%d: restore failed\n", SelectedDrive
);
831 /* Seek the drive to the requested track. The drive must have been
832 * calibrated at some point before this.
835 static void fd_seek( void )
837 if (SUD
.track
== ReqTrack
<< SUDT
->stretch
) {
842 if (ATARIHW_PRESENT(FDCSPEED
)) {
843 dma_wd
.fdc_speed
= 0; /* always seek witch 8 Mhz */
847 DPRINT(("fd_seek() to track %d\n",ReqTrack
));
848 FDC_WRITE( FDCREG_DATA
, ReqTrack
<< SUDT
->stretch
);
850 SET_IRQ_HANDLER( fd_seek_done
);
851 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
| SUD
.steprate
);
854 set_head_settle_flag();
860 static void fd_seek_done( int status
)
862 DPRINT(("fd_seek_done()\n"));
865 /* set the correct speed */
866 if (ATARIHW_PRESENT(FDCSPEED
))
867 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
868 if (status
& FDCSTAT_RECNF
) {
869 printk(KERN_ERR
"fd%d: seek error (to track %d)\n",
870 SelectedDrive
, ReqTrack
);
871 /* we don't know exactly which track we are on now! */
876 SUD
.track
= ReqTrack
<< SUDT
->stretch
;
886 /* This does the actual reading/writing after positioning the head
887 * over the correct track.
890 static int MultReadInProgress
= 0;
893 static void fd_rwsec( void )
895 unsigned long paddr
, flags
;
896 unsigned int rwflag
, old_motoron
;
899 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector
, ReqCmd
== WRITE
? 'w' : 'r' ));
900 if (ReqCmd
== WRITE
) {
901 if (ATARIHW_PRESENT(EXTD_DMA
)) {
902 paddr
= (unsigned long)VTOP(ReqData
);
905 copy_buffer( ReqData
, DMABuffer
);
906 paddr
= PhysDMABuffer
;
908 dma_cache_maintenance( paddr
, 512, 1 );
913 paddr
= PhysTrackBuffer
;
915 paddr
= ATARIHW_PRESENT(EXTD_DMA
) ? VTOP(ReqData
) : PhysDMABuffer
;
919 fd_select_side( ReqSide
);
921 /* Start sector of this operation */
922 FDC_WRITE( FDCREG_SECTOR
, read_track
? 1 : ReqSector
);
924 /* Cheat for track if stretch != 0 */
926 track
= FDC_READ( FDCREG_TRACK
);
928 FDC_WRITE( FDCREG_TRACK
, track
>> SUDT
->stretch
);
935 dma_wd
.dma_lo
= (unsigned char)paddr
;
938 dma_wd
.dma_md
= (unsigned char)paddr
;
941 if (ATARIHW_PRESENT(EXTD_DMA
))
942 st_dma_ext_dmahi
= (unsigned short)paddr
;
944 dma_wd
.dma_hi
= (unsigned char)paddr
;
946 restore_flags(flags
);
948 /* Clear FIFO and switch DMA to correct mode */
949 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
951 dma_wd
.dma_mode_status
= 0x90 | (rwflag
^ 0x100);
953 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
956 /* How many sectors for DMA */
957 dma_wd
.fdc_acces_seccount
= read_track
? SUDT
->spt
: 1;
961 /* Start operation */
962 dma_wd
.dma_mode_status
= FDCSELREG_STP
| rwflag
;
964 SET_IRQ_HANDLER( fd_rwsec_done
);
965 dma_wd
.fdc_acces_seccount
=
966 (get_head_settle_flag() |
967 (rwflag
? FDCCMD_WRSEC
: (FDCCMD_RDSEC
| (read_track
? FDCCMDADD_M
: 0))));
969 old_motoron
= MotorOn
;
972 /* wait for interrupt */
975 /* If reading a whole track, wait about one disk rotation and
976 * then check if all sectors are read. The FDC will even
977 * search for the first non-existent sector and need 1 sec to
978 * recognise that it isn't present :-(
980 del_timer (&readtrack_timer
);
981 readtrack_timer
.expires
=
982 jiffies
+ HZ
/5 + (old_motoron
? 0 : HZ
);
983 /* 1 rot. + 5 rot.s if motor was off */
984 MultReadInProgress
= 1;
985 add_timer( &readtrack_timer
);
991 static void fd_readtrack_check( unsigned long dummy
)
993 unsigned long flags
, addr
, addr2
;
998 del_timer( &readtrack_timer
);
1000 if (!MultReadInProgress
) {
1001 /* This prevents a race condition that could arise if the
1002 * interrupt is triggered while the calling of this timer
1003 * callback function takes place. The IRQ function then has
1004 * already cleared 'MultReadInProgress' when flow of control
1007 restore_flags(flags
);
1011 /* get the current DMA address */
1012 /* ++ f.a. read twice to avoid being fooled by switcher */
1016 addr
= dma_wd
.dma_lo
& 0xff;
1018 addr
|= (dma_wd
.dma_md
& 0xff) << 8;
1020 if (ATARIHW_PRESENT( EXTD_DMA
))
1021 addr
|= (st_dma_ext_dmahi
& 0xffff) << 16;
1023 addr
|= (dma_wd
.dma_hi
& 0xff) << 16;
1025 } while(addr
!= addr2
);
1027 if (addr
>= PhysTrackBuffer
+ SUDT
->spt
*512) {
1028 /* already read enough data, force an FDC interrupt to stop
1029 * the read operation
1031 SET_IRQ_HANDLER( NULL
);
1032 MultReadInProgress
= 0;
1033 restore_flags(flags
);
1034 DPRINT(("fd_readtrack_check(): done\n"));
1035 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1038 /* No error until now -- the FDC would have interrupted
1044 /* not yet finished, wait another tenth rotation */
1045 restore_flags(flags
);
1046 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1047 readtrack_timer
.expires
= jiffies
+ HZ
/5/10;
1048 add_timer( &readtrack_timer
);
1053 static void fd_rwsec_done( int status
)
1055 DPRINT(("fd_rwsec_done()\n"));
1058 del_timer(&readtrack_timer
);
1059 if (!MultReadInProgress
)
1061 MultReadInProgress
= 0;
1063 fd_rwsec_done1(status
);
1066 static void fd_rwsec_done1(int status
)
1072 /* Correct the track if stretch != 0 */
1073 if (SUDT
->stretch
) {
1074 track
= FDC_READ( FDCREG_TRACK
);
1076 FDC_WRITE( FDCREG_TRACK
, track
<< SUDT
->stretch
);
1079 if (!UseTrackbuffer
) {
1080 dma_wd
.dma_mode_status
= 0x90;
1082 if (!(dma_wd
.dma_mode_status
& 0x01)) {
1083 printk(KERN_ERR
"fd%d: DMA error\n", SelectedDrive
);
1089 if (ReqCmd
== WRITE
&& (status
& FDCSTAT_WPROT
)) {
1090 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1093 if ((status
& FDCSTAT_RECNF
) &&
1094 /* RECNF is no error after a multiple read when the FDC
1095 searched for a non-existent sector! */
1096 !(read_track
&& FDC_READ(FDCREG_SECTOR
) > SUDT
->spt
)) {
1098 if (SUDT
> disk_type
) {
1099 if (SUDT
[-1].blocks
> ReqBlock
) {
1100 /* try another disk type */
1102 floppy_sizes
[SelectedDrive
] = SUDT
->blocks
>> 1;
1107 if (SUD
.flags
& FTD_MSG
)
1108 printk(KERN_INFO
"fd%d: Auto-detected floppy type %s\n",
1109 SelectedDrive
, SUDT
->name
);
1113 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1114 if (SUD
.autoprobe
) {
1115 SUDT
= disk_type
+ StartDiskType
[DriveType
];
1116 floppy_sizes
[SelectedDrive
] = SUDT
->blocks
>> 1;
1121 if (ATARIHW_PRESENT(FDCSPEED
)) {
1122 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
1125 setup_req_params( SelectedDrive
);
1127 do_fd_action( SelectedDrive
);
1131 printk(KERN_ERR
"fd%d: sector %d not found (side %d, track %d)\n",
1132 SelectedDrive
, FDC_READ (FDCREG_SECTOR
), ReqSide
, ReqTrack
);
1135 if (status
& FDCSTAT_CRC
) {
1136 printk(KERN_ERR
"fd%d: CRC error (side %d, track %d, sector %d)\n",
1137 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1140 if (status
& FDCSTAT_LOST
) {
1141 printk(KERN_ERR
"fd%d: lost data (side %d, track %d, sector %d)\n",
1142 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1148 if (ReqCmd
== READ
) {
1151 addr
= ATARIHW_PRESENT( EXTD_DMA
) ? ReqData
: DMABuffer
;
1152 dma_cache_maintenance( VTOP(addr
), 512, 0 );
1153 if (!ATARIHW_PRESENT( EXTD_DMA
))
1154 copy_buffer (addr
, ReqData
);
1156 dma_cache_maintenance( PhysTrackBuffer
, MaxSectors
[DriveType
] * 512, 0 );
1157 BufferDrive
= SelectedDrive
;
1158 BufferSide
= ReqSide
;
1159 BufferTrack
= ReqTrack
;
1160 copy_buffer (SECTOR_BUFFER (ReqSector
), ReqData
);
1164 if (++ReqCnt
< CURRENT
->current_nr_sectors
) {
1165 /* read next sector */
1166 setup_req_params( SelectedDrive
);
1167 do_fd_action( SelectedDrive
);
1170 /* all sectors finished */
1171 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
1172 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
1184 static void fd_writetrack( void )
1186 unsigned long paddr
, flags
;
1189 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack
, ReqSide
));
1191 paddr
= PhysTrackBuffer
;
1192 dma_cache_maintenance( paddr
, BUFFER_SIZE
, 1 );
1194 fd_select_side( ReqSide
);
1196 /* Cheat for track if stretch != 0 */
1197 if (SUDT
->stretch
) {
1198 track
= FDC_READ( FDCREG_TRACK
);
1200 FDC_WRITE(FDCREG_TRACK
,track
>> SUDT
->stretch
);
1207 dma_wd
.dma_lo
= (unsigned char)paddr
;
1210 dma_wd
.dma_md
= (unsigned char)paddr
;
1213 if (ATARIHW_PRESENT( EXTD_DMA
))
1214 st_dma_ext_dmahi
= (unsigned short)paddr
;
1216 dma_wd
.dma_hi
= (unsigned char)paddr
;
1218 restore_flags(flags
);
1220 /* Clear FIFO and switch DMA to correct mode */
1221 dma_wd
.dma_mode_status
= 0x190;
1223 dma_wd
.dma_mode_status
= 0x90;
1225 dma_wd
.dma_mode_status
= 0x190;
1228 /* How many sectors for DMA */
1229 dma_wd
.fdc_acces_seccount
= BUFFER_SIZE
/512;
1232 /* Start operation */
1233 dma_wd
.dma_mode_status
= FDCSELREG_STP
| 0x100;
1235 SET_IRQ_HANDLER( fd_writetrack_done
);
1236 dma_wd
.fdc_acces_seccount
= FDCCMD_WRTRA
| get_head_settle_flag();
1240 /* wait for interrupt */
1244 static void fd_writetrack_done( int status
)
1246 DPRINT(("fd_writetrack_done()\n"));
1250 if (status
& FDCSTAT_WPROT
) {
1251 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1254 if (status
& FDCSTAT_LOST
) {
1255 printk(KERN_ERR
"fd%d: lost data (side %d, track %d)\n",
1256 SelectedDrive
, ReqSide
, ReqTrack
);
1260 wake_up( &format_wait
);
1267 static void fd_times_out( unsigned long dummy
)
1269 atari_disable_irq( IRQ_MFP_FDC
);
1270 if (!FloppyIRQHandler
) goto end
; /* int occurred after timer was fired, but
1271 * before we came here... */
1273 SET_IRQ_HANDLER( NULL
);
1274 /* If the timeout occurred while the readtrack_check timer was
1275 * active, we need to cancel it, else bad things will happen */
1277 del_timer( &readtrack_timer
);
1278 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1281 printk(KERN_ERR
"floppy timeout\n" );
1284 atari_enable_irq( IRQ_MFP_FDC
);
1288 /* The (noop) seek operation here is needed to make the WP bit in the
1289 * FDC status register accessible for check_change. If the last disk
1290 * operation would have been a RDSEC, this bit would always read as 0
1291 * no matter what :-( To save time, the seek goes to the track we're
1295 static void finish_fdc( void )
1298 finish_fdc_done( 0 );
1301 DPRINT(("finish_fdc: dummy seek started\n"));
1302 FDC_WRITE (FDCREG_DATA
, SUD
.track
);
1303 SET_IRQ_HANDLER( finish_fdc_done
);
1304 FDC_WRITE (FDCREG_CMD
, FDCCMD_SEEK
);
1307 /* we must wait for the IRQ here, because the ST-DMA
1308 is released immediately afterwards and the interrupt
1309 may be delivered to the wrong driver. */
1314 static void finish_fdc_done( int dummy
)
1316 unsigned long flags
;
1318 DPRINT(("finish_fdc_done entered\n"));
1322 if ((timer_active
& (1 << FLOPPY_TIMER
)) &&
1323 timer_table
[FLOPPY_TIMER
].expires
< jiffies
+ 5)
1324 /* If the check for a disk change is done too early after this
1325 * last seek command, the WP bit still reads wrong :-((
1327 timer_table
[FLOPPY_TIMER
].expires
= jiffies
+ 5;
1329 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY
);
1330 del_timer( &motor_off_timer
);
1331 START_MOTOR_OFF_TIMER( FD_MOTOR_OFF_DELAY
);
1337 wake_up( &fdc_wait
);
1338 restore_flags(flags
);
1340 DPRINT(("finish_fdc() finished\n"));
1344 /* Prevent "aliased" accesses. */
1345 static int fd_ref
[4] = { 0,0,0,0 };
1346 static int fd_device
[4] = { 0,0,0,0 };
1349 * Current device number. Taken either from the block header or from the
1350 * format request descriptor.
1352 #define CURRENT_DEVICE (CURRENT->rq_dev)
1354 /* Current error count. */
1355 #define CURRENT_ERRORS (CURRENT->errors)
1358 /* dummy for blk.h */
1359 static void floppy_off( unsigned int nr
) {}
1362 /* The detection of disk changes is a dark chapter in Atari history :-(
1363 * Because the "Drive ready" signal isn't present in the Atari
1364 * hardware, one has to rely on the "Write Protect". This works fine,
1365 * as long as no write protected disks are used. TOS solves this
1366 * problem by introducing tri-state logic ("maybe changed") and
1367 * looking at the serial number in block 0. This isn't possible for
1368 * Linux, since the floppy driver can't make assumptions about the
1369 * filesystem used on the disk and thus the contents of block 0. I've
1370 * chosen the method to always say "The disk was changed" if it is
1371 * unsure whether it was. This implies that every open or mount
1372 * invalidates the disk buffers if you work with write protected
1373 * disks. But at least this is better than working with incorrect data
1374 * due to unrecognised disk changes.
1377 static int check_floppy_change (kdev_t dev
)
1379 unsigned int drive
= MINOR(dev
) & 0x03;
1381 if (MAJOR(dev
) != MAJOR_NR
) {
1382 printk(KERN_ERR
"floppy_changed: not a floppy\n");
1386 if (test_bit (drive
, &fake_change
)) {
1387 /* simulated change (e.g. after formatting) */
1390 if (test_bit (drive
, &changed_floppies
)) {
1391 /* surely changed (the WP signal changed at least once) */
1395 /* WP is on -> could be changed: to be sure, buffers should be
1404 static int floppy_revalidate (kdev_t dev
)
1406 int drive
= MINOR(dev
) & 3;
1408 if (test_bit(drive
, &changed_floppies
) ||
1409 test_bit(drive
, &fake_change
) ||
1410 unit
[drive
].disktype
== 0) {
1411 if (UD
.flags
& FTD_MSG
)
1412 printk(KERN_ERR
"floppy: clear format %p!\n", UDT
);
1414 clear_bit(drive
, &fake_change
);
1415 clear_bit(drive
, &changed_floppies
);
1416 /* MSch: clearing geometry makes sense only for autoprobe
1417 formats, for 'permanent user-defined' parameter:
1418 restore default_params[] here if flagged valid! */
1419 if (default_params
[drive
].blocks
== 0)
1422 UDT
= &default_params
[drive
];
1427 static __inline__
void copy_buffer(void *from
, void *to
)
1429 ulong
*p1
= (ulong
*)from
, *p2
= (ulong
*)to
;
1432 for( cnt
= 512/4; cnt
; cnt
-- )
1437 /* This sets up the global variables describing the current request. */
1439 static void setup_req_params( int drive
)
1441 int block
= ReqBlock
+ ReqCnt
;
1443 ReqTrack
= block
/ UDT
->spt
;
1444 ReqSector
= block
- ReqTrack
* UDT
->spt
+ 1;
1445 ReqSide
= ReqTrack
& 1;
1447 ReqData
= ReqBuffer
+ 512 * ReqCnt
;
1450 read_track
= (ReqCmd
== READ
&& CURRENT_ERRORS
== 0);
1454 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide
,
1455 ReqTrack
, ReqSector
, (unsigned long)ReqData
));
1459 static void redo_fd_request(void)
1461 int device
, drive
, type
;
1463 DPRINT(("redo_fd_request: CURRENT=%08lx CURRENT->dev=%04x CURRENT->sector=%ld\n",
1464 (unsigned long)CURRENT
, CURRENT
? CURRENT
->rq_dev
: 0,
1465 CURRENT
? CURRENT
->sector
: 0 ));
1469 if (CURRENT
&& CURRENT
->rq_status
== RQ_INACTIVE
){
1478 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
1479 panic(DEVICE_NAME
": request list destroyed");
1481 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
1482 panic(DEVICE_NAME
": block not locked");
1484 device
= MINOR(CURRENT_DEVICE
);
1488 if (!UD
.connected
) {
1489 /* drive not connected */
1490 printk(KERN_ERR
"Unknown Device: fd%d\n", drive
);
1498 UDT
= disk_type
+ StartDiskType
[DriveType
];
1499 floppy_sizes
[drive
] = UDT
->blocks
>> 1;
1504 /* user supplied disk type */
1505 if (--type
>= NUM_DISK_MINORS
) {
1506 printk(KERN_WARNING
"fd%d: invalid disk format", drive
);
1510 if (minor2disktype
[type
].drive_types
> DriveType
) {
1511 printk(KERN_WARNING
"fd%d: unsupported disk format", drive
);
1515 type
= minor2disktype
[type
].index
;
1516 UDT
= &disk_type
[type
];
1517 floppy_sizes
[drive
] = UDT
->blocks
>> 1;
1521 if (CURRENT
->sector
+ 1 > UDT
->blocks
) {
1526 /* stop deselect timer */
1527 del_timer( &motor_off_timer
);
1530 ReqCmd
= CURRENT
->cmd
;
1531 ReqBlock
= CURRENT
->sector
;
1532 ReqBuffer
= CURRENT
->buffer
;
1533 setup_req_params( drive
);
1534 do_fd_action( drive
);
1543 void do_fd_request(void)
1545 unsigned long flags
;
1547 DPRINT(("do_fd_request for pid %d\n",current
->pid
));
1548 while( fdc_busy
) sleep_on( &fdc_wait
);
1550 stdma_lock(floppy_irq
, NULL
);
1552 atari_disable_irq( IRQ_MFP_FDC
);
1553 save_flags(flags
); /* The request function is called with ints
1554 sti(); * disabled... so must save the IPL for later */
1556 restore_flags(flags
);
1557 atari_enable_irq( IRQ_MFP_FDC
);
1561 static int invalidate_drive(kdev_t rdev
)
1563 /* invalidate the buffer track to force a reread */
1565 set_bit(MINOR(rdev
) & 3, &fake_change
);
1566 check_disk_change(rdev
);
1570 static int fd_ioctl(struct inode
*inode
, struct file
*filp
,
1571 unsigned int cmd
, unsigned long param
)
1573 #define IOCTL_MODE_BIT 8
1574 #define OPEN_WRITE_BIT 16
1575 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
1579 struct atari_format_descr fmt_desc
;
1580 struct atari_disk_type
*dtp
;
1581 struct floppy_struct getprm
;
1583 struct floppy_struct setprm
;
1585 device
= inode
->i_rdev
;
1587 RO_IOCTLS (device
, param
);
1589 drive
= MINOR (device
);
1596 if (--type
>= NUM_DISK_MINORS
)
1598 if (minor2disktype
[type
].drive_types
> DriveType
)
1600 type
= minor2disktype
[type
].index
;
1601 dtp
= &disk_type
[type
];
1602 if (UD
.flags
& FTD_MSG
)
1603 printk (KERN_ERR
"floppy%d: found dtp %p name %s!\n",
1604 drive
, dtp
, dtp
->name
);
1612 if (cmd
== BLKGETSIZE
)
1613 return put_user(dtp
->blocks
, (long *)param
);
1615 memset((void *)&getprm
, 0, sizeof(getprm
));
1616 getprm
.size
= dtp
->blocks
;
1617 getprm
.sect
= dtp
->spt
;
1619 getprm
.track
= dtp
->blocks
/dtp
->spt
/2;
1620 getprm
.stretch
= dtp
->stretch
;
1621 if (copy_to_user((void *)param
, &getprm
, sizeof(getprm
)))
1625 if (!capable(CAP_SYS_ADMIN
))
1629 read_ahead
[MAJOR(inode
->i_rdev
)] = param
;
1632 return put_user(read_ahead
[MAJOR(inode
->i_rdev
)],
1635 if (!capable(CAP_SYS_ADMIN
))
1637 fsync_dev(inode
->i_rdev
);
1638 invalidate_buffers(inode
->i_rdev
);
1647 * MSch 7/96: simple 'set geometry' case: just set the
1648 * 'default' device params (minor == 0).
1649 * Currently, the drive geometry is cleared after each
1650 * disk change and subsequent revalidate()! simple
1651 * implementation of FDDEFPRM: save geometry from a
1652 * FDDEFPRM call and restore it in floppy_revalidate() !
1655 /* get the parameters from user space */
1656 if (fd_ref
[drive
] != 1 && fd_ref
[drive
] != -1)
1658 if (copy_from_user(&setprm
, (void *) param
, sizeof(setprm
)))
1661 * first of all: check for floppy change and revalidate,
1662 * or the next access will revalidate - and clear UDT :-(
1665 if (check_floppy_change(device
))
1666 floppy_revalidate(device
);
1668 if (UD
.flags
& FTD_MSG
)
1669 printk (KERN_INFO
"floppy%d: setting size %d spt %d str %d!\n",
1670 drive
, setprm
.size
, setprm
.sect
, setprm
.stretch
);
1672 /* what if type > 0 here? Overwrite specified entry ? */
1674 /* refuse to re-set a predefined type for now */
1680 * type == 0: first look for a matching entry in the type list,
1681 * and set the UD.disktype field to use the perdefined entry.
1682 * TODO: add user-defined format to head of autoprobe list ?
1683 * Useful to include the user-type for future autodetection!
1686 for (settype
= 0; settype
< NUM_DISK_MINORS
; settype
++) {
1688 if (minor2disktype
[settype
].drive_types
> DriveType
) {
1689 /* skip this one, invalid for drive ... */
1692 setidx
= minor2disktype
[settype
].index
;
1693 dtp
= &disk_type
[setidx
];
1695 /* found matching entry ?? */
1696 if ( dtp
->blocks
== setprm
.size
1697 && dtp
->spt
== setprm
.sect
1698 && dtp
->stretch
== setprm
.stretch
) {
1699 if (UD
.flags
& FTD_MSG
)
1700 printk (KERN_INFO
"floppy%d: setting %s %p!\n",
1701 drive
, dtp
->name
, dtp
);
1703 floppy_sizes
[drive
] = UDT
->blocks
>> 1;
1705 if (cmd
== FDDEFPRM
) {
1706 /* save settings as permanent default type */
1707 default_params
[drive
].name
= dtp
->name
;
1708 default_params
[drive
].spt
= dtp
->spt
;
1709 default_params
[drive
].blocks
= dtp
->blocks
;
1710 default_params
[drive
].fdc_speed
= dtp
->fdc_speed
;
1711 default_params
[drive
].stretch
= dtp
->stretch
;
1719 /* no matching disk type found above - setting user_params */
1721 if (cmd
== FDDEFPRM
) {
1722 /* set permanent type */
1723 dtp
= &default_params
[drive
];
1725 /* set user type (reset by disk change!) */
1726 dtp
= &user_params
[drive
];
1728 dtp
->name
= "user format";
1729 dtp
->blocks
= setprm
.size
;
1730 dtp
->spt
= setprm
.sect
;
1731 if (setprm
.sect
> 14)
1735 dtp
->stretch
= setprm
.stretch
;
1737 if (UD
.flags
& FTD_MSG
)
1738 printk (KERN_INFO
"floppy%d: blk %d spt %d str %d!\n",
1739 drive
, dtp
->blocks
, dtp
->spt
, dtp
->stretch
);
1742 if (!dtp
|| setprm
.track
!= dtp
->blocks
/dtp
->spt
/2 ||
1749 floppy_sizes
[drive
] = UDT
->blocks
>> 1;
1753 UD
.flags
|= FTD_MSG
;
1756 UD
.flags
&= ~FTD_MSG
;
1758 case FDSETEMSGTRESH
:
1763 if (fd_ref
[drive
] != 1 && fd_ref
[drive
] != -1)
1765 if (copy_from_user(&fmt_desc
, (void *) param
, sizeof(fmt_desc
)))
1767 return do_format(device
, &fmt_desc
);
1770 /* MSch: invalidate default_params */
1771 default_params
[drive
].blocks
= 0;
1772 floppy_sizes
[drive
] = MAX_DISK_SIZE
;
1773 return invalidate_drive (device
);
1776 return invalidate_drive(device
);
1782 /* Initialize the 'unit' variable for drive 'drive' */
1784 __initfunc(static void fd_probe( int drive
))
1789 if (!fd_test_drive_present( drive
))
1794 switch( UserSteprate
[drive
] ) {
1796 UD
.steprate
= FDCSTEP_2
;
1799 UD
.steprate
= FDCSTEP_3
;
1802 UD
.steprate
= FDCSTEP_6
;
1805 UD
.steprate
= FDCSTEP_12
;
1807 default: /* should be -1 for "not set by user" */
1808 if (ATARIHW_PRESENT( FDCSPEED
) || MACH_IS_MEDUSA
)
1809 UD
.steprate
= FDCSTEP_3
;
1811 UD
.steprate
= FDCSTEP_6
;
1814 MotorOn
= 1; /* from probe restore operation! */
1818 /* This function tests the physical presence of a floppy drive (not
1819 * whether a disk is inserted). This is done by issuing a restore
1820 * command, waiting max. 2 seconds (that should be enough to move the
1821 * head across the whole disk) and looking at the state of the "TR00"
1822 * signal. This should now be raised if there is a drive connected
1823 * (and there is no hardware failure :-) Otherwise, the drive is
1827 __initfunc(static int fd_test_drive_present( int drive
))
1829 unsigned long timeout
;
1830 unsigned char status
;
1833 if (drive
>= (MACH_IS_FALCON
? 1 : 2)) return( 0 );
1834 fd_select_drive( drive
);
1836 /* disable interrupt temporarily */
1837 atari_turnoff_irq( IRQ_MFP_FDC
);
1838 FDC_WRITE (FDCREG_TRACK
, 0xff00);
1839 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| FDCCMDADD_H
| FDCSTEP_6
);
1841 for( ok
= 0, timeout
= jiffies
+ 2*HZ
+HZ
/2; jiffies
< timeout
; ) {
1842 if (!(mfp
.par_dt_reg
& 0x20))
1846 status
= FDC_READ( FDCREG_STATUS
);
1847 ok
= (status
& FDCSTAT_TR00
) != 0;
1849 /* force interrupt to abort restore operation (FDC would try
1850 * about 50 seconds!) */
1851 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1853 status
= FDC_READ( FDCREG_STATUS
);
1857 /* dummy seek command to make WP bit accessible */
1858 FDC_WRITE( FDCREG_DATA
, 0 );
1859 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
);
1860 while( mfp
.par_dt_reg
& 0x20 )
1862 status
= FDC_READ( FDCREG_STATUS
);
1865 atari_turnon_irq( IRQ_MFP_FDC
);
1870 /* Look how many and which kind of drives are connected. If there are
1871 * floppies, additionally start the disk-change and motor-off timers.
1874 __initfunc(static void config_types( void ))
1878 /* for probing drives, set the FDC speed to 8 MHz */
1879 if (ATARIHW_PRESENT(FDCSPEED
))
1880 dma_wd
.fdc_speed
= 0;
1882 printk(KERN_INFO
"Probing floppy drive(s):\n");
1883 for( drive
= 0; drive
< FD_MAX_UNITS
; drive
++ ) {
1886 printk(KERN_INFO
"fd%d\n", drive
);
1891 if (FDC_READ( FDCREG_STATUS
) & FDCSTAT_BUSY
) {
1892 /* If FDC is still busy from probing, give it another FORCI
1893 * command to abort the operation. If this isn't done, the FDC
1894 * will interrupt later and its IRQ line stays low, because
1895 * the status register isn't read. And this will block any
1896 * interrupts on this IRQ line :-(
1898 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1900 FDC_READ( FDCREG_STATUS
);
1905 START_MOTOR_OFF_TIMER( FD_MOTOR_OFF_DELAY
);
1906 if (cnt
== 1) fd_select_drive( 0 );
1907 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY
);
1912 * floppy_open check for aliasing (/dev/fd0 can be the same as
1913 * /dev/PS0 etc), and disallows simultaneous access to the same
1914 * drive with different device numbers.
1917 static int floppy_open( struct inode
*inode
, struct file
*filp
)
1923 DPRINT (("Weird, open called with filp=0\n"));
1927 drive
= MINOR(inode
->i_rdev
) & 3;
1928 type
= MINOR(inode
->i_rdev
) >> 2;
1929 DPRINT(("fd_open: type=%d\n",type
));
1930 if (drive
>= FD_MAX_UNITS
|| type
> NUM_DISK_MINORS
)
1933 old_dev
= fd_device
[drive
];
1935 if (fd_ref
[drive
] && old_dev
!= MINOR(inode
->i_rdev
))
1938 if (fd_ref
[drive
] == -1 || (fd_ref
[drive
] && filp
->f_flags
& O_EXCL
))
1943 if (filp
->f_flags
& O_EXCL
)
1948 fd_device
[drive
] = MINOR(inode
->i_rdev
);
1950 if (old_dev
&& old_dev
!= MINOR(inode
->i_rdev
))
1951 invalidate_buffers(MKDEV(FLOPPY_MAJOR
, old_dev
));
1953 /* Allow ioctls if we have write-permissions even if read-only open */
1954 if (filp
->f_mode
& 2 || permission (inode
, 2) == 0)
1955 filp
->f_mode
|= IOCTL_MODE_BIT
;
1956 if (filp
->f_mode
& 2)
1957 filp
->f_mode
|= OPEN_WRITE_BIT
;
1959 if (filp
->f_flags
& O_NDELAY
)
1962 if (filp
->f_mode
& 3) {
1963 check_disk_change(inode
->i_rdev
);
1964 if (filp
->f_mode
& 2) {
1966 floppy_release(inode
, filp
);
1976 static int floppy_release( struct inode
* inode
, struct file
* filp
)
1980 drive
= MINOR(inode
->i_rdev
) & 3;
1983 * If filp is NULL, we're being called from blkdev_release
1984 * or after a failed mount attempt. In the former case the
1985 * device has already been sync'ed, and in the latter no
1986 * sync is required. Otherwise, sync if filp is writable.
1988 if (filp
&& (filp
->f_mode
& (2 | OPEN_WRITE_BIT
)))
1989 block_fsync (filp
, filp
->f_dentry
);
1991 if (fd_ref
[drive
] < 0)
1993 else if (!fd_ref
[drive
]--) {
1994 printk(KERN_ERR
"floppy_release with fd_ref == 0");
2002 static struct file_operations floppy_fops
= {
2003 NULL
, /* lseek - default */
2004 block_read
, /* read - general block-dev read */
2005 block_write
, /* write - general block-dev write */
2006 NULL
, /* readdir - bad */
2008 fd_ioctl
, /* ioctl */
2010 floppy_open
, /* open */
2012 floppy_release
, /* release */
2013 block_fsync
, /* fsync */
2015 check_floppy_change
, /* media_change */
2016 floppy_revalidate
, /* revalidate */
2019 __initfunc(int atari_floppy_init (void))
2024 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2028 /* Hades doesn't have Atari-compatible floppy */
2031 if (register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
2032 printk(KERN_ERR
"Unable to get major %d for floppy\n",MAJOR_NR
);
2036 if (UseTrackbuffer
< 0)
2037 /* not set by user -> use default: for now, we turn
2038 track buffering off for all Medusas, though it
2039 could be used with ones that have a counter
2040 card. But the test is too hard :-( */
2041 UseTrackbuffer
= !MACH_IS_MEDUSA
;
2043 /* initialize variables */
2047 /* initialize check_change timer */
2048 timer_table
[FLOPPY_TIMER
].fn
= check_change
;
2049 timer_active
&= ~(1 << FLOPPY_TIMER
);
2051 DMABuffer
= atari_stram_alloc( BUFFER_SIZE
+512, NULL
, "ataflop" );
2053 printk(KERN_ERR
"atari_floppy_init: cannot get dma buffer\n");
2054 unregister_blkdev(MAJOR_NR
, "fd");
2057 TrackBuffer
= DMABuffer
+ 512;
2058 PhysDMABuffer
= (unsigned long) VTOP(DMABuffer
);
2059 PhysTrackBuffer
= (unsigned long) VTOP(TrackBuffer
);
2060 BufferDrive
= BufferSide
= BufferTrack
= -1;
2062 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
2067 for (i
= 0; i
< 256; i
++)
2068 if ((i
>> 2) > 0 && (i
>> 2) <= NUM_DISK_MINORS
) {
2069 int type
= minor2disktype
[(i
>> 2) - 1].index
;
2070 floppy_sizes
[i
] = disk_type
[type
].blocks
>> 1;
2072 floppy_sizes
[i
] = MAX_DISK_SIZE
;
2074 blk_size
[MAJOR_NR
] = floppy_sizes
;
2075 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
2076 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
2078 printk(KERN_INFO
"Atari floppy driver: max. %cD, %strack buffering\n",
2079 DriveType
== 0 ? 'D' : DriveType
== 1 ? 'H' : 'E',
2080 UseTrackbuffer
? "" : "no ");
2083 (void)do_floppy
; /* avoid warning about unused variable */
2088 __initfunc(void atari_floppy_setup( char *str
, int *ints
))
2093 printk(KERN_ERR
"ataflop_setup: no arguments!\n" );
2096 else if (ints
[0] > 2+FD_MAX_UNITS
) {
2097 printk(KERN_ERR
"ataflop_setup: too many arguments\n" );
2100 if (ints
[1] < 0 || ints
[1] > 2)
2101 printk(KERN_ERR
"ataflop_setup: bad drive type\n" );
2103 DriveType
= ints
[1];
2106 UseTrackbuffer
= (ints
[2] > 0);
2108 for( i
= 3; i
<= ints
[0] && i
-3 < FD_MAX_UNITS
; ++i
) {
2109 if (ints
[i
] != 2 && ints
[i
] != 3 && ints
[i
] != 6 && ints
[i
] != 12)
2110 printk(KERN_ERR
"ataflop_setup: bad steprate\n" );
2112 UserSteprate
[i
-3] = ints
[i
];
2117 int init_module (void)
2121 return atari_floppy_init ();
2124 void cleanup_module (void)
2126 unregister_blkdev(MAJOR_NR
, "fd");
2128 blk_dev
[MAJOR_NR
].request_fn
= 0;
2129 timer_active
&= ~(1 << FLOPPY_TIMER
);
2130 timer_table
[FLOPPY_TIMER
].fn
= 0;
2131 atari_stram_free( DMABuffer
);