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