GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / block / amiflop.c
blob550e8b70c9eb3a4c80c112939a8da8c339c972fc
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>
57 #include <linux/slab.h>
59 #include <linux/fd.h>
60 #include <linux/hdreg.h>
61 #include <linux/delay.h>
62 #include <linux/init.h>
63 #include <linux/smp_lock.h>
64 #include <linux/amifdreg.h>
65 #include <linux/amifd.h>
66 #include <linux/buffer_head.h>
67 #include <linux/blkdev.h>
68 #include <linux/elevator.h>
69 #include <linux/interrupt.h>
70 #include <linux/platform_device.h>
72 #include <asm/setup.h>
73 #include <asm/uaccess.h>
74 #include <asm/amigahw.h>
75 #include <asm/amigaints.h>
76 #include <asm/irq.h>
78 #undef DEBUG /* print _LOTS_ of infos */
80 #define RAW_IOCTL
81 #ifdef RAW_IOCTL
82 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
83 #endif
86 * Defines
90 * Error codes
92 #define FD_OK 0 /* operation succeeded */
93 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
94 #define FD_NOUNIT 1 /* unit does not exist */
95 #define FD_UNITBUSY 2 /* unit already active */
96 #define FD_NOTACTIVE 3 /* unit is not active */
97 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
99 #define MFM_NOSYNC 1
100 #define MFM_HEADER 2
101 #define MFM_DATA 3
102 #define MFM_TRACK 4
105 * Floppy ID values
107 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
108 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
109 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
110 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
112 static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
114 module_param(fd_def_df0, ulong, 0);
115 MODULE_LICENSE("GPL");
117 static struct request_queue *floppy_queue;
120 * Macros
122 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
123 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
124 #define SELECT(mask) (ciab.prb &= ~mask)
125 #define DESELECT(mask) (ciab.prb |= mask)
126 #define SELMASK(drive) (1 << (3 + (drive & 3)))
128 static struct fd_drive_type drive_types[] = {
129 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
130 /* warning: times are now in milliseconds (ms) */
131 { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
132 { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
133 { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
134 { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
136 static int num_dr_types = ARRAY_SIZE(drive_types);
138 static int amiga_read(int), dos_read(int);
139 static void amiga_write(int), dos_write(int);
140 static struct fd_data_type data_types[] = {
141 { "Amiga", 11 , amiga_read, amiga_write},
142 { "MS-Dos", 9, dos_read, dos_write}
145 /* current info on each unit */
146 static struct amiga_floppy_struct unit[FD_MAX_UNITS];
148 static struct timer_list flush_track_timer[FD_MAX_UNITS];
149 static struct timer_list post_write_timer;
150 static struct timer_list motor_on_timer;
151 static struct timer_list motor_off_timer[FD_MAX_UNITS];
152 static int on_attempts;
154 /* Synchronization of FDC access */
155 /* request loop (trackbuffer) */
156 static volatile int fdc_busy = -1;
157 static volatile int fdc_nested;
158 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
160 static DECLARE_COMPLETION(motor_on_completion);
162 static volatile int selected = -1; /* currently selected drive */
164 static int writepending;
165 static int writefromint;
166 static char *raw_buf;
168 static DEFINE_SPINLOCK(amiflop_lock);
170 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
173 * These are global variables, as that's the easiest way to give
174 * information to interrupts. They are the data used for the current
175 * request.
177 static volatile char block_flag;
178 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
180 /* MS-Dos MFM Coding tables (should go quick and easy) */
181 static unsigned char mfmencode[16]={
182 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
183 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
185 static unsigned char mfmdecode[128];
187 /* floppy internal millisecond timer stuff */
188 static DECLARE_COMPLETION(ms_wait_completion);
189 #define MS_TICKS ((amiga_eclock+50)/1000)
192 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
193 * max X times - some types of errors increase the errorcount by 2 or
194 * even 3, so we might actually retry only X/2 times before giving up.
196 #define MAX_ERRORS 12
198 #define custom amiga_custom
200 /* Prevent "aliased" accesses. */
201 static int fd_ref[4] = { 0,0,0,0 };
202 static int fd_device[4] = { 0, 0, 0, 0 };
205 * Here come the actual hardware access and helper functions.
206 * They are not reentrant and single threaded because all drives
207 * share the same hardware and the same trackbuffer.
210 /* Milliseconds timer */
212 static irqreturn_t ms_isr(int irq, void *dummy)
214 complete(&ms_wait_completion);
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 int ticks;
223 static DEFINE_MUTEX(mutex);
225 if (ms > 0) {
226 mutex_lock(&mutex);
227 ticks = MS_TICKS*ms-1;
228 ciaa.tblo=ticks%256;
229 ciaa.tbhi=ticks/256;
230 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
231 wait_for_completion(&ms_wait_completion);
232 mutex_unlock(&mutex);
236 /* Hardware semaphore */
238 /* returns true when we would get the semaphore */
239 static inline int try_fdc(int drive)
241 drive &= 3;
242 return ((fdc_busy < 0) || (fdc_busy == drive));
245 static void get_fdc(int drive)
247 unsigned long flags;
249 drive &= 3;
250 #ifdef DEBUG
251 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
252 #endif
253 local_irq_save(flags);
254 wait_event(fdc_wait, try_fdc(drive));
255 fdc_busy = drive;
256 fdc_nested++;
257 local_irq_restore(flags);
260 static inline void rel_fdc(void)
262 #ifdef DEBUG
263 if (fdc_nested == 0)
264 printk("fd: unmatched rel_fdc\n");
265 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
266 #endif
267 fdc_nested--;
268 if (fdc_nested == 0) {
269 fdc_busy = -1;
270 wake_up(&fdc_wait);
274 static void fd_select (int drive)
276 unsigned char prb = ~0;
278 drive&=3;
279 #ifdef DEBUG
280 printk("selecting %d\n",drive);
281 #endif
282 if (drive == selected)
283 return;
284 get_fdc(drive);
285 selected = drive;
287 if (unit[drive].track % 2 != 0)
288 prb &= ~DSKSIDE;
289 if (unit[drive].motor == 1)
290 prb &= ~DSKMOTOR;
291 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
292 ciab.prb = prb;
293 prb &= ~SELMASK(drive);
294 ciab.prb = prb;
295 rel_fdc();
298 static void fd_deselect (int drive)
300 unsigned char prb;
301 unsigned long flags;
303 drive&=3;
304 #ifdef DEBUG
305 printk("deselecting %d\n",drive);
306 #endif
307 if (drive != selected) {
308 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
309 return;
312 get_fdc(drive);
313 local_irq_save(flags);
315 selected = -1;
317 prb = ciab.prb;
318 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
319 ciab.prb = prb;
321 local_irq_restore (flags);
322 rel_fdc();
326 static void motor_on_callback(unsigned long nr)
328 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
329 complete_all(&motor_on_completion);
330 } else {
331 motor_on_timer.expires = jiffies + HZ/10;
332 add_timer(&motor_on_timer);
336 static int fd_motor_on(int nr)
338 nr &= 3;
340 del_timer(motor_off_timer + nr);
342 if (!unit[nr].motor) {
343 unit[nr].motor = 1;
344 fd_select(nr);
346 INIT_COMPLETION(motor_on_completion);
347 motor_on_timer.data = nr;
348 mod_timer(&motor_on_timer, jiffies + HZ/2);
350 on_attempts = 10;
351 wait_for_completion(&motor_on_completion);
352 fd_deselect(nr);
355 if (on_attempts == 0) {
356 on_attempts = -1;
357 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
360 return 1;
363 static void fd_motor_off(unsigned long drive)
365 long calledfromint;
366 #ifdef MODULE
367 long decusecount;
369 decusecount = drive & 0x40000000;
370 #endif
371 calledfromint = drive & 0x80000000;
372 drive&=3;
373 if (calledfromint && !try_fdc(drive)) {
374 /* We would be blocked in an interrupt, so try again later */
375 motor_off_timer[drive].expires = jiffies + 1;
376 add_timer(motor_off_timer + drive);
377 return;
379 unit[drive].motor = 0;
380 fd_select(drive);
381 udelay (1);
382 fd_deselect(drive);
385 static void floppy_off (unsigned int nr)
387 int drive;
389 drive = nr & 3;
390 /* called this way it is always from interrupt */
391 motor_off_timer[drive].data = nr | 0x80000000;
392 mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
395 static int fd_calibrate(int drive)
397 unsigned char prb;
398 int n;
400 drive &= 3;
401 get_fdc(drive);
402 if (!fd_motor_on (drive))
403 return 0;
404 fd_select (drive);
405 prb = ciab.prb;
406 prb |= DSKSIDE;
407 prb &= ~DSKDIREC;
408 ciab.prb = prb;
409 for (n = unit[drive].type->tracks/2; n != 0; --n) {
410 if (ciaa.pra & DSKTRACK0)
411 break;
412 prb &= ~DSKSTEP;
413 ciab.prb = prb;
414 prb |= DSKSTEP;
415 udelay (2);
416 ciab.prb = prb;
417 ms_delay(unit[drive].type->step_delay);
419 ms_delay (unit[drive].type->settle_time);
420 prb |= DSKDIREC;
421 n = unit[drive].type->tracks + 20;
422 for (;;) {
423 prb &= ~DSKSTEP;
424 ciab.prb = prb;
425 prb |= DSKSTEP;
426 udelay (2);
427 ciab.prb = prb;
428 ms_delay(unit[drive].type->step_delay + 1);
429 if ((ciaa.pra & DSKTRACK0) == 0)
430 break;
431 if (--n == 0) {
432 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
433 fd_motor_off (drive);
434 unit[drive].track = -1;
435 rel_fdc();
436 return 0;
439 unit[drive].track = 0;
440 ms_delay(unit[drive].type->settle_time);
442 rel_fdc();
443 fd_deselect(drive);
444 return 1;
447 static int fd_seek(int drive, int track)
449 unsigned char prb;
450 int cnt;
452 #ifdef DEBUG
453 printk("seeking drive %d to track %d\n",drive,track);
454 #endif
455 drive &= 3;
456 get_fdc(drive);
457 if (unit[drive].track == track) {
458 rel_fdc();
459 return 1;
461 if (!fd_motor_on(drive)) {
462 rel_fdc();
463 return 0;
465 if (unit[drive].track < 0 && !fd_calibrate(drive)) {
466 rel_fdc();
467 return 0;
470 fd_select (drive);
471 cnt = unit[drive].track/2 - track/2;
472 prb = ciab.prb;
473 prb |= DSKSIDE | DSKDIREC;
474 if (track % 2 != 0)
475 prb &= ~DSKSIDE;
476 if (cnt < 0) {
477 cnt = - cnt;
478 prb &= ~DSKDIREC;
480 ciab.prb = prb;
481 if (track % 2 != unit[drive].track % 2)
482 ms_delay (unit[drive].type->side_time);
483 unit[drive].track = track;
484 if (cnt == 0) {
485 rel_fdc();
486 fd_deselect(drive);
487 return 1;
489 do {
490 prb &= ~DSKSTEP;
491 ciab.prb = prb;
492 prb |= DSKSTEP;
493 udelay (1);
494 ciab.prb = prb;
495 ms_delay (unit[drive].type->step_delay);
496 } while (--cnt != 0);
497 ms_delay (unit[drive].type->settle_time);
499 rel_fdc();
500 fd_deselect(drive);
501 return 1;
504 static unsigned long fd_get_drive_id(int drive)
506 int i;
507 ulong id = 0;
509 drive&=3;
510 get_fdc(drive);
511 /* set up for ID */
512 MOTOR_ON;
513 udelay(2);
514 SELECT(SELMASK(drive));
515 udelay(2);
516 DESELECT(SELMASK(drive));
517 udelay(2);
518 MOTOR_OFF;
519 udelay(2);
520 SELECT(SELMASK(drive));
521 udelay(2);
522 DESELECT(SELMASK(drive));
523 udelay(2);
525 /* loop and read disk ID */
526 for (i=0; i<32; i++) {
527 SELECT(SELMASK(drive));
528 udelay(2);
530 /* read and store value of DSKRDY */
531 id <<= 1;
532 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
534 DESELECT(SELMASK(drive));
537 rel_fdc();
540 * RB: At least A500/A2000's df0: don't identify themselves.
541 * As every (real) Amiga has at least a 3.5" DD drive as df0:
542 * we default to that if df0: doesn't identify as a certain
543 * type.
545 if(drive == 0 && id == FD_NODRIVE)
547 id = fd_def_df0;
548 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
550 /* return the ID value */
551 return (id);
554 static irqreturn_t fd_block_done(int irq, void *dummy)
556 if (block_flag)
557 custom.dsklen = 0x4000;
559 if (block_flag == 2) { /* writing */
560 writepending = 2;
561 post_write_timer.expires = jiffies + 1; /* at least 2 ms */
562 post_write_timer.data = selected;
563 add_timer(&post_write_timer);
565 else { /* reading */
566 block_flag = 0;
567 wake_up (&wait_fd_block);
569 return IRQ_HANDLED;
572 static void raw_read(int drive)
574 drive&=3;
575 get_fdc(drive);
576 wait_event(wait_fd_block, !block_flag);
577 fd_select(drive);
578 /* setup adkcon bits correctly */
579 custom.adkcon = ADK_MSBSYNC;
580 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
582 custom.dsksync = MFM_SYNC;
584 custom.dsklen = 0;
585 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
586 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
587 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
589 block_flag = 1;
591 wait_event(wait_fd_block, !block_flag);
593 custom.dsklen = 0;
594 fd_deselect(drive);
595 rel_fdc();
598 static int raw_write(int drive)
600 ushort adk;
602 drive&=3;
603 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
604 if ((ciaa.pra & DSKPROT) == 0) {
605 rel_fdc();
606 return 0;
608 wait_event(wait_fd_block, !block_flag);
609 fd_select(drive);
610 /* clear adkcon bits */
611 custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
612 /* set appropriate adkcon bits */
613 adk = ADK_SETCLR|ADK_FAST;
614 if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
615 adk |= ADK_PRECOMP1;
616 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
617 adk |= ADK_PRECOMP0;
618 custom.adkcon = adk;
620 custom.dsklen = DSKLEN_WRITE;
621 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
622 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
623 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
625 block_flag = 2;
626 return 1;
630 * to be called at least 2ms after the write has finished but before any
631 * other access to the hardware.
633 static void post_write (unsigned long drive)
635 #ifdef DEBUG
636 printk("post_write for drive %ld\n",drive);
637 #endif
638 drive &= 3;
639 custom.dsklen = 0;
640 block_flag = 0;
641 writepending = 0;
642 writefromint = 0;
643 unit[drive].dirty = 0;
644 wake_up(&wait_fd_block);
645 fd_deselect(drive);
646 rel_fdc(); /* corresponds to get_fdc() in raw_write */
651 * The following functions are to convert the block contents into raw data
652 * written to disk and vice versa.
653 * (Add other formats here ;-))
656 static unsigned long scan_sync(unsigned long raw, unsigned long end)
658 ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
660 while (ptr < endp && *ptr++ != 0x4489)
662 if (ptr < endp) {
663 while (*ptr == 0x4489 && ptr < endp)
664 ptr++;
665 return (ulong)ptr;
667 return 0;
670 static inline unsigned long checksum(unsigned long *addr, int len)
672 unsigned long csum = 0;
674 len /= sizeof(*addr);
675 while (len-- > 0)
676 csum ^= *addr++;
677 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
679 return csum;
682 static unsigned long decode (unsigned long *data, unsigned long *raw,
683 int len)
685 ulong *odd, *even;
687 /* convert length from bytes to longwords */
688 len >>= 2;
689 odd = raw;
690 even = odd + len;
692 /* prepare return pointer */
693 raw += len * 2;
695 do {
696 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
697 } while (--len != 0);
699 return (ulong)raw;
702 struct header {
703 unsigned char magic;
704 unsigned char track;
705 unsigned char sect;
706 unsigned char ord;
707 unsigned char labels[16];
708 unsigned long hdrchk;
709 unsigned long datachk;
712 static int amiga_read(int drive)
714 unsigned long raw;
715 unsigned long end;
716 int scnt;
717 unsigned long csum;
718 struct header hdr;
720 drive&=3;
721 raw = (long) raw_buf;
722 end = raw + unit[drive].type->read_size;
724 for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
725 if (!(raw = scan_sync(raw, end))) {
726 printk (KERN_INFO "can't find sync for sector %d\n", scnt);
727 return MFM_NOSYNC;
730 raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
731 raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
732 raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
733 raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
734 csum = checksum((ulong *)&hdr,
735 (char *)&hdr.hdrchk-(char *)&hdr);
737 #ifdef DEBUG
738 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
739 hdr.magic, hdr.track, hdr.sect, hdr.ord,
740 *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
741 *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
742 hdr.hdrchk, hdr.datachk);
743 #endif
745 if (hdr.hdrchk != csum) {
746 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
747 return MFM_HEADER;
750 /* verify track */
751 if (hdr.track != unit[drive].track) {
752 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
753 return MFM_TRACK;
756 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
757 (ulong *)raw, 512);
758 csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
760 if (hdr.datachk != csum) {
761 printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
762 hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
763 hdr.datachk, csum);
764 printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
765 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
766 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
767 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
768 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
769 return MFM_DATA;
773 return 0;
776 static void encode(unsigned long data, unsigned long *dest)
778 unsigned long data2;
780 data &= 0x55555555;
781 data2 = data ^ 0x55555555;
782 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
784 if (*(dest - 1) & 0x00000001)
785 data &= 0x7FFFFFFF;
787 *dest = data;
790 static void encode_block(unsigned long *dest, unsigned long *src, int len)
792 int cnt, to_cnt = 0;
793 unsigned long data;
795 /* odd bits */
796 for (cnt = 0; cnt < len / 4; cnt++) {
797 data = src[cnt] >> 1;
798 encode(data, dest + to_cnt++);
801 /* even bits */
802 for (cnt = 0; cnt < len / 4; cnt++) {
803 data = src[cnt];
804 encode(data, dest + to_cnt++);
808 static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
810 struct header hdr;
811 int i;
813 disk&=3;
814 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
815 raw++;
816 *raw++ = 0x44894489;
818 hdr.magic = 0xFF;
819 hdr.track = unit[disk].track;
820 hdr.sect = cnt;
821 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
822 for (i = 0; i < 16; i++)
823 hdr.labels[i] = 0;
824 hdr.hdrchk = checksum((ulong *)&hdr,
825 (char *)&hdr.hdrchk-(char *)&hdr);
826 hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
828 encode_block(raw, (ulong *)&hdr.magic, 4);
829 raw += 2;
830 encode_block(raw, (ulong *)&hdr.labels, 16);
831 raw += 8;
832 encode_block(raw, (ulong *)&hdr.hdrchk, 4);
833 raw += 2;
834 encode_block(raw, (ulong *)&hdr.datachk, 4);
835 raw += 2;
836 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
837 raw += 256;
839 return raw;
842 static void amiga_write(int disk)
844 unsigned int cnt;
845 unsigned long *ptr = (unsigned long *)raw_buf;
847 disk&=3;
848 /* gap space */
849 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
850 *ptr++ = 0xaaaaaaaa;
852 /* sectors */
853 for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
854 ptr = putsec (disk, ptr, cnt);
855 *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
859 struct dos_header {
860 unsigned char track, /* 0-80 */
861 side, /* 0-1 */
862 sec, /* 0-...*/
863 len_desc;/* 2 */
864 unsigned short crc; /* on 68000 we got an alignment problem,
865 but this compiler solves it by adding silently
866 adding a pad byte so data won't fit
867 and this took about 3h to discover.... */
868 unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
871 /* crc routines are borrowed from the messydos-handler */
873 /* excerpt from the messydos-device
874 ; The CRC is computed not only over the actual data, but including
875 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
876 ; As we don't read or encode these fields into our buffers, we have to
877 ; preload the registers containing the CRC with the values they would have
878 ; after stepping over these fields.
880 ; How CRCs "really" work:
882 ; First, you should regard a bitstring as a series of coefficients of
883 ; polynomials. We calculate with these polynomials in modulo-2
884 ; arithmetic, in which both add and subtract are done the same as
885 ; exclusive-or. Now, we modify our data (a very long polynomial) in
886 ; such a way that it becomes divisible by the CCITT-standard 16-bit
887 ; 16 12 5
888 ; polynomial: x + x + x + 1, represented by $11021. The easiest
889 ; way to do this would be to multiply (using proper arithmetic) our
890 ; datablock with $11021. So we have:
891 ; data * $11021 =
892 ; data * ($10000 + $1021) =
893 ; data * $10000 + data * $1021
894 ; The left part of this is simple: Just add two 0 bytes. But then
895 ; the right part (data $1021) remains difficult and even could have
896 ; a carry into the left part. The solution is to use a modified
897 ; multiplication, which has a result that is not correct, but with
898 ; a difference of any multiple of $11021. We then only need to keep
899 ; the 16 least significant bits of the result.
901 ; The following algorithm does this for us:
903 ; unsigned char *data, c, crclo, crchi;
904 ; while (not done) {
905 ; c = *data++ + crchi;
906 ; crchi = (@ c) >> 8 + crclo;
907 ; crclo = @ c;
910 ; Remember, + is done with EOR, the @ operator is in two tables (high
911 ; and low byte separately), which is calculated as
913 ; $1021 * (c & $F0)
914 ; xor $1021 * (c & $0F)
915 ; xor $1021 * (c >> 4) (* is regular multiplication)
918 ; Anyway, the end result is the same as the remainder of the division of
919 ; the data by $11021. I am afraid I need to study theory a bit more...
922 my only works was to code this from manx to C....
926 static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
928 static unsigned char CRCTable1[] = {
929 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
930 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
931 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
932 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
933 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
934 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
935 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
936 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
937 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
938 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
939 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
940 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
941 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
942 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
943 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
944 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
947 static unsigned char CRCTable2[] = {
948 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
949 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
950 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
951 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
952 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
953 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
954 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
955 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
956 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
957 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
958 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
959 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
960 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
961 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
962 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
963 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
966 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
967 register int i;
968 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
970 CRCT1=CRCTable1;
971 CRCT2=CRCTable2;
972 data=data_a3;
973 crcl=data_d1;
974 crch=data_d0;
975 for (i=data_d3; i>=0; i--) {
976 c = (*data++) ^ crch;
977 crch = CRCT1[c] ^ crcl;
978 crcl = CRCT2[c];
980 return (crch<<8)|crcl;
983 static inline ushort dos_hdr_crc (struct dos_header *hdr)
985 return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
988 static inline ushort dos_data_crc(unsigned char *data)
990 return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
993 static inline unsigned char dos_decode_byte(ushort word)
995 register ushort w2;
996 register unsigned char byte;
997 register unsigned char *dec = mfmdecode;
999 w2=word;
1000 w2>>=8;
1001 w2&=127;
1002 byte = dec[w2];
1003 byte <<= 4;
1004 w2 = word & 127;
1005 byte |= dec[w2];
1006 return byte;
1009 static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
1011 int i;
1013 for (i = 0; i < len; i++)
1014 *data++=dos_decode_byte(*raw++);
1015 return ((ulong)raw);
1018 #ifdef DEBUG
1019 static void dbg(unsigned long ptr)
1021 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
1022 ((ulong *)ptr)[0], ((ulong *)ptr)[1],
1023 ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
1025 #endif
1027 static int dos_read(int drive)
1029 unsigned long end;
1030 unsigned long raw;
1031 int scnt;
1032 unsigned short crc,data_crc[2];
1033 struct dos_header hdr;
1035 drive&=3;
1036 raw = (long) raw_buf;
1037 end = raw + unit[drive].type->read_size;
1039 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1040 do { /* search for the right sync of each sec-hdr */
1041 if (!(raw = scan_sync (raw, end))) {
1042 printk(KERN_INFO "dos_read: no hdr sync on "
1043 "track %d, unit %d for sector %d\n",
1044 unit[drive].track,drive,scnt);
1045 return MFM_NOSYNC;
1047 #ifdef DEBUG
1048 dbg(raw);
1049 #endif
1050 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1051 raw+=2; /* skip over headermark */
1052 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1053 crc = dos_hdr_crc(&hdr);
1055 #ifdef DEBUG
1056 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1057 hdr.sec, hdr.len_desc, hdr.crc);
1058 #endif
1060 if (crc != hdr.crc) {
1061 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
1062 hdr.crc, crc);
1063 return MFM_HEADER;
1065 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1066 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
1067 hdr.track,
1068 unit[drive].track/unit[drive].type->heads);
1069 return MFM_TRACK;
1072 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1073 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
1074 hdr.side,
1075 unit[drive].track%unit[drive].type->heads);
1076 return MFM_TRACK;
1079 if (hdr.len_desc != 2) {
1080 printk(KERN_INFO "dos_read: unknown sector len "
1081 "descriptor %d\n", hdr.len_desc);
1082 return MFM_DATA;
1084 #ifdef DEBUG
1085 printk("hdr accepted\n");
1086 #endif
1087 if (!(raw = scan_sync (raw, end))) {
1088 printk(KERN_INFO "dos_read: no data sync on track "
1089 "%d, unit %d for sector%d, disk sector %d\n",
1090 unit[drive].track, drive, scnt, hdr.sec);
1091 return MFM_NOSYNC;
1093 #ifdef DEBUG
1094 dbg(raw);
1095 #endif
1097 if (*((ushort *)raw)!=0x5545) {
1098 printk(KERN_INFO "dos_read: no data mark after "
1099 "sync (%d,%d,%d,%d) sc=%d\n",
1100 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1101 return MFM_NOSYNC;
1104 raw+=2; /* skip data mark (included in checksum) */
1105 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1106 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1107 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1109 if (crc != data_crc[0]) {
1110 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
1111 "sc=%d, %x %x\n", hdr.track, hdr.side,
1112 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
1113 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1114 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1115 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1116 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1117 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1118 return MFM_DATA;
1121 return 0;
1124 static inline ushort dos_encode_byte(unsigned char byte)
1126 register unsigned char *enc, b2, b1;
1127 register ushort word;
1129 enc=mfmencode;
1130 b1=byte;
1131 b2=b1>>4;
1132 b1&=15;
1133 word=enc[b2] <<8 | enc [b1];
1134 return (word|((word&(256|64)) ? 0: 128));
1137 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1139 int i;
1141 for (i = 0; i < len; i++) {
1142 *dest=dos_encode_byte(*src++);
1143 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1144 dest++;
1148 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1150 static struct dos_header hdr={0,0,0,2,0,
1151 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1152 int i;
1153 static ushort crc[2]={0,0x4e4e};
1155 drive&=3;
1156 /* id gap 1 */
1157 /* the MFM word before is always 9254 */
1158 for(i=0;i<6;i++)
1159 *raw++=0xaaaaaaaa;
1160 /* 3 sync + 1 headermark */
1161 *raw++=0x44894489;
1162 *raw++=0x44895554;
1164 /* fill in the variable parts of the header */
1165 hdr.track=unit[drive].track/unit[drive].type->heads;
1166 hdr.side=unit[drive].track%unit[drive].type->heads;
1167 hdr.sec=cnt+1;
1168 hdr.crc=dos_hdr_crc(&hdr);
1170 /* header (without "magic") and id gap 2*/
1171 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1172 raw+=14;
1174 /*id gap 3 */
1175 for(i=0;i<6;i++)
1176 *raw++=0xaaaaaaaa;
1178 /* 3 syncs and 1 datamark */
1179 *raw++=0x44894489;
1180 *raw++=0x44895545;
1182 /* data */
1183 dos_encode_block((ushort *)raw,
1184 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
1185 raw+=256;
1187 /*data crc + jd's special gap (long words :-/) */
1188 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1189 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1190 raw+=2;
1192 /* data gap */
1193 for(i=0;i<38;i++)
1194 *raw++=0x92549254;
1196 return raw; /* wrote 652 MFM words */
1199 static void dos_write(int disk)
1201 int cnt;
1202 unsigned long raw = (unsigned long) raw_buf;
1203 unsigned long *ptr=(unsigned long *)raw;
1205 disk&=3;
1206 /* really gap4 + indexgap , but we write it first and round it up */
1207 for (cnt=0;cnt<425;cnt++)
1208 *ptr++=0x92549254;
1210 /* the following is just guessed */
1211 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1212 for(cnt=0;cnt<473;cnt++)
1213 *ptr++=0x92549254;
1215 /* now the index marks...*/
1216 for (cnt=0;cnt<20;cnt++)
1217 *ptr++=0x92549254;
1218 for (cnt=0;cnt<6;cnt++)
1219 *ptr++=0xaaaaaaaa;
1220 *ptr++=0x52245224;
1221 *ptr++=0x52245552;
1222 for (cnt=0;cnt<20;cnt++)
1223 *ptr++=0x92549254;
1225 /* sectors */
1226 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1227 ptr=ms_putsec(disk,ptr,cnt);
1229 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1233 * Here comes the high level stuff (i.e. the filesystem interface)
1234 * and helper functions.
1235 * Normally this should be the only part that has to be adapted to
1236 * different kernel versions.
1239 static void flush_track_callback(unsigned long nr)
1241 nr&=3;
1242 writefromint = 1;
1243 if (!try_fdc(nr)) {
1244 /* we might block in an interrupt, so try again later */
1245 flush_track_timer[nr].expires = jiffies + 1;
1246 add_timer(flush_track_timer + nr);
1247 return;
1249 get_fdc(nr);
1250 (*unit[nr].dtype->write_fkt)(nr);
1251 if (!raw_write(nr)) {
1252 printk (KERN_NOTICE "floppy disk write protected\n");
1253 writefromint = 0;
1254 writepending = 0;
1256 rel_fdc();
1259 static int non_int_flush_track (unsigned long nr)
1261 unsigned long flags;
1263 nr&=3;
1264 writefromint = 0;
1265 del_timer(&post_write_timer);
1266 get_fdc(nr);
1267 if (!fd_motor_on(nr)) {
1268 writepending = 0;
1269 rel_fdc();
1270 return 0;
1272 local_irq_save(flags);
1273 if (writepending != 2) {
1274 local_irq_restore(flags);
1275 (*unit[nr].dtype->write_fkt)(nr);
1276 if (!raw_write(nr)) {
1277 printk (KERN_NOTICE "floppy disk write protected "
1278 "in write!\n");
1279 writepending = 0;
1280 return 0;
1282 wait_event(wait_fd_block, block_flag != 2);
1284 else {
1285 local_irq_restore(flags);
1286 ms_delay(2); /* 2 ms post_write delay */
1287 post_write(nr);
1289 rel_fdc();
1290 return 1;
1293 static int get_track(int drive, int track)
1295 int error, errcnt;
1297 drive&=3;
1298 if (unit[drive].track == track)
1299 return 0;
1300 get_fdc(drive);
1301 if (!fd_motor_on(drive)) {
1302 rel_fdc();
1303 return -1;
1306 if (unit[drive].dirty == 1) {
1307 del_timer (flush_track_timer + drive);
1308 non_int_flush_track (drive);
1310 errcnt = 0;
1311 while (errcnt < MAX_ERRORS) {
1312 if (!fd_seek(drive, track))
1313 return -1;
1314 raw_read(drive);
1315 error = (*unit[drive].dtype->read_fkt)(drive);
1316 if (error == 0) {
1317 rel_fdc();
1318 return 0;
1320 /* Read Error Handling: recalibrate and try again */
1321 unit[drive].track = -1;
1322 errcnt++;
1324 rel_fdc();
1325 return -1;
1328 static void redo_fd_request(void)
1330 struct request *rq;
1331 unsigned int cnt, block, track, sector;
1332 int drive;
1333 struct amiga_floppy_struct *floppy;
1334 char *data;
1335 unsigned long flags;
1336 int err;
1338 next_req:
1339 rq = blk_fetch_request(floppy_queue);
1340 if (!rq) {
1341 /* Nothing left to do */
1342 return;
1345 floppy = rq->rq_disk->private_data;
1346 drive = floppy - unit;
1348 next_segment:
1349 /* Here someone could investigate to be more efficient */
1350 for (cnt = 0, err = 0; cnt < blk_rq_cur_sectors(rq); cnt++) {
1351 #ifdef DEBUG
1352 printk("fd: sector %ld + %d requested for %s\n",
1353 blk_rq_pos(rq), cnt,
1354 (rq_data_dir(rq) == READ) ? "read" : "write");
1355 #endif
1356 block = blk_rq_pos(rq) + cnt;
1357 if ((int)block > floppy->blocks) {
1358 err = -EIO;
1359 break;
1362 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1363 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1364 data = rq->buffer + 512 * cnt;
1365 #ifdef DEBUG
1366 printk("access to track %d, sector %d, with buffer at "
1367 "0x%08lx\n", track, sector, data);
1368 #endif
1370 if (get_track(drive, track) == -1) {
1371 err = -EIO;
1372 break;
1375 if (rq_data_dir(rq) == READ) {
1376 memcpy(data, floppy->trackbuf + sector * 512, 512);
1377 } else {
1378 memcpy(floppy->trackbuf + sector * 512, data, 512);
1380 /* keep the drive spinning while writes are scheduled */
1381 if (!fd_motor_on(drive)) {
1382 err = -EIO;
1383 break;
1386 * setup a callback to write the track buffer
1387 * after a short (1 tick) delay.
1389 local_irq_save(flags);
1391 floppy->dirty = 1;
1392 /* reset the timer */
1393 mod_timer (flush_track_timer + drive, jiffies + 1);
1394 local_irq_restore(flags);
1398 if (__blk_end_request_cur(rq, err))
1399 goto next_segment;
1400 goto next_req;
1403 static void do_fd_request(struct request_queue * q)
1405 redo_fd_request();
1408 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1410 int drive = MINOR(bdev->bd_dev) & 3;
1412 geo->heads = unit[drive].type->heads;
1413 geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1414 geo->cylinders = unit[drive].type->tracks;
1415 return 0;
1418 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1419 unsigned int cmd, unsigned long param)
1421 struct amiga_floppy_struct *p = bdev->bd_disk->private_data;
1422 int drive = p - unit;
1423 static struct floppy_struct getprm;
1424 void __user *argp = (void __user *)param;
1426 switch(cmd){
1427 case FDFMTBEG:
1428 get_fdc(drive);
1429 if (fd_ref[drive] > 1) {
1430 rel_fdc();
1431 return -EBUSY;
1433 fsync_bdev(bdev);
1434 if (fd_motor_on(drive) == 0) {
1435 rel_fdc();
1436 return -ENODEV;
1438 if (fd_calibrate(drive) == 0) {
1439 rel_fdc();
1440 return -ENXIO;
1442 floppy_off(drive);
1443 rel_fdc();
1444 break;
1445 case FDFMTTRK:
1446 if (param < p->type->tracks * p->type->heads)
1448 get_fdc(drive);
1449 if (fd_seek(drive,param) != 0){
1450 memset(p->trackbuf, FD_FILL_BYTE,
1451 p->dtype->sects * p->type->sect_mult * 512);
1452 non_int_flush_track(drive);
1454 floppy_off(drive);
1455 rel_fdc();
1457 else
1458 return -EINVAL;
1459 break;
1460 case FDFMTEND:
1461 floppy_off(drive);
1462 invalidate_bdev(bdev);
1463 break;
1464 case FDGETPRM:
1465 memset((void *)&getprm, 0, sizeof (getprm));
1466 getprm.track=p->type->tracks;
1467 getprm.head=p->type->heads;
1468 getprm.sect=p->dtype->sects * p->type->sect_mult;
1469 getprm.size=p->blocks;
1470 if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
1471 return -EFAULT;
1472 break;
1473 case FDSETPRM:
1474 case FDDEFPRM:
1475 return -EINVAL;
1476 case FDFLUSH: /* unconditionally, even if not needed */
1477 del_timer (flush_track_timer + drive);
1478 non_int_flush_track(drive);
1479 break;
1480 #ifdef RAW_IOCTL
1481 case IOCTL_RAW_TRACK:
1482 if (copy_to_user(argp, raw_buf, p->type->read_size))
1483 return -EFAULT;
1484 else
1485 return p->type->read_size;
1486 #endif
1487 default:
1488 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
1489 cmd, drive);
1490 return -ENOSYS;
1492 return 0;
1495 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1496 unsigned int cmd, unsigned long param)
1498 int ret;
1500 lock_kernel();
1501 ret = fd_locked_ioctl(bdev, mode, cmd, param);
1502 unlock_kernel();
1504 return ret;
1507 static void fd_probe(int dev)
1509 unsigned long code;
1510 int type;
1511 int drive;
1513 drive = dev & 3;
1514 code = fd_get_drive_id(drive);
1516 /* get drive type */
1517 for (type = 0; type < num_dr_types; type++)
1518 if (drive_types[type].code == code)
1519 break;
1521 if (type >= num_dr_types) {
1522 printk(KERN_WARNING "fd_probe: unsupported drive type "
1523 "%08lx found\n", code);
1524 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1525 return;
1528 unit[drive].type = drive_types + type;
1529 unit[drive].track = -1;
1531 unit[drive].disk = -1;
1532 unit[drive].motor = 0;
1533 unit[drive].busy = 0;
1534 unit[drive].status = -1;
1538 * floppy_open check for aliasing (/dev/fd0 can be the same as
1539 * /dev/PS0 etc), and disallows simultaneous access to the same
1540 * drive with different device numbers.
1542 static int floppy_open(struct block_device *bdev, fmode_t mode)
1544 int drive = MINOR(bdev->bd_dev) & 3;
1545 int system = (MINOR(bdev->bd_dev) & 4) >> 2;
1546 int old_dev;
1547 unsigned long flags;
1549 lock_kernel();
1550 old_dev = fd_device[drive];
1552 if (fd_ref[drive] && old_dev != system) {
1553 unlock_kernel();
1554 return -EBUSY;
1557 if (mode & (FMODE_READ|FMODE_WRITE)) {
1558 check_disk_change(bdev);
1559 if (mode & FMODE_WRITE) {
1560 int wrprot;
1562 get_fdc(drive);
1563 fd_select (drive);
1564 wrprot = !(ciaa.pra & DSKPROT);
1565 fd_deselect (drive);
1566 rel_fdc();
1568 if (wrprot) {
1569 unlock_kernel();
1570 return -EROFS;
1575 local_irq_save(flags);
1576 fd_ref[drive]++;
1577 fd_device[drive] = system;
1578 local_irq_restore(flags);
1580 unit[drive].dtype=&data_types[system];
1581 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1582 data_types[system].sects*unit[drive].type->sect_mult;
1583 set_capacity(unit[drive].gendisk, unit[drive].blocks);
1585 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1586 unit[drive].type->name, data_types[system].name);
1588 unlock_kernel();
1589 return 0;
1592 static int floppy_release(struct gendisk *disk, fmode_t mode)
1594 struct amiga_floppy_struct *p = disk->private_data;
1595 int drive = p - unit;
1597 lock_kernel();
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 unlock_kernel();
1612 return 0;
1616 * floppy-change is never called from an interrupt, so we can relax a bit
1617 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1618 * to the desired drive, but it will probably not survive the sleep if
1619 * several floppies are used at the same time: thus the loop.
1621 static int amiga_floppy_change(struct gendisk *disk)
1623 struct amiga_floppy_struct *p = disk->private_data;
1624 int drive = p - unit;
1625 int changed;
1626 static int first_time = 1;
1628 if (first_time)
1629 changed = first_time--;
1630 else {
1631 get_fdc(drive);
1632 fd_select (drive);
1633 changed = !(ciaa.pra & DSKCHANGE);
1634 fd_deselect (drive);
1635 rel_fdc();
1638 if (changed) {
1639 fd_probe(drive);
1640 p->track = -1;
1641 p->dirty = 0;
1642 writepending = 0; /* if this was true before, too bad! */
1643 writefromint = 0;
1644 return 1;
1646 return 0;
1649 static const struct block_device_operations floppy_fops = {
1650 .owner = THIS_MODULE,
1651 .open = floppy_open,
1652 .release = floppy_release,
1653 .ioctl = fd_ioctl,
1654 .getgeo = fd_getgeo,
1655 .media_changed = amiga_floppy_change,
1658 static int __init fd_probe_drives(void)
1660 int drive,drives,nomem;
1662 printk(KERN_INFO "FD: probing units\nfound ");
1663 drives=0;
1664 nomem=0;
1665 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1666 struct gendisk *disk;
1667 fd_probe(drive);
1668 if (unit[drive].type->code == FD_NODRIVE)
1669 continue;
1670 disk = alloc_disk(1);
1671 if (!disk) {
1672 unit[drive].type->code = FD_NODRIVE;
1673 continue;
1675 unit[drive].gendisk = disk;
1676 drives++;
1677 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
1678 printk("no mem for ");
1679 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
1680 drives--;
1681 nomem = 1;
1683 printk("fd%d ",drive);
1684 disk->major = FLOPPY_MAJOR;
1685 disk->first_minor = drive;
1686 disk->fops = &floppy_fops;
1687 sprintf(disk->disk_name, "fd%d", drive);
1688 disk->private_data = &unit[drive];
1689 disk->queue = floppy_queue;
1690 set_capacity(disk, 880*2);
1691 add_disk(disk);
1693 if ((drives > 0) || (nomem == 0)) {
1694 if (drives == 0)
1695 printk("no drives");
1696 printk("\n");
1697 return drives;
1699 printk("\n");
1700 return -ENOMEM;
1703 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1705 int drive = *part & 3;
1706 if (unit[drive].type->code == FD_NODRIVE)
1707 return NULL;
1708 *part = 0;
1709 return get_disk(unit[drive].gendisk);
1712 static int __init amiga_floppy_probe(struct platform_device *pdev)
1714 int i, ret;
1716 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1717 return -EBUSY;
1719 ret = -ENOMEM;
1720 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE, "Floppy")) ==
1721 NULL) {
1722 printk("fd: cannot get chip mem buffer\n");
1723 goto out_blkdev;
1726 ret = -EBUSY;
1727 if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
1728 printk("fd: cannot get irq for dma\n");
1729 goto out_irq;
1732 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
1733 printk("fd: cannot get irq for timer\n");
1734 goto out_irq2;
1737 ret = -ENOMEM;
1738 floppy_queue = blk_init_queue(do_fd_request, &amiflop_lock);
1739 if (!floppy_queue)
1740 goto out_queue;
1742 ret = -ENODEV;
1743 if (fd_probe_drives() < 1) /* No usable drives */
1744 goto out_probe;
1746 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1747 floppy_find, NULL, NULL);
1749 /* initialize variables */
1750 init_timer(&motor_on_timer);
1751 motor_on_timer.expires = 0;
1752 motor_on_timer.data = 0;
1753 motor_on_timer.function = motor_on_callback;
1754 for (i = 0; i < FD_MAX_UNITS; i++) {
1755 init_timer(&motor_off_timer[i]);
1756 motor_off_timer[i].expires = 0;
1757 motor_off_timer[i].data = i|0x80000000;
1758 motor_off_timer[i].function = fd_motor_off;
1759 init_timer(&flush_track_timer[i]);
1760 flush_track_timer[i].expires = 0;
1761 flush_track_timer[i].data = i;
1762 flush_track_timer[i].function = flush_track_callback;
1764 unit[i].track = -1;
1767 init_timer(&post_write_timer);
1768 post_write_timer.expires = 0;
1769 post_write_timer.data = 0;
1770 post_write_timer.function = post_write;
1772 for (i = 0; i < 128; i++)
1773 mfmdecode[i]=255;
1774 for (i = 0; i < 16; i++)
1775 mfmdecode[mfmencode[i]]=i;
1777 /* make sure that disk DMA is enabled */
1778 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1780 /* init ms timer */
1781 ciaa.crb = 8; /* one-shot, stop */
1782 return 0;
1784 out_probe:
1785 blk_cleanup_queue(floppy_queue);
1786 out_queue:
1787 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1788 out_irq2:
1789 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1790 out_irq:
1791 amiga_chip_free(raw_buf);
1792 out_blkdev:
1793 unregister_blkdev(FLOPPY_MAJOR,"fd");
1794 return ret;
1798 static struct platform_driver amiga_floppy_driver = {
1799 .driver = {
1800 .name = "amiga-floppy",
1801 .owner = THIS_MODULE,
1805 static int __init amiga_floppy_init(void)
1807 return platform_driver_probe(&amiga_floppy_driver, amiga_floppy_probe);
1810 module_init(amiga_floppy_init);
1812 #ifndef MODULE
1813 static int __init amiga_floppy_setup (char *str)
1815 int n;
1816 if (!MACH_IS_AMIGA)
1817 return 0;
1818 if (!get_option(&str, &n))
1819 return 0;
1820 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", n);
1821 fd_def_df0 = n;
1822 return 1;
1825 __setup("floppy=", amiga_floppy_setup);
1826 #endif
1828 MODULE_ALIAS("platform:amiga-floppy");