64 #include <linux/module.h>
78 #define FD_MAX_UNITS 2
83 static struct request *fd_request;
87 static struct atari_disk_type {
93 } atari_disk_type[] = {
94 {
"d360", 9, 720, 0, 0},
95 {
"D360", 9, 720, 0, 1},
96 {
"D720", 9,1440, 0, 0},
97 {
"D820", 10,1640, 0, 0},
100 {
"h1200",15,2400, 3, 0},
101 {
"H1440",18,2880, 3, 0},
102 {
"H1640",20,3280, 3, 0},
104 #define MAX_TYPE_HD 6
105 {
"E2880",36,5760, 3, 0},
106 {
"E3280",40,6560, 3, 0},
108 #define MAX_TYPE_ED 8
110 {
"H1680",21,3360, 3, 0},
111 {
"h410",10,820, 0, 1},
112 {
"h1476",18,2952, 3, 0},
113 {
"H1722",21,3444, 3, 0},
114 {
"h420",10,840, 0, 1},
115 {
"H830",10,1660, 0, 0},
116 {
"h1494",18,2952, 3, 0},
117 {
"H1743",21,3486, 3, 0},
118 {
"h880",11,1760, 0, 0},
119 {
"D1040",13,2080, 0, 0},
120 {
"D1120",14,2240, 0, 0},
121 {
"h1600",20,3200, 3, 0},
122 {
"H1760",22,3520, 3, 0},
123 {
"H1920",24,3840, 3, 0},
124 {
"E3200",40,6400, 3, 0},
125 {
"E3520",44,7040, 3, 0},
126 {
"E3840",48,7680, 3, 0},
127 {
"H1840",23,3680, 3, 0},
128 {
"D800",10,1600, 0, 0},
131 static int StartDiskType[] = {
141 static int DriveType =
TYPE_HD;
149 } minor2disktype[] = {
184 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
190 #define MAX_DISK_SIZE 3280
196 static struct atari_disk_type user_params[
FD_MAX_UNITS];
204 static struct atari_disk_type default_params[
FD_MAX_UNITS];
207 static struct atari_floppy_struct {
211 struct atari_disk_type *disktype;
215 unsigned int steprate;
219 struct gendisk *disk;
224 #define UD unit[drive]
225 #define UDT unit[drive].disktype
226 #define SUD unit[SelectedDrive]
227 #define SUDT unit[SelectedDrive].disktype
230 #define FDC_READ(reg) ({ \
232 unsigned short __val; \
234 dma_wd.dma_mode_status = 0x80 | (reg); \
236 __val = dma_wd.fdc_acces_seccount; \
242 #define FDC_WRITE(reg,val) \
246 dma_wd.dma_mode_status = 0x80 | (reg); \
248 dma_wd.fdc_acces_seccount = (val); \
261 static int MaxSectors[] = {
265 15*512, 30*512, 60*512
268 #define BUFFER_SIZE (BufferSize[DriveType])
271 static unsigned long PhysDMABuffer;
273 static int UseTrackbuffer = -1;
277 static unsigned long PhysTrackBuffer;
278 static int BufferDrive, BufferSide, BufferTrack;
279 static int read_track;
281 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
282 #define IS_BUFFERED(drive,side,track) \
283 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
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;
302 static volatile int fdc_busy = 0;
306 static unsigned long changed_floppies = 0xff, fake_change = 0;
307 #define CHECK_CHANGE_DELAY HZ/2
309 #define FD_MOTOR_OFF_DELAY (3*HZ)
310 #define FD_MOTOR_OFF_MAXTRY (10*20)
312 #define FLOPPY_TIMEOUT (6*HZ)
313 #define RECALIBRATE_ERRORS 4
324 static int Probing = 0;
329 static int NeedSeek = 0;
333 #define DPRINT(a) printk a
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 );
346 static void fd_error(
void );
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);
366 static void fd_probe(
int drive );
367 static int fd_test_drive_present(
int drive );
368 static void config_types(
void );
370 static int floppy_release(
struct gendisk *disk,
fmode_t mode);
374 static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
375 static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
379 static void fd_end_request_cur(
int err)
385 static inline void start_motor_off_timer(
void)
391 static inline void start_check_change_timer(
void )
396 static inline void start_timeout(
void)
401 static inline void stop_timeout(
void)
408 static void fd_select_side(
int side )
427 static void fd_select_drive(
int drive )
432 if (drive == SelectedDrive)
452 SelectedDrive = drive;
458 static void fd_deselect(
void )
480 static void fd_motor_off_timer(
unsigned long dummy )
484 if (SelectedDrive < 0)
493 if (!(status & 0x80)) {
516 static void check_change(
unsigned long dummy )
518 static int drive = 0;
521 unsigned char old_porta;
524 if (++drive > 1 || !
UD.connected)
532 old_porta =
sound_ym.rd_data_reg_sel;
534 ~(drive == 0 ?
DSKDRV0 : DSKDRV1);
538 if (stat !=
UD.wpstat) {
539 DPRINT((
"wpstat[%d] = %d\n", drive, stat ));
541 set_bit (drive, &changed_floppies);
546 start_check_change_timer();
554 static inline void set_head_settle_flag(
void)
559 static inline int get_head_settle_flag(
void)
561 int tmp = HeadSettleFlag;
566 static inline void copy_buffer(
void *
from,
void *to)
571 for (cnt = 512/4;
cnt; cnt--)
582 static irqreturn_t floppy_irq (
int irq,
void *dummy)
592 DPRINT((
"FDC irq, status = %02x handler = %08lx\n",status,(
unsigned long)handler));
596 DPRINT((
"FDC irq, no handler\n"));
606 static void fd_error(
void )
618 fd_request->errors++;
621 fd_end_request_cur(-
EIO);
625 if (SelectedDrive != -1)
633 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
638 #define FILL(n,val) \
640 memset( p, val, n ); \
650 DPRINT((
"do_format( dr=%d tr=%d he=%d offs=%d )\n",
654 while( fdc_busy )
sleep_on( &fdc_wait );
666 type = minor2disktype[
type].index;
683 FILL( 60 * (nsect / 9), 0x4e );
690 *p++ = (nsect + sect - desc->
sect_offset) % nsect + 1;
705 ReqTrack = desc->
track;
706 ReqSide = desc->
head;
707 do_fd_action( drive );
712 return( FormatError ? -
EIO : 0 );
725 static void do_fd_action(
int drive )
727 DPRINT((
"do_fd_action\n"));
729 if (UseTrackbuffer && !IsFormatting) {
732 if (ReqCmd ==
READ) {
734 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
736 setup_req_params( drive );
741 fd_end_request_cur(0);
754 if (SelectedDrive != drive)
755 fd_select_drive( drive );
759 else if (
UD.track != ReqTrack <<
UDT->stretch)
761 else if (IsFormatting)
770 static void fd_calibrate(
void )
772 if (
SUD.track >= 0) {
773 fd_calibrate_done( 0 );
779 DPRINT((
"fd_calibrate\n"));
791 static void fd_calibrate_done(
int status )
793 DPRINT((
"fd_calibrate_done()\n"));
814 static void fd_seek(
void )
816 if (
SUD.track == ReqTrack <<
SUDT->stretch) {
826 DPRINT((
"fd_seek() to track %d\n",ReqTrack));
833 set_head_settle_flag();
839 static void fd_seek_done(
int status )
841 DPRINT((
"fd_seek_done()\n"));
849 SelectedDrive, ReqTrack );
855 SUD.track = ReqTrack <<
SUDT->stretch;
869 static int MultReadInProgress = 0;
872 static void fd_rwsec(
void )
875 unsigned int rwflag, old_motoron;
878 DPRINT((
"fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd ==
WRITE ?
'w' :
'r' ));
879 if (ReqCmd ==
WRITE) {
885 paddr = PhysDMABuffer;
887 dma_cache_maintenance( paddr, 512, 1 );
892 paddr = PhysTrackBuffer;
899 fd_select_side( ReqSide );
928 dma_wd.dma_mode_status = 0x90 | rwflag;
930 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
932 dma_wd.dma_mode_status = 0x90 | rwflag;
936 dma_wd.fdc_acces_seccount = read_track ?
SUDT->spt : 1;
944 dma_wd.fdc_acces_seccount =
945 (get_head_settle_flag() |
948 old_motoron = MotorOn;
959 MultReadInProgress = 1;
962 jiffies +
HZ/5 + (old_motoron ? 0 :
HZ));
968 static void fd_readtrack_check(
unsigned long dummy )
974 if (!MultReadInProgress) {
990 addr =
dma_wd.dma_lo & 0xff;
992 addr |= (
dma_wd.dma_md & 0xff) << 8;
997 addr |= (
dma_wd.dma_hi & 0xff) << 16;
999 }
while(addr != addr2);
1001 if (addr >= PhysTrackBuffer +
SUDT->spt*512) {
1006 MultReadInProgress = 0;
1008 DPRINT((
"fd_readtrack_check(): done\n"));
1020 DPRINT((
"fd_readtrack_check(): not yet finished\n"));
1026 static void fd_rwsec_done(
int status )
1028 DPRINT((
"fd_rwsec_done()\n"));
1032 if (!MultReadInProgress)
1034 MultReadInProgress = 0;
1036 fd_rwsec_done1(status);
1039 static void fd_rwsec_done1(
int status)
1046 if (
SUDT->stretch) {
1052 if (!UseTrackbuffer) {
1053 dma_wd.dma_mode_status = 0x90;
1055 if (!(
dma_wd.dma_mode_status & 0x01)) {
1066 if ((status & FDCSTAT_RECNF) &&
1071 if (
SUDT > atari_disk_type) {
1072 if (
SUDT[-1].blocks > ReqBlock) {
1075 set_capacity(
unit[SelectedDrive].disk,
1083 SelectedDrive,
SUDT->name );
1088 if (
SUD.autoprobe) {
1089 SUDT = atari_disk_type + StartDiskType[DriveType];
1090 set_capacity(
unit[SelectedDrive].disk,
1100 setup_req_params( SelectedDrive );
1102 do_fd_action( SelectedDrive );
1106 printk(
KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1111 printk(
KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1116 printk(
KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1123 if (ReqCmd ==
READ) {
1129 copy_buffer (addr, ReqData);
1131 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1132 BufferDrive = SelectedDrive;
1133 BufferSide = ReqSide;
1134 BufferTrack = ReqTrack;
1139 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1141 setup_req_params( SelectedDrive );
1142 do_fd_action( SelectedDrive );
1146 fd_end_request_cur(0);
1157 static void fd_writetrack(
void )
1162 DPRINT((
"fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1164 paddr = PhysTrackBuffer;
1167 fd_select_side( ReqSide );
1170 if (
SUDT->stretch) {
1193 dma_wd.dma_mode_status = 0x190;
1195 dma_wd.dma_mode_status = 0x90;
1197 dma_wd.dma_mode_status = 0x190;
1216 static void fd_writetrack_done(
int status )
1218 DPRINT((
"fd_writetrack_done()\n"));
1222 if (status & FDCSTAT_WPROT) {
1226 if (status & FDCSTAT_LOST) {
1228 SelectedDrive, ReqSide, ReqTrack );
1239 static void fd_times_out(
unsigned long dummy )
1242 if (!FloppyIRQHandler)
goto end;
1267 static void finish_fdc(
void )
1270 finish_fdc_done( 0 );
1273 DPRINT((
"finish_fdc: dummy seek started\n"));
1286 static void finish_fdc_done(
int dummy )
1288 unsigned long flags;
1290 DPRINT((
"finish_fdc_done entered\n"));
1294 if (timer_pending(&fd_timer) &&
time_before(fd_timer.expires, jiffies + 5))
1300 start_check_change_timer();
1301 start_motor_off_timer();
1309 DPRINT((
"finish_fdc() finished\n"));
1327 static unsigned int floppy_check_events(
struct gendisk *disk,
1328 unsigned int clearing)
1330 struct atari_floppy_struct *
p = disk->private_data;
1331 unsigned int drive = p -
unit;
1332 if (
test_bit (drive, &fake_change)) {
1334 return DISK_EVENT_MEDIA_CHANGE;
1336 if (
test_bit (drive, &changed_floppies)) {
1338 return DISK_EVENT_MEDIA_CHANGE;
1344 return DISK_EVENT_MEDIA_CHANGE;
1350 static int floppy_revalidate(
struct gendisk *disk)
1352 struct atari_floppy_struct *p = disk->private_data;
1353 unsigned int drive = p -
unit;
1355 if (
test_bit(drive, &changed_floppies) ||
1366 if (default_params[drive].blocks == 0)
1369 UDT = &default_params[drive];
1377 static void setup_req_params(
int drive )
1379 int block = ReqBlock + ReqCnt;
1381 ReqTrack = block /
UDT->spt;
1382 ReqSector = block - ReqTrack *
UDT->spt + 1;
1383 ReqSide = ReqTrack & 1;
1385 ReqData = ReqBuffer + 512 * ReqCnt;
1388 read_track = (ReqCmd ==
READ && fd_request->errors == 0);
1392 DPRINT((
"Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1393 ReqTrack, ReqSector, (
unsigned long)ReqData ));
1399 static struct request *set_next_request(
void)
1402 int old_pos = fdc_queue;
1406 q = unit[fdc_queue].disk->queue;
1414 }
while (fdc_queue != old_pos);
1420 static void redo_fd_request(
void)
1423 struct atari_floppy_struct *floppy;
1425 DPRINT((
"redo_fd_request: fd_request=%p dev=%s fd_request->sector=%ld\n",
1426 fd_request, fd_request ? fd_request->rq_disk->disk_name :
"",
1427 fd_request ? blk_rq_pos(fd_request) : 0 ));
1433 fd_request = set_next_request();
1438 floppy = fd_request->rq_disk->private_data;
1439 drive = floppy -
unit;
1440 type = floppy->type;
1442 if (!
UD.connected) {
1445 fd_end_request_cur(-
EIO);
1452 UDT = atari_disk_type + StartDiskType[DriveType];
1453 set_capacity(floppy->disk,
UDT->blocks);
1461 fd_end_request_cur(-
EIO);
1464 if (minor2disktype[type].
drive_types > DriveType) {
1466 fd_end_request_cur(-
EIO);
1469 type = minor2disktype[
type].index;
1471 set_capacity(floppy->disk,
UDT->blocks);
1475 if (blk_rq_pos(fd_request) + 1 >
UDT->blocks) {
1476 fd_end_request_cur(-
EIO);
1484 ReqCmd = rq_data_dir(fd_request);
1485 ReqBlock = blk_rq_pos(fd_request);
1486 ReqBuffer = fd_request->buffer;
1487 setup_req_params( drive );
1488 do_fd_action( drive );
1500 while( fdc_busy )
sleep_on( &fdc_wait );
1510 unsigned int cmd,
unsigned long param)
1512 struct gendisk *disk = bdev->
bd_disk;
1513 struct atari_floppy_struct *floppy = disk->private_data;
1514 int drive = floppy -
unit;
1515 int type = floppy->type;
1517 struct atari_disk_type *dtp;
1530 type = minor2disktype[
type].index;
1531 dtp = &atari_disk_type[
type];
1534 drive, dtp, dtp->name);
1542 memset((
void *)&getprm, 0,
sizeof(getprm));
1543 getprm.size = dtp->blocks;
1544 getprm.sect = dtp->spt;
1546 getprm.track = dtp->blocks/dtp->spt/2;
1547 getprm.stretch = dtp->stretch;
1565 if (floppy->ref != 1 && floppy->ref != -1)
1574 if (floppy_check_events(disk, 0))
1575 floppy_revalidate(disk);
1579 drive, setprm.size, setprm.sect, setprm.stretch);
1597 if (minor2disktype[settype].
drive_types > DriveType) {
1601 setidx = minor2disktype[settype].index;
1602 dtp = &atari_disk_type[setidx];
1605 if ( dtp->blocks == setprm.size
1606 && dtp->spt == setprm.sect
1607 && dtp->stretch == setprm.stretch ) {
1610 drive, dtp->name, dtp);
1612 set_capacity(floppy->disk,
UDT->blocks);
1616 default_params[drive].name = dtp->name;
1617 default_params[drive].spt = dtp->spt;
1618 default_params[drive].blocks = dtp->blocks;
1619 default_params[drive].fdc_speed = dtp->fdc_speed;
1620 default_params[drive].stretch = dtp->stretch;
1632 dtp = &default_params[drive];
1635 dtp = &user_params[drive];
1637 dtp->name =
"user format";
1638 dtp->blocks = setprm.size;
1639 dtp->spt = setprm.sect;
1640 if (setprm.sect > 14)
1644 dtp->stretch = setprm.stretch;
1648 drive, dtp->blocks, dtp->spt, dtp->stretch);
1651 if (setprm.track != dtp->blocks/dtp->spt/2 ||
1658 set_capacity(floppy->disk,
UDT->blocks);
1672 if (floppy->ref != 1 && floppy->ref != -1)
1676 return do_format(drive, type, &fmt_desc);
1680 default_params[drive].blocks = 0;
1695 unsigned int cmd,
unsigned long arg)
1700 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1708 static void __init fd_probe(
int drive )
1713 if (!fd_test_drive_present( drive ))
1718 switch( UserSteprate[drive] ) {
1751 static int __init fd_test_drive_present(
int drive )
1758 fd_select_drive( drive );
1767 if (!(
st_mfp.par_dt_reg & 0x20))
1784 while(
st_mfp.par_dt_reg & 0x20 )
1798 static void __init config_types(
void )
1829 start_motor_off_timer();
1830 if (cnt == 1) fd_select_drive( 0 );
1831 start_check_change_timer();
1843 struct atari_floppy_struct *p = bdev->
bd_disk->private_data;
1846 DPRINT((
"fd_open: type=%d\n",type));
1847 if (p->ref && p->type != type)
1850 if (p->ref == -1 || (p->ref && mode &
FMODE_EXCL))
1853 if (mode & FMODE_EXCL)
1883 ret = floppy_open(bdev, mode);
1889 static int floppy_release(
struct gendisk *disk,
fmode_t mode)
1891 struct atari_floppy_struct *p = disk->private_data;
1895 else if (!p->ref--) {
1903 static const struct block_device_operations floppy_fops = {
1905 .open = floppy_unlocked_open,
1906 .release = floppy_release,
1908 .check_events = floppy_check_events,
1909 .revalidate_disk= floppy_revalidate,
1914 int drive = *part & 3;
1915 int type = *part >> 2;
1916 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1922 static int __init atari_floppy_init (
void)
1939 if (UseTrackbuffer < 0)
1958 BufferDrive = BufferSide = BufferTrack = -1;
1964 unit[
i].disk->first_minor =
i;
1965 sprintf(unit[i].disk->disk_name,
"fd%d", i);
1966 unit[
i].disk->fops = &floppy_fops;
1967 unit[
i].disk->private_data = &unit[
i];
1970 if (!unit[i].disk->queue)
1979 printk(
KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
1980 DriveType == 0 ?
'D' : DriveType == 1 ?
'H' :
'E',
1981 UseTrackbuffer ?
"" :
"no ");
1999 static int __init atari_floppy_setup(
char *
str)
2013 else if (ints[0] > 2+FD_MAX_UNITS) {
2017 if (ints[1] < 0 || ints[1] > 2)
2020 DriveType = ints[1];
2023 UseTrackbuffer = (ints[2] > 0);
2026 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2029 UserSteprate[i-3] = ints[
i];
2034 __setup(
"floppy=", atari_floppy_setup);
2037 static void __exit atari_floppy_exit(
void)