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