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