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