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