File: /usr/src/linux/drivers/block/amiflop.c

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