2 * linux/amiga/amiflop.c
4 * Copyright (C) 1993 Greg Harp
5 * Portions of this driver are based on code contributed by Brad Pepers
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)
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
18 * - fixed a bug with the internal trackbuffer when using multiple
20 * - made formatting a bit safer
21 * - added command line and machine based default for "silent" df0
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)
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
32 * - replaced udelays() by a timer (CIAA timer B) for the waits
33 * needed for the disk mechanic.
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
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
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.
49 * restructured jan 1997 by Joerg Dorchain
50 * - Fixed Bug accessing multiple disks
52 * - added trackbuffer for each drive to speed things up
53 * - fixed some race conditions (who finds the next may send it to me ;-)
56 #include <linux/module.h>
58 #include <linux/sched.h>
60 #include <linux/fcntl.h>
61 #include <linux/kernel.h>
62 #include <linux/timer.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>
74 #include <asm/setup.h>
75 #include <asm/uaccess.h>
76 #include <asm/amigahw.h>
77 #include <asm/amigaints.h>
80 #define MAJOR_NR FLOPPY_MAJOR
81 #include <linux/blk.h>
83 #undef DEBUG /* print _LOTS_ of infos */
87 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
97 #define FD_OK 0 /* operation succeeded */
98 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
99 #define FD_NOUNIT 1 /* unit does not exist */
100 #define FD_UNITBUSY 2 /* unit already active */
101 #define FD_NOTACTIVE 3 /* unit is not active */
102 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
112 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
113 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
114 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
115 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
117 static long int fd_def_df0
= FD_DD_3
; /* default for df0 if it doesn't identify */
119 MODULE_PARM(fd_def_df0
,"l");
124 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
125 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
126 #define SELECT(mask) (ciab.prb &= ~mask)
127 #define DESELECT(mask) (ciab.prb |= mask)
128 #define SELMASK(drive) (1 << (3 + (drive & 3)))
130 #define DRIVE(x) ((x) & 3)
131 #define PROBE(x) ((x) >> 2) & 1)
132 #define TYPE(x) ((x) >> 3) & 2)
133 #define DATA(x) ((x) >> 5) & 3)
135 static struct fd_drive_type drive_types
[] = {
136 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
137 /* warning: times are now in milliseconds (ms) */
138 { FD_DD_3
, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
139 { FD_HD_3
, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
140 { FD_DD_5
, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
141 { FD_NODRIVE
, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
143 static int num_dr_types
= sizeof(drive_types
) / sizeof(drive_types
[0]);
145 /* defaults for 3 1/2" HD-Disks */
146 static int floppy_sizes
[256]={880,880,880,880,720,720,720,720,};
147 static int floppy_blocksizes
[256]={0,};
148 /* hardsector size assumed to be 512 */
150 static int amiga_read(int), dos_read(int);
151 static void amiga_write(int), dos_write(int);
152 static struct fd_data_type data_types
[] = {
153 { "Amiga", 11 , amiga_read
, amiga_write
},
154 { "MS-Dos", 9, dos_read
, dos_write
}
157 /* current info on each unit */
158 static struct amiga_floppy_struct unit
[FD_MAX_UNITS
] = {{ 0,}};
160 static struct timer_list flush_track_timer
[FD_MAX_UNITS
];
161 static struct timer_list post_write_timer
;
162 static struct timer_list motor_on_timer
;
163 static struct timer_list motor_off_timer
[FD_MAX_UNITS
];
164 static int on_attempts
;
166 /* Synchronization of FDC access */
167 /* request loop (trackbuffer) */
168 static volatile int fdc_busy
= -1;
169 static volatile int fdc_nested
= 0;
170 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait
);
172 static DECLARE_WAIT_QUEUE_HEAD(motor_wait
);
174 static volatile int selected
= -1; /* currently selected drive */
176 static int writepending
= 0;
177 static int writefromint
= 0;
178 static char *raw_buf
;
180 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
183 * These are global variables, as that's the easiest way to give
184 * information to interrupts. They are the data used for the current
187 static volatile char block_flag
= 0;
188 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block
);
190 /* MS-Dos MFM Coding tables (should go quick and easy) */
191 static unsigned char mfmencode
[16]={
192 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
193 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
195 static unsigned char mfmdecode
[128];
197 /* floppy internal millisecond timer stuff */
198 static volatile int ms_busy
= -1;
199 static DECLARE_WAIT_QUEUE_HEAD(ms_wait
);
200 #define MS_TICKS ((amiga_eclock+50)/1000)
203 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
204 * max X times - some types of errors increase the errorcount by 2 or
205 * even 3, so we might actually retry only X/2 times before giving up.
207 #define MAX_ERRORS 12
209 /* Prevent "aliased" accesses. */
210 static int fd_ref
[4] = { 0,0,0,0 };
211 static int fd_device
[4] = { 0,0,0,0 };
214 * Current device number. Taken either from the block header or from the
215 * format request descriptor.
217 #define CURRENT_DEVICE (CURRENT->rq_dev)
219 /* Current error count. */
220 #define CURRENT_ERRORS (CURRENT->errors)
225 * Here come the actual hardware access and helper functions.
226 * They are not reentrant and single threaded because all drives
227 * share the same hardware and the same trackbuffer.
230 /* Milliseconds timer */
232 static void ms_isr(int irq
, void *dummy
, struct pt_regs
*fp
)
238 /* all waits are queued up
239 A more generic routine would do a schedule a la timer.device */
240 static void ms_delay(int ms
)
250 restore_flags(flags
);
251 ticks
= MS_TICKS
*ms
-1;
254 ciaa
.crb
=0x19; /*count eclock, force load, one-shoot, start */
259 /* Hardware semaphore */
261 /* returns true when we would get the semaphore */
262 static inline int try_fdc(int drive
)
265 return ((fdc_busy
< 0) || (fdc_busy
== drive
));
268 static void get_fdc(int drive
)
274 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive
,fdc_busy
,fdc_nested
);
278 while (!try_fdc(drive
))
282 restore_flags(flags
);
285 static inline void rel_fdc(void)
289 printk("fd: unmatched rel_fdc\n");
290 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy
,fdc_nested
);
293 if (fdc_nested
== 0) {
299 static void fd_select (int drive
)
301 unsigned char prb
= ~0;
305 printk("selecting %d\n",drive
);
307 if (drive
== selected
)
312 if (unit
[drive
].track
% 2 != 0)
314 if (unit
[drive
].motor
== 1)
316 ciab
.prb
|= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
318 prb
&= ~SELMASK(drive
);
323 static void fd_deselect (int drive
)
330 printk("deselecting %d\n",drive
);
332 if (drive
!= selected
) {
333 printk(KERN_WARNING
"Deselecting drive %d while %d was selected!\n",drive
,selected
);
344 prb
|= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
347 restore_flags (flags
);
352 static void motor_on_callback(unsigned long nr
)
354 if (!(ciaa
.pra
& DSKRDY
) || --on_attempts
== 0) {
355 wake_up (&motor_wait
);
357 motor_on_timer
.expires
= jiffies
+ HZ
/10;
358 add_timer(&motor_on_timer
);
362 static int fd_motor_on(int nr
)
366 del_timer(motor_off_timer
+ nr
);
368 if (!unit
[nr
].motor
) {
372 del_timer(&motor_on_timer
);
373 motor_on_timer
.data
= nr
;
374 motor_on_timer
.expires
= jiffies
+ HZ
/2;
375 add_timer(&motor_on_timer
);
378 sleep_on (&motor_wait
);
382 if (on_attempts
== 0) {
385 printk (KERN_ERR
"motor_on failed, turning motor off\n");
389 printk (KERN_WARNING
"DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
396 static void fd_motor_off(unsigned long drive
)
402 decusecount
= drive
& 0x40000000;
404 calledfromint
= drive
& 0x80000000;
406 if (calledfromint
&& !try_fdc(drive
)) {
407 /* We would be blocked in an interrupt, so try again later */
408 motor_off_timer
[drive
].expires
= jiffies
+ 1;
409 add_timer(motor_off_timer
+ drive
);
412 unit
[drive
].motor
= 0;
419 this is the last interrupt for any drive access, happens after
420 release (from floppy_off). So we have to wait until now to decrease
428 static void floppy_off (unsigned int nr
)
433 del_timer(motor_off_timer
+ drive
);
434 motor_off_timer
[drive
].expires
= jiffies
+ 3*HZ
;
435 /* called this way it is always from interrupt */
436 motor_off_timer
[drive
].data
= nr
| 0x80000000;
437 add_timer(motor_off_timer
+ nr
);
440 static int fd_calibrate(int drive
)
447 if (!fd_motor_on (drive
))
454 for (n
= unit
[drive
].type
->tracks
/2; n
!= 0; --n
) {
455 if (ciaa
.pra
& DSKTRACK0
)
462 ms_delay(unit
[drive
].type
->step_delay
);
464 ms_delay (unit
[drive
].type
->settle_time
);
466 n
= unit
[drive
].type
->tracks
+ 20;
473 ms_delay(unit
[drive
].type
->step_delay
+ 1);
474 if ((ciaa
.pra
& DSKTRACK0
) == 0)
477 printk (KERN_ERR
"fd%d: calibrate failed, turning motor off\n", drive
);
478 fd_motor_off (drive
);
479 unit
[drive
].track
= -1;
484 unit
[drive
].track
= 0;
485 ms_delay(unit
[drive
].type
->settle_time
);
492 static int fd_seek(int drive
, int track
)
498 printk("seeking drive %d to track %d\n",drive
,track
);
502 if (unit
[drive
].track
== track
) {
506 if (!fd_motor_on(drive
)) {
510 if (unit
[drive
].track
< 0 && !fd_calibrate(drive
)) {
516 cnt
= unit
[drive
].track
/2 - track
/2;
518 prb
|= DSKSIDE
| DSKDIREC
;
526 if (track
% 2 != unit
[drive
].track
% 2)
527 ms_delay (unit
[drive
].type
->side_time
);
528 unit
[drive
].track
= track
;
540 ms_delay (unit
[drive
].type
->step_delay
);
541 } while (--cnt
!= 0);
542 ms_delay (unit
[drive
].type
->settle_time
);
549 static unsigned long fd_get_drive_id(int drive
)
559 SELECT(SELMASK(drive
));
561 DESELECT(SELMASK(drive
));
565 SELECT(SELMASK(drive
));
567 DESELECT(SELMASK(drive
));
570 /* loop and read disk ID */
571 for (i
=0; i
<32; i
++) {
572 SELECT(SELMASK(drive
));
575 /* read and store value of DSKRDY */
577 id
|= (ciaa
.pra
& DSKRDY
) ? 0 : 1; /* cia regs are low-active! */
579 DESELECT(SELMASK(drive
));
585 * RB: At least A500/A2000's df0: don't identify themselves.
586 * As every (real) Amiga has at least a 3.5" DD drive as df0:
587 * we default to that if df0: doesn't identify as a certain
590 if(drive
== 0 && id
== FD_NODRIVE
)
593 printk(KERN_NOTICE
"fd: drive 0 didn't identify, setting default %08lx\n", (ulong
)fd_def_df0
);
595 /* return the ID value */
599 static void fd_block_done(int irq
, void *dummy
, struct pt_regs
*fp
)
602 custom
.dsklen
= 0x4000;
604 if (block_flag
== 2) { /* writing */
606 post_write_timer
.expires
= jiffies
+ 1; /* at least 2 ms */
607 post_write_timer
.data
= selected
;
608 add_timer(&post_write_timer
);
612 wake_up (&wait_fd_block
);
616 static void raw_read(int drive
)
621 sleep_on(&wait_fd_block
);
623 /* setup adkcon bits correctly */
624 custom
.adkcon
= ADK_MSBSYNC
;
625 custom
.adkcon
= ADK_SETCLR
|ADK_WORDSYNC
|ADK_FAST
;
627 custom
.dsksync
= MFM_SYNC
;
630 custom
.dskptr
= (u_char
*)ZTWO_PADDR((u_char
*)raw_buf
);
631 custom
.dsklen
= unit
[drive
].type
->read_size
/sizeof(short) | DSKLEN_DMAEN
;
632 custom
.dsklen
= unit
[drive
].type
->read_size
/sizeof(short) | DSKLEN_DMAEN
;
637 sleep_on (&wait_fd_block
);
644 static int raw_write(int drive
)
649 get_fdc(drive
); /* corresponds to rel_fdc() in post_write() */
650 if ((ciaa
.pra
& DSKPROT
) == 0) {
655 sleep_on(&wait_fd_block
);
657 /* clear adkcon bits */
658 custom
.adkcon
= ADK_PRECOMP1
|ADK_PRECOMP0
|ADK_WORDSYNC
|ADK_MSBSYNC
;
659 /* set appropriate adkcon bits */
660 adk
= ADK_SETCLR
|ADK_FAST
;
661 if ((ulong
)unit
[drive
].track
>= unit
[drive
].type
->precomp2
)
663 else if ((ulong
)unit
[drive
].track
>= unit
[drive
].type
->precomp1
)
667 custom
.dsklen
= DSKLEN_WRITE
;
668 custom
.dskptr
= (u_char
*)ZTWO_PADDR((u_char
*)raw_buf
);
669 custom
.dsklen
= unit
[drive
].type
->write_size
/sizeof(short) | DSKLEN_DMAEN
|DSKLEN_WRITE
;
670 custom
.dsklen
= unit
[drive
].type
->write_size
/sizeof(short) | DSKLEN_DMAEN
|DSKLEN_WRITE
;
677 * to be called at least 2ms after the write has finished but before any
678 * other access to the hardware.
680 static void post_write (unsigned long drive
)
683 printk("post_write for drive %ld\n",drive
);
690 unit
[drive
].dirty
= 0;
691 wake_up(&wait_fd_block
);
693 rel_fdc(); /* corresponds to get_fdc() in raw_write */
698 * The following functions are to convert the block contents into raw data
699 * written to disk and vice versa.
700 * (Add other formats here ;-))
703 static unsigned long scan_sync(unsigned long raw
, unsigned long end
)
705 ushort
*ptr
= (ushort
*)raw
, *endp
= (ushort
*)end
;
707 while (ptr
< endp
&& *ptr
++ != 0x4489)
710 while (*ptr
== 0x4489 && ptr
< endp
)
717 static inline unsigned long checksum(unsigned long *addr
, int len
)
719 unsigned long csum
= 0;
721 len
/= sizeof(*addr
);
724 csum
= ((csum
>>1) & 0x55555555) ^ (csum
& 0x55555555);
729 static unsigned long decode (unsigned long *data
, unsigned long *raw
,
734 /* convert length from bytes to longwords */
739 /* prepare return pointer */
743 *data
++ = ((*odd
++ & 0x55555555) << 1) | (*even
++ & 0x55555555);
744 } while (--len
!= 0);
754 unsigned char labels
[16];
755 unsigned long hdrchk
;
756 unsigned long datachk
;
759 static int amiga_read(int drive
)
768 raw
= (long) raw_buf
;
769 end
= raw
+ unit
[drive
].type
->read_size
;
771 for (scnt
= 0;scnt
< unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
; scnt
++) {
772 if (!(raw
= scan_sync(raw
, end
))) {
773 printk (KERN_INFO
"can't find sync for sector %d\n", scnt
);
777 raw
= decode ((ulong
*)&hdr
.magic
, (ulong
*)raw
, 4);
778 raw
= decode ((ulong
*)&hdr
.labels
, (ulong
*)raw
, 16);
779 raw
= decode ((ulong
*)&hdr
.hdrchk
, (ulong
*)raw
, 4);
780 raw
= decode ((ulong
*)&hdr
.datachk
, (ulong
*)raw
, 4);
781 csum
= checksum((ulong
*)&hdr
,
782 (char *)&hdr
.hdrchk
-(char *)&hdr
);
785 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
786 hdr
.magic
, hdr
.track
, hdr
.sect
, hdr
.ord
,
787 *(ulong
*)&hdr
.labels
[0], *(ulong
*)&hdr
.labels
[4],
788 *(ulong
*)&hdr
.labels
[8], *(ulong
*)&hdr
.labels
[12],
789 hdr
.hdrchk
, hdr
.datachk
);
792 if (hdr
.hdrchk
!= csum
) {
793 printk(KERN_INFO
"MFM_HEADER: %08lx,%08lx\n", hdr
.hdrchk
, csum
);
798 if (hdr
.track
!= unit
[drive
].track
) {
799 printk(KERN_INFO
"MFM_TRACK: %d, %d\n", hdr
.track
, unit
[drive
].track
);
803 raw
= decode ((ulong
*)(unit
[drive
].trackbuf
+ hdr
.sect
*512),
805 csum
= checksum((ulong
*)(unit
[drive
].trackbuf
+ hdr
.sect
*512), 512);
807 if (hdr
.datachk
!= csum
) {
808 printk(KERN_INFO
"MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
809 hdr
.magic
, hdr
.track
, hdr
.sect
, hdr
.ord
, scnt
,
811 printk (KERN_INFO
"data=(%lx,%lx,%lx,%lx)\n",
812 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[0],
813 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[1],
814 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[2],
815 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[3]);
823 static void encode(unsigned long data
, unsigned long *dest
)
828 data2
= data
^ 0x55555555;
829 data
|= ((data2
>> 1) | 0x80000000) & (data2
<< 1);
831 if (*(dest
- 1) & 0x00000001)
837 static void encode_block(unsigned long *dest
, unsigned long *src
, int len
)
843 for (cnt
= 0; cnt
< len
/ 4; cnt
++) {
844 data
= src
[cnt
] >> 1;
845 encode(data
, dest
+ to_cnt
++);
849 for (cnt
= 0; cnt
< len
/ 4; cnt
++) {
851 encode(data
, dest
+ to_cnt
++);
855 static unsigned long *putsec(int disk
, unsigned long *raw
, int cnt
)
861 *raw
= (raw
[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
866 hdr
.track
= unit
[disk
].track
;
868 hdr
.ord
= unit
[disk
].dtype
->sects
* unit
[disk
].type
->sect_mult
- cnt
;
869 for (i
= 0; i
< 16; i
++)
871 hdr
.hdrchk
= checksum((ulong
*)&hdr
,
872 (char *)&hdr
.hdrchk
-(char *)&hdr
);
873 hdr
.datachk
= checksum((ulong
*)(unit
[disk
].trackbuf
+cnt
*512), 512);
875 encode_block(raw
, (ulong
*)&hdr
.magic
, 4);
877 encode_block(raw
, (ulong
*)&hdr
.labels
, 16);
879 encode_block(raw
, (ulong
*)&hdr
.hdrchk
, 4);
881 encode_block(raw
, (ulong
*)&hdr
.datachk
, 4);
883 encode_block(raw
, (ulong
*)(unit
[disk
].trackbuf
+cnt
*512), 512);
889 static void amiga_write(int disk
)
892 unsigned long *ptr
= (unsigned long *)raw_buf
;
896 for (cnt
= 0; cnt
< 415 * unit
[disk
].type
->sect_mult
; cnt
++)
900 for (cnt
= 0; cnt
< unit
[disk
].dtype
->sects
* unit
[disk
].type
->sect_mult
; cnt
++)
901 ptr
= putsec (disk
, ptr
, cnt
);
902 *(ushort
*)ptr
= (ptr
[-1]&1) ? 0x2AA8 : 0xAAA8;
907 unsigned char track
, /* 0-80 */
911 unsigned short crc
; /* on 68000 we got an alignment problem,
912 but this compiler solves it by adding silently
913 adding a pad byte so data won't fit
914 and this took about 3h to discover.... */
915 unsigned char gap1
[22]; /* for longword-alignedness (0x4e) */
918 /* crc routines are borrowed from the messydos-handler */
920 /* excerpt from the messydos-device
921 ; The CRC is computed not only over the actual data, but including
922 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
923 ; As we don't read or encode these fields into our buffers, we have to
924 ; preload the registers containing the CRC with the values they would have
925 ; after stepping over these fields.
927 ; How CRCs "really" work:
929 ; First, you should regard a bitstring as a series of coefficients of
930 ; polynomials. We calculate with these polynomials in modulo-2
931 ; arithmetic, in which both add and subtract are done the same as
932 ; exclusive-or. Now, we modify our data (a very long polynomial) in
933 ; such a way that it becomes divisible by the CCITT-standard 16-bit
935 ; polynomial: x + x + x + 1, represented by $11021. The easiest
936 ; way to do this would be to multiply (using proper arithmetic) our
937 ; datablock with $11021. So we have:
939 ; data * ($10000 + $1021) =
940 ; data * $10000 + data * $1021
941 ; The left part of this is simple: Just add two 0 bytes. But then
942 ; the right part (data $1021) remains difficult and even could have
943 ; a carry into the left part. The solution is to use a modified
944 ; multiplication, which has a result that is not correct, but with
945 ; a difference of any multiple of $11021. We then only need to keep
946 ; the 16 least significant bits of the result.
948 ; The following algorithm does this for us:
950 ; unsigned char *data, c, crclo, crchi;
952 ; c = *data++ + crchi;
953 ; crchi = (@ c) >> 8 + crclo;
957 ; Remember, + is done with EOR, the @ operator is in two tables (high
958 ; and low byte separately), which is calculated as
961 ; xor $1021 * (c & $0F)
962 ; xor $1021 * (c >> 4) (* is regular multiplication)
965 ; Anyway, the end result is the same as the remainder of the division of
966 ; the data by $11021. I am afraid I need to study theory a bit more...
969 my only works was to code this from manx to C....
973 static ushort
dos_crc(void * data_a3
, int data_d0
, int data_d1
, int data_d3
)
975 static unsigned char CRCTable1
[] = {
976 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
977 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
978 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
979 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
980 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
981 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
982 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
983 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
984 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
985 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
986 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
987 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
988 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
989 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
990 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
991 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
994 static unsigned char CRCTable2
[] = {
995 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
996 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
997 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
998 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
999 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
1000 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
1001 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
1002 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
1003 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
1004 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
1005 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
1006 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
1007 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
1008 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
1009 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
1010 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
1013 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
1015 register unsigned char *CRCT1
, *CRCT2
, *data
, c
, crch
, crcl
;
1022 for (i
=data_d3
; i
>=0; i
--) {
1023 c
= (*data
++) ^ crch
;
1024 crch
= CRCT1
[c
] ^ crcl
;
1027 return (crch
<<8)|crcl
;
1030 static inline ushort
dos_hdr_crc (struct dos_header
*hdr
)
1032 return dos_crc(&(hdr
->track
), 0xb2, 0x30, 3); /* precomputed magic */
1035 static inline ushort
dos_data_crc(unsigned char *data
)
1037 return dos_crc(data
, 0xe2, 0x95 ,511); /* precomputed magic */
1040 static inline unsigned char dos_decode_byte(ushort word
)
1043 register unsigned char byte
;
1044 register unsigned char *dec
= mfmdecode
;
1056 static unsigned long dos_decode(unsigned char *data
, unsigned short *raw
, int len
)
1060 for (i
= 0; i
< len
; i
++)
1061 *data
++=dos_decode_byte(*raw
++);
1062 return ((ulong
)raw
);
1066 static void dbg(unsigned long ptr
)
1068 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr
,
1069 ((ulong
*)ptr
)[0], ((ulong
*)ptr
)[1],
1070 ((ulong
*)ptr
)[2], ((ulong
*)ptr
)[3]);
1074 static int dos_read(int drive
)
1079 unsigned short crc
,data_crc
[2];
1080 struct dos_header hdr
;
1083 raw
= (long) raw_buf
;
1084 end
= raw
+ unit
[drive
].type
->read_size
;
1086 for (scnt
=0; scnt
< unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
; scnt
++) {
1087 do { /* search for the right sync of each sec-hdr */
1088 if (!(raw
= scan_sync (raw
, end
))) {
1089 printk(KERN_INFO
"dos_read: no hdr sync on "
1090 "track %d, unit %d for sector %d\n",
1091 unit
[drive
].track
,drive
,scnt
);
1097 } while (*((ushort
*)raw
)!=0x5554); /* loop usually only once done */
1098 raw
+=2; /* skip over headermark */
1099 raw
= dos_decode((unsigned char *)&hdr
,(ushort
*) raw
,8);
1100 crc
= dos_hdr_crc(&hdr
);
1103 printk("(%3d,%d,%2d,%d) %x\n", hdr
.track
, hdr
.side
,
1104 hdr
.sec
, hdr
.len_desc
, hdr
.crc
);
1107 if (crc
!= hdr
.crc
) {
1108 printk(KERN_INFO
"dos_read: MFM_HEADER %04x,%04x\n",
1112 if (hdr
.track
!= unit
[drive
].track
/unit
[drive
].type
->heads
) {
1113 printk(KERN_INFO
"dos_read: MFM_TRACK %d, %d\n",
1115 unit
[drive
].track
/unit
[drive
].type
->heads
);
1119 if (hdr
.side
!= unit
[drive
].track
%unit
[drive
].type
->heads
) {
1120 printk(KERN_INFO
"dos_read: MFM_SIDE %d, %d\n",
1122 unit
[drive
].track
%unit
[drive
].type
->heads
);
1126 if (hdr
.len_desc
!= 2) {
1127 printk(KERN_INFO
"dos_read: unknown sector len "
1128 "descriptor %d\n", hdr
.len_desc
);
1132 printk("hdr accepted\n");
1134 if (!(raw
= scan_sync (raw
, end
))) {
1135 printk(KERN_INFO
"dos_read: no data sync on track "
1136 "%d, unit %d for sector%d, disk sector %d\n",
1137 unit
[drive
].track
, drive
, scnt
, hdr
.sec
);
1144 if (*((ushort
*)raw
)!=0x5545) {
1145 printk(KERN_INFO
"dos_read: no data mark after "
1146 "sync (%d,%d,%d,%d) sc=%d\n",
1147 hdr
.track
,hdr
.side
,hdr
.sec
,hdr
.len_desc
,scnt
);
1151 raw
+=2; /* skip data mark (included in checksum) */
1152 raw
= dos_decode((unsigned char *)(unit
[drive
].trackbuf
+ (hdr
.sec
- 1) * 512), (ushort
*) raw
, 512);
1153 raw
= dos_decode((unsigned char *)data_crc
,(ushort
*) raw
,4);
1154 crc
= dos_data_crc(unit
[drive
].trackbuf
+ (hdr
.sec
- 1) * 512);
1156 if (crc
!= data_crc
[0]) {
1157 printk(KERN_INFO
"dos_read: MFM_DATA (%d,%d,%d,%d) "
1158 "sc=%d, %x %x\n", hdr
.track
, hdr
.side
,
1159 hdr
.sec
, hdr
.len_desc
, scnt
,data_crc
[0], crc
);
1160 printk(KERN_INFO
"data=(%lx,%lx,%lx,%lx,...)\n",
1161 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[0],
1162 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[1],
1163 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[2],
1164 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[3]);
1171 static inline ushort
dos_encode_byte(unsigned char byte
)
1173 register unsigned char *enc
, b2
, b1
;
1174 register ushort word
;
1180 word
=enc
[b2
] <<8 | enc
[b1
];
1181 return (word
|((word
&(256|64)) ? 0: 128));
1184 static void dos_encode_block(ushort
*dest
, unsigned char *src
, int len
)
1188 for (i
= 0; i
< len
; i
++) {
1189 *dest
=dos_encode_byte(*src
++);
1190 *dest
|=((dest
[-1]&1)||(*dest
&0x4000))? 0: 0x8000;
1195 static unsigned long *ms_putsec(int drive
, unsigned long *raw
, int cnt
)
1197 static struct dos_header hdr
={0,0,0,2,0,
1198 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1200 static ushort crc
[2]={0,0x4e4e};
1204 /* the MFM word before is always 9254 */
1207 /* 3 sync + 1 headermark */
1211 /* fill in the variable parts of the header */
1212 hdr
.track
=unit
[drive
].track
/unit
[drive
].type
->heads
;
1213 hdr
.side
=unit
[drive
].track
%unit
[drive
].type
->heads
;
1215 hdr
.crc
=dos_hdr_crc(&hdr
);
1217 /* header (without "magic") and id gap 2*/
1218 dos_encode_block((ushort
*)raw
,(unsigned char *) &hdr
.track
,28);
1225 /* 3 syncs and 1 datamark */
1230 dos_encode_block((ushort
*)raw
,
1231 (unsigned char *)unit
[drive
].trackbuf
+cnt
*512,512);
1234 /*data crc + jd's special gap (long words :-/) */
1235 crc
[0]=dos_data_crc(unit
[drive
].trackbuf
+cnt
*512);
1236 dos_encode_block((ushort
*) raw
,(unsigned char *)crc
,4);
1243 return raw
; /* wrote 652 MFM words */
1246 static void dos_write(int disk
)
1249 unsigned long raw
= (unsigned long) raw_buf
;
1250 unsigned long *ptr
=(unsigned long *)raw
;
1253 /* really gap4 + indexgap , but we write it first and round it up */
1254 for (cnt
=0;cnt
<425;cnt
++)
1257 /* the following is just guessed */
1258 if (unit
[disk
].type
->sect_mult
==2) /* check for HD-Disks */
1259 for(cnt
=0;cnt
<473;cnt
++)
1262 /* now the index marks...*/
1263 for (cnt
=0;cnt
<20;cnt
++)
1265 for (cnt
=0;cnt
<6;cnt
++)
1269 for (cnt
=0;cnt
<20;cnt
++)
1273 for(cnt
= 0; cnt
< unit
[disk
].dtype
->sects
* unit
[disk
].type
->sect_mult
; cnt
++)
1274 ptr
=ms_putsec(disk
,ptr
,cnt
);
1276 *(ushort
*)ptr
= 0xaaa8; /* MFM word before is always 0x9254 */
1280 * Here comes the high level stuff (i.e. the filesystem interface)
1281 * and helper functions.
1282 * Normally this should be the only part that has to be adapted to
1283 * different kernel versions.
1286 /* FIXME: this assumes the drive is still spinning -
1287 * which is only true if we complete writing a track within three seconds
1289 static void flush_track_callback(unsigned long nr
)
1294 /* we might block in an interrupt, so try again later */
1295 flush_track_timer
[nr
].expires
= jiffies
+ 1;
1296 add_timer(flush_track_timer
+ nr
);
1300 (*unit
[nr
].dtype
->write_fkt
)(nr
);
1301 if (!raw_write(nr
)) {
1302 printk (KERN_NOTICE
"floppy disk write protected\n");
1309 static int non_int_flush_track (unsigned long nr
)
1311 unsigned long flags
;
1315 del_timer(&post_write_timer
);
1317 if (!fd_motor_on(nr
)) {
1324 if (writepending
!= 2) {
1325 restore_flags(flags
);
1326 (*unit
[nr
].dtype
->write_fkt
)(nr
);
1327 if (!raw_write(nr
)) {
1328 printk (KERN_NOTICE
"floppy disk write protected "
1333 while (block_flag
== 2)
1334 sleep_on (&wait_fd_block
);
1337 restore_flags(flags
);
1338 ms_delay(2); /* 2 ms post_write delay */
1345 static int get_track(int drive
, int track
)
1350 if (unit
[drive
].track
== track
)
1353 if (!fd_motor_on(drive
)) {
1358 if (unit
[drive
].dirty
== 1) {
1359 del_timer (flush_track_timer
+ drive
);
1360 non_int_flush_track (drive
);
1363 while (errcnt
< MAX_ERRORS
) {
1364 if (!fd_seek(drive
, track
))
1367 error
= (*unit
[drive
].dtype
->read_fkt
)(drive
);
1372 /* Read Error Handling: recalibrate and try again */
1373 unit
[drive
].track
= -1;
1380 static void redo_fd_request(void)
1382 unsigned int cnt
, block
, track
, sector
;
1384 struct amiga_floppy_struct
*floppy
;
1386 unsigned long flags
;
1388 if (CURRENT
&& CURRENT
->rq_status
== RQ_INACTIVE
){
1394 /* Nothing left to do */
1398 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
1399 panic(DEVICE_NAME
": request list destroyed");
1401 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
1402 panic(DEVICE_NAME
": block not locked");
1404 device
= MINOR(CURRENT_DEVICE
);
1406 /* manual selection */
1408 floppy
= unit
+ drive
;
1410 /* Auto-detection */
1412 printk("redo_fd_request: can't handle auto detect\n");
1413 printk("redo_fd_request: default to normal\n");
1416 floppy
= unit
+ drive
;
1419 /* Here someone could investigate to be more efficient */
1420 for (cnt
= 0; cnt
< CURRENT
->current_nr_sectors
; cnt
++) {
1422 printk("fd: sector %ld + %d requested for %s\n",
1423 CURRENT
->sector
,cnt
,
1424 (CURRENT
->cmd
==READ
)?"read":"write");
1426 block
= CURRENT
->sector
+ cnt
;
1427 if ((int)block
> floppy
->blocks
) {
1432 track
= block
/ (floppy
->dtype
->sects
* floppy
->type
->sect_mult
);
1433 sector
= block
% (floppy
->dtype
->sects
* floppy
->type
->sect_mult
);
1434 data
= CURRENT
->buffer
+ 512 * cnt
;
1436 printk("access to track %d, sector %d, with buffer at "
1437 "0x%08lx\n", track
, sector
, data
);
1440 if ((CURRENT
->cmd
!= READ
) && (CURRENT
->cmd
!= WRITE
)) {
1441 printk(KERN_WARNING
"do_fd_request: unknown command\n");
1445 if (get_track(drive
, track
) == -1) {
1450 switch (CURRENT
->cmd
) {
1452 memcpy(data
, unit
[drive
].trackbuf
+ sector
* 512, 512);
1456 memcpy(unit
[drive
].trackbuf
+ sector
* 512, data
, 512);
1458 /* keep the drive spinning while writes are scheduled */
1459 if (!fd_motor_on(drive
)) {
1464 * setup a callback to write the track buffer
1465 * after a short (1 tick) delay.
1470 unit
[drive
].dirty
= 1;
1471 /* reset the timer */
1472 del_timer (flush_track_timer
+ drive
);
1474 flush_track_timer
[drive
].expires
= jiffies
+ 1;
1475 add_timer (flush_track_timer
+ drive
);
1476 restore_flags (flags
);
1480 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
1481 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
1487 static void do_fd_request(void)
1492 static int fd_ioctl(struct inode
*inode
, struct file
*filp
,
1493 unsigned int cmd
, unsigned long param
)
1495 int drive
= inode
->i_rdev
& 3;
1496 static struct floppy_struct getprm
;
1497 struct super_block
* sb
;
1502 struct hd_geometry loc
;
1503 loc
.heads
= unit
[drive
].type
->heads
;
1504 loc
.sectors
= unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
;
1505 loc
.cylinders
= unit
[drive
].type
->tracks
;
1507 if (copy_to_user((void *)param
, (void *)&loc
,
1508 sizeof(struct hd_geometry
)))
1514 if (fd_ref
[drive
] > 1) {
1518 fsync_dev(inode
->i_rdev
);
1519 if (fd_motor_on(drive
) == 0) {
1523 if (fd_calibrate(drive
) == 0) {
1531 if (param
< unit
[drive
].type
->tracks
* unit
[drive
].type
->heads
)
1534 if (fd_seek(drive
,param
) != 0){
1535 memset(unit
[drive
].trackbuf
, FD_FILL_BYTE
,
1536 unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
* 512);
1537 non_int_flush_track(drive
);
1547 sb
= get_super(inode
->i_rdev
);
1549 invalidate_inodes(sb
);
1550 invalidate_buffers(inode
->i_rdev
);
1553 memset((void *)&getprm
, 0, sizeof (getprm
));
1554 getprm
.track
=unit
[drive
].type
->tracks
;
1555 getprm
.head
=unit
[drive
].type
->heads
;
1556 getprm
.sect
=unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
;
1557 getprm
.size
=unit
[drive
].blocks
;
1558 if (copy_to_user((void *)param
,
1560 sizeof(struct floppy_struct
)))
1564 return put_user(unit
[drive
].blocks
,(long *)param
);
1569 case FDFLUSH
: /* unconditionally, even if not needed */
1570 del_timer (flush_track_timer
+ drive
);
1571 non_int_flush_track(drive
);
1574 case IOCTL_RAW_TRACK
:
1575 if (copy_to_user((void *)param
, raw_buf
,
1576 unit
[drive
].type
->read_size
))
1579 return unit
[drive
].type
->read_size
;
1582 printk(KERN_DEBUG
"fd_ioctl: unknown cmd %d for drive %d.",
1589 static void fd_probe(int dev
)
1596 code
= fd_get_drive_id(drive
);
1598 /* get drive type */
1599 for (type
= 0; type
< num_dr_types
; type
++)
1600 if (drive_types
[type
].code
== code
)
1603 if (type
>= num_dr_types
) {
1604 printk(KERN_WARNING
"fd_probe: unsupported drive type "
1605 "%08lx found\n", code
);
1606 unit
[drive
].type
= &drive_types
[num_dr_types
-1]; /* FD_NODRIVE */
1610 unit
[drive
].type
= drive_types
+ type
;
1611 unit
[drive
].track
= -1;
1613 unit
[drive
].disk
= -1;
1614 unit
[drive
].motor
= 0;
1615 unit
[drive
].busy
= 0;
1616 unit
[drive
].status
= -1;
1620 * floppy_open check for aliasing (/dev/fd0 can be the same as
1621 * /dev/PS0 etc), and disallows simultaneous access to the same
1622 * drive with different device numbers.
1624 static int floppy_open(struct inode
*inode
, struct file
*filp
)
1629 unsigned long flags
;
1631 drive
= MINOR(inode
->i_rdev
) & 3;
1632 old_dev
= fd_device
[drive
];
1635 if (old_dev
!= inode
->i_rdev
)
1638 if (unit
[drive
].type
->code
== FD_NODRIVE
)
1641 if (filp
&& filp
->f_mode
& 3) {
1642 check_disk_change(inode
->i_rdev
);
1643 if (filp
->f_mode
& 2 ) {
1648 wrprot
= !(ciaa
.pra
& DSKPROT
);
1649 fd_deselect (drive
);
1660 fd_device
[drive
] = inode
->i_rdev
;
1662 if (unit
[drive
].motor
== 0)
1665 restore_flags(flags
);
1667 if (old_dev
&& old_dev
!= inode
->i_rdev
)
1668 invalidate_buffers(old_dev
);
1670 system
=(inode
->i_rdev
& 4)>>2;
1671 unit
[drive
].dtype
=&data_types
[system
];
1672 unit
[drive
].blocks
=unit
[drive
].type
->heads
*unit
[drive
].type
->tracks
*
1673 data_types
[system
].sects
*unit
[drive
].type
->sect_mult
;
1674 floppy_sizes
[MINOR(inode
->i_rdev
)] = unit
[drive
].blocks
>> 1;
1676 printk(KERN_INFO
"fd%d: accessing %s-disk with %s-layout\n",drive
,
1677 unit
[drive
].type
->name
, data_types
[system
].name
);
1682 static int floppy_release(struct inode
* inode
, struct file
* filp
)
1685 struct super_block
* sb
;
1687 int drive
= MINOR(inode
->i_rdev
) & 3;
1689 fsync_dev(inode
->i_rdev
);
1692 /* This is now handled in floppy_change, but still useful for debugging */
1693 sb
= get_super(inode
->i_rdev
);
1695 invalidate_inodes(sb
);
1696 invalidate_buffers(inode
->i_rdev
);
1699 if (unit
[drive
].dirty
== 1) {
1700 del_timer (flush_track_timer
+ drive
);
1701 non_int_flush_track (drive
);
1704 if (!fd_ref
[drive
]--) {
1705 printk(KERN_CRIT
"floppy_release with fd_ref == 0");
1709 /* the mod_use counter is handled this way */
1710 floppy_off (drive
| 0x40000000);
1716 * floppy-change is never called from an interrupt, so we can relax a bit
1717 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1718 * to the desired drive, but it will probably not survive the sleep if
1719 * several floppies are used at the same time: thus the loop.
1721 static int amiga_floppy_change(kdev_t dev
)
1723 int drive
= MINOR(dev
) & 3;
1725 static int first_time
= 1;
1727 if (MAJOR(dev
) != MAJOR_NR
) {
1728 printk(KERN_CRIT
"floppy_change: not a floppy\n");
1733 changed
= first_time
--;
1737 changed
= !(ciaa
.pra
& DSKCHANGE
);
1738 fd_deselect (drive
);
1744 unit
[drive
].track
= -1;
1745 unit
[drive
].dirty
= 0;
1746 writepending
= 0; /* if this was true before, too bad! */
1753 static struct file_operations floppy_fops
= {
1754 NULL
, /* lseek - default */
1755 block_read
, /* read - general block-dev read */
1756 block_write
, /* write - general block-dev write */
1757 NULL
, /* readdir - bad */
1759 fd_ioctl
, /* ioctl */
1761 floppy_open
, /* open */
1763 floppy_release
, /* release */
1764 block_fsync
, /* fsync */
1766 amiga_floppy_change
, /* check_media_change */
1767 NULL
, /* revalidate */
1770 void __init
amiga_floppy_setup (char *str
, int *ints
)
1772 printk (KERN_INFO
"amiflop: Setting default df0 to %x\n", ints
[1]);
1773 fd_def_df0
= ints
[1];
1776 static int __init
fd_probe_drives(void)
1778 int drive
,drives
,nomem
;
1780 printk(KERN_INFO
"FD: probing units\n" KERN_INFO
"found ");
1783 for(drive
=0;drive
<FD_MAX_UNITS
;drive
++) {
1785 if (unit
[drive
].type
->code
!= FD_NODRIVE
) {
1787 if ((unit
[drive
].trackbuf
= kmalloc(FLOPPY_MAX_SECTORS
* 512, GFP_KERNEL
)) == NULL
) {
1788 printk("no mem for ");
1789 unit
[drive
].type
= &drive_types
[num_dr_types
- 1]; /* FD_NODRIVE */
1793 printk("fd%d ",drive
);
1796 if ((drives
> 0) || (nomem
== 0)) {
1798 printk("no drives");
1806 int __init
amiga_floppy_init(void)
1810 if (!AMIGAHW_PRESENT(AMI_FLOPPY
))
1813 if (register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
1814 printk("fd: Unable to get major %d for floppy\n",MAJOR_NR
);
1818 if ((raw_buf
= (char *)amiga_chip_alloc (RAW_BUF_SIZE
)) == NULL
) {
1819 printk("fd: cannot get chip mem buffer\n");
1820 unregister_blkdev(MAJOR_NR
,"fd");
1824 if (!request_irq(IRQ_AMIGA_DSKBLK
, fd_block_done
, 0, "floppy_dma", NULL
)) {
1825 printk("fd: cannot get irq for dma\n");
1826 amiga_chip_free(raw_buf
);
1827 unregister_blkdev(MAJOR_NR
,"fd");
1830 if (!request_irq(IRQ_AMIGA_CIAA_TB
, ms_isr
, 0, "floppy_timer", NULL
)) {
1831 printk("fd: cannot get irq for timer\n");
1832 free_irq(IRQ_AMIGA_DSKBLK
, NULL
);
1833 amiga_chip_free(raw_buf
);
1834 unregister_blkdev(MAJOR_NR
,"fd");
1837 if (fd_probe_drives() < 1) { /* No usable drives */
1838 free_irq(IRQ_AMIGA_CIAA_TB
, NULL
);
1839 free_irq(IRQ_AMIGA_DSKBLK
, NULL
);
1840 amiga_chip_free(raw_buf
);
1841 unregister_blkdev(MAJOR_NR
,"fd");
1845 /* initialize variables */
1846 motor_on_timer
.next
= NULL
;
1847 motor_on_timer
.prev
= NULL
;
1848 motor_on_timer
.expires
= 0;
1849 motor_on_timer
.data
= 0;
1850 motor_on_timer
.function
= motor_on_callback
;
1851 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1852 motor_off_timer
[i
].next
= NULL
;
1853 motor_off_timer
[i
].prev
= NULL
;
1854 motor_off_timer
[i
].expires
= 0;
1855 motor_off_timer
[i
].data
= i
|0x80000000;
1856 motor_off_timer
[i
].function
= fd_motor_off
;
1857 flush_track_timer
[i
].next
= NULL
;
1858 flush_track_timer
[i
].prev
= NULL
;
1859 flush_track_timer
[i
].expires
= 0;
1860 flush_track_timer
[i
].data
= i
;
1861 flush_track_timer
[i
].function
= flush_track_callback
;
1866 post_write_timer
.next
= NULL
;
1867 post_write_timer
.prev
= NULL
;
1868 post_write_timer
.expires
= 0;
1869 post_write_timer
.data
= 0;
1870 post_write_timer
.function
= post_write
;
1872 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
1873 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
1874 blk_size
[MAJOR_NR
] = floppy_sizes
;
1876 for (i
= 0; i
< 128; i
++)
1878 for (i
= 0; i
< 16; i
++)
1879 mfmdecode
[mfmencode
[i
]]=i
;
1881 /* make sure that disk DMA is enabled */
1882 custom
.dmacon
= DMAF_SETCLR
| DMAF_DISK
;
1885 ciaa
.crb
= 8; /* one-shot, stop */
1887 (void)do_floppy
; /* avoid warning about unused variable */
1892 #include <linux/version.h>
1894 int init_module(void)
1898 return amiga_floppy_init();
1901 void cleanup_module(void)
1905 for( i
= 0; i
< FD_MAX_UNITS
; i
++)
1906 if (unit
[i
].type
->code
!= FD_NODRIVE
)
1907 kfree(unit
[i
].trackbuf
);
1908 free_irq(IRQ_AMIGA_CIAA_TB
, NULL
);
1909 free_irq(IRQ_AMIGA_DSKBLK
, NULL
);
1910 custom
.dmacon
= DMAF_DISK
; /* disable DMA */
1911 amiga_chip_free(raw_buf
);
1912 blk_size
[MAJOR_NR
] = NULL
;
1913 blksize_size
[MAJOR_NR
] = NULL
;
1914 blk_dev
[MAJOR_NR
].request_fn
= NULL
;
1915 unregister_blkdev(MAJOR_NR
, "fd");