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