Linux Audio

Check our new training course

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