blob: a5af1d6dda8bc37eb25b5127bffb80c4dfa04406 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/block/ataflop.c
3 *
4 * Copyright (C) 1993 Greg Harp
5 * Atari Support by Bjoern Brauel, Roman Hodek
6 *
7 * Big cleanup Sep 11..14 1994 Roman Hodek:
8 * - Driver now works interrupt driven
9 * - Support for two drives; should work, but I cannot test that :-(
10 * - Reading is done in whole tracks and buffered to speed up things
11 * - Disk change detection and drive deselecting after motor-off
12 * similar to TOS
13 * - Autodetection of disk format (DD/HD); untested yet, because I
14 * don't have an HD drive :-(
15 *
16 * Fixes Nov 13 1994 Martin Schaller:
17 * - Autodetection works now
18 * - Support for 5 1/4'' disks
19 * - Removed drive type (unknown on atari)
20 * - Do seeks with 8 Mhz
21 *
22 * Changes by Andreas Schwab:
23 * - After errors in multiple read mode try again reading single sectors
24 * (Feb 1995):
25 * - Clean up error handling
26 * - Set blk_size for proper size checking
27 * - Initialize track register when testing presence of floppy
28 * - Implement some ioctl's
29 *
30 * Changes by Torsten Lang:
31 * - When probing the floppies we should add the FDCCMDADD_H flag since
32 * the FDC will otherwise wait forever when no disk is inserted...
33 *
34 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
35 * - MFPDELAY() after each FDC access -> atari
36 * - more/other disk formats
37 * - DMA to the block buffer directly if we have a 32bit DMA
38 * - for medusa, the step rate is always 3ms
39 * - on medusa, use only cache_push()
40 * Roman:
41 * - Make disk format numbering independent from minors
42 * - Let user set max. supported drive type (speeds up format
43 * detection, saves buffer space)
44 *
45 * Roman 10/15/95:
46 * - implement some more ioctls
47 * - disk formatting
48 *
49 * Andreas 95/12/12:
50 * - increase gap size at start of track for HD/ED disks
51 *
52 * Michael (MSch) 11/07/96:
53 * - implemented FDSETPRM and FDDEFPRM ioctl
54 *
55 * Andreas (97/03/19):
56 * - implemented missing BLK* ioctls
57 *
58 * Things left to do:
59 * - Formatting
60 * - Maybe a better strategy for disk change detection (does anyone
61 * know one?)
62 */
63
64#include <linux/module.h>
65
66#include <linux/fd.h>
67#include <linux/delay.h>
68#include <linux/init.h>
69#include <linux/blkdev.h>
70
71#include <asm/atafd.h>
72#include <asm/atafdreg.h>
73#include <asm/atariints.h>
74#include <asm/atari_stdma.h>
75#include <asm/atari_stram.h>
76
77#define FD_MAX_UNITS 2
78
79#undef DEBUG
80
81static struct request_queue *floppy_queue;
Tejun Heoa336ca62009-05-08 11:54:03 +090082static struct request *fd_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* Disk types: DD, HD, ED */
85static struct atari_disk_type {
86 const char *name;
87 unsigned spt; /* sectors per track */
88 unsigned blocks; /* total number of blocks */
89 unsigned fdc_speed; /* fdc_speed setting */
90 unsigned stretch; /* track doubling ? */
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -080091} atari_disk_type[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
93 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
94 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
95 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
96/* formats above are probed for type DD */
97#define MAX_TYPE_DD 3
98 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
99 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
100 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
101/* formats above are probed for types DD and HD */
102#define MAX_TYPE_HD 6
103 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
104 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
105/* formats above are probed for types DD, HD and ED */
106#define MAX_TYPE_ED 8
107/* types below are never autoprobed */
108 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
109 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
110 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
111 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
112 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
113 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
114 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
115 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
116 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
117 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
118 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
119 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
120 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
121 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
122 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
123 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
124 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
125 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
126 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
127};
128
129static int StartDiskType[] = {
130 MAX_TYPE_DD,
131 MAX_TYPE_HD,
132 MAX_TYPE_ED
133};
134
135#define TYPE_DD 0
136#define TYPE_HD 1
137#define TYPE_ED 2
138
139static int DriveType = TYPE_HD;
140
141static DEFINE_SPINLOCK(ataflop_lock);
142
143/* Array for translating minors into disk formats */
144static struct {
145 int index;
146 unsigned drive_types;
147} minor2disktype[] = {
148 { 0, TYPE_DD }, /* 1: d360 */
149 { 4, TYPE_HD }, /* 2: h1200 */
150 { 1, TYPE_DD }, /* 3: D360 */
151 { 2, TYPE_DD }, /* 4: D720 */
152 { 1, TYPE_DD }, /* 5: h360 = D360 */
153 { 2, TYPE_DD }, /* 6: h720 = D720 */
154 { 5, TYPE_HD }, /* 7: H1440 */
155 { 7, TYPE_ED }, /* 8: E2880 */
156/* some PC formats :-) */
157 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
158 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
159 { 9, TYPE_HD }, /* 11: H1680 */
160 { 10, TYPE_DD }, /* 12: h410 */
161 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
162 { 11, TYPE_HD }, /* 14: h1476 */
163 { 12, TYPE_HD }, /* 15: H1722 */
164 { 13, TYPE_DD }, /* 16: h420 */
165 { 14, TYPE_DD }, /* 17: H830 */
166 { 15, TYPE_HD }, /* 18: h1494 */
167 { 16, TYPE_HD }, /* 19: H1743 */
168 { 17, TYPE_DD }, /* 20: h880 */
169 { 18, TYPE_DD }, /* 21: D1040 */
170 { 19, TYPE_DD }, /* 22: D1120 */
171 { 20, TYPE_HD }, /* 23: h1600 */
172 { 21, TYPE_HD }, /* 24: H1760 */
173 { 22, TYPE_HD }, /* 25: H1920 */
174 { 23, TYPE_ED }, /* 26: E3200 */
175 { 24, TYPE_ED }, /* 27: E3520 */
176 { 25, TYPE_ED }, /* 28: E3840 */
177 { 26, TYPE_HD }, /* 29: H1840 */
178 { 27, TYPE_DD }, /* 30: D800 */
179 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
180};
181
Tobias Klauser945f3902006-01-08 01:05:11 -0800182#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184/*
185 * Maximum disk size (in kilobytes). This default is used whenever the
186 * current disk size is unknown.
187 */
188#define MAX_DISK_SIZE 3280
189
190/*
191 * MSch: User-provided type information. 'drive' points to
192 * the respective entry of this array. Set by FDSETPRM ioctls.
193 */
194static struct atari_disk_type user_params[FD_MAX_UNITS];
195
196/*
197 * User-provided permanent type information. 'drive' points to
198 * the respective entry of this array. Set by FDDEFPRM ioctls,
199 * restored upon disk change by floppy_revalidate() if valid (as seen by
200 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
201 */
202static struct atari_disk_type default_params[FD_MAX_UNITS];
203
204/* current info on each unit */
205static struct atari_floppy_struct {
206 int connected; /* !=0 : drive is connected */
207 int autoprobe; /* !=0 : do autoprobe */
208
209 struct atari_disk_type *disktype; /* current type of disk */
210
211 int track; /* current head position or -1 if
212 unknown */
213 unsigned int steprate; /* steprate setting */
214 unsigned int wpstat; /* current state of WP signal (for
215 disk change detection) */
216 int flags; /* flags */
217 struct gendisk *disk;
218 int ref;
219 int type;
220} unit[FD_MAX_UNITS];
221
222#define UD unit[drive]
223#define UDT unit[drive].disktype
224#define SUD unit[SelectedDrive]
225#define SUDT unit[SelectedDrive].disktype
226
227
228#define FDC_READ(reg) ({ \
229 /* unsigned long __flags; */ \
230 unsigned short __val; \
231 /* local_irq_save(__flags); */ \
232 dma_wd.dma_mode_status = 0x80 | (reg); \
233 udelay(25); \
234 __val = dma_wd.fdc_acces_seccount; \
235 MFPDELAY(); \
236 /* local_irq_restore(__flags); */ \
237 __val & 0xff; \
238})
239
240#define FDC_WRITE(reg,val) \
241 do { \
242 /* unsigned long __flags; */ \
243 /* local_irq_save(__flags); */ \
244 dma_wd.dma_mode_status = 0x80 | (reg); \
245 udelay(25); \
246 dma_wd.fdc_acces_seccount = (val); \
247 MFPDELAY(); \
248 /* local_irq_restore(__flags); */ \
249 } while(0)
250
251
252/* Buffering variables:
253 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
254 * operations. Second, a track buffer is used to cache a whole track
255 * of the disk to save read operations. These are two separate buffers
256 * because that allows write operations without clearing the track buffer.
257 */
258
259static int MaxSectors[] = {
260 11, 22, 44
261};
262static int BufferSize[] = {
263 15*512, 30*512, 60*512
264};
265
266#define BUFFER_SIZE (BufferSize[DriveType])
267
268unsigned char *DMABuffer; /* buffer for writes */
269static unsigned long PhysDMABuffer; /* physical address */
270
271static int UseTrackbuffer = -1; /* Do track buffering? */
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800272module_param(UseTrackbuffer, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274unsigned char *TrackBuffer; /* buffer for reads */
275static unsigned long PhysTrackBuffer; /* physical address */
276static int BufferDrive, BufferSide, BufferTrack;
277static int read_track; /* non-zero if we are reading whole tracks */
278
279#define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
280#define IS_BUFFERED(drive,side,track) \
281 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
282
283/*
284 * These are global variables, as that's the easiest way to give
285 * information to interrupts. They are the data used for the current
286 * request.
287 */
288static int SelectedDrive = 0;
289static int ReqCmd, ReqBlock;
290static int ReqSide, ReqTrack, ReqSector, ReqCnt;
291static int HeadSettleFlag = 0;
292static unsigned char *ReqData, *ReqBuffer;
293static int MotorOn = 0, MotorOffTrys;
294static int IsFormatting = 0, FormatError;
295
296static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800297module_param_array(UserSteprate, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/* Synchronization of FDC access. */
300static volatile int fdc_busy = 0;
301static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
302static DECLARE_WAIT_QUEUE_HEAD(format_wait);
303
304static unsigned long changed_floppies = 0xff, fake_change = 0;
305#define CHECK_CHANGE_DELAY HZ/2
306
307#define FD_MOTOR_OFF_DELAY (3*HZ)
308#define FD_MOTOR_OFF_MAXTRY (10*20)
309
310#define FLOPPY_TIMEOUT (6*HZ)
311#define RECALIBRATE_ERRORS 4 /* After this many errors the drive
312 * will be recalibrated. */
313#define MAX_ERRORS 8 /* After this many errors the driver
314 * will give up. */
315
316
317/*
318 * The driver is trying to determine the correct media format
319 * while Probing is set. fd_rwsec_done() clears it after a
320 * successful access.
321 */
322static int Probing = 0;
323
324/* This flag is set when a dummy seek is necessary to make the WP
325 * status bit accessible.
326 */
327static int NeedSeek = 0;
328
329
330#ifdef DEBUG
331#define DPRINT(a) printk a
332#else
333#define DPRINT(a)
334#endif
335
336/***************************** Prototypes *****************************/
337
338static void fd_select_side( int side );
339static void fd_select_drive( int drive );
340static void fd_deselect( void );
341static void fd_motor_off_timer( unsigned long dummy );
342static void check_change( unsigned long dummy );
David Howells7d12e782006-10-05 14:55:46 +0100343static irqreturn_t floppy_irq (int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static void fd_error( void );
345static int do_format(int drive, int type, struct atari_format_descr *desc);
346static void do_fd_action( int drive );
347static void fd_calibrate( void );
348static void fd_calibrate_done( int status );
349static void fd_seek( void );
350static void fd_seek_done( int status );
351static void fd_rwsec( void );
352static void fd_readtrack_check( unsigned long dummy );
353static void fd_rwsec_done( int status );
354static void fd_rwsec_done1(int status);
355static void fd_writetrack( void );
356static void fd_writetrack_done( int status );
357static void fd_times_out( unsigned long dummy );
358static void finish_fdc( void );
359static void finish_fdc_done( int dummy );
360static void setup_req_params( int drive );
361static void redo_fd_request( void);
Al Viro60ad2342008-03-02 09:23:54 -0500362static int fd_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 cmd, unsigned long param);
364static void fd_probe( int drive );
365static int fd_test_drive_present( int drive );
366static void config_types( void );
Al Viro60ad2342008-03-02 09:23:54 -0500367static int floppy_open(struct block_device *bdev, fmode_t mode);
368static int floppy_release(struct gendisk *disk, fmode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370/************************* End of Prototypes **************************/
371
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700372static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
373static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
374static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
375static DEFINE_TIMER(fd_timer, check_change, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Tejun Heoa336ca62009-05-08 11:54:03 +0900377static void fd_end_request_cur(int err)
378{
379 if (!__blk_end_request_cur(fd_request, err))
380 fd_request = NULL;
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static inline void start_motor_off_timer(void)
384{
385 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
386 MotorOffTrys = 0;
387}
388
389static inline void start_check_change_timer( void )
390{
391 mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
392}
393
394static inline void start_timeout(void)
395{
396 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
397}
398
399static inline void stop_timeout(void)
400{
401 del_timer(&timeout_timer);
402}
403
404/* Select the side to use. */
405
406static void fd_select_side( int side )
407{
408 unsigned long flags;
409
410 /* protect against various other ints mucking around with the PSG */
411 local_irq_save(flags);
412
413 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
414 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
415 sound_ym.rd_data_reg_sel & 0xfe;
416
417 local_irq_restore(flags);
418}
419
420
421/* Select a drive, update the FDC's track register and set the correct
422 * clock speed for this disk's type.
423 */
424
425static void fd_select_drive( int drive )
426{
427 unsigned long flags;
428 unsigned char tmp;
429
430 if (drive == SelectedDrive)
431 return;
432
433 /* protect against various other ints mucking around with the PSG */
434 local_irq_save(flags);
435 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
436 tmp = sound_ym.rd_data_reg_sel;
437 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
438 atari_dont_touch_floppy_select = 1;
439 local_irq_restore(flags);
440
441 /* restore track register to saved value */
442 FDC_WRITE( FDCREG_TRACK, UD.track );
443 udelay(25);
444
445 /* select 8/16 MHz */
446 if (UDT)
447 if (ATARIHW_PRESENT(FDCSPEED))
448 dma_wd.fdc_speed = UDT->fdc_speed;
449
450 SelectedDrive = drive;
451}
452
453
454/* Deselect both drives. */
455
456static void fd_deselect( void )
457{
458 unsigned long flags;
459
460 /* protect against various other ints mucking around with the PSG */
461 local_irq_save(flags);
462 atari_dont_touch_floppy_select = 0;
463 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
464 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
465 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
466 /* On Falcon, the drive B select line is used on the printer port, so
467 * leave it alone... */
468 SelectedDrive = -1;
469 local_irq_restore(flags);
470}
471
472
473/* This timer function deselects the drives when the FDC switched the
474 * motor off. The deselection cannot happen earlier because the FDC
475 * counts the index signals, which arrive only if one drive is selected.
476 */
477
478static void fd_motor_off_timer( unsigned long dummy )
479{
480 unsigned char status;
481
482 if (SelectedDrive < 0)
483 /* no drive selected, needn't deselect anyone */
484 return;
485
486 if (stdma_islocked())
487 goto retry;
488
489 status = FDC_READ( FDCREG_STATUS );
490
491 if (!(status & 0x80)) {
492 /* motor already turned off by FDC -> deselect drives */
493 MotorOn = 0;
494 fd_deselect();
495 return;
496 }
497 /* not yet off, try again */
498
499 retry:
500 /* Test again later; if tested too often, it seems there is no disk
501 * in the drive and the FDC will leave the motor on forever (or,
502 * at least until a disk is inserted). So we'll test only twice
503 * per second from then on...
504 */
505 mod_timer(&motor_off_timer,
506 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
507}
508
509
510/* This function is repeatedly called to detect disk changes (as good
511 * as possible) and keep track of the current state of the write protection.
512 */
513
514static void check_change( unsigned long dummy )
515{
516 static int drive = 0;
517
518 unsigned long flags;
519 unsigned char old_porta;
520 int stat;
521
522 if (++drive > 1 || !UD.connected)
523 drive = 0;
524
525 /* protect against various other ints mucking around with the PSG */
526 local_irq_save(flags);
527
528 if (!stdma_islocked()) {
529 sound_ym.rd_data_reg_sel = 14;
530 old_porta = sound_ym.rd_data_reg_sel;
531 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
532 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
533 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
534 sound_ym.wd_data = old_porta;
535
536 if (stat != UD.wpstat) {
537 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
538 UD.wpstat = stat;
539 set_bit (drive, &changed_floppies);
540 }
541 }
542 local_irq_restore(flags);
543
544 start_check_change_timer();
545}
546
547
548/* Handling of the Head Settling Flag: This flag should be set after each
549 * seek operation, because we don't use seeks with verify.
550 */
551
552static inline void set_head_settle_flag(void)
553{
554 HeadSettleFlag = FDCCMDADD_E;
555}
556
557static inline int get_head_settle_flag(void)
558{
559 int tmp = HeadSettleFlag;
560 HeadSettleFlag = 0;
561 return( tmp );
562}
563
564static inline void copy_buffer(void *from, void *to)
565{
566 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
567 int cnt;
568
569 for (cnt = 512/4; cnt; cnt--)
570 *p2++ = *p1++;
571}
572
573
574
575
576/* General Interrupt Handling */
577
578static void (*FloppyIRQHandler)( int status ) = NULL;
579
David Howells7d12e782006-10-05 14:55:46 +0100580static irqreturn_t floppy_irq (int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 unsigned char status;
583 void (*handler)( int );
584
585 handler = xchg(&FloppyIRQHandler, NULL);
586
587 if (handler) {
588 nop();
589 status = FDC_READ( FDCREG_STATUS );
590 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
591 handler( status );
592 }
593 else {
594 DPRINT(("FDC irq, no handler\n"));
595 }
596 return IRQ_HANDLED;
597}
598
599
600/* Error handling: If some error happened, retry some times, then
601 * recalibrate, then try again, and fail after MAX_ERRORS.
602 */
603
604static void fd_error( void )
605{
606 if (IsFormatting) {
607 IsFormatting = 0;
608 FormatError = 1;
609 wake_up( &format_wait );
610 return;
611 }
612
Tejun Heoa336ca62009-05-08 11:54:03 +0900613 if (!fd_request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return;
615
Tejun Heoa336ca62009-05-08 11:54:03 +0900616 fd_request->errors++;
617 if (fd_request->errors >= MAX_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
Tejun Heoa336ca62009-05-08 11:54:03 +0900619 fd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Tejun Heoa336ca62009-05-08 11:54:03 +0900621 else if (fd_request->errors == RECALIBRATE_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
623 if (SelectedDrive != -1)
624 SUD.track = -1;
625 }
626 redo_fd_request();
627}
628
629
630
631#define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
632
633
634/* ---------- Formatting ---------- */
635
636#define FILL(n,val) \
637 do { \
638 memset( p, val, n ); \
639 p += n; \
640 } while(0)
641
642static int do_format(int drive, int type, struct atari_format_descr *desc)
643{
644 unsigned char *p;
645 int sect, nsect;
646 unsigned long flags;
647
648 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
649 drive, desc->track, desc->head, desc->sect_offset ));
650
651 local_irq_save(flags);
652 while( fdc_busy ) sleep_on( &fdc_wait );
653 fdc_busy = 1;
654 stdma_lock(floppy_irq, NULL);
655 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
656 local_irq_restore(flags);
657
658 if (type) {
659 if (--type >= NUM_DISK_MINORS ||
660 minor2disktype[type].drive_types > DriveType) {
661 redo_fd_request();
662 return -EINVAL;
663 }
664 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -0800665 UDT = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
669 redo_fd_request();
670 return -EINVAL;
671 }
672
673 nsect = UDT->spt;
674 p = TrackBuffer;
675 /* The track buffer is used for the raw track data, so its
676 contents become invalid! */
677 BufferDrive = -1;
678 /* stop deselect timer */
679 del_timer( &motor_off_timer );
680
681 FILL( 60 * (nsect / 9), 0x4e );
682 for( sect = 0; sect < nsect; ++sect ) {
683 FILL( 12, 0 );
684 FILL( 3, 0xf5 );
685 *p++ = 0xfe;
686 *p++ = desc->track;
687 *p++ = desc->head;
688 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
689 *p++ = 2;
690 *p++ = 0xf7;
691 FILL( 22, 0x4e );
692 FILL( 12, 0 );
693 FILL( 3, 0xf5 );
694 *p++ = 0xfb;
695 FILL( 512, 0xe5 );
696 *p++ = 0xf7;
697 FILL( 40, 0x4e );
698 }
699 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
700
701 IsFormatting = 1;
702 FormatError = 0;
703 ReqTrack = desc->track;
704 ReqSide = desc->head;
705 do_fd_action( drive );
706
707 sleep_on( &format_wait );
708
709 redo_fd_request();
710 return( FormatError ? -EIO : 0 );
711}
712
713
714/* do_fd_action() is the general procedure for a fd request: All
715 * required parameter settings (drive select, side select, track
716 * position) are checked and set if needed. For each of these
717 * parameters and the actual reading or writing exist two functions:
718 * one that starts the setting (or skips it if possible) and one
719 * callback for the "done" interrupt. Each done func calls the next
720 * set function to propagate the request down to fd_rwsec_done().
721 */
722
723static void do_fd_action( int drive )
724{
725 DPRINT(("do_fd_action\n"));
726
727 if (UseTrackbuffer && !IsFormatting) {
728 repeat:
729 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
730 if (ReqCmd == READ) {
731 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
Tejun Heoa336ca62009-05-08 11:54:03 +0900732 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* read next sector */
734 setup_req_params( drive );
735 goto repeat;
736 }
737 else {
738 /* all sectors finished */
Tejun Heoa336ca62009-05-08 11:54:03 +0900739 fd_end_request_cur(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 redo_fd_request();
741 return;
742 }
743 }
744 else {
745 /* cmd == WRITE, pay attention to track buffer
746 * consistency! */
747 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
748 }
749 }
750 }
751
752 if (SelectedDrive != drive)
753 fd_select_drive( drive );
754
755 if (UD.track == -1)
756 fd_calibrate();
757 else if (UD.track != ReqTrack << UDT->stretch)
758 fd_seek();
759 else if (IsFormatting)
760 fd_writetrack();
761 else
762 fd_rwsec();
763}
764
765
766/* Seek to track 0 if the current track is unknown */
767
768static void fd_calibrate( void )
769{
770 if (SUD.track >= 0) {
771 fd_calibrate_done( 0 );
772 return;
773 }
774
775 if (ATARIHW_PRESENT(FDCSPEED))
776 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
777 DPRINT(("fd_calibrate\n"));
778 SET_IRQ_HANDLER( fd_calibrate_done );
779 /* we can't verify, since the speed may be incorrect */
780 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
781
782 NeedSeek = 1;
783 MotorOn = 1;
784 start_timeout();
785 /* wait for IRQ */
786}
787
788
789static void fd_calibrate_done( int status )
790{
791 DPRINT(("fd_calibrate_done()\n"));
792 stop_timeout();
793
794 /* set the correct speed now */
795 if (ATARIHW_PRESENT(FDCSPEED))
796 dma_wd.fdc_speed = SUDT->fdc_speed;
797 if (status & FDCSTAT_RECNF) {
798 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
799 fd_error();
800 }
801 else {
802 SUD.track = 0;
803 fd_seek();
804 }
805}
806
807
808/* Seek the drive to the requested track. The drive must have been
809 * calibrated at some point before this.
810 */
811
812static void fd_seek( void )
813{
814 if (SUD.track == ReqTrack << SUDT->stretch) {
815 fd_seek_done( 0 );
816 return;
817 }
818
819 if (ATARIHW_PRESENT(FDCSPEED)) {
820 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
821 MFPDELAY();
822 }
823
824 DPRINT(("fd_seek() to track %d\n",ReqTrack));
825 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
826 udelay(25);
827 SET_IRQ_HANDLER( fd_seek_done );
828 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
829
830 MotorOn = 1;
831 set_head_settle_flag();
832 start_timeout();
833 /* wait for IRQ */
834}
835
836
837static void fd_seek_done( int status )
838{
839 DPRINT(("fd_seek_done()\n"));
840 stop_timeout();
841
842 /* set the correct speed */
843 if (ATARIHW_PRESENT(FDCSPEED))
844 dma_wd.fdc_speed = SUDT->fdc_speed;
845 if (status & FDCSTAT_RECNF) {
846 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
847 SelectedDrive, ReqTrack );
848 /* we don't know exactly which track we are on now! */
849 SUD.track = -1;
850 fd_error();
851 }
852 else {
853 SUD.track = ReqTrack << SUDT->stretch;
854 NeedSeek = 0;
855 if (IsFormatting)
856 fd_writetrack();
857 else
858 fd_rwsec();
859 }
860}
861
862
863/* This does the actual reading/writing after positioning the head
864 * over the correct track.
865 */
866
867static int MultReadInProgress = 0;
868
869
870static void fd_rwsec( void )
871{
872 unsigned long paddr, flags;
873 unsigned int rwflag, old_motoron;
874 unsigned int track;
875
876 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
877 if (ReqCmd == WRITE) {
878 if (ATARIHW_PRESENT(EXTD_DMA)) {
879 paddr = virt_to_phys(ReqData);
880 }
881 else {
882 copy_buffer( ReqData, DMABuffer );
883 paddr = PhysDMABuffer;
884 }
885 dma_cache_maintenance( paddr, 512, 1 );
886 rwflag = 0x100;
887 }
888 else {
889 if (read_track)
890 paddr = PhysTrackBuffer;
891 else
892 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
893 virt_to_phys(ReqData) : PhysDMABuffer;
894 rwflag = 0;
895 }
896
897 fd_select_side( ReqSide );
898
899 /* Start sector of this operation */
900 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
901 MFPDELAY();
902 /* Cheat for track if stretch != 0 */
903 if (SUDT->stretch) {
904 track = FDC_READ( FDCREG_TRACK);
905 MFPDELAY();
906 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
907 }
908 udelay(25);
909
910 /* Setup DMA */
911 local_irq_save(flags);
912 dma_wd.dma_lo = (unsigned char)paddr;
913 MFPDELAY();
914 paddr >>= 8;
915 dma_wd.dma_md = (unsigned char)paddr;
916 MFPDELAY();
917 paddr >>= 8;
918 if (ATARIHW_PRESENT(EXTD_DMA))
919 st_dma_ext_dmahi = (unsigned short)paddr;
920 else
921 dma_wd.dma_hi = (unsigned char)paddr;
922 MFPDELAY();
923 local_irq_restore(flags);
924
925 /* Clear FIFO and switch DMA to correct mode */
926 dma_wd.dma_mode_status = 0x90 | rwflag;
927 MFPDELAY();
928 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
929 MFPDELAY();
930 dma_wd.dma_mode_status = 0x90 | rwflag;
931 MFPDELAY();
932
933 /* How many sectors for DMA */
934 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
935
936 udelay(25);
937
938 /* Start operation */
939 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
940 udelay(25);
941 SET_IRQ_HANDLER( fd_rwsec_done );
942 dma_wd.fdc_acces_seccount =
943 (get_head_settle_flag() |
944 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
945
946 old_motoron = MotorOn;
947 MotorOn = 1;
948 NeedSeek = 1;
949 /* wait for interrupt */
950
951 if (read_track) {
952 /* If reading a whole track, wait about one disk rotation and
953 * then check if all sectors are read. The FDC will even
954 * search for the first non-existent sector and need 1 sec to
955 * recognise that it isn't present :-(
956 */
957 MultReadInProgress = 1;
958 mod_timer(&readtrack_timer,
959 /* 1 rot. + 5 rot.s if motor was off */
960 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
961 }
962 start_timeout();
963}
964
965
966static void fd_readtrack_check( unsigned long dummy )
967{
968 unsigned long flags, addr, addr2;
969
970 local_irq_save(flags);
971
972 if (!MultReadInProgress) {
973 /* This prevents a race condition that could arise if the
974 * interrupt is triggered while the calling of this timer
975 * callback function takes place. The IRQ function then has
976 * already cleared 'MultReadInProgress' when flow of control
977 * gets here.
978 */
979 local_irq_restore(flags);
980 return;
981 }
982
983 /* get the current DMA address */
984 /* ++ f.a. read twice to avoid being fooled by switcher */
985 addr = 0;
986 do {
987 addr2 = addr;
988 addr = dma_wd.dma_lo & 0xff;
989 MFPDELAY();
990 addr |= (dma_wd.dma_md & 0xff) << 8;
991 MFPDELAY();
992 if (ATARIHW_PRESENT( EXTD_DMA ))
993 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
994 else
995 addr |= (dma_wd.dma_hi & 0xff) << 16;
996 MFPDELAY();
997 } while(addr != addr2);
998
999 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1000 /* already read enough data, force an FDC interrupt to stop
1001 * the read operation
1002 */
1003 SET_IRQ_HANDLER( NULL );
1004 MultReadInProgress = 0;
1005 local_irq_restore(flags);
1006 DPRINT(("fd_readtrack_check(): done\n"));
1007 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1008 udelay(25);
1009
1010 /* No error until now -- the FDC would have interrupted
1011 * otherwise!
1012 */
1013 fd_rwsec_done1(0);
1014 }
1015 else {
1016 /* not yet finished, wait another tenth rotation */
1017 local_irq_restore(flags);
1018 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1019 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1020 }
1021}
1022
1023
1024static void fd_rwsec_done( int status )
1025{
1026 DPRINT(("fd_rwsec_done()\n"));
1027
1028 if (read_track) {
1029 del_timer(&readtrack_timer);
1030 if (!MultReadInProgress)
1031 return;
1032 MultReadInProgress = 0;
1033 }
1034 fd_rwsec_done1(status);
1035}
1036
1037static void fd_rwsec_done1(int status)
1038{
1039 unsigned int track;
1040
1041 stop_timeout();
1042
1043 /* Correct the track if stretch != 0 */
1044 if (SUDT->stretch) {
1045 track = FDC_READ( FDCREG_TRACK);
1046 MFPDELAY();
1047 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1048 }
1049
1050 if (!UseTrackbuffer) {
1051 dma_wd.dma_mode_status = 0x90;
1052 MFPDELAY();
1053 if (!(dma_wd.dma_mode_status & 0x01)) {
1054 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1055 goto err_end;
1056 }
1057 }
1058 MFPDELAY();
1059
1060 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1061 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1062 goto err_end;
1063 }
1064 if ((status & FDCSTAT_RECNF) &&
1065 /* RECNF is no error after a multiple read when the FDC
1066 searched for a non-existent sector! */
1067 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1068 if (Probing) {
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001069 if (SUDT > atari_disk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (SUDT[-1].blocks > ReqBlock) {
1071 /* try another disk type */
1072 SUDT--;
1073 set_capacity(unit[SelectedDrive].disk,
1074 SUDT->blocks);
1075 } else
1076 Probing = 0;
1077 }
1078 else {
1079 if (SUD.flags & FTD_MSG)
1080 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1081 SelectedDrive, SUDT->name );
1082 Probing=0;
1083 }
1084 } else {
1085/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1086 if (SUD.autoprobe) {
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001087 SUDT = atari_disk_type + StartDiskType[DriveType];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 set_capacity(unit[SelectedDrive].disk,
1089 SUDT->blocks);
1090 Probing = 1;
1091 }
1092 }
1093 if (Probing) {
1094 if (ATARIHW_PRESENT(FDCSPEED)) {
1095 dma_wd.fdc_speed = SUDT->fdc_speed;
1096 MFPDELAY();
1097 }
1098 setup_req_params( SelectedDrive );
1099 BufferDrive = -1;
1100 do_fd_action( SelectedDrive );
1101 return;
1102 }
1103
1104 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1105 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1106 goto err_end;
1107 }
1108 if (status & FDCSTAT_CRC) {
1109 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1110 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1111 goto err_end;
1112 }
1113 if (status & FDCSTAT_LOST) {
1114 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1115 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1116 goto err_end;
1117 }
1118
1119 Probing = 0;
1120
1121 if (ReqCmd == READ) {
1122 if (!read_track) {
1123 void *addr;
1124 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1125 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1126 if (!ATARIHW_PRESENT( EXTD_DMA ))
1127 copy_buffer (addr, ReqData);
1128 } else {
1129 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1130 BufferDrive = SelectedDrive;
1131 BufferSide = ReqSide;
1132 BufferTrack = ReqTrack;
1133 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1134 }
1135 }
1136
Tejun Heoa336ca62009-05-08 11:54:03 +09001137 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* read next sector */
1139 setup_req_params( SelectedDrive );
1140 do_fd_action( SelectedDrive );
1141 }
1142 else {
1143 /* all sectors finished */
Tejun Heoa336ca62009-05-08 11:54:03 +09001144 fd_end_request_cur(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 redo_fd_request();
1146 }
1147 return;
1148
1149 err_end:
1150 BufferDrive = -1;
1151 fd_error();
1152}
1153
1154
1155static void fd_writetrack( void )
1156{
1157 unsigned long paddr, flags;
1158 unsigned int track;
1159
1160 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1161
1162 paddr = PhysTrackBuffer;
1163 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1164
1165 fd_select_side( ReqSide );
1166
1167 /* Cheat for track if stretch != 0 */
1168 if (SUDT->stretch) {
1169 track = FDC_READ( FDCREG_TRACK);
1170 MFPDELAY();
1171 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1172 }
1173 udelay(40);
1174
1175 /* Setup DMA */
1176 local_irq_save(flags);
1177 dma_wd.dma_lo = (unsigned char)paddr;
1178 MFPDELAY();
1179 paddr >>= 8;
1180 dma_wd.dma_md = (unsigned char)paddr;
1181 MFPDELAY();
1182 paddr >>= 8;
1183 if (ATARIHW_PRESENT( EXTD_DMA ))
1184 st_dma_ext_dmahi = (unsigned short)paddr;
1185 else
1186 dma_wd.dma_hi = (unsigned char)paddr;
1187 MFPDELAY();
1188 local_irq_restore(flags);
1189
1190 /* Clear FIFO and switch DMA to correct mode */
1191 dma_wd.dma_mode_status = 0x190;
1192 MFPDELAY();
1193 dma_wd.dma_mode_status = 0x90;
1194 MFPDELAY();
1195 dma_wd.dma_mode_status = 0x190;
1196 MFPDELAY();
1197
1198 /* How many sectors for DMA */
1199 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1200 udelay(40);
1201
1202 /* Start operation */
1203 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1204 udelay(40);
1205 SET_IRQ_HANDLER( fd_writetrack_done );
1206 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1207
1208 MotorOn = 1;
1209 start_timeout();
1210 /* wait for interrupt */
1211}
1212
1213
1214static void fd_writetrack_done( int status )
1215{
1216 DPRINT(("fd_writetrack_done()\n"));
1217
1218 stop_timeout();
1219
1220 if (status & FDCSTAT_WPROT) {
1221 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1222 goto err_end;
1223 }
1224 if (status & FDCSTAT_LOST) {
1225 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1226 SelectedDrive, ReqSide, ReqTrack );
1227 goto err_end;
1228 }
1229
1230 wake_up( &format_wait );
1231 return;
1232
1233 err_end:
1234 fd_error();
1235}
1236
1237static void fd_times_out( unsigned long dummy )
1238{
1239 atari_disable_irq( IRQ_MFP_FDC );
1240 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1241 * before we came here... */
1242
1243 SET_IRQ_HANDLER( NULL );
1244 /* If the timeout occurred while the readtrack_check timer was
1245 * active, we need to cancel it, else bad things will happen */
1246 if (UseTrackbuffer)
1247 del_timer( &readtrack_timer );
1248 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1249 udelay( 25 );
1250
1251 printk(KERN_ERR "floppy timeout\n" );
1252 fd_error();
1253 end:
1254 atari_enable_irq( IRQ_MFP_FDC );
1255}
1256
1257
1258/* The (noop) seek operation here is needed to make the WP bit in the
1259 * FDC status register accessible for check_change. If the last disk
1260 * operation would have been a RDSEC, this bit would always read as 0
1261 * no matter what :-( To save time, the seek goes to the track we're
1262 * already on.
1263 */
1264
1265static void finish_fdc( void )
1266{
1267 if (!NeedSeek) {
1268 finish_fdc_done( 0 );
1269 }
1270 else {
1271 DPRINT(("finish_fdc: dummy seek started\n"));
1272 FDC_WRITE (FDCREG_DATA, SUD.track);
1273 SET_IRQ_HANDLER( finish_fdc_done );
1274 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1275 MotorOn = 1;
1276 start_timeout();
1277 /* we must wait for the IRQ here, because the ST-DMA
1278 is released immediately afterwards and the interrupt
1279 may be delivered to the wrong driver. */
1280 }
1281}
1282
1283
1284static void finish_fdc_done( int dummy )
1285{
1286 unsigned long flags;
1287
1288 DPRINT(("finish_fdc_done entered\n"));
1289 stop_timeout();
1290 NeedSeek = 0;
1291
1292 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1293 /* If the check for a disk change is done too early after this
1294 * last seek command, the WP bit still reads wrong :-((
1295 */
1296 mod_timer(&fd_timer, jiffies + 5);
1297 else
1298 start_check_change_timer();
1299 start_motor_off_timer();
1300
1301 local_irq_save(flags);
1302 stdma_release();
1303 fdc_busy = 0;
1304 wake_up( &fdc_wait );
1305 local_irq_restore(flags);
1306
1307 DPRINT(("finish_fdc() finished\n"));
1308}
1309
1310/* The detection of disk changes is a dark chapter in Atari history :-(
1311 * Because the "Drive ready" signal isn't present in the Atari
1312 * hardware, one has to rely on the "Write Protect". This works fine,
1313 * as long as no write protected disks are used. TOS solves this
1314 * problem by introducing tri-state logic ("maybe changed") and
1315 * looking at the serial number in block 0. This isn't possible for
1316 * Linux, since the floppy driver can't make assumptions about the
1317 * filesystem used on the disk and thus the contents of block 0. I've
1318 * chosen the method to always say "The disk was changed" if it is
1319 * unsure whether it was. This implies that every open or mount
1320 * invalidates the disk buffers if you work with write protected
1321 * disks. But at least this is better than working with incorrect data
1322 * due to unrecognised disk changes.
1323 */
1324
1325static int check_floppy_change(struct gendisk *disk)
1326{
1327 struct atari_floppy_struct *p = disk->private_data;
1328 unsigned int drive = p - unit;
1329 if (test_bit (drive, &fake_change)) {
1330 /* simulated change (e.g. after formatting) */
1331 return 1;
1332 }
1333 if (test_bit (drive, &changed_floppies)) {
1334 /* surely changed (the WP signal changed at least once) */
1335 return 1;
1336 }
1337 if (UD.wpstat) {
1338 /* WP is on -> could be changed: to be sure, buffers should be
1339 * invalidated...
1340 */
1341 return 1;
1342 }
1343
1344 return 0;
1345}
1346
1347static int floppy_revalidate(struct gendisk *disk)
1348{
1349 struct atari_floppy_struct *p = disk->private_data;
1350 unsigned int drive = p - unit;
1351
1352 if (test_bit(drive, &changed_floppies) ||
1353 test_bit(drive, &fake_change) ||
1354 p->disktype == 0) {
1355 if (UD.flags & FTD_MSG)
1356 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1357 BufferDrive = -1;
1358 clear_bit(drive, &fake_change);
1359 clear_bit(drive, &changed_floppies);
1360 /* MSch: clearing geometry makes sense only for autoprobe
1361 formats, for 'permanent user-defined' parameter:
1362 restore default_params[] here if flagged valid! */
1363 if (default_params[drive].blocks == 0)
Al Viro2c7af512006-01-12 01:06:29 -08001364 UDT = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 else
1366 UDT = &default_params[drive];
1367 }
1368 return 0;
1369}
1370
1371
1372/* This sets up the global variables describing the current request. */
1373
1374static void setup_req_params( int drive )
1375{
1376 int block = ReqBlock + ReqCnt;
1377
1378 ReqTrack = block / UDT->spt;
1379 ReqSector = block - ReqTrack * UDT->spt + 1;
1380 ReqSide = ReqTrack & 1;
1381 ReqTrack >>= 1;
1382 ReqData = ReqBuffer + 512 * ReqCnt;
1383
1384 if (UseTrackbuffer)
Tejun Heoa336ca62009-05-08 11:54:03 +09001385 read_track = (ReqCmd == READ && fd_request->errors == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 else
1387 read_track = 0;
1388
1389 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1390 ReqTrack, ReqSector, (unsigned long)ReqData ));
1391}
1392
1393
1394static void redo_fd_request(void)
1395{
1396 int drive, type;
1397 struct atari_floppy_struct *floppy;
1398
Tejun Heoa336ca62009-05-08 11:54:03 +09001399 DPRINT(("redo_fd_request: fd_request=%p dev=%s fd_request->sector=%ld\n",
1400 fd_request, fd_request ? fd_request->rq_disk->disk_name : "",
1401 fd_request ? blk_rq_pos(fd_request) : 0 ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 IsFormatting = 0;
1404
1405repeat:
Tejun Heoa336ca62009-05-08 11:54:03 +09001406 if (!fd_request) {
Tejun Heo9934c8c2009-05-08 11:54:16 +09001407 fd_request = blk_fetch_request(floppy_queue);
Tejun Heoa336ca62009-05-08 11:54:03 +09001408 if (!fd_request)
1409 goto the_end;
Tejun Heoa336ca62009-05-08 11:54:03 +09001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Tejun Heoa336ca62009-05-08 11:54:03 +09001412 floppy = fd_request->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 drive = floppy - unit;
1414 type = floppy->type;
1415
1416 if (!UD.connected) {
1417 /* drive not connected */
1418 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
Tejun Heoa336ca62009-05-08 11:54:03 +09001419 fd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto repeat;
1421 }
1422
1423 if (type == 0) {
1424 if (!UDT) {
1425 Probing = 1;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001426 UDT = atari_disk_type + StartDiskType[DriveType];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 set_capacity(floppy->disk, UDT->blocks);
1428 UD.autoprobe = 1;
1429 }
1430 }
1431 else {
1432 /* user supplied disk type */
1433 if (--type >= NUM_DISK_MINORS) {
1434 printk(KERN_WARNING "fd%d: invalid disk format", drive );
Tejun Heoa336ca62009-05-08 11:54:03 +09001435 fd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto repeat;
1437 }
1438 if (minor2disktype[type].drive_types > DriveType) {
1439 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
Tejun Heoa336ca62009-05-08 11:54:03 +09001440 fd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto repeat;
1442 }
1443 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001444 UDT = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 set_capacity(floppy->disk, UDT->blocks);
1446 UD.autoprobe = 0;
1447 }
1448
Tejun Heoa336ca62009-05-08 11:54:03 +09001449 if (blk_rq_pos(fd_request) + 1 > UDT->blocks) {
1450 fd_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto repeat;
1452 }
1453
1454 /* stop deselect timer */
1455 del_timer( &motor_off_timer );
1456
1457 ReqCnt = 0;
Tejun Heoa336ca62009-05-08 11:54:03 +09001458 ReqCmd = rq_data_dir(fd_request);
1459 ReqBlock = blk_rq_pos(fd_request);
1460 ReqBuffer = fd_request->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 setup_req_params( drive );
1462 do_fd_action( drive );
1463
1464 return;
1465
1466 the_end:
1467 finish_fdc();
1468}
1469
1470
Jens Axboe165125e2007-07-24 09:28:11 +02001471void do_fd_request(struct request_queue * q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 unsigned long flags;
1474
1475 DPRINT(("do_fd_request for pid %d\n",current->pid));
1476 while( fdc_busy ) sleep_on( &fdc_wait );
1477 fdc_busy = 1;
1478 stdma_lock(floppy_irq, NULL);
1479
1480 atari_disable_irq( IRQ_MFP_FDC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 redo_fd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 atari_enable_irq( IRQ_MFP_FDC );
1483}
1484
Al Viro60ad2342008-03-02 09:23:54 -05001485static int fd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 unsigned int cmd, unsigned long param)
1487{
Al Viro60ad2342008-03-02 09:23:54 -05001488 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct atari_floppy_struct *floppy = disk->private_data;
1490 int drive = floppy - unit;
1491 int type = floppy->type;
1492 struct atari_format_descr fmt_desc;
1493 struct atari_disk_type *dtp;
1494 struct floppy_struct getprm;
1495 int settype;
1496 struct floppy_struct setprm;
Al Viro2c7af512006-01-12 01:06:29 -08001497 void __user *argp = (void __user *)param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 switch (cmd) {
1500 case FDGETPRM:
1501 if (type) {
1502 if (--type >= NUM_DISK_MINORS)
1503 return -ENODEV;
1504 if (minor2disktype[type].drive_types > DriveType)
1505 return -ENODEV;
1506 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001507 dtp = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (UD.flags & FTD_MSG)
1509 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1510 drive, dtp, dtp->name);
1511 }
1512 else {
1513 if (!UDT)
1514 return -ENXIO;
1515 else
1516 dtp = UDT;
1517 }
1518 memset((void *)&getprm, 0, sizeof(getprm));
1519 getprm.size = dtp->blocks;
1520 getprm.sect = dtp->spt;
1521 getprm.head = 2;
1522 getprm.track = dtp->blocks/dtp->spt/2;
1523 getprm.stretch = dtp->stretch;
Al Viro2c7af512006-01-12 01:06:29 -08001524 if (copy_to_user(argp, &getprm, sizeof(getprm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -EFAULT;
1526 return 0;
1527 }
1528 switch (cmd) {
1529 case FDSETPRM:
1530 case FDDEFPRM:
1531 /*
1532 * MSch 7/96: simple 'set geometry' case: just set the
1533 * 'default' device params (minor == 0).
1534 * Currently, the drive geometry is cleared after each
1535 * disk change and subsequent revalidate()! simple
1536 * implementation of FDDEFPRM: save geometry from a
1537 * FDDEFPRM call and restore it in floppy_revalidate() !
1538 */
1539
1540 /* get the parameters from user space */
1541 if (floppy->ref != 1 && floppy->ref != -1)
1542 return -EBUSY;
Al Viro2c7af512006-01-12 01:06:29 -08001543 if (copy_from_user(&setprm, argp, sizeof(setprm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -EFAULT;
1545 /*
1546 * first of all: check for floppy change and revalidate,
1547 * or the next access will revalidate - and clear UDT :-(
1548 */
1549
1550 if (check_floppy_change(disk))
1551 floppy_revalidate(disk);
1552
1553 if (UD.flags & FTD_MSG)
1554 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1555 drive, setprm.size, setprm.sect, setprm.stretch);
1556
1557 /* what if type > 0 here? Overwrite specified entry ? */
1558 if (type) {
1559 /* refuse to re-set a predefined type for now */
1560 redo_fd_request();
1561 return -EINVAL;
1562 }
1563
1564 /*
1565 * type == 0: first look for a matching entry in the type list,
1566 * and set the UD.disktype field to use the perdefined entry.
1567 * TODO: add user-defined format to head of autoprobe list ?
1568 * Useful to include the user-type for future autodetection!
1569 */
1570
1571 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1572 int setidx = 0;
1573 if (minor2disktype[settype].drive_types > DriveType) {
1574 /* skip this one, invalid for drive ... */
1575 continue;
1576 }
1577 setidx = minor2disktype[settype].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001578 dtp = &atari_disk_type[setidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* found matching entry ?? */
1581 if ( dtp->blocks == setprm.size
1582 && dtp->spt == setprm.sect
1583 && dtp->stretch == setprm.stretch ) {
1584 if (UD.flags & FTD_MSG)
1585 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1586 drive, dtp->name, dtp);
1587 UDT = dtp;
1588 set_capacity(floppy->disk, UDT->blocks);
1589
1590 if (cmd == FDDEFPRM) {
1591 /* save settings as permanent default type */
1592 default_params[drive].name = dtp->name;
1593 default_params[drive].spt = dtp->spt;
1594 default_params[drive].blocks = dtp->blocks;
1595 default_params[drive].fdc_speed = dtp->fdc_speed;
1596 default_params[drive].stretch = dtp->stretch;
1597 }
1598
1599 return 0;
1600 }
1601
1602 }
1603
1604 /* no matching disk type found above - setting user_params */
1605
1606 if (cmd == FDDEFPRM) {
1607 /* set permanent type */
1608 dtp = &default_params[drive];
1609 } else
1610 /* set user type (reset by disk change!) */
1611 dtp = &user_params[drive];
1612
1613 dtp->name = "user format";
1614 dtp->blocks = setprm.size;
1615 dtp->spt = setprm.sect;
1616 if (setprm.sect > 14)
1617 dtp->fdc_speed = 3;
1618 else
1619 dtp->fdc_speed = 0;
1620 dtp->stretch = setprm.stretch;
1621
1622 if (UD.flags & FTD_MSG)
1623 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1624 drive, dtp->blocks, dtp->spt, dtp->stretch);
1625
1626 /* sanity check */
Julia Lawall8f474282009-07-13 22:43:41 +02001627 if (setprm.track != dtp->blocks/dtp->spt/2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 setprm.head != 2) {
1629 redo_fd_request();
1630 return -EINVAL;
1631 }
1632
1633 UDT = dtp;
1634 set_capacity(floppy->disk, UDT->blocks);
1635
1636 return 0;
1637 case FDMSGON:
1638 UD.flags |= FTD_MSG;
1639 return 0;
1640 case FDMSGOFF:
1641 UD.flags &= ~FTD_MSG;
1642 return 0;
1643 case FDSETEMSGTRESH:
1644 return -EINVAL;
1645 case FDFMTBEG:
1646 return 0;
1647 case FDFMTTRK:
1648 if (floppy->ref != 1 && floppy->ref != -1)
1649 return -EBUSY;
Al Viro2c7af512006-01-12 01:06:29 -08001650 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return -EFAULT;
1652 return do_format(drive, type, &fmt_desc);
1653 case FDCLRPRM:
1654 UDT = NULL;
1655 /* MSch: invalidate default_params */
1656 default_params[drive].blocks = 0;
1657 set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
1658 case FDFMTEND:
1659 case FDFLUSH:
1660 /* invalidate the buffer track to force a reread */
1661 BufferDrive = -1;
1662 set_bit(drive, &fake_change);
Al Viro60ad2342008-03-02 09:23:54 -05001663 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665 default:
1666 return -EINVAL;
1667 }
1668}
1669
1670
1671/* Initialize the 'unit' variable for drive 'drive' */
1672
1673static void __init fd_probe( int drive )
1674{
1675 UD.connected = 0;
1676 UDT = NULL;
1677
1678 if (!fd_test_drive_present( drive ))
1679 return;
1680
1681 UD.connected = 1;
1682 UD.track = 0;
1683 switch( UserSteprate[drive] ) {
1684 case 2:
1685 UD.steprate = FDCSTEP_2;
1686 break;
1687 case 3:
1688 UD.steprate = FDCSTEP_3;
1689 break;
1690 case 6:
1691 UD.steprate = FDCSTEP_6;
1692 break;
1693 case 12:
1694 UD.steprate = FDCSTEP_12;
1695 break;
1696 default: /* should be -1 for "not set by user" */
1697 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1698 UD.steprate = FDCSTEP_3;
1699 else
1700 UD.steprate = FDCSTEP_6;
1701 break;
1702 }
1703 MotorOn = 1; /* from probe restore operation! */
1704}
1705
1706
1707/* This function tests the physical presence of a floppy drive (not
1708 * whether a disk is inserted). This is done by issuing a restore
1709 * command, waiting max. 2 seconds (that should be enough to move the
1710 * head across the whole disk) and looking at the state of the "TR00"
1711 * signal. This should now be raised if there is a drive connected
1712 * (and there is no hardware failure :-) Otherwise, the drive is
1713 * declared absent.
1714 */
1715
1716static int __init fd_test_drive_present( int drive )
1717{
1718 unsigned long timeout;
1719 unsigned char status;
1720 int ok;
1721
1722 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1723 fd_select_drive( drive );
1724
1725 /* disable interrupt temporarily */
1726 atari_turnoff_irq( IRQ_MFP_FDC );
1727 FDC_WRITE (FDCREG_TRACK, 0xff00);
1728 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1729
1730 timeout = jiffies + 2*HZ+HZ/2;
1731 while (time_before(jiffies, timeout))
Geert Uytterhoeven3d92e8f2009-02-22 09:38:47 +01001732 if (!(st_mfp.par_dt_reg & 0x20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 break;
1734
1735 status = FDC_READ( FDCREG_STATUS );
1736 ok = (status & FDCSTAT_TR00) != 0;
1737
1738 /* force interrupt to abort restore operation (FDC would try
1739 * about 50 seconds!) */
1740 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1741 udelay(500);
1742 status = FDC_READ( FDCREG_STATUS );
1743 udelay(20);
1744
1745 if (ok) {
1746 /* dummy seek command to make WP bit accessible */
1747 FDC_WRITE( FDCREG_DATA, 0 );
1748 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
Geert Uytterhoeven3d92e8f2009-02-22 09:38:47 +01001749 while( st_mfp.par_dt_reg & 0x20 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 ;
1751 status = FDC_READ( FDCREG_STATUS );
1752 }
1753
1754 atari_turnon_irq( IRQ_MFP_FDC );
1755 return( ok );
1756}
1757
1758
1759/* Look how many and which kind of drives are connected. If there are
1760 * floppies, additionally start the disk-change and motor-off timers.
1761 */
1762
1763static void __init config_types( void )
1764{
1765 int drive, cnt = 0;
1766
1767 /* for probing drives, set the FDC speed to 8 MHz */
1768 if (ATARIHW_PRESENT(FDCSPEED))
1769 dma_wd.fdc_speed = 0;
1770
1771 printk(KERN_INFO "Probing floppy drive(s):\n");
1772 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1773 fd_probe( drive );
1774 if (UD.connected) {
1775 printk(KERN_INFO "fd%d\n", drive);
1776 ++cnt;
1777 }
1778 }
1779
1780 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1781 /* If FDC is still busy from probing, give it another FORCI
1782 * command to abort the operation. If this isn't done, the FDC
1783 * will interrupt later and its IRQ line stays low, because
1784 * the status register isn't read. And this will block any
1785 * interrupts on this IRQ line :-(
1786 */
1787 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1788 udelay(500);
1789 FDC_READ( FDCREG_STATUS );
1790 udelay(20);
1791 }
1792
1793 if (cnt > 0) {
1794 start_motor_off_timer();
1795 if (cnt == 1) fd_select_drive( 0 );
1796 start_check_change_timer();
1797 }
1798}
1799
1800/*
1801 * floppy_open check for aliasing (/dev/fd0 can be the same as
1802 * /dev/PS0 etc), and disallows simultaneous access to the same
1803 * drive with different device numbers.
1804 */
1805
Al Viro60ad2342008-03-02 09:23:54 -05001806static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Al Viro60ad2342008-03-02 09:23:54 -05001808 struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1809 int type = MINOR(bdev->bd_dev) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 DPRINT(("fd_open: type=%d\n",type));
1812 if (p->ref && p->type != type)
1813 return -EBUSY;
1814
Al Viro60ad2342008-03-02 09:23:54 -05001815 if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return -EBUSY;
1817
Al Viro60ad2342008-03-02 09:23:54 -05001818 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 p->ref = -1;
1820 else
1821 p->ref++;
1822
1823 p->type = type;
1824
Al Viro60ad2342008-03-02 09:23:54 -05001825 if (mode & FMODE_NDELAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 0;
1827
Al Viro60ad2342008-03-02 09:23:54 -05001828 if (mode & (FMODE_READ|FMODE_WRITE)) {
1829 check_disk_change(bdev);
1830 if (mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (p->wpstat) {
1832 if (p->ref < 0)
1833 p->ref = 0;
1834 else
1835 p->ref--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return -EROFS;
1837 }
1838 }
1839 }
1840 return 0;
1841}
1842
1843
Al Viro60ad2342008-03-02 09:23:54 -05001844static int floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Al Viro60ad2342008-03-02 09:23:54 -05001846 struct atari_floppy_struct *p = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (p->ref < 0)
1848 p->ref = 0;
1849 else if (!p->ref--) {
1850 printk(KERN_ERR "floppy_release with fd_ref == 0");
1851 p->ref = 0;
1852 }
1853 return 0;
1854}
1855
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001856static const struct block_device_operations floppy_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 .owner = THIS_MODULE,
Al Viro60ad2342008-03-02 09:23:54 -05001858 .open = floppy_open,
1859 .release = floppy_release,
1860 .locked_ioctl = fd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .media_changed = check_floppy_change,
1862 .revalidate_disk= floppy_revalidate,
1863};
1864
1865static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1866{
1867 int drive = *part & 3;
1868 int type = *part >> 2;
1869 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1870 return NULL;
1871 *part = 0;
1872 return get_disk(unit[drive].disk);
1873}
1874
1875static int __init atari_floppy_init (void)
1876{
1877 int i;
1878
1879 if (!MACH_IS_ATARI)
1880 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
Geert Uytterhoevene945b562008-07-17 21:16:10 +02001881 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1884 return -EBUSY;
1885
1886 for (i = 0; i < FD_MAX_UNITS; i++) {
1887 unit[i].disk = alloc_disk(1);
1888 if (!unit[i].disk)
1889 goto Enomem;
1890 }
1891
1892 if (UseTrackbuffer < 0)
1893 /* not set by user -> use default: for now, we turn
1894 track buffering off for all Medusas, though it
1895 could be used with ones that have a counter
1896 card. But the test is too hard :-( */
1897 UseTrackbuffer = !MACH_IS_MEDUSA;
1898
1899 /* initialize variables */
1900 SelectedDrive = -1;
1901 BufferDrive = -1;
1902
1903 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
1904 if (!DMABuffer) {
1905 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
1906 goto Enomem;
1907 }
1908 TrackBuffer = DMABuffer + 512;
1909 PhysDMABuffer = virt_to_phys(DMABuffer);
1910 PhysTrackBuffer = virt_to_phys(TrackBuffer);
1911 BufferDrive = BufferSide = BufferTrack = -1;
1912
1913 floppy_queue = blk_init_queue(do_fd_request, &ataflop_lock);
1914 if (!floppy_queue)
1915 goto Enomem;
1916
1917 for (i = 0; i < FD_MAX_UNITS; i++) {
1918 unit[i].track = -1;
1919 unit[i].flags = 0;
1920 unit[i].disk->major = FLOPPY_MAJOR;
1921 unit[i].disk->first_minor = i;
1922 sprintf(unit[i].disk->disk_name, "fd%d", i);
1923 unit[i].disk->fops = &floppy_fops;
1924 unit[i].disk->private_data = &unit[i];
1925 unit[i].disk->queue = floppy_queue;
1926 set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
1927 add_disk(unit[i].disk);
1928 }
1929
1930 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1931 floppy_find, NULL, NULL);
1932
1933 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
1934 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
1935 UseTrackbuffer ? "" : "no ");
1936 config_types();
1937
1938 return 0;
1939Enomem:
1940 while (i--)
1941 put_disk(unit[i].disk);
1942 if (floppy_queue)
1943 blk_cleanup_queue(floppy_queue);
1944 unregister_blkdev(FLOPPY_MAJOR, "fd");
1945 return -ENOMEM;
1946}
1947
Al Viro63907432006-01-12 01:06:36 -08001948#ifndef MODULE
1949static int __init atari_floppy_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Al Viro63907432006-01-12 01:06:36 -08001951 int ints[3 + FD_MAX_UNITS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int i;
Al Viro63907432006-01-12 01:06:36 -08001953
1954 if (!MACH_IS_ATARI)
1955 return 0;
1956
1957 str = get_options(str, 3 + FD_MAX_UNITS, ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 if (ints[0] < 1) {
1960 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
Al Viro63907432006-01-12 01:06:36 -08001961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963 else if (ints[0] > 2+FD_MAX_UNITS) {
1964 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
1965 }
1966
1967 if (ints[1] < 0 || ints[1] > 2)
1968 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
1969 else
1970 DriveType = ints[1];
1971
1972 if (ints[0] >= 2)
1973 UseTrackbuffer = (ints[2] > 0);
1974
1975 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
1976 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
1977 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
1978 else
1979 UserSteprate[i-3] = ints[i];
1980 }
Al Viro63907432006-01-12 01:06:36 -08001981 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Al Viro63907432006-01-12 01:06:36 -08001984__setup("floppy=", atari_floppy_setup);
1985#endif
1986
1987static void __exit atari_floppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int i;
1990 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
1991 for (i = 0; i < FD_MAX_UNITS; i++) {
1992 del_gendisk(unit[i].disk);
1993 put_disk(unit[i].disk);
1994 }
1995 unregister_blkdev(FLOPPY_MAJOR, "fd");
1996
1997 blk_cleanup_queue(floppy_queue);
1998 del_timer_sync(&fd_timer);
1999 atari_stram_free( DMABuffer );
2000}
2001
2002module_init(atari_floppy_init)
2003module_exit(atari_floppy_exit)
2004
2005MODULE_LICENSE("GPL");