1 /*
2 * linux/amiga/amiflop.c
3 *
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)
13 *
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
17 *
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
22 *
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)
26 *
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.
34 *
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
38 *
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.
48 *
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 ;-)
54 */
55
56 #include <linux/module.h>
57
58 #include <linux/sched.h>
59 #include <linux/fs.h>
60 #include <linux/fcntl.h>
61 #include <linux/kernel.h>
62 #include <linux/timer.h>
63 #include <linux/fd.h>
64 #include <linux/hdreg.h>
65 #include <linux/errno.h>
66 #include <linux/types.h>
67 #include <linux/delay.h>
68 #include <linux/string.h>
69 #include <linux/slab.h>
70 #include <linux/init.h>
71 #include <linux/amifdreg.h>
72 #include <linux/amifd.h>
73 #include <linux/ioport.h>
74
75 #include <asm/setup.h>
76 #include <asm/uaccess.h>
77 #include <asm/amigahw.h>
78 #include <asm/amigaints.h>
79 #include <asm/irq.h>
80
81 #define MAJOR_NR FLOPPY_MAJOR
82 #include <linux/blk.h>
83
84 #undef DEBUG /* print _LOTS_ of infos */
85
86 #define RAW_IOCTL
87 #ifdef RAW_IOCTL
88 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
89 #endif
90
91 /*
92 * Defines
93 */
94
95 /*
96 * Error codes
97 */
98 #define FD_OK 0 /* operation succeeded */
99 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
100 #define FD_NOUNIT 1 /* unit does not exist */
101 #define FD_UNITBUSY 2 /* unit already active */
102 #define FD_NOTACTIVE 3 /* unit is not active */
103 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
104
105 #define MFM_NOSYNC 1
106 #define MFM_HEADER 2
107 #define MFM_DATA 3
108 #define MFM_TRACK 4
109
110 /*
111 * Floppy ID values
112 */
113 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
114 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
115 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
116 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
117
118 static long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
119
120 MODULE_PARM(fd_def_df0,"l");
121
122 /*
123 * Macros
124 */
125 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
126 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
127 #define SELECT(mask) (ciab.prb &= ~mask)
128 #define DESELECT(mask) (ciab.prb |= mask)
129 #define SELMASK(drive) (1 << (3 + (drive & 3)))
130
131 static struct fd_drive_type drive_types[] = {
132 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
133 /* warning: times are now in milliseconds (ms) */
134 { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
135 { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
136 { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
137 { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
138 };
139 static int num_dr_types = sizeof(drive_types) / sizeof(drive_types[0]);
140
141 /* defaults for 3 1/2" HD-Disks */
142 static int floppy_sizes[256]={880,880,880,880,720,720,720,720,};
143 static int floppy_blocksizes[256];
144 /* hardsector size assumed to be 512 */
145
146 static int amiga_read(int), dos_read(int);
147 static void amiga_write(int), dos_write(int);
148 static struct fd_data_type data_types[] = {
149 { "Amiga", 11 , amiga_read, amiga_write},
150 { "MS-Dos", 9, dos_read, dos_write}
151 };
152
153 /* current info on each unit */
154 static struct amiga_floppy_struct unit[FD_MAX_UNITS];
155
156 static struct timer_list flush_track_timer[FD_MAX_UNITS];
157 static struct timer_list post_write_timer;
158 static struct timer_list motor_on_timer;
159 static struct timer_list motor_off_timer[FD_MAX_UNITS];
160 static int on_attempts;
161
162 /* Synchronization of FDC access */
163 /* request loop (trackbuffer) */
164 static volatile int fdc_busy = -1;
165 static volatile int fdc_nested;
166 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
167
168 static DECLARE_WAIT_QUEUE_HEAD(motor_wait);
169
170 static volatile int selected = -1; /* currently selected drive */
171
172 static int writepending;
173 static int writefromint;
174 static char *raw_buf;
175
176 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
177
178 /*
179 * These are global variables, as that's the easiest way to give
180 * information to interrupts. They are the data used for the current
181 * request.
182 */
183 static volatile char block_flag;
184 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
185
186 /* MS-Dos MFM Coding tables (should go quick and easy) */
187 static unsigned char mfmencode[16]={
188 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
189 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
190 };
191 static unsigned char mfmdecode[128];
192
193 /* floppy internal millisecond timer stuff */
194 static volatile int ms_busy = -1;
195 static DECLARE_WAIT_QUEUE_HEAD(ms_wait);
196 #define MS_TICKS ((amiga_eclock+50)/1000)
197
198 /*
199 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
200 * max X times - some types of errors increase the errorcount by 2 or
201 * even 3, so we might actually retry only X/2 times before giving up.
202 */
203 #define MAX_ERRORS 12
204
205 /* Prevent "aliased" accesses. */
206 static int fd_ref[4] = { 0,0,0,0 };
207 static int fd_device[4] = { 0,0,0,0 };
208
209 /*
210 * Current device number. Taken either from the block header or from the
211 * format request descriptor.
212 */
213 #define CURRENT_DEVICE (CURRENT->rq_dev)
214
215 /* Current error count. */
216 #define CURRENT_ERRORS (CURRENT->errors)
217
218
219
220 /*
221 * Here come the actual hardware access and helper functions.
222 * They are not reentrant and single threaded because all drives
223 * share the same hardware and the same trackbuffer.
224 */
225
226 /* Milliseconds timer */
227
228 static void ms_isr(int irq, void *dummy, struct pt_regs *fp)
229 {
230 ms_busy = -1;
231 wake_up(&ms_wait);
232 }
233
234 /* all waits are queued up
235 A more generic routine would do a schedule a la timer.device */
236 static void ms_delay(int ms)
237 {
238 unsigned long flags;
239 int ticks;
240 if (ms > 0) {
241 save_flags(flags);
242 cli();
243 while (ms_busy == 0)
244 sleep_on(&ms_wait);
245 ms_busy = 0;
246 restore_flags(flags);
247 ticks = MS_TICKS*ms-1;
248 ciaa.tblo=ticks%256;
249 ciaa.tbhi=ticks/256;
250 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
251 sleep_on(&ms_wait);
252 }
253 }
254
255 /* Hardware semaphore */
256
257 /* returns true when we would get the semaphore */
258 static inline int try_fdc(int drive)
259 {
260 drive &= 3;
261 return ((fdc_busy < 0) || (fdc_busy == drive));
262 }
263
264 static void get_fdc(int drive)
265 {
266 unsigned long flags;
267
268 drive &= 3;
269 #ifdef DEBUG
270 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
271 #endif
272 save_flags(flags);
273 cli();
274 while (!try_fdc(drive))
275 sleep_on(&fdc_wait);
276 fdc_busy = drive;
277 fdc_nested++;
278 restore_flags(flags);
279 }
280
281 static inline void rel_fdc(void)
282 {
283 #ifdef DEBUG
284 if (fdc_nested == 0)
285 printk("fd: unmatched rel_fdc\n");
286 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
287 #endif
288 fdc_nested--;
289 if (fdc_nested == 0) {
290 fdc_busy = -1;
291 wake_up(&fdc_wait);
292 }
293 }
294
295 static void fd_select (int drive)
296 {
297 unsigned char prb = ~0;
298
299 drive&=3;
300 #ifdef DEBUG
301 printk("selecting %d\n",drive);
302 #endif
303 if (drive == selected)
304 return;
305 get_fdc(drive);
306 selected = drive;
307
308 if (unit[drive].track % 2 != 0)
309 prb &= ~DSKSIDE;
310 if (unit[drive].motor == 1)
311 prb &= ~DSKMOTOR;
312 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
313 ciab.prb = prb;
314 prb &= ~SELMASK(drive);
315 ciab.prb = prb;
316 rel_fdc();
317 }
318
319 static void fd_deselect (int drive)
320 {
321 unsigned char prb;
322 unsigned long flags;
323
324 drive&=3;
325 #ifdef DEBUG
326 printk("deselecting %d\n",drive);
327 #endif
328 if (drive != selected) {
329 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
330 return;
331 }
332
333 get_fdc(drive);
334 save_flags (flags);
335 sti();
336
337 selected = -1;
338
339 prb = ciab.prb;
340 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
341 ciab.prb = prb;
342
343 restore_flags (flags);
344 rel_fdc();
345
346 }
347
348 static void motor_on_callback(unsigned long nr)
349 {
350 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
351 wake_up (&motor_wait);
352 } else {
353 motor_on_timer.expires = jiffies + HZ/10;
354 add_timer(&motor_on_timer);
355 }
356 }
357
358 static int fd_motor_on(int nr)
359 {
360 nr &= 3;
361
362 del_timer(motor_off_timer + nr);
363
364 if (!unit[nr].motor) {
365 unit[nr].motor = 1;
366 fd_select(nr);
367
368 del_timer(&motor_on_timer);
369 motor_on_timer.data = nr;
370 motor_on_timer.expires = jiffies + HZ/2;
371 add_timer(&motor_on_timer);
372
373 on_attempts = 10;
374 sleep_on (&motor_wait);
375 fd_deselect(nr);
376 }
377
378 if (on_attempts == 0) {
379 on_attempts = -1;
380 #if 0
381 printk (KERN_ERR "motor_on failed, turning motor off\n");
382 fd_motor_off (nr);
383 return 0;
384 #else
385 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
386 #endif
387 }
388
389 return 1;
390 }
391
392 static void fd_motor_off(unsigned long drive)
393 {
394 long calledfromint;
395 #ifdef MODULE
396 long decusecount;
397
398 decusecount = drive & 0x40000000;
399 #endif
400 calledfromint = drive & 0x80000000;
401 drive&=3;
402 if (calledfromint && !try_fdc(drive)) {
403 /* We would be blocked in an interrupt, so try again later */
404 motor_off_timer[drive].expires = jiffies + 1;
405 add_timer(motor_off_timer + drive);
406 return;
407 }
408 unit[drive].motor = 0;
409 fd_select(drive);
410 udelay (1);
411 fd_deselect(drive);
412
413 #ifdef MODULE
414 /*
415 this is the last interrupt for any drive access, happens after
416 release (from floppy_off). So we have to wait until now to decrease
417 the use count.
418 */
419 if (decusecount)
420 MOD_DEC_USE_COUNT;
421 #endif
422 }
423
424 static void floppy_off (unsigned int nr)
425 {
426 int drive;
427
428 drive = nr & 3;
429 del_timer(motor_off_timer + drive);
430 motor_off_timer[drive].expires = jiffies + 3*HZ;
431 /* called this way it is always from interrupt */
432 motor_off_timer[drive].data = nr | 0x80000000;
433 add_timer(motor_off_timer + nr);
434 }
435
436 static int fd_calibrate(int drive)
437 {
438 unsigned char prb;
439 int n;
440
441 drive &= 3;
442 get_fdc(drive);
443 if (!fd_motor_on (drive))
444 return 0;
445 fd_select (drive);
446 prb = ciab.prb;
447 prb |= DSKSIDE;
448 prb &= ~DSKDIREC;
449 ciab.prb = prb;
450 for (n = unit[drive].type->tracks/2; n != 0; --n) {
451 if (ciaa.pra & DSKTRACK0)
452 break;
453 prb &= ~DSKSTEP;
454 ciab.prb = prb;
455 prb |= DSKSTEP;
456 udelay (2);
457 ciab.prb = prb;
458 ms_delay(unit[drive].type->step_delay);
459 }
460 ms_delay (unit[drive].type->settle_time);
461 prb |= DSKDIREC;
462 n = unit[drive].type->tracks + 20;
463 for (;;) {
464 prb &= ~DSKSTEP;
465 ciab.prb = prb;
466 prb |= DSKSTEP;
467 udelay (2);
468 ciab.prb = prb;
469 ms_delay(unit[drive].type->step_delay + 1);
470 if ((ciaa.pra & DSKTRACK0) == 0)
471 break;
472 if (--n == 0) {
473 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
474 fd_motor_off (drive);
475 unit[drive].track = -1;
476 rel_fdc();
477 return 0;
478 }
479 }
480 unit[drive].track = 0;
481 ms_delay(unit[drive].type->settle_time);
482
483 rel_fdc();
484 fd_deselect(drive);
485 return 1;
486 }
487
488 static int fd_seek(int drive, int track)
489 {
490 unsigned char prb;
491 int cnt;
492
493 #ifdef DEBUG
494 printk("seeking drive %d to track %d\n",drive,track);
495 #endif
496 drive &= 3;
497 get_fdc(drive);
498 if (unit[drive].track == track) {
499 rel_fdc();
500 return 1;
501 }
502 if (!fd_motor_on(drive)) {
503 rel_fdc();
504 return 0;
505 }
506 if (unit[drive].track < 0 && !fd_calibrate(drive)) {
507 rel_fdc();
508 return 0;
509 }
510
511 fd_select (drive);
512 cnt = unit[drive].track/2 - track/2;
513 prb = ciab.prb;
514 prb |= DSKSIDE | DSKDIREC;
515 if (track % 2 != 0)
516 prb &= ~DSKSIDE;
517 if (cnt < 0) {
518 cnt = - cnt;
519 prb &= ~DSKDIREC;
520 }
521 ciab.prb = prb;
522 if (track % 2 != unit[drive].track % 2)
523 ms_delay (unit[drive].type->side_time);
524 unit[drive].track = track;
525 if (cnt == 0) {
526 rel_fdc();
527 fd_deselect(drive);
528 return 1;
529 }
530 do {
531 prb &= ~DSKSTEP;
532 ciab.prb = prb;
533 prb |= DSKSTEP;
534 udelay (1);
535 ciab.prb = prb;
536 ms_delay (unit[drive].type->step_delay);
537 } while (--cnt != 0);
538 ms_delay (unit[drive].type->settle_time);
539
540 rel_fdc();
541 fd_deselect(drive);
542 return 1;
543 }
544
545 static unsigned long fd_get_drive_id(int drive)
546 {
547 int i;
548 ulong id = 0;
549
550 drive&=3;
551 get_fdc(drive);
552 /* set up for ID */
553 MOTOR_ON;
554 udelay(2);
555 SELECT(SELMASK(drive));
556 udelay(2);
557 DESELECT(SELMASK(drive));
558 udelay(2);
559 MOTOR_OFF;
560 udelay(2);
561 SELECT(SELMASK(drive));
562 udelay(2);
563 DESELECT(SELMASK(drive));
564 udelay(2);
565
566 /* loop and read disk ID */
567 for (i=0; i<32; i++) {
568 SELECT(SELMASK(drive));
569 udelay(2);
570
571 /* read and store value of DSKRDY */
572 id <<= 1;
573 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
574
575 DESELECT(SELMASK(drive));
576 }
577
578 rel_fdc();
579
580 /*
581 * RB: At least A500/A2000's df0: don't identify themselves.
582 * As every (real) Amiga has at least a 3.5" DD drive as df0:
583 * we default to that if df0: doesn't identify as a certain
584 * type.
585 */
586 if(drive == 0 && id == FD_NODRIVE)
587 {
588 id = fd_def_df0;
589 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
590 }
591 /* return the ID value */
592 return (id);
593 }
594
595 static void fd_block_done(int irq, void *dummy, struct pt_regs *fp)
596 {
597 if (block_flag)
598 custom.dsklen = 0x4000;
599
600 if (block_flag == 2) { /* writing */
601 writepending = 2;
602 post_write_timer.expires = jiffies + 1; /* at least 2 ms */
603 post_write_timer.data = selected;
604 add_timer(&post_write_timer);
605 }
606 else { /* reading */
607 block_flag = 0;
608 wake_up (&wait_fd_block);
609 }
610 }
611
612 static void raw_read(int drive)
613 {
614 drive&=3;
615 get_fdc(drive);
616 while (block_flag)
617 sleep_on(&wait_fd_block);
618 fd_select(drive);
619 /* setup adkcon bits correctly */
620 custom.adkcon = ADK_MSBSYNC;
621 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
622
623 custom.dsksync = MFM_SYNC;
624
625 custom.dsklen = 0;
626 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
627 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
628 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
629
630 block_flag = 1;
631
632 while (block_flag)
633 sleep_on (&wait_fd_block);
634
635 custom.dsklen = 0;
636 fd_deselect(drive);
637 rel_fdc();
638 }
639
640 static int raw_write(int drive)
641 {
642 ushort adk;
643
644 drive&=3;
645 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
646 if ((ciaa.pra & DSKPROT) == 0) {
647 rel_fdc();
648 return 0;
649 }
650 while (block_flag)
651 sleep_on(&wait_fd_block);
652 fd_select(drive);
653 /* clear adkcon bits */
654 custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
655 /* set appropriate adkcon bits */
656 adk = ADK_SETCLR|ADK_FAST;
657 if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
658 adk |= ADK_PRECOMP1;
659 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
660 adk |= ADK_PRECOMP0;
661 custom.adkcon = adk;
662
663 custom.dsklen = DSKLEN_WRITE;
664 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
665 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
666 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
667
668 block_flag = 2;
669 return 1;
670 }
671
672 /*
673 * to be called at least 2ms after the write has finished but before any
674 * other access to the hardware.
675 */
676 static void post_write (unsigned long drive)
677 {
678 #ifdef DEBUG
679 printk("post_write for drive %ld\n",drive);
680 #endif
681 drive &= 3;
682 custom.dsklen = 0;
683 block_flag = 0;
684 writepending = 0;
685 writefromint = 0;
686 unit[drive].dirty = 0;
687 wake_up(&wait_fd_block);
688 fd_deselect(drive);
689 rel_fdc(); /* corresponds to get_fdc() in raw_write */
690 }
691
692
693 /*
694 * The following functions are to convert the block contents into raw data
695 * written to disk and vice versa.
696 * (Add other formats here ;-))
697 */
698
699 static unsigned long scan_sync(unsigned long raw, unsigned long end)
700 {
701 ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
702
703 while (ptr < endp && *ptr++ != 0x4489)
704 ;
705 if (ptr < endp) {
706 while (*ptr == 0x4489 && ptr < endp)
707 ptr++;
708 return (ulong)ptr;
709 }
710 return 0;
711 }
712
713 static inline unsigned long checksum(unsigned long *addr, int len)
714 {
715 unsigned long csum = 0;
716
717 len /= sizeof(*addr);
718 while (len-- > 0)
719 csum ^= *addr++;
720 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
721
722 return csum;
723 }
724
725 static unsigned long decode (unsigned long *data, unsigned long *raw,
726 int len)
727 {
728 ulong *odd, *even;
729
730 /* convert length from bytes to longwords */
731 len >>= 2;
732 odd = raw;
733 even = odd + len;
734
735 /* prepare return pointer */
736 raw += len * 2;
737
738 do {
739 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
740 } while (--len != 0);
741
742 return (ulong)raw;
743 }
744
745 struct header {
746 unsigned char magic;
747 unsigned char track;
748 unsigned char sect;
749 unsigned char ord;
750 unsigned char labels[16];
751 unsigned long hdrchk;
752 unsigned long datachk;
753 };
754
755 static int amiga_read(int drive)
756 {
757 unsigned long raw;
758 unsigned long end;
759 int scnt;
760 unsigned long csum;
761 struct header hdr;
762
763 drive&=3;
764 raw = (long) raw_buf;
765 end = raw + unit[drive].type->read_size;
766
767 for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
768 if (!(raw = scan_sync(raw, end))) {
769 printk (KERN_INFO "can't find sync for sector %d\n", scnt);
770 return MFM_NOSYNC;
771 }
772
773 raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
774 raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
775 raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
776 raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
777 csum = checksum((ulong *)&hdr,
778 (char *)&hdr.hdrchk-(char *)&hdr);
779
780 #ifdef DEBUG
781 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
782 hdr.magic, hdr.track, hdr.sect, hdr.ord,
783 *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
784 *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
785 hdr.hdrchk, hdr.datachk);
786 #endif
787
788 if (hdr.hdrchk != csum) {
789 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
790 return MFM_HEADER;
791 }
792
793 /* verify track */
794 if (hdr.track != unit[drive].track) {
795 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
796 return MFM_TRACK;
797 }
798
799 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
800 (ulong *)raw, 512);
801 csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
802
803 if (hdr.datachk != csum) {
804 printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
805 hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
806 hdr.datachk, csum);
807 printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
808 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
809 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
810 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
811 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
812 return MFM_DATA;
813 }
814 }
815
816 return 0;
817 }
818
819 static void encode(unsigned long data, unsigned long *dest)
820 {
821 unsigned long data2;
822
823 data &= 0x55555555;
824 data2 = data ^ 0x55555555;
825 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
826
827 if (*(dest - 1) & 0x00000001)
828 data &= 0x7FFFFFFF;
829
830 *dest = data;
831 }
832
833 static void encode_block(unsigned long *dest, unsigned long *src, int len)
834 {
835 int cnt, to_cnt = 0;
836 unsigned long data;
837
838 /* odd bits */
839 for (cnt = 0; cnt < len / 4; cnt++) {
840 data = src[cnt] >> 1;
841 encode(data, dest + to_cnt++);
842 }
843
844 /* even bits */
845 for (cnt = 0; cnt < len / 4; cnt++) {
846 data = src[cnt];
847 encode(data, dest + to_cnt++);
848 }
849 }
850
851 static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
852 {
853 struct header hdr;
854 int i;
855
856 disk&=3;
857 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
858 raw++;
859 *raw++ = 0x44894489;
860
861 hdr.magic = 0xFF;
862 hdr.track = unit[disk].track;
863 hdr.sect = cnt;
864 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
865 for (i = 0; i < 16; i++)
866 hdr.labels[i] = 0;
867 hdr.hdrchk = checksum((ulong *)&hdr,
868 (char *)&hdr.hdrchk-(char *)&hdr);
869 hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
870
871 encode_block(raw, (ulong *)&hdr.magic, 4);
872 raw += 2;
873 encode_block(raw, (ulong *)&hdr.labels, 16);
874 raw += 8;
875 encode_block(raw, (ulong *)&hdr.hdrchk, 4);
876 raw += 2;
877 encode_block(raw, (ulong *)&hdr.datachk, 4);
878 raw += 2;
879 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
880 raw += 256;
881
882 return raw;
883 }
884
885 static void amiga_write(int disk)
886 {
887 unsigned int cnt;
888 unsigned long *ptr = (unsigned long *)raw_buf;
889
890 disk&=3;
891 /* gap space */
892 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
893 *ptr++ = 0xaaaaaaaa;
894
895 /* sectors */
896 for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
897 ptr = putsec (disk, ptr, cnt);
898 *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
899 }
900
901
902 struct dos_header {
903 unsigned char track, /* 0-80 */
904 side, /* 0-1 */
905 sec, /* 0-...*/
906 len_desc;/* 2 */
907 unsigned short crc; /* on 68000 we got an alignment problem,
908 but this compiler solves it by adding silently
909 adding a pad byte so data won't fit
910 and this took about 3h to discover.... */
911 unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
912 };
913
914 /* crc routines are borrowed from the messydos-handler */
915
916 /* excerpt from the messydos-device
917 ; The CRC is computed not only over the actual data, but including
918 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
919 ; As we don't read or encode these fields into our buffers, we have to
920 ; preload the registers containing the CRC with the values they would have
921 ; after stepping over these fields.
922 ;
923 ; How CRCs "really" work:
924 ;
925 ; First, you should regard a bitstring as a series of coefficients of
926 ; polynomials. We calculate with these polynomials in modulo-2
927 ; arithmetic, in which both add and subtract are done the same as
928 ; exclusive-or. Now, we modify our data (a very long polynomial) in
929 ; such a way that it becomes divisible by the CCITT-standard 16-bit
930 ; 16 12 5
931 ; polynomial: x + x + x + 1, represented by $11021. The easiest
932 ; way to do this would be to multiply (using proper arithmetic) our
933 ; datablock with $11021. So we have:
934 ; data * $11021 =
935 ; data * ($10000 + $1021) =
936 ; data * $10000 + data * $1021
937 ; The left part of this is simple: Just add two 0 bytes. But then
938 ; the right part (data $1021) remains difficult and even could have
939 ; a carry into the left part. The solution is to use a modified
940 ; multiplication, which has a result that is not correct, but with
941 ; a difference of any multiple of $11021. We then only need to keep
942 ; the 16 least significant bits of the result.
943 ;
944 ; The following algorithm does this for us:
945 ;
946 ; unsigned char *data, c, crclo, crchi;
947 ; while (not done) {
948 ; c = *data++ + crchi;
949 ; crchi = (@ c) >> 8 + crclo;
950 ; crclo = @ c;
951 ; }
952 ;
953 ; Remember, + is done with EOR, the @ operator is in two tables (high
954 ; and low byte separately), which is calculated as
955 ;
956 ; $1021 * (c & $F0)
957 ; xor $1021 * (c & $0F)
958 ; xor $1021 * (c >> 4) (* is regular multiplication)
959 ;
960 ;
961 ; Anyway, the end result is the same as the remainder of the division of
962 ; the data by $11021. I am afraid I need to study theory a bit more...
963
964
965 my only works was to code this from manx to C....
966
967 */
968
969 static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
970 {
971 static unsigned char CRCTable1[] = {
972 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
973 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
974 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
975 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
976 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
977 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
978 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
979 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
980 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
981 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
982 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
983 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
984 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
985 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
986 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
987 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
988 };
989
990 static unsigned char CRCTable2[] = {
991 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
992 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
993 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
994 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
995 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
996 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
997 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
998 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
999 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
1000 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
1001 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
1002 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
1003 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
1004 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
1005 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
1006 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
1007 };
1008
1009 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
1010 register int i;
1011 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
1012
1013 CRCT1=CRCTable1;
1014 CRCT2=CRCTable2;
1015 data=data_a3;
1016 crcl=data_d1;
1017 crch=data_d0;
1018 for (i=data_d3; i>=0; i--) {
1019 c = (*data++) ^ crch;
1020 crch = CRCT1[c] ^ crcl;
1021 crcl = CRCT2[c];
1022 }
1023 return (crch<<8)|crcl;
1024 }
1025
1026 static inline ushort dos_hdr_crc (struct dos_header *hdr)
1027 {
1028 return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
1029 }
1030
1031 static inline ushort dos_data_crc(unsigned char *data)
1032 {
1033 return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
1034 }
1035
1036 static inline unsigned char dos_decode_byte(ushort word)
1037 {
1038 register ushort w2;
1039 register unsigned char byte;
1040 register unsigned char *dec = mfmdecode;
1041
1042 w2=word;
1043 w2>>=8;
1044 w2&=127;
1045 byte = dec[w2];
1046 byte <<= 4;
1047 w2 = word & 127;
1048 byte |= dec[w2];
1049 return byte;
1050 }
1051
1052 static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
1053 {
1054 int i;
1055
1056 for (i = 0; i < len; i++)
1057 *data++=dos_decode_byte(*raw++);
1058 return ((ulong)raw);
1059 }
1060
1061 #ifdef DEBUG
1062 static void dbg(unsigned long ptr)
1063 {
1064 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
1065 ((ulong *)ptr)[0], ((ulong *)ptr)[1],
1066 ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
1067 }
1068 #endif
1069
1070 static int dos_read(int drive)
1071 {
1072 unsigned long end;
1073 unsigned long raw;
1074 int scnt;
1075 unsigned short crc,data_crc[2];
1076 struct dos_header hdr;
1077
1078 drive&=3;
1079 raw = (long) raw_buf;
1080 end = raw + unit[drive].type->read_size;
1081
1082 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1083 do { /* search for the right sync of each sec-hdr */
1084 if (!(raw = scan_sync (raw, end))) {
1085 printk(KERN_INFO "dos_read: no hdr sync on "
1086 "track %d, unit %d for sector %d\n",
1087 unit[drive].track,drive,scnt);
1088 return MFM_NOSYNC;
1089 }
1090 #ifdef DEBUG
1091 dbg(raw);
1092 #endif
1093 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1094 raw+=2; /* skip over headermark */
1095 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1096 crc = dos_hdr_crc(&hdr);
1097
1098 #ifdef DEBUG
1099 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1100 hdr.sec, hdr.len_desc, hdr.crc);
1101 #endif
1102
1103 if (crc != hdr.crc) {
1104 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
1105 hdr.crc, crc);
1106 return MFM_HEADER;
1107 }
1108 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1109 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
1110 hdr.track,
1111 unit[drive].track/unit[drive].type->heads);
1112 return MFM_TRACK;
1113 }
1114
1115 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1116 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
1117 hdr.side,
1118 unit[drive].track%unit[drive].type->heads);
1119 return MFM_TRACK;
1120 }
1121
1122 if (hdr.len_desc != 2) {
1123 printk(KERN_INFO "dos_read: unknown sector len "
1124 "descriptor %d\n", hdr.len_desc);
1125 return MFM_DATA;
1126 }
1127 #ifdef DEBUG
1128 printk("hdr accepted\n");
1129 #endif
1130 if (!(raw = scan_sync (raw, end))) {
1131 printk(KERN_INFO "dos_read: no data sync on track "
1132 "%d, unit %d for sector%d, disk sector %d\n",
1133 unit[drive].track, drive, scnt, hdr.sec);
1134 return MFM_NOSYNC;
1135 }
1136 #ifdef DEBUG
1137 dbg(raw);
1138 #endif
1139
1140 if (*((ushort *)raw)!=0x5545) {
1141 printk(KERN_INFO "dos_read: no data mark after "
1142 "sync (%d,%d,%d,%d) sc=%d\n",
1143 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1144 return MFM_NOSYNC;
1145 }
1146
1147 raw+=2; /* skip data mark (included in checksum) */
1148 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1149 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1150 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1151
1152 if (crc != data_crc[0]) {
1153 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
1154 "sc=%d, %x %x\n", hdr.track, hdr.side,
1155 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
1156 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1157 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1158 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1159 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1160 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1161 return MFM_DATA;
1162 }
1163 }
1164 return 0;
1165 }
1166
1167 static inline ushort dos_encode_byte(unsigned char byte)
1168 {
1169 register unsigned char *enc, b2, b1;
1170 register ushort word;
1171
1172 enc=mfmencode;
1173 b1=byte;
1174 b2=b1>>4;
1175 b1&=15;
1176 word=enc[b2] <<8 | enc [b1];
1177 return (word|((word&(256|64)) ? 0: 128));
1178 }
1179
1180 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1181 {
1182 int i;
1183
1184 for (i = 0; i < len; i++) {
1185 *dest=dos_encode_byte(*src++);
1186 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1187 dest++;
1188 }
1189 }
1190
1191 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1192 {
1193 static struct dos_header hdr={0,0,0,2,0,
1194 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1195 int i;
1196 static ushort crc[2]={0,0x4e4e};
1197
1198 drive&=3;
1199 /* id gap 1 */
1200 /* the MFM word before is always 9254 */
1201 for(i=0;i<6;i++)
1202 *raw++=0xaaaaaaaa;
1203 /* 3 sync + 1 headermark */
1204 *raw++=0x44894489;
1205 *raw++=0x44895554;
1206
1207 /* fill in the variable parts of the header */
1208 hdr.track=unit[drive].track/unit[drive].type->heads;
1209 hdr.side=unit[drive].track%unit[drive].type->heads;
1210 hdr.sec=cnt+1;
1211 hdr.crc=dos_hdr_crc(&hdr);
1212
1213 /* header (without "magic") and id gap 2*/
1214 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1215 raw+=14;
1216
1217 /*id gap 3 */
1218 for(i=0;i<6;i++)
1219 *raw++=0xaaaaaaaa;
1220
1221 /* 3 syncs and 1 datamark */
1222 *raw++=0x44894489;
1223 *raw++=0x44895545;
1224
1225 /* data */
1226 dos_encode_block((ushort *)raw,
1227 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
1228 raw+=256;
1229
1230 /*data crc + jd's special gap (long words :-/) */
1231 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1232 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1233 raw+=2;
1234
1235 /* data gap */
1236 for(i=0;i<38;i++)
1237 *raw++=0x92549254;
1238
1239 return raw; /* wrote 652 MFM words */
1240 }
1241
1242 static void dos_write(int disk)
1243 {
1244 int cnt;
1245 unsigned long raw = (unsigned long) raw_buf;
1246 unsigned long *ptr=(unsigned long *)raw;
1247
1248 disk&=3;
1249 /* really gap4 + indexgap , but we write it first and round it up */
1250 for (cnt=0;cnt<425;cnt++)
1251 *ptr++=0x92549254;
1252
1253 /* the following is just guessed */
1254 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1255 for(cnt=0;cnt<473;cnt++)
1256 *ptr++=0x92549254;
1257
1258 /* now the index marks...*/
1259 for (cnt=0;cnt<20;cnt++)
1260 *ptr++=0x92549254;
1261 for (cnt=0;cnt<6;cnt++)
1262 *ptr++=0xaaaaaaaa;
1263 *ptr++=0x52245224;
1264 *ptr++=0x52245552;
1265 for (cnt=0;cnt<20;cnt++)
1266 *ptr++=0x92549254;
1267
1268 /* sectors */
1269 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1270 ptr=ms_putsec(disk,ptr,cnt);
1271
1272 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1273 }
1274
1275 /*
1276 * Here comes the high level stuff (i.e. the filesystem interface)
1277 * and helper functions.
1278 * Normally this should be the only part that has to be adapted to
1279 * different kernel versions.
1280 */
1281
1282 /* FIXME: this assumes the drive is still spinning -
1283 * which is only true if we complete writing a track within three seconds
1284 */
1285 static void flush_track_callback(unsigned long nr)
1286 {
1287 nr&=3;
1288 writefromint = 1;
1289 if (!try_fdc(nr)) {
1290 /* we might block in an interrupt, so try again later */
1291 flush_track_timer[nr].expires = jiffies + 1;
1292 add_timer(flush_track_timer + nr);
1293 return;
1294 }
1295 get_fdc(nr);
1296 (*unit[nr].dtype->write_fkt)(nr);
1297 if (!raw_write(nr)) {
1298 printk (KERN_NOTICE "floppy disk write protected\n");
1299 writefromint = 0;
1300 writepending = 0;
1301 }
1302 rel_fdc();
1303 }
1304
1305 static int non_int_flush_track (unsigned long nr)
1306 {
1307 unsigned long flags;
1308
1309 nr&=3;
1310 writefromint = 0;
1311 del_timer(&post_write_timer);
1312 get_fdc(nr);
1313 if (!fd_motor_on(nr)) {
1314 writepending = 0;
1315 rel_fdc();
1316 return 0;
1317 }
1318 save_flags(flags);
1319 cli();
1320 if (writepending != 2) {
1321 restore_flags(flags);
1322 (*unit[nr].dtype->write_fkt)(nr);
1323 if (!raw_write(nr)) {
1324 printk (KERN_NOTICE "floppy disk write protected "
1325 "in write!\n");
1326 writepending = 0;
1327 return 0;
1328 }
1329 while (block_flag == 2)
1330 sleep_on (&wait_fd_block);
1331 }
1332 else {
1333 restore_flags(flags);
1334 ms_delay(2); /* 2 ms post_write delay */
1335 post_write(nr);
1336 }
1337 rel_fdc();
1338 return 1;
1339 }
1340
1341 static int get_track(int drive, int track)
1342 {
1343 int error, errcnt;
1344
1345 drive&=3;
1346 if (unit[drive].track == track)
1347 return 0;
1348 get_fdc(drive);
1349 if (!fd_motor_on(drive)) {
1350 rel_fdc();
1351 return -1;
1352 }
1353
1354 if (unit[drive].dirty == 1) {
1355 del_timer (flush_track_timer + drive);
1356 non_int_flush_track (drive);
1357 }
1358 errcnt = 0;
1359 while (errcnt < MAX_ERRORS) {
1360 if (!fd_seek(drive, track))
1361 return -1;
1362 raw_read(drive);
1363 error = (*unit[drive].dtype->read_fkt)(drive);
1364 if (error == 0) {
1365 rel_fdc();
1366 return 0;
1367 }
1368 /* Read Error Handling: recalibrate and try again */
1369 unit[drive].track = -1;
1370 errcnt++;
1371 }
1372 rel_fdc();
1373 return -1;
1374 }
1375
1376 static void redo_fd_request(void)
1377 {
1378 unsigned int cnt, block, track, sector;
1379 int device, drive;
1380 struct amiga_floppy_struct *floppy;
1381 char *data;
1382 unsigned long flags;
1383
1384 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
1385 return;
1386 }
1387
1388 repeat:
1389 if (QUEUE_EMPTY) {
1390 /* Nothing left to do */
1391 return;
1392 }
1393
1394 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1395 panic(DEVICE_NAME ": request list destroyed");
1396
1397 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1398 panic(DEVICE_NAME ": block not locked");
1399
1400 device = MINOR(CURRENT_DEVICE);
1401 if (device < 8) {
1402 /* manual selection */
1403 drive = device & 3;
1404 floppy = unit + drive;
1405 } else {
1406 /* Auto-detection */
1407 #ifdef DEBUG
1408 printk("redo_fd_request: can't handle auto detect\n");
1409 printk("redo_fd_request: default to normal\n");
1410 #endif
1411 drive = device & 3;
1412 floppy = unit + drive;
1413 }
1414
1415 /* Here someone could investigate to be more efficient */
1416 for (cnt = 0; cnt < CURRENT->current_nr_sectors; cnt++) {
1417 #ifdef DEBUG
1418 printk("fd: sector %ld + %d requested for %s\n",
1419 CURRENT->sector,cnt,
1420 (CURRENT->cmd==READ)?"read":"write");
1421 #endif
1422 block = CURRENT->sector + cnt;
1423 if ((int)block > floppy->blocks) {
1424 end_request(0);
1425 goto repeat;
1426 }
1427
1428 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1429 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1430 data = CURRENT->buffer + 512 * cnt;
1431 #ifdef DEBUG
1432 printk("access to track %d, sector %d, with buffer at "
1433 "0x%08lx\n", track, sector, data);
1434 #endif
1435
1436 if ((CURRENT->cmd != READ) && (CURRENT->cmd != WRITE)) {
1437 printk(KERN_WARNING "do_fd_request: unknown command\n");
1438 end_request(0);
1439 goto repeat;
1440 }
1441 if (get_track(drive, track) == -1) {
1442 end_request(0);
1443 goto repeat;
1444 }
1445
1446 switch (CURRENT->cmd) {
1447 case READ:
1448 memcpy(data, unit[drive].trackbuf + sector * 512, 512);
1449 break;
1450
1451 case WRITE:
1452 memcpy(unit[drive].trackbuf + sector * 512, data, 512);
1453
1454 /* keep the drive spinning while writes are scheduled */
1455 if (!fd_motor_on(drive)) {
1456 end_request(0);
1457 goto repeat;
1458 }
1459 /*
1460 * setup a callback to write the track buffer
1461 * after a short (1 tick) delay.
1462 */
1463 save_flags (flags);
1464 cli();
1465
1466 unit[drive].dirty = 1;
1467 /* reset the timer */
1468 del_timer (flush_track_timer + drive);
1469
1470 flush_track_timer[drive].expires = jiffies + 1;
1471 add_timer (flush_track_timer + drive);
1472 restore_flags (flags);
1473 break;
1474 }
1475 }
1476 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1477 CURRENT->sector += CURRENT->current_nr_sectors;
1478
1479 end_request(1);
1480 goto repeat;
1481 }
1482
1483 static void do_fd_request(request_queue_t * q)
1484 {
1485 redo_fd_request();
1486 }
1487
1488 static int fd_ioctl(struct inode *inode, struct file *filp,
1489 unsigned int cmd, unsigned long param)
1490 {
1491 int drive = inode->i_rdev & 3;
1492 static struct floppy_struct getprm;
1493 struct super_block * sb;
1494
1495 switch(cmd){
1496 case HDIO_GETGEO:
1497 {
1498 struct hd_geometry loc;
1499 loc.heads = unit[drive].type->heads;
1500 loc.sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1501 loc.cylinders = unit[drive].type->tracks;
1502 loc.start = 0;
1503 if (copy_to_user((void *)param, (void *)&loc,
1504 sizeof(struct hd_geometry)))
1505 return -EFAULT;
1506 break;
1507 }
1508 case FDFMTBEG:
1509 get_fdc(drive);
1510 if (fd_ref[drive] > 1) {
1511 rel_fdc();
1512 return -EBUSY;
1513 }
1514 fsync_dev(inode->i_rdev);
1515 if (fd_motor_on(drive) == 0) {
1516 rel_fdc();
1517 return -ENODEV;
1518 }
1519 if (fd_calibrate(drive) == 0) {
1520 rel_fdc();
1521 return -ENXIO;
1522 }
1523 floppy_off(drive);
1524 rel_fdc();
1525 break;
1526 case FDFMTTRK:
1527 if (param < unit[drive].type->tracks * unit[drive].type->heads)
1528 {
1529 get_fdc(drive);
1530 if (fd_seek(drive,param) != 0){
1531 memset(unit[drive].trackbuf, FD_FILL_BYTE,
1532 unit[drive].dtype->sects * unit[drive].type->sect_mult * 512);
1533 non_int_flush_track(drive);
1534 }
1535 floppy_off(drive);
1536 rel_fdc();
1537 }
1538 else
1539 return -EINVAL;
1540 break;
1541 case FDFMTEND:
1542 floppy_off(drive);
1543 sb = get_super(inode->i_rdev);
1544 if (sb)
1545 invalidate_inodes(sb);
1546 invalidate_buffers(inode->i_rdev);
1547 break;
1548 case FDGETPRM:
1549 memset((void *)&getprm, 0, sizeof (getprm));
1550 getprm.track=unit[drive].type->tracks;
1551 getprm.head=unit[drive].type->heads;
1552 getprm.sect=unit[drive].dtype->sects * unit[drive].type->sect_mult;
1553 getprm.size=unit[drive].blocks;
1554 if (copy_to_user((void *)param,
1555 (void *)&getprm,
1556 sizeof(struct floppy_struct)))
1557 return -EFAULT;
1558 break;
1559 case BLKGETSIZE:
1560 return put_user(unit[drive].blocks,(long *)param);
1561 break;
1562 case FDSETPRM:
1563 case FDDEFPRM:
1564 return -EINVAL;
1565 case FDFLUSH: /* unconditionally, even if not needed */
1566 del_timer (flush_track_timer + drive);
1567 non_int_flush_track(drive);
1568 break;
1569 #ifdef RAW_IOCTL
1570 case IOCTL_RAW_TRACK:
1571 if (copy_to_user((void *)param, raw_buf,
1572 unit[drive].type->read_size))
1573 return -EFAULT;
1574 else
1575 return unit[drive].type->read_size;
1576 #endif
1577 default:
1578 printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
1579 cmd, drive);
1580 return -ENOSYS;
1581 }
1582 return 0;
1583 }
1584
1585 static void fd_probe(int dev)
1586 {
1587 unsigned long code;
1588 int type;
1589 int drive;
1590
1591 drive = dev & 3;
1592 code = fd_get_drive_id(drive);
1593
1594 /* get drive type */
1595 for (type = 0; type < num_dr_types; type++)
1596 if (drive_types[type].code == code)
1597 break;
1598
1599 if (type >= num_dr_types) {
1600 printk(KERN_WARNING "fd_probe: unsupported drive type "
1601 "%08lx found\n", code);
1602 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1603 return;
1604 }
1605
1606 unit[drive].type = drive_types + type;
1607 unit[drive].track = -1;
1608
1609 unit[drive].disk = -1;
1610 unit[drive].motor = 0;
1611 unit[drive].busy = 0;
1612 unit[drive].status = -1;
1613 }
1614
1615 /*
1616 * floppy_open check for aliasing (/dev/fd0 can be the same as
1617 * /dev/PS0 etc), and disallows simultaneous access to the same
1618 * drive with different device numbers.
1619 */
1620 static int floppy_open(struct inode *inode, struct file *filp)
1621 {
1622 int drive;
1623 int old_dev;
1624 int system;
1625 unsigned long flags;
1626
1627 drive = MINOR(inode->i_rdev) & 3;
1628 old_dev = fd_device[drive];
1629
1630 if (fd_ref[drive])
1631 if (old_dev != inode->i_rdev)
1632 return -EBUSY;
1633
1634 if (unit[drive].type->code == FD_NODRIVE)
1635 return -ENODEV;
1636
1637 if (filp && filp->f_mode & 3) {
1638 check_disk_change(inode->i_rdev);
1639 if (filp->f_mode & 2 ) {
1640 int wrprot;
1641
1642 get_fdc(drive);
1643 fd_select (drive);
1644 wrprot = !(ciaa.pra & DSKPROT);
1645 fd_deselect (drive);
1646 rel_fdc();
1647
1648 if (wrprot)
1649 return -EROFS;
1650 }
1651 }
1652
1653 save_flags(flags);
1654 cli();
1655 fd_ref[drive]++;
1656 fd_device[drive] = inode->i_rdev;
1657 #ifdef MODULE
1658 if (unit[drive].motor == 0)
1659 MOD_INC_USE_COUNT;
1660 #endif
1661 restore_flags(flags);
1662
1663 if (old_dev && old_dev != inode->i_rdev)
1664 invalidate_buffers(old_dev);
1665
1666 system=(inode->i_rdev & 4)>>2;
1667 unit[drive].dtype=&data_types[system];
1668 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1669 data_types[system].sects*unit[drive].type->sect_mult;
1670 floppy_sizes[MINOR(inode->i_rdev)] = unit[drive].blocks >> 1;
1671
1672 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1673 unit[drive].type->name, data_types[system].name);
1674
1675 return 0;
1676 }
1677
1678 static int floppy_release(struct inode * inode, struct file * filp)
1679 {
1680 #ifdef DEBUG
1681 struct super_block * sb;
1682 #endif
1683 int drive = MINOR(inode->i_rdev) & 3;
1684
1685 if (unit[drive].dirty == 1) {
1686 del_timer (flush_track_timer + drive);
1687 non_int_flush_track (drive);
1688 }
1689
1690 if (!fd_ref[drive]--) {
1691 printk(KERN_CRIT "floppy_release with fd_ref == 0");
1692 fd_ref[drive] = 0;
1693 }
1694 #ifdef MODULE
1695 /* the mod_use counter is handled this way */
1696 floppy_off (drive | 0x40000000);
1697 #endif
1698 return 0;
1699 }
1700
1701 /*
1702 * floppy-change is never called from an interrupt, so we can relax a bit
1703 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1704 * to the desired drive, but it will probably not survive the sleep if
1705 * several floppies are used at the same time: thus the loop.
1706 */
1707 static int amiga_floppy_change(kdev_t dev)
1708 {
1709 int drive = MINOR(dev) & 3;
1710 int changed;
1711 static int first_time = 1;
1712
1713 if (MAJOR(dev) != MAJOR_NR) {
1714 printk(KERN_CRIT "floppy_change: not a floppy\n");
1715 return 0;
1716 }
1717
1718 if (first_time)
1719 changed = first_time--;
1720 else {
1721 get_fdc(drive);
1722 fd_select (drive);
1723 changed = !(ciaa.pra & DSKCHANGE);
1724 fd_deselect (drive);
1725 rel_fdc();
1726 }
1727
1728 if (changed) {
1729 fd_probe(drive);
1730 unit[drive].track = -1;
1731 unit[drive].dirty = 0;
1732 writepending = 0; /* if this was true before, too bad! */
1733 writefromint = 0;
1734 return 1;
1735 }
1736 return 0;
1737 }
1738
1739 static struct block_device_operations floppy_fops = {
1740 open: floppy_open,
1741 release: floppy_release,
1742 ioctl: fd_ioctl,
1743 check_media_change: amiga_floppy_change,
1744 };
1745
1746 void __init amiga_floppy_setup (char *str, int *ints)
1747 {
1748 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", ints[1]);
1749 fd_def_df0 = ints[1];
1750 }
1751
1752 static int __init fd_probe_drives(void)
1753 {
1754 int drive,drives,nomem;
1755
1756 printk(KERN_INFO "FD: probing units\n" KERN_INFO "found ");
1757 drives=0;
1758 nomem=0;
1759 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1760 fd_probe(drive);
1761 if (unit[drive].type->code != FD_NODRIVE) {
1762 drives++;
1763 if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
1764 printk("no mem for ");
1765 unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
1766 drives--;
1767 nomem = 1;
1768 }
1769 printk("fd%d ",drive);
1770 }
1771 }
1772 if ((drives > 0) || (nomem == 0)) {
1773 if (drives == 0)
1774 printk("no drives");
1775 printk("\n");
1776 return drives;
1777 }
1778 printk("\n");
1779 return -ENOMEM;
1780 }
1781
1782 int __init amiga_floppy_init(void)
1783 {
1784 int i;
1785
1786 if (!AMIGAHW_PRESENT(AMI_FLOPPY))
1787 return -ENXIO;
1788
1789 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
1790 printk("fd: Unable to get major %d for floppy\n",MAJOR_NR);
1791 return -EBUSY;
1792 }
1793 /*
1794 * We request DSKPTR, DSKLEN and DSKDATA only, because the other
1795 * floppy registers are too spreaded over the custom register space
1796 */
1797 if (!request_mem_region(CUSTOM_PHYSADDR+0x20, 8, "amiflop [Paula]")) {
1798 printk("fd: cannot get floppy registers\n");
1799 unregister_blkdev(MAJOR_NR,"fd");
1800 return -EBUSY;
1801 }
1802 if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE, "Floppy")) ==
1803 NULL) {
1804 printk("fd: cannot get chip mem buffer\n");
1805 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1806 unregister_blkdev(MAJOR_NR,"fd");
1807 return -ENOMEM;
1808 }
1809 if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
1810 printk("fd: cannot get irq for dma\n");
1811 amiga_chip_free(raw_buf);
1812 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1813 unregister_blkdev(MAJOR_NR,"fd");
1814 return -EBUSY;
1815 }
1816 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
1817 printk("fd: cannot get irq for timer\n");
1818 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1819 amiga_chip_free(raw_buf);
1820 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1821 unregister_blkdev(MAJOR_NR,"fd");
1822 return -EBUSY;
1823 }
1824 if (fd_probe_drives() < 1) { /* No usable drives */
1825 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1826 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1827 amiga_chip_free(raw_buf);
1828 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1829 unregister_blkdev(MAJOR_NR,"fd");
1830 return -ENXIO;
1831 }
1832
1833 /* initialize variables */
1834 init_timer(&motor_on_timer);
1835 motor_on_timer.expires = 0;
1836 motor_on_timer.data = 0;
1837 motor_on_timer.function = motor_on_callback;
1838 for (i = 0; i < FD_MAX_UNITS; i++) {
1839 init_timer(&motor_off_timer[i]);
1840 motor_off_timer[i].expires = 0;
1841 motor_off_timer[i].data = i|0x80000000;
1842 motor_off_timer[i].function = fd_motor_off;
1843 init_timer(&flush_track_timer[i]);
1844 flush_track_timer[i].expires = 0;
1845 flush_track_timer[i].data = i;
1846 flush_track_timer[i].function = flush_track_callback;
1847
1848 unit[i].track = -1;
1849 }
1850
1851 init_timer(&post_write_timer);
1852 post_write_timer.expires = 0;
1853 post_write_timer.data = 0;
1854 post_write_timer.function = post_write;
1855
1856 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1857 blksize_size[MAJOR_NR] = floppy_blocksizes;
1858 blk_size[MAJOR_NR] = floppy_sizes;
1859
1860 for (i = 0; i < 128; i++)
1861 mfmdecode[i]=255;
1862 for (i = 0; i < 16; i++)
1863 mfmdecode[mfmencode[i]]=i;
1864
1865 /* make sure that disk DMA is enabled */
1866 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1867
1868 /* init ms timer */
1869 ciaa.crb = 8; /* one-shot, stop */
1870
1871 (void)do_floppy; /* avoid warning about unused variable */
1872 return 0;
1873 }
1874
1875 #ifdef MODULE
1876 #include <linux/version.h>
1877
1878 int init_module(void)
1879 {
1880 if (!MACH_IS_AMIGA)
1881 return -ENXIO;
1882 return amiga_floppy_init();
1883 }
1884
1885 void cleanup_module(void)
1886 {
1887 int i;
1888
1889 for( i = 0; i < FD_MAX_UNITS; i++)
1890 if (unit[i].type->code != FD_NODRIVE)
1891 kfree(unit[i].trackbuf);
1892 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1893 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1894 custom.dmacon = DMAF_DISK; /* disable DMA */
1895 amiga_chip_free(raw_buf);
1896 blk_size[MAJOR_NR] = NULL;
1897 blksize_size[MAJOR_NR] = NULL;
1898 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1899 release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
1900 unregister_blkdev(MAJOR_NR, "fd");
1901 }
1902 #endif
1903
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.