Import 2.3.1pre2
[davej-history.git] / drivers / block / amiflop.c
blob27ad0ec758da1c1796d6d1eb6193f12cc64755b3
1 /*
2 * linux/amiga/amiflop.c
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)
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
19 * disks the same time
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
31 * trackbuffer
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
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.
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 ;-)
56 #include <linux/module.h>
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>
74 #include <asm/setup.h>
75 #include <asm/uaccess.h>
76 #include <asm/amigahw.h>
77 #include <asm/amigaints.h>
78 #include <asm/irq.h>
80 #define MAJOR_NR FLOPPY_MAJOR
81 #include <linux/blk.h>
83 #undef DEBUG /* print _LOTS_ of infos */
85 #define RAW_IOCTL
86 #ifdef RAW_IOCTL
87 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
88 #endif
91 * Defines
95 * Error codes
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) */
104 #define MFM_NOSYNC 1
105 #define MFM_HEADER 2
106 #define MFM_DATA 3
107 #define MFM_TRACK 4
110 * Floppy ID values
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");
122 * Macros
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
185 * request.
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)
234 ms_busy = -1;
235 wake_up(&ms_wait);
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)
242 unsigned long flags;
243 int ticks;
244 if (ms > 0) {
245 save_flags(flags);
246 cli();
247 while (ms_busy == 0)
248 sleep_on(&ms_wait);
249 ms_busy = 0;
250 restore_flags(flags);
251 ticks = MS_TICKS*ms-1;
252 ciaa.tblo=ticks%256;
253 ciaa.tbhi=ticks/256;
254 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
255 sleep_on(&ms_wait);
259 /* Hardware semaphore */
261 /* returns true when we would get the semaphore */
262 static inline int try_fdc(int drive)
264 drive &= 3;
265 return ((fdc_busy < 0) || (fdc_busy == drive));
268 static void get_fdc(int drive)
270 unsigned long flags;
272 drive &= 3;
273 #ifdef DEBUG
274 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
275 #endif
276 save_flags(flags);
277 cli();
278 while (!try_fdc(drive))
279 sleep_on(&fdc_wait);
280 fdc_busy = drive;
281 fdc_nested++;
282 restore_flags(flags);
285 static inline void rel_fdc(void)
287 #ifdef DEBUG
288 if (fdc_nested == 0)
289 printk("fd: unmatched rel_fdc\n");
290 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
291 #endif
292 fdc_nested--;
293 if (fdc_nested == 0) {
294 fdc_busy = -1;
295 wake_up(&fdc_wait);
299 static void fd_select (int drive)
301 unsigned char prb = ~0;
303 drive&=3;
304 #ifdef DEBUG
305 printk("selecting %d\n",drive);
306 #endif
307 if (drive == selected)
308 return;
309 get_fdc(drive);
310 selected = drive;
312 if (unit[drive].track % 2 != 0)
313 prb &= ~DSKSIDE;
314 if (unit[drive].motor == 1)
315 prb &= ~DSKMOTOR;
316 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
317 ciab.prb = prb;
318 prb &= ~SELMASK(drive);
319 ciab.prb = prb;
320 rel_fdc();
323 static void fd_deselect (int drive)
325 unsigned char prb;
326 unsigned long flags;
328 drive&=3;
329 #ifdef DEBUG
330 printk("deselecting %d\n",drive);
331 #endif
332 if (drive != selected) {
333 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
334 return;
337 get_fdc(drive);
338 save_flags (flags);
339 sti();
341 selected = -1;
343 prb = ciab.prb;
344 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
345 ciab.prb = prb;
347 restore_flags (flags);
348 rel_fdc();
352 static void motor_on_callback(unsigned long nr)
354 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
355 wake_up (&motor_wait);
356 } else {
357 motor_on_timer.expires = jiffies + HZ/10;
358 add_timer(&motor_on_timer);
362 static int fd_motor_on(int nr)
364 nr &= 3;
366 del_timer(motor_off_timer + nr);
368 if (!unit[nr].motor) {
369 unit[nr].motor = 1;
370 fd_select(nr);
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);
377 on_attempts = 10;
378 sleep_on (&motor_wait);
379 fd_deselect(nr);
382 if (on_attempts == 0) {
383 on_attempts = -1;
384 #if 0
385 printk (KERN_ERR "motor_on failed, turning motor off\n");
386 fd_motor_off (nr);
387 return 0;
388 #else
389 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
390 #endif
393 return 1;
396 static void fd_motor_off(unsigned long drive)
398 long calledfromint;
399 #ifdef MODULE
400 long decusecount;
402 decusecount = drive & 0x40000000;
403 #endif
404 calledfromint = drive & 0x80000000;
405 drive&=3;
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);
410 return;
412 unit[drive].motor = 0;
413 fd_select(drive);
414 udelay (1);
415 fd_deselect(drive);
417 #ifdef MODULE
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
421 the use count.
423 if (decusecount)
424 MOD_DEC_USE_COUNT;
425 #endif
428 static void floppy_off (unsigned int nr)
430 int drive;
432 drive = nr & 3;
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)
442 unsigned char prb;
443 int n;
445 drive &= 3;
446 get_fdc(drive);
447 if (!fd_motor_on (drive))
448 return 0;
449 fd_select (drive);
450 prb = ciab.prb;
451 prb |= DSKSIDE;
452 prb &= ~DSKDIREC;
453 ciab.prb = prb;
454 for (n = unit[drive].type->tracks/2; n != 0; --n) {
455 if (ciaa.pra & DSKTRACK0)
456 break;
457 prb &= ~DSKSTEP;
458 ciab.prb = prb;
459 prb |= DSKSTEP;
460 udelay (2);
461 ciab.prb = prb;
462 ms_delay(unit[drive].type->step_delay);
464 ms_delay (unit[drive].type->settle_time);
465 prb |= DSKDIREC;
466 n = unit[drive].type->tracks + 20;
467 for (;;) {
468 prb &= ~DSKSTEP;
469 ciab.prb = prb;
470 prb |= DSKSTEP;
471 udelay (2);
472 ciab.prb = prb;
473 ms_delay(unit[drive].type->step_delay + 1);
474 if ((ciaa.pra & DSKTRACK0) == 0)
475 break;
476 if (--n == 0) {
477 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
478 fd_motor_off (drive);
479 unit[drive].track = -1;
480 rel_fdc();
481 return 0;
484 unit[drive].track = 0;
485 ms_delay(unit[drive].type->settle_time);
487 rel_fdc();
488 fd_deselect(drive);
489 return 1;
492 static int fd_seek(int drive, int track)
494 unsigned char prb;
495 int cnt;
497 #ifdef DEBUG
498 printk("seeking drive %d to track %d\n",drive,track);
499 #endif
500 drive &= 3;
501 get_fdc(drive);
502 if (unit[drive].track == track) {
503 rel_fdc();
504 return 1;
506 if (!fd_motor_on(drive)) {
507 rel_fdc();
508 return 0;
510 if (unit[drive].track < 0 && !fd_calibrate(drive)) {
511 rel_fdc();
512 return 0;
515 fd_select (drive);
516 cnt = unit[drive].track/2 - track/2;
517 prb = ciab.prb;
518 prb |= DSKSIDE | DSKDIREC;
519 if (track % 2 != 0)
520 prb &= ~DSKSIDE;
521 if (cnt < 0) {
522 cnt = - cnt;
523 prb &= ~DSKDIREC;
525 ciab.prb = prb;
526 if (track % 2 != unit[drive].track % 2)
527 ms_delay (unit[drive].type->side_time);
528 unit[drive].track = track;
529 if (cnt == 0) {
530 rel_fdc();
531 fd_deselect(drive);
532 return 1;
534 do {
535 prb &= ~DSKSTEP;
536 ciab.prb = prb;
537 prb |= DSKSTEP;
538 udelay (1);
539 ciab.prb = prb;
540 ms_delay (unit[drive].type->step_delay);
541 } while (--cnt != 0);
542 ms_delay (unit[drive].type->settle_time);
544 rel_fdc();
545 fd_deselect(drive);
546 return 1;
549 static unsigned long fd_get_drive_id(int drive)
551 int i;
552 ulong id = 0;
554 drive&=3;
555 get_fdc(drive);
556 /* set up for ID */
557 MOTOR_ON;
558 udelay(2);
559 SELECT(SELMASK(drive));
560 udelay(2);
561 DESELECT(SELMASK(drive));
562 udelay(2);
563 MOTOR_OFF;
564 udelay(2);
565 SELECT(SELMASK(drive));
566 udelay(2);
567 DESELECT(SELMASK(drive));
568 udelay(2);
570 /* loop and read disk ID */
571 for (i=0; i<32; i++) {
572 SELECT(SELMASK(drive));
573 udelay(2);
575 /* read and store value of DSKRDY */
576 id <<= 1;
577 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
579 DESELECT(SELMASK(drive));
582 rel_fdc();
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
588 * type.
590 if(drive == 0 && id == FD_NODRIVE)
592 id = fd_def_df0;
593 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
595 /* return the ID value */
596 return (id);
599 static void fd_block_done(int irq, void *dummy, struct pt_regs *fp)
601 if (block_flag)
602 custom.dsklen = 0x4000;
604 if (block_flag == 2) { /* writing */
605 writepending = 2;
606 post_write_timer.expires = jiffies + 1; /* at least 2 ms */
607 post_write_timer.data = selected;
608 add_timer(&post_write_timer);
610 else { /* reading */
611 block_flag = 0;
612 wake_up (&wait_fd_block);
616 static void raw_read(int drive)
618 drive&=3;
619 get_fdc(drive);
620 while (block_flag)
621 sleep_on(&wait_fd_block);
622 fd_select(drive);
623 /* setup adkcon bits correctly */
624 custom.adkcon = ADK_MSBSYNC;
625 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
627 custom.dsksync = MFM_SYNC;
629 custom.dsklen = 0;
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;
634 block_flag = 1;
636 while (block_flag)
637 sleep_on (&wait_fd_block);
639 custom.dsklen = 0;
640 fd_deselect(drive);
641 rel_fdc();
644 static int raw_write(int drive)
646 ushort adk;
648 drive&=3;
649 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
650 if ((ciaa.pra & DSKPROT) == 0) {
651 rel_fdc();
652 return 0;
654 while (block_flag)
655 sleep_on(&wait_fd_block);
656 fd_select(drive);
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)
662 adk |= ADK_PRECOMP1;
663 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
664 adk |= ADK_PRECOMP0;
665 custom.adkcon = adk;
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;
672 block_flag = 2;
673 return 1;
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)
682 #ifdef DEBUG
683 printk("post_write for drive %ld\n",drive);
684 #endif
685 drive &= 3;
686 custom.dsklen = 0;
687 block_flag = 0;
688 writepending = 0;
689 writefromint = 0;
690 unit[drive].dirty = 0;
691 wake_up(&wait_fd_block);
692 fd_deselect(drive);
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)
709 if (ptr < endp) {
710 while (*ptr == 0x4489 && ptr < endp)
711 ptr++;
712 return (ulong)ptr;
714 return 0;
717 static inline unsigned long checksum(unsigned long *addr, int len)
719 unsigned long csum = 0;
721 len /= sizeof(*addr);
722 while (len-- > 0)
723 csum ^= *addr++;
724 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
726 return csum;
729 static unsigned long decode (unsigned long *data, unsigned long *raw,
730 int len)
732 ulong *odd, *even;
734 /* convert length from bytes to longwords */
735 len >>= 2;
736 odd = raw;
737 even = odd + len;
739 /* prepare return pointer */
740 raw += len * 2;
742 do {
743 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
744 } while (--len != 0);
746 return (ulong)raw;
749 struct header {
750 unsigned char magic;
751 unsigned char track;
752 unsigned char sect;
753 unsigned char ord;
754 unsigned char labels[16];
755 unsigned long hdrchk;
756 unsigned long datachk;
759 static int amiga_read(int drive)
761 unsigned long raw;
762 unsigned long end;
763 int scnt;
764 unsigned long csum;
765 struct header hdr;
767 drive&=3;
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);
774 return MFM_NOSYNC;
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);
784 #ifdef DEBUG
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);
790 #endif
792 if (hdr.hdrchk != csum) {
793 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
794 return MFM_HEADER;
797 /* verify track */
798 if (hdr.track != unit[drive].track) {
799 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
800 return MFM_TRACK;
803 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
804 (ulong *)raw, 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,
810 hdr.datachk, csum);
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]);
816 return MFM_DATA;
820 return 0;
823 static void encode(unsigned long data, unsigned long *dest)
825 unsigned long data2;
827 data &= 0x55555555;
828 data2 = data ^ 0x55555555;
829 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
831 if (*(dest - 1) & 0x00000001)
832 data &= 0x7FFFFFFF;
834 *dest = data;
837 static void encode_block(unsigned long *dest, unsigned long *src, int len)
839 int cnt, to_cnt = 0;
840 unsigned long data;
842 /* odd bits */
843 for (cnt = 0; cnt < len / 4; cnt++) {
844 data = src[cnt] >> 1;
845 encode(data, dest + to_cnt++);
848 /* even bits */
849 for (cnt = 0; cnt < len / 4; cnt++) {
850 data = src[cnt];
851 encode(data, dest + to_cnt++);
855 static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
857 struct header hdr;
858 int i;
860 disk&=3;
861 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
862 raw++;
863 *raw++ = 0x44894489;
865 hdr.magic = 0xFF;
866 hdr.track = unit[disk].track;
867 hdr.sect = cnt;
868 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
869 for (i = 0; i < 16; i++)
870 hdr.labels[i] = 0;
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);
876 raw += 2;
877 encode_block(raw, (ulong *)&hdr.labels, 16);
878 raw += 8;
879 encode_block(raw, (ulong *)&hdr.hdrchk, 4);
880 raw += 2;
881 encode_block(raw, (ulong *)&hdr.datachk, 4);
882 raw += 2;
883 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
884 raw += 256;
886 return raw;
889 static void amiga_write(int disk)
891 unsigned int cnt;
892 unsigned long *ptr = (unsigned long *)raw_buf;
894 disk&=3;
895 /* gap space */
896 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
897 *ptr++ = 0xaaaaaaaa;
899 /* sectors */
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;
906 struct dos_header {
907 unsigned char track, /* 0-80 */
908 side, /* 0-1 */
909 sec, /* 0-...*/
910 len_desc;/* 2 */
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
934 ; 16 12 5
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:
938 ; data * $11021 =
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;
951 ; while (not done) {
952 ; c = *data++ + crchi;
953 ; crchi = (@ c) >> 8 + crclo;
954 ; crclo = @ c;
957 ; Remember, + is done with EOR, the @ operator is in two tables (high
958 ; and low byte separately), which is calculated as
960 ; $1021 * (c & $F0)
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 */
1014 register int i;
1015 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
1017 CRCT1=CRCTable1;
1018 CRCT2=CRCTable2;
1019 data=data_a3;
1020 crcl=data_d1;
1021 crch=data_d0;
1022 for (i=data_d3; i>=0; i--) {
1023 c = (*data++) ^ crch;
1024 crch = CRCT1[c] ^ crcl;
1025 crcl = CRCT2[c];
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)
1042 register ushort w2;
1043 register unsigned char byte;
1044 register unsigned char *dec = mfmdecode;
1046 w2=word;
1047 w2>>=8;
1048 w2&=127;
1049 byte = dec[w2];
1050 byte <<= 4;
1051 w2 = word & 127;
1052 byte |= dec[w2];
1053 return byte;
1056 static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
1058 int i;
1060 for (i = 0; i < len; i++)
1061 *data++=dos_decode_byte(*raw++);
1062 return ((ulong)raw);
1065 #ifdef DEBUG
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],((ulong *)ptr)[2],((ulong *)ptr)[3]);
1071 #endif
1073 static int dos_read(int drive)
1075 unsigned long end;
1076 unsigned long raw;
1077 int scnt;
1078 unsigned short crc,data_crc[2];
1079 struct dos_header hdr;
1081 drive&=3;
1082 raw = (long) raw_buf;
1083 end = raw + unit[drive].type->read_size;
1085 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1086 do { /* search for the right sync of each sec-hdr */
1087 if (!(raw = scan_sync (raw, end))) {
1088 printk(KERN_INFO "dos_read: no hdr sync on track %d, unit %d for sector %d\n",
1089 unit[drive].track,drive,scnt);
1090 return MFM_NOSYNC;
1092 #ifdef DEBUG
1093 dbg(raw);
1094 #endif
1095 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1096 raw+=2; /* skip over headermark */
1097 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1098 crc = dos_hdr_crc(&hdr);
1100 #ifdef DEBUG
1101 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1102 hdr.sec, hdr.len_desc, hdr.crc);
1103 #endif
1105 if (crc != hdr.crc) {
1106 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n", hdr.crc, crc);
1107 return MFM_HEADER;
1109 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1110 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n", hdr.track,
1111 unit[drive].track/unit[drive].type->heads);
1112 return MFM_TRACK;
1115 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1116 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n", hdr.side,
1117 unit[drive].track%unit[drive].type->heads);
1118 return MFM_TRACK;
1121 if (hdr.len_desc != 2) {
1122 printk(KERN_INFO "dos_read: unknown sector len descriptor %d\n", hdr.len_desc);
1123 return MFM_DATA;
1125 #ifdef DEBUG
1126 printk("hdr accepted\n");
1127 #endif
1128 if (!(raw = scan_sync (raw, end))) {
1129 printk(KERN_INFO "dos_read: no data sync on track %d, unit %d for sector%d, disk sector %d\n",
1130 unit[drive].track, drive, scnt, hdr.sec);
1131 return MFM_NOSYNC;
1133 #ifdef DEBUG
1134 dbg(raw);
1135 #endif
1137 if (*((ushort *)raw)!=0x5545) {
1138 printk(KERN_INFO "dos_read: no data mark after sync (%d,%d,%d,%d) sc=%d\n",
1139 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1140 return MFM_NOSYNC;
1143 raw+=2; /* skip data mark (included in checksum) */
1144 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1145 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1146 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1148 if (crc != data_crc[0]) {
1149 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) sc=%d, %x %x\n",
1150 hdr.track, hdr.side, hdr.sec, hdr.len_desc,
1151 scnt,data_crc[0], crc);
1152 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1153 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1154 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1155 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1156 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1157 return MFM_DATA;
1160 return 0;
1163 static inline ushort dos_encode_byte(unsigned char byte)
1165 register unsigned char *enc, b2, b1;
1166 register ushort word;
1168 enc=mfmencode;
1169 b1=byte;
1170 b2=b1>>4;
1171 b1&=15;
1172 word=enc[b2] <<8 | enc [b1];
1173 return (word|((word&(256|64)) ? 0: 128));
1176 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1178 int i;
1180 for (i = 0; i < len; i++) {
1181 *dest=dos_encode_byte(*src++);
1182 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1183 dest++;
1187 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1189 static struct dos_header hdr={0,0,0,2,0,
1190 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1191 int i;
1192 static ushort crc[2]={0,0x4e4e};
1194 drive&=3;
1195 /* id gap 1 */
1196 /* the MFM word before is always 9254 */
1197 for(i=0;i<6;i++)
1198 *raw++=0xaaaaaaaa;
1199 /* 3 sync + 1 headermark */
1200 *raw++=0x44894489;
1201 *raw++=0x44895554;
1203 /* fill in the variable parts of the header */
1204 hdr.track=unit[drive].track/unit[drive].type->heads;
1205 hdr.side=unit[drive].track%unit[drive].type->heads;
1206 hdr.sec=cnt+1;
1207 hdr.crc=dos_hdr_crc(&hdr);
1209 /* header (without "magic") and id gap 2*/
1210 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1211 raw+=14;
1213 /*id gap 3 */
1214 for(i=0;i<6;i++)
1215 *raw++=0xaaaaaaaa;
1217 /* 3 syncs and 1 datamark */
1218 *raw++=0x44894489;
1219 *raw++=0x44895545;
1221 /* data */
1222 dos_encode_block((ushort *)raw,(unsigned char *)unit[drive].trackbuf+cnt*512,512);
1223 raw+=256;
1225 /*data crc + jd's special gap (long words :-/) */
1226 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1227 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1228 raw+=2;
1230 /* data gap */
1231 for(i=0;i<38;i++)
1232 *raw++=0x92549254;
1234 return raw; /* wrote 652 MFM words */
1237 static void dos_write(int disk)
1239 int cnt;
1240 unsigned long raw = (unsigned long) raw_buf;
1241 unsigned long *ptr=(unsigned long *)raw;
1243 disk&=3;
1244 /* really gap4 + indexgap , but we write it first and round it up */
1245 for (cnt=0;cnt<425;cnt++)
1246 *ptr++=0x92549254;
1248 /* the following is just guessed */
1249 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1250 for(cnt=0;cnt<473;cnt++)
1251 *ptr++=0x92549254;
1253 /* now the index marks...*/
1254 for (cnt=0;cnt<20;cnt++)
1255 *ptr++=0x92549254;
1256 for (cnt=0;cnt<6;cnt++)
1257 *ptr++=0xaaaaaaaa;
1258 *ptr++=0x52245224;
1259 *ptr++=0x52245552;
1260 for (cnt=0;cnt<20;cnt++)
1261 *ptr++=0x92549254;
1263 /* sectors */
1264 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1265 ptr=ms_putsec(disk,ptr,cnt);
1267 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1271 * Here comes the high level stuff (i.e. the filesystem interface)
1272 * and helper functions.
1273 * Normally this should be the only part that has to be adapted to
1274 * different kernel versions.
1277 /* FIXME: this assumes the drive is still spinning -
1278 * which is only true if we complete writing a track within three seconds
1280 static void flush_track_callback(unsigned long nr)
1282 nr&=3;
1283 writefromint = 1;
1284 if (!try_fdc(nr)) {
1285 /* we might block in an interrupt, so try again later */
1286 flush_track_timer[nr].expires = jiffies + 1;
1287 add_timer(flush_track_timer + nr);
1288 return;
1290 get_fdc(nr);
1291 (*unit[nr].dtype->write_fkt)(nr);
1292 if (!raw_write(nr)) {
1293 printk (KERN_NOTICE "floppy disk write protected\n");
1294 writefromint = 0;
1295 writepending = 0;
1297 rel_fdc();
1300 static int non_int_flush_track (unsigned long nr)
1302 unsigned long flags;
1304 nr&=3;
1305 writefromint = 0;
1306 del_timer(&post_write_timer);
1307 get_fdc(nr);
1308 if (!fd_motor_on(nr)) {
1309 writepending = 0;
1310 rel_fdc();
1311 return 0;
1313 save_flags(flags);
1314 cli();
1315 if (writepending != 2) {
1316 restore_flags(flags);
1317 (*unit[nr].dtype->write_fkt)(nr);
1318 if (!raw_write(nr)) {
1319 printk (KERN_NOTICE "floppy disk write protected in write!\n");
1320 writepending = 0;
1321 return 0;
1323 while (block_flag == 2)
1324 sleep_on (&wait_fd_block);
1326 else {
1327 restore_flags(flags);
1328 ms_delay(2); /* 2 ms post_write delay */
1329 post_write(nr);
1331 rel_fdc();
1332 return 1;
1335 static int get_track(int drive, int track)
1337 int error, errcnt;
1339 drive&=3;
1340 if (unit[drive].track == track)
1341 return 0;
1342 get_fdc(drive);
1343 if (!fd_motor_on(drive)) {
1344 rel_fdc();
1345 return -1;
1348 if (unit[drive].dirty == 1) {
1349 del_timer (flush_track_timer + drive);
1350 non_int_flush_track (drive);
1352 errcnt = 0;
1353 while (errcnt < MAX_ERRORS) {
1354 if (!fd_seek(drive, track))
1355 return -1;
1356 raw_read(drive);
1357 error = (*unit[drive].dtype->read_fkt)(drive);
1358 if (error == 0) {
1359 rel_fdc();
1360 return 0;
1362 /* Read Error Handling: recalibrate and try again */
1363 unit[drive].track = -1;
1364 errcnt++;
1366 rel_fdc();
1367 return -1;
1370 static void redo_fd_request(void)
1372 unsigned int cnt, block, track, sector;
1373 int device, drive;
1374 struct amiga_floppy_struct *floppy;
1375 char *data;
1376 unsigned long flags;
1378 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
1379 return;
1382 repeat:
1383 if (!CURRENT) {
1384 /* Nothing left to do */
1385 return;
1388 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1389 panic(DEVICE_NAME ": request list destroyed");
1391 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1392 panic(DEVICE_NAME ": block not locked");
1394 device = MINOR(CURRENT_DEVICE);
1395 if (device < 8) {
1396 /* manual selection */
1397 drive = device & 3;
1398 floppy = unit + drive;
1399 } else {
1400 /* Auto-detection */
1401 #ifdef DEBUG
1402 printk("redo_fd_request: can't handle auto detect\n");
1403 printk("redo_fd_request: default to normal\n");
1404 #endif
1405 drive = device & 3;
1406 floppy = unit + drive;
1409 /* Here someone could investigate to be more efficient */
1410 for (cnt = 0; cnt < CURRENT->current_nr_sectors; cnt++) {
1411 #ifdef DEBUG
1412 printk("fd: sector %ld + %d requested for %s\n",CURRENT->sector,cnt,
1413 (CURRENT->cmd==READ)?"read":"write");
1414 #endif
1415 block = CURRENT->sector + cnt;
1416 if ((int)block > floppy->blocks) {
1417 end_request(0);
1418 goto repeat;
1421 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1422 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1423 data = CURRENT->buffer + 512 * cnt;
1424 #ifdef DEBUG
1425 printk("access to track %d, sector %d, with buffer at 0x%08lx\n",
1426 track, sector, data);
1427 #endif
1429 if ((CURRENT->cmd != READ) && (CURRENT->cmd != WRITE)) {
1430 printk(KERN_WARNING "do_fd_request: unknown command\n");
1431 end_request(0);
1432 goto repeat;
1434 if (get_track(drive, track) == -1) {
1435 end_request(0);
1436 goto repeat;
1439 switch (CURRENT->cmd) {
1440 case READ:
1441 memcpy(data, unit[drive].trackbuf + sector * 512, 512);
1442 break;
1444 case WRITE:
1445 memcpy(unit[drive].trackbuf + sector * 512, data, 512);
1447 /* keep the drive spinning while writes are scheduled */
1448 if (!fd_motor_on(drive)) {
1449 end_request(0);
1450 goto repeat;
1453 * setup a callback to write the track buffer
1454 * after a short (1 tick) delay.
1456 save_flags (flags);
1457 cli();
1459 unit[drive].dirty = 1;
1460 /* reset the timer */
1461 del_timer (flush_track_timer + drive);
1463 flush_track_timer[drive].expires = jiffies + 1;
1464 add_timer (flush_track_timer + drive);
1465 restore_flags (flags);
1466 break;
1469 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1470 CURRENT->sector += CURRENT->current_nr_sectors;
1472 end_request(1);
1473 goto repeat;
1476 static void do_fd_request(void)
1478 redo_fd_request();
1481 static int fd_ioctl(struct inode *inode, struct file *filp,
1482 unsigned int cmd, unsigned long param)
1484 int drive = inode->i_rdev & 3;
1485 static struct floppy_struct getprm;
1486 struct super_block * sb;
1488 switch(cmd){
1489 case HDIO_GETGEO:
1491 struct hd_geometry loc;
1492 loc.heads = unit[drive].type->heads;
1493 loc.sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1494 loc.cylinders = unit[drive].type->tracks;
1495 loc.start = 0;
1496 if (copy_to_user((void *)param, (void *)&loc,
1497 sizeof(struct hd_geometry)))
1498 return -EFAULT;
1499 break;
1501 case FDFMTBEG:
1502 get_fdc(drive);
1503 if (fd_ref[drive] > 1) {
1504 rel_fdc();
1505 return -EBUSY;
1507 fsync_dev(inode->i_rdev);
1508 if (fd_motor_on(drive) == 0) {
1509 rel_fdc();
1510 return -ENODEV;
1512 if (fd_calibrate(drive) == 0) {
1513 rel_fdc();
1514 return -ENXIO;
1516 floppy_off(drive);
1517 rel_fdc();
1518 break;
1519 case FDFMTTRK:
1520 if (param < unit[drive].type->tracks * unit[drive].type->heads)
1522 get_fdc(drive);
1523 if (fd_seek(drive,param) != 0){
1524 memset(unit[drive].trackbuf, FD_FILL_BYTE,
1525 unit[drive].dtype->sects * unit[drive].type->sect_mult * 512);
1526 non_int_flush_track(drive);
1528 floppy_off(drive);
1529 rel_fdc();
1531 else
1532 return -EINVAL;
1533 break;
1534 case FDFMTEND:
1535 floppy_off(drive);
1536 sb = get_super(inode->i_rdev);
1537 if (sb)
1538 invalidate_inodes(sb);
1539 invalidate_buffers(inode->i_rdev);
1540 break;
1541 case FDGETPRM:
1542 memset((void *)&getprm, 0, sizeof (getprm));
1543 getprm.track=unit[drive].type->tracks;
1544 getprm.head=unit[drive].type->heads;
1545 getprm.sect=unit[drive].dtype->sects * unit[drive].type->sect_mult;
1546 getprm.size=unit[drive].blocks;
1547 if (copy_to_user((void *)param,
1548 (void *)&getprm,
1549 sizeof(struct floppy_struct)))
1550 return -EFAULT;
1551 break;
1552 case BLKGETSIZE:
1553 return put_user(unit[drive].blocks,(long *)param);
1554 break;
1555 case FDSETPRM:
1556 case FDDEFPRM:
1557 return -EINVAL;
1558 case FDFLUSH: /* unconditionally, even if not needed */
1559 del_timer (flush_track_timer + drive);
1560 non_int_flush_track(drive);
1561 break;
1562 #ifdef RAW_IOCTL
1563 case IOCTL_RAW_TRACK:
1564 if (copy_to_user((void *)param, raw_buf,
1565 unit[drive].type->read_size))
1566 return -EFAULT;
1567 else
1568 return unit[drive].type->read_size;
1569 #endif
1570 default:
1571 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",cmd,drive);
1572 return -ENOSYS;
1574 return 0;
1577 static void fd_probe(int dev)
1579 unsigned long code;
1580 int type;
1581 int drive;
1583 drive = dev & 3;
1584 code = fd_get_drive_id(drive);
1586 /* get drive type */
1587 for (type = 0; type < num_dr_types; type++)
1588 if (drive_types[type].code == code)
1589 break;
1591 if (type >= num_dr_types) {
1592 printk(KERN_WARNING "fd_probe: unsupported drive type %08lx found\n",
1593 code);
1594 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1595 return;
1598 unit[drive].type = drive_types + type;
1599 unit[drive].track = -1;
1601 unit[drive].disk = -1;
1602 unit[drive].motor = 0;
1603 unit[drive].busy = 0;
1604 unit[drive].status = -1;
1608 * floppy_open check for aliasing (/dev/fd0 can be the same as
1609 * /dev/PS0 etc), and disallows simultaneous access to the same
1610 * drive with different device numbers.
1612 static int floppy_open(struct inode *inode, struct file *filp)
1614 int drive;
1615 int old_dev;
1616 int system;
1617 unsigned long flags;
1619 drive = MINOR(inode->i_rdev) & 3;
1620 old_dev = fd_device[drive];
1622 if (fd_ref[drive])
1623 if (old_dev != inode->i_rdev)
1624 return -EBUSY;
1626 if (unit[drive].type->code == FD_NODRIVE)
1627 return -ENODEV;
1629 if (filp && filp->f_mode & 3) {
1630 check_disk_change(inode->i_rdev);
1631 if (filp->f_mode & 2 ) {
1632 int wrprot;
1634 get_fdc(drive);
1635 fd_select (drive);
1636 wrprot = !(ciaa.pra & DSKPROT);
1637 fd_deselect (drive);
1638 rel_fdc();
1640 if (wrprot)
1641 return -EROFS;
1645 save_flags(flags);
1646 cli();
1647 fd_ref[drive]++;
1648 fd_device[drive] = inode->i_rdev;
1649 #ifdef MODULE
1650 if (unit[drive].motor == 0)
1651 MOD_INC_USE_COUNT;
1652 #endif
1653 restore_flags(flags);
1655 if (old_dev && old_dev != inode->i_rdev)
1656 invalidate_buffers(old_dev);
1658 system=(inode->i_rdev & 4)>>2;
1659 unit[drive].dtype=&data_types[system];
1660 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1661 data_types[system].sects*unit[drive].type->sect_mult;
1662 floppy_sizes[MINOR(inode->i_rdev)] = unit[drive].blocks >> 1;
1664 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1665 unit[drive].type->name, data_types[system].name);
1667 return 0;
1670 static int floppy_release(struct inode * inode, struct file * filp)
1672 #ifdef DEBUG
1673 struct super_block * sb;
1674 #endif
1675 int drive = MINOR(inode->i_rdev) & 3;
1677 fsync_dev(inode->i_rdev);
1679 #ifdef DEBUG
1680 /* This is now handled in floppy_change, but still useful for debugging */
1681 sb = get_super(inode->i_rdev);
1682 if (sb)
1683 invalidate_inodes(sb);
1684 invalidate_buffers(inode->i_rdev);
1685 #endif
1687 if (unit[drive].dirty == 1) {
1688 del_timer (flush_track_timer + drive);
1689 non_int_flush_track (drive);
1692 if (!fd_ref[drive]--) {
1693 printk(KERN_CRIT "floppy_release with fd_ref == 0");
1694 fd_ref[drive] = 0;
1696 #ifdef MODULE
1697 /* the mod_use counter is handled this way */
1698 floppy_off (drive | 0x40000000);
1699 #endif
1700 return 0;
1704 * floppy-change is never called from an interrupt, so we can relax a bit
1705 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1706 * to the desired drive, but it will probably not survive the sleep if
1707 * several floppies are used at the same time: thus the loop.
1709 static int amiga_floppy_change(kdev_t dev)
1711 int drive = MINOR(dev) & 3;
1712 int changed;
1713 static int first_time = 1;
1715 if (MAJOR(dev) != MAJOR_NR) {
1716 printk(KERN_CRIT "floppy_change: not a floppy\n");
1717 return 0;
1720 if (first_time)
1721 changed = first_time--;
1722 else {
1723 get_fdc(drive);
1724 fd_select (drive);
1725 changed = !(ciaa.pra & DSKCHANGE);
1726 fd_deselect (drive);
1727 rel_fdc();
1730 if (changed) {
1731 fd_probe(drive);
1732 unit[drive].track = -1;
1733 unit[drive].dirty = 0;
1734 writepending = 0; /* if this was true before, too bad! */
1735 writefromint = 0;
1736 return 1;
1738 return 0;
1741 static struct file_operations floppy_fops = {
1742 NULL, /* lseek - default */
1743 block_read, /* read - general block-dev read */
1744 block_write, /* write - general block-dev write */
1745 NULL, /* readdir - bad */
1746 NULL, /* select */
1747 fd_ioctl, /* ioctl */
1748 NULL, /* mmap */
1749 floppy_open, /* open */
1750 NULL, /* flush */
1751 floppy_release, /* release */
1752 block_fsync, /* fsync */
1753 NULL, /* fasync */
1754 amiga_floppy_change, /* check_media_change */
1755 NULL, /* revalidate */
1758 __initfunc(void amiga_floppy_setup (char *str, int *ints))
1760 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", ints[1]);
1761 fd_def_df0 = ints[1];
1764 __initfunc(static int fd_probe_drives(void))
1766 int drive,drives,nomem;
1768 printk(KERN_INFO "FD: probing units\n" KERN_INFO "found ");
1769 drives=0;
1770 nomem=0;
1771 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1772 fd_probe(drive);
1773 if (unit[drive].type->code != FD_NODRIVE) {
1774 drives++;
1775 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
1776 printk("no mem for ");
1777 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
1778 drives--;
1779 nomem = 1;
1781 printk("fd%d ",drive);
1784 if ((drives > 0) || (nomem == 0)) {
1785 if (drives == 0)
1786 printk("no drives");
1787 printk("\n");
1788 return drives;
1790 printk("\n");
1791 return -ENOMEM;
1794 __initfunc(int amiga_floppy_init(void))
1796 int i;
1798 if (!AMIGAHW_PRESENT(AMI_FLOPPY))
1799 return -ENXIO;
1800 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
1801 printk("fd: Unable to get major %d for floppy\n",MAJOR_NR);
1802 return -EBUSY;
1804 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE)) == NULL) {
1805 printk("fd: cannot get chip mem buffer\n");
1806 unregister_blkdev(MAJOR_NR,"fd");
1807 return -ENOMEM;
1810 if (request_irq(IRQ_FLOPPY, fd_block_done, 0, "floppy_dma", NULL) != 0) {
1811 printk("fd: cannot get irq for dma\n");
1812 amiga_chip_free(raw_buf);
1813 unregister_blkdev(MAJOR_NR,"fd");
1814 return -EBUSY;
1816 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL) != 0) {
1817 printk("fd: cannot get irq for timer\n");
1818 free_irq(IRQ_FLOPPY, NULL);
1819 amiga_chip_free(raw_buf);
1820 unregister_blkdev(MAJOR_NR,"fd");
1821 return -EBUSY;
1823 if (fd_probe_drives() < 1) { /* No usable drives */
1824 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1825 free_irq(IRQ_FLOPPY, NULL);
1826 amiga_chip_free(raw_buf);
1827 unregister_blkdev(MAJOR_NR,"fd");
1828 return -ENXIO;
1831 /* initialize variables */
1832 motor_on_timer.next = NULL;
1833 motor_on_timer.prev = NULL;
1834 motor_on_timer.expires = 0;
1835 motor_on_timer.data = 0;
1836 motor_on_timer.function = motor_on_callback;
1837 for (i = 0; i < FD_MAX_UNITS; i++) {
1838 motor_off_timer[i].next = NULL;
1839 motor_off_timer[i].prev = NULL;
1840 motor_off_timer[i].expires = 0;
1841 motor_off_timer[i].data = i|0x80000000;
1842 motor_off_timer[i].function = fd_motor_off;
1843 flush_track_timer[i].next = NULL;
1844 flush_track_timer[i].prev = NULL;
1845 flush_track_timer[i].expires = 0;
1846 flush_track_timer[i].data = i;
1847 flush_track_timer[i].function = flush_track_callback;
1849 unit[i].track = -1;
1852 post_write_timer.next = NULL;
1853 post_write_timer.prev = NULL;
1854 post_write_timer.expires = 0;
1855 post_write_timer.data = 0;
1856 post_write_timer.function = post_write;
1858 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1859 blksize_size[MAJOR_NR] = floppy_blocksizes;
1860 blk_size[MAJOR_NR] = floppy_sizes;
1862 for (i = 0; i < 128; i++)
1863 mfmdecode[i]=255;
1864 for (i = 0; i < 16; i++)
1865 mfmdecode[mfmencode[i]]=i;
1867 /* make sure that disk DMA is enabled */
1868 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1870 /* init ms timer */
1871 ciaa.crb = 8; /* one-shot, stop */
1873 (void)do_floppy; /* avoid warning about unused variable */
1874 return 0;
1877 #ifdef MODULE
1878 #include <linux/version.h>
1880 int init_module(void)
1882 if (!MACH_IS_AMIGA)
1883 return -ENXIO;
1884 return amiga_floppy_init();
1887 void cleanup_module(void)
1889 int i;
1891 for( i = 0; i < FD_MAX_UNITS; i++)
1892 if (unit[i].type->code != FD_NODRIVE)
1893 kfree(unit[i].trackbuf);
1894 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1895 free_irq(IRQ_FLOPPY, NULL);
1896 custom.dmacon = DMAF_DISK; /* disable DMA */
1897 amiga_chip_free(raw_buf);
1898 blk_size[MAJOR_NR] = NULL;
1899 blksize_size[MAJOR_NR] = NULL;
1900 blk_dev[MAJOR_NR].request_fn = NULL;
1901 unregister_blkdev(MAJOR_NR, "fd");
1903 #endif