Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / block / amiflop.c
blob2a8af685926fe0d2c7b197c535c6c0549c8224f1
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/fd.h>
59 #include <linux/hdreg.h>
60 #include <linux/delay.h>
61 #include <linux/init.h>
62 #include <linux/amifdreg.h>
63 #include <linux/amifd.h>
64 #include <linux/buffer_head.h>
65 #include <linux/blkdev.h>
66 #include <linux/elevator.h>
68 #include <asm/setup.h>
69 #include <asm/uaccess.h>
70 #include <asm/amigahw.h>
71 #include <asm/amigaints.h>
72 #include <asm/irq.h>
74 #undef DEBUG /* print _LOTS_ of infos */
76 #define RAW_IOCTL
77 #ifdef RAW_IOCTL
78 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
79 #endif
82 * Defines
86 * Error codes
88 #define FD_OK 0 /* operation succeeded */
89 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
90 #define FD_NOUNIT 1 /* unit does not exist */
91 #define FD_UNITBUSY 2 /* unit already active */
92 #define FD_NOTACTIVE 3 /* unit is not active */
93 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
95 #define MFM_NOSYNC 1
96 #define MFM_HEADER 2
97 #define MFM_DATA 3
98 #define MFM_TRACK 4
101 * Floppy ID values
103 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
104 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
105 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
106 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
108 static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
110 module_param(fd_def_df0, ulong, 0);
111 MODULE_LICENSE("GPL");
113 static struct request_queue *floppy_queue;
114 #define QUEUE (floppy_queue)
115 #define CURRENT elv_next_request(floppy_queue)
118 * Macros
120 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
121 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
122 #define SELECT(mask) (ciab.prb &= ~mask)
123 #define DESELECT(mask) (ciab.prb |= mask)
124 #define SELMASK(drive) (1 << (3 + (drive & 3)))
126 static struct fd_drive_type drive_types[] = {
127 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
128 /* warning: times are now in milliseconds (ms) */
129 { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
130 { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
131 { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
132 { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
134 static int num_dr_types = ARRAY_SIZE(drive_types);
136 static int amiga_read(int), dos_read(int);
137 static void amiga_write(int), dos_write(int);
138 static struct fd_data_type data_types[] = {
139 { "Amiga", 11 , amiga_read, amiga_write},
140 { "MS-Dos", 9, dos_read, dos_write}
143 /* current info on each unit */
144 static struct amiga_floppy_struct unit[FD_MAX_UNITS];
146 static struct timer_list flush_track_timer[FD_MAX_UNITS];
147 static struct timer_list post_write_timer;
148 static struct timer_list motor_on_timer;
149 static struct timer_list motor_off_timer[FD_MAX_UNITS];
150 static int on_attempts;
152 /* Synchronization of FDC access */
153 /* request loop (trackbuffer) */
154 static volatile int fdc_busy = -1;
155 static volatile int fdc_nested;
156 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
158 static DECLARE_WAIT_QUEUE_HEAD(motor_wait);
160 static volatile int selected = -1; /* currently selected drive */
162 static int writepending;
163 static int writefromint;
164 static char *raw_buf;
166 static DEFINE_SPINLOCK(amiflop_lock);
168 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
171 * These are global variables, as that's the easiest way to give
172 * information to interrupts. They are the data used for the current
173 * request.
175 static volatile char block_flag;
176 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
178 /* MS-Dos MFM Coding tables (should go quick and easy) */
179 static unsigned char mfmencode[16]={
180 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
181 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
183 static unsigned char mfmdecode[128];
185 /* floppy internal millisecond timer stuff */
186 static volatile int ms_busy = -1;
187 static DECLARE_WAIT_QUEUE_HEAD(ms_wait);
188 #define MS_TICKS ((amiga_eclock+50)/1000)
191 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
192 * max X times - some types of errors increase the errorcount by 2 or
193 * even 3, so we might actually retry only X/2 times before giving up.
195 #define MAX_ERRORS 12
197 #define custom amiga_custom
199 /* Prevent "aliased" accesses. */
200 static int fd_ref[4] = { 0,0,0,0 };
201 static int fd_device[4] = { 0, 0, 0, 0 };
204 * Here come the actual hardware access and helper functions.
205 * They are not reentrant and single threaded because all drives
206 * share the same hardware and the same trackbuffer.
209 /* Milliseconds timer */
211 static irqreturn_t ms_isr(int irq, void *dummy, struct pt_regs *fp)
213 ms_busy = -1;
214 wake_up(&ms_wait);
215 return IRQ_HANDLED;
218 /* all waits are queued up
219 A more generic routine would do a schedule a la timer.device */
220 static void ms_delay(int ms)
222 unsigned long flags;
223 int ticks;
224 if (ms > 0) {
225 local_irq_save(flags);
226 while (ms_busy == 0)
227 sleep_on(&ms_wait);
228 ms_busy = 0;
229 local_irq_restore(flags);
230 ticks = MS_TICKS*ms-1;
231 ciaa.tblo=ticks%256;
232 ciaa.tbhi=ticks/256;
233 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
234 sleep_on(&ms_wait);
238 /* Hardware semaphore */
240 /* returns true when we would get the semaphore */
241 static inline int try_fdc(int drive)
243 drive &= 3;
244 return ((fdc_busy < 0) || (fdc_busy == drive));
247 static void get_fdc(int drive)
249 unsigned long flags;
251 drive &= 3;
252 #ifdef DEBUG
253 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
254 #endif
255 local_irq_save(flags);
256 while (!try_fdc(drive))
257 sleep_on(&fdc_wait);
258 fdc_busy = drive;
259 fdc_nested++;
260 local_irq_restore(flags);
263 static inline void rel_fdc(void)
265 #ifdef DEBUG
266 if (fdc_nested == 0)
267 printk("fd: unmatched rel_fdc\n");
268 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
269 #endif
270 fdc_nested--;
271 if (fdc_nested == 0) {
272 fdc_busy = -1;
273 wake_up(&fdc_wait);
277 static void fd_select (int drive)
279 unsigned char prb = ~0;
281 drive&=3;
282 #ifdef DEBUG
283 printk("selecting %d\n",drive);
284 #endif
285 if (drive == selected)
286 return;
287 get_fdc(drive);
288 selected = drive;
290 if (unit[drive].track % 2 != 0)
291 prb &= ~DSKSIDE;
292 if (unit[drive].motor == 1)
293 prb &= ~DSKMOTOR;
294 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
295 ciab.prb = prb;
296 prb &= ~SELMASK(drive);
297 ciab.prb = prb;
298 rel_fdc();
301 static void fd_deselect (int drive)
303 unsigned char prb;
304 unsigned long flags;
306 drive&=3;
307 #ifdef DEBUG
308 printk("deselecting %d\n",drive);
309 #endif
310 if (drive != selected) {
311 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
312 return;
315 get_fdc(drive);
316 local_irq_save(flags);
318 selected = -1;
320 prb = ciab.prb;
321 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
322 ciab.prb = prb;
324 local_irq_restore (flags);
325 rel_fdc();
329 static void motor_on_callback(unsigned long nr)
331 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
332 wake_up (&motor_wait);
333 } else {
334 motor_on_timer.expires = jiffies + HZ/10;
335 add_timer(&motor_on_timer);
339 static int fd_motor_on(int nr)
341 nr &= 3;
343 del_timer(motor_off_timer + nr);
345 if (!unit[nr].motor) {
346 unit[nr].motor = 1;
347 fd_select(nr);
349 motor_on_timer.data = nr;
350 mod_timer(&motor_on_timer, jiffies + HZ/2);
352 on_attempts = 10;
353 sleep_on (&motor_wait);
354 fd_deselect(nr);
357 if (on_attempts == 0) {
358 on_attempts = -1;
359 #if 0
360 printk (KERN_ERR "motor_on failed, turning motor off\n");
361 fd_motor_off (nr);
362 return 0;
363 #else
364 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
365 #endif
368 return 1;
371 static void fd_motor_off(unsigned long drive)
373 long calledfromint;
374 #ifdef MODULE
375 long decusecount;
377 decusecount = drive & 0x40000000;
378 #endif
379 calledfromint = drive & 0x80000000;
380 drive&=3;
381 if (calledfromint && !try_fdc(drive)) {
382 /* We would be blocked in an interrupt, so try again later */
383 motor_off_timer[drive].expires = jiffies + 1;
384 add_timer(motor_off_timer + drive);
385 return;
387 unit[drive].motor = 0;
388 fd_select(drive);
389 udelay (1);
390 fd_deselect(drive);
393 static void floppy_off (unsigned int nr)
395 int drive;
397 drive = nr & 3;
398 /* called this way it is always from interrupt */
399 motor_off_timer[drive].data = nr | 0x80000000;
400 mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
403 static int fd_calibrate(int drive)
405 unsigned char prb;
406 int n;
408 drive &= 3;
409 get_fdc(drive);
410 if (!fd_motor_on (drive))
411 return 0;
412 fd_select (drive);
413 prb = ciab.prb;
414 prb |= DSKSIDE;
415 prb &= ~DSKDIREC;
416 ciab.prb = prb;
417 for (n = unit[drive].type->tracks/2; n != 0; --n) {
418 if (ciaa.pra & DSKTRACK0)
419 break;
420 prb &= ~DSKSTEP;
421 ciab.prb = prb;
422 prb |= DSKSTEP;
423 udelay (2);
424 ciab.prb = prb;
425 ms_delay(unit[drive].type->step_delay);
427 ms_delay (unit[drive].type->settle_time);
428 prb |= DSKDIREC;
429 n = unit[drive].type->tracks + 20;
430 for (;;) {
431 prb &= ~DSKSTEP;
432 ciab.prb = prb;
433 prb |= DSKSTEP;
434 udelay (2);
435 ciab.prb = prb;
436 ms_delay(unit[drive].type->step_delay + 1);
437 if ((ciaa.pra & DSKTRACK0) == 0)
438 break;
439 if (--n == 0) {
440 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
441 fd_motor_off (drive);
442 unit[drive].track = -1;
443 rel_fdc();
444 return 0;
447 unit[drive].track = 0;
448 ms_delay(unit[drive].type->settle_time);
450 rel_fdc();
451 fd_deselect(drive);
452 return 1;
455 static int fd_seek(int drive, int track)
457 unsigned char prb;
458 int cnt;
460 #ifdef DEBUG
461 printk("seeking drive %d to track %d\n",drive,track);
462 #endif
463 drive &= 3;
464 get_fdc(drive);
465 if (unit[drive].track == track) {
466 rel_fdc();
467 return 1;
469 if (!fd_motor_on(drive)) {
470 rel_fdc();
471 return 0;
473 if (unit[drive].track < 0 && !fd_calibrate(drive)) {
474 rel_fdc();
475 return 0;
478 fd_select (drive);
479 cnt = unit[drive].track/2 - track/2;
480 prb = ciab.prb;
481 prb |= DSKSIDE | DSKDIREC;
482 if (track % 2 != 0)
483 prb &= ~DSKSIDE;
484 if (cnt < 0) {
485 cnt = - cnt;
486 prb &= ~DSKDIREC;
488 ciab.prb = prb;
489 if (track % 2 != unit[drive].track % 2)
490 ms_delay (unit[drive].type->side_time);
491 unit[drive].track = track;
492 if (cnt == 0) {
493 rel_fdc();
494 fd_deselect(drive);
495 return 1;
497 do {
498 prb &= ~DSKSTEP;
499 ciab.prb = prb;
500 prb |= DSKSTEP;
501 udelay (1);
502 ciab.prb = prb;
503 ms_delay (unit[drive].type->step_delay);
504 } while (--cnt != 0);
505 ms_delay (unit[drive].type->settle_time);
507 rel_fdc();
508 fd_deselect(drive);
509 return 1;
512 static unsigned long fd_get_drive_id(int drive)
514 int i;
515 ulong id = 0;
517 drive&=3;
518 get_fdc(drive);
519 /* set up for ID */
520 MOTOR_ON;
521 udelay(2);
522 SELECT(SELMASK(drive));
523 udelay(2);
524 DESELECT(SELMASK(drive));
525 udelay(2);
526 MOTOR_OFF;
527 udelay(2);
528 SELECT(SELMASK(drive));
529 udelay(2);
530 DESELECT(SELMASK(drive));
531 udelay(2);
533 /* loop and read disk ID */
534 for (i=0; i<32; i++) {
535 SELECT(SELMASK(drive));
536 udelay(2);
538 /* read and store value of DSKRDY */
539 id <<= 1;
540 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
542 DESELECT(SELMASK(drive));
545 rel_fdc();
548 * RB: At least A500/A2000's df0: don't identify themselves.
549 * As every (real) Amiga has at least a 3.5" DD drive as df0:
550 * we default to that if df0: doesn't identify as a certain
551 * type.
553 if(drive == 0 && id == FD_NODRIVE)
555 id = fd_def_df0;
556 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
558 /* return the ID value */
559 return (id);
562 static irqreturn_t fd_block_done(int irq, void *dummy, struct pt_regs *fp)
564 if (block_flag)
565 custom.dsklen = 0x4000;
567 if (block_flag == 2) { /* writing */
568 writepending = 2;
569 post_write_timer.expires = jiffies + 1; /* at least 2 ms */
570 post_write_timer.data = selected;
571 add_timer(&post_write_timer);
573 else { /* reading */
574 block_flag = 0;
575 wake_up (&wait_fd_block);
577 return IRQ_HANDLED;
580 static void raw_read(int drive)
582 drive&=3;
583 get_fdc(drive);
584 while (block_flag)
585 sleep_on(&wait_fd_block);
586 fd_select(drive);
587 /* setup adkcon bits correctly */
588 custom.adkcon = ADK_MSBSYNC;
589 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
591 custom.dsksync = MFM_SYNC;
593 custom.dsklen = 0;
594 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
595 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
596 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
598 block_flag = 1;
600 while (block_flag)
601 sleep_on (&wait_fd_block);
603 custom.dsklen = 0;
604 fd_deselect(drive);
605 rel_fdc();
608 static int raw_write(int drive)
610 ushort adk;
612 drive&=3;
613 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
614 if ((ciaa.pra & DSKPROT) == 0) {
615 rel_fdc();
616 return 0;
618 while (block_flag)
619 sleep_on(&wait_fd_block);
620 fd_select(drive);
621 /* clear adkcon bits */
622 custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
623 /* set appropriate adkcon bits */
624 adk = ADK_SETCLR|ADK_FAST;
625 if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
626 adk |= ADK_PRECOMP1;
627 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
628 adk |= ADK_PRECOMP0;
629 custom.adkcon = adk;
631 custom.dsklen = DSKLEN_WRITE;
632 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
633 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
634 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
636 block_flag = 2;
637 return 1;
641 * to be called at least 2ms after the write has finished but before any
642 * other access to the hardware.
644 static void post_write (unsigned long drive)
646 #ifdef DEBUG
647 printk("post_write for drive %ld\n",drive);
648 #endif
649 drive &= 3;
650 custom.dsklen = 0;
651 block_flag = 0;
652 writepending = 0;
653 writefromint = 0;
654 unit[drive].dirty = 0;
655 wake_up(&wait_fd_block);
656 fd_deselect(drive);
657 rel_fdc(); /* corresponds to get_fdc() in raw_write */
662 * The following functions are to convert the block contents into raw data
663 * written to disk and vice versa.
664 * (Add other formats here ;-))
667 static unsigned long scan_sync(unsigned long raw, unsigned long end)
669 ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
671 while (ptr < endp && *ptr++ != 0x4489)
673 if (ptr < endp) {
674 while (*ptr == 0x4489 && ptr < endp)
675 ptr++;
676 return (ulong)ptr;
678 return 0;
681 static inline unsigned long checksum(unsigned long *addr, int len)
683 unsigned long csum = 0;
685 len /= sizeof(*addr);
686 while (len-- > 0)
687 csum ^= *addr++;
688 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
690 return csum;
693 static unsigned long decode (unsigned long *data, unsigned long *raw,
694 int len)
696 ulong *odd, *even;
698 /* convert length from bytes to longwords */
699 len >>= 2;
700 odd = raw;
701 even = odd + len;
703 /* prepare return pointer */
704 raw += len * 2;
706 do {
707 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
708 } while (--len != 0);
710 return (ulong)raw;
713 struct header {
714 unsigned char magic;
715 unsigned char track;
716 unsigned char sect;
717 unsigned char ord;
718 unsigned char labels[16];
719 unsigned long hdrchk;
720 unsigned long datachk;
723 static int amiga_read(int drive)
725 unsigned long raw;
726 unsigned long end;
727 int scnt;
728 unsigned long csum;
729 struct header hdr;
731 drive&=3;
732 raw = (long) raw_buf;
733 end = raw + unit[drive].type->read_size;
735 for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
736 if (!(raw = scan_sync(raw, end))) {
737 printk (KERN_INFO "can't find sync for sector %d\n", scnt);
738 return MFM_NOSYNC;
741 raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
742 raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
743 raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
744 raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
745 csum = checksum((ulong *)&hdr,
746 (char *)&hdr.hdrchk-(char *)&hdr);
748 #ifdef DEBUG
749 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
750 hdr.magic, hdr.track, hdr.sect, hdr.ord,
751 *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
752 *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
753 hdr.hdrchk, hdr.datachk);
754 #endif
756 if (hdr.hdrchk != csum) {
757 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
758 return MFM_HEADER;
761 /* verify track */
762 if (hdr.track != unit[drive].track) {
763 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
764 return MFM_TRACK;
767 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
768 (ulong *)raw, 512);
769 csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
771 if (hdr.datachk != csum) {
772 printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
773 hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
774 hdr.datachk, csum);
775 printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
776 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
777 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
778 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
779 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
780 return MFM_DATA;
784 return 0;
787 static void encode(unsigned long data, unsigned long *dest)
789 unsigned long data2;
791 data &= 0x55555555;
792 data2 = data ^ 0x55555555;
793 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
795 if (*(dest - 1) & 0x00000001)
796 data &= 0x7FFFFFFF;
798 *dest = data;
801 static void encode_block(unsigned long *dest, unsigned long *src, int len)
803 int cnt, to_cnt = 0;
804 unsigned long data;
806 /* odd bits */
807 for (cnt = 0; cnt < len / 4; cnt++) {
808 data = src[cnt] >> 1;
809 encode(data, dest + to_cnt++);
812 /* even bits */
813 for (cnt = 0; cnt < len / 4; cnt++) {
814 data = src[cnt];
815 encode(data, dest + to_cnt++);
819 static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
821 struct header hdr;
822 int i;
824 disk&=3;
825 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
826 raw++;
827 *raw++ = 0x44894489;
829 hdr.magic = 0xFF;
830 hdr.track = unit[disk].track;
831 hdr.sect = cnt;
832 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
833 for (i = 0; i < 16; i++)
834 hdr.labels[i] = 0;
835 hdr.hdrchk = checksum((ulong *)&hdr,
836 (char *)&hdr.hdrchk-(char *)&hdr);
837 hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
839 encode_block(raw, (ulong *)&hdr.magic, 4);
840 raw += 2;
841 encode_block(raw, (ulong *)&hdr.labels, 16);
842 raw += 8;
843 encode_block(raw, (ulong *)&hdr.hdrchk, 4);
844 raw += 2;
845 encode_block(raw, (ulong *)&hdr.datachk, 4);
846 raw += 2;
847 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
848 raw += 256;
850 return raw;
853 static void amiga_write(int disk)
855 unsigned int cnt;
856 unsigned long *ptr = (unsigned long *)raw_buf;
858 disk&=3;
859 /* gap space */
860 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
861 *ptr++ = 0xaaaaaaaa;
863 /* sectors */
864 for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
865 ptr = putsec (disk, ptr, cnt);
866 *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
870 struct dos_header {
871 unsigned char track, /* 0-80 */
872 side, /* 0-1 */
873 sec, /* 0-...*/
874 len_desc;/* 2 */
875 unsigned short crc; /* on 68000 we got an alignment problem,
876 but this compiler solves it by adding silently
877 adding a pad byte so data won't fit
878 and this took about 3h to discover.... */
879 unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
882 /* crc routines are borrowed from the messydos-handler */
884 /* excerpt from the messydos-device
885 ; The CRC is computed not only over the actual data, but including
886 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
887 ; As we don't read or encode these fields into our buffers, we have to
888 ; preload the registers containing the CRC with the values they would have
889 ; after stepping over these fields.
891 ; How CRCs "really" work:
893 ; First, you should regard a bitstring as a series of coefficients of
894 ; polynomials. We calculate with these polynomials in modulo-2
895 ; arithmetic, in which both add and subtract are done the same as
896 ; exclusive-or. Now, we modify our data (a very long polynomial) in
897 ; such a way that it becomes divisible by the CCITT-standard 16-bit
898 ; 16 12 5
899 ; polynomial: x + x + x + 1, represented by $11021. The easiest
900 ; way to do this would be to multiply (using proper arithmetic) our
901 ; datablock with $11021. So we have:
902 ; data * $11021 =
903 ; data * ($10000 + $1021) =
904 ; data * $10000 + data * $1021
905 ; The left part of this is simple: Just add two 0 bytes. But then
906 ; the right part (data $1021) remains difficult and even could have
907 ; a carry into the left part. The solution is to use a modified
908 ; multiplication, which has a result that is not correct, but with
909 ; a difference of any multiple of $11021. We then only need to keep
910 ; the 16 least significant bits of the result.
912 ; The following algorithm does this for us:
914 ; unsigned char *data, c, crclo, crchi;
915 ; while (not done) {
916 ; c = *data++ + crchi;
917 ; crchi = (@ c) >> 8 + crclo;
918 ; crclo = @ c;
921 ; Remember, + is done with EOR, the @ operator is in two tables (high
922 ; and low byte separately), which is calculated as
924 ; $1021 * (c & $F0)
925 ; xor $1021 * (c & $0F)
926 ; xor $1021 * (c >> 4) (* is regular multiplication)
929 ; Anyway, the end result is the same as the remainder of the division of
930 ; the data by $11021. I am afraid I need to study theory a bit more...
933 my only works was to code this from manx to C....
937 static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
939 static unsigned char CRCTable1[] = {
940 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
941 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
942 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
943 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
944 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
945 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
946 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
947 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
948 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
949 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
950 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
951 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
952 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
953 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
954 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
955 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
958 static unsigned char CRCTable2[] = {
959 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
960 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
961 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
962 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
963 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
964 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
965 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
966 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
967 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
968 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
969 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
970 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
971 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
972 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
973 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
974 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
977 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
978 register int i;
979 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
981 CRCT1=CRCTable1;
982 CRCT2=CRCTable2;
983 data=data_a3;
984 crcl=data_d1;
985 crch=data_d0;
986 for (i=data_d3; i>=0; i--) {
987 c = (*data++) ^ crch;
988 crch = CRCT1[c] ^ crcl;
989 crcl = CRCT2[c];
991 return (crch<<8)|crcl;
994 static inline ushort dos_hdr_crc (struct dos_header *hdr)
996 return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
999 static inline ushort dos_data_crc(unsigned char *data)
1001 return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
1004 static inline unsigned char dos_decode_byte(ushort word)
1006 register ushort w2;
1007 register unsigned char byte;
1008 register unsigned char *dec = mfmdecode;
1010 w2=word;
1011 w2>>=8;
1012 w2&=127;
1013 byte = dec[w2];
1014 byte <<= 4;
1015 w2 = word & 127;
1016 byte |= dec[w2];
1017 return byte;
1020 static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
1022 int i;
1024 for (i = 0; i < len; i++)
1025 *data++=dos_decode_byte(*raw++);
1026 return ((ulong)raw);
1029 #ifdef DEBUG
1030 static void dbg(unsigned long ptr)
1032 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
1033 ((ulong *)ptr)[0], ((ulong *)ptr)[1],
1034 ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
1036 #endif
1038 static int dos_read(int drive)
1040 unsigned long end;
1041 unsigned long raw;
1042 int scnt;
1043 unsigned short crc,data_crc[2];
1044 struct dos_header hdr;
1046 drive&=3;
1047 raw = (long) raw_buf;
1048 end = raw + unit[drive].type->read_size;
1050 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1051 do { /* search for the right sync of each sec-hdr */
1052 if (!(raw = scan_sync (raw, end))) {
1053 printk(KERN_INFO "dos_read: no hdr sync on "
1054 "track %d, unit %d for sector %d\n",
1055 unit[drive].track,drive,scnt);
1056 return MFM_NOSYNC;
1058 #ifdef DEBUG
1059 dbg(raw);
1060 #endif
1061 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1062 raw+=2; /* skip over headermark */
1063 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1064 crc = dos_hdr_crc(&hdr);
1066 #ifdef DEBUG
1067 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1068 hdr.sec, hdr.len_desc, hdr.crc);
1069 #endif
1071 if (crc != hdr.crc) {
1072 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
1073 hdr.crc, crc);
1074 return MFM_HEADER;
1076 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1077 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
1078 hdr.track,
1079 unit[drive].track/unit[drive].type->heads);
1080 return MFM_TRACK;
1083 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1084 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
1085 hdr.side,
1086 unit[drive].track%unit[drive].type->heads);
1087 return MFM_TRACK;
1090 if (hdr.len_desc != 2) {
1091 printk(KERN_INFO "dos_read: unknown sector len "
1092 "descriptor %d\n", hdr.len_desc);
1093 return MFM_DATA;
1095 #ifdef DEBUG
1096 printk("hdr accepted\n");
1097 #endif
1098 if (!(raw = scan_sync (raw, end))) {
1099 printk(KERN_INFO "dos_read: no data sync on track "
1100 "%d, unit %d for sector%d, disk sector %d\n",
1101 unit[drive].track, drive, scnt, hdr.sec);
1102 return MFM_NOSYNC;
1104 #ifdef DEBUG
1105 dbg(raw);
1106 #endif
1108 if (*((ushort *)raw)!=0x5545) {
1109 printk(KERN_INFO "dos_read: no data mark after "
1110 "sync (%d,%d,%d,%d) sc=%d\n",
1111 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1112 return MFM_NOSYNC;
1115 raw+=2; /* skip data mark (included in checksum) */
1116 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1117 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1118 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1120 if (crc != data_crc[0]) {
1121 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
1122 "sc=%d, %x %x\n", hdr.track, hdr.side,
1123 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
1124 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1125 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1126 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1127 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1128 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1129 return MFM_DATA;
1132 return 0;
1135 static inline ushort dos_encode_byte(unsigned char byte)
1137 register unsigned char *enc, b2, b1;
1138 register ushort word;
1140 enc=mfmencode;
1141 b1=byte;
1142 b2=b1>>4;
1143 b1&=15;
1144 word=enc[b2] <<8 | enc [b1];
1145 return (word|((word&(256|64)) ? 0: 128));
1148 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1150 int i;
1152 for (i = 0; i < len; i++) {
1153 *dest=dos_encode_byte(*src++);
1154 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1155 dest++;
1159 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1161 static struct dos_header hdr={0,0,0,2,0,
1162 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1163 int i;
1164 static ushort crc[2]={0,0x4e4e};
1166 drive&=3;
1167 /* id gap 1 */
1168 /* the MFM word before is always 9254 */
1169 for(i=0;i<6;i++)
1170 *raw++=0xaaaaaaaa;
1171 /* 3 sync + 1 headermark */
1172 *raw++=0x44894489;
1173 *raw++=0x44895554;
1175 /* fill in the variable parts of the header */
1176 hdr.track=unit[drive].track/unit[drive].type->heads;
1177 hdr.side=unit[drive].track%unit[drive].type->heads;
1178 hdr.sec=cnt+1;
1179 hdr.crc=dos_hdr_crc(&hdr);
1181 /* header (without "magic") and id gap 2*/
1182 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1183 raw+=14;
1185 /*id gap 3 */
1186 for(i=0;i<6;i++)
1187 *raw++=0xaaaaaaaa;
1189 /* 3 syncs and 1 datamark */
1190 *raw++=0x44894489;
1191 *raw++=0x44895545;
1193 /* data */
1194 dos_encode_block((ushort *)raw,
1195 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
1196 raw+=256;
1198 /*data crc + jd's special gap (long words :-/) */
1199 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1200 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1201 raw+=2;
1203 /* data gap */
1204 for(i=0;i<38;i++)
1205 *raw++=0x92549254;
1207 return raw; /* wrote 652 MFM words */
1210 static void dos_write(int disk)
1212 int cnt;
1213 unsigned long raw = (unsigned long) raw_buf;
1214 unsigned long *ptr=(unsigned long *)raw;
1216 disk&=3;
1217 /* really gap4 + indexgap , but we write it first and round it up */
1218 for (cnt=0;cnt<425;cnt++)
1219 *ptr++=0x92549254;
1221 /* the following is just guessed */
1222 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1223 for(cnt=0;cnt<473;cnt++)
1224 *ptr++=0x92549254;
1226 /* now the index marks...*/
1227 for (cnt=0;cnt<20;cnt++)
1228 *ptr++=0x92549254;
1229 for (cnt=0;cnt<6;cnt++)
1230 *ptr++=0xaaaaaaaa;
1231 *ptr++=0x52245224;
1232 *ptr++=0x52245552;
1233 for (cnt=0;cnt<20;cnt++)
1234 *ptr++=0x92549254;
1236 /* sectors */
1237 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1238 ptr=ms_putsec(disk,ptr,cnt);
1240 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1244 * Here comes the high level stuff (i.e. the filesystem interface)
1245 * and helper functions.
1246 * Normally this should be the only part that has to be adapted to
1247 * different kernel versions.
1250 /* FIXME: this assumes the drive is still spinning -
1251 * which is only true if we complete writing a track within three seconds
1253 static void flush_track_callback(unsigned long nr)
1255 nr&=3;
1256 writefromint = 1;
1257 if (!try_fdc(nr)) {
1258 /* we might block in an interrupt, so try again later */
1259 flush_track_timer[nr].expires = jiffies + 1;
1260 add_timer(flush_track_timer + nr);
1261 return;
1263 get_fdc(nr);
1264 (*unit[nr].dtype->write_fkt)(nr);
1265 if (!raw_write(nr)) {
1266 printk (KERN_NOTICE "floppy disk write protected\n");
1267 writefromint = 0;
1268 writepending = 0;
1270 rel_fdc();
1273 static int non_int_flush_track (unsigned long nr)
1275 unsigned long flags;
1277 nr&=3;
1278 writefromint = 0;
1279 del_timer(&post_write_timer);
1280 get_fdc(nr);
1281 if (!fd_motor_on(nr)) {
1282 writepending = 0;
1283 rel_fdc();
1284 return 0;
1286 local_irq_save(flags);
1287 if (writepending != 2) {
1288 local_irq_restore(flags);
1289 (*unit[nr].dtype->write_fkt)(nr);
1290 if (!raw_write(nr)) {
1291 printk (KERN_NOTICE "floppy disk write protected "
1292 "in write!\n");
1293 writepending = 0;
1294 return 0;
1296 while (block_flag == 2)
1297 sleep_on (&wait_fd_block);
1299 else {
1300 local_irq_restore(flags);
1301 ms_delay(2); /* 2 ms post_write delay */
1302 post_write(nr);
1304 rel_fdc();
1305 return 1;
1308 static int get_track(int drive, int track)
1310 int error, errcnt;
1312 drive&=3;
1313 if (unit[drive].track == track)
1314 return 0;
1315 get_fdc(drive);
1316 if (!fd_motor_on(drive)) {
1317 rel_fdc();
1318 return -1;
1321 if (unit[drive].dirty == 1) {
1322 del_timer (flush_track_timer + drive);
1323 non_int_flush_track (drive);
1325 errcnt = 0;
1326 while (errcnt < MAX_ERRORS) {
1327 if (!fd_seek(drive, track))
1328 return -1;
1329 raw_read(drive);
1330 error = (*unit[drive].dtype->read_fkt)(drive);
1331 if (error == 0) {
1332 rel_fdc();
1333 return 0;
1335 /* Read Error Handling: recalibrate and try again */
1336 unit[drive].track = -1;
1337 errcnt++;
1339 rel_fdc();
1340 return -1;
1343 static void redo_fd_request(void)
1345 unsigned int cnt, block, track, sector;
1346 int drive;
1347 struct amiga_floppy_struct *floppy;
1348 char *data;
1349 unsigned long flags;
1351 repeat:
1352 if (!CURRENT) {
1353 /* Nothing left to do */
1354 return;
1357 floppy = CURRENT->rq_disk->private_data;
1358 drive = floppy - unit;
1360 /* Here someone could investigate to be more efficient */
1361 for (cnt = 0; cnt < CURRENT->current_nr_sectors; cnt++) {
1362 #ifdef DEBUG
1363 printk("fd: sector %ld + %d requested for %s\n",
1364 CURRENT->sector,cnt,
1365 (CURRENT->cmd==READ)?"read":"write");
1366 #endif
1367 block = CURRENT->sector + cnt;
1368 if ((int)block > floppy->blocks) {
1369 end_request(CURRENT, 0);
1370 goto repeat;
1373 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1374 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1375 data = CURRENT->buffer + 512 * cnt;
1376 #ifdef DEBUG
1377 printk("access to track %d, sector %d, with buffer at "
1378 "0x%08lx\n", track, sector, data);
1379 #endif
1381 if ((rq_data_dir(CURRENT) != READ) && (rq_data_dir(CURRENT) != WRITE)) {
1382 printk(KERN_WARNING "do_fd_request: unknown command\n");
1383 end_request(CURRENT, 0);
1384 goto repeat;
1386 if (get_track(drive, track) == -1) {
1387 end_request(CURRENT, 0);
1388 goto repeat;
1391 switch (rq_data_dir(CURRENT)) {
1392 case READ:
1393 memcpy(data, floppy->trackbuf + sector * 512, 512);
1394 break;
1396 case WRITE:
1397 memcpy(floppy->trackbuf + sector * 512, data, 512);
1399 /* keep the drive spinning while writes are scheduled */
1400 if (!fd_motor_on(drive)) {
1401 end_request(CURRENT, 0);
1402 goto repeat;
1405 * setup a callback to write the track buffer
1406 * after a short (1 tick) delay.
1408 local_irq_save(flags);
1410 floppy->dirty = 1;
1411 /* reset the timer */
1412 mod_timer (flush_track_timer + drive, jiffies + 1);
1413 local_irq_restore(flags);
1414 break;
1417 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1418 CURRENT->sector += CURRENT->current_nr_sectors;
1420 end_request(CURRENT, 1);
1421 goto repeat;
1424 static void do_fd_request(request_queue_t * q)
1426 redo_fd_request();
1429 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1431 int drive = MINOR(bdev->bd_dev) & 3;
1433 geo->heads = unit[drive].type->heads;
1434 geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1435 geo->cylinders = unit[drive].type->tracks;
1436 return 0;
1439 static int fd_ioctl(struct inode *inode, struct file *filp,
1440 unsigned int cmd, unsigned long param)
1442 int drive = iminor(inode) & 3;
1443 static struct floppy_struct getprm;
1444 void __user *argp = (void __user *)param;
1446 switch(cmd){
1447 case FDFMTBEG:
1448 get_fdc(drive);
1449 if (fd_ref[drive] > 1) {
1450 rel_fdc();
1451 return -EBUSY;
1453 fsync_bdev(inode->i_bdev);
1454 if (fd_motor_on(drive) == 0) {
1455 rel_fdc();
1456 return -ENODEV;
1458 if (fd_calibrate(drive) == 0) {
1459 rel_fdc();
1460 return -ENXIO;
1462 floppy_off(drive);
1463 rel_fdc();
1464 break;
1465 case FDFMTTRK:
1466 if (param < unit[drive].type->tracks * unit[drive].type->heads)
1468 get_fdc(drive);
1469 if (fd_seek(drive,param) != 0){
1470 memset(unit[drive].trackbuf, FD_FILL_BYTE,
1471 unit[drive].dtype->sects * unit[drive].type->sect_mult * 512);
1472 non_int_flush_track(drive);
1474 floppy_off(drive);
1475 rel_fdc();
1477 else
1478 return -EINVAL;
1479 break;
1480 case FDFMTEND:
1481 floppy_off(drive);
1482 invalidate_bdev(inode->i_bdev, 0);
1483 break;
1484 case FDGETPRM:
1485 memset((void *)&getprm, 0, sizeof (getprm));
1486 getprm.track=unit[drive].type->tracks;
1487 getprm.head=unit[drive].type->heads;
1488 getprm.sect=unit[drive].dtype->sects * unit[drive].type->sect_mult;
1489 getprm.size=unit[drive].blocks;
1490 if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
1491 return -EFAULT;
1492 break;
1493 case FDSETPRM:
1494 case FDDEFPRM:
1495 return -EINVAL;
1496 case FDFLUSH: /* unconditionally, even if not needed */
1497 del_timer (flush_track_timer + drive);
1498 non_int_flush_track(drive);
1499 break;
1500 #ifdef RAW_IOCTL
1501 case IOCTL_RAW_TRACK:
1502 if (copy_to_user(argp, raw_buf, unit[drive].type->read_size))
1503 return -EFAULT;
1504 else
1505 return unit[drive].type->read_size;
1506 #endif
1507 default:
1508 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
1509 cmd, drive);
1510 return -ENOSYS;
1512 return 0;
1515 static void fd_probe(int dev)
1517 unsigned long code;
1518 int type;
1519 int drive;
1521 drive = dev & 3;
1522 code = fd_get_drive_id(drive);
1524 /* get drive type */
1525 for (type = 0; type < num_dr_types; type++)
1526 if (drive_types[type].code == code)
1527 break;
1529 if (type >= num_dr_types) {
1530 printk(KERN_WARNING "fd_probe: unsupported drive type "
1531 "%08lx found\n", code);
1532 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1533 return;
1536 unit[drive].type = drive_types + type;
1537 unit[drive].track = -1;
1539 unit[drive].disk = -1;
1540 unit[drive].motor = 0;
1541 unit[drive].busy = 0;
1542 unit[drive].status = -1;
1546 * floppy_open check for aliasing (/dev/fd0 can be the same as
1547 * /dev/PS0 etc), and disallows simultaneous access to the same
1548 * drive with different device numbers.
1550 static int floppy_open(struct inode *inode, struct file *filp)
1552 int drive = iminor(inode) & 3;
1553 int system = (iminor(inode) & 4) >> 2;
1554 int old_dev;
1555 unsigned long flags;
1557 old_dev = fd_device[drive];
1559 if (fd_ref[drive] && old_dev != system)
1560 return -EBUSY;
1562 if (filp && filp->f_mode & 3) {
1563 check_disk_change(inode->i_bdev);
1564 if (filp->f_mode & 2 ) {
1565 int wrprot;
1567 get_fdc(drive);
1568 fd_select (drive);
1569 wrprot = !(ciaa.pra & DSKPROT);
1570 fd_deselect (drive);
1571 rel_fdc();
1573 if (wrprot)
1574 return -EROFS;
1578 local_irq_save(flags);
1579 fd_ref[drive]++;
1580 fd_device[drive] = system;
1581 local_irq_restore(flags);
1583 unit[drive].dtype=&data_types[system];
1584 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1585 data_types[system].sects*unit[drive].type->sect_mult;
1586 set_capacity(unit[drive].gendisk, unit[drive].blocks);
1588 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1589 unit[drive].type->name, data_types[system].name);
1591 return 0;
1594 static int floppy_release(struct inode * inode, struct file * filp)
1596 int drive = iminor(inode) & 3;
1598 if (unit[drive].dirty == 1) {
1599 del_timer (flush_track_timer + drive);
1600 non_int_flush_track (drive);
1603 if (!fd_ref[drive]--) {
1604 printk(KERN_CRIT "floppy_release with fd_ref == 0");
1605 fd_ref[drive] = 0;
1607 #ifdef MODULE
1608 /* the mod_use counter is handled this way */
1609 floppy_off (drive | 0x40000000);
1610 #endif
1611 return 0;
1615 * floppy-change is never called from an interrupt, so we can relax a bit
1616 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1617 * to the desired drive, but it will probably not survive the sleep if
1618 * several floppies are used at the same time: thus the loop.
1620 static int amiga_floppy_change(struct gendisk *disk)
1622 struct amiga_floppy_struct *p = disk->private_data;
1623 int drive = p - unit;
1624 int changed;
1625 static int first_time = 1;
1627 if (first_time)
1628 changed = first_time--;
1629 else {
1630 get_fdc(drive);
1631 fd_select (drive);
1632 changed = !(ciaa.pra & DSKCHANGE);
1633 fd_deselect (drive);
1634 rel_fdc();
1637 if (changed) {
1638 fd_probe(drive);
1639 p->track = -1;
1640 p->dirty = 0;
1641 writepending = 0; /* if this was true before, too bad! */
1642 writefromint = 0;
1643 return 1;
1645 return 0;
1648 static struct block_device_operations floppy_fops = {
1649 .owner = THIS_MODULE,
1650 .open = floppy_open,
1651 .release = floppy_release,
1652 .ioctl = fd_ioctl,
1653 .getgeo = fd_getgeo,
1654 .media_changed = amiga_floppy_change,
1657 static int __init fd_probe_drives(void)
1659 int drive,drives,nomem;
1661 printk(KERN_INFO "FD: probing units\n" KERN_INFO "found ");
1662 drives=0;
1663 nomem=0;
1664 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1665 struct gendisk *disk;
1666 fd_probe(drive);
1667 if (unit[drive].type->code == FD_NODRIVE)
1668 continue;
1669 disk = alloc_disk(1);
1670 if (!disk) {
1671 unit[drive].type->code = FD_NODRIVE;
1672 continue;
1674 unit[drive].gendisk = disk;
1675 drives++;
1676 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
1677 printk("no mem for ");
1678 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
1679 drives--;
1680 nomem = 1;
1682 printk("fd%d ",drive);
1683 disk->major = FLOPPY_MAJOR;
1684 disk->first_minor = drive;
1685 disk->fops = &floppy_fops;
1686 sprintf(disk->disk_name, "fd%d", drive);
1687 disk->private_data = &unit[drive];
1688 disk->queue = floppy_queue;
1689 set_capacity(disk, 880*2);
1690 add_disk(disk);
1692 if ((drives > 0) || (nomem == 0)) {
1693 if (drives == 0)
1694 printk("no drives");
1695 printk("\n");
1696 return drives;
1698 printk("\n");
1699 return -ENOMEM;
1702 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1704 int drive = *part & 3;
1705 if (unit[drive].type->code == FD_NODRIVE)
1706 return NULL;
1707 *part = 0;
1708 return get_disk(unit[drive].gendisk);
1711 int __init amiga_floppy_init(void)
1713 int i, ret;
1715 if (!AMIGAHW_PRESENT(AMI_FLOPPY))
1716 return -ENXIO;
1718 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1719 return -EBUSY;
1722 * We request DSKPTR, DSKLEN and DSKDATA only, because the other
1723 * floppy registers are too spreaded over the custom register space
1725 ret = -EBUSY;
1726 if (!request_mem_region(CUSTOM_PHYSADDR+0x20, 8, "amiflop [Paula]")) {
1727 printk("fd: cannot get floppy registers\n");
1728 goto out_blkdev;
1731 ret = -ENOMEM;
1732 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE, "Floppy")) ==
1733 NULL) {
1734 printk("fd: cannot get chip mem buffer\n");
1735 goto out_memregion;
1738 ret = -EBUSY;
1739 if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
1740 printk("fd: cannot get irq for dma\n");
1741 goto out_irq;
1744 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
1745 printk("fd: cannot get irq for timer\n");
1746 goto out_irq2;
1749 ret = -ENOMEM;
1750 floppy_queue = blk_init_queue(do_fd_request, &amiflop_lock);
1751 if (!floppy_queue)
1752 goto out_queue;
1754 ret = -ENXIO;
1755 if (fd_probe_drives() < 1) /* No usable drives */
1756 goto out_probe;
1758 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1759 floppy_find, NULL, NULL);
1761 /* initialize variables */
1762 init_timer(&motor_on_timer);
1763 motor_on_timer.expires = 0;
1764 motor_on_timer.data = 0;
1765 motor_on_timer.function = motor_on_callback;
1766 for (i = 0; i < FD_MAX_UNITS; i++) {
1767 init_timer(&motor_off_timer[i]);
1768 motor_off_timer[i].expires = 0;
1769 motor_off_timer[i].data = i|0x80000000;
1770 motor_off_timer[i].function = fd_motor_off;
1771 init_timer(&flush_track_timer[i]);
1772 flush_track_timer[i].expires = 0;
1773 flush_track_timer[i].data = i;
1774 flush_track_timer[i].function = flush_track_callback;
1776 unit[i].track = -1;
1779 init_timer(&post_write_timer);
1780 post_write_timer.expires = 0;
1781 post_write_timer.data = 0;
1782 post_write_timer.function = post_write;
1784 for (i = 0; i < 128; i++)
1785 mfmdecode[i]=255;
1786 for (i = 0; i < 16; i++)
1787 mfmdecode[mfmencode[i]]=i;
1789 /* make sure that disk DMA is enabled */
1790 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1792 /* init ms timer */
1793 ciaa.crb = 8; /* one-shot, stop */
1794 return 0;
1796 out_probe:
1797 blk_cleanup_queue(floppy_queue);
1798 out_queue:
1799 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1800 out_irq2:
1801 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1802 out_irq:
1803 amiga_chip_free(raw_buf);
1804 out_memregion:
1805 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1806 out_blkdev:
1807 unregister_blkdev(FLOPPY_MAJOR,"fd");
1808 return ret;
1811 #ifdef MODULE
1813 int init_module(void)
1815 if (!MACH_IS_AMIGA)
1816 return -ENXIO;
1817 return amiga_floppy_init();
1820 #if 0 /* not safe to unload */
1821 void cleanup_module(void)
1823 int i;
1825 for( i = 0; i < FD_MAX_UNITS; i++) {
1826 if (unit[i].type->code != FD_NODRIVE) {
1827 del_gendisk(unit[i].gendisk);
1828 put_disk(unit[i].gendisk);
1829 kfree(unit[i].trackbuf);
1832 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
1833 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1834 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1835 custom.dmacon = DMAF_DISK; /* disable DMA */
1836 amiga_chip_free(raw_buf);
1837 blk_cleanup_queue(floppy_queue);
1838 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1839 unregister_blkdev(FLOPPY_MAJOR, "fd");
1841 #endif
1843 #else
1844 static int __init amiga_floppy_setup (char *str)
1846 int n;
1847 if (!MACH_IS_AMIGA)
1848 return 0;
1849 if (!get_option(&str, &n))
1850 return 0;
1851 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", n);
1852 fd_def_df0 = n;
1853 return 1;
1856 __setup("floppy=", amiga_floppy_setup);
1857 #endif