Blame view

drivers/block/amiflop.c 46.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  /*
   *  linux/amiga/amiflop.c
   *
   *  Copyright (C) 1993  Greg Harp
   *  Portions of this driver are based on code contributed by Brad Pepers
   *  
   *  revised 28.5.95 by Joerg Dorchain
   *  - now no bugs(?) any more for both HD & DD
   *  - added support for 40 Track 5.25" drives, 80-track hopefully behaves
   *    like 3.5" dd (no way to test - are there any 5.25" drives out there
   *    that work on an A4000?)
   *  - wrote formatting routine (maybe dirty, but works)
   *
   *  june/july 1995 added ms-dos support by Joerg Dorchain
   *  (portions based on messydos.device and various contributors)
   *  - currently only 9 and 18 sector disks
   *
   *  - fixed a bug with the internal trackbuffer when using multiple 
   *    disks the same time
   *  - made formatting a bit safer
   *  - added command line and machine based default for "silent" df0
   *
   *  december 1995 adapted for 1.2.13pl4 by Joerg Dorchain
   *  - works but I think it's inefficient. (look in redo_fd_request)
   *    But the changes were very efficient. (only three and a half lines)
   *
   *  january 1996 added special ioctl for tracking down read/write problems
   *  - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data
   *    is copied to area. (area should be large enough since no checking is
   *    done - 30K is currently sufficient). return the actual size of the
   *    trackbuffer
   *  - replaced udelays() by a timer (CIAA timer B) for the waits 
   *    needed for the disk mechanic.
   *
   *  february 1996 fixed error recovery and multiple disk access
   *  - both got broken the first time I tampered with the driver :-(
   *  - still not safe, but better than before
   *
   *  revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel.
   *  - Minor changes to accept the kdev_t.
   *  - Replaced some more udelays with ms_delays. Udelay is just a loop,
   *    and so the delay will be different depending on the given
   *    processor :-(
   *  - The driver could use a major cleanup because of the new
   *    major/minor handling that came with kdev_t. It seems to work for
   *    the time being, but I can't guarantee that it will stay like
   *    that when we start using 16 (24?) bit minors.
   *
   * restructured jan 1997 by Joerg Dorchain
   * - Fixed Bug accessing multiple disks
   * - some code cleanup
   * - added trackbuffer for each drive to speed things up
   * - fixed some race conditions (who finds the next may send it to me ;-)
   */
  
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
57
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
60
61
62
  
  #include <linux/fd.h>
  #include <linux/hdreg.h>
  #include <linux/delay.h>
  #include <linux/init.h>
2a48fc0ab   Arnd Bergmann   block: autoconver...
63
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
  #include <linux/amifdreg.h>
  #include <linux/amifd.h>
ff01bb483   Al Viro   fs: move code out...
66
  #include <linux/fs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
  #include <linux/blkdev.h>
  #include <linux/elevator.h>
b5dc7840b   Roman Zippel   [PATCH] m68k: int...
69
  #include <linux/interrupt.h>
92183b346   Geert Uytterhoeven   m68k: amiga - Flo...
70
  #include <linux/platform_device.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  
  #include <asm/setup.h>
  #include <asm/uaccess.h>
  #include <asm/amigahw.h>
  #include <asm/amigaints.h>
  #include <asm/irq.h>
  
  #undef DEBUG /* print _LOTS_ of infos */
  
  #define RAW_IOCTL
  #ifdef RAW_IOCTL
  #define IOCTL_RAW_TRACK 0x5254524B  /* 'RTRK' */
  #endif
  
  /*
   *  Defines
   */
  
  /*
   *  Error codes
   */
  #define FD_OK		0	/* operation succeeded */
  #define FD_ERROR	-1	/* general error (seek, read, write, etc) */
  #define FD_NOUNIT	1	/* unit does not exist */
  #define FD_UNITBUSY	2	/* unit already active */
  #define FD_NOTACTIVE	3	/* unit is not active */
  #define FD_NOTREADY	4	/* unit is not ready (motor not on/no disk) */
  
  #define MFM_NOSYNC	1
  #define MFM_HEADER	2
  #define MFM_DATA	3
  #define MFM_TRACK	4
  
  /*
   *  Floppy ID values
   */
  #define FD_NODRIVE	0x00000000  /* response when no unit is present */
  #define FD_DD_3 	0xffffffff  /* double-density 3.5" (880K) drive */
  #define FD_HD_3 	0x55555555  /* high-density 3.5" (1760K) drive */
  #define FD_DD_5 	0xaaaaaaaa  /* double-density 5.25" (440K) drive */
2a48fc0ab   Arnd Bergmann   block: autoconver...
111
  static DEFINE_MUTEX(amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
115
  static unsigned long int fd_def_df0 = FD_DD_3;     /* default for df0 if it doesn't identify */
  
  module_param(fd_def_df0, ulong, 0);
  MODULE_LICENSE("GPL");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  /*
   *  Macros
   */
  #define MOTOR_ON	(ciab.prb &= ~DSKMOTOR)
  #define MOTOR_OFF	(ciab.prb |= DSKMOTOR)
  #define SELECT(mask)    (ciab.prb &= ~mask)
  #define DESELECT(mask)  (ciab.prb |= mask)
  #define SELMASK(drive)  (1 << (3 + (drive & 3)))
  
  static struct fd_drive_type drive_types[] = {
  /*  code	name	   tr he   rdsz   wrsz sm pc1 pc2 sd  st st*/
  /*  warning: times are now in milliseconds (ms)                    */
  { FD_DD_3,	"DD 3.5",  80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
  { FD_HD_3,	"HD 3.5",  80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
  { FD_DD_5,	"DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
  { FD_NODRIVE, "No Drive", 0, 0,     0,     0, 0,  0,  0,  0,  0, 0}
  };
945f390f0   Tobias Klauser   [PATCH] drivers/b...
133
  static int num_dr_types = ARRAY_SIZE(drive_types);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  
  static int amiga_read(int), dos_read(int);
  static void amiga_write(int), dos_write(int);
  static struct fd_data_type data_types[] = {
  	{ "Amiga", 11 , amiga_read, amiga_write},
  	{ "MS-Dos", 9, dos_read, dos_write}
  };
  
  /* current info on each unit */
  static struct amiga_floppy_struct unit[FD_MAX_UNITS];
  
  static struct timer_list flush_track_timer[FD_MAX_UNITS];
  static struct timer_list post_write_timer;
  static struct timer_list motor_on_timer;
  static struct timer_list motor_off_timer[FD_MAX_UNITS];
  static int on_attempts;
  
  /* Synchronization of FDC access */
  /* request loop (trackbuffer) */
  static volatile int fdc_busy = -1;
  static volatile int fdc_nested;
  static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
   
6d0be946e   Andreas Bombe   m68k: amiflop - G...
157
  static DECLARE_COMPLETION(motor_on_completion);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
  
  static volatile int selected = -1;	/* currently selected drive */
  
  static int writepending;
  static int writefromint;
  static char *raw_buf;
786029ff8   Vivek Goyal   amiga floppy: Sto...
164
  static int fdc_queue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  
  static DEFINE_SPINLOCK(amiflop_lock);
  
  #define RAW_BUF_SIZE 30000  /* size of raw disk data */
  
  /*
   * These are global variables, as that's the easiest way to give
   * information to interrupts. They are the data used for the current
   * request.
   */
  static volatile char block_flag;
  static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
  
  /* MS-Dos MFM Coding tables (should go quick and easy) */
  static unsigned char mfmencode[16]={
  	0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
  	0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
  };
  static unsigned char mfmdecode[128];
  
  /* floppy internal millisecond timer stuff */
6d0be946e   Andreas Bombe   m68k: amiflop - G...
186
  static DECLARE_COMPLETION(ms_wait_completion);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
189
190
191
192
193
194
  #define MS_TICKS ((amiga_eclock+50)/1000)
  
  /*
   * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
   * max X times - some types of errors increase the errorcount by 2 or
   * even 3, so we might actually retry only X/2 times before giving up.
   */
  #define MAX_ERRORS 12
b4290a23c   Al Viro   [PATCH] m68k: nam...
195
  #define custom amiga_custom
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
200
201
202
203
204
205
206
  /* Prevent "aliased" accesses. */
  static int fd_ref[4] = { 0,0,0,0 };
  static int fd_device[4] = { 0, 0, 0, 0 };
  
  /*
   * Here come the actual hardware access and helper functions.
   * They are not reentrant and single threaded because all drives
   * share the same hardware and the same trackbuffer.
   */
  
  /* Milliseconds timer */
7d12e780e   David Howells   IRQ: Maintain reg...
207
  static irqreturn_t ms_isr(int irq, void *dummy)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
  {
6d0be946e   Andreas Bombe   m68k: amiflop - G...
209
  	complete(&ms_wait_completion);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
214
215
216
  	return IRQ_HANDLED;
  }
  
  /* all waits are queued up 
     A more generic routine would do a schedule a la timer.device */
  static void ms_delay(int ms)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
  	int ticks;
6d0be946e   Andreas Bombe   m68k: amiflop - G...
218
  	static DEFINE_MUTEX(mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219
  	if (ms > 0) {
6d0be946e   Andreas Bombe   m68k: amiflop - G...
220
  		mutex_lock(&mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
223
224
  		ticks = MS_TICKS*ms-1;
  		ciaa.tblo=ticks%256;
  		ciaa.tbhi=ticks/256;
  		ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
6d0be946e   Andreas Bombe   m68k: amiflop - G...
225
226
  		wait_for_completion(&ms_wait_completion);
  		mutex_unlock(&mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  	}
  }
  
  /* Hardware semaphore */
  
  /* returns true when we would get the semaphore */
  static inline int try_fdc(int drive)
  {
  	drive &= 3;
  	return ((fdc_busy < 0) || (fdc_busy == drive));
  }
  
  static void get_fdc(int drive)
  {
  	unsigned long flags;
  
  	drive &= 3;
  #ifdef DEBUG
  	printk("get_fdc: drive %d  fdc_busy %d  fdc_nested %d
  ",drive,fdc_busy,fdc_nested);
  #endif
  	local_irq_save(flags);
6d0be946e   Andreas Bombe   m68k: amiflop - G...
249
  	wait_event(fdc_wait, try_fdc(drive));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  	fdc_busy = drive;
  	fdc_nested++;
  	local_irq_restore(flags);
  }
  
  static inline void rel_fdc(void)
  {
  #ifdef DEBUG
  	if (fdc_nested == 0)
  		printk("fd: unmatched rel_fdc
  ");
  	printk("rel_fdc: fdc_busy %d fdc_nested %d
  ",fdc_busy,fdc_nested);
  #endif
  	fdc_nested--;
  	if (fdc_nested == 0) {
  		fdc_busy = -1;
  		wake_up(&fdc_wait);
  	}
  }
  
  static void fd_select (int drive)
  {
  	unsigned char prb = ~0;
  
  	drive&=3;
  #ifdef DEBUG
  	printk("selecting %d
  ",drive);
  #endif
  	if (drive == selected)
  		return;
  	get_fdc(drive);
  	selected = drive;
  
  	if (unit[drive].track % 2 != 0)
  		prb &= ~DSKSIDE;
  	if (unit[drive].motor == 1)
  		prb &= ~DSKMOTOR;
  	ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
  	ciab.prb = prb;
  	prb &= ~SELMASK(drive);
  	ciab.prb = prb;
  	rel_fdc();
  }
  
  static void fd_deselect (int drive)
  {
  	unsigned char prb;
  	unsigned long flags;
  
  	drive&=3;
  #ifdef DEBUG
  	printk("deselecting %d
  ",drive);
  #endif
  	if (drive != selected) {
  		printk(KERN_WARNING "Deselecting drive %d while %d was selected!
  ",drive,selected);
  		return;
  	}
  
  	get_fdc(drive);
  	local_irq_save(flags);
  
  	selected = -1;
  
  	prb = ciab.prb;
  	prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
  	ciab.prb = prb;
  
  	local_irq_restore (flags);
  	rel_fdc();
  
  }
  
  static void motor_on_callback(unsigned long nr)
  {
  	if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
6d0be946e   Andreas Bombe   m68k: amiflop - G...
329
  		complete_all(&motor_on_completion);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  	} else {
  		motor_on_timer.expires = jiffies + HZ/10;
  		add_timer(&motor_on_timer);
  	}
  }
  
  static int fd_motor_on(int nr)
  {
  	nr &= 3;
  
  	del_timer(motor_off_timer + nr);
  
  	if (!unit[nr].motor) {
  		unit[nr].motor = 1;
  		fd_select(nr);
6d0be946e   Andreas Bombe   m68k: amiflop - G...
345
  		INIT_COMPLETION(motor_on_completion);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
  		motor_on_timer.data = nr;
  		mod_timer(&motor_on_timer, jiffies + HZ/2);
  
  		on_attempts = 10;
6d0be946e   Andreas Bombe   m68k: amiflop - G...
350
  		wait_for_completion(&motor_on_completion);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
  		fd_deselect(nr);
  	}
  
  	if (on_attempts == 0) {
  		on_attempts = -1;
  #if 0
  		printk (KERN_ERR "motor_on failed, turning motor off
  ");
  		fd_motor_off (nr);
  		return 0;
  #else
  		printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding
  ");
  #endif
  	}
  
  	return 1;
  }
  
  static void fd_motor_off(unsigned long drive)
  {
  	long calledfromint;
  #ifdef MODULE
  	long decusecount;
  
  	decusecount = drive & 0x40000000;
  #endif
  	calledfromint = drive & 0x80000000;
  	drive&=3;
  	if (calledfromint && !try_fdc(drive)) {
  		/* We would be blocked in an interrupt, so try again later */
  		motor_off_timer[drive].expires = jiffies + 1;
  		add_timer(motor_off_timer + drive);
  		return;
  	}
  	unit[drive].motor = 0;
  	fd_select(drive);
  	udelay (1);
  	fd_deselect(drive);
  }
  
  static void floppy_off (unsigned int nr)
  {
  	int drive;
  
  	drive = nr & 3;
  	/* called this way it is always from interrupt */
  	motor_off_timer[drive].data = nr | 0x80000000;
  	mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
  }
  
  static int fd_calibrate(int drive)
  {
  	unsigned char prb;
  	int n;
  
  	drive &= 3;
  	get_fdc(drive);
  	if (!fd_motor_on (drive))
  		return 0;
  	fd_select (drive);
  	prb = ciab.prb;
  	prb |= DSKSIDE;
  	prb &= ~DSKDIREC;
  	ciab.prb = prb;
  	for (n = unit[drive].type->tracks/2; n != 0; --n) {
  		if (ciaa.pra & DSKTRACK0)
  			break;
  		prb &= ~DSKSTEP;
  		ciab.prb = prb;
  		prb |= DSKSTEP;
  		udelay (2);
  		ciab.prb = prb;
  		ms_delay(unit[drive].type->step_delay);
  	}
  	ms_delay (unit[drive].type->settle_time);
  	prb |= DSKDIREC;
  	n = unit[drive].type->tracks + 20;
  	for (;;) {
  		prb &= ~DSKSTEP;
  		ciab.prb = prb;
  		prb |= DSKSTEP;
  		udelay (2);
  		ciab.prb = prb;
  		ms_delay(unit[drive].type->step_delay + 1);
  		if ((ciaa.pra & DSKTRACK0) == 0)
  			break;
  		if (--n == 0) {
  			printk (KERN_ERR "fd%d: calibrate failed, turning motor off
  ", drive);
  			fd_motor_off (drive);
  			unit[drive].track = -1;
  			rel_fdc();
  			return 0;
  		}
  	}
  	unit[drive].track = 0;
  	ms_delay(unit[drive].type->settle_time);
  
  	rel_fdc();
  	fd_deselect(drive);
  	return 1;
  }
  
  static int fd_seek(int drive, int track)
  {
  	unsigned char prb;
  	int cnt;
  
  #ifdef DEBUG
  	printk("seeking drive %d to track %d
  ",drive,track);
  #endif
  	drive &= 3;
  	get_fdc(drive);
  	if (unit[drive].track == track) {
  		rel_fdc();
  		return 1;
  	}
  	if (!fd_motor_on(drive)) {
  		rel_fdc();
  		return 0;
  	}
  	if (unit[drive].track < 0 && !fd_calibrate(drive)) {
  		rel_fdc();
  		return 0;
  	}
  
  	fd_select (drive);
  	cnt = unit[drive].track/2 - track/2;
  	prb = ciab.prb;
  	prb |= DSKSIDE | DSKDIREC;
  	if (track % 2 != 0)
  		prb &= ~DSKSIDE;
  	if (cnt < 0) {
  		cnt = - cnt;
  		prb &= ~DSKDIREC;
  	}
  	ciab.prb = prb;
  	if (track % 2 != unit[drive].track % 2)
  		ms_delay (unit[drive].type->side_time);
  	unit[drive].track = track;
  	if (cnt == 0) {
  		rel_fdc();
  		fd_deselect(drive);
  		return 1;
  	}
  	do {
  		prb &= ~DSKSTEP;
  		ciab.prb = prb;
  		prb |= DSKSTEP;
  		udelay (1);
  		ciab.prb = prb;
  		ms_delay (unit[drive].type->step_delay);
  	} while (--cnt != 0);
  	ms_delay (unit[drive].type->settle_time);
  
  	rel_fdc();
  	fd_deselect(drive);
  	return 1;
  }
  
  static unsigned long fd_get_drive_id(int drive)
  {
  	int i;
  	ulong id = 0;
  
    	drive&=3;
    	get_fdc(drive);
  	/* set up for ID */
  	MOTOR_ON;
  	udelay(2);
  	SELECT(SELMASK(drive));
  	udelay(2);
  	DESELECT(SELMASK(drive));
  	udelay(2);
  	MOTOR_OFF;
  	udelay(2);
  	SELECT(SELMASK(drive));
  	udelay(2);
  	DESELECT(SELMASK(drive));
  	udelay(2);
  
  	/* loop and read disk ID */
  	for (i=0; i<32; i++) {
  		SELECT(SELMASK(drive));
  		udelay(2);
  
  		/* read and store value of DSKRDY */
  		id <<= 1;
  		id |= (ciaa.pra & DSKRDY) ? 0 : 1;	/* cia regs are low-active! */
  
  		DESELECT(SELMASK(drive));
  	}
  
  	rel_fdc();
  
          /*
           * RB: At least A500/A2000's df0: don't identify themselves.
           * As every (real) Amiga has at least a 3.5" DD drive as df0:
           * we default to that if df0: doesn't identify as a certain
           * type.
           */
          if(drive == 0 && id == FD_NODRIVE)
  	{
                  id = fd_def_df0;
                  printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx
  ", (ulong)fd_def_df0);
  	}
  	/* return the ID value */
  	return (id);
  }
7d12e780e   David Howells   IRQ: Maintain reg...
563
  static irqreturn_t fd_block_done(int irq, void *dummy)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  {
  	if (block_flag)
  		custom.dsklen = 0x4000;
  
  	if (block_flag == 2) { /* writing */
  		writepending = 2;
  		post_write_timer.expires = jiffies + 1; /* at least 2 ms */
  		post_write_timer.data = selected;
  		add_timer(&post_write_timer);
  	}
  	else {                /* reading */
  		block_flag = 0;
  		wake_up (&wait_fd_block);
  	}
  	return IRQ_HANDLED;
  }
  
  static void raw_read(int drive)
  {
  	drive&=3;
  	get_fdc(drive);
6d0be946e   Andreas Bombe   m68k: amiflop - G...
585
  	wait_event(wait_fd_block, !block_flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
587
588
589
590
591
592
593
594
595
596
597
598
  	fd_select(drive);
  	/* setup adkcon bits correctly */
  	custom.adkcon = ADK_MSBSYNC;
  	custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
  
  	custom.dsksync = MFM_SYNC;
  
  	custom.dsklen = 0;
  	custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
  	custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
  	custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
  
  	block_flag = 1;
6d0be946e   Andreas Bombe   m68k: amiflop - G...
599
  	wait_event(wait_fd_block, !block_flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  
  	custom.dsklen = 0;
  	fd_deselect(drive);
  	rel_fdc();
  }
  
  static int raw_write(int drive)
  {
  	ushort adk;
  
  	drive&=3;
  	get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
  	if ((ciaa.pra & DSKPROT) == 0) {
  		rel_fdc();
  		return 0;
  	}
6d0be946e   Andreas Bombe   m68k: amiflop - G...
616
  	wait_event(wait_fd_block, !block_flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
  	fd_select(drive);
  	/* clear adkcon bits */
  	custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
  	/* set appropriate adkcon bits */
  	adk = ADK_SETCLR|ADK_FAST;
  	if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
  		adk |= ADK_PRECOMP1;
  	else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
  		adk |= ADK_PRECOMP0;
  	custom.adkcon = adk;
  
  	custom.dsklen = DSKLEN_WRITE;
  	custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
  	custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
  	custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
  
  	block_flag = 2;
  	return 1;
  }
  
  /*
   * to be called at least 2ms after the write has finished but before any
   * other access to the hardware.
   */
  static void post_write (unsigned long drive)
  {
  #ifdef DEBUG
  	printk("post_write for drive %ld
  ",drive);
  #endif
  	drive &= 3;
  	custom.dsklen = 0;
  	block_flag = 0;
  	writepending = 0;
  	writefromint = 0;
  	unit[drive].dirty = 0;
  	wake_up(&wait_fd_block);
  	fd_deselect(drive);
  	rel_fdc(); /* corresponds to get_fdc() in raw_write */
  }
  
  
  /*
   * The following functions are to convert the block contents into raw data
   * written to disk and vice versa.
   * (Add other formats here ;-))
   */
  
  static unsigned long scan_sync(unsigned long raw, unsigned long end)
  {
  	ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
  
  	while (ptr < endp && *ptr++ != 0x4489)
  		;
  	if (ptr < endp) {
  		while (*ptr == 0x4489 && ptr < endp)
  			ptr++;
  		return (ulong)ptr;
  	}
  	return 0;
  }
  
  static inline unsigned long checksum(unsigned long *addr, int len)
  {
  	unsigned long csum = 0;
  
  	len /= sizeof(*addr);
  	while (len-- > 0)
  		csum ^= *addr++;
  	csum = ((csum>>1) & 0x55555555)  ^  (csum & 0x55555555);
  
  	return csum;
  }
  
  static unsigned long decode (unsigned long *data, unsigned long *raw,
  			     int len)
  {
  	ulong *odd, *even;
  
  	/* convert length from bytes to longwords */
  	len >>= 2;
  	odd = raw;
  	even = odd + len;
  
  	/* prepare return pointer */
  	raw += len * 2;
  
  	do {
  		*data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
  	} while (--len != 0);
  
  	return (ulong)raw;
  }
  
  struct header {
  	unsigned char magic;
  	unsigned char track;
  	unsigned char sect;
  	unsigned char ord;
  	unsigned char labels[16];
  	unsigned long hdrchk;
  	unsigned long datachk;
  };
  
  static int amiga_read(int drive)
  {
  	unsigned long raw;
  	unsigned long end;
  	int scnt;
  	unsigned long csum;
  	struct header hdr;
  
  	drive&=3;
  	raw = (long) raw_buf;
  	end = raw + unit[drive].type->read_size;
  
  	for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
  		if (!(raw = scan_sync(raw, end))) {
  			printk (KERN_INFO "can't find sync for sector %d
  ", scnt);
  			return MFM_NOSYNC;
  		}
  
  		raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
  		raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
  		raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
  		raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
  		csum = checksum((ulong *)&hdr,
  				(char *)&hdr.hdrchk-(char *)&hdr);
  
  #ifdef DEBUG
  		printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx
  ",
  			hdr.magic, hdr.track, hdr.sect, hdr.ord,
  			*(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
  			*(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
  			hdr.hdrchk, hdr.datachk);
  #endif
  
  		if (hdr.hdrchk != csum) {
  			printk(KERN_INFO "MFM_HEADER: %08lx,%08lx
  ", hdr.hdrchk, csum);
  			return MFM_HEADER;
  		}
  
  		/* verify track */
  		if (hdr.track != unit[drive].track) {
  			printk(KERN_INFO "MFM_TRACK: %d, %d
  ", hdr.track, unit[drive].track);
  			return MFM_TRACK;
  		}
  
  		raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
  			      (ulong *)raw, 512);
  		csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
  
  		if (hdr.datachk != csum) {
  			printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx
  ",
  			       hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
  			       hdr.datachk, csum);
  			printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)
  ",
  				((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
  				((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
  				((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
  				((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
  			return MFM_DATA;
  		}
  	}
  
  	return 0;
  }
  
  static void encode(unsigned long data, unsigned long *dest)
  {
  	unsigned long data2;
  
  	data &= 0x55555555;
  	data2 = data ^ 0x55555555;
  	data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
  
  	if (*(dest - 1) & 0x00000001)
  		data &= 0x7FFFFFFF;
  
  	*dest = data;
  }
  
  static void encode_block(unsigned long *dest, unsigned long *src, int len)
  {
  	int cnt, to_cnt = 0;
  	unsigned long data;
  
  	/* odd bits */
  	for (cnt = 0; cnt < len / 4; cnt++) {
  		data = src[cnt] >> 1;
  		encode(data, dest + to_cnt++);
  	}
  
  	/* even bits */
  	for (cnt = 0; cnt < len / 4; cnt++) {
  		data = src[cnt];
  		encode(data, dest + to_cnt++);
  	}
  }
  
  static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
  {
  	struct header hdr;
  	int i;
  
  	disk&=3;
  	*raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
  	raw++;
  	*raw++ = 0x44894489;
  
  	hdr.magic = 0xFF;
  	hdr.track = unit[disk].track;
  	hdr.sect = cnt;
  	hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
  	for (i = 0; i < 16; i++)
  		hdr.labels[i] = 0;
  	hdr.hdrchk = checksum((ulong *)&hdr,
  			      (char *)&hdr.hdrchk-(char *)&hdr);
  	hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
  
  	encode_block(raw, (ulong *)&hdr.magic, 4);
  	raw += 2;
  	encode_block(raw, (ulong *)&hdr.labels, 16);
  	raw += 8;
  	encode_block(raw, (ulong *)&hdr.hdrchk, 4);
  	raw += 2;
  	encode_block(raw, (ulong *)&hdr.datachk, 4);
  	raw += 2;
  	encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
  	raw += 256;
  
  	return raw;
  }
  
  static void amiga_write(int disk)
  {
  	unsigned int cnt;
  	unsigned long *ptr = (unsigned long *)raw_buf;
  
  	disk&=3;
  	/* gap space */
  	for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
  		*ptr++ = 0xaaaaaaaa;
  
  	/* sectors */
  	for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
  		ptr = putsec (disk, ptr, cnt);
  	*(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
  }
  
  
  struct dos_header {
  	unsigned char track,   /* 0-80 */
  		side,    /* 0-1 */
  		sec,     /* 0-...*/
  		len_desc;/* 2 */
  	unsigned short crc;     /* on 68000 we got an alignment problem, 
  				   but this compiler solves it  by adding silently 
  				   adding a pad byte so data won't fit
  				   and this took about 3h to discover.... */
  	unsigned char gap1[22];     /* for longword-alignedness (0x4e) */
  };
  
  /* crc routines are borrowed from the messydos-handler  */
  
  /* excerpt from the messydos-device           
  ; The CRC is computed not only over the actual data, but including
  ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
  ; As we don't read or encode these fields into our buffers, we have to
  ; preload the registers containing the CRC with the values they would have
  ; after stepping over these fields.
  ;
  ; How CRCs "really" work:
  ;
  ; First, you should regard a bitstring as a series of coefficients of
  ; polynomials. We calculate with these polynomials in modulo-2
  ; arithmetic, in which both add and subtract are done the same as
  ; exclusive-or. Now, we modify our data (a very long polynomial) in
  ; such a way that it becomes divisible by the CCITT-standard 16-bit
  ;		 16   12   5
  ; polynomial:	x  + x	+ x + 1, represented by $11021. The easiest
  ; way to do this would be to multiply (using proper arithmetic) our
  ; datablock with $11021. So we have:
  ;   data * $11021		 =
  ;   data * ($10000 + $1021)      =
  ;   data * $10000 + data * $1021
  ; The left part of this is simple: Just add two 0 bytes. But then
  ; the right part (data $1021) remains difficult and even could have
  ; a carry into the left part. The solution is to use a modified
  ; multiplication, which has a result that is not correct, but with
  ; a difference of any multiple of $11021. We then only need to keep
  ; the 16 least significant bits of the result.
  ;
  ; The following algorithm does this for us:
  ;
  ;   unsigned char *data, c, crclo, crchi;
  ;   while (not done) {
  ;	c = *data++ + crchi;
  ;	crchi = (@ c) >> 8 + crclo;
  ;	crclo = @ c;
  ;   }
  ;
  ; Remember, + is done with EOR, the @ operator is in two tables (high
  ; and low byte separately), which is calculated as
  ;
  ;      $1021 * (c & $F0)
  ;  xor $1021 * (c & $0F)
  ;  xor $1021 * (c >> 4)         (* is regular multiplication)
  ;
  ;
  ; Anyway, the end result is the same as the remainder of the division of
  ; the data by $11021. I am afraid I need to study theory a bit more...
  
  
  my only works was to code this from manx to C....
  
  */
  
  static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
  {
  	static unsigned char CRCTable1[] = {
  		0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
  		0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
  		0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
  		0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
  		0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
  		0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
  		0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
  		0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
  		0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
  		0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
  		0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
  		0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
  		0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
  		0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
  		0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
  		0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
  	};
  
  	static unsigned char CRCTable2[] = {
  		0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
  		0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
  		0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
  		0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
  		0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
  		0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
  		0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
  		0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
  		0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
  		0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
  		0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
  		0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
  		0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
  		0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
  		0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
  		0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
  	};
  
  /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
  	register int i;
  	register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
  
  	CRCT1=CRCTable1;
  	CRCT2=CRCTable2;
  	data=data_a3;
  	crcl=data_d1;
  	crch=data_d0;
  	for (i=data_d3; i>=0; i--) {
  		c = (*data++) ^ crch;
  		crch = CRCT1[c] ^ crcl;
  		crcl = CRCT2[c];
  	}
  	return (crch<<8)|crcl;
  }
  
  static inline ushort dos_hdr_crc (struct dos_header *hdr)
  {
  	return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
  }
  
  static inline ushort dos_data_crc(unsigned char *data)
  {
  	return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
  }
  
  static inline unsigned char dos_decode_byte(ushort word)
  {
  	register ushort w2;
  	register unsigned char byte;
  	register unsigned char *dec = mfmdecode;
  
  	w2=word;
  	w2>>=8;
  	w2&=127;
  	byte = dec[w2];
  	byte <<= 4;
  	w2 = word & 127;
  	byte |= dec[w2];
  	return byte;
  }
  
  static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
  {
  	int i;
  
  	for (i = 0; i < len; i++)
  		*data++=dos_decode_byte(*raw++);
  	return ((ulong)raw);
  }
  
  #ifdef DEBUG
  static void dbg(unsigned long ptr)
  {
  	printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx
  ", ptr,
  	       ((ulong *)ptr)[0], ((ulong *)ptr)[1],
  	       ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
  }
  #endif
  
  static int dos_read(int drive)
  {
  	unsigned long end;
  	unsigned long raw;
  	int scnt;
  	unsigned short crc,data_crc[2];
  	struct dos_header hdr;
  
  	drive&=3;
  	raw = (long) raw_buf;
  	end = raw + unit[drive].type->read_size;
  
  	for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
  		do { /* search for the right sync of each sec-hdr */
  			if (!(raw = scan_sync (raw, end))) {
  				printk(KERN_INFO "dos_read: no hdr sync on "
  				       "track %d, unit %d for sector %d
  ",
  				       unit[drive].track,drive,scnt);
  				return MFM_NOSYNC;
  			}
  #ifdef DEBUG
  			dbg(raw);
  #endif
  		} while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
  		raw+=2; /* skip over headermark */
  		raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
  		crc = dos_hdr_crc(&hdr);
  
  #ifdef DEBUG
  		printk("(%3d,%d,%2d,%d) %x
  ", hdr.track, hdr.side,
  		       hdr.sec, hdr.len_desc, hdr.crc);
  #endif
  
  		if (crc != hdr.crc) {
  			printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x
  ",
  			       hdr.crc, crc);
  			return MFM_HEADER;
  		}
  		if (hdr.track != unit[drive].track/unit[drive].type->heads) {
  			printk(KERN_INFO "dos_read: MFM_TRACK %d, %d
  ",
  			       hdr.track,
  			       unit[drive].track/unit[drive].type->heads);
  			return MFM_TRACK;
  		}
  
  		if (hdr.side != unit[drive].track%unit[drive].type->heads) {
  			printk(KERN_INFO "dos_read: MFM_SIDE %d, %d
  ",
  			       hdr.side,
  			       unit[drive].track%unit[drive].type->heads);
  			return MFM_TRACK;
  		}
  
  		if (hdr.len_desc != 2) {
  			printk(KERN_INFO "dos_read: unknown sector len "
  			       "descriptor %d
  ", hdr.len_desc);
  			return MFM_DATA;
  		}
  #ifdef DEBUG
  		printk("hdr accepted
  ");
  #endif
  		if (!(raw = scan_sync (raw, end))) {
  			printk(KERN_INFO "dos_read: no data sync on track "
  			       "%d, unit %d for sector%d, disk sector %d
  ",
  			       unit[drive].track, drive, scnt, hdr.sec);
  			return MFM_NOSYNC;
  		}
  #ifdef DEBUG
  		dbg(raw);
  #endif
  
  		if (*((ushort *)raw)!=0x5545) {
  			printk(KERN_INFO "dos_read: no data mark after "
  			       "sync (%d,%d,%d,%d) sc=%d
  ",
  			       hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
  			return MFM_NOSYNC;
  		}
  
  		raw+=2;  /* skip data mark (included in checksum) */
  		raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
  		raw = dos_decode((unsigned char  *)data_crc,(ushort *) raw,4);
  		crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
  
  		if (crc != data_crc[0]) {
  			printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
  			       "sc=%d, %x %x
  ", hdr.track, hdr.side,
  			       hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
  			printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)
  ",
  			       ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
  			       ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
  			       ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
  			       ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
  			return MFM_DATA;
  		}
  	}
  	return 0;
  }
  
  static inline ushort dos_encode_byte(unsigned char byte)
  {
  	register unsigned char *enc, b2, b1;
  	register ushort word;
  
  	enc=mfmencode;
  	b1=byte;
  	b2=b1>>4;
  	b1&=15;
  	word=enc[b2] <<8 | enc [b1];
  	return (word|((word&(256|64)) ? 0: 128));
  }
  
  static void dos_encode_block(ushort *dest, unsigned char *src, int len)
  {
  	int i;
  
  	for (i = 0; i < len; i++) {
  		*dest=dos_encode_byte(*src++);
  		*dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
  		dest++;
  	}
  }
  
  static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
  {
  	static struct dos_header hdr={0,0,0,2,0,
  	  {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
  	int i;
  	static ushort crc[2]={0,0x4e4e};
  
  	drive&=3;
  /* id gap 1 */
  /* the MFM word before is always 9254 */
  	for(i=0;i<6;i++)
  		*raw++=0xaaaaaaaa;
  /* 3 sync + 1 headermark */
  	*raw++=0x44894489;
  	*raw++=0x44895554;
  
  /* fill in the variable parts of the header */
  	hdr.track=unit[drive].track/unit[drive].type->heads;
  	hdr.side=unit[drive].track%unit[drive].type->heads;
  	hdr.sec=cnt+1;
  	hdr.crc=dos_hdr_crc(&hdr);
  
  /* header (without "magic") and id gap 2*/
  	dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
  	raw+=14;
  
  /*id gap 3 */
  	for(i=0;i<6;i++)
  		*raw++=0xaaaaaaaa;
  
  /* 3 syncs and 1 datamark */
  	*raw++=0x44894489;
  	*raw++=0x44895545;
  
  /* data */
  	dos_encode_block((ushort *)raw,
  			 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
  	raw+=256;
  
  /*data crc + jd's special gap (long words :-/) */
  	crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
  	dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
  	raw+=2;
  
  /* data gap */
  	for(i=0;i<38;i++)
  		*raw++=0x92549254;
  
  	return raw; /* wrote 652 MFM words */
  }
  
  static void dos_write(int disk)
  {
  	int cnt;
  	unsigned long raw = (unsigned long) raw_buf;
  	unsigned long *ptr=(unsigned long *)raw;
  
  	disk&=3;
  /* really gap4 + indexgap , but we write it first and round it up */
  	for (cnt=0;cnt<425;cnt++)
  		*ptr++=0x92549254;
  
  /* the following is just guessed */
  	if (unit[disk].type->sect_mult==2)  /* check for HD-Disks */
  		for(cnt=0;cnt<473;cnt++)
  			*ptr++=0x92549254;
  
  /* now the index marks...*/
  	for (cnt=0;cnt<20;cnt++)
  		*ptr++=0x92549254;
  	for (cnt=0;cnt<6;cnt++)
  		*ptr++=0xaaaaaaaa;
  	*ptr++=0x52245224;
  	*ptr++=0x52245552;
  	for (cnt=0;cnt<20;cnt++)
  		*ptr++=0x92549254;
  
  /* sectors */
  	for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
  		ptr=ms_putsec(disk,ptr,cnt);
  
  	*(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
  }
  
  /*
   * Here comes the high level stuff (i.e. the filesystem interface)
   * and helper functions.
   * Normally this should be the only part that has to be adapted to
   * different kernel versions.
   */
  
  /* FIXME: this assumes the drive is still spinning -
   * which is only true if we complete writing a track within three seconds
   */
  static void flush_track_callback(unsigned long nr)
  {
  	nr&=3;
  	writefromint = 1;
  	if (!try_fdc(nr)) {
  		/* we might block in an interrupt, so try again later */
  		flush_track_timer[nr].expires = jiffies + 1;
  		add_timer(flush_track_timer + nr);
  		return;
  	}
  	get_fdc(nr);
  	(*unit[nr].dtype->write_fkt)(nr);
  	if (!raw_write(nr)) {
  		printk (KERN_NOTICE "floppy disk write protected
  ");
  		writefromint = 0;
  		writepending = 0;
  	}
  	rel_fdc();
  }
  
  static int non_int_flush_track (unsigned long nr)
  {
  	unsigned long flags;
  
  	nr&=3;
  	writefromint = 0;
  	del_timer(&post_write_timer);
  	get_fdc(nr);
  	if (!fd_motor_on(nr)) {
  		writepending = 0;
  		rel_fdc();
  		return 0;
  	}
  	local_irq_save(flags);
  	if (writepending != 2) {
  		local_irq_restore(flags);
  		(*unit[nr].dtype->write_fkt)(nr);
  		if (!raw_write(nr)) {
  			printk (KERN_NOTICE "floppy disk write protected "
  				"in write!
  ");
  			writepending = 0;
  			return 0;
  		}
6d0be946e   Andreas Bombe   m68k: amiflop - G...
1314
  		wait_event(wait_fd_block, block_flag != 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
  	}
  	else {
  		local_irq_restore(flags);
  		ms_delay(2); /* 2 ms post_write delay */
  		post_write(nr);
  	}
  	rel_fdc();
  	return 1;
  }
  
  static int get_track(int drive, int track)
  {
  	int error, errcnt;
  
  	drive&=3;
  	if (unit[drive].track == track)
  		return 0;
  	get_fdc(drive);
  	if (!fd_motor_on(drive)) {
  		rel_fdc();
  		return -1;
  	}
  
  	if (unit[drive].dirty == 1) {
  		del_timer (flush_track_timer + drive);
  		non_int_flush_track (drive);
  	}
  	errcnt = 0;
  	while (errcnt < MAX_ERRORS) {
  		if (!fd_seek(drive, track))
  			return -1;
  		raw_read(drive);
  		error = (*unit[drive].dtype->read_fkt)(drive);
  		if (error == 0) {
  			rel_fdc();
  			return 0;
  		}
  		/* Read Error Handling: recalibrate and try again */
  		unit[drive].track = -1;
  		errcnt++;
  	}
  	rel_fdc();
  	return -1;
  }
786029ff8   Vivek Goyal   amiga floppy: Sto...
1359
1360
1361
1362
1363
1364
1365
  /*
   * Round-robin between our available drives, doing one request from each
   */
  static struct request *set_next_request(void)
  {
  	struct request_queue *q;
  	int cnt = FD_MAX_UNITS;
3e9bb2a07   Vivek Goyal   block: fix amiga ...
1366
  	struct request *rq = NULL;
786029ff8   Vivek Goyal   amiga floppy: Sto...
1367
1368
1369
1370
1371
  
  	/* Find next queue we can dispatch from */
  	fdc_queue = fdc_queue + 1;
  	if (fdc_queue == FD_MAX_UNITS)
  		fdc_queue = 0;
504c6d1b4   Vivek Goyal   amiga floppy: Com...
1372
  	for(cnt = FD_MAX_UNITS; cnt > 0; cnt--) {
786029ff8   Vivek Goyal   amiga floppy: Sto...
1373
1374
1375
1376
  
  		if (unit[fdc_queue].type->code == FD_NODRIVE) {
  			if (++fdc_queue == FD_MAX_UNITS)
  				fdc_queue = 0;
504c6d1b4   Vivek Goyal   amiga floppy: Com...
1377
  			continue;
786029ff8   Vivek Goyal   amiga floppy: Sto...
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
  		}
  
  		q = unit[fdc_queue].gendisk->queue;
  		if (q) {
  			rq = blk_fetch_request(q);
  			if (rq)
  				break;
  		}
  
  		if (++fdc_queue == FD_MAX_UNITS)
  			fdc_queue = 0;
  	}
  
  	return rq;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1393
1394
  static void redo_fd_request(void)
  {
9e31bebee   Tejun Heo   amiflop: dequeue ...
1395
  	struct request *rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1396
1397
1398
1399
1400
  	unsigned int cnt, block, track, sector;
  	int drive;
  	struct amiga_floppy_struct *floppy;
  	char *data;
  	unsigned long flags;
9e31bebee   Tejun Heo   amiflop: dequeue ...
1401
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1402

9e31bebee   Tejun Heo   amiflop: dequeue ...
1403
  next_req:
786029ff8   Vivek Goyal   amiga floppy: Sto...
1404
  	rq = set_next_request();
9e31bebee   Tejun Heo   amiflop: dequeue ...
1405
  	if (!rq) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1406
1407
1408
  		/* Nothing left to do */
  		return;
  	}
9e31bebee   Tejun Heo   amiflop: dequeue ...
1409
  	floppy = rq->rq_disk->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1410
  	drive = floppy - unit;
9e31bebee   Tejun Heo   amiflop: dequeue ...
1411
  next_segment:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1412
  	/* Here someone could investigate to be more efficient */
9e31bebee   Tejun Heo   amiflop: dequeue ...
1413
  	for (cnt = 0, err = 0; cnt < blk_rq_cur_sectors(rq); cnt++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
1415
1416
  #ifdef DEBUG
  		printk("fd: sector %ld + %d requested for %s
  ",
9e31bebee   Tejun Heo   amiflop: dequeue ...
1417
1418
  		       blk_rq_pos(rq), cnt,
  		       (rq_data_dir(rq) == READ) ? "read" : "write");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1419
  #endif
9e31bebee   Tejun Heo   amiflop: dequeue ...
1420
  		block = blk_rq_pos(rq) + cnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1421
  		if ((int)block > floppy->blocks) {
9e31bebee   Tejun Heo   amiflop: dequeue ...
1422
1423
  			err = -EIO;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1424
1425
1426
1427
  		}
  
  		track = block / (floppy->dtype->sects * floppy->type->sect_mult);
  		sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
9e31bebee   Tejun Heo   amiflop: dequeue ...
1428
  		data = rq->buffer + 512 * cnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1429
1430
1431
1432
1433
  #ifdef DEBUG
  		printk("access to track %d, sector %d, with buffer at "
  		       "0x%08lx
  ", track, sector, data);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1434
  		if (get_track(drive, track) == -1) {
9e31bebee   Tejun Heo   amiflop: dequeue ...
1435
1436
  			err = -EIO;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
  		}
9e31bebee   Tejun Heo   amiflop: dequeue ...
1438
  		if (rq_data_dir(rq) == READ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1439
  			memcpy(data, floppy->trackbuf + sector * 512, 512);
9e31bebee   Tejun Heo   amiflop: dequeue ...
1440
  		} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1441
1442
1443
1444
  			memcpy(floppy->trackbuf + sector * 512, data, 512);
  
  			/* keep the drive spinning while writes are scheduled */
  			if (!fd_motor_on(drive)) {
9e31bebee   Tejun Heo   amiflop: dequeue ...
1445
1446
  				err = -EIO;
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
  			}
  			/*
  			 * setup a callback to write the track buffer
  			 * after a short (1 tick) delay.
  			 */
  			local_irq_save(flags);
  
  			floppy->dirty = 1;
  		        /* reset the timer */
  			mod_timer (flush_track_timer + drive, jiffies + 1);
  			local_irq_restore(flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1458
1459
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1460

9e31bebee   Tejun Heo   amiflop: dequeue ...
1461
1462
1463
  	if (__blk_end_request_cur(rq, err))
  		goto next_segment;
  	goto next_req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1464
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
1465
  static void do_fd_request(struct request_queue * q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1466
1467
1468
  {
  	redo_fd_request();
  }
a885c8c43   Christoph Hellwig   [PATCH] Add block...
1469
1470
1471
1472
1473
1474
1475
1476
1477
  static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  {
  	int drive = MINOR(bdev->bd_dev) & 3;
  
  	geo->heads = unit[drive].type->heads;
  	geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
  	geo->cylinders = unit[drive].type->tracks;
  	return 0;
  }
8a6cfeb6d   Arnd Bergmann   block: push down ...
1478
  static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1479
1480
  		    unsigned int cmd, unsigned long param)
  {
47225db51   Al Viro   [PATCH] switch am...
1481
1482
  	struct amiga_floppy_struct *p = bdev->bd_disk->private_data;
  	int drive = p - unit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1483
  	static struct floppy_struct getprm;
8a423e542   Al Viro   [PATCH] m68k: ami...
1484
  	void __user *argp = (void __user *)param;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1485
1486
  
  	switch(cmd){
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1487
1488
1489
1490
1491
1492
  	case FDFMTBEG:
  		get_fdc(drive);
  		if (fd_ref[drive] > 1) {
  			rel_fdc();
  			return -EBUSY;
  		}
47225db51   Al Viro   [PATCH] switch am...
1493
  		fsync_bdev(bdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
  		if (fd_motor_on(drive) == 0) {
  			rel_fdc();
  			return -ENODEV;
  		}
  		if (fd_calibrate(drive) == 0) {
  			rel_fdc();
  			return -ENXIO;
  		}
  		floppy_off(drive);
  		rel_fdc();
  		break;
  	case FDFMTTRK:
47225db51   Al Viro   [PATCH] switch am...
1506
  		if (param < p->type->tracks * p->type->heads)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1507
1508
1509
  		{
  			get_fdc(drive);
  			if (fd_seek(drive,param) != 0){
47225db51   Al Viro   [PATCH] switch am...
1510
1511
  				memset(p->trackbuf, FD_FILL_BYTE,
  				       p->dtype->sects * p->type->sect_mult * 512);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
  				non_int_flush_track(drive);
  			}
  			floppy_off(drive);
  			rel_fdc();
  		}
  		else
  			return -EINVAL;
  		break;
  	case FDFMTEND:
  		floppy_off(drive);
47225db51   Al Viro   [PATCH] switch am...
1522
  		invalidate_bdev(bdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1523
1524
1525
  		break;
  	case FDGETPRM:
  		memset((void *)&getprm, 0, sizeof (getprm));
47225db51   Al Viro   [PATCH] switch am...
1526
1527
1528
1529
  		getprm.track=p->type->tracks;
  		getprm.head=p->type->heads;
  		getprm.sect=p->dtype->sects * p->type->sect_mult;
  		getprm.size=p->blocks;
8a423e542   Al Viro   [PATCH] m68k: ami...
1530
  		if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
  			return -EFAULT;
  		break;
  	case FDSETPRM:
  	case FDDEFPRM:
  		return -EINVAL;
  	case FDFLUSH: /* unconditionally, even if not needed */
  		del_timer (flush_track_timer + drive);
  		non_int_flush_track(drive);
  		break;
  #ifdef RAW_IOCTL
  	case IOCTL_RAW_TRACK:
47225db51   Al Viro   [PATCH] switch am...
1542
  		if (copy_to_user(argp, raw_buf, p->type->read_size))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1543
1544
  			return -EFAULT;
  		else
47225db51   Al Viro   [PATCH] switch am...
1545
  			return p->type->read_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1546
1547
1548
1549
1550
1551
1552
1553
  #endif
  	default:
  		printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
  		       cmd, drive);
  		return -ENOSYS;
  	}
  	return 0;
  }
8a6cfeb6d   Arnd Bergmann   block: push down ...
1554
1555
1556
1557
  static int fd_ioctl(struct block_device *bdev, fmode_t mode,
  			     unsigned int cmd, unsigned long param)
  {
  	int ret;
2a48fc0ab   Arnd Bergmann   block: autoconver...
1558
  	mutex_lock(&amiflop_mutex);
8a6cfeb6d   Arnd Bergmann   block: push down ...
1559
  	ret = fd_locked_ioctl(bdev, mode, cmd, param);
2a48fc0ab   Arnd Bergmann   block: autoconver...
1560
  	mutex_unlock(&amiflop_mutex);
8a6cfeb6d   Arnd Bergmann   block: push down ...
1561
1562
1563
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
  static void fd_probe(int dev)
  {
  	unsigned long code;
  	int type;
  	int drive;
  
  	drive = dev & 3;
  	code = fd_get_drive_id(drive);
  
  	/* get drive type */
  	for (type = 0; type < num_dr_types; type++)
  		if (drive_types[type].code == code)
  			break;
  
  	if (type >= num_dr_types) {
  		printk(KERN_WARNING "fd_probe: unsupported drive type "
  		       "%08lx found
  ", code);
  		unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
  		return;
  	}
  
  	unit[drive].type = drive_types + type;
  	unit[drive].track = -1;
  
  	unit[drive].disk = -1;
  	unit[drive].motor = 0;
  	unit[drive].busy = 0;
  	unit[drive].status = -1;
  }
  
  /*
   * floppy_open check for aliasing (/dev/fd0 can be the same as
   * /dev/PS0 etc), and disallows simultaneous access to the same
   * drive with different device numbers.
   */
47225db51   Al Viro   [PATCH] switch am...
1600
  static int floppy_open(struct block_device *bdev, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1601
  {
47225db51   Al Viro   [PATCH] switch am...
1602
1603
  	int drive = MINOR(bdev->bd_dev) & 3;
  	int system =  (MINOR(bdev->bd_dev) & 4) >> 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1604
1605
  	int old_dev;
  	unsigned long flags;
2a48fc0ab   Arnd Bergmann   block: autoconver...
1606
  	mutex_lock(&amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1607
  	old_dev = fd_device[drive];
6e9624b8c   Arnd Bergmann   block: push down ...
1608
  	if (fd_ref[drive] && old_dev != system) {
2a48fc0ab   Arnd Bergmann   block: autoconver...
1609
  		mutex_unlock(&amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1610
  		return -EBUSY;
6e9624b8c   Arnd Bergmann   block: push down ...
1611
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1612

47225db51   Al Viro   [PATCH] switch am...
1613
1614
1615
  	if (mode & (FMODE_READ|FMODE_WRITE)) {
  		check_disk_change(bdev);
  		if (mode & FMODE_WRITE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1616
1617
1618
1619
1620
1621
1622
  			int wrprot;
  
  			get_fdc(drive);
  			fd_select (drive);
  			wrprot = !(ciaa.pra & DSKPROT);
  			fd_deselect (drive);
  			rel_fdc();
6e9624b8c   Arnd Bergmann   block: push down ...
1623
  			if (wrprot) {
2a48fc0ab   Arnd Bergmann   block: autoconver...
1624
  				mutex_unlock(&amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1625
  				return -EROFS;
6e9624b8c   Arnd Bergmann   block: push down ...
1626
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
  		}
  	}
  
  	local_irq_save(flags);
  	fd_ref[drive]++;
  	fd_device[drive] = system;
  	local_irq_restore(flags);
  
  	unit[drive].dtype=&data_types[system];
  	unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
  		data_types[system].sects*unit[drive].type->sect_mult;
  	set_capacity(unit[drive].gendisk, unit[drive].blocks);
  
  	printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout
  ",drive,
  	       unit[drive].type->name, data_types[system].name);
2a48fc0ab   Arnd Bergmann   block: autoconver...
1643
  	mutex_unlock(&amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1644
1645
  	return 0;
  }
47225db51   Al Viro   [PATCH] switch am...
1646
  static int floppy_release(struct gendisk *disk, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1647
  {
47225db51   Al Viro   [PATCH] switch am...
1648
1649
  	struct amiga_floppy_struct *p = disk->private_data;
  	int drive = p - unit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1650

2a48fc0ab   Arnd Bergmann   block: autoconver...
1651
  	mutex_lock(&amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
  	if (unit[drive].dirty == 1) {
  		del_timer (flush_track_timer + drive);
  		non_int_flush_track (drive);
  	}
    
  	if (!fd_ref[drive]--) {
  		printk(KERN_CRIT "floppy_release with fd_ref == 0");
  		fd_ref[drive] = 0;
  	}
  #ifdef MODULE
  /* the mod_use counter is handled this way */
  	floppy_off (drive | 0x40000000);
  #endif
2a48fc0ab   Arnd Bergmann   block: autoconver...
1665
  	mutex_unlock(&amiflop_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1666
1667
1668
1669
  	return 0;
  }
  
  /*
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
1670
   * check_events is never called from an interrupt, so we can relax a bit
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671
1672
1673
1674
   * here, sleep etc. Note that floppy-on tries to set current_DOR to point
   * to the desired drive, but it will probably not survive the sleep if
   * several floppies are used at the same time: thus the loop.
   */
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
1675
  static unsigned amiga_check_events(struct gendisk *disk, unsigned int clearing)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
  {
  	struct amiga_floppy_struct *p = disk->private_data;
  	int drive = p - unit;
  	int changed;
  	static int first_time = 1;
  
  	if (first_time)
  		changed = first_time--;
  	else {
  		get_fdc(drive);
  		fd_select (drive);
  		changed = !(ciaa.pra & DSKCHANGE);
  		fd_deselect (drive);
  		rel_fdc();
  	}
  
  	if (changed) {
  		fd_probe(drive);
  		p->track = -1;
  		p->dirty = 0;
  		writepending = 0; /* if this was true before, too bad! */
  		writefromint = 0;
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
1698
  		return DISK_EVENT_MEDIA_CHANGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1699
1700
1701
  	}
  	return 0;
  }
83d5cde47   Alexey Dobriyan   const: make block...
1702
  static const struct block_device_operations floppy_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1703
  	.owner		= THIS_MODULE,
47225db51   Al Viro   [PATCH] switch am...
1704
1705
  	.open		= floppy_open,
  	.release	= floppy_release,
8a6cfeb6d   Arnd Bergmann   block: push down ...
1706
  	.ioctl		= fd_ioctl,
a885c8c43   Christoph Hellwig   [PATCH] Add block...
1707
  	.getgeo		= fd_getgeo,
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
1708
  	.check_events	= amiga_check_events,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1709
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1710
1711
1712
  static int __init fd_probe_drives(void)
  {
  	int drive,drives,nomem;
ad361c988   Joe Perches   Remove multiple K...
1713
1714
  	printk(KERN_INFO "FD: probing units
  found ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
  	drives=0;
  	nomem=0;
  	for(drive=0;drive<FD_MAX_UNITS;drive++) {
  		struct gendisk *disk;
  		fd_probe(drive);
  		if (unit[drive].type->code == FD_NODRIVE)
  			continue;
  		disk = alloc_disk(1);
  		if (!disk) {
  			unit[drive].type->code = FD_NODRIVE;
  			continue;
  		}
  		unit[drive].gendisk = disk;
786029ff8   Vivek Goyal   amiga floppy: Sto...
1728
1729
1730
1731
1732
1733
  
  		disk->queue = blk_init_queue(do_fd_request, &amiflop_lock);
  		if (!disk->queue) {
  			unit[drive].type->code = FD_NODRIVE;
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
  		drives++;
  		if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
  			printk("no mem for ");
  			unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
  			drives--;
  			nomem = 1;
  		}
  		printk("fd%d ",drive);
  		disk->major = FLOPPY_MAJOR;
  		disk->first_minor = drive;
  		disk->fops = &floppy_fops;
  		sprintf(disk->disk_name, "fd%d", drive);
  		disk->private_data = &unit[drive];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
  		set_capacity(disk, 880*2);
  		add_disk(disk);
  	}
  	if ((drives > 0) || (nomem == 0)) {
  		if (drives == 0)
  			printk("no drives");
  		printk("
  ");
  		return drives;
  	}
  	printk("
  ");
  	return -ENOMEM;
  }
   
  static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  {
  	int drive = *part & 3;
  	if (unit[drive].type->code == FD_NODRIVE)
  		return NULL;
  	*part = 0;
  	return get_disk(unit[drive].gendisk);
  }
92183b346   Geert Uytterhoeven   m68k: amiga - Flo...
1770
  static int __init amiga_floppy_probe(struct platform_device *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1771
1772
  {
  	int i, ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1773
1774
  	if (register_blkdev(FLOPPY_MAJOR,"fd"))
  		return -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1775
  	ret = -ENOMEM;
059718d57   Geert Uytterhoeven   m68k/block: amifl...
1776
1777
  	raw_buf = amiga_chip_alloc(RAW_BUF_SIZE, "Floppy");
  	if (!raw_buf) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1778
1779
  		printk("fd: cannot get chip mem buffer
  ");
92183b346   Geert Uytterhoeven   m68k: amiga - Flo...
1780
  		goto out_blkdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
  	}
  
  	ret = -EBUSY;
  	if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
  		printk("fd: cannot get irq for dma
  ");
  		goto out_irq;
  	}
  
  	if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
  		printk("fd: cannot get irq for timer
  ");
  		goto out_irq2;
  	}
fd5b462f0   Geert Uytterhoeven   m68k: Return -ENO...
1795
  	ret = -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
  	if (fd_probe_drives() < 1) /* No usable drives */
  		goto out_probe;
  
  	blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  				floppy_find, NULL, NULL);
  
  	/* initialize variables */
  	init_timer(&motor_on_timer);
  	motor_on_timer.expires = 0;
  	motor_on_timer.data = 0;
  	motor_on_timer.function = motor_on_callback;
  	for (i = 0; i < FD_MAX_UNITS; i++) {
  		init_timer(&motor_off_timer[i]);
  		motor_off_timer[i].expires = 0;
  		motor_off_timer[i].data = i|0x80000000;
  		motor_off_timer[i].function = fd_motor_off;
  		init_timer(&flush_track_timer[i]);
  		flush_track_timer[i].expires = 0;
  		flush_track_timer[i].data = i;
  		flush_track_timer[i].function = flush_track_callback;
  
  		unit[i].track = -1;
  	}
  
  	init_timer(&post_write_timer);
  	post_write_timer.expires = 0;
  	post_write_timer.data = 0;
  	post_write_timer.function = post_write;
    
  	for (i = 0; i < 128; i++)
  		mfmdecode[i]=255;
  	for (i = 0; i < 16; i++)
  		mfmdecode[mfmencode[i]]=i;
  
  	/* make sure that disk DMA is enabled */
  	custom.dmacon = DMAF_SETCLR | DMAF_DISK;
  
  	/* init ms timer */
  	ciaa.crb = 8; /* one-shot, stop */
  	return 0;
  
  out_probe:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1838
1839
1840
1841
1842
  	free_irq(IRQ_AMIGA_CIAA_TB, NULL);
  out_irq2:
  	free_irq(IRQ_AMIGA_DSKBLK, NULL);
  out_irq:
  	amiga_chip_free(raw_buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1843
1844
1845
1846
  out_blkdev:
  	unregister_blkdev(FLOPPY_MAJOR,"fd");
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1847
  #if 0 /* not safe to unload */
92183b346   Geert Uytterhoeven   m68k: amiga - Flo...
1848
  static int __exit amiga_floppy_remove(struct platform_device *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1849
1850
1851
1852
1853
  {
  	int i;
  
  	for( i = 0; i < FD_MAX_UNITS; i++) {
  		if (unit[i].type->code != FD_NODRIVE) {
786029ff8   Vivek Goyal   amiga floppy: Sto...
1854
  			struct request_queue *q = unit[i].gendisk->queue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1855
1856
1857
  			del_gendisk(unit[i].gendisk);
  			put_disk(unit[i].gendisk);
  			kfree(unit[i].trackbuf);
786029ff8   Vivek Goyal   amiga floppy: Sto...
1858
1859
  			if (q)
  				blk_cleanup_queue(q);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1860
1861
1862
1863
1864
1865
1866
  		}
  	}
  	blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  	free_irq(IRQ_AMIGA_CIAA_TB, NULL);
  	free_irq(IRQ_AMIGA_DSKBLK, NULL);
  	custom.dmacon = DMAF_DISK; /* disable DMA */
  	amiga_chip_free(raw_buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1867
1868
1869
  	unregister_blkdev(FLOPPY_MAJOR, "fd");
  }
  #endif
639074354   Al Viro   [PATCH] m68k: kil...
1870

92183b346   Geert Uytterhoeven   m68k: amiga - Flo...
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
  static struct platform_driver amiga_floppy_driver = {
  	.driver   = {
  		.name	= "amiga-floppy",
  		.owner	= THIS_MODULE,
  	},
  };
  
  static int __init amiga_floppy_init(void)
  {
  	return platform_driver_probe(&amiga_floppy_driver, amiga_floppy_probe);
  }
  
  module_init(amiga_floppy_init);
  
  #ifndef MODULE
639074354   Al Viro   [PATCH] m68k: kil...
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
  static int __init amiga_floppy_setup (char *str)
  {
  	int n;
  	if (!MACH_IS_AMIGA)
  		return 0;
  	if (!get_option(&str, &n))
  		return 0;
  	printk (KERN_INFO "amiflop: Setting default df0 to %x
  ", n);
  	fd_def_df0 = n;
9b41046cd   OGAWA Hirofumi   [PATCH] Don't pas...
1896
  	return 1;
639074354   Al Viro   [PATCH] m68k: kil...
1897
1898
1899
  }
  
  __setup("floppy=", amiga_floppy_setup);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1900
  #endif
92183b346   Geert Uytterhoeven   m68k: amiga - Flo...
1901
1902
  
  MODULE_ALIAS("platform:amiga-floppy");