First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / block / amiflop.c
blob0552258390240ae8f71ebf89d09c4542eff749e9
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>
67 #include <linux/interrupt.h>
69 #include <asm/setup.h>
70 #include <asm/uaccess.h>
71 #include <asm/amigahw.h>
72 #include <asm/amigaints.h>
73 #include <asm/irq.h>
75 #undef DEBUG /* print _LOTS_ of infos */
77 #define RAW_IOCTL
78 #ifdef RAW_IOCTL
79 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
80 #endif
83 * Defines
87 * Error codes
89 #define FD_OK 0 /* operation succeeded */
90 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
91 #define FD_NOUNIT 1 /* unit does not exist */
92 #define FD_UNITBUSY 2 /* unit already active */
93 #define FD_NOTACTIVE 3 /* unit is not active */
94 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
96 #define MFM_NOSYNC 1
97 #define MFM_HEADER 2
98 #define MFM_DATA 3
99 #define MFM_TRACK 4
102 * Floppy ID values
104 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
105 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
106 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
107 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
109 static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
111 module_param(fd_def_df0, ulong, 0);
112 MODULE_LICENSE("GPL");
114 static struct request_queue *floppy_queue;
117 * Macros
119 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
120 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
121 #define SELECT(mask) (ciab.prb &= ~mask)
122 #define DESELECT(mask) (ciab.prb |= mask)
123 #define SELMASK(drive) (1 << (3 + (drive & 3)))
125 static struct fd_drive_type drive_types[] = {
126 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
127 /* warning: times are now in milliseconds (ms) */
128 { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
129 { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
130 { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
131 { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
133 static int num_dr_types = ARRAY_SIZE(drive_types);
135 static int amiga_read(int), dos_read(int);
136 static void amiga_write(int), dos_write(int);
137 static struct fd_data_type data_types[] = {
138 { "Amiga", 11 , amiga_read, amiga_write},
139 { "MS-Dos", 9, dos_read, dos_write}
142 /* current info on each unit */
143 static struct amiga_floppy_struct unit[FD_MAX_UNITS];
145 static struct timer_list flush_track_timer[FD_MAX_UNITS];
146 static struct timer_list post_write_timer;
147 static struct timer_list motor_on_timer;
148 static struct timer_list motor_off_timer[FD_MAX_UNITS];
149 static int on_attempts;
151 /* Synchronization of FDC access */
152 /* request loop (trackbuffer) */
153 static volatile int fdc_busy = -1;
154 static volatile int fdc_nested;
155 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
157 static DECLARE_COMPLETION(motor_on_completion);
159 static volatile int selected = -1; /* currently selected drive */
161 static int writepending;
162 static int writefromint;
163 static char *raw_buf;
165 static DEFINE_SPINLOCK(amiflop_lock);
167 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
170 * These are global variables, as that's the easiest way to give
171 * information to interrupts. They are the data used for the current
172 * request.
174 static volatile char block_flag;
175 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
177 /* MS-Dos MFM Coding tables (should go quick and easy) */
178 static unsigned char mfmencode[16]={
179 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
180 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
182 static unsigned char mfmdecode[128];
184 /* floppy internal millisecond timer stuff */
185 static DECLARE_COMPLETION(ms_wait_completion);
186 #define MS_TICKS ((amiga_eclock+50)/1000)
189 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
190 * max X times - some types of errors increase the errorcount by 2 or
191 * even 3, so we might actually retry only X/2 times before giving up.
193 #define MAX_ERRORS 12
195 #define custom amiga_custom
197 /* Prevent "aliased" accesses. */
198 static int fd_ref[4] = { 0,0,0,0 };
199 static int fd_device[4] = { 0, 0, 0, 0 };
202 * Here come the actual hardware access and helper functions.
203 * They are not reentrant and single threaded because all drives
204 * share the same hardware and the same trackbuffer.
207 /* Milliseconds timer */
209 static irqreturn_t ms_isr(int irq, void *dummy)
211 complete(&ms_wait_completion);
212 return IRQ_HANDLED;
215 /* all waits are queued up
216 A more generic routine would do a schedule a la timer.device */
217 static void ms_delay(int ms)
219 int ticks;
220 static DEFINE_MUTEX(mutex);
222 if (ms > 0) {
223 mutex_lock(&mutex);
224 ticks = MS_TICKS*ms-1;
225 ciaa.tblo=ticks%256;
226 ciaa.tbhi=ticks/256;
227 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
228 wait_for_completion(&ms_wait_completion);
229 mutex_unlock(&mutex);
233 /* Hardware semaphore */
235 /* returns true when we would get the semaphore */
236 static inline int try_fdc(int drive)
238 drive &= 3;
239 return ((fdc_busy < 0) || (fdc_busy == drive));
242 static void get_fdc(int drive)
244 unsigned long flags;
246 drive &= 3;
247 #ifdef DEBUG
248 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
249 #endif
250 local_irq_save(flags);
251 wait_event(fdc_wait, try_fdc(drive));
252 fdc_busy = drive;
253 fdc_nested++;
254 local_irq_restore(flags);
257 static inline void rel_fdc(void)
259 #ifdef DEBUG
260 if (fdc_nested == 0)
261 printk("fd: unmatched rel_fdc\n");
262 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
263 #endif
264 fdc_nested--;
265 if (fdc_nested == 0) {
266 fdc_busy = -1;
267 wake_up(&fdc_wait);
271 static void fd_select (int drive)
273 unsigned char prb = ~0;
275 drive&=3;
276 #ifdef DEBUG
277 printk("selecting %d\n",drive);
278 #endif
279 if (drive == selected)
280 return;
281 get_fdc(drive);
282 selected = drive;
284 if (unit[drive].track % 2 != 0)
285 prb &= ~DSKSIDE;
286 if (unit[drive].motor == 1)
287 prb &= ~DSKMOTOR;
288 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
289 ciab.prb = prb;
290 prb &= ~SELMASK(drive);
291 ciab.prb = prb;
292 rel_fdc();
295 static void fd_deselect (int drive)
297 unsigned char prb;
298 unsigned long flags;
300 drive&=3;
301 #ifdef DEBUG
302 printk("deselecting %d\n",drive);
303 #endif
304 if (drive != selected) {
305 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
306 return;
309 get_fdc(drive);
310 local_irq_save(flags);
312 selected = -1;
314 prb = ciab.prb;
315 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
316 ciab.prb = prb;
318 local_irq_restore (flags);
319 rel_fdc();
323 static void motor_on_callback(unsigned long nr)
325 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
326 complete_all(&motor_on_completion);
327 } else {
328 motor_on_timer.expires = jiffies + HZ/10;
329 add_timer(&motor_on_timer);
333 static int fd_motor_on(int nr)
335 nr &= 3;
337 del_timer(motor_off_timer + nr);
339 if (!unit[nr].motor) {
340 unit[nr].motor = 1;
341 fd_select(nr);
343 INIT_COMPLETION(motor_on_completion);
344 motor_on_timer.data = nr;
345 mod_timer(&motor_on_timer, jiffies + HZ/2);
347 on_attempts = 10;
348 wait_for_completion(&motor_on_completion);
349 fd_deselect(nr);
352 if (on_attempts == 0) {
353 on_attempts = -1;
354 #if 0
355 printk (KERN_ERR "motor_on failed, turning motor off\n");
356 fd_motor_off (nr);
357 return 0;
358 #else
359 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
360 #endif
363 return 1;
366 static void fd_motor_off(unsigned long drive)
368 long calledfromint;
369 #ifdef MODULE
370 long decusecount;
372 decusecount = drive & 0x40000000;
373 #endif
374 calledfromint = drive & 0x80000000;
375 drive&=3;
376 if (calledfromint && !try_fdc(drive)) {
377 /* We would be blocked in an interrupt, so try again later */
378 motor_off_timer[drive].expires = jiffies + 1;
379 add_timer(motor_off_timer + drive);
380 return;
382 unit[drive].motor = 0;
383 fd_select(drive);
384 udelay (1);
385 fd_deselect(drive);
388 static void floppy_off (unsigned int nr)
390 int drive;
392 drive = nr & 3;
393 /* called this way it is always from interrupt */
394 motor_off_timer[drive].data = nr | 0x80000000;
395 mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
398 static int fd_calibrate(int drive)
400 unsigned char prb;
401 int n;
403 drive &= 3;
404 get_fdc(drive);
405 if (!fd_motor_on (drive))
406 return 0;
407 fd_select (drive);
408 prb = ciab.prb;
409 prb |= DSKSIDE;
410 prb &= ~DSKDIREC;
411 ciab.prb = prb;
412 for (n = unit[drive].type->tracks/2; n != 0; --n) {
413 if (ciaa.pra & DSKTRACK0)
414 break;
415 prb &= ~DSKSTEP;
416 ciab.prb = prb;
417 prb |= DSKSTEP;
418 udelay (2);
419 ciab.prb = prb;
420 ms_delay(unit[drive].type->step_delay);
422 ms_delay (unit[drive].type->settle_time);
423 prb |= DSKDIREC;
424 n = unit[drive].type->tracks + 20;
425 for (;;) {
426 prb &= ~DSKSTEP;
427 ciab.prb = prb;
428 prb |= DSKSTEP;
429 udelay (2);
430 ciab.prb = prb;
431 ms_delay(unit[drive].type->step_delay + 1);
432 if ((ciaa.pra & DSKTRACK0) == 0)
433 break;
434 if (--n == 0) {
435 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
436 fd_motor_off (drive);
437 unit[drive].track = -1;
438 rel_fdc();
439 return 0;
442 unit[drive].track = 0;
443 ms_delay(unit[drive].type->settle_time);
445 rel_fdc();
446 fd_deselect(drive);
447 return 1;
450 static int fd_seek(int drive, int track)
452 unsigned char prb;
453 int cnt;
455 #ifdef DEBUG
456 printk("seeking drive %d to track %d\n",drive,track);
457 #endif
458 drive &= 3;
459 get_fdc(drive);
460 if (unit[drive].track == track) {
461 rel_fdc();
462 return 1;
464 if (!fd_motor_on(drive)) {
465 rel_fdc();
466 return 0;
468 if (unit[drive].track < 0 && !fd_calibrate(drive)) {
469 rel_fdc();
470 return 0;
473 fd_select (drive);
474 cnt = unit[drive].track/2 - track/2;
475 prb = ciab.prb;
476 prb |= DSKSIDE | DSKDIREC;
477 if (track % 2 != 0)
478 prb &= ~DSKSIDE;
479 if (cnt < 0) {
480 cnt = - cnt;
481 prb &= ~DSKDIREC;
483 ciab.prb = prb;
484 if (track % 2 != unit[drive].track % 2)
485 ms_delay (unit[drive].type->side_time);
486 unit[drive].track = track;
487 if (cnt == 0) {
488 rel_fdc();
489 fd_deselect(drive);
490 return 1;
492 do {
493 prb &= ~DSKSTEP;
494 ciab.prb = prb;
495 prb |= DSKSTEP;
496 udelay (1);
497 ciab.prb = prb;
498 ms_delay (unit[drive].type->step_delay);
499 } while (--cnt != 0);
500 ms_delay (unit[drive].type->settle_time);
502 rel_fdc();
503 fd_deselect(drive);
504 return 1;
507 static unsigned long fd_get_drive_id(int drive)
509 int i;
510 ulong id = 0;
512 drive&=3;
513 get_fdc(drive);
514 /* set up for ID */
515 MOTOR_ON;
516 udelay(2);
517 SELECT(SELMASK(drive));
518 udelay(2);
519 DESELECT(SELMASK(drive));
520 udelay(2);
521 MOTOR_OFF;
522 udelay(2);
523 SELECT(SELMASK(drive));
524 udelay(2);
525 DESELECT(SELMASK(drive));
526 udelay(2);
528 /* loop and read disk ID */
529 for (i=0; i<32; i++) {
530 SELECT(SELMASK(drive));
531 udelay(2);
533 /* read and store value of DSKRDY */
534 id <<= 1;
535 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
537 DESELECT(SELMASK(drive));
540 rel_fdc();
543 * RB: At least A500/A2000's df0: don't identify themselves.
544 * As every (real) Amiga has at least a 3.5" DD drive as df0:
545 * we default to that if df0: doesn't identify as a certain
546 * type.
548 if(drive == 0 && id == FD_NODRIVE)
550 id = fd_def_df0;
551 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
553 /* return the ID value */
554 return (id);
557 static irqreturn_t fd_block_done(int irq, void *dummy)
559 if (block_flag)
560 custom.dsklen = 0x4000;
562 if (block_flag == 2) { /* writing */
563 writepending = 2;
564 post_write_timer.expires = jiffies + 1; /* at least 2 ms */
565 post_write_timer.data = selected;
566 add_timer(&post_write_timer);
568 else { /* reading */
569 block_flag = 0;
570 wake_up (&wait_fd_block);
572 return IRQ_HANDLED;
575 static void raw_read(int drive)
577 drive&=3;
578 get_fdc(drive);
579 wait_event(wait_fd_block, !block_flag);
580 fd_select(drive);
581 /* setup adkcon bits correctly */
582 custom.adkcon = ADK_MSBSYNC;
583 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
585 custom.dsksync = MFM_SYNC;
587 custom.dsklen = 0;
588 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
589 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
590 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
592 block_flag = 1;
594 wait_event(wait_fd_block, !block_flag);
596 custom.dsklen = 0;
597 fd_deselect(drive);
598 rel_fdc();
601 static int raw_write(int drive)
603 ushort adk;
605 drive&=3;
606 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
607 if ((ciaa.pra & DSKPROT) == 0) {
608 rel_fdc();
609 return 0;
611 wait_event(wait_fd_block, !block_flag);
612 fd_select(drive);
613 /* clear adkcon bits */
614 custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
615 /* set appropriate adkcon bits */
616 adk = ADK_SETCLR|ADK_FAST;
617 if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
618 adk |= ADK_PRECOMP1;
619 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
620 adk |= ADK_PRECOMP0;
621 custom.adkcon = adk;
623 custom.dsklen = DSKLEN_WRITE;
624 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
625 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
626 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
628 block_flag = 2;
629 return 1;
633 * to be called at least 2ms after the write has finished but before any
634 * other access to the hardware.
636 static void post_write (unsigned long drive)
638 #ifdef DEBUG
639 printk("post_write for drive %ld\n",drive);
640 #endif
641 drive &= 3;
642 custom.dsklen = 0;
643 block_flag = 0;
644 writepending = 0;
645 writefromint = 0;
646 unit[drive].dirty = 0;
647 wake_up(&wait_fd_block);
648 fd_deselect(drive);
649 rel_fdc(); /* corresponds to get_fdc() in raw_write */
654 * The following functions are to convert the block contents into raw data
655 * written to disk and vice versa.
656 * (Add other formats here ;-))
659 static unsigned long scan_sync(unsigned long raw, unsigned long end)
661 ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
663 while (ptr < endp && *ptr++ != 0x4489)
665 if (ptr < endp) {
666 while (*ptr == 0x4489 && ptr < endp)
667 ptr++;
668 return (ulong)ptr;
670 return 0;
673 static inline unsigned long checksum(unsigned long *addr, int len)
675 unsigned long csum = 0;
677 len /= sizeof(*addr);
678 while (len-- > 0)
679 csum ^= *addr++;
680 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
682 return csum;
685 static unsigned long decode (unsigned long *data, unsigned long *raw,
686 int len)
688 ulong *odd, *even;
690 /* convert length from bytes to longwords */
691 len >>= 2;
692 odd = raw;
693 even = odd + len;
695 /* prepare return pointer */
696 raw += len * 2;
698 do {
699 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
700 } while (--len != 0);
702 return (ulong)raw;
705 struct header {
706 unsigned char magic;
707 unsigned char track;
708 unsigned char sect;
709 unsigned char ord;
710 unsigned char labels[16];
711 unsigned long hdrchk;
712 unsigned long datachk;
715 static int amiga_read(int drive)
717 unsigned long raw;
718 unsigned long end;
719 int scnt;
720 unsigned long csum;
721 struct header hdr;
723 drive&=3;
724 raw = (long) raw_buf;
725 end = raw + unit[drive].type->read_size;
727 for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
728 if (!(raw = scan_sync(raw, end))) {
729 printk (KERN_INFO "can't find sync for sector %d\n", scnt);
730 return MFM_NOSYNC;
733 raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
734 raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
735 raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
736 raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
737 csum = checksum((ulong *)&hdr,
738 (char *)&hdr.hdrchk-(char *)&hdr);
740 #ifdef DEBUG
741 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
742 hdr.magic, hdr.track, hdr.sect, hdr.ord,
743 *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
744 *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
745 hdr.hdrchk, hdr.datachk);
746 #endif
748 if (hdr.hdrchk != csum) {
749 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
750 return MFM_HEADER;
753 /* verify track */
754 if (hdr.track != unit[drive].track) {
755 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
756 return MFM_TRACK;
759 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
760 (ulong *)raw, 512);
761 csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
763 if (hdr.datachk != csum) {
764 printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
765 hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
766 hdr.datachk, csum);
767 printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
768 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
769 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
770 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
771 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
772 return MFM_DATA;
776 return 0;
779 static void encode(unsigned long data, unsigned long *dest)
781 unsigned long data2;
783 data &= 0x55555555;
784 data2 = data ^ 0x55555555;
785 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
787 if (*(dest - 1) & 0x00000001)
788 data &= 0x7FFFFFFF;
790 *dest = data;
793 static void encode_block(unsigned long *dest, unsigned long *src, int len)
795 int cnt, to_cnt = 0;
796 unsigned long data;
798 /* odd bits */
799 for (cnt = 0; cnt < len / 4; cnt++) {
800 data = src[cnt] >> 1;
801 encode(data, dest + to_cnt++);
804 /* even bits */
805 for (cnt = 0; cnt < len / 4; cnt++) {
806 data = src[cnt];
807 encode(data, dest + to_cnt++);
811 static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
813 struct header hdr;
814 int i;
816 disk&=3;
817 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
818 raw++;
819 *raw++ = 0x44894489;
821 hdr.magic = 0xFF;
822 hdr.track = unit[disk].track;
823 hdr.sect = cnt;
824 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
825 for (i = 0; i < 16; i++)
826 hdr.labels[i] = 0;
827 hdr.hdrchk = checksum((ulong *)&hdr,
828 (char *)&hdr.hdrchk-(char *)&hdr);
829 hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
831 encode_block(raw, (ulong *)&hdr.magic, 4);
832 raw += 2;
833 encode_block(raw, (ulong *)&hdr.labels, 16);
834 raw += 8;
835 encode_block(raw, (ulong *)&hdr.hdrchk, 4);
836 raw += 2;
837 encode_block(raw, (ulong *)&hdr.datachk, 4);
838 raw += 2;
839 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
840 raw += 256;
842 return raw;
845 static void amiga_write(int disk)
847 unsigned int cnt;
848 unsigned long *ptr = (unsigned long *)raw_buf;
850 disk&=3;
851 /* gap space */
852 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
853 *ptr++ = 0xaaaaaaaa;
855 /* sectors */
856 for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
857 ptr = putsec (disk, ptr, cnt);
858 *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
862 struct dos_header {
863 unsigned char track, /* 0-80 */
864 side, /* 0-1 */
865 sec, /* 0-...*/
866 len_desc;/* 2 */
867 unsigned short crc; /* on 68000 we got an alignment problem,
868 but this compiler solves it by adding silently
869 adding a pad byte so data won't fit
870 and this took about 3h to discover.... */
871 unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
874 /* crc routines are borrowed from the messydos-handler */
876 /* excerpt from the messydos-device
877 ; The CRC is computed not only over the actual data, but including
878 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
879 ; As we don't read or encode these fields into our buffers, we have to
880 ; preload the registers containing the CRC with the values they would have
881 ; after stepping over these fields.
883 ; How CRCs "really" work:
885 ; First, you should regard a bitstring as a series of coefficients of
886 ; polynomials. We calculate with these polynomials in modulo-2
887 ; arithmetic, in which both add and subtract are done the same as
888 ; exclusive-or. Now, we modify our data (a very long polynomial) in
889 ; such a way that it becomes divisible by the CCITT-standard 16-bit
890 ; 16 12 5
891 ; polynomial: x + x + x + 1, represented by $11021. The easiest
892 ; way to do this would be to multiply (using proper arithmetic) our
893 ; datablock with $11021. So we have:
894 ; data * $11021 =
895 ; data * ($10000 + $1021) =
896 ; data * $10000 + data * $1021
897 ; The left part of this is simple: Just add two 0 bytes. But then
898 ; the right part (data $1021) remains difficult and even could have
899 ; a carry into the left part. The solution is to use a modified
900 ; multiplication, which has a result that is not correct, but with
901 ; a difference of any multiple of $11021. We then only need to keep
902 ; the 16 least significant bits of the result.
904 ; The following algorithm does this for us:
906 ; unsigned char *data, c, crclo, crchi;
907 ; while (not done) {
908 ; c = *data++ + crchi;
909 ; crchi = (@ c) >> 8 + crclo;
910 ; crclo = @ c;
913 ; Remember, + is done with EOR, the @ operator is in two tables (high
914 ; and low byte separately), which is calculated as
916 ; $1021 * (c & $F0)
917 ; xor $1021 * (c & $0F)
918 ; xor $1021 * (c >> 4) (* is regular multiplication)
921 ; Anyway, the end result is the same as the remainder of the division of
922 ; the data by $11021. I am afraid I need to study theory a bit more...
925 my only works was to code this from manx to C....
929 static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
931 static unsigned char CRCTable1[] = {
932 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
933 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
934 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
935 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
936 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
937 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
938 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
939 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
940 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
941 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
942 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
943 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
944 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
945 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
946 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
947 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
950 static unsigned char CRCTable2[] = {
951 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
952 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
953 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
954 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
955 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
956 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
957 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
958 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
959 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
960 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
961 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
962 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
963 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
964 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
965 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
966 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
969 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
970 register int i;
971 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
973 CRCT1=CRCTable1;
974 CRCT2=CRCTable2;
975 data=data_a3;
976 crcl=data_d1;
977 crch=data_d0;
978 for (i=data_d3; i>=0; i--) {
979 c = (*data++) ^ crch;
980 crch = CRCT1[c] ^ crcl;
981 crcl = CRCT2[c];
983 return (crch<<8)|crcl;
986 static inline ushort dos_hdr_crc (struct dos_header *hdr)
988 return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
991 static inline ushort dos_data_crc(unsigned char *data)
993 return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
996 static inline unsigned char dos_decode_byte(ushort word)
998 register ushort w2;
999 register unsigned char byte;
1000 register unsigned char *dec = mfmdecode;
1002 w2=word;
1003 w2>>=8;
1004 w2&=127;
1005 byte = dec[w2];
1006 byte <<= 4;
1007 w2 = word & 127;
1008 byte |= dec[w2];
1009 return byte;
1012 static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
1014 int i;
1016 for (i = 0; i < len; i++)
1017 *data++=dos_decode_byte(*raw++);
1018 return ((ulong)raw);
1021 #ifdef DEBUG
1022 static void dbg(unsigned long ptr)
1024 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
1025 ((ulong *)ptr)[0], ((ulong *)ptr)[1],
1026 ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
1028 #endif
1030 static int dos_read(int drive)
1032 unsigned long end;
1033 unsigned long raw;
1034 int scnt;
1035 unsigned short crc,data_crc[2];
1036 struct dos_header hdr;
1038 drive&=3;
1039 raw = (long) raw_buf;
1040 end = raw + unit[drive].type->read_size;
1042 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1043 do { /* search for the right sync of each sec-hdr */
1044 if (!(raw = scan_sync (raw, end))) {
1045 printk(KERN_INFO "dos_read: no hdr sync on "
1046 "track %d, unit %d for sector %d\n",
1047 unit[drive].track,drive,scnt);
1048 return MFM_NOSYNC;
1050 #ifdef DEBUG
1051 dbg(raw);
1052 #endif
1053 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1054 raw+=2; /* skip over headermark */
1055 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1056 crc = dos_hdr_crc(&hdr);
1058 #ifdef DEBUG
1059 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1060 hdr.sec, hdr.len_desc, hdr.crc);
1061 #endif
1063 if (crc != hdr.crc) {
1064 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
1065 hdr.crc, crc);
1066 return MFM_HEADER;
1068 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1069 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
1070 hdr.track,
1071 unit[drive].track/unit[drive].type->heads);
1072 return MFM_TRACK;
1075 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1076 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
1077 hdr.side,
1078 unit[drive].track%unit[drive].type->heads);
1079 return MFM_TRACK;
1082 if (hdr.len_desc != 2) {
1083 printk(KERN_INFO "dos_read: unknown sector len "
1084 "descriptor %d\n", hdr.len_desc);
1085 return MFM_DATA;
1087 #ifdef DEBUG
1088 printk("hdr accepted\n");
1089 #endif
1090 if (!(raw = scan_sync (raw, end))) {
1091 printk(KERN_INFO "dos_read: no data sync on track "
1092 "%d, unit %d for sector%d, disk sector %d\n",
1093 unit[drive].track, drive, scnt, hdr.sec);
1094 return MFM_NOSYNC;
1096 #ifdef DEBUG
1097 dbg(raw);
1098 #endif
1100 if (*((ushort *)raw)!=0x5545) {
1101 printk(KERN_INFO "dos_read: no data mark after "
1102 "sync (%d,%d,%d,%d) sc=%d\n",
1103 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1104 return MFM_NOSYNC;
1107 raw+=2; /* skip data mark (included in checksum) */
1108 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1109 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1110 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1112 if (crc != data_crc[0]) {
1113 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
1114 "sc=%d, %x %x\n", hdr.track, hdr.side,
1115 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
1116 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1117 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1118 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1119 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1120 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1121 return MFM_DATA;
1124 return 0;
1127 static inline ushort dos_encode_byte(unsigned char byte)
1129 register unsigned char *enc, b2, b1;
1130 register ushort word;
1132 enc=mfmencode;
1133 b1=byte;
1134 b2=b1>>4;
1135 b1&=15;
1136 word=enc[b2] <<8 | enc [b1];
1137 return (word|((word&(256|64)) ? 0: 128));
1140 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1142 int i;
1144 for (i = 0; i < len; i++) {
1145 *dest=dos_encode_byte(*src++);
1146 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1147 dest++;
1151 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1153 static struct dos_header hdr={0,0,0,2,0,
1154 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1155 int i;
1156 static ushort crc[2]={0,0x4e4e};
1158 drive&=3;
1159 /* id gap 1 */
1160 /* the MFM word before is always 9254 */
1161 for(i=0;i<6;i++)
1162 *raw++=0xaaaaaaaa;
1163 /* 3 sync + 1 headermark */
1164 *raw++=0x44894489;
1165 *raw++=0x44895554;
1167 /* fill in the variable parts of the header */
1168 hdr.track=unit[drive].track/unit[drive].type->heads;
1169 hdr.side=unit[drive].track%unit[drive].type->heads;
1170 hdr.sec=cnt+1;
1171 hdr.crc=dos_hdr_crc(&hdr);
1173 /* header (without "magic") and id gap 2*/
1174 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1175 raw+=14;
1177 /*id gap 3 */
1178 for(i=0;i<6;i++)
1179 *raw++=0xaaaaaaaa;
1181 /* 3 syncs and 1 datamark */
1182 *raw++=0x44894489;
1183 *raw++=0x44895545;
1185 /* data */
1186 dos_encode_block((ushort *)raw,
1187 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
1188 raw+=256;
1190 /*data crc + jd's special gap (long words :-/) */
1191 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1192 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1193 raw+=2;
1195 /* data gap */
1196 for(i=0;i<38;i++)
1197 *raw++=0x92549254;
1199 return raw; /* wrote 652 MFM words */
1202 static void dos_write(int disk)
1204 int cnt;
1205 unsigned long raw = (unsigned long) raw_buf;
1206 unsigned long *ptr=(unsigned long *)raw;
1208 disk&=3;
1209 /* really gap4 + indexgap , but we write it first and round it up */
1210 for (cnt=0;cnt<425;cnt++)
1211 *ptr++=0x92549254;
1213 /* the following is just guessed */
1214 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1215 for(cnt=0;cnt<473;cnt++)
1216 *ptr++=0x92549254;
1218 /* now the index marks...*/
1219 for (cnt=0;cnt<20;cnt++)
1220 *ptr++=0x92549254;
1221 for (cnt=0;cnt<6;cnt++)
1222 *ptr++=0xaaaaaaaa;
1223 *ptr++=0x52245224;
1224 *ptr++=0x52245552;
1225 for (cnt=0;cnt<20;cnt++)
1226 *ptr++=0x92549254;
1228 /* sectors */
1229 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1230 ptr=ms_putsec(disk,ptr,cnt);
1232 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1236 * Here comes the high level stuff (i.e. the filesystem interface)
1237 * and helper functions.
1238 * Normally this should be the only part that has to be adapted to
1239 * different kernel versions.
1242 /* FIXME: this assumes the drive is still spinning -
1243 * which is only true if we complete writing a track within three seconds
1245 static void flush_track_callback(unsigned long nr)
1247 nr&=3;
1248 writefromint = 1;
1249 if (!try_fdc(nr)) {
1250 /* we might block in an interrupt, so try again later */
1251 flush_track_timer[nr].expires = jiffies + 1;
1252 add_timer(flush_track_timer + nr);
1253 return;
1255 get_fdc(nr);
1256 (*unit[nr].dtype->write_fkt)(nr);
1257 if (!raw_write(nr)) {
1258 printk (KERN_NOTICE "floppy disk write protected\n");
1259 writefromint = 0;
1260 writepending = 0;
1262 rel_fdc();
1265 static int non_int_flush_track (unsigned long nr)
1267 unsigned long flags;
1269 nr&=3;
1270 writefromint = 0;
1271 del_timer(&post_write_timer);
1272 get_fdc(nr);
1273 if (!fd_motor_on(nr)) {
1274 writepending = 0;
1275 rel_fdc();
1276 return 0;
1278 local_irq_save(flags);
1279 if (writepending != 2) {
1280 local_irq_restore(flags);
1281 (*unit[nr].dtype->write_fkt)(nr);
1282 if (!raw_write(nr)) {
1283 printk (KERN_NOTICE "floppy disk write protected "
1284 "in write!\n");
1285 writepending = 0;
1286 return 0;
1288 wait_event(wait_fd_block, block_flag != 2);
1290 else {
1291 local_irq_restore(flags);
1292 ms_delay(2); /* 2 ms post_write delay */
1293 post_write(nr);
1295 rel_fdc();
1296 return 1;
1299 static int get_track(int drive, int track)
1301 int error, errcnt;
1303 drive&=3;
1304 if (unit[drive].track == track)
1305 return 0;
1306 get_fdc(drive);
1307 if (!fd_motor_on(drive)) {
1308 rel_fdc();
1309 return -1;
1312 if (unit[drive].dirty == 1) {
1313 del_timer (flush_track_timer + drive);
1314 non_int_flush_track (drive);
1316 errcnt = 0;
1317 while (errcnt < MAX_ERRORS) {
1318 if (!fd_seek(drive, track))
1319 return -1;
1320 raw_read(drive);
1321 error = (*unit[drive].dtype->read_fkt)(drive);
1322 if (error == 0) {
1323 rel_fdc();
1324 return 0;
1326 /* Read Error Handling: recalibrate and try again */
1327 unit[drive].track = -1;
1328 errcnt++;
1330 rel_fdc();
1331 return -1;
1334 static void redo_fd_request(void)
1336 struct request *rq;
1337 unsigned int cnt, block, track, sector;
1338 int drive;
1339 struct amiga_floppy_struct *floppy;
1340 char *data;
1341 unsigned long flags;
1342 int err;
1344 next_req:
1345 rq = blk_fetch_request(floppy_queue);
1346 if (!rq) {
1347 /* Nothing left to do */
1348 return;
1351 floppy = rq->rq_disk->private_data;
1352 drive = floppy - unit;
1354 next_segment:
1355 /* Here someone could investigate to be more efficient */
1356 for (cnt = 0, err = 0; cnt < blk_rq_cur_sectors(rq); cnt++) {
1357 #ifdef DEBUG
1358 printk("fd: sector %ld + %d requested for %s\n",
1359 blk_rq_pos(rq), cnt,
1360 (rq_data_dir(rq) == READ) ? "read" : "write");
1361 #endif
1362 block = blk_rq_pos(rq) + cnt;
1363 if ((int)block > floppy->blocks) {
1364 err = -EIO;
1365 break;
1368 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1369 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1370 data = rq->buffer + 512 * cnt;
1371 #ifdef DEBUG
1372 printk("access to track %d, sector %d, with buffer at "
1373 "0x%08lx\n", track, sector, data);
1374 #endif
1376 if (get_track(drive, track) == -1) {
1377 err = -EIO;
1378 break;
1381 if (rq_data_dir(rq) == READ) {
1382 memcpy(data, floppy->trackbuf + sector * 512, 512);
1383 } else {
1384 memcpy(floppy->trackbuf + sector * 512, data, 512);
1386 /* keep the drive spinning while writes are scheduled */
1387 if (!fd_motor_on(drive)) {
1388 err = -EIO;
1389 break;
1392 * setup a callback to write the track buffer
1393 * after a short (1 tick) delay.
1395 local_irq_save(flags);
1397 floppy->dirty = 1;
1398 /* reset the timer */
1399 mod_timer (flush_track_timer + drive, jiffies + 1);
1400 local_irq_restore(flags);
1404 if (__blk_end_request_cur(rq, err))
1405 goto next_segment;
1406 goto next_req;
1409 static void do_fd_request(struct request_queue * q)
1411 redo_fd_request();
1414 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1416 int drive = MINOR(bdev->bd_dev) & 3;
1418 geo->heads = unit[drive].type->heads;
1419 geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1420 geo->cylinders = unit[drive].type->tracks;
1421 return 0;
1424 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1425 unsigned int cmd, unsigned long param)
1427 struct amiga_floppy_struct *p = bdev->bd_disk->private_data;
1428 int drive = p - unit;
1429 static struct floppy_struct getprm;
1430 void __user *argp = (void __user *)param;
1432 switch(cmd){
1433 case FDFMTBEG:
1434 get_fdc(drive);
1435 if (fd_ref[drive] > 1) {
1436 rel_fdc();
1437 return -EBUSY;
1439 fsync_bdev(bdev);
1440 if (fd_motor_on(drive) == 0) {
1441 rel_fdc();
1442 return -ENODEV;
1444 if (fd_calibrate(drive) == 0) {
1445 rel_fdc();
1446 return -ENXIO;
1448 floppy_off(drive);
1449 rel_fdc();
1450 break;
1451 case FDFMTTRK:
1452 if (param < p->type->tracks * p->type->heads)
1454 get_fdc(drive);
1455 if (fd_seek(drive,param) != 0){
1456 memset(p->trackbuf, FD_FILL_BYTE,
1457 p->dtype->sects * p->type->sect_mult * 512);
1458 non_int_flush_track(drive);
1460 floppy_off(drive);
1461 rel_fdc();
1463 else
1464 return -EINVAL;
1465 break;
1466 case FDFMTEND:
1467 floppy_off(drive);
1468 invalidate_bdev(bdev);
1469 break;
1470 case FDGETPRM:
1471 memset((void *)&getprm, 0, sizeof (getprm));
1472 getprm.track=p->type->tracks;
1473 getprm.head=p->type->heads;
1474 getprm.sect=p->dtype->sects * p->type->sect_mult;
1475 getprm.size=p->blocks;
1476 if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
1477 return -EFAULT;
1478 break;
1479 case FDSETPRM:
1480 case FDDEFPRM:
1481 return -EINVAL;
1482 case FDFLUSH: /* unconditionally, even if not needed */
1483 del_timer (flush_track_timer + drive);
1484 non_int_flush_track(drive);
1485 break;
1486 #ifdef RAW_IOCTL
1487 case IOCTL_RAW_TRACK:
1488 if (copy_to_user(argp, raw_buf, p->type->read_size))
1489 return -EFAULT;
1490 else
1491 return p->type->read_size;
1492 #endif
1493 default:
1494 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
1495 cmd, drive);
1496 return -ENOSYS;
1498 return 0;
1501 static void fd_probe(int dev)
1503 unsigned long code;
1504 int type;
1505 int drive;
1507 drive = dev & 3;
1508 code = fd_get_drive_id(drive);
1510 /* get drive type */
1511 for (type = 0; type < num_dr_types; type++)
1512 if (drive_types[type].code == code)
1513 break;
1515 if (type >= num_dr_types) {
1516 printk(KERN_WARNING "fd_probe: unsupported drive type "
1517 "%08lx found\n", code);
1518 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1519 return;
1522 unit[drive].type = drive_types + type;
1523 unit[drive].track = -1;
1525 unit[drive].disk = -1;
1526 unit[drive].motor = 0;
1527 unit[drive].busy = 0;
1528 unit[drive].status = -1;
1532 * floppy_open check for aliasing (/dev/fd0 can be the same as
1533 * /dev/PS0 etc), and disallows simultaneous access to the same
1534 * drive with different device numbers.
1536 static int floppy_open(struct block_device *bdev, fmode_t mode)
1538 int drive = MINOR(bdev->bd_dev) & 3;
1539 int system = (MINOR(bdev->bd_dev) & 4) >> 2;
1540 int old_dev;
1541 unsigned long flags;
1543 old_dev = fd_device[drive];
1545 if (fd_ref[drive] && old_dev != system)
1546 return -EBUSY;
1548 if (mode & (FMODE_READ|FMODE_WRITE)) {
1549 check_disk_change(bdev);
1550 if (mode & FMODE_WRITE) {
1551 int wrprot;
1553 get_fdc(drive);
1554 fd_select (drive);
1555 wrprot = !(ciaa.pra & DSKPROT);
1556 fd_deselect (drive);
1557 rel_fdc();
1559 if (wrprot)
1560 return -EROFS;
1564 local_irq_save(flags);
1565 fd_ref[drive]++;
1566 fd_device[drive] = system;
1567 local_irq_restore(flags);
1569 unit[drive].dtype=&data_types[system];
1570 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1571 data_types[system].sects*unit[drive].type->sect_mult;
1572 set_capacity(unit[drive].gendisk, unit[drive].blocks);
1574 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1575 unit[drive].type->name, data_types[system].name);
1577 return 0;
1580 static int floppy_release(struct gendisk *disk, fmode_t mode)
1582 struct amiga_floppy_struct *p = disk->private_data;
1583 int drive = p - unit;
1585 if (unit[drive].dirty == 1) {
1586 del_timer (flush_track_timer + drive);
1587 non_int_flush_track (drive);
1590 if (!fd_ref[drive]--) {
1591 printk(KERN_CRIT "floppy_release with fd_ref == 0");
1592 fd_ref[drive] = 0;
1594 #ifdef MODULE
1595 /* the mod_use counter is handled this way */
1596 floppy_off (drive | 0x40000000);
1597 #endif
1598 return 0;
1602 * floppy-change is never called from an interrupt, so we can relax a bit
1603 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1604 * to the desired drive, but it will probably not survive the sleep if
1605 * several floppies are used at the same time: thus the loop.
1607 static int amiga_floppy_change(struct gendisk *disk)
1609 struct amiga_floppy_struct *p = disk->private_data;
1610 int drive = p - unit;
1611 int changed;
1612 static int first_time = 1;
1614 if (first_time)
1615 changed = first_time--;
1616 else {
1617 get_fdc(drive);
1618 fd_select (drive);
1619 changed = !(ciaa.pra & DSKCHANGE);
1620 fd_deselect (drive);
1621 rel_fdc();
1624 if (changed) {
1625 fd_probe(drive);
1626 p->track = -1;
1627 p->dirty = 0;
1628 writepending = 0; /* if this was true before, too bad! */
1629 writefromint = 0;
1630 return 1;
1632 return 0;
1635 static const struct block_device_operations floppy_fops = {
1636 .owner = THIS_MODULE,
1637 .open = floppy_open,
1638 .release = floppy_release,
1639 .locked_ioctl = fd_ioctl,
1640 .getgeo = fd_getgeo,
1641 .media_changed = amiga_floppy_change,
1644 static int __init fd_probe_drives(void)
1646 int drive,drives,nomem;
1648 printk(KERN_INFO "FD: probing units\nfound ");
1649 drives=0;
1650 nomem=0;
1651 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1652 struct gendisk *disk;
1653 fd_probe(drive);
1654 if (unit[drive].type->code == FD_NODRIVE)
1655 continue;
1656 disk = alloc_disk(1);
1657 if (!disk) {
1658 unit[drive].type->code = FD_NODRIVE;
1659 continue;
1661 unit[drive].gendisk = disk;
1662 drives++;
1663 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
1664 printk("no mem for ");
1665 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
1666 drives--;
1667 nomem = 1;
1669 printk("fd%d ",drive);
1670 disk->major = FLOPPY_MAJOR;
1671 disk->first_minor = drive;
1672 disk->fops = &floppy_fops;
1673 sprintf(disk->disk_name, "fd%d", drive);
1674 disk->private_data = &unit[drive];
1675 disk->queue = floppy_queue;
1676 set_capacity(disk, 880*2);
1677 add_disk(disk);
1679 if ((drives > 0) || (nomem == 0)) {
1680 if (drives == 0)
1681 printk("no drives");
1682 printk("\n");
1683 return drives;
1685 printk("\n");
1686 return -ENOMEM;
1689 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1691 int drive = *part & 3;
1692 if (unit[drive].type->code == FD_NODRIVE)
1693 return NULL;
1694 *part = 0;
1695 return get_disk(unit[drive].gendisk);
1698 static int __init amiga_floppy_init(void)
1700 int i, ret;
1702 if (!MACH_IS_AMIGA)
1703 return -ENODEV;
1705 if (!AMIGAHW_PRESENT(AMI_FLOPPY))
1706 return -ENODEV;
1708 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1709 return -EBUSY;
1712 * We request DSKPTR, DSKLEN and DSKDATA only, because the other
1713 * floppy registers are too spreaded over the custom register space
1715 ret = -EBUSY;
1716 if (!request_mem_region(CUSTOM_PHYSADDR+0x20, 8, "amiflop [Paula]")) {
1717 printk("fd: cannot get floppy registers\n");
1718 goto out_blkdev;
1721 ret = -ENOMEM;
1722 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE, "Floppy")) ==
1723 NULL) {
1724 printk("fd: cannot get chip mem buffer\n");
1725 goto out_memregion;
1728 ret = -EBUSY;
1729 if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
1730 printk("fd: cannot get irq for dma\n");
1731 goto out_irq;
1734 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
1735 printk("fd: cannot get irq for timer\n");
1736 goto out_irq2;
1739 ret = -ENOMEM;
1740 floppy_queue = blk_init_queue(do_fd_request, &amiflop_lock);
1741 if (!floppy_queue)
1742 goto out_queue;
1744 ret = -ENODEV;
1745 if (fd_probe_drives() < 1) /* No usable drives */
1746 goto out_probe;
1748 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1749 floppy_find, NULL, NULL);
1751 /* initialize variables */
1752 init_timer(&motor_on_timer);
1753 motor_on_timer.expires = 0;
1754 motor_on_timer.data = 0;
1755 motor_on_timer.function = motor_on_callback;
1756 for (i = 0; i < FD_MAX_UNITS; i++) {
1757 init_timer(&motor_off_timer[i]);
1758 motor_off_timer[i].expires = 0;
1759 motor_off_timer[i].data = i|0x80000000;
1760 motor_off_timer[i].function = fd_motor_off;
1761 init_timer(&flush_track_timer[i]);
1762 flush_track_timer[i].expires = 0;
1763 flush_track_timer[i].data = i;
1764 flush_track_timer[i].function = flush_track_callback;
1766 unit[i].track = -1;
1769 init_timer(&post_write_timer);
1770 post_write_timer.expires = 0;
1771 post_write_timer.data = 0;
1772 post_write_timer.function = post_write;
1774 for (i = 0; i < 128; i++)
1775 mfmdecode[i]=255;
1776 for (i = 0; i < 16; i++)
1777 mfmdecode[mfmencode[i]]=i;
1779 /* make sure that disk DMA is enabled */
1780 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1782 /* init ms timer */
1783 ciaa.crb = 8; /* one-shot, stop */
1784 return 0;
1786 out_probe:
1787 blk_cleanup_queue(floppy_queue);
1788 out_queue:
1789 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1790 out_irq2:
1791 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1792 out_irq:
1793 amiga_chip_free(raw_buf);
1794 out_memregion:
1795 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1796 out_blkdev:
1797 unregister_blkdev(FLOPPY_MAJOR,"fd");
1798 return ret;
1801 module_init(amiga_floppy_init);
1802 #ifdef MODULE
1804 #if 0 /* not safe to unload */
1805 void cleanup_module(void)
1807 int i;
1809 for( i = 0; i < FD_MAX_UNITS; i++) {
1810 if (unit[i].type->code != FD_NODRIVE) {
1811 del_gendisk(unit[i].gendisk);
1812 put_disk(unit[i].gendisk);
1813 kfree(unit[i].trackbuf);
1816 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
1817 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1818 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1819 custom.dmacon = DMAF_DISK; /* disable DMA */
1820 amiga_chip_free(raw_buf);
1821 blk_cleanup_queue(floppy_queue);
1822 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1823 unregister_blkdev(FLOPPY_MAJOR, "fd");
1825 #endif
1827 #else
1828 static int __init amiga_floppy_setup (char *str)
1830 int n;
1831 if (!MACH_IS_AMIGA)
1832 return 0;
1833 if (!get_option(&str, &n))
1834 return 0;
1835 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", n);
1836 fd_def_df0 = n;
1837 return 1;
1840 __setup("floppy=", amiga_floppy_setup);
1841 #endif