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>
57 #include <linux/slab.h>
60 #include <linux/hdreg.h>
61 #include <linux/delay.h>
62 #include <linux/init.h>
63 #include <linux/mutex.h>
64 #include <linux/amifdreg.h>
65 #include <linux/amifd.h>
67 #include <linux/blkdev.h>
68 #include <linux/elevator.h>
69 #include <linux/interrupt.h>
70 #include <linux/platform_device.h>
72 #include <asm/setup.h>
73 #include <linux/uaccess.h>
74 #include <asm/amigahw.h>
75 #include <asm/amigaints.h>
78 #undef DEBUG /* print _LOTS_ of infos */
82 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
92 #define FD_OK 0 /* operation succeeded */
93 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
94 #define FD_NOUNIT 1 /* unit does not exist */
95 #define FD_UNITBUSY 2 /* unit already active */
96 #define FD_NOTACTIVE 3 /* unit is not active */
97 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
107 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
108 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
109 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
110 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
112 static DEFINE_MUTEX(amiflop_mutex
);
113 static unsigned long int fd_def_df0
= FD_DD_3
; /* default for df0 if it doesn't identify */
115 module_param(fd_def_df0
, ulong
, 0);
116 MODULE_LICENSE("GPL");
121 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
122 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
123 #define SELECT(mask) (ciab.prb &= ~mask)
124 #define DESELECT(mask) (ciab.prb |= mask)
125 #define SELMASK(drive) (1 << (3 + (drive & 3)))
127 static struct fd_drive_type drive_types
[] = {
128 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
129 /* warning: times are now in milliseconds (ms) */
130 { FD_DD_3
, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
131 { FD_HD_3
, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
132 { FD_DD_5
, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
133 { FD_NODRIVE
, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
135 static int num_dr_types
= ARRAY_SIZE(drive_types
);
137 static int amiga_read(int), dos_read(int);
138 static void amiga_write(int), dos_write(int);
139 static struct fd_data_type data_types
[] = {
140 { "Amiga", 11 , amiga_read
, amiga_write
},
141 { "MS-Dos", 9, dos_read
, dos_write
}
144 /* current info on each unit */
145 static struct amiga_floppy_struct unit
[FD_MAX_UNITS
];
147 static struct timer_list flush_track_timer
[FD_MAX_UNITS
];
148 static struct timer_list post_write_timer
;
149 static unsigned long post_write_timer_drive
;
150 static struct timer_list motor_on_timer
;
151 static struct timer_list motor_off_timer
[FD_MAX_UNITS
];
152 static int on_attempts
;
154 /* Synchronization of FDC access */
155 /* request loop (trackbuffer) */
156 static volatile int fdc_busy
= -1;
157 static volatile int fdc_nested
;
158 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait
);
160 static DECLARE_COMPLETION(motor_on_completion
);
162 static volatile int selected
= -1; /* currently selected drive */
164 static int writepending
;
165 static int writefromint
;
166 static char *raw_buf
;
167 static int fdc_queue
;
169 static DEFINE_SPINLOCK(amiflop_lock
);
171 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
174 * These are global variables, as that's the easiest way to give
175 * information to interrupts. They are the data used for the current
178 static volatile char block_flag
;
179 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block
);
181 /* MS-Dos MFM Coding tables (should go quick and easy) */
182 static unsigned char mfmencode
[16]={
183 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
184 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
186 static unsigned char mfmdecode
[128];
188 /* floppy internal millisecond timer stuff */
189 static DECLARE_COMPLETION(ms_wait_completion
);
190 #define MS_TICKS ((amiga_eclock+50)/1000)
193 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
194 * max X times - some types of errors increase the errorcount by 2 or
195 * even 3, so we might actually retry only X/2 times before giving up.
197 #define MAX_ERRORS 12
199 #define custom amiga_custom
201 /* Prevent "aliased" accesses. */
202 static int fd_ref
[4] = { 0,0,0,0 };
203 static int fd_device
[4] = { 0, 0, 0, 0 };
206 * Here come the actual hardware access and helper functions.
207 * They are not reentrant and single threaded because all drives
208 * share the same hardware and the same trackbuffer.
211 /* Milliseconds timer */
213 static irqreturn_t
ms_isr(int irq
, void *dummy
)
215 complete(&ms_wait_completion
);
219 /* all waits are queued up
220 A more generic routine would do a schedule a la timer.device */
221 static void ms_delay(int ms
)
224 static DEFINE_MUTEX(mutex
);
228 ticks
= MS_TICKS
*ms
-1;
231 ciaa
.crb
=0x19; /*count eclock, force load, one-shoot, start */
232 wait_for_completion(&ms_wait_completion
);
233 mutex_unlock(&mutex
);
237 /* Hardware semaphore */
239 /* returns true when we would get the semaphore */
240 static inline int try_fdc(int drive
)
243 return ((fdc_busy
< 0) || (fdc_busy
== drive
));
246 static void get_fdc(int drive
)
252 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive
,fdc_busy
,fdc_nested
);
254 local_irq_save(flags
);
255 wait_event(fdc_wait
, try_fdc(drive
));
258 local_irq_restore(flags
);
261 static inline void rel_fdc(void)
265 printk("fd: unmatched rel_fdc\n");
266 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy
,fdc_nested
);
269 if (fdc_nested
== 0) {
275 static void fd_select (int drive
)
277 unsigned char prb
= ~0;
281 printk("selecting %d\n",drive
);
283 if (drive
== selected
)
288 if (unit
[drive
].track
% 2 != 0)
290 if (unit
[drive
].motor
== 1)
292 ciab
.prb
|= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
294 prb
&= ~SELMASK(drive
);
299 static void fd_deselect (int drive
)
306 printk("deselecting %d\n",drive
);
308 if (drive
!= selected
) {
309 printk(KERN_WARNING
"Deselecting drive %d while %d was selected!\n",drive
,selected
);
314 local_irq_save(flags
);
319 prb
|= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
322 local_irq_restore (flags
);
327 static void motor_on_callback(struct timer_list
*unused
)
329 if (!(ciaa
.pra
& DSKRDY
) || --on_attempts
== 0) {
330 complete_all(&motor_on_completion
);
332 motor_on_timer
.expires
= jiffies
+ HZ
/10;
333 add_timer(&motor_on_timer
);
337 static int fd_motor_on(int nr
)
341 del_timer(motor_off_timer
+ nr
);
343 if (!unit
[nr
].motor
) {
347 reinit_completion(&motor_on_completion
);
348 mod_timer(&motor_on_timer
, jiffies
+ HZ
/2);
351 wait_for_completion(&motor_on_completion
);
355 if (on_attempts
== 0) {
358 printk (KERN_ERR
"motor_on failed, turning motor off\n");
359 fd_motor_off (motor_off_timer
+ nr
);
362 printk (KERN_WARNING
"DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
369 static void fd_motor_off(struct timer_list
*timer
)
371 unsigned long drive
= ((unsigned long)timer
-
372 (unsigned long)&motor_off_timer
[0]) /
373 sizeof(motor_off_timer
[0]);
376 if (!try_fdc(drive
)) {
377 /* We would be blocked in an interrupt, so try again later */
378 timer
->expires
= jiffies
+ 1;
382 unit
[drive
].motor
= 0;
388 static void floppy_off (unsigned int nr
)
393 mod_timer(motor_off_timer
+ drive
, jiffies
+ 3*HZ
);
396 static int fd_calibrate(int drive
)
403 if (!fd_motor_on (drive
))
410 for (n
= unit
[drive
].type
->tracks
/2; n
!= 0; --n
) {
411 if (ciaa
.pra
& DSKTRACK0
)
418 ms_delay(unit
[drive
].type
->step_delay
);
420 ms_delay (unit
[drive
].type
->settle_time
);
422 n
= unit
[drive
].type
->tracks
+ 20;
429 ms_delay(unit
[drive
].type
->step_delay
+ 1);
430 if ((ciaa
.pra
& DSKTRACK0
) == 0)
433 printk (KERN_ERR
"fd%d: calibrate failed, turning motor off\n", drive
);
434 fd_motor_off (motor_off_timer
+ drive
);
435 unit
[drive
].track
= -1;
440 unit
[drive
].track
= 0;
441 ms_delay(unit
[drive
].type
->settle_time
);
448 static int fd_seek(int drive
, int track
)
454 printk("seeking drive %d to track %d\n",drive
,track
);
458 if (unit
[drive
].track
== track
) {
462 if (!fd_motor_on(drive
)) {
466 if (unit
[drive
].track
< 0 && !fd_calibrate(drive
)) {
472 cnt
= unit
[drive
].track
/2 - track
/2;
474 prb
|= DSKSIDE
| DSKDIREC
;
482 if (track
% 2 != unit
[drive
].track
% 2)
483 ms_delay (unit
[drive
].type
->side_time
);
484 unit
[drive
].track
= track
;
496 ms_delay (unit
[drive
].type
->step_delay
);
497 } while (--cnt
!= 0);
498 ms_delay (unit
[drive
].type
->settle_time
);
505 static unsigned long fd_get_drive_id(int drive
)
515 SELECT(SELMASK(drive
));
517 DESELECT(SELMASK(drive
));
521 SELECT(SELMASK(drive
));
523 DESELECT(SELMASK(drive
));
526 /* loop and read disk ID */
527 for (i
=0; i
<32; i
++) {
528 SELECT(SELMASK(drive
));
531 /* read and store value of DSKRDY */
533 id
|= (ciaa
.pra
& DSKRDY
) ? 0 : 1; /* cia regs are low-active! */
535 DESELECT(SELMASK(drive
));
541 * RB: At least A500/A2000's df0: don't identify themselves.
542 * As every (real) Amiga has at least a 3.5" DD drive as df0:
543 * we default to that if df0: doesn't identify as a certain
546 if(drive
== 0 && id
== FD_NODRIVE
)
549 printk(KERN_NOTICE
"fd: drive 0 didn't identify, setting default %08lx\n", (ulong
)fd_def_df0
);
551 /* return the ID value */
555 static irqreturn_t
fd_block_done(int irq
, void *dummy
)
558 custom
.dsklen
= 0x4000;
560 if (block_flag
== 2) { /* writing */
562 post_write_timer
.expires
= jiffies
+ 1; /* at least 2 ms */
563 post_write_timer_drive
= selected
;
564 add_timer(&post_write_timer
);
568 wake_up (&wait_fd_block
);
573 static void raw_read(int drive
)
577 wait_event(wait_fd_block
, !block_flag
);
579 /* setup adkcon bits correctly */
580 custom
.adkcon
= ADK_MSBSYNC
;
581 custom
.adkcon
= ADK_SETCLR
|ADK_WORDSYNC
|ADK_FAST
;
583 custom
.dsksync
= MFM_SYNC
;
586 custom
.dskptr
= (u_char
*)ZTWO_PADDR((u_char
*)raw_buf
);
587 custom
.dsklen
= unit
[drive
].type
->read_size
/sizeof(short) | DSKLEN_DMAEN
;
588 custom
.dsklen
= unit
[drive
].type
->read_size
/sizeof(short) | DSKLEN_DMAEN
;
592 wait_event(wait_fd_block
, !block_flag
);
599 static int raw_write(int drive
)
604 get_fdc(drive
); /* corresponds to rel_fdc() in post_write() */
605 if ((ciaa
.pra
& DSKPROT
) == 0) {
609 wait_event(wait_fd_block
, !block_flag
);
611 /* clear adkcon bits */
612 custom
.adkcon
= ADK_PRECOMP1
|ADK_PRECOMP0
|ADK_WORDSYNC
|ADK_MSBSYNC
;
613 /* set appropriate adkcon bits */
614 adk
= ADK_SETCLR
|ADK_FAST
;
615 if ((ulong
)unit
[drive
].track
>= unit
[drive
].type
->precomp2
)
617 else if ((ulong
)unit
[drive
].track
>= unit
[drive
].type
->precomp1
)
621 custom
.dsklen
= DSKLEN_WRITE
;
622 custom
.dskptr
= (u_char
*)ZTWO_PADDR((u_char
*)raw_buf
);
623 custom
.dsklen
= unit
[drive
].type
->write_size
/sizeof(short) | DSKLEN_DMAEN
|DSKLEN_WRITE
;
624 custom
.dsklen
= unit
[drive
].type
->write_size
/sizeof(short) | DSKLEN_DMAEN
|DSKLEN_WRITE
;
631 * to be called at least 2ms after the write has finished but before any
632 * other access to the hardware.
634 static void post_write (unsigned long drive
)
637 printk("post_write for drive %ld\n",drive
);
644 unit
[drive
].dirty
= 0;
645 wake_up(&wait_fd_block
);
647 rel_fdc(); /* corresponds to get_fdc() in raw_write */
650 static void post_write_callback(struct timer_list
*timer
)
652 post_write(post_write_timer_drive
);
656 * The following functions are to convert the block contents into raw data
657 * written to disk and vice versa.
658 * (Add other formats here ;-))
661 static unsigned long scan_sync(unsigned long raw
, unsigned long end
)
663 ushort
*ptr
= (ushort
*)raw
, *endp
= (ushort
*)end
;
665 while (ptr
< endp
&& *ptr
++ != 0x4489)
668 while (*ptr
== 0x4489 && ptr
< endp
)
675 static inline unsigned long checksum(unsigned long *addr
, int len
)
677 unsigned long csum
= 0;
679 len
/= sizeof(*addr
);
682 csum
= ((csum
>>1) & 0x55555555) ^ (csum
& 0x55555555);
687 static unsigned long decode (unsigned long *data
, unsigned long *raw
,
692 /* convert length from bytes to longwords */
697 /* prepare return pointer */
701 *data
++ = ((*odd
++ & 0x55555555) << 1) | (*even
++ & 0x55555555);
702 } while (--len
!= 0);
712 unsigned char labels
[16];
713 unsigned long hdrchk
;
714 unsigned long datachk
;
717 static int amiga_read(int drive
)
726 raw
= (long) raw_buf
;
727 end
= raw
+ unit
[drive
].type
->read_size
;
729 for (scnt
= 0;scnt
< unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
; scnt
++) {
730 if (!(raw
= scan_sync(raw
, end
))) {
731 printk (KERN_INFO
"can't find sync for sector %d\n", scnt
);
735 raw
= decode ((ulong
*)&hdr
.magic
, (ulong
*)raw
, 4);
736 raw
= decode ((ulong
*)&hdr
.labels
, (ulong
*)raw
, 16);
737 raw
= decode ((ulong
*)&hdr
.hdrchk
, (ulong
*)raw
, 4);
738 raw
= decode ((ulong
*)&hdr
.datachk
, (ulong
*)raw
, 4);
739 csum
= checksum((ulong
*)&hdr
,
740 (char *)&hdr
.hdrchk
-(char *)&hdr
);
743 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
744 hdr
.magic
, hdr
.track
, hdr
.sect
, hdr
.ord
,
745 *(ulong
*)&hdr
.labels
[0], *(ulong
*)&hdr
.labels
[4],
746 *(ulong
*)&hdr
.labels
[8], *(ulong
*)&hdr
.labels
[12],
747 hdr
.hdrchk
, hdr
.datachk
);
750 if (hdr
.hdrchk
!= csum
) {
751 printk(KERN_INFO
"MFM_HEADER: %08lx,%08lx\n", hdr
.hdrchk
, csum
);
756 if (hdr
.track
!= unit
[drive
].track
) {
757 printk(KERN_INFO
"MFM_TRACK: %d, %d\n", hdr
.track
, unit
[drive
].track
);
761 raw
= decode ((ulong
*)(unit
[drive
].trackbuf
+ hdr
.sect
*512),
763 csum
= checksum((ulong
*)(unit
[drive
].trackbuf
+ hdr
.sect
*512), 512);
765 if (hdr
.datachk
!= csum
) {
766 printk(KERN_INFO
"MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
767 hdr
.magic
, hdr
.track
, hdr
.sect
, hdr
.ord
, scnt
,
769 printk (KERN_INFO
"data=(%lx,%lx,%lx,%lx)\n",
770 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[0],
771 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[1],
772 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[2],
773 ((ulong
*)(unit
[drive
].trackbuf
+hdr
.sect
*512))[3]);
781 static void encode(unsigned long data
, unsigned long *dest
)
786 data2
= data
^ 0x55555555;
787 data
|= ((data2
>> 1) | 0x80000000) & (data2
<< 1);
789 if (*(dest
- 1) & 0x00000001)
795 static void encode_block(unsigned long *dest
, unsigned long *src
, int len
)
801 for (cnt
= 0; cnt
< len
/ 4; cnt
++) {
802 data
= src
[cnt
] >> 1;
803 encode(data
, dest
+ to_cnt
++);
807 for (cnt
= 0; cnt
< len
/ 4; cnt
++) {
809 encode(data
, dest
+ to_cnt
++);
813 static unsigned long *putsec(int disk
, unsigned long *raw
, int cnt
)
819 *raw
= (raw
[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
824 hdr
.track
= unit
[disk
].track
;
826 hdr
.ord
= unit
[disk
].dtype
->sects
* unit
[disk
].type
->sect_mult
- cnt
;
827 for (i
= 0; i
< 16; i
++)
829 hdr
.hdrchk
= checksum((ulong
*)&hdr
,
830 (char *)&hdr
.hdrchk
-(char *)&hdr
);
831 hdr
.datachk
= checksum((ulong
*)(unit
[disk
].trackbuf
+cnt
*512), 512);
833 encode_block(raw
, (ulong
*)&hdr
.magic
, 4);
835 encode_block(raw
, (ulong
*)&hdr
.labels
, 16);
837 encode_block(raw
, (ulong
*)&hdr
.hdrchk
, 4);
839 encode_block(raw
, (ulong
*)&hdr
.datachk
, 4);
841 encode_block(raw
, (ulong
*)(unit
[disk
].trackbuf
+cnt
*512), 512);
847 static void amiga_write(int disk
)
850 unsigned long *ptr
= (unsigned long *)raw_buf
;
854 for (cnt
= 0; cnt
< 415 * unit
[disk
].type
->sect_mult
; cnt
++)
858 for (cnt
= 0; cnt
< unit
[disk
].dtype
->sects
* unit
[disk
].type
->sect_mult
; cnt
++)
859 ptr
= putsec (disk
, ptr
, cnt
);
860 *(ushort
*)ptr
= (ptr
[-1]&1) ? 0x2AA8 : 0xAAA8;
865 unsigned char track
, /* 0-80 */
869 unsigned short crc
; /* on 68000 we got an alignment problem,
870 but this compiler solves it by adding silently
871 adding a pad byte so data won't fit
872 and this took about 3h to discover.... */
873 unsigned char gap1
[22]; /* for longword-alignedness (0x4e) */
876 /* crc routines are borrowed from the messydos-handler */
878 /* excerpt from the messydos-device
879 ; The CRC is computed not only over the actual data, but including
880 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
881 ; As we don't read or encode these fields into our buffers, we have to
882 ; preload the registers containing the CRC with the values they would have
883 ; after stepping over these fields.
885 ; How CRCs "really" work:
887 ; First, you should regard a bitstring as a series of coefficients of
888 ; polynomials. We calculate with these polynomials in modulo-2
889 ; arithmetic, in which both add and subtract are done the same as
890 ; exclusive-or. Now, we modify our data (a very long polynomial) in
891 ; such a way that it becomes divisible by the CCITT-standard 16-bit
893 ; polynomial: x + x + x + 1, represented by $11021. The easiest
894 ; way to do this would be to multiply (using proper arithmetic) our
895 ; datablock with $11021. So we have:
897 ; data * ($10000 + $1021) =
898 ; data * $10000 + data * $1021
899 ; The left part of this is simple: Just add two 0 bytes. But then
900 ; the right part (data $1021) remains difficult and even could have
901 ; a carry into the left part. The solution is to use a modified
902 ; multiplication, which has a result that is not correct, but with
903 ; a difference of any multiple of $11021. We then only need to keep
904 ; the 16 least significant bits of the result.
906 ; The following algorithm does this for us:
908 ; unsigned char *data, c, crclo, crchi;
910 ; c = *data++ + crchi;
911 ; crchi = (@ c) >> 8 + crclo;
915 ; Remember, + is done with EOR, the @ operator is in two tables (high
916 ; and low byte separately), which is calculated as
919 ; xor $1021 * (c & $0F)
920 ; xor $1021 * (c >> 4) (* is regular multiplication)
923 ; Anyway, the end result is the same as the remainder of the division of
924 ; the data by $11021. I am afraid I need to study theory a bit more...
927 my only works was to code this from manx to C....
931 static ushort
dos_crc(void * data_a3
, int data_d0
, int data_d1
, int data_d3
)
933 static unsigned char CRCTable1
[] = {
934 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
935 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
936 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
937 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
938 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
939 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
940 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
941 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
942 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
943 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
944 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
945 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
946 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
947 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
948 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
949 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
952 static unsigned char CRCTable2
[] = {
953 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
954 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
955 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
956 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
957 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
958 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
959 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
960 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
961 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
962 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
963 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
964 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
965 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
966 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
967 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
968 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
971 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
973 register unsigned char *CRCT1
, *CRCT2
, *data
, c
, crch
, crcl
;
980 for (i
=data_d3
; i
>=0; i
--) {
981 c
= (*data
++) ^ crch
;
982 crch
= CRCT1
[c
] ^ crcl
;
985 return (crch
<<8)|crcl
;
988 static inline ushort
dos_hdr_crc (struct dos_header
*hdr
)
990 return dos_crc(&(hdr
->track
), 0xb2, 0x30, 3); /* precomputed magic */
993 static inline ushort
dos_data_crc(unsigned char *data
)
995 return dos_crc(data
, 0xe2, 0x95 ,511); /* precomputed magic */
998 static inline unsigned char dos_decode_byte(ushort word
)
1001 register unsigned char byte
;
1002 register unsigned char *dec
= mfmdecode
;
1014 static unsigned long dos_decode(unsigned char *data
, unsigned short *raw
, int len
)
1018 for (i
= 0; i
< len
; i
++)
1019 *data
++=dos_decode_byte(*raw
++);
1020 return ((ulong
)raw
);
1024 static void dbg(unsigned long ptr
)
1026 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr
,
1027 ((ulong
*)ptr
)[0], ((ulong
*)ptr
)[1],
1028 ((ulong
*)ptr
)[2], ((ulong
*)ptr
)[3]);
1032 static int dos_read(int drive
)
1037 unsigned short crc
,data_crc
[2];
1038 struct dos_header hdr
;
1041 raw
= (long) raw_buf
;
1042 end
= raw
+ unit
[drive
].type
->read_size
;
1044 for (scnt
=0; scnt
< unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
; scnt
++) {
1045 do { /* search for the right sync of each sec-hdr */
1046 if (!(raw
= scan_sync (raw
, end
))) {
1047 printk(KERN_INFO
"dos_read: no hdr sync on "
1048 "track %d, unit %d for sector %d\n",
1049 unit
[drive
].track
,drive
,scnt
);
1055 } while (*((ushort
*)raw
)!=0x5554); /* loop usually only once done */
1056 raw
+=2; /* skip over headermark */
1057 raw
= dos_decode((unsigned char *)&hdr
,(ushort
*) raw
,8);
1058 crc
= dos_hdr_crc(&hdr
);
1061 printk("(%3d,%d,%2d,%d) %x\n", hdr
.track
, hdr
.side
,
1062 hdr
.sec
, hdr
.len_desc
, hdr
.crc
);
1065 if (crc
!= hdr
.crc
) {
1066 printk(KERN_INFO
"dos_read: MFM_HEADER %04x,%04x\n",
1070 if (hdr
.track
!= unit
[drive
].track
/unit
[drive
].type
->heads
) {
1071 printk(KERN_INFO
"dos_read: MFM_TRACK %d, %d\n",
1073 unit
[drive
].track
/unit
[drive
].type
->heads
);
1077 if (hdr
.side
!= unit
[drive
].track
%unit
[drive
].type
->heads
) {
1078 printk(KERN_INFO
"dos_read: MFM_SIDE %d, %d\n",
1080 unit
[drive
].track
%unit
[drive
].type
->heads
);
1084 if (hdr
.len_desc
!= 2) {
1085 printk(KERN_INFO
"dos_read: unknown sector len "
1086 "descriptor %d\n", hdr
.len_desc
);
1090 printk("hdr accepted\n");
1092 if (!(raw
= scan_sync (raw
, end
))) {
1093 printk(KERN_INFO
"dos_read: no data sync on track "
1094 "%d, unit %d for sector%d, disk sector %d\n",
1095 unit
[drive
].track
, drive
, scnt
, hdr
.sec
);
1102 if (*((ushort
*)raw
)!=0x5545) {
1103 printk(KERN_INFO
"dos_read: no data mark after "
1104 "sync (%d,%d,%d,%d) sc=%d\n",
1105 hdr
.track
,hdr
.side
,hdr
.sec
,hdr
.len_desc
,scnt
);
1109 raw
+=2; /* skip data mark (included in checksum) */
1110 raw
= dos_decode((unsigned char *)(unit
[drive
].trackbuf
+ (hdr
.sec
- 1) * 512), (ushort
*) raw
, 512);
1111 raw
= dos_decode((unsigned char *)data_crc
,(ushort
*) raw
,4);
1112 crc
= dos_data_crc(unit
[drive
].trackbuf
+ (hdr
.sec
- 1) * 512);
1114 if (crc
!= data_crc
[0]) {
1115 printk(KERN_INFO
"dos_read: MFM_DATA (%d,%d,%d,%d) "
1116 "sc=%d, %x %x\n", hdr
.track
, hdr
.side
,
1117 hdr
.sec
, hdr
.len_desc
, scnt
,data_crc
[0], crc
);
1118 printk(KERN_INFO
"data=(%lx,%lx,%lx,%lx,...)\n",
1119 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[0],
1120 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[1],
1121 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[2],
1122 ((ulong
*)(unit
[drive
].trackbuf
+(hdr
.sec
-1)*512))[3]);
1129 static inline ushort
dos_encode_byte(unsigned char byte
)
1131 register unsigned char *enc
, b2
, b1
;
1132 register ushort word
;
1138 word
=enc
[b2
] <<8 | enc
[b1
];
1139 return (word
|((word
&(256|64)) ? 0: 128));
1142 static void dos_encode_block(ushort
*dest
, unsigned char *src
, int len
)
1146 for (i
= 0; i
< len
; i
++) {
1147 *dest
=dos_encode_byte(*src
++);
1148 *dest
|=((dest
[-1]&1)||(*dest
&0x4000))? 0: 0x8000;
1153 static unsigned long *ms_putsec(int drive
, unsigned long *raw
, int cnt
)
1155 static struct dos_header hdr
={0,0,0,2,0,
1156 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1158 static ushort crc
[2]={0,0x4e4e};
1162 /* the MFM word before is always 9254 */
1165 /* 3 sync + 1 headermark */
1169 /* fill in the variable parts of the header */
1170 hdr
.track
=unit
[drive
].track
/unit
[drive
].type
->heads
;
1171 hdr
.side
=unit
[drive
].track
%unit
[drive
].type
->heads
;
1173 hdr
.crc
=dos_hdr_crc(&hdr
);
1175 /* header (without "magic") and id gap 2*/
1176 dos_encode_block((ushort
*)raw
,(unsigned char *) &hdr
.track
,28);
1183 /* 3 syncs and 1 datamark */
1188 dos_encode_block((ushort
*)raw
,
1189 (unsigned char *)unit
[drive
].trackbuf
+cnt
*512,512);
1192 /*data crc + jd's special gap (long words :-/) */
1193 crc
[0]=dos_data_crc(unit
[drive
].trackbuf
+cnt
*512);
1194 dos_encode_block((ushort
*) raw
,(unsigned char *)crc
,4);
1201 return raw
; /* wrote 652 MFM words */
1204 static void dos_write(int disk
)
1207 unsigned long raw
= (unsigned long) raw_buf
;
1208 unsigned long *ptr
=(unsigned long *)raw
;
1211 /* really gap4 + indexgap , but we write it first and round it up */
1212 for (cnt
=0;cnt
<425;cnt
++)
1215 /* the following is just guessed */
1216 if (unit
[disk
].type
->sect_mult
==2) /* check for HD-Disks */
1217 for(cnt
=0;cnt
<473;cnt
++)
1220 /* now the index marks...*/
1221 for (cnt
=0;cnt
<20;cnt
++)
1223 for (cnt
=0;cnt
<6;cnt
++)
1227 for (cnt
=0;cnt
<20;cnt
++)
1231 for(cnt
= 0; cnt
< unit
[disk
].dtype
->sects
* unit
[disk
].type
->sect_mult
; cnt
++)
1232 ptr
=ms_putsec(disk
,ptr
,cnt
);
1234 *(ushort
*)ptr
= 0xaaa8; /* MFM word before is always 0x9254 */
1238 * Here comes the high level stuff (i.e. the filesystem interface)
1239 * and helper functions.
1240 * Normally this should be the only part that has to be adapted to
1241 * different kernel versions.
1244 /* FIXME: this assumes the drive is still spinning -
1245 * which is only true if we complete writing a track within three seconds
1247 static void flush_track_callback(struct timer_list
*timer
)
1249 unsigned long nr
= ((unsigned long)timer
-
1250 (unsigned long)&flush_track_timer
[0]) /
1251 sizeof(flush_track_timer
[0]);
1256 /* we might block in an interrupt, so try again later */
1257 flush_track_timer
[nr
].expires
= jiffies
+ 1;
1258 add_timer(flush_track_timer
+ nr
);
1262 (*unit
[nr
].dtype
->write_fkt
)(nr
);
1263 if (!raw_write(nr
)) {
1264 printk (KERN_NOTICE
"floppy disk write protected\n");
1271 static int non_int_flush_track (unsigned long nr
)
1273 unsigned long flags
;
1277 del_timer(&post_write_timer
);
1279 if (!fd_motor_on(nr
)) {
1284 local_irq_save(flags
);
1285 if (writepending
!= 2) {
1286 local_irq_restore(flags
);
1287 (*unit
[nr
].dtype
->write_fkt
)(nr
);
1288 if (!raw_write(nr
)) {
1289 printk (KERN_NOTICE
"floppy disk write protected "
1294 wait_event(wait_fd_block
, block_flag
!= 2);
1297 local_irq_restore(flags
);
1298 ms_delay(2); /* 2 ms post_write delay */
1305 static int get_track(int drive
, int track
)
1310 if (unit
[drive
].track
== track
)
1313 if (!fd_motor_on(drive
)) {
1318 if (unit
[drive
].dirty
== 1) {
1319 del_timer (flush_track_timer
+ drive
);
1320 non_int_flush_track (drive
);
1323 while (errcnt
< MAX_ERRORS
) {
1324 if (!fd_seek(drive
, track
))
1327 error
= (*unit
[drive
].dtype
->read_fkt
)(drive
);
1332 /* Read Error Handling: recalibrate and try again */
1333 unit
[drive
].track
= -1;
1341 * Round-robin between our available drives, doing one request from each
1343 static struct request
*set_next_request(void)
1345 struct request_queue
*q
;
1346 int cnt
= FD_MAX_UNITS
;
1347 struct request
*rq
= NULL
;
1349 /* Find next queue we can dispatch from */
1350 fdc_queue
= fdc_queue
+ 1;
1351 if (fdc_queue
== FD_MAX_UNITS
)
1354 for(cnt
= FD_MAX_UNITS
; cnt
> 0; cnt
--) {
1356 if (unit
[fdc_queue
].type
->code
== FD_NODRIVE
) {
1357 if (++fdc_queue
== FD_MAX_UNITS
)
1362 q
= unit
[fdc_queue
].gendisk
->queue
;
1364 rq
= blk_fetch_request(q
);
1369 if (++fdc_queue
== FD_MAX_UNITS
)
1376 static void redo_fd_request(void)
1379 unsigned int cnt
, block
, track
, sector
;
1381 struct amiga_floppy_struct
*floppy
;
1383 unsigned long flags
;
1387 rq
= set_next_request();
1389 /* Nothing left to do */
1393 floppy
= rq
->rq_disk
->private_data
;
1394 drive
= floppy
- unit
;
1397 /* Here someone could investigate to be more efficient */
1398 for (cnt
= 0, err
= BLK_STS_OK
; cnt
< blk_rq_cur_sectors(rq
); cnt
++) {
1400 printk("fd: sector %ld + %d requested for %s\n",
1401 blk_rq_pos(rq
), cnt
,
1402 (rq_data_dir(rq
) == READ
) ? "read" : "write");
1404 block
= blk_rq_pos(rq
) + cnt
;
1405 if ((int)block
> floppy
->blocks
) {
1406 err
= BLK_STS_IOERR
;
1410 track
= block
/ (floppy
->dtype
->sects
* floppy
->type
->sect_mult
);
1411 sector
= block
% (floppy
->dtype
->sects
* floppy
->type
->sect_mult
);
1412 data
= bio_data(rq
->bio
) + 512 * cnt
;
1414 printk("access to track %d, sector %d, with buffer at "
1415 "0x%08lx\n", track
, sector
, data
);
1418 if (get_track(drive
, track
) == -1) {
1419 err
= BLK_STS_IOERR
;
1423 if (rq_data_dir(rq
) == READ
) {
1424 memcpy(data
, floppy
->trackbuf
+ sector
* 512, 512);
1426 memcpy(floppy
->trackbuf
+ sector
* 512, data
, 512);
1428 /* keep the drive spinning while writes are scheduled */
1429 if (!fd_motor_on(drive
)) {
1430 err
= BLK_STS_IOERR
;
1434 * setup a callback to write the track buffer
1435 * after a short (1 tick) delay.
1437 local_irq_save(flags
);
1440 /* reset the timer */
1441 mod_timer (flush_track_timer
+ drive
, jiffies
+ 1);
1442 local_irq_restore(flags
);
1446 if (__blk_end_request_cur(rq
, err
))
1451 static void do_fd_request(struct request_queue
* q
)
1456 static int fd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1458 int drive
= MINOR(bdev
->bd_dev
) & 3;
1460 geo
->heads
= unit
[drive
].type
->heads
;
1461 geo
->sectors
= unit
[drive
].dtype
->sects
* unit
[drive
].type
->sect_mult
;
1462 geo
->cylinders
= unit
[drive
].type
->tracks
;
1466 static int fd_locked_ioctl(struct block_device
*bdev
, fmode_t mode
,
1467 unsigned int cmd
, unsigned long param
)
1469 struct amiga_floppy_struct
*p
= bdev
->bd_disk
->private_data
;
1470 int drive
= p
- unit
;
1471 static struct floppy_struct getprm
;
1472 void __user
*argp
= (void __user
*)param
;
1477 if (fd_ref
[drive
] > 1) {
1482 if (fd_motor_on(drive
) == 0) {
1486 if (fd_calibrate(drive
) == 0) {
1494 if (param
< p
->type
->tracks
* p
->type
->heads
)
1497 if (fd_seek(drive
,param
) != 0){
1498 memset(p
->trackbuf
, FD_FILL_BYTE
,
1499 p
->dtype
->sects
* p
->type
->sect_mult
* 512);
1500 non_int_flush_track(drive
);
1510 invalidate_bdev(bdev
);
1513 memset((void *)&getprm
, 0, sizeof (getprm
));
1514 getprm
.track
=p
->type
->tracks
;
1515 getprm
.head
=p
->type
->heads
;
1516 getprm
.sect
=p
->dtype
->sects
* p
->type
->sect_mult
;
1517 getprm
.size
=p
->blocks
;
1518 if (copy_to_user(argp
, &getprm
, sizeof(struct floppy_struct
)))
1524 case FDFLUSH
: /* unconditionally, even if not needed */
1525 del_timer (flush_track_timer
+ drive
);
1526 non_int_flush_track(drive
);
1529 case IOCTL_RAW_TRACK
:
1530 if (copy_to_user(argp
, raw_buf
, p
->type
->read_size
))
1533 return p
->type
->read_size
;
1536 printk(KERN_DEBUG
"fd_ioctl: unknown cmd %d for drive %d.",
1543 static int fd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1544 unsigned int cmd
, unsigned long param
)
1548 mutex_lock(&amiflop_mutex
);
1549 ret
= fd_locked_ioctl(bdev
, mode
, cmd
, param
);
1550 mutex_unlock(&amiflop_mutex
);
1555 static void fd_probe(int dev
)
1562 code
= fd_get_drive_id(drive
);
1564 /* get drive type */
1565 for (type
= 0; type
< num_dr_types
; type
++)
1566 if (drive_types
[type
].code
== code
)
1569 if (type
>= num_dr_types
) {
1570 printk(KERN_WARNING
"fd_probe: unsupported drive type "
1571 "%08lx found\n", code
);
1572 unit
[drive
].type
= &drive_types
[num_dr_types
-1]; /* FD_NODRIVE */
1576 unit
[drive
].type
= drive_types
+ type
;
1577 unit
[drive
].track
= -1;
1579 unit
[drive
].disk
= -1;
1580 unit
[drive
].motor
= 0;
1581 unit
[drive
].busy
= 0;
1582 unit
[drive
].status
= -1;
1586 * floppy_open check for aliasing (/dev/fd0 can be the same as
1587 * /dev/PS0 etc), and disallows simultaneous access to the same
1588 * drive with different device numbers.
1590 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
1592 int drive
= MINOR(bdev
->bd_dev
) & 3;
1593 int system
= (MINOR(bdev
->bd_dev
) & 4) >> 2;
1595 unsigned long flags
;
1597 mutex_lock(&amiflop_mutex
);
1598 old_dev
= fd_device
[drive
];
1600 if (fd_ref
[drive
] && old_dev
!= system
) {
1601 mutex_unlock(&amiflop_mutex
);
1605 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1606 check_disk_change(bdev
);
1607 if (mode
& FMODE_WRITE
) {
1612 wrprot
= !(ciaa
.pra
& DSKPROT
);
1613 fd_deselect (drive
);
1617 mutex_unlock(&amiflop_mutex
);
1623 local_irq_save(flags
);
1625 fd_device
[drive
] = system
;
1626 local_irq_restore(flags
);
1628 unit
[drive
].dtype
=&data_types
[system
];
1629 unit
[drive
].blocks
=unit
[drive
].type
->heads
*unit
[drive
].type
->tracks
*
1630 data_types
[system
].sects
*unit
[drive
].type
->sect_mult
;
1631 set_capacity(unit
[drive
].gendisk
, unit
[drive
].blocks
);
1633 printk(KERN_INFO
"fd%d: accessing %s-disk with %s-layout\n",drive
,
1634 unit
[drive
].type
->name
, data_types
[system
].name
);
1636 mutex_unlock(&amiflop_mutex
);
1640 static void floppy_release(struct gendisk
*disk
, fmode_t mode
)
1642 struct amiga_floppy_struct
*p
= disk
->private_data
;
1643 int drive
= p
- unit
;
1645 mutex_lock(&amiflop_mutex
);
1646 if (unit
[drive
].dirty
== 1) {
1647 del_timer (flush_track_timer
+ drive
);
1648 non_int_flush_track (drive
);
1651 if (!fd_ref
[drive
]--) {
1652 printk(KERN_CRIT
"floppy_release with fd_ref == 0");
1658 mutex_unlock(&amiflop_mutex
);
1662 * check_events is never called from an interrupt, so we can relax a bit
1663 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1664 * to the desired drive, but it will probably not survive the sleep if
1665 * several floppies are used at the same time: thus the loop.
1667 static unsigned amiga_check_events(struct gendisk
*disk
, unsigned int clearing
)
1669 struct amiga_floppy_struct
*p
= disk
->private_data
;
1670 int drive
= p
- unit
;
1672 static int first_time
= 1;
1675 changed
= first_time
--;
1679 changed
= !(ciaa
.pra
& DSKCHANGE
);
1680 fd_deselect (drive
);
1688 writepending
= 0; /* if this was true before, too bad! */
1690 return DISK_EVENT_MEDIA_CHANGE
;
1695 static const struct block_device_operations floppy_fops
= {
1696 .owner
= THIS_MODULE
,
1697 .open
= floppy_open
,
1698 .release
= floppy_release
,
1700 .getgeo
= fd_getgeo
,
1701 .check_events
= amiga_check_events
,
1704 static struct gendisk
*fd_alloc_disk(int drive
)
1706 struct gendisk
*disk
;
1708 disk
= alloc_disk(1);
1712 disk
->queue
= blk_init_queue(do_fd_request
, &amiflop_lock
);
1713 if (IS_ERR(disk
->queue
)) {
1718 unit
[drive
].trackbuf
= kmalloc(FLOPPY_MAX_SECTORS
* 512, GFP_KERNEL
);
1719 if (!unit
[drive
].trackbuf
)
1720 goto out_cleanup_queue
;
1725 blk_cleanup_queue(disk
->queue
);
1730 unit
[drive
].type
->code
= FD_NODRIVE
;
1734 static int __init
fd_probe_drives(void)
1736 int drive
,drives
,nomem
;
1738 pr_info("FD: probing units\nfound");
1741 for(drive
=0;drive
<FD_MAX_UNITS
;drive
++) {
1742 struct gendisk
*disk
;
1744 if (unit
[drive
].type
->code
== FD_NODRIVE
)
1747 disk
= fd_alloc_disk(drive
);
1749 pr_cont(" no mem for fd%d", drive
);
1753 unit
[drive
].gendisk
= disk
;
1756 pr_cont(" fd%d",drive
);
1757 disk
->major
= FLOPPY_MAJOR
;
1758 disk
->first_minor
= drive
;
1759 disk
->fops
= &floppy_fops
;
1760 sprintf(disk
->disk_name
, "fd%d", drive
);
1761 disk
->private_data
= &unit
[drive
];
1762 set_capacity(disk
, 880*2);
1765 if ((drives
> 0) || (nomem
== 0)) {
1767 pr_cont(" no drives");
1775 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
1777 int drive
= *part
& 3;
1778 if (unit
[drive
].type
->code
== FD_NODRIVE
)
1781 return get_disk_and_module(unit
[drive
].gendisk
);
1784 static int __init
amiga_floppy_probe(struct platform_device
*pdev
)
1788 if (register_blkdev(FLOPPY_MAJOR
,"fd"))
1792 raw_buf
= amiga_chip_alloc(RAW_BUF_SIZE
, "Floppy");
1794 printk("fd: cannot get chip mem buffer\n");
1799 if (request_irq(IRQ_AMIGA_DSKBLK
, fd_block_done
, 0, "floppy_dma", NULL
)) {
1800 printk("fd: cannot get irq for dma\n");
1804 if (request_irq(IRQ_AMIGA_CIAA_TB
, ms_isr
, 0, "floppy_timer", NULL
)) {
1805 printk("fd: cannot get irq for timer\n");
1810 if (fd_probe_drives() < 1) /* No usable drives */
1813 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
1814 floppy_find
, NULL
, NULL
);
1816 /* initialize variables */
1817 timer_setup(&motor_on_timer
, motor_on_callback
, 0);
1818 motor_on_timer
.expires
= 0;
1819 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1820 timer_setup(&motor_off_timer
[i
], fd_motor_off
, 0);
1821 motor_off_timer
[i
].expires
= 0;
1822 timer_setup(&flush_track_timer
[i
], flush_track_callback
, 0);
1823 flush_track_timer
[i
].expires
= 0;
1828 timer_setup(&post_write_timer
, post_write_callback
, 0);
1829 post_write_timer
.expires
= 0;
1831 for (i
= 0; i
< 128; i
++)
1833 for (i
= 0; i
< 16; i
++)
1834 mfmdecode
[mfmencode
[i
]]=i
;
1836 /* make sure that disk DMA is enabled */
1837 custom
.dmacon
= DMAF_SETCLR
| DMAF_DISK
;
1840 ciaa
.crb
= 8; /* one-shot, stop */
1844 free_irq(IRQ_AMIGA_CIAA_TB
, NULL
);
1846 free_irq(IRQ_AMIGA_DSKBLK
, NULL
);
1848 amiga_chip_free(raw_buf
);
1850 unregister_blkdev(FLOPPY_MAJOR
,"fd");
1854 static struct platform_driver amiga_floppy_driver
= {
1856 .name
= "amiga-floppy",
1860 static int __init
amiga_floppy_init(void)
1862 return platform_driver_probe(&amiga_floppy_driver
, amiga_floppy_probe
);
1865 module_init(amiga_floppy_init
);
1868 static int __init
amiga_floppy_setup (char *str
)
1873 if (!get_option(&str
, &n
))
1875 printk (KERN_INFO
"amiflop: Setting default df0 to %x\n", n
);
1880 __setup("floppy=", amiga_floppy_setup
);
1883 MODULE_ALIAS("platform:amiga-floppy");