Import 2.3.25pre1
[davej-history.git] / drivers / block / amiflop.c
blobb27d120958a75e4c341638d2dad9888299e8952a
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],
1070 ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
1072 #endif
1074 static int dos_read(int drive)
1076 unsigned long end;
1077 unsigned long raw;
1078 int scnt;
1079 unsigned short crc,data_crc[2];
1080 struct dos_header hdr;
1082 drive&=3;
1083 raw = (long) raw_buf;
1084 end = raw + unit[drive].type->read_size;
1086 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1087 do { /* search for the right sync of each sec-hdr */
1088 if (!(raw = scan_sync (raw, end))) {
1089 printk(KERN_INFO "dos_read: no hdr sync on "
1090 "track %d, unit %d for sector %d\n",
1091 unit[drive].track,drive,scnt);
1092 return MFM_NOSYNC;
1094 #ifdef DEBUG
1095 dbg(raw);
1096 #endif
1097 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1098 raw+=2; /* skip over headermark */
1099 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1100 crc = dos_hdr_crc(&hdr);
1102 #ifdef DEBUG
1103 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1104 hdr.sec, hdr.len_desc, hdr.crc);
1105 #endif
1107 if (crc != hdr.crc) {
1108 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
1109 hdr.crc, crc);
1110 return MFM_HEADER;
1112 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1113 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
1114 hdr.track,
1115 unit[drive].track/unit[drive].type->heads);
1116 return MFM_TRACK;
1119 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1120 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
1121 hdr.side,
1122 unit[drive].track%unit[drive].type->heads);
1123 return MFM_TRACK;
1126 if (hdr.len_desc != 2) {
1127 printk(KERN_INFO "dos_read: unknown sector len "
1128 "descriptor %d\n", hdr.len_desc);
1129 return MFM_DATA;
1131 #ifdef DEBUG
1132 printk("hdr accepted\n");
1133 #endif
1134 if (!(raw = scan_sync (raw, end))) {
1135 printk(KERN_INFO "dos_read: no data sync on track "
1136 "%d, unit %d for sector%d, disk sector %d\n",
1137 unit[drive].track, drive, scnt, hdr.sec);
1138 return MFM_NOSYNC;
1140 #ifdef DEBUG
1141 dbg(raw);
1142 #endif
1144 if (*((ushort *)raw)!=0x5545) {
1145 printk(KERN_INFO "dos_read: no data mark after "
1146 "sync (%d,%d,%d,%d) sc=%d\n",
1147 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1148 return MFM_NOSYNC;
1151 raw+=2; /* skip data mark (included in checksum) */
1152 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1153 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1154 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1156 if (crc != data_crc[0]) {
1157 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
1158 "sc=%d, %x %x\n", hdr.track, hdr.side,
1159 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
1160 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1161 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1162 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1163 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1164 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1165 return MFM_DATA;
1168 return 0;
1171 static inline ushort dos_encode_byte(unsigned char byte)
1173 register unsigned char *enc, b2, b1;
1174 register ushort word;
1176 enc=mfmencode;
1177 b1=byte;
1178 b2=b1>>4;
1179 b1&=15;
1180 word=enc[b2] <<8 | enc [b1];
1181 return (word|((word&(256|64)) ? 0: 128));
1184 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1186 int i;
1188 for (i = 0; i < len; i++) {
1189 *dest=dos_encode_byte(*src++);
1190 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1191 dest++;
1195 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1197 static struct dos_header hdr={0,0,0,2,0,
1198 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1199 int i;
1200 static ushort crc[2]={0,0x4e4e};
1202 drive&=3;
1203 /* id gap 1 */
1204 /* the MFM word before is always 9254 */
1205 for(i=0;i<6;i++)
1206 *raw++=0xaaaaaaaa;
1207 /* 3 sync + 1 headermark */
1208 *raw++=0x44894489;
1209 *raw++=0x44895554;
1211 /* fill in the variable parts of the header */
1212 hdr.track=unit[drive].track/unit[drive].type->heads;
1213 hdr.side=unit[drive].track%unit[drive].type->heads;
1214 hdr.sec=cnt+1;
1215 hdr.crc=dos_hdr_crc(&hdr);
1217 /* header (without "magic") and id gap 2*/
1218 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1219 raw+=14;
1221 /*id gap 3 */
1222 for(i=0;i<6;i++)
1223 *raw++=0xaaaaaaaa;
1225 /* 3 syncs and 1 datamark */
1226 *raw++=0x44894489;
1227 *raw++=0x44895545;
1229 /* data */
1230 dos_encode_block((ushort *)raw,
1231 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
1232 raw+=256;
1234 /*data crc + jd's special gap (long words :-/) */
1235 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1236 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1237 raw+=2;
1239 /* data gap */
1240 for(i=0;i<38;i++)
1241 *raw++=0x92549254;
1243 return raw; /* wrote 652 MFM words */
1246 static void dos_write(int disk)
1248 int cnt;
1249 unsigned long raw = (unsigned long) raw_buf;
1250 unsigned long *ptr=(unsigned long *)raw;
1252 disk&=3;
1253 /* really gap4 + indexgap , but we write it first and round it up */
1254 for (cnt=0;cnt<425;cnt++)
1255 *ptr++=0x92549254;
1257 /* the following is just guessed */
1258 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1259 for(cnt=0;cnt<473;cnt++)
1260 *ptr++=0x92549254;
1262 /* now the index marks...*/
1263 for (cnt=0;cnt<20;cnt++)
1264 *ptr++=0x92549254;
1265 for (cnt=0;cnt<6;cnt++)
1266 *ptr++=0xaaaaaaaa;
1267 *ptr++=0x52245224;
1268 *ptr++=0x52245552;
1269 for (cnt=0;cnt<20;cnt++)
1270 *ptr++=0x92549254;
1272 /* sectors */
1273 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1274 ptr=ms_putsec(disk,ptr,cnt);
1276 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1280 * Here comes the high level stuff (i.e. the filesystem interface)
1281 * and helper functions.
1282 * Normally this should be the only part that has to be adapted to
1283 * different kernel versions.
1286 /* FIXME: this assumes the drive is still spinning -
1287 * which is only true if we complete writing a track within three seconds
1289 static void flush_track_callback(unsigned long nr)
1291 nr&=3;
1292 writefromint = 1;
1293 if (!try_fdc(nr)) {
1294 /* we might block in an interrupt, so try again later */
1295 flush_track_timer[nr].expires = jiffies + 1;
1296 add_timer(flush_track_timer + nr);
1297 return;
1299 get_fdc(nr);
1300 (*unit[nr].dtype->write_fkt)(nr);
1301 if (!raw_write(nr)) {
1302 printk (KERN_NOTICE "floppy disk write protected\n");
1303 writefromint = 0;
1304 writepending = 0;
1306 rel_fdc();
1309 static int non_int_flush_track (unsigned long nr)
1311 unsigned long flags;
1313 nr&=3;
1314 writefromint = 0;
1315 del_timer(&post_write_timer);
1316 get_fdc(nr);
1317 if (!fd_motor_on(nr)) {
1318 writepending = 0;
1319 rel_fdc();
1320 return 0;
1322 save_flags(flags);
1323 cli();
1324 if (writepending != 2) {
1325 restore_flags(flags);
1326 (*unit[nr].dtype->write_fkt)(nr);
1327 if (!raw_write(nr)) {
1328 printk (KERN_NOTICE "floppy disk write protected "
1329 "in write!\n");
1330 writepending = 0;
1331 return 0;
1333 while (block_flag == 2)
1334 sleep_on (&wait_fd_block);
1336 else {
1337 restore_flags(flags);
1338 ms_delay(2); /* 2 ms post_write delay */
1339 post_write(nr);
1341 rel_fdc();
1342 return 1;
1345 static int get_track(int drive, int track)
1347 int error, errcnt;
1349 drive&=3;
1350 if (unit[drive].track == track)
1351 return 0;
1352 get_fdc(drive);
1353 if (!fd_motor_on(drive)) {
1354 rel_fdc();
1355 return -1;
1358 if (unit[drive].dirty == 1) {
1359 del_timer (flush_track_timer + drive);
1360 non_int_flush_track (drive);
1362 errcnt = 0;
1363 while (errcnt < MAX_ERRORS) {
1364 if (!fd_seek(drive, track))
1365 return -1;
1366 raw_read(drive);
1367 error = (*unit[drive].dtype->read_fkt)(drive);
1368 if (error == 0) {
1369 rel_fdc();
1370 return 0;
1372 /* Read Error Handling: recalibrate and try again */
1373 unit[drive].track = -1;
1374 errcnt++;
1376 rel_fdc();
1377 return -1;
1380 static void redo_fd_request(void)
1382 unsigned int cnt, block, track, sector;
1383 int device, drive;
1384 struct amiga_floppy_struct *floppy;
1385 char *data;
1386 unsigned long flags;
1388 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
1389 return;
1392 repeat:
1393 if (!CURRENT) {
1394 /* Nothing left to do */
1395 return;
1398 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1399 panic(DEVICE_NAME ": request list destroyed");
1401 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1402 panic(DEVICE_NAME ": block not locked");
1404 device = MINOR(CURRENT_DEVICE);
1405 if (device < 8) {
1406 /* manual selection */
1407 drive = device & 3;
1408 floppy = unit + drive;
1409 } else {
1410 /* Auto-detection */
1411 #ifdef DEBUG
1412 printk("redo_fd_request: can't handle auto detect\n");
1413 printk("redo_fd_request: default to normal\n");
1414 #endif
1415 drive = device & 3;
1416 floppy = unit + drive;
1419 /* Here someone could investigate to be more efficient */
1420 for (cnt = 0; cnt < CURRENT->current_nr_sectors; cnt++) {
1421 #ifdef DEBUG
1422 printk("fd: sector %ld + %d requested for %s\n",
1423 CURRENT->sector,cnt,
1424 (CURRENT->cmd==READ)?"read":"write");
1425 #endif
1426 block = CURRENT->sector + cnt;
1427 if ((int)block > floppy->blocks) {
1428 end_request(0);
1429 goto repeat;
1432 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1433 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1434 data = CURRENT->buffer + 512 * cnt;
1435 #ifdef DEBUG
1436 printk("access to track %d, sector %d, with buffer at "
1437 "0x%08lx\n", track, sector, data);
1438 #endif
1440 if ((CURRENT->cmd != READ) && (CURRENT->cmd != WRITE)) {
1441 printk(KERN_WARNING "do_fd_request: unknown command\n");
1442 end_request(0);
1443 goto repeat;
1445 if (get_track(drive, track) == -1) {
1446 end_request(0);
1447 goto repeat;
1450 switch (CURRENT->cmd) {
1451 case READ:
1452 memcpy(data, unit[drive].trackbuf + sector * 512, 512);
1453 break;
1455 case WRITE:
1456 memcpy(unit[drive].trackbuf + sector * 512, data, 512);
1458 /* keep the drive spinning while writes are scheduled */
1459 if (!fd_motor_on(drive)) {
1460 end_request(0);
1461 goto repeat;
1464 * setup a callback to write the track buffer
1465 * after a short (1 tick) delay.
1467 save_flags (flags);
1468 cli();
1470 unit[drive].dirty = 1;
1471 /* reset the timer */
1472 del_timer (flush_track_timer + drive);
1474 flush_track_timer[drive].expires = jiffies + 1;
1475 add_timer (flush_track_timer + drive);
1476 restore_flags (flags);
1477 break;
1480 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1481 CURRENT->sector += CURRENT->current_nr_sectors;
1483 end_request(1);
1484 goto repeat;
1487 static void do_fd_request(void)
1489 redo_fd_request();
1492 static int fd_ioctl(struct inode *inode, struct file *filp,
1493 unsigned int cmd, unsigned long param)
1495 int drive = inode->i_rdev & 3;
1496 static struct floppy_struct getprm;
1497 struct super_block * sb;
1499 switch(cmd){
1500 case HDIO_GETGEO:
1502 struct hd_geometry loc;
1503 loc.heads = unit[drive].type->heads;
1504 loc.sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1505 loc.cylinders = unit[drive].type->tracks;
1506 loc.start = 0;
1507 if (copy_to_user((void *)param, (void *)&loc,
1508 sizeof(struct hd_geometry)))
1509 return -EFAULT;
1510 break;
1512 case FDFMTBEG:
1513 get_fdc(drive);
1514 if (fd_ref[drive] > 1) {
1515 rel_fdc();
1516 return -EBUSY;
1518 fsync_dev(inode->i_rdev);
1519 if (fd_motor_on(drive) == 0) {
1520 rel_fdc();
1521 return -ENODEV;
1523 if (fd_calibrate(drive) == 0) {
1524 rel_fdc();
1525 return -ENXIO;
1527 floppy_off(drive);
1528 rel_fdc();
1529 break;
1530 case FDFMTTRK:
1531 if (param < unit[drive].type->tracks * unit[drive].type->heads)
1533 get_fdc(drive);
1534 if (fd_seek(drive,param) != 0){
1535 memset(unit[drive].trackbuf, FD_FILL_BYTE,
1536 unit[drive].dtype->sects * unit[drive].type->sect_mult * 512);
1537 non_int_flush_track(drive);
1539 floppy_off(drive);
1540 rel_fdc();
1542 else
1543 return -EINVAL;
1544 break;
1545 case FDFMTEND:
1546 floppy_off(drive);
1547 sb = get_super(inode->i_rdev);
1548 if (sb)
1549 invalidate_inodes(sb);
1550 invalidate_buffers(inode->i_rdev);
1551 break;
1552 case FDGETPRM:
1553 memset((void *)&getprm, 0, sizeof (getprm));
1554 getprm.track=unit[drive].type->tracks;
1555 getprm.head=unit[drive].type->heads;
1556 getprm.sect=unit[drive].dtype->sects * unit[drive].type->sect_mult;
1557 getprm.size=unit[drive].blocks;
1558 if (copy_to_user((void *)param,
1559 (void *)&getprm,
1560 sizeof(struct floppy_struct)))
1561 return -EFAULT;
1562 break;
1563 case BLKGETSIZE:
1564 return put_user(unit[drive].blocks,(long *)param);
1565 break;
1566 case FDSETPRM:
1567 case FDDEFPRM:
1568 return -EINVAL;
1569 case FDFLUSH: /* unconditionally, even if not needed */
1570 del_timer (flush_track_timer + drive);
1571 non_int_flush_track(drive);
1572 break;
1573 #ifdef RAW_IOCTL
1574 case IOCTL_RAW_TRACK:
1575 if (copy_to_user((void *)param, raw_buf,
1576 unit[drive].type->read_size))
1577 return -EFAULT;
1578 else
1579 return unit[drive].type->read_size;
1580 #endif
1581 default:
1582 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
1583 cmd, drive);
1584 return -ENOSYS;
1586 return 0;
1589 static void fd_probe(int dev)
1591 unsigned long code;
1592 int type;
1593 int drive;
1595 drive = dev & 3;
1596 code = fd_get_drive_id(drive);
1598 /* get drive type */
1599 for (type = 0; type < num_dr_types; type++)
1600 if (drive_types[type].code == code)
1601 break;
1603 if (type >= num_dr_types) {
1604 printk(KERN_WARNING "fd_probe: unsupported drive type "
1605 "%08lx found\n", code);
1606 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1607 return;
1610 unit[drive].type = drive_types + type;
1611 unit[drive].track = -1;
1613 unit[drive].disk = -1;
1614 unit[drive].motor = 0;
1615 unit[drive].busy = 0;
1616 unit[drive].status = -1;
1620 * floppy_open check for aliasing (/dev/fd0 can be the same as
1621 * /dev/PS0 etc), and disallows simultaneous access to the same
1622 * drive with different device numbers.
1624 static int floppy_open(struct inode *inode, struct file *filp)
1626 int drive;
1627 int old_dev;
1628 int system;
1629 unsigned long flags;
1631 drive = MINOR(inode->i_rdev) & 3;
1632 old_dev = fd_device[drive];
1634 if (fd_ref[drive])
1635 if (old_dev != inode->i_rdev)
1636 return -EBUSY;
1638 if (unit[drive].type->code == FD_NODRIVE)
1639 return -ENODEV;
1641 if (filp && filp->f_mode & 3) {
1642 check_disk_change(inode->i_rdev);
1643 if (filp->f_mode & 2 ) {
1644 int wrprot;
1646 get_fdc(drive);
1647 fd_select (drive);
1648 wrprot = !(ciaa.pra & DSKPROT);
1649 fd_deselect (drive);
1650 rel_fdc();
1652 if (wrprot)
1653 return -EROFS;
1657 save_flags(flags);
1658 cli();
1659 fd_ref[drive]++;
1660 fd_device[drive] = inode->i_rdev;
1661 #ifdef MODULE
1662 if (unit[drive].motor == 0)
1663 MOD_INC_USE_COUNT;
1664 #endif
1665 restore_flags(flags);
1667 if (old_dev && old_dev != inode->i_rdev)
1668 invalidate_buffers(old_dev);
1670 system=(inode->i_rdev & 4)>>2;
1671 unit[drive].dtype=&data_types[system];
1672 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1673 data_types[system].sects*unit[drive].type->sect_mult;
1674 floppy_sizes[MINOR(inode->i_rdev)] = unit[drive].blocks >> 1;
1676 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1677 unit[drive].type->name, data_types[system].name);
1679 return 0;
1682 static int floppy_release(struct inode * inode, struct file * filp)
1684 #ifdef DEBUG
1685 struct super_block * sb;
1686 #endif
1687 int drive = MINOR(inode->i_rdev) & 3;
1689 fsync_dev(inode->i_rdev);
1691 #ifdef DEBUG
1692 /* This is now handled in floppy_change, but still useful for debugging */
1693 sb = get_super(inode->i_rdev);
1694 if (sb)
1695 invalidate_inodes(sb);
1696 invalidate_buffers(inode->i_rdev);
1697 #endif
1699 if (unit[drive].dirty == 1) {
1700 del_timer (flush_track_timer + drive);
1701 non_int_flush_track (drive);
1704 if (!fd_ref[drive]--) {
1705 printk(KERN_CRIT "floppy_release with fd_ref == 0");
1706 fd_ref[drive] = 0;
1708 #ifdef MODULE
1709 /* the mod_use counter is handled this way */
1710 floppy_off (drive | 0x40000000);
1711 #endif
1712 return 0;
1716 * floppy-change is never called from an interrupt, so we can relax a bit
1717 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1718 * to the desired drive, but it will probably not survive the sleep if
1719 * several floppies are used at the same time: thus the loop.
1721 static int amiga_floppy_change(kdev_t dev)
1723 int drive = MINOR(dev) & 3;
1724 int changed;
1725 static int first_time = 1;
1727 if (MAJOR(dev) != MAJOR_NR) {
1728 printk(KERN_CRIT "floppy_change: not a floppy\n");
1729 return 0;
1732 if (first_time)
1733 changed = first_time--;
1734 else {
1735 get_fdc(drive);
1736 fd_select (drive);
1737 changed = !(ciaa.pra & DSKCHANGE);
1738 fd_deselect (drive);
1739 rel_fdc();
1742 if (changed) {
1743 fd_probe(drive);
1744 unit[drive].track = -1;
1745 unit[drive].dirty = 0;
1746 writepending = 0; /* if this was true before, too bad! */
1747 writefromint = 0;
1748 return 1;
1750 return 0;
1753 static struct file_operations floppy_fops = {
1754 NULL, /* lseek - default */
1755 block_read, /* read - general block-dev read */
1756 block_write, /* write - general block-dev write */
1757 NULL, /* readdir - bad */
1758 NULL, /* select */
1759 fd_ioctl, /* ioctl */
1760 NULL, /* mmap */
1761 floppy_open, /* open */
1762 NULL, /* flush */
1763 floppy_release, /* release */
1764 block_fsync, /* fsync */
1765 NULL, /* fasync */
1766 amiga_floppy_change, /* check_media_change */
1767 NULL, /* revalidate */
1770 void __init amiga_floppy_setup (char *str, int *ints)
1772 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", ints[1]);
1773 fd_def_df0 = ints[1];
1776 static int __init fd_probe_drives(void)
1778 int drive,drives,nomem;
1780 printk(KERN_INFO "FD: probing units\n" KERN_INFO "found ");
1781 drives=0;
1782 nomem=0;
1783 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1784 fd_probe(drive);
1785 if (unit[drive].type->code != FD_NODRIVE) {
1786 drives++;
1787 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
1788 printk("no mem for ");
1789 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
1790 drives--;
1791 nomem = 1;
1793 printk("fd%d ",drive);
1796 if ((drives > 0) || (nomem == 0)) {
1797 if (drives == 0)
1798 printk("no drives");
1799 printk("\n");
1800 return drives;
1802 printk("\n");
1803 return -ENOMEM;
1806 int __init amiga_floppy_init(void)
1808 int i;
1810 if (!AMIGAHW_PRESENT(AMI_FLOPPY))
1811 return -ENXIO;
1813 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
1814 printk("fd: Unable to get major %d for floppy\n",MAJOR_NR);
1815 return -EBUSY;
1818 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE)) == NULL) {
1819 printk("fd: cannot get chip mem buffer\n");
1820 unregister_blkdev(MAJOR_NR,"fd");
1821 return -ENOMEM;
1824 if (!request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
1825 printk("fd: cannot get irq for dma\n");
1826 amiga_chip_free(raw_buf);
1827 unregister_blkdev(MAJOR_NR,"fd");
1828 return -EBUSY;
1830 if (!request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
1831 printk("fd: cannot get irq for timer\n");
1832 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1833 amiga_chip_free(raw_buf);
1834 unregister_blkdev(MAJOR_NR,"fd");
1835 return -EBUSY;
1837 if (fd_probe_drives() < 1) { /* No usable drives */
1838 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1839 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1840 amiga_chip_free(raw_buf);
1841 unregister_blkdev(MAJOR_NR,"fd");
1842 return -ENXIO;
1845 /* initialize variables */
1846 motor_on_timer.next = NULL;
1847 motor_on_timer.prev = NULL;
1848 motor_on_timer.expires = 0;
1849 motor_on_timer.data = 0;
1850 motor_on_timer.function = motor_on_callback;
1851 for (i = 0; i < FD_MAX_UNITS; i++) {
1852 motor_off_timer[i].next = NULL;
1853 motor_off_timer[i].prev = NULL;
1854 motor_off_timer[i].expires = 0;
1855 motor_off_timer[i].data = i|0x80000000;
1856 motor_off_timer[i].function = fd_motor_off;
1857 flush_track_timer[i].next = NULL;
1858 flush_track_timer[i].prev = NULL;
1859 flush_track_timer[i].expires = 0;
1860 flush_track_timer[i].data = i;
1861 flush_track_timer[i].function = flush_track_callback;
1863 unit[i].track = -1;
1866 post_write_timer.next = NULL;
1867 post_write_timer.prev = NULL;
1868 post_write_timer.expires = 0;
1869 post_write_timer.data = 0;
1870 post_write_timer.function = post_write;
1872 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1873 blksize_size[MAJOR_NR] = floppy_blocksizes;
1874 blk_size[MAJOR_NR] = floppy_sizes;
1876 for (i = 0; i < 128; i++)
1877 mfmdecode[i]=255;
1878 for (i = 0; i < 16; i++)
1879 mfmdecode[mfmencode[i]]=i;
1881 /* make sure that disk DMA is enabled */
1882 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1884 /* init ms timer */
1885 ciaa.crb = 8; /* one-shot, stop */
1887 (void)do_floppy; /* avoid warning about unused variable */
1888 return 0;
1891 #ifdef MODULE
1892 #include <linux/version.h>
1894 int init_module(void)
1896 if (!MACH_IS_AMIGA)
1897 return -ENXIO;
1898 return amiga_floppy_init();
1901 void cleanup_module(void)
1903 int i;
1905 for( i = 0; i < FD_MAX_UNITS; i++)
1906 if (unit[i].type->code != FD_NODRIVE)
1907 kfree(unit[i].trackbuf);
1908 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1909 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1910 custom.dmacon = DMAF_DISK; /* disable DMA */
1911 amiga_chip_free(raw_buf);
1912 blk_size[MAJOR_NR] = NULL;
1913 blksize_size[MAJOR_NR] = NULL;
1914 blk_dev[MAJOR_NR].request_fn = NULL;
1915 unregister_blkdev(MAJOR_NR, "fd");
1917 #endif