1/* 2 * linux/amiga/amiflop.c 3 * 4 * Copyright (C) 1993 Greg Harp 5 * Portions of this driver are based on code contributed by Brad Pepers 6 * 7 * revised 28.5.95 by Joerg Dorchain 8 * - now no bugs(?) any more for both HD & DD 9 * - added support for 40 Track 5.25" drives, 80-track hopefully behaves 10 * like 3.5" dd (no way to test - are there any 5.25" drives out there 11 * that work on an A4000?) 12 * - wrote formatting routine (maybe dirty, but works) 13 * 14 * june/july 1995 added ms-dos support by Joerg Dorchain 15 * (portions based on messydos.device and various contributors) 16 * - currently only 9 and 18 sector disks 17 * 18 * - fixed a bug with the internal trackbuffer when using multiple 19 * disks the same time 20 * - made formatting a bit safer 21 * - added command line and machine based default for "silent" df0 22 * 23 * december 1995 adapted for 1.2.13pl4 by Joerg Dorchain 24 * - works but I think it's inefficient. (look in redo_fd_request) 25 * But the changes were very efficient. (only three and a half lines) 26 * 27 * january 1996 added special ioctl for tracking down read/write problems 28 * - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data 29 * is copied to area. (area should be large enough since no checking is 30 * done - 30K is currently sufficient). return the actual size of the 31 * trackbuffer 32 * - replaced udelays() by a timer (CIAA timer B) for the waits 33 * needed for the disk mechanic. 34 * 35 * february 1996 fixed error recovery and multiple disk access 36 * - both got broken the first time I tampered with the driver :-( 37 * - still not safe, but better than before 38 * 39 * revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel. 40 * - Minor changes to accept the kdev_t. 41 * - Replaced some more udelays with ms_delays. Udelay is just a loop, 42 * and so the delay will be different depending on the given 43 * processor :-( 44 * - The driver could use a major cleanup because of the new 45 * major/minor handling that came with kdev_t. It seems to work for 46 * the time being, but I can't guarantee that it will stay like 47 * that when we start using 16 (24?) bit minors. 48 * 49 * restructured jan 1997 by Joerg Dorchain 50 * - Fixed Bug accessing multiple disks 51 * - some code cleanup 52 * - added trackbuffer for each drive to speed things up 53 * - fixed some race conditions (who finds the next may send it to me ;-) 54 */ 55 56#include <linux/module.h> 57 58#include <linux/sched.h> 59#include <linux/fs.h> 60#include <linux/fcntl.h> 61#include <linux/kernel.h> 62#include <linux/timer.h> 63#include <linux/fd.h> 64#include <linux/hdreg.h> 65#include <linux/errno.h> 66#include <linux/types.h> 67#include <linux/delay.h> 68#include <linux/string.h> 69#include <linux/slab.h> 70#include <linux/init.h> 71#include <linux/amifdreg.h> 72#include <linux/amifd.h> 73#include <linux/ioport.h> 74 75#include <asm/setup.h> 76#include <asm/uaccess.h> 77#include <asm/amigahw.h> 78#include <asm/amigaints.h> 79#include <asm/irq.h> 80 81#define MAJOR_NR FLOPPY_MAJOR 82#include <linux/blk.h> 83 84#undef DEBUG /* print _LOTS_ of infos */ 85 86#define RAW_IOCTL 87#ifdef RAW_IOCTL 88#define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */ 89#endif 90 91/* 92 * Defines 93 */ 94 95/* 96 * Error codes 97 */ 98#define FD_OK 0 /* operation succeeded */ 99#define FD_ERROR -1 /* general error (seek, read, write, etc) */ 100#define FD_NOUNIT 1 /* unit does not exist */ 101#define FD_UNITBUSY 2 /* unit already active */ 102#define FD_NOTACTIVE 3 /* unit is not active */ 103#define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */ 104 105#define MFM_NOSYNC 1 106#define MFM_HEADER 2 107#define MFM_DATA 3 108#define MFM_TRACK 4 109 110/* 111 * Floppy ID values 112 */ 113#define FD_NODRIVE 0x00000000 /* response when no unit is present */ 114#define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */ 115#define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */ 116#define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */ 117 118static long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */ 119 120MODULE_PARM(fd_def_df0,"l"); 121MODULE_LICENSE("GPL"); 122 123/* 124 * Macros 125 */ 126#define MOTOR_ON (ciab.prb &= ~DSKMOTOR) 127#define MOTOR_OFF (ciab.prb |= DSKMOTOR) 128#define SELECT(mask) (ciab.prb &= ~mask) 129#define DESELECT(mask) (ciab.prb |= mask) 130#define SELMASK(drive) (1 << (3 + (drive & 3))) 131 132static struct fd_drive_type drive_types[] = { 133/* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/ 134/* warning: times are now in milliseconds (ms) */ 135{ FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1}, 136{ FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1}, 137{ FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2}, 138{ FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 139}; 140static int num_dr_types = sizeof(drive_types) / sizeof(drive_types[0]); 141 142/* defaults for 3 1/2" HD-Disks */ 143static int floppy_sizes[256]={880,880,880,880,720,720,720,720,}; 144static int floppy_blocksizes[256]; 145/* hardsector size assumed to be 512 */ 146 147static int amiga_read(int), dos_read(int); 148static void amiga_write(int), dos_write(int); 149static struct fd_data_type data_types[] = { 150 { "Amiga", 11 , amiga_read, amiga_write}, 151 { "MS-Dos", 9, dos_read, dos_write} 152}; 153 154/* current info on each unit */ 155static struct amiga_floppy_struct unit[FD_MAX_UNITS]; 156 157static struct timer_list flush_track_timer[FD_MAX_UNITS]; 158static struct timer_list post_write_timer; 159static struct timer_list motor_on_timer; 160static struct timer_list motor_off_timer[FD_MAX_UNITS]; 161static int on_attempts; 162 163/* Synchronization of FDC access */ 164/* request loop (trackbuffer) */ 165static volatile int fdc_busy = -1; 166static volatile int fdc_nested; 167static DECLARE_WAIT_QUEUE_HEAD(fdc_wait); 168 169static DECLARE_WAIT_QUEUE_HEAD(motor_wait); 170 171static volatile int selected = -1; /* currently selected drive */ 172 173static int writepending; 174static int writefromint; 175static char *raw_buf; 176 177#define RAW_BUF_SIZE 30000 /* size of raw disk data */ 178 179/* 180 * These are global variables, as that's the easiest way to give 181 * information to interrupts. They are the data used for the current 182 * request. 183 */ 184static volatile char block_flag; 185static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block); 186 187/* MS-Dos MFM Coding tables (should go quick and easy) */ 188static unsigned char mfmencode[16]={ 189 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15, 190 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55 191}; 192static unsigned char mfmdecode[128]; 193 194/* floppy internal millisecond timer stuff */ 195static volatile int ms_busy = -1; 196static DECLARE_WAIT_QUEUE_HEAD(ms_wait); 197#define MS_TICKS ((amiga_eclock+50)/1000) 198 199/* 200 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read 201 * max X times - some types of errors increase the errorcount by 2 or 202 * even 3, so we might actually retry only X/2 times before giving up. 203 */ 204#define MAX_ERRORS 12 205 206/* Prevent "aliased" accesses. */ 207static int fd_ref[4] = { 0,0,0,0 }; 208static int fd_device[4] = { 0,0,0,0 }; 209 210/* 211 * Current device number. Taken either from the block header or from the 212 * format request descriptor. 213 */ 214#define CURRENT_DEVICE (CURRENT->rq_dev) 215 216/* Current error count. */ 217#define CURRENT_ERRORS (CURRENT->errors) 218 219 220 221/* 222 * Here come the actual hardware access and helper functions. 223 * They are not reentrant and single threaded because all drives 224 * share the same hardware and the same trackbuffer. 225 */ 226 227/* Milliseconds timer */ 228 229static void ms_isr(int irq, void *dummy, struct pt_regs *fp) 230{ 231 ms_busy = -1; 232 wake_up(&ms_wait); 233} 234 235/* all waits are queued up 236 A more generic routine would do a schedule a la timer.device */ 237static void ms_delay(int ms) 238{ 239 unsigned long flags; 240 int ticks; 241 if (ms > 0) { 242 save_flags(flags); 243 cli(); 244 while (ms_busy == 0) 245 sleep_on(&ms_wait); 246 ms_busy = 0; 247 restore_flags(flags); 248 ticks = MS_TICKS*ms-1; 249 ciaa.tblo=ticks%256; 250 ciaa.tbhi=ticks/256; 251 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */ 252 sleep_on(&ms_wait); 253 } 254} 255 256/* Hardware semaphore */ 257 258/* returns true when we would get the semaphore */ 259static inline int try_fdc(int drive) 260{ 261 drive &= 3; 262 return ((fdc_busy < 0) || (fdc_busy == drive)); 263} 264 265static void get_fdc(int drive) 266{ 267 unsigned long flags; 268 269 drive &= 3; 270#ifdef DEBUG 271 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested); 272#endif 273 save_flags(flags); 274 cli(); 275 while (!try_fdc(drive)) 276 sleep_on(&fdc_wait); 277 fdc_busy = drive; 278 fdc_nested++; 279 restore_flags(flags); 280} 281 282static inline void rel_fdc(void) 283{ 284#ifdef DEBUG 285 if (fdc_nested == 0) 286 printk("fd: unmatched rel_fdc\n"); 287 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested); 288#endif 289 fdc_nested--; 290 if (fdc_nested == 0) { 291 fdc_busy = -1; 292 wake_up(&fdc_wait); 293 } 294} 295 296static void fd_select (int drive) 297{ 298 unsigned char prb = ~0; 299 300 drive&=3; 301#ifdef DEBUG 302 printk("selecting %d\n",drive); 303#endif 304 if (drive == selected) 305 return; 306 get_fdc(drive); 307 selected = drive; 308 309 if (unit[drive].track % 2 != 0) 310 prb &= ~DSKSIDE; 311 if (unit[drive].motor == 1) 312 prb &= ~DSKMOTOR; 313 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3)); 314 ciab.prb = prb; 315 prb &= ~SELMASK(drive); 316 ciab.prb = prb; 317 rel_fdc(); 318} 319 320static void fd_deselect (int drive) 321{ 322 unsigned char prb; 323 unsigned long flags; 324 325 drive&=3; 326#ifdef DEBUG 327 printk("deselecting %d\n",drive); 328#endif 329 if (drive != selected) { 330 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected); 331 return; 332 } 333 334 get_fdc(drive); 335 save_flags (flags); 336 sti(); 337 338 selected = -1; 339 340 prb = ciab.prb; 341 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3)); 342 ciab.prb = prb; 343 344 restore_flags (flags); 345 rel_fdc(); 346 347} 348 349static void motor_on_callback(unsigned long nr) 350{ 351 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) { 352 wake_up (&motor_wait); 353 } else { 354 motor_on_timer.expires = jiffies + HZ/10; 355 add_timer(&motor_on_timer); 356 } 357} 358 359static int fd_motor_on(int nr) 360{ 361 nr &= 3; 362 363 del_timer(motor_off_timer + nr); 364 365 if (!unit[nr].motor) { 366 unit[nr].motor = 1; 367 fd_select(nr); 368 369 del_timer(&motor_on_timer); 370 motor_on_timer.data = nr; 371 motor_on_timer.expires = jiffies + HZ/2; 372 add_timer(&motor_on_timer); 373 374 on_attempts = 10; 375 sleep_on (&motor_wait); 376 fd_deselect(nr); 377 } 378 379 if (on_attempts == 0) { 380 on_attempts = -1; 381 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n"); 382 } 383 384 return 1; 385} 386 387static void fd_motor_off(unsigned long drive) 388{ 389 long calledfromint; 390#ifdef MODULE 391 long decusecount; 392 393 decusecount = drive & 0x40000000; 394#endif 395 calledfromint = drive & 0x80000000; 396 drive&=3; 397 if (calledfromint && !try_fdc(drive)) { 398 /* We would be blocked in an interrupt, so try again later */ 399 motor_off_timer[drive].expires = jiffies + 1; 400 add_timer(motor_off_timer + drive); 401 return; 402 } 403 unit[drive].motor = 0; 404 fd_select(drive); 405 udelay (1); 406 fd_deselect(drive); 407 408#ifdef MODULE 409/* 410 this is the last interrupt for any drive access, happens after 411 release (from floppy_off). So we have to wait until now to decrease 412 the use count. 413*/ 414 if (decusecount) 415 MOD_DEC_USE_COUNT; 416#endif 417} 418 419static void floppy_off (unsigned int nr) 420{ 421 int drive; 422 423 drive = nr & 3; 424 del_timer(motor_off_timer + drive); 425 motor_off_timer[drive].expires = jiffies + 3*HZ; 426 /* called this way it is always from interrupt */ 427 motor_off_timer[drive].data = nr | 0x80000000; 428 add_timer(motor_off_timer + nr); 429} 430 431static int fd_calibrate(int drive) 432{ 433 unsigned char prb; 434 int n; 435 436 drive &= 3; 437 get_fdc(drive); 438 if (!fd_motor_on (drive)) 439 return 0; 440 fd_select (drive); 441 prb = ciab.prb; 442 prb |= DSKSIDE; 443 prb &= ~DSKDIREC; 444 ciab.prb = prb; 445 for (n = unit[drive].type->tracks/2; n != 0; --n) { 446 if (ciaa.pra & DSKTRACK0) 447 break; 448 prb &= ~DSKSTEP; 449 ciab.prb = prb; 450 prb |= DSKSTEP; 451 udelay (2); 452 ciab.prb = prb; 453 ms_delay(unit[drive].type->step_delay); 454 } 455 ms_delay (unit[drive].type->settle_time); 456 prb |= DSKDIREC; 457 n = unit[drive].type->tracks + 20; 458 for (;;) { 459 prb &= ~DSKSTEP; 460 ciab.prb = prb; 461 prb |= DSKSTEP; 462 udelay (2); 463 ciab.prb = prb; 464 ms_delay(unit[drive].type->step_delay + 1); 465 if ((ciaa.pra & DSKTRACK0) == 0) 466 break; 467 if (--n == 0) { 468 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive); 469 fd_motor_off (drive); 470 unit[drive].track = -1; 471 rel_fdc(); 472 return 0; 473 } 474 } 475 unit[drive].track = 0; 476 ms_delay(unit[drive].type->settle_time); 477 478 rel_fdc(); 479 fd_deselect(drive); 480 return 1; 481} 482 483static int fd_seek(int drive, int track) 484{ 485 unsigned char prb; 486 int cnt; 487 488#ifdef DEBUG 489 printk("seeking drive %d to track %d\n",drive,track); 490#endif 491 drive &= 3; 492 get_fdc(drive); 493 if (unit[drive].track == track) { 494 rel_fdc(); 495 return 1; 496 } 497 if (!fd_motor_on(drive)) { 498 rel_fdc(); 499 return 0; 500 } 501 if (unit[drive].track < 0 && !fd_calibrate(drive)) { 502 rel_fdc(); 503 return 0; 504 } 505 506 fd_select (drive); 507 cnt = unit[drive].track/2 - track/2; 508 prb = ciab.prb; 509 prb |= DSKSIDE | DSKDIREC; 510 if (track % 2 != 0) 511 prb &= ~DSKSIDE; 512 if (cnt < 0) { 513 cnt = - cnt; 514 prb &= ~DSKDIREC; 515 } 516 ciab.prb = prb; 517 if (track % 2 != unit[drive].track % 2) 518 ms_delay (unit[drive].type->side_time); 519 unit[drive].track = track; 520 if (cnt == 0) { 521 rel_fdc(); 522 fd_deselect(drive); 523 return 1; 524 } 525 do { 526 prb &= ~DSKSTEP; 527 ciab.prb = prb; 528 prb |= DSKSTEP; 529 udelay (1); 530 ciab.prb = prb; 531 ms_delay (unit[drive].type->step_delay); 532 } while (--cnt != 0); 533 ms_delay (unit[drive].type->settle_time); 534 535 rel_fdc(); 536 fd_deselect(drive); 537 return 1; 538} 539 540static unsigned long fd_get_drive_id(int drive) 541{ 542 int i; 543 ulong id = 0; 544 545 drive&=3; 546 get_fdc(drive); 547 /* set up for ID */ 548 MOTOR_ON; 549 udelay(2); 550 SELECT(SELMASK(drive)); 551 udelay(2); 552 DESELECT(SELMASK(drive)); 553 udelay(2); 554 MOTOR_OFF; 555 udelay(2); 556 SELECT(SELMASK(drive)); 557 udelay(2); 558 DESELECT(SELMASK(drive)); 559 udelay(2); 560 561 /* loop and read disk ID */ 562 for (i=0; i<32; i++) { 563 SELECT(SELMASK(drive)); 564 udelay(2); 565 566 /* read and store value of DSKRDY */ 567 id <<= 1; 568 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */ 569 570 DESELECT(SELMASK(drive)); 571 } 572 573 rel_fdc(); 574 575 /* 576 * RB: At least A500/A2000's df0: don't identify themselves. 577 * As every (real) Amiga has at least a 3.5" DD drive as df0: 578 * we default to that if df0: doesn't identify as a certain 579 * type. 580 */ 581 if(drive == 0 && id == FD_NODRIVE) 582 { 583 id = fd_def_df0; 584 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0); 585 } 586 /* return the ID value */ 587 return (id); 588} 589 590static void fd_block_done(int irq, void *dummy, struct pt_regs *fp) 591{ 592 if (block_flag) 593 custom.dsklen = 0x4000; 594 595 if (block_flag == 2) { /* writing */ 596 writepending = 2; 597 post_write_timer.expires = jiffies + 1; /* at least 2 ms */ 598 post_write_timer.data = selected; 599 add_timer(&post_write_timer); 600 } 601 else { /* reading */ 602 block_flag = 0; 603 wake_up (&wait_fd_block); 604 } 605} 606 607static void raw_read(int drive) 608{ 609 drive&=3; 610 get_fdc(drive); 611 while (block_flag) 612 sleep_on(&wait_fd_block); 613 fd_select(drive); 614 /* setup adkcon bits correctly */ 615 custom.adkcon = ADK_MSBSYNC; 616 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST; 617 618 custom.dsksync = MFM_SYNC; 619 620 custom.dsklen = 0; 621 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf); 622 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN; 623 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN; 624 625 block_flag = 1; 626 627 while (block_flag) 628 sleep_on (&wait_fd_block); 629 630 custom.dsklen = 0; 631 fd_deselect(drive); 632 rel_fdc(); 633} 634 635static int raw_write(int drive) 636{ 637 ushort adk; 638 639 drive&=3; 640 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */ 641 if ((ciaa.pra & DSKPROT) == 0) { 642 rel_fdc(); 643 return 0; 644 } 645 while (block_flag) 646 sleep_on(&wait_fd_block); 647 fd_select(drive); 648 /* clear adkcon bits */ 649 custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC; 650 /* set appropriate adkcon bits */ 651 adk = ADK_SETCLR|ADK_FAST; 652 if ((ulong)unit[drive].track >= unit[drive].type->precomp2) 653 adk |= ADK_PRECOMP1; 654 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1) 655 adk |= ADK_PRECOMP0; 656 custom.adkcon = adk; 657 658 custom.dsklen = DSKLEN_WRITE; 659 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf); 660 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE; 661 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE; 662 663 block_flag = 2; 664 return 1; 665} 666 667/* 668 * to be called at least 2ms after the write has finished but before any 669 * other access to the hardware. 670 */ 671static void post_write (unsigned long drive) 672{ 673#ifdef DEBUG 674 printk("post_write for drive %ld\n",drive); 675#endif 676 drive &= 3; 677 custom.dsklen = 0; 678 block_flag = 0; 679 writepending = 0; 680 writefromint = 0; 681 unit[drive].dirty = 0; 682 wake_up(&wait_fd_block); 683 fd_deselect(drive); 684 rel_fdc(); /* corresponds to get_fdc() in raw_write */ 685} 686 687 688/* 689 * The following functions are to convert the block contents into raw data 690 * written to disk and vice versa. 691 * (Add other formats here ;-)) 692 */ 693 694static unsigned long scan_sync(unsigned long raw, unsigned long end) 695{ 696 ushort *ptr = (ushort *)raw, *endp = (ushort *)end; 697 698 while (ptr < endp && *ptr++ != 0x4489) 699 ; 700 if (ptr < endp) { 701 while (*ptr == 0x4489 && ptr < endp) 702 ptr++; 703 return (ulong)ptr; 704 } 705 return 0; 706} 707 708static inline unsigned long checksum(unsigned long *addr, int len) 709{ 710 unsigned long csum = 0; 711 712 len /= sizeof(*addr); 713 while (len-- > 0) 714 csum ^= *addr++; 715 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555); 716 717 return csum; 718} 719 720static unsigned long decode (unsigned long *data, unsigned long *raw, 721 int len) 722{ 723 ulong *odd, *even; 724 725 /* convert length from bytes to longwords */ 726 len >>= 2; 727 odd = raw; 728 even = odd + len; 729 730 /* prepare return pointer */ 731 raw += len * 2; 732 733 do { 734 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555); 735 } while (--len != 0); 736 737 return (ulong)raw; 738} 739 740struct header { 741 unsigned char magic; 742 unsigned char track; 743 unsigned char sect; 744 unsigned char ord; 745 unsigned char labels[16]; 746 unsigned long hdrchk; 747 unsigned long datachk; 748}; 749 750static int amiga_read(int drive) 751{ 752 unsigned long raw; 753 unsigned long end; 754 int scnt; 755 unsigned long csum; 756 struct header hdr; 757 758 drive&=3; 759 raw = (long) raw_buf; 760 end = raw + unit[drive].type->read_size; 761 762 for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) { 763 if (!(raw = scan_sync(raw, end))) { 764 printk (KERN_INFO "can't find sync for sector %d\n", scnt); 765 return MFM_NOSYNC; 766 } 767 768 raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4); 769 raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16); 770 raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4); 771 raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4); 772 csum = checksum((ulong *)&hdr, 773 (char *)&hdr.hdrchk-(char *)&hdr); 774 775#ifdef DEBUG 776 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n", 777 hdr.magic, hdr.track, hdr.sect, hdr.ord, 778 *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4], 779 *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12], 780 hdr.hdrchk, hdr.datachk); 781#endif 782 783 if (hdr.hdrchk != csum) { 784 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum); 785 return MFM_HEADER; 786 } 787 788 /* verify track */ 789 if (hdr.track != unit[drive].track) { 790 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track); 791 return MFM_TRACK; 792 } 793 794 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512), 795 (ulong *)raw, 512); 796 csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512); 797 798 if (hdr.datachk != csum) { 799 printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n", 800 hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt, 801 hdr.datachk, csum); 802 printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n", 803 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0], 804 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1], 805 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2], 806 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]); 807 return MFM_DATA; 808 } 809 } 810 811 return 0; 812} 813 814static void encode(unsigned long data, unsigned long *dest) 815{ 816 unsigned long data2; 817 818 data &= 0x55555555; 819 data2 = data ^ 0x55555555; 820 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1); 821 822 if (*(dest - 1) & 0x00000001) 823 data &= 0x7FFFFFFF; 824 825 *dest = data; 826} 827 828static void encode_block(unsigned long *dest, unsigned long *src, int len) 829{ 830 int cnt, to_cnt = 0; 831 unsigned long data; 832 833 /* odd bits */ 834 for (cnt = 0; cnt < len / 4; cnt++) { 835 data = src[cnt] >> 1; 836 encode(data, dest + to_cnt++); 837 } 838 839 /* even bits */ 840 for (cnt = 0; cnt < len / 4; cnt++) { 841 data = src[cnt]; 842 encode(data, dest + to_cnt++); 843 } 844} 845 846static unsigned long *putsec(int disk, unsigned long *raw, int cnt) 847{ 848 struct header hdr; 849 int i; 850 851 disk&=3; 852 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA; 853 raw++; 854 *raw++ = 0x44894489; 855 856 hdr.magic = 0xFF; 857 hdr.track = unit[disk].track; 858 hdr.sect = cnt; 859 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt; 860 for (i = 0; i < 16; i++) 861 hdr.labels[i] = 0; 862 hdr.hdrchk = checksum((ulong *)&hdr, 863 (char *)&hdr.hdrchk-(char *)&hdr); 864 hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512); 865 866 encode_block(raw, (ulong *)&hdr.magic, 4); 867 raw += 2; 868 encode_block(raw, (ulong *)&hdr.labels, 16); 869 raw += 8; 870 encode_block(raw, (ulong *)&hdr.hdrchk, 4); 871 raw += 2; 872 encode_block(raw, (ulong *)&hdr.datachk, 4); 873 raw += 2; 874 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512); 875 raw += 256; 876 877 return raw; 878} 879 880static void amiga_write(int disk) 881{ 882 unsigned int cnt; 883 unsigned long *ptr = (unsigned long *)raw_buf; 884 885 disk&=3; 886 /* gap space */ 887 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++) 888 *ptr++ = 0xaaaaaaaa; 889 890 /* sectors */ 891 for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++) 892 ptr = putsec (disk, ptr, cnt); 893 *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8; 894} 895 896 897struct dos_header { 898 unsigned char track, /* 0-80 */ 899 side, /* 0-1 */ 900 sec, /* 0-...*/ 901 len_desc;/* 2 */ 902 unsigned short crc; /* on 68000 we got an alignment problem, 903 but this compiler solves it by adding silently 904 adding a pad byte so data won't fit 905 and this took about 3h to discover.... */ 906 unsigned char gap1[22]; /* for longword-alignedness (0x4e) */ 907}; 908 909/* crc routines are borrowed from the messydos-handler */ 910 911/* excerpt from the messydos-device 912; The CRC is computed not only over the actual data, but including 913; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb). 914; As we don't read or encode these fields into our buffers, we have to 915; preload the registers containing the CRC with the values they would have 916; after stepping over these fields. 917; 918; How CRCs "really" work: 919; 920; First, you should regard a bitstring as a series of coefficients of 921; polynomials. We calculate with these polynomials in modulo-2 922; arithmetic, in which both add and subtract are done the same as 923; exclusive-or. Now, we modify our data (a very long polynomial) in 924; such a way that it becomes divisible by the CCITT-standard 16-bit 925; 16 12 5 926; polynomial: x + x + x + 1, represented by $11021. The easiest 927; way to do this would be to multiply (using proper arithmetic) our 928; datablock with $11021. So we have: 929; data * $11021 = 930; data * ($10000 + $1021) = 931; data * $10000 + data * $1021 932; The left part of this is simple: Just add two 0 bytes. But then 933; the right part (data $1021) remains difficult and even could have 934; a carry into the left part. The solution is to use a modified 935; multiplication, which has a result that is not correct, but with 936; a difference of any multiple of $11021. We then only need to keep 937; the 16 least significant bits of the result. 938; 939; The following algorithm does this for us: 940; 941; unsigned char *data, c, crclo, crchi; 942; while (not done) { 943; c = *data++ + crchi; 944; crchi = (@ c) >> 8 + crclo; 945; crclo = @ c; 946; } 947; 948; Remember, + is done with EOR, the @ operator is in two tables (high 949; and low byte separately), which is calculated as 950; 951; $1021 * (c & $F0) 952; xor $1021 * (c & $0F) 953; xor $1021 * (c >> 4) (* is regular multiplication) 954; 955; 956; Anyway, the end result is the same as the remainder of the division of 957; the data by $11021. I am afraid I need to study theory a bit more... 958 959 960my only works was to code this from manx to C.... 961 962*/ 963 964static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3) 965{ 966 static unsigned char CRCTable1[] = { 967 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1, 968 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3, 969 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5, 970 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7, 971 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9, 972 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab, 973 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d, 974 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f, 975 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60, 976 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72, 977 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44, 978 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56, 979 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28, 980 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a, 981 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c, 982 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e 983 }; 984 985 static unsigned char CRCTable2[] = { 986 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef, 987 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde, 988 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d, 989 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc, 990 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b, 991 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a, 992 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49, 993 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78, 994 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67, 995 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56, 996 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05, 997 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34, 998 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3, 999 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92, 1000 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1, 1001 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0 1002 }; 1003 1004/* look at the asm-code - what looks in C a bit strange is almost as good as handmade */ 1005 register int i; 1006 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl; 1007 1008 CRCT1=CRCTable1; 1009 CRCT2=CRCTable2; 1010 data=data_a3; 1011 crcl=data_d1; 1012 crch=data_d0; 1013 for (i=data_d3; i>=0; i--) { 1014 c = (*data++) ^ crch; 1015 crch = CRCT1[c] ^ crcl; 1016 crcl = CRCT2[c]; 1017 } 1018 return (crch<<8)|crcl; 1019} 1020 1021static inline ushort dos_hdr_crc (struct dos_header *hdr) 1022{ 1023 return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */ 1024} 1025 1026static inline ushort dos_data_crc(unsigned char *data) 1027{ 1028 return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */ 1029} 1030 1031static inline unsigned char dos_decode_byte(ushort word) 1032{ 1033 register ushort w2; 1034 register unsigned char byte; 1035 register unsigned char *dec = mfmdecode; 1036 1037 w2=word; 1038 w2>>=8; 1039 w2&=127; 1040 byte = dec[w2]; 1041 byte <<= 4; 1042 w2 = word & 127; 1043 byte |= dec[w2]; 1044 return byte; 1045} 1046 1047static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len) 1048{ 1049 int i; 1050 1051 for (i = 0; i < len; i++) 1052 *data++=dos_decode_byte(*raw++); 1053 return ((ulong)raw); 1054} 1055 1056#ifdef DEBUG 1057static void dbg(unsigned long ptr) 1058{ 1059 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr, 1060 ((ulong *)ptr)[0], ((ulong *)ptr)[1], 1061 ((ulong *)ptr)[2], ((ulong *)ptr)[3]); 1062} 1063#endif 1064 1065static int dos_read(int drive) 1066{ 1067 unsigned long end; 1068 unsigned long raw; 1069 int scnt; 1070 unsigned short crc,data_crc[2]; 1071 struct dos_header hdr; 1072 1073 drive&=3; 1074 raw = (long) raw_buf; 1075 end = raw + unit[drive].type->read_size; 1076 1077 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) { 1078 do { /* search for the right sync of each sec-hdr */ 1079 if (!(raw = scan_sync (raw, end))) { 1080 printk(KERN_INFO "dos_read: no hdr sync on " 1081 "track %d, unit %d for sector %d\n", 1082 unit[drive].track,drive,scnt); 1083 return MFM_NOSYNC; 1084 } 1085#ifdef DEBUG 1086 dbg(raw); 1087#endif 1088 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */ 1089 raw+=2; /* skip over headermark */ 1090 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8); 1091 crc = dos_hdr_crc(&hdr); 1092 1093#ifdef DEBUG 1094 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side, 1095 hdr.sec, hdr.len_desc, hdr.crc); 1096#endif 1097 1098 if (crc != hdr.crc) { 1099 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n", 1100 hdr.crc, crc); 1101 return MFM_HEADER; 1102 } 1103 if (hdr.track != unit[drive].track/unit[drive].type->heads) { 1104 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n", 1105 hdr.track, 1106 unit[drive].track/unit[drive].type->heads); 1107 return MFM_TRACK; 1108 } 1109 1110 if (hdr.side != unit[drive].track%unit[drive].type->heads) { 1111 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n", 1112 hdr.side, 1113 unit[drive].track%unit[drive].type->heads); 1114 return MFM_TRACK; 1115 } 1116 1117 if (hdr.len_desc != 2) { 1118 printk(KERN_INFO "dos_read: unknown sector len " 1119 "descriptor %d\n", hdr.len_desc); 1120 return MFM_DATA; 1121 } 1122#ifdef DEBUG 1123 printk("hdr accepted\n"); 1124#endif 1125 if (!(raw = scan_sync (raw, end))) { 1126 printk(KERN_INFO "dos_read: no data sync on track " 1127 "%d, unit %d for sector%d, disk sector %d\n", 1128 unit[drive].track, drive, scnt, hdr.sec); 1129 return MFM_NOSYNC; 1130 } 1131#ifdef DEBUG 1132 dbg(raw); 1133#endif 1134 1135 if (*((ushort *)raw)!=0x5545) { 1136 printk(KERN_INFO "dos_read: no data mark after " 1137 "sync (%d,%d,%d,%d) sc=%d\n", 1138 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt); 1139 return MFM_NOSYNC; 1140 } 1141 1142 raw+=2; /* skip data mark (included in checksum) */ 1143 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512); 1144 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4); 1145 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512); 1146 1147 if (crc != data_crc[0]) { 1148 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) " 1149 "sc=%d, %x %x\n", hdr.track, hdr.side, 1150 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc); 1151 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n", 1152 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0], 1153 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1], 1154 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2], 1155 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]); 1156 return MFM_DATA; 1157 } 1158 } 1159 return 0; 1160} 1161 1162static inline ushort dos_encode_byte(unsigned char byte) 1163{ 1164 register unsigned char *enc, b2, b1; 1165 register ushort word; 1166 1167 enc=mfmencode; 1168 b1=byte; 1169 b2=b1>>4; 1170 b1&=15; 1171 word=enc[b2] <<8 | enc [b1]; 1172 return (word|((word&(256|64)) ? 0: 128)); 1173} 1174 1175static void dos_encode_block(ushort *dest, unsigned char *src, int len) 1176{ 1177 int i; 1178 1179 for (i = 0; i < len; i++) { 1180 *dest=dos_encode_byte(*src++); 1181 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000; 1182 dest++; 1183 } 1184} 1185 1186static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt) 1187{ 1188 static struct dos_header hdr={0,0,0,2,0, 1189 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}}; 1190 int i; 1191 static ushort crc[2]={0,0x4e4e}; 1192 1193 drive&=3; 1194/* id gap 1 */ 1195/* the MFM word before is always 9254 */ 1196 for(i=0;i<6;i++) 1197 *raw++=0xaaaaaaaa; 1198/* 3 sync + 1 headermark */ 1199 *raw++=0x44894489; 1200 *raw++=0x44895554; 1201 1202/* fill in the variable parts of the header */ 1203 hdr.track=unit[drive].track/unit[drive].type->heads; 1204 hdr.side=unit[drive].track%unit[drive].type->heads; 1205 hdr.sec=cnt+1; 1206 hdr.crc=dos_hdr_crc(&hdr); 1207 1208/* header (without "magic") and id gap 2*/ 1209 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28); 1210 raw+=14; 1211 1212/*id gap 3 */ 1213 for(i=0;i<6;i++) 1214 *raw++=0xaaaaaaaa; 1215 1216/* 3 syncs and 1 datamark */ 1217 *raw++=0x44894489; 1218 *raw++=0x44895545; 1219 1220/* data */ 1221 dos_encode_block((ushort *)raw, 1222 (unsigned char *)unit[drive].trackbuf+cnt*512,512); 1223 raw+=256; 1224 1225/*data crc + jd's special gap (long words :-/) */ 1226 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512); 1227 dos_encode_block((ushort *) raw,(unsigned char *)crc,4); 1228 raw+=2; 1229 1230/* data gap */ 1231 for(i=0;i<38;i++) 1232 *raw++=0x92549254; 1233 1234 return raw; /* wrote 652 MFM words */ 1235} 1236 1237static void dos_write(int disk) 1238{ 1239 int cnt; 1240 unsigned long raw = (unsigned long) raw_buf; 1241 unsigned long *ptr=(unsigned long *)raw; 1242 1243 disk&=3; 1244/* really gap4 + indexgap , but we write it first and round it up */ 1245 for (cnt=0;cnt<425;cnt++) 1246 *ptr++=0x92549254; 1247 1248/* the following is just guessed */ 1249 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */ 1250 for(cnt=0;cnt<473;cnt++) 1251 *ptr++=0x92549254; 1252 1253/* now the index marks...*/ 1254 for (cnt=0;cnt<20;cnt++) 1255 *ptr++=0x92549254; 1256 for (cnt=0;cnt<6;cnt++) 1257 *ptr++=0xaaaaaaaa; 1258 *ptr++=0x52245224; 1259 *ptr++=0x52245552; 1260 for (cnt=0;cnt<20;cnt++) 1261 *ptr++=0x92549254; 1262 1263/* sectors */ 1264 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++) 1265 ptr=ms_putsec(disk,ptr,cnt); 1266 1267 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */ 1268} 1269 1270/* 1271 * Here comes the high level stuff (i.e. the filesystem interface) 1272 * and helper functions. 1273 * Normally this should be the only part that has to be adapted to 1274 * different kernel versions. 1275 */ 1276 1277static void flush_track_callback(unsigned long nr) 1278{ 1279 nr&=3; 1280 writefromint = 1; 1281 if (!try_fdc(nr)) { 1282 /* we might block in an interrupt, so try again later */ 1283 flush_track_timer[nr].expires = jiffies + 1; 1284 add_timer(flush_track_timer + nr); 1285 return; 1286 } 1287 get_fdc(nr); 1288 (*unit[nr].dtype->write_fkt)(nr); 1289 if (!raw_write(nr)) { 1290 printk (KERN_NOTICE "floppy disk write protected\n"); 1291 writefromint = 0; 1292 writepending = 0; 1293 } 1294 rel_fdc(); 1295} 1296 1297static int non_int_flush_track (unsigned long nr) 1298{ 1299 unsigned long flags; 1300 1301 nr&=3; 1302 writefromint = 0; 1303 del_timer(&post_write_timer); 1304 get_fdc(nr); 1305 if (!fd_motor_on(nr)) { 1306 writepending = 0; 1307 rel_fdc(); 1308 return 0; 1309 } 1310 save_flags(flags); 1311 cli(); 1312 if (writepending != 2) { 1313 restore_flags(flags); 1314 (*unit[nr].dtype->write_fkt)(nr); 1315 if (!raw_write(nr)) { 1316 printk (KERN_NOTICE "floppy disk write protected " 1317 "in write!\n"); 1318 writepending = 0; 1319 return 0; 1320 } 1321 while (block_flag == 2) 1322 sleep_on (&wait_fd_block); 1323 } 1324 else { 1325 restore_flags(flags); 1326 ms_delay(2); /* 2 ms post_write delay */ 1327 post_write(nr); 1328 } 1329 rel_fdc(); 1330 return 1; 1331} 1332 1333static int get_track(int drive, int track) 1334{ 1335 int error, errcnt; 1336 1337 drive&=3; 1338 if (unit[drive].track == track) 1339 return 0; 1340 get_fdc(drive); 1341 if (!fd_motor_on(drive)) { 1342 rel_fdc(); 1343 return -1; 1344 } 1345 1346 if (unit[drive].dirty == 1) { 1347 del_timer (flush_track_timer + drive); 1348 non_int_flush_track (drive); 1349 } 1350 errcnt = 0; 1351 while (errcnt < MAX_ERRORS) { 1352 if (!fd_seek(drive, track)) 1353 return -1; 1354 raw_read(drive); 1355 error = (*unit[drive].dtype->read_fkt)(drive); 1356 if (error == 0) { 1357 rel_fdc(); 1358 return 0; 1359 } 1360 /* Read Error Handling: recalibrate and try again */ 1361 unit[drive].track = -1; 1362 errcnt++; 1363 } 1364 rel_fdc(); 1365 return -1; 1366} 1367 1368static void redo_fd_request(void) 1369{ 1370 unsigned int cnt, block, track, sector; 1371 int device, drive; 1372 struct amiga_floppy_struct *floppy; 1373 char *data; 1374 unsigned long flags; 1375 1376 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){ 1377 return; 1378 } 1379 1380 repeat: 1381 if (QUEUE_EMPTY) { 1382 /* Nothing left to do */ 1383 return; 1384 } 1385 1386 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) 1387 panic(DEVICE_NAME ": request list destroyed"); 1388 1389 if (CURRENT->bh && !buffer_locked(CURRENT->bh)) 1390 panic(DEVICE_NAME ": block not locked"); 1391 1392 device = MINOR(CURRENT_DEVICE); 1393 if (device < 8) { 1394 /* manual selection */ 1395 drive = device & 3; 1396 floppy = unit + drive; 1397 } else { 1398 /* Auto-detection */ 1399#ifdef DEBUG 1400 printk("redo_fd_request: can't handle auto detect\n"); 1401 printk("redo_fd_request: default to normal\n"); 1402#endif 1403 drive = device & 3; 1404 floppy = unit + drive; 1405 } 1406 1407 /* Here someone could investigate to be more efficient */ 1408 for (cnt = 0; cnt < CURRENT->current_nr_sectors; cnt++) { 1409#ifdef DEBUG 1410 printk("fd: sector %ld + %d requested for %s\n", 1411 CURRENT->sector,cnt, 1412 (CURRENT->cmd==READ)?"read":"write"); 1413#endif 1414 block = CURRENT->sector + cnt; 1415 if ((int)block > floppy->blocks) { 1416 end_request(0); 1417 goto repeat; 1418 } 1419 1420 track = block / (floppy->dtype->sects * floppy->type->sect_mult); 1421 sector = block % (floppy->dtype->sects * floppy->type->sect_mult); 1422 data = CURRENT->buffer + 512 * cnt; 1423#ifdef DEBUG 1424 printk("access to track %d, sector %d, with buffer at " 1425 "0x%08lx\n", track, sector, data); 1426#endif 1427 1428 if ((CURRENT->cmd != READ) && (CURRENT->cmd != WRITE)) { 1429 printk(KERN_WARNING "do_fd_request: unknown command\n"); 1430 end_request(0); 1431 goto repeat; 1432 } 1433 if (get_track(drive, track) == -1) { 1434 end_request(0); 1435 goto repeat; 1436 } 1437 1438 switch (CURRENT->cmd) { 1439 case READ: 1440 memcpy(data, unit[drive].trackbuf + sector * 512, 512); 1441 break; 1442 1443 case WRITE: 1444 memcpy(unit[drive].trackbuf + sector * 512, data, 512); 1445 1446 /* keep the drive spinning while writes are scheduled */ 1447 if (!fd_motor_on(drive)) { 1448 end_request(0); 1449 goto repeat; 1450 } 1451 /* 1452 * setup a callback to write the track buffer 1453 * after a short (1 tick) delay. 1454 */ 1455 save_flags (flags); 1456 cli(); 1457 1458 unit[drive].dirty = 1; 1459 /* reset the timer */ 1460 del_timer (flush_track_timer + drive); 1461 1462 flush_track_timer[drive].expires = jiffies + 1; 1463 add_timer (flush_track_timer + drive); 1464 restore_flags (flags); 1465 break; 1466 } 1467 } 1468 CURRENT->nr_sectors -= CURRENT->current_nr_sectors; 1469 CURRENT->sector += CURRENT->current_nr_sectors; 1470 1471 end_request(1); 1472 goto repeat; 1473} 1474 1475static void do_fd_request(request_queue_t * q) 1476{ 1477 redo_fd_request(); 1478} 1479 1480static int fd_ioctl(struct inode *inode, struct file *filp, 1481 unsigned int cmd, unsigned long param) 1482{ 1483 int drive = inode->i_rdev & 3; 1484 static struct floppy_struct getprm; 1485 1486 switch(cmd){ 1487 case HDIO_GETGEO: 1488 { 1489 struct hd_geometry loc; 1490 loc.heads = unit[drive].type->heads; 1491 loc.sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult; 1492 loc.cylinders = unit[drive].type->tracks; 1493 loc.start = 0; 1494 if (copy_to_user((void *)param, (void *)&loc, 1495 sizeof(struct hd_geometry))) 1496 return -EFAULT; 1497 break; 1498 } 1499 case FDFMTBEG: 1500 get_fdc(drive); 1501 if (fd_ref[drive] > 1) { 1502 rel_fdc(); 1503 return -EBUSY; 1504 } 1505 fsync_dev(inode->i_rdev); 1506 if (fd_motor_on(drive) == 0) { 1507 rel_fdc(); 1508 return -ENODEV; 1509 } 1510 if (fd_calibrate(drive) == 0) { 1511 rel_fdc(); 1512 return -ENXIO; 1513 } 1514 floppy_off(drive); 1515 rel_fdc(); 1516 break; 1517 case FDFMTTRK: 1518 if (param < unit[drive].type->tracks * unit[drive].type->heads) 1519 { 1520 get_fdc(drive); 1521 if (fd_seek(drive,param) != 0){ 1522 memset(unit[drive].trackbuf, FD_FILL_BYTE, 1523 unit[drive].dtype->sects * unit[drive].type->sect_mult * 512); 1524 non_int_flush_track(drive); 1525 } 1526 floppy_off(drive); 1527 rel_fdc(); 1528 } 1529 else 1530 return -EINVAL; 1531 break; 1532 case FDFMTEND: 1533 floppy_off(drive); 1534 invalidate_device(inode->i_rdev, 0); 1535 break; 1536 case FDGETPRM: 1537 memset((void *)&getprm, 0, sizeof (getprm)); 1538 getprm.track=unit[drive].type->tracks; 1539 getprm.head=unit[drive].type->heads; 1540 getprm.sect=unit[drive].dtype->sects * unit[drive].type->sect_mult; 1541 getprm.size=unit[drive].blocks; 1542 if (copy_to_user((void *)param, 1543 (void *)&getprm, 1544 sizeof(struct floppy_struct))) 1545 return -EFAULT; 1546 break; 1547 case BLKGETSIZE: 1548 return put_user(unit[drive].blocks,(unsigned long *)param); 1549 break; 1550 case BLKGETSIZE64: 1551 return put_user((u64)unit[drive].blocks << 9, (u64 *)param); 1552 break; 1553 case FDSETPRM: 1554 case FDDEFPRM: 1555 return -EINVAL; 1556 case FDFLUSH: /* unconditionally, even if not needed */ 1557 del_timer (flush_track_timer + drive); 1558 non_int_flush_track(drive); 1559 break; 1560#ifdef RAW_IOCTL 1561 case IOCTL_RAW_TRACK: 1562 if (copy_to_user((void *)param, raw_buf, 1563 unit[drive].type->read_size)) 1564 return -EFAULT; 1565 else 1566 return unit[drive].type->read_size; 1567#endif 1568 default: 1569 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.", 1570 cmd, drive); 1571 return -ENOSYS; 1572 } 1573 return 0; 1574} 1575 1576static void fd_probe(int dev) 1577{ 1578 unsigned long code; 1579 int type; 1580 int drive; 1581 1582 drive = dev & 3; 1583 code = fd_get_drive_id(drive); 1584 1585 /* get drive type */ 1586 for (type = 0; type < num_dr_types; type++) 1587 if (drive_types[type].code == code) 1588 break; 1589 1590 if (type >= num_dr_types) { 1591 printk(KERN_WARNING "fd_probe: unsupported drive type " 1592 "%08lx found\n", code); 1593 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */ 1594 return; 1595 } 1596 1597 unit[drive].type = drive_types + type; 1598 unit[drive].track = -1; 1599 1600 unit[drive].disk = -1; 1601 unit[drive].motor = 0; 1602 unit[drive].busy = 0; 1603 unit[drive].status = -1; 1604} 1605 1606/* 1607 * floppy_open check for aliasing (/dev/fd0 can be the same as 1608 * /dev/PS0 etc), and disallows simultaneous access to the same 1609 * drive with different device numbers. 1610 */ 1611static int floppy_open(struct inode *inode, struct file *filp) 1612{ 1613 int drive; 1614 int old_dev; 1615 int system; 1616 unsigned long flags; 1617 1618 drive = MINOR(inode->i_rdev) & 3; 1619 old_dev = fd_device[drive]; 1620 1621 if (fd_ref[drive]) 1622 if (old_dev != inode->i_rdev) 1623 return -EBUSY; 1624 1625 if (unit[drive].type->code == FD_NODRIVE) 1626 return -ENODEV; 1627 1628 if (filp && filp->f_mode & 3) { 1629 check_disk_change(inode->i_rdev); 1630 if (filp->f_mode & 2 ) { 1631 int wrprot; 1632 1633 get_fdc(drive); 1634 fd_select (drive); 1635 wrprot = !(ciaa.pra & DSKPROT); 1636 fd_deselect (drive); 1637 rel_fdc(); 1638 1639 if (wrprot) 1640 return -EROFS; 1641 } 1642 } 1643 1644 save_flags(flags); 1645 cli(); 1646 fd_ref[drive]++; 1647 fd_device[drive] = inode->i_rdev; 1648#ifdef MODULE 1649 if (unit[drive].motor == 0) 1650 MOD_INC_USE_COUNT; 1651#endif 1652 restore_flags(flags); 1653 1654 if (old_dev && old_dev != inode->i_rdev) 1655 invalidate_buffers(old_dev); 1656 1657 system=(inode->i_rdev & 4)>>2; 1658 unit[drive].dtype=&data_types[system]; 1659 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks* 1660 data_types[system].sects*unit[drive].type->sect_mult; 1661 floppy_sizes[MINOR(inode->i_rdev)] = unit[drive].blocks >> 1; 1662 1663 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive, 1664 unit[drive].type->name, data_types[system].name); 1665 1666 return 0; 1667} 1668 1669static int floppy_release(struct inode * inode, struct file * filp) 1670{ 1671 int drive = MINOR(inode->i_rdev) & 3; 1672 1673 if (unit[drive].dirty == 1) { 1674 del_timer (flush_track_timer + drive); 1675 non_int_flush_track (drive); 1676 } 1677 1678 if (!fd_ref[drive]--) { 1679 printk(KERN_CRIT "floppy_release with fd_ref == 0"); 1680 fd_ref[drive] = 0; 1681 } 1682#ifdef MODULE 1683/* the mod_use counter is handled this way */ 1684 floppy_off (drive | 0x40000000); 1685#endif 1686 return 0; 1687} 1688 1689/* 1690 * floppy-change is never called from an interrupt, so we can relax a bit 1691 * here, sleep etc. Note that floppy-on tries to set current_DOR to point 1692 * to the desired drive, but it will probably not survive the sleep if 1693 * several floppies are used at the same time: thus the loop. 1694 */ 1695static int amiga_floppy_change(kdev_t dev) 1696{ 1697 int drive = MINOR(dev) & 3; 1698 int changed; 1699 static int first_time = 1; 1700 1701 if (MAJOR(dev) != MAJOR_NR) { 1702 printk(KERN_CRIT "floppy_change: not a floppy\n"); 1703 return 0; 1704 } 1705 1706 if (first_time) 1707 changed = first_time--; 1708 else { 1709 get_fdc(drive); 1710 fd_select (drive); 1711 changed = !(ciaa.pra & DSKCHANGE); 1712 fd_deselect (drive); 1713 rel_fdc(); 1714 } 1715 1716 if (changed) { 1717 fd_probe(drive); 1718 unit[drive].track = -1; 1719 unit[drive].dirty = 0; 1720 writepending = 0; /* if this was true before, too bad! */ 1721 writefromint = 0; 1722 return 1; 1723 } 1724 return 0; 1725} 1726 1727static struct block_device_operations floppy_fops = { 1728 owner: THIS_MODULE, 1729 open: floppy_open, 1730 release: floppy_release, 1731 ioctl: fd_ioctl, 1732 check_media_change: amiga_floppy_change, 1733}; 1734 1735void __init amiga_floppy_setup (char *str, int *ints) 1736{ 1737 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", ints[1]); 1738 fd_def_df0 = ints[1]; 1739} 1740 1741static int __init fd_probe_drives(void) 1742{ 1743 int drive,drives,nomem; 1744 1745 printk(KERN_INFO "FD: probing units\n" KERN_INFO "found "); 1746 drives=0; 1747 nomem=0; 1748 for(drive=0;drive<FD_MAX_UNITS;drive++) { 1749 fd_probe(drive); 1750 if (unit[drive].type->code != FD_NODRIVE) { 1751 drives++; 1752 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) { 1753 printk("no mem for "); 1754 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */ 1755 drives--; 1756 nomem = 1; 1757 } 1758 printk("fd%d ",drive); 1759 } 1760 } 1761 if ((drives > 0) || (nomem == 0)) { 1762 if (drives == 0) 1763 printk("no drives"); 1764 printk("\n"); 1765 return drives; 1766 } 1767 printk("\n"); 1768 return -ENOMEM; 1769} 1770 1771int __init amiga_floppy_init(void) 1772{ 1773 int i; 1774 1775 if (!AMIGAHW_PRESENT(AMI_FLOPPY)) 1776 return -ENXIO; 1777 1778 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) { 1779 printk("fd: Unable to get major %d for floppy\n",MAJOR_NR); 1780 return -EBUSY; 1781 } 1782 /* 1783 * We request DSKPTR, DSKLEN and DSKDATA only, because the other 1784 * floppy registers are too spreaded over the custom register space 1785 */ 1786 if (!request_mem_region(CUSTOM_PHYSADDR+0x20, 8, "amiflop [Paula]")) { 1787 printk("fd: cannot get floppy registers\n"); 1788 unregister_blkdev(MAJOR_NR,"fd"); 1789 return -EBUSY; 1790 } 1791 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE, "Floppy")) == 1792 NULL) { 1793 printk("fd: cannot get chip mem buffer\n"); 1794 release_mem_region(CUSTOM_PHYSADDR+0x20, 8); 1795 unregister_blkdev(MAJOR_NR,"fd"); 1796 return -ENOMEM; 1797 } 1798 if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) { 1799 printk("fd: cannot get irq for dma\n"); 1800 amiga_chip_free(raw_buf); 1801 release_mem_region(CUSTOM_PHYSADDR+0x20, 8); 1802 unregister_blkdev(MAJOR_NR,"fd"); 1803 return -EBUSY; 1804 } 1805 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) { 1806 printk("fd: cannot get irq for timer\n"); 1807 free_irq(IRQ_AMIGA_DSKBLK, NULL); 1808 amiga_chip_free(raw_buf); 1809 release_mem_region(CUSTOM_PHYSADDR+0x20, 8); 1810 unregister_blkdev(MAJOR_NR,"fd"); 1811 return -EBUSY; 1812 } 1813 if (fd_probe_drives() < 1) { /* No usable drives */ 1814 free_irq(IRQ_AMIGA_CIAA_TB, NULL); 1815 free_irq(IRQ_AMIGA_DSKBLK, NULL); 1816 amiga_chip_free(raw_buf); 1817 release_mem_region(CUSTOM_PHYSADDR+0x20, 8); 1818 unregister_blkdev(MAJOR_NR,"fd"); 1819 return -ENXIO; 1820 } 1821 1822 /* initialize variables */ 1823 init_timer(&motor_on_timer); 1824 motor_on_timer.expires = 0; 1825 motor_on_timer.data = 0; 1826 motor_on_timer.function = motor_on_callback; 1827 for (i = 0; i < FD_MAX_UNITS; i++) { 1828 init_timer(&motor_off_timer[i]); 1829 motor_off_timer[i].expires = 0; 1830 motor_off_timer[i].data = i|0x80000000; 1831 motor_off_timer[i].function = fd_motor_off; 1832 init_timer(&flush_track_timer[i]); 1833 flush_track_timer[i].expires = 0; 1834 flush_track_timer[i].data = i; 1835 flush_track_timer[i].function = flush_track_callback; 1836 1837 unit[i].track = -1; 1838 } 1839 1840 init_timer(&post_write_timer); 1841 post_write_timer.expires = 0; 1842 post_write_timer.data = 0; 1843 post_write_timer.function = post_write; 1844 1845 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST); 1846 blksize_size[MAJOR_NR] = floppy_blocksizes; 1847 blk_size[MAJOR_NR] = floppy_sizes; 1848 1849 for (i = 0; i < 128; i++) 1850 mfmdecode[i]=255; 1851 for (i = 0; i < 16; i++) 1852 mfmdecode[mfmencode[i]]=i; 1853 1854 /* make sure that disk DMA is enabled */ 1855 custom.dmacon = DMAF_SETCLR | DMAF_DISK; 1856 1857 /* init ms timer */ 1858 ciaa.crb = 8; /* one-shot, stop */ 1859 1860 (void)do_floppy; /* avoid warning about unused variable */ 1861 return 0; 1862} 1863 1864#ifdef MODULE 1865#include <linux/version.h> 1866 1867int init_module(void) 1868{ 1869 if (!MACH_IS_AMIGA) 1870 return -ENXIO; 1871 return amiga_floppy_init(); 1872} 1873 1874void cleanup_module(void) 1875{ 1876 int i; 1877 1878 for( i = 0; i < FD_MAX_UNITS; i++) 1879 if (unit[i].type->code != FD_NODRIVE) 1880 kfree(unit[i].trackbuf); 1881 free_irq(IRQ_AMIGA_CIAA_TB, NULL); 1882 free_irq(IRQ_AMIGA_DSKBLK, NULL); 1883 custom.dmacon = DMAF_DISK; /* disable DMA */ 1884 amiga_chip_free(raw_buf); 1885 blk_size[MAJOR_NR] = NULL; 1886 blksize_size[MAJOR_NR] = NULL; 1887 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR)); 1888 release_mem_region(CUSTOM_PHYSADDR+0x20, 8); 1889 unregister_blkdev(MAJOR_NR, "fd"); 1890} 1891#endif 1892