blob: bf769e6e32fef92b76dffd0f94455ba9096c30b3 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/block/ataflop.c
4 *
5 * Copyright (C) 1993 Greg Harp
6 * Atari Support by Bjoern Brauel, Roman Hodek
7 *
8 * Big cleanup Sep 11..14 1994 Roman Hodek:
9 * - Driver now works interrupt driven
10 * - Support for two drives; should work, but I cannot test that :-(
11 * - Reading is done in whole tracks and buffered to speed up things
12 * - Disk change detection and drive deselecting after motor-off
13 * similar to TOS
14 * - Autodetection of disk format (DD/HD); untested yet, because I
15 * don't have an HD drive :-(
16 *
17 * Fixes Nov 13 1994 Martin Schaller:
18 * - Autodetection works now
19 * - Support for 5 1/4'' disks
20 * - Removed drive type (unknown on atari)
21 * - Do seeks with 8 Mhz
22 *
23 * Changes by Andreas Schwab:
24 * - After errors in multiple read mode try again reading single sectors
25 * (Feb 1995):
26 * - Clean up error handling
27 * - Set blk_size for proper size checking
28 * - Initialize track register when testing presence of floppy
29 * - Implement some ioctl's
30 *
31 * Changes by Torsten Lang:
32 * - When probing the floppies we should add the FDCCMDADD_H flag since
33 * the FDC will otherwise wait forever when no disk is inserted...
34 *
35 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
36 * - MFPDELAY() after each FDC access -> atari
37 * - more/other disk formats
38 * - DMA to the block buffer directly if we have a 32bit DMA
39 * - for medusa, the step rate is always 3ms
40 * - on medusa, use only cache_push()
41 * Roman:
42 * - Make disk format numbering independent from minors
43 * - Let user set max. supported drive type (speeds up format
44 * detection, saves buffer space)
45 *
46 * Roman 10/15/95:
47 * - implement some more ioctls
48 * - disk formatting
49 *
50 * Andreas 95/12/12:
51 * - increase gap size at start of track for HD/ED disks
52 *
53 * Michael (MSch) 11/07/96:
54 * - implemented FDSETPRM and FDDEFPRM ioctl
55 *
56 * Andreas (97/03/19):
57 * - implemented missing BLK* ioctls
58 *
59 * Things left to do:
60 * - Formatting
61 * - Maybe a better strategy for disk change detection (does anyone
62 * know one?)
63 */
64
65#include <linux/module.h>
66
67#include <linux/fd.h>
68#include <linux/delay.h>
69#include <linux/init.h>
Omar Sandoval6ec39382018-10-15 09:18:24 -060070#include <linux/blk-mq.h>
Christoph Hellwigb81e0c22021-09-20 14:33:25 +020071#include <linux/major.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072#include <linux/mutex.h>
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +010073#include <linux/completion.h>
74#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/atariints.h>
77#include <asm/atari_stdma.h>
78#include <asm/atari_stram.h>
79
80#define FD_MAX_UNITS 2
81
82#undef DEBUG
83
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020084static DEFINE_MUTEX(ataflop_mutex);
Tejun Heoa336ca62009-05-08 11:54:03 +090085static struct request *fd_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Omar Sandoval3e6b8c3c2018-10-11 12:20:48 -070087/*
88 * WD1772 stuff
89 */
90
91/* register codes */
92
93#define FDCSELREG_STP (0x80) /* command/status register */
94#define FDCSELREG_TRA (0x82) /* track register */
95#define FDCSELREG_SEC (0x84) /* sector register */
96#define FDCSELREG_DTA (0x86) /* data register */
97
98/* register names for FDC_READ/WRITE macros */
99
100#define FDCREG_CMD 0
101#define FDCREG_STATUS 0
102#define FDCREG_TRACK 2
103#define FDCREG_SECTOR 4
104#define FDCREG_DATA 6
105
106/* command opcodes */
107
108#define FDCCMD_RESTORE (0x00) /* - */
109#define FDCCMD_SEEK (0x10) /* | */
110#define FDCCMD_STEP (0x20) /* | TYP 1 Commands */
111#define FDCCMD_STIN (0x40) /* | */
112#define FDCCMD_STOT (0x60) /* - */
113#define FDCCMD_RDSEC (0x80) /* - TYP 2 Commands */
114#define FDCCMD_WRSEC (0xa0) /* - " */
115#define FDCCMD_RDADR (0xc0) /* - */
116#define FDCCMD_RDTRA (0xe0) /* | TYP 3 Commands */
117#define FDCCMD_WRTRA (0xf0) /* - */
118#define FDCCMD_FORCI (0xd0) /* - TYP 4 Command */
119
120/* command modifier bits */
121
122#define FDCCMDADD_SR6 (0x00) /* step rate settings */
123#define FDCCMDADD_SR12 (0x01)
124#define FDCCMDADD_SR2 (0x02)
125#define FDCCMDADD_SR3 (0x03)
126#define FDCCMDADD_V (0x04) /* verify */
127#define FDCCMDADD_H (0x08) /* wait for spin-up */
128#define FDCCMDADD_U (0x10) /* update track register */
129#define FDCCMDADD_M (0x10) /* multiple sector access */
130#define FDCCMDADD_E (0x04) /* head settling flag */
131#define FDCCMDADD_P (0x02) /* precompensation off */
132#define FDCCMDADD_A0 (0x01) /* DAM flag */
133
134/* status register bits */
135
136#define FDCSTAT_MOTORON (0x80) /* motor on */
137#define FDCSTAT_WPROT (0x40) /* write protected (FDCCMD_WR*) */
138#define FDCSTAT_SPINUP (0x20) /* motor speed stable (Type I) */
139#define FDCSTAT_DELDAM (0x20) /* sector has deleted DAM (Type II+III) */
140#define FDCSTAT_RECNF (0x10) /* record not found */
141#define FDCSTAT_CRC (0x08) /* CRC error */
142#define FDCSTAT_TR00 (0x04) /* Track 00 flag (Type I) */
143#define FDCSTAT_LOST (0x04) /* Lost Data (Type II+III) */
144#define FDCSTAT_IDX (0x02) /* Index status (Type I) */
145#define FDCSTAT_DRQ (0x02) /* DRQ status (Type II+III) */
146#define FDCSTAT_BUSY (0x01) /* FDC is busy */
147
148
149/* PSG Port A Bit Nr 0 .. Side Sel .. 0 -> Side 1 1 -> Side 2 */
150#define DSKSIDE (0x01)
151
152#define DSKDRVNONE (0x06)
153#define DSKDRV0 (0x02)
154#define DSKDRV1 (0x04)
155
156/* step rates */
157#define FDCSTEP_6 0x00
158#define FDCSTEP_12 0x01
159#define FDCSTEP_2 0x02
160#define FDCSTEP_3 0x03
161
162struct atari_format_descr {
163 int track; /* to be formatted */
164 int head; /* "" "" */
165 int sect_offset; /* offset of first sector */
166};
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Disk types: DD, HD, ED */
169static struct atari_disk_type {
170 const char *name;
171 unsigned spt; /* sectors per track */
172 unsigned blocks; /* total number of blocks */
173 unsigned fdc_speed; /* fdc_speed setting */
174 unsigned stretch; /* track doubling ? */
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -0800175} atari_disk_type[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
177 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
178 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
179 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
180/* formats above are probed for type DD */
181#define MAX_TYPE_DD 3
182 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
183 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
184 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
185/* formats above are probed for types DD and HD */
186#define MAX_TYPE_HD 6
187 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
188 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
189/* formats above are probed for types DD, HD and ED */
190#define MAX_TYPE_ED 8
191/* types below are never autoprobed */
192 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
193 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
194 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
195 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
196 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
197 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
198 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
199 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
200 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
201 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
202 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
203 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
204 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
205 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
206 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
207 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
208 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
209 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
210 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
211};
212
213static int StartDiskType[] = {
214 MAX_TYPE_DD,
215 MAX_TYPE_HD,
216 MAX_TYPE_ED
217};
218
219#define TYPE_DD 0
220#define TYPE_HD 1
221#define TYPE_ED 2
222
223static int DriveType = TYPE_HD;
224
225static DEFINE_SPINLOCK(ataflop_lock);
226
227/* Array for translating minors into disk formats */
228static struct {
229 int index;
230 unsigned drive_types;
231} minor2disktype[] = {
232 { 0, TYPE_DD }, /* 1: d360 */
233 { 4, TYPE_HD }, /* 2: h1200 */
234 { 1, TYPE_DD }, /* 3: D360 */
235 { 2, TYPE_DD }, /* 4: D720 */
236 { 1, TYPE_DD }, /* 5: h360 = D360 */
237 { 2, TYPE_DD }, /* 6: h720 = D720 */
238 { 5, TYPE_HD }, /* 7: H1440 */
239 { 7, TYPE_ED }, /* 8: E2880 */
240/* some PC formats :-) */
241 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
242 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
243 { 9, TYPE_HD }, /* 11: H1680 */
244 { 10, TYPE_DD }, /* 12: h410 */
245 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
246 { 11, TYPE_HD }, /* 14: h1476 */
247 { 12, TYPE_HD }, /* 15: H1722 */
248 { 13, TYPE_DD }, /* 16: h420 */
249 { 14, TYPE_DD }, /* 17: H830 */
250 { 15, TYPE_HD }, /* 18: h1494 */
251 { 16, TYPE_HD }, /* 19: H1743 */
252 { 17, TYPE_DD }, /* 20: h880 */
253 { 18, TYPE_DD }, /* 21: D1040 */
254 { 19, TYPE_DD }, /* 22: D1120 */
255 { 20, TYPE_HD }, /* 23: h1600 */
256 { 21, TYPE_HD }, /* 24: H1760 */
257 { 22, TYPE_HD }, /* 25: H1920 */
258 { 23, TYPE_ED }, /* 26: E3200 */
259 { 24, TYPE_ED }, /* 27: E3520 */
260 { 25, TYPE_ED }, /* 28: E3840 */
261 { 26, TYPE_HD }, /* 29: H1840 */
262 { 27, TYPE_DD }, /* 30: D800 */
263 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
264};
265
Tobias Klauser945f3902006-01-08 01:05:11 -0800266#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/*
269 * Maximum disk size (in kilobytes). This default is used whenever the
270 * current disk size is unknown.
271 */
272#define MAX_DISK_SIZE 3280
273
274/*
275 * MSch: User-provided type information. 'drive' points to
276 * the respective entry of this array. Set by FDSETPRM ioctls.
277 */
278static struct atari_disk_type user_params[FD_MAX_UNITS];
279
280/*
281 * User-provided permanent type information. 'drive' points to
282 * the respective entry of this array. Set by FDDEFPRM ioctls,
283 * restored upon disk change by floppy_revalidate() if valid (as seen by
284 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
285 */
286static struct atari_disk_type default_params[FD_MAX_UNITS];
287
288/* current info on each unit */
289static struct atari_floppy_struct {
290 int connected; /* !=0 : drive is connected */
291 int autoprobe; /* !=0 : do autoprobe */
292
293 struct atari_disk_type *disktype; /* current type of disk */
294
295 int track; /* current head position or -1 if
296 unknown */
297 unsigned int steprate; /* steprate setting */
298 unsigned int wpstat; /* current state of WP signal (for
299 disk change detection) */
300 int flags; /* flags */
Christoph Hellwigbf9c0532020-10-29 15:58:38 +0100301 struct gendisk *disk[NUM_DISK_MINORS];
Luis Chamberlain573effb2021-09-27 15:03:00 -0700302 bool registered[NUM_DISK_MINORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int ref;
304 int type;
Omar Sandoval6ec39382018-10-15 09:18:24 -0600305 struct blk_mq_tag_set tag_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306} unit[FD_MAX_UNITS];
307
308#define UD unit[drive]
309#define UDT unit[drive].disktype
310#define SUD unit[SelectedDrive]
311#define SUDT unit[SelectedDrive].disktype
312
313
314#define FDC_READ(reg) ({ \
315 /* unsigned long __flags; */ \
316 unsigned short __val; \
317 /* local_irq_save(__flags); */ \
318 dma_wd.dma_mode_status = 0x80 | (reg); \
319 udelay(25); \
320 __val = dma_wd.fdc_acces_seccount; \
321 MFPDELAY(); \
322 /* local_irq_restore(__flags); */ \
323 __val & 0xff; \
324})
325
326#define FDC_WRITE(reg,val) \
327 do { \
328 /* unsigned long __flags; */ \
329 /* local_irq_save(__flags); */ \
330 dma_wd.dma_mode_status = 0x80 | (reg); \
331 udelay(25); \
332 dma_wd.fdc_acces_seccount = (val); \
333 MFPDELAY(); \
334 /* local_irq_restore(__flags); */ \
335 } while(0)
336
337
338/* Buffering variables:
339 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
340 * operations. Second, a track buffer is used to cache a whole track
341 * of the disk to save read operations. These are two separate buffers
342 * because that allows write operations without clearing the track buffer.
343 */
344
345static int MaxSectors[] = {
346 11, 22, 44
347};
348static int BufferSize[] = {
349 15*512, 30*512, 60*512
350};
351
352#define BUFFER_SIZE (BufferSize[DriveType])
353
354unsigned char *DMABuffer; /* buffer for writes */
355static unsigned long PhysDMABuffer; /* physical address */
356
357static int UseTrackbuffer = -1; /* Do track buffering? */
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800358module_param(UseTrackbuffer, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360unsigned char *TrackBuffer; /* buffer for reads */
361static unsigned long PhysTrackBuffer; /* physical address */
362static int BufferDrive, BufferSide, BufferTrack;
363static int read_track; /* non-zero if we are reading whole tracks */
364
365#define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
366#define IS_BUFFERED(drive,side,track) \
367 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
368
369/*
370 * These are global variables, as that's the easiest way to give
371 * information to interrupts. They are the data used for the current
372 * request.
373 */
374static int SelectedDrive = 0;
375static int ReqCmd, ReqBlock;
376static int ReqSide, ReqTrack, ReqSector, ReqCnt;
377static int HeadSettleFlag = 0;
378static unsigned char *ReqData, *ReqBuffer;
379static int MotorOn = 0, MotorOffTrys;
380static int IsFormatting = 0, FormatError;
381
382static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800383module_param_array(UserSteprate, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100385static DECLARE_COMPLETION(format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387static unsigned long changed_floppies = 0xff, fake_change = 0;
388#define CHECK_CHANGE_DELAY HZ/2
389
390#define FD_MOTOR_OFF_DELAY (3*HZ)
391#define FD_MOTOR_OFF_MAXTRY (10*20)
392
393#define FLOPPY_TIMEOUT (6*HZ)
394#define RECALIBRATE_ERRORS 4 /* After this many errors the drive
395 * will be recalibrated. */
396#define MAX_ERRORS 8 /* After this many errors the driver
397 * will give up. */
398
399
400/*
401 * The driver is trying to determine the correct media format
402 * while Probing is set. fd_rwsec_done() clears it after a
403 * successful access.
404 */
405static int Probing = 0;
406
407/* This flag is set when a dummy seek is necessary to make the WP
408 * status bit accessible.
409 */
410static int NeedSeek = 0;
411
412
413#ifdef DEBUG
414#define DPRINT(a) printk a
415#else
416#define DPRINT(a)
417#endif
418
419/***************************** Prototypes *****************************/
420
421static void fd_select_side( int side );
422static void fd_select_drive( int drive );
423static void fd_deselect( void );
Kees Cook24ed9602017-08-28 11:28:21 -0700424static void fd_motor_off_timer(struct timer_list *unused);
425static void check_change(struct timer_list *unused);
David Howells7d12e782006-10-05 14:55:46 +0100426static irqreturn_t floppy_irq (int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void fd_error( void );
428static int do_format(int drive, int type, struct atari_format_descr *desc);
429static void do_fd_action( int drive );
430static void fd_calibrate( void );
431static void fd_calibrate_done( int status );
432static void fd_seek( void );
433static void fd_seek_done( int status );
434static void fd_rwsec( void );
Kees Cook24ed9602017-08-28 11:28:21 -0700435static void fd_readtrack_check(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void fd_rwsec_done( int status );
437static void fd_rwsec_done1(int status);
438static void fd_writetrack( void );
439static void fd_writetrack_done( int status );
Kees Cook24ed9602017-08-28 11:28:21 -0700440static void fd_times_out(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void finish_fdc( void );
442static void finish_fdc_done( int dummy );
443static void setup_req_params( int drive );
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200444static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 cmd, unsigned long param);
446static void fd_probe( int drive );
447static int fd_test_drive_present( int drive );
448static void config_types( void );
Al Viro60ad2342008-03-02 09:23:54 -0500449static int floppy_open(struct block_device *bdev, fmode_t mode);
Al Virodb2a1442013-05-05 21:52:57 -0400450static void floppy_release(struct gendisk *disk, fmode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452/************************* End of Prototypes **************************/
453
Kees Cook1d27e3e2017-10-04 16:27:04 -0700454static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer);
455static DEFINE_TIMER(readtrack_timer, fd_readtrack_check);
456static DEFINE_TIMER(timeout_timer, fd_times_out);
457static DEFINE_TIMER(fd_timer, check_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200459static void fd_end_request_cur(blk_status_t err)
Tejun Heoa336ca62009-05-08 11:54:03 +0900460{
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300461 DPRINT(("fd_end_request_cur(), bytes %d of %d\n",
462 blk_rq_cur_bytes(fd_request),
463 blk_rq_bytes(fd_request)));
464
Omar Sandoval6ec39382018-10-15 09:18:24 -0600465 if (!blk_update_request(fd_request, err,
466 blk_rq_cur_bytes(fd_request))) {
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300467 DPRINT(("calling __blk_mq_end_request()\n"));
Omar Sandoval6ec39382018-10-15 09:18:24 -0600468 __blk_mq_end_request(fd_request, err);
Tejun Heoa336ca62009-05-08 11:54:03 +0900469 fd_request = NULL;
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300470 } else {
471 /* requeue rest of request */
472 DPRINT(("calling blk_mq_requeue_request()\n"));
473 blk_mq_requeue_request(fd_request, true);
474 fd_request = NULL;
Omar Sandoval6ec39382018-10-15 09:18:24 -0600475 }
Tejun Heoa336ca62009-05-08 11:54:03 +0900476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static inline void start_motor_off_timer(void)
479{
480 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
481 MotorOffTrys = 0;
482}
483
484static inline void start_check_change_timer( void )
485{
486 mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
487}
488
489static inline void start_timeout(void)
490{
491 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
492}
493
494static inline void stop_timeout(void)
495{
496 del_timer(&timeout_timer);
497}
498
499/* Select the side to use. */
500
501static void fd_select_side( int side )
502{
503 unsigned long flags;
504
505 /* protect against various other ints mucking around with the PSG */
506 local_irq_save(flags);
507
508 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
509 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
510 sound_ym.rd_data_reg_sel & 0xfe;
511
512 local_irq_restore(flags);
513}
514
515
516/* Select a drive, update the FDC's track register and set the correct
517 * clock speed for this disk's type.
518 */
519
520static void fd_select_drive( int drive )
521{
522 unsigned long flags;
523 unsigned char tmp;
524
525 if (drive == SelectedDrive)
526 return;
527
528 /* protect against various other ints mucking around with the PSG */
529 local_irq_save(flags);
530 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
531 tmp = sound_ym.rd_data_reg_sel;
532 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
533 atari_dont_touch_floppy_select = 1;
534 local_irq_restore(flags);
535
536 /* restore track register to saved value */
537 FDC_WRITE( FDCREG_TRACK, UD.track );
538 udelay(25);
539
540 /* select 8/16 MHz */
541 if (UDT)
542 if (ATARIHW_PRESENT(FDCSPEED))
543 dma_wd.fdc_speed = UDT->fdc_speed;
544
545 SelectedDrive = drive;
546}
547
548
549/* Deselect both drives. */
550
551static void fd_deselect( void )
552{
553 unsigned long flags;
554
555 /* protect against various other ints mucking around with the PSG */
556 local_irq_save(flags);
557 atari_dont_touch_floppy_select = 0;
558 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
559 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
560 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
561 /* On Falcon, the drive B select line is used on the printer port, so
562 * leave it alone... */
563 SelectedDrive = -1;
564 local_irq_restore(flags);
565}
566
567
568/* This timer function deselects the drives when the FDC switched the
569 * motor off. The deselection cannot happen earlier because the FDC
570 * counts the index signals, which arrive only if one drive is selected.
571 */
572
Kees Cook24ed9602017-08-28 11:28:21 -0700573static void fd_motor_off_timer(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 unsigned char status;
576
577 if (SelectedDrive < 0)
578 /* no drive selected, needn't deselect anyone */
579 return;
580
581 if (stdma_islocked())
582 goto retry;
583
584 status = FDC_READ( FDCREG_STATUS );
585
586 if (!(status & 0x80)) {
587 /* motor already turned off by FDC -> deselect drives */
588 MotorOn = 0;
589 fd_deselect();
590 return;
591 }
592 /* not yet off, try again */
593
594 retry:
595 /* Test again later; if tested too often, it seems there is no disk
596 * in the drive and the FDC will leave the motor on forever (or,
597 * at least until a disk is inserted). So we'll test only twice
598 * per second from then on...
599 */
600 mod_timer(&motor_off_timer,
601 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
602}
603
604
605/* This function is repeatedly called to detect disk changes (as good
606 * as possible) and keep track of the current state of the write protection.
607 */
608
Kees Cook24ed9602017-08-28 11:28:21 -0700609static void check_change(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 static int drive = 0;
612
613 unsigned long flags;
614 unsigned char old_porta;
615 int stat;
616
617 if (++drive > 1 || !UD.connected)
618 drive = 0;
619
620 /* protect against various other ints mucking around with the PSG */
621 local_irq_save(flags);
622
623 if (!stdma_islocked()) {
624 sound_ym.rd_data_reg_sel = 14;
625 old_porta = sound_ym.rd_data_reg_sel;
626 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
627 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
628 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
629 sound_ym.wd_data = old_porta;
630
631 if (stat != UD.wpstat) {
632 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
633 UD.wpstat = stat;
634 set_bit (drive, &changed_floppies);
635 }
636 }
637 local_irq_restore(flags);
638
639 start_check_change_timer();
640}
641
642
643/* Handling of the Head Settling Flag: This flag should be set after each
644 * seek operation, because we don't use seeks with verify.
645 */
646
647static inline void set_head_settle_flag(void)
648{
649 HeadSettleFlag = FDCCMDADD_E;
650}
651
652static inline int get_head_settle_flag(void)
653{
654 int tmp = HeadSettleFlag;
655 HeadSettleFlag = 0;
656 return( tmp );
657}
658
659static inline void copy_buffer(void *from, void *to)
660{
661 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
662 int cnt;
663
664 for (cnt = 512/4; cnt; cnt--)
665 *p2++ = *p1++;
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/* General Interrupt Handling */
669
670static void (*FloppyIRQHandler)( int status ) = NULL;
671
David Howells7d12e782006-10-05 14:55:46 +0100672static irqreturn_t floppy_irq (int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 unsigned char status;
675 void (*handler)( int );
676
677 handler = xchg(&FloppyIRQHandler, NULL);
678
679 if (handler) {
680 nop();
681 status = FDC_READ( FDCREG_STATUS );
682 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
683 handler( status );
684 }
685 else {
686 DPRINT(("FDC irq, no handler\n"));
687 }
688 return IRQ_HANDLED;
689}
690
691
692/* Error handling: If some error happened, retry some times, then
693 * recalibrate, then try again, and fail after MAX_ERRORS.
694 */
695
696static void fd_error( void )
697{
698 if (IsFormatting) {
699 IsFormatting = 0;
700 FormatError = 1;
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100701 complete(&format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return;
703 }
704
Tejun Heoa336ca62009-05-08 11:54:03 +0900705 if (!fd_request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return;
707
Christoph Hellwigc8e90782017-04-20 16:03:13 +0200708 fd_request->error_count++;
709 if (fd_request->error_count >= MAX_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200711 fd_end_request_cur(BLK_STS_IOERR);
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300712 finish_fdc();
713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Christoph Hellwigc8e90782017-04-20 16:03:13 +0200715 else if (fd_request->error_count == RECALIBRATE_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
717 if (SelectedDrive != -1)
718 SUD.track = -1;
719 }
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300720 /* need to re-run request to recalibrate */
721 atari_disable_irq( IRQ_MFP_FDC );
722
723 setup_req_params( SelectedDrive );
724 do_fd_action( SelectedDrive );
725
726 atari_enable_irq( IRQ_MFP_FDC );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729
730
731#define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
732
733
734/* ---------- Formatting ---------- */
735
736#define FILL(n,val) \
737 do { \
738 memset( p, val, n ); \
739 p += n; \
740 } while(0)
741
742static int do_format(int drive, int type, struct atari_format_descr *desc)
743{
Christoph Hellwigbf9c0532020-10-29 15:58:38 +0100744 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned char *p;
746 int sect, nsect;
747 unsigned long flags;
Omar Sandoval6ec39382018-10-15 09:18:24 -0600748 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dan Carpenter1ffec382021-04-21 13:18:35 +0300750 if (type) {
Christoph Hellwigbf9c0532020-10-29 15:58:38 +0100751 type--;
Dan Carpenter1ffec382021-04-21 13:18:35 +0300752 if (type >= NUM_DISK_MINORS ||
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300753 minor2disktype[type].drive_types > DriveType) {
754 finish_fdc();
Dan Carpenter1ffec382021-04-21 13:18:35 +0300755 return -EINVAL;
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300756 }
Dan Carpenter1ffec382021-04-21 13:18:35 +0300757 }
Christoph Hellwigbf9c0532020-10-29 15:58:38 +0100758
759 q = unit[drive].disk[type]->queue;
Omar Sandoval6ec39382018-10-15 09:18:24 -0600760 blk_mq_freeze_queue(q);
761 blk_mq_quiesce_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 stdma_lock(floppy_irq, NULL);
765 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
766 local_irq_restore(flags);
767
768 if (type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -0800770 UDT = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300774 finish_fdc();
Omar Sandoval6ec39382018-10-15 09:18:24 -0600775 ret = -EINVAL;
776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
779 nsect = UDT->spt;
780 p = TrackBuffer;
781 /* The track buffer is used for the raw track data, so its
782 contents become invalid! */
783 BufferDrive = -1;
784 /* stop deselect timer */
785 del_timer( &motor_off_timer );
786
787 FILL( 60 * (nsect / 9), 0x4e );
788 for( sect = 0; sect < nsect; ++sect ) {
789 FILL( 12, 0 );
790 FILL( 3, 0xf5 );
791 *p++ = 0xfe;
792 *p++ = desc->track;
793 *p++ = desc->head;
794 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
795 *p++ = 2;
796 *p++ = 0xf7;
797 FILL( 22, 0x4e );
798 FILL( 12, 0 );
799 FILL( 3, 0xf5 );
800 *p++ = 0xfb;
801 FILL( 512, 0xe5 );
802 *p++ = 0xf7;
803 FILL( 40, 0x4e );
804 }
805 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
806
807 IsFormatting = 1;
808 FormatError = 0;
809 ReqTrack = desc->track;
810 ReqSide = desc->head;
811 do_fd_action( drive );
812
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100813 wait_for_completion(&format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300815 finish_fdc();
Omar Sandoval6ec39382018-10-15 09:18:24 -0600816 ret = FormatError ? -EIO : 0;
817out:
818 blk_mq_unquiesce_queue(q);
819 blk_mq_unfreeze_queue(q);
820 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823
824/* do_fd_action() is the general procedure for a fd request: All
825 * required parameter settings (drive select, side select, track
826 * position) are checked and set if needed. For each of these
827 * parameters and the actual reading or writing exist two functions:
828 * one that starts the setting (or skips it if possible) and one
829 * callback for the "done" interrupt. Each done func calls the next
830 * set function to propagate the request down to fd_rwsec_done().
831 */
832
833static void do_fd_action( int drive )
834{
835 DPRINT(("do_fd_action\n"));
836
837 if (UseTrackbuffer && !IsFormatting) {
838 repeat:
839 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
840 if (ReqCmd == READ) {
841 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
Tejun Heoa336ca62009-05-08 11:54:03 +0900842 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* read next sector */
844 setup_req_params( drive );
845 goto repeat;
846 }
847 else {
848 /* all sectors finished */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200849 fd_end_request_cur(BLK_STS_OK);
Michael Schmitzd28e4df2021-10-24 13:20:13 +1300850 finish_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return;
852 }
853 }
854 else {
855 /* cmd == WRITE, pay attention to track buffer
856 * consistency! */
857 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
858 }
859 }
860 }
861
862 if (SelectedDrive != drive)
863 fd_select_drive( drive );
864
865 if (UD.track == -1)
866 fd_calibrate();
867 else if (UD.track != ReqTrack << UDT->stretch)
868 fd_seek();
869 else if (IsFormatting)
870 fd_writetrack();
871 else
872 fd_rwsec();
873}
874
875
876/* Seek to track 0 if the current track is unknown */
877
878static void fd_calibrate( void )
879{
880 if (SUD.track >= 0) {
881 fd_calibrate_done( 0 );
882 return;
883 }
884
885 if (ATARIHW_PRESENT(FDCSPEED))
zhengbin9677d642019-11-28 11:09:32 +0800886 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 DPRINT(("fd_calibrate\n"));
888 SET_IRQ_HANDLER( fd_calibrate_done );
889 /* we can't verify, since the speed may be incorrect */
890 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
891
892 NeedSeek = 1;
893 MotorOn = 1;
894 start_timeout();
895 /* wait for IRQ */
896}
897
898
899static void fd_calibrate_done( int status )
900{
901 DPRINT(("fd_calibrate_done()\n"));
902 stop_timeout();
903
904 /* set the correct speed now */
905 if (ATARIHW_PRESENT(FDCSPEED))
906 dma_wd.fdc_speed = SUDT->fdc_speed;
907 if (status & FDCSTAT_RECNF) {
908 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
909 fd_error();
910 }
911 else {
912 SUD.track = 0;
913 fd_seek();
914 }
915}
916
917
918/* Seek the drive to the requested track. The drive must have been
919 * calibrated at some point before this.
920 */
921
922static void fd_seek( void )
923{
924 if (SUD.track == ReqTrack << SUDT->stretch) {
925 fd_seek_done( 0 );
926 return;
927 }
928
929 if (ATARIHW_PRESENT(FDCSPEED)) {
930 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
931 MFPDELAY();
932 }
933
934 DPRINT(("fd_seek() to track %d\n",ReqTrack));
935 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
936 udelay(25);
937 SET_IRQ_HANDLER( fd_seek_done );
938 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
939
940 MotorOn = 1;
941 set_head_settle_flag();
942 start_timeout();
943 /* wait for IRQ */
944}
945
946
947static void fd_seek_done( int status )
948{
949 DPRINT(("fd_seek_done()\n"));
950 stop_timeout();
951
952 /* set the correct speed */
953 if (ATARIHW_PRESENT(FDCSPEED))
954 dma_wd.fdc_speed = SUDT->fdc_speed;
955 if (status & FDCSTAT_RECNF) {
956 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
957 SelectedDrive, ReqTrack );
958 /* we don't know exactly which track we are on now! */
959 SUD.track = -1;
960 fd_error();
961 }
962 else {
963 SUD.track = ReqTrack << SUDT->stretch;
964 NeedSeek = 0;
965 if (IsFormatting)
966 fd_writetrack();
967 else
968 fd_rwsec();
969 }
970}
971
972
973/* This does the actual reading/writing after positioning the head
974 * over the correct track.
975 */
976
977static int MultReadInProgress = 0;
978
979
980static void fd_rwsec( void )
981{
982 unsigned long paddr, flags;
983 unsigned int rwflag, old_motoron;
984 unsigned int track;
985
986 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
987 if (ReqCmd == WRITE) {
988 if (ATARIHW_PRESENT(EXTD_DMA)) {
989 paddr = virt_to_phys(ReqData);
990 }
991 else {
992 copy_buffer( ReqData, DMABuffer );
993 paddr = PhysDMABuffer;
994 }
995 dma_cache_maintenance( paddr, 512, 1 );
996 rwflag = 0x100;
997 }
998 else {
999 if (read_track)
1000 paddr = PhysTrackBuffer;
1001 else
1002 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
1003 virt_to_phys(ReqData) : PhysDMABuffer;
1004 rwflag = 0;
1005 }
1006
1007 fd_select_side( ReqSide );
1008
1009 /* Start sector of this operation */
1010 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
1011 MFPDELAY();
1012 /* Cheat for track if stretch != 0 */
1013 if (SUDT->stretch) {
1014 track = FDC_READ( FDCREG_TRACK);
1015 MFPDELAY();
1016 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
1017 }
1018 udelay(25);
1019
1020 /* Setup DMA */
1021 local_irq_save(flags);
1022 dma_wd.dma_lo = (unsigned char)paddr;
1023 MFPDELAY();
1024 paddr >>= 8;
1025 dma_wd.dma_md = (unsigned char)paddr;
1026 MFPDELAY();
1027 paddr >>= 8;
1028 if (ATARIHW_PRESENT(EXTD_DMA))
1029 st_dma_ext_dmahi = (unsigned short)paddr;
1030 else
1031 dma_wd.dma_hi = (unsigned char)paddr;
1032 MFPDELAY();
1033 local_irq_restore(flags);
1034
1035 /* Clear FIFO and switch DMA to correct mode */
1036 dma_wd.dma_mode_status = 0x90 | rwflag;
1037 MFPDELAY();
1038 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
1039 MFPDELAY();
1040 dma_wd.dma_mode_status = 0x90 | rwflag;
1041 MFPDELAY();
1042
1043 /* How many sectors for DMA */
1044 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1045
1046 udelay(25);
1047
1048 /* Start operation */
1049 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1050 udelay(25);
1051 SET_IRQ_HANDLER( fd_rwsec_done );
1052 dma_wd.fdc_acces_seccount =
1053 (get_head_settle_flag() |
1054 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1055
1056 old_motoron = MotorOn;
1057 MotorOn = 1;
1058 NeedSeek = 1;
1059 /* wait for interrupt */
1060
1061 if (read_track) {
1062 /* If reading a whole track, wait about one disk rotation and
1063 * then check if all sectors are read. The FDC will even
1064 * search for the first non-existent sector and need 1 sec to
1065 * recognise that it isn't present :-(
1066 */
1067 MultReadInProgress = 1;
1068 mod_timer(&readtrack_timer,
1069 /* 1 rot. + 5 rot.s if motor was off */
1070 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1071 }
1072 start_timeout();
1073}
1074
1075
Kees Cook24ed9602017-08-28 11:28:21 -07001076static void fd_readtrack_check(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 unsigned long flags, addr, addr2;
1079
1080 local_irq_save(flags);
1081
1082 if (!MultReadInProgress) {
1083 /* This prevents a race condition that could arise if the
1084 * interrupt is triggered while the calling of this timer
1085 * callback function takes place. The IRQ function then has
1086 * already cleared 'MultReadInProgress' when flow of control
1087 * gets here.
1088 */
1089 local_irq_restore(flags);
1090 return;
1091 }
1092
1093 /* get the current DMA address */
1094 /* ++ f.a. read twice to avoid being fooled by switcher */
1095 addr = 0;
1096 do {
1097 addr2 = addr;
1098 addr = dma_wd.dma_lo & 0xff;
1099 MFPDELAY();
1100 addr |= (dma_wd.dma_md & 0xff) << 8;
1101 MFPDELAY();
1102 if (ATARIHW_PRESENT( EXTD_DMA ))
1103 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1104 else
1105 addr |= (dma_wd.dma_hi & 0xff) << 16;
1106 MFPDELAY();
1107 } while(addr != addr2);
1108
1109 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1110 /* already read enough data, force an FDC interrupt to stop
1111 * the read operation
1112 */
1113 SET_IRQ_HANDLER( NULL );
1114 MultReadInProgress = 0;
1115 local_irq_restore(flags);
1116 DPRINT(("fd_readtrack_check(): done\n"));
1117 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1118 udelay(25);
1119
1120 /* No error until now -- the FDC would have interrupted
1121 * otherwise!
1122 */
1123 fd_rwsec_done1(0);
1124 }
1125 else {
1126 /* not yet finished, wait another tenth rotation */
1127 local_irq_restore(flags);
1128 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1129 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1130 }
1131}
1132
1133
1134static void fd_rwsec_done( int status )
1135{
1136 DPRINT(("fd_rwsec_done()\n"));
1137
1138 if (read_track) {
1139 del_timer(&readtrack_timer);
1140 if (!MultReadInProgress)
1141 return;
1142 MultReadInProgress = 0;
1143 }
1144 fd_rwsec_done1(status);
1145}
1146
1147static void fd_rwsec_done1(int status)
1148{
1149 unsigned int track;
1150
1151 stop_timeout();
1152
1153 /* Correct the track if stretch != 0 */
1154 if (SUDT->stretch) {
1155 track = FDC_READ( FDCREG_TRACK);
1156 MFPDELAY();
1157 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1158 }
1159
1160 if (!UseTrackbuffer) {
1161 dma_wd.dma_mode_status = 0x90;
1162 MFPDELAY();
1163 if (!(dma_wd.dma_mode_status & 0x01)) {
1164 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1165 goto err_end;
1166 }
1167 }
1168 MFPDELAY();
1169
1170 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1171 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1172 goto err_end;
1173 }
1174 if ((status & FDCSTAT_RECNF) &&
1175 /* RECNF is no error after a multiple read when the FDC
1176 searched for a non-existent sector! */
1177 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1178 if (Probing) {
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001179 if (SUDT > atari_disk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (SUDT[-1].blocks > ReqBlock) {
1181 /* try another disk type */
1182 SUDT--;
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001183 set_capacity(unit[SelectedDrive].disk[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 SUDT->blocks);
1185 } else
1186 Probing = 0;
1187 }
1188 else {
1189 if (SUD.flags & FTD_MSG)
1190 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1191 SelectedDrive, SUDT->name );
1192 Probing=0;
1193 }
1194 } else {
1195/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1196 if (SUD.autoprobe) {
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001197 SUDT = atari_disk_type + StartDiskType[DriveType];
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001198 set_capacity(unit[SelectedDrive].disk[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 SUDT->blocks);
1200 Probing = 1;
1201 }
1202 }
1203 if (Probing) {
1204 if (ATARIHW_PRESENT(FDCSPEED)) {
1205 dma_wd.fdc_speed = SUDT->fdc_speed;
1206 MFPDELAY();
1207 }
1208 setup_req_params( SelectedDrive );
1209 BufferDrive = -1;
1210 do_fd_action( SelectedDrive );
1211 return;
1212 }
1213
1214 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1215 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1216 goto err_end;
1217 }
1218 if (status & FDCSTAT_CRC) {
1219 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1220 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1221 goto err_end;
1222 }
1223 if (status & FDCSTAT_LOST) {
1224 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1225 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1226 goto err_end;
1227 }
1228
1229 Probing = 0;
1230
1231 if (ReqCmd == READ) {
1232 if (!read_track) {
1233 void *addr;
1234 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1235 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1236 if (!ATARIHW_PRESENT( EXTD_DMA ))
1237 copy_buffer (addr, ReqData);
1238 } else {
1239 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1240 BufferDrive = SelectedDrive;
1241 BufferSide = ReqSide;
1242 BufferTrack = ReqTrack;
1243 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1244 }
1245 }
1246
Tejun Heoa336ca62009-05-08 11:54:03 +09001247 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /* read next sector */
1249 setup_req_params( SelectedDrive );
1250 do_fd_action( SelectedDrive );
1251 }
1252 else {
1253 /* all sectors finished */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001254 fd_end_request_cur(BLK_STS_OK);
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001255 finish_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 return;
1258
1259 err_end:
1260 BufferDrive = -1;
1261 fd_error();
1262}
1263
1264
1265static void fd_writetrack( void )
1266{
1267 unsigned long paddr, flags;
1268 unsigned int track;
1269
1270 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1271
1272 paddr = PhysTrackBuffer;
1273 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1274
1275 fd_select_side( ReqSide );
1276
1277 /* Cheat for track if stretch != 0 */
1278 if (SUDT->stretch) {
1279 track = FDC_READ( FDCREG_TRACK);
1280 MFPDELAY();
1281 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1282 }
1283 udelay(40);
1284
1285 /* Setup DMA */
1286 local_irq_save(flags);
1287 dma_wd.dma_lo = (unsigned char)paddr;
1288 MFPDELAY();
1289 paddr >>= 8;
1290 dma_wd.dma_md = (unsigned char)paddr;
1291 MFPDELAY();
1292 paddr >>= 8;
1293 if (ATARIHW_PRESENT( EXTD_DMA ))
1294 st_dma_ext_dmahi = (unsigned short)paddr;
1295 else
1296 dma_wd.dma_hi = (unsigned char)paddr;
1297 MFPDELAY();
1298 local_irq_restore(flags);
1299
1300 /* Clear FIFO and switch DMA to correct mode */
1301 dma_wd.dma_mode_status = 0x190;
1302 MFPDELAY();
1303 dma_wd.dma_mode_status = 0x90;
1304 MFPDELAY();
1305 dma_wd.dma_mode_status = 0x190;
1306 MFPDELAY();
1307
1308 /* How many sectors for DMA */
1309 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1310 udelay(40);
1311
1312 /* Start operation */
1313 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1314 udelay(40);
1315 SET_IRQ_HANDLER( fd_writetrack_done );
1316 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1317
1318 MotorOn = 1;
1319 start_timeout();
1320 /* wait for interrupt */
1321}
1322
1323
1324static void fd_writetrack_done( int status )
1325{
1326 DPRINT(("fd_writetrack_done()\n"));
1327
1328 stop_timeout();
1329
1330 if (status & FDCSTAT_WPROT) {
1331 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1332 goto err_end;
1333 }
1334 if (status & FDCSTAT_LOST) {
1335 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1336 SelectedDrive, ReqSide, ReqTrack );
1337 goto err_end;
1338 }
1339
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +01001340 complete(&format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return;
1342
1343 err_end:
1344 fd_error();
1345}
1346
Kees Cook24ed9602017-08-28 11:28:21 -07001347static void fd_times_out(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 atari_disable_irq( IRQ_MFP_FDC );
1350 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1351 * before we came here... */
1352
1353 SET_IRQ_HANDLER( NULL );
1354 /* If the timeout occurred while the readtrack_check timer was
1355 * active, we need to cancel it, else bad things will happen */
1356 if (UseTrackbuffer)
1357 del_timer( &readtrack_timer );
1358 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1359 udelay( 25 );
1360
1361 printk(KERN_ERR "floppy timeout\n" );
1362 fd_error();
1363 end:
1364 atari_enable_irq( IRQ_MFP_FDC );
1365}
1366
1367
1368/* The (noop) seek operation here is needed to make the WP bit in the
1369 * FDC status register accessible for check_change. If the last disk
1370 * operation would have been a RDSEC, this bit would always read as 0
1371 * no matter what :-( To save time, the seek goes to the track we're
1372 * already on.
1373 */
1374
1375static void finish_fdc( void )
1376{
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001377 if (!NeedSeek || !stdma_is_locked_by(floppy_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 finish_fdc_done( 0 );
1379 }
1380 else {
1381 DPRINT(("finish_fdc: dummy seek started\n"));
1382 FDC_WRITE (FDCREG_DATA, SUD.track);
1383 SET_IRQ_HANDLER( finish_fdc_done );
1384 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1385 MotorOn = 1;
1386 start_timeout();
1387 /* we must wait for the IRQ here, because the ST-DMA
1388 is released immediately afterwards and the interrupt
1389 may be delivered to the wrong driver. */
1390 }
1391}
1392
1393
1394static void finish_fdc_done( int dummy )
1395{
1396 unsigned long flags;
1397
1398 DPRINT(("finish_fdc_done entered\n"));
1399 stop_timeout();
1400 NeedSeek = 0;
1401
1402 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1403 /* If the check for a disk change is done too early after this
1404 * last seek command, the WP bit still reads wrong :-((
1405 */
1406 mod_timer(&fd_timer, jiffies + 5);
1407 else
1408 start_check_change_timer();
1409 start_motor_off_timer();
1410
1411 local_irq_save(flags);
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001412 if (stdma_is_locked_by(floppy_irq))
1413 stdma_release();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 local_irq_restore(flags);
1415
1416 DPRINT(("finish_fdc() finished\n"));
1417}
1418
1419/* The detection of disk changes is a dark chapter in Atari history :-(
1420 * Because the "Drive ready" signal isn't present in the Atari
1421 * hardware, one has to rely on the "Write Protect". This works fine,
1422 * as long as no write protected disks are used. TOS solves this
1423 * problem by introducing tri-state logic ("maybe changed") and
1424 * looking at the serial number in block 0. This isn't possible for
1425 * Linux, since the floppy driver can't make assumptions about the
1426 * filesystem used on the disk and thus the contents of block 0. I've
1427 * chosen the method to always say "The disk was changed" if it is
1428 * unsure whether it was. This implies that every open or mount
1429 * invalidates the disk buffers if you work with write protected
1430 * disks. But at least this is better than working with incorrect data
1431 * due to unrecognised disk changes.
1432 */
1433
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001434static unsigned int floppy_check_events(struct gendisk *disk,
1435 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct atari_floppy_struct *p = disk->private_data;
1438 unsigned int drive = p - unit;
1439 if (test_bit (drive, &fake_change)) {
1440 /* simulated change (e.g. after formatting) */
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001441 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443 if (test_bit (drive, &changed_floppies)) {
1444 /* surely changed (the WP signal changed at least once) */
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001445 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 if (UD.wpstat) {
1448 /* WP is on -> could be changed: to be sure, buffers should be
1449 * invalidated...
1450 */
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001451 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
1454 return 0;
1455}
1456
1457static int floppy_revalidate(struct gendisk *disk)
1458{
1459 struct atari_floppy_struct *p = disk->private_data;
1460 unsigned int drive = p - unit;
1461
1462 if (test_bit(drive, &changed_floppies) ||
Jiapeng Chongdf75db12021-10-29 17:50:29 +08001463 test_bit(drive, &fake_change) || !p->disktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (UD.flags & FTD_MSG)
1465 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1466 BufferDrive = -1;
1467 clear_bit(drive, &fake_change);
1468 clear_bit(drive, &changed_floppies);
1469 /* MSch: clearing geometry makes sense only for autoprobe
1470 formats, for 'permanent user-defined' parameter:
1471 restore default_params[] here if flagged valid! */
1472 if (default_params[drive].blocks == 0)
Al Viro2c7af512006-01-12 01:06:29 -08001473 UDT = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 else
1475 UDT = &default_params[drive];
1476 }
1477 return 0;
1478}
1479
1480
1481/* This sets up the global variables describing the current request. */
1482
1483static void setup_req_params( int drive )
1484{
1485 int block = ReqBlock + ReqCnt;
1486
1487 ReqTrack = block / UDT->spt;
1488 ReqSector = block - ReqTrack * UDT->spt + 1;
1489 ReqSide = ReqTrack & 1;
1490 ReqTrack >>= 1;
1491 ReqData = ReqBuffer + 512 * ReqCnt;
1492
1493 if (UseTrackbuffer)
Christoph Hellwigc8e90782017-04-20 16:03:13 +02001494 read_track = (ReqCmd == READ && fd_request->error_count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 else
1496 read_track = 0;
1497
1498 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1499 ReqTrack, ReqSector, (unsigned long)ReqData ));
1500}
1501
Omar Sandoval6ec39382018-10-15 09:18:24 -06001502static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
1503 const struct blk_mq_queue_data *bd)
Vivek Goyal639e2f22010-09-24 20:35:45 +02001504{
Omar Sandoval6ec39382018-10-15 09:18:24 -06001505 struct atari_floppy_struct *floppy = bd->rq->rq_disk->private_data;
1506 int drive = floppy - unit;
1507 int type = floppy->type;
Vivek Goyal639e2f22010-09-24 20:35:45 +02001508
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001509 DPRINT(("Queue request: drive %d type %d sectors %d of %d last %d\n",
1510 drive, type, blk_rq_cur_sectors(bd->rq),
1511 blk_rq_sectors(bd->rq), bd->last));
Michael Schmitz86d46fd2021-10-19 19:13:21 +13001512
Omar Sandoval6ec39382018-10-15 09:18:24 -06001513 spin_lock_irq(&ataflop_lock);
1514 if (fd_request) {
1515 spin_unlock_irq(&ataflop_lock);
1516 return BLK_STS_DEV_RESOURCE;
1517 }
1518 if (!stdma_try_lock(floppy_irq, NULL)) {
1519 spin_unlock_irq(&ataflop_lock);
1520 return BLK_STS_RESOURCE;
1521 }
1522 fd_request = bd->rq;
1523 blk_mq_start_request(fd_request);
Vivek Goyal639e2f22010-09-24 20:35:45 +02001524
Omar Sandoval6ec39382018-10-15 09:18:24 -06001525 atari_disable_irq( IRQ_MFP_FDC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 IsFormatting = 0;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (!UD.connected) {
1530 /* drive not connected */
1531 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001532 fd_end_request_cur(BLK_STS_IOERR);
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001533 stdma_release();
Omar Sandoval6ec39382018-10-15 09:18:24 -06001534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
1537 if (type == 0) {
1538 if (!UDT) {
1539 Probing = 1;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001540 UDT = atari_disk_type + StartDiskType[DriveType];
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001541 set_capacity(bd->rq->rq_disk, UDT->blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 UD.autoprobe = 1;
1543 }
1544 }
1545 else {
1546 /* user supplied disk type */
1547 if (--type >= NUM_DISK_MINORS) {
1548 printk(KERN_WARNING "fd%d: invalid disk format", drive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001549 fd_end_request_cur(BLK_STS_IOERR);
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001550 stdma_release();
Omar Sandoval6ec39382018-10-15 09:18:24 -06001551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 if (minor2disktype[type].drive_types > DriveType) {
1554 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001555 fd_end_request_cur(BLK_STS_IOERR);
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001556 stdma_release();
Omar Sandoval6ec39382018-10-15 09:18:24 -06001557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001560 UDT = &atari_disk_type[type];
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001561 set_capacity(bd->rq->rq_disk, UDT->blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 UD.autoprobe = 0;
1563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /* stop deselect timer */
1566 del_timer( &motor_off_timer );
1567
1568 ReqCnt = 0;
Tejun Heoa336ca62009-05-08 11:54:03 +09001569 ReqCmd = rq_data_dir(fd_request);
1570 ReqBlock = blk_rq_pos(fd_request);
Jens Axboeb4f42e22014-04-10 09:46:28 -06001571 ReqBuffer = bio_data(fd_request->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 setup_req_params( drive );
1573 do_fd_action( drive );
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 atari_enable_irq( IRQ_MFP_FDC );
Omar Sandoval6ec39382018-10-15 09:18:24 -06001576
1577out:
1578 spin_unlock_irq(&ataflop_lock);
1579 return BLK_STS_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001582static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 unsigned int cmd, unsigned long param)
1584{
Al Viro60ad2342008-03-02 09:23:54 -05001585 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 struct atari_floppy_struct *floppy = disk->private_data;
1587 int drive = floppy - unit;
1588 int type = floppy->type;
1589 struct atari_format_descr fmt_desc;
1590 struct atari_disk_type *dtp;
1591 struct floppy_struct getprm;
1592 int settype;
1593 struct floppy_struct setprm;
Al Viro2c7af512006-01-12 01:06:29 -08001594 void __user *argp = (void __user *)param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 switch (cmd) {
1597 case FDGETPRM:
1598 if (type) {
1599 if (--type >= NUM_DISK_MINORS)
1600 return -ENODEV;
1601 if (minor2disktype[type].drive_types > DriveType)
1602 return -ENODEV;
1603 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001604 dtp = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (UD.flags & FTD_MSG)
1606 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1607 drive, dtp, dtp->name);
1608 }
1609 else {
1610 if (!UDT)
1611 return -ENXIO;
1612 else
1613 dtp = UDT;
1614 }
1615 memset((void *)&getprm, 0, sizeof(getprm));
1616 getprm.size = dtp->blocks;
1617 getprm.sect = dtp->spt;
1618 getprm.head = 2;
1619 getprm.track = dtp->blocks/dtp->spt/2;
1620 getprm.stretch = dtp->stretch;
Al Viro2c7af512006-01-12 01:06:29 -08001621 if (copy_to_user(argp, &getprm, sizeof(getprm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return -EFAULT;
1623 return 0;
1624 }
1625 switch (cmd) {
1626 case FDSETPRM:
1627 case FDDEFPRM:
1628 /*
1629 * MSch 7/96: simple 'set geometry' case: just set the
1630 * 'default' device params (minor == 0).
1631 * Currently, the drive geometry is cleared after each
1632 * disk change and subsequent revalidate()! simple
1633 * implementation of FDDEFPRM: save geometry from a
1634 * FDDEFPRM call and restore it in floppy_revalidate() !
1635 */
1636
1637 /* get the parameters from user space */
1638 if (floppy->ref != 1 && floppy->ref != -1)
1639 return -EBUSY;
Al Viro2c7af512006-01-12 01:06:29 -08001640 if (copy_from_user(&setprm, argp, sizeof(setprm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return -EFAULT;
1642 /*
1643 * first of all: check for floppy change and revalidate,
1644 * or the next access will revalidate - and clear UDT :-(
1645 */
1646
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001647 if (floppy_check_events(disk, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 floppy_revalidate(disk);
1649
1650 if (UD.flags & FTD_MSG)
1651 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1652 drive, setprm.size, setprm.sect, setprm.stretch);
1653
1654 /* what if type > 0 here? Overwrite specified entry ? */
1655 if (type) {
1656 /* refuse to re-set a predefined type for now */
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001657 finish_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return -EINVAL;
1659 }
1660
1661 /*
1662 * type == 0: first look for a matching entry in the type list,
1663 * and set the UD.disktype field to use the perdefined entry.
1664 * TODO: add user-defined format to head of autoprobe list ?
1665 * Useful to include the user-type for future autodetection!
1666 */
1667
1668 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1669 int setidx = 0;
1670 if (minor2disktype[settype].drive_types > DriveType) {
1671 /* skip this one, invalid for drive ... */
1672 continue;
1673 }
1674 setidx = minor2disktype[settype].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001675 dtp = &atari_disk_type[setidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* found matching entry ?? */
1678 if ( dtp->blocks == setprm.size
1679 && dtp->spt == setprm.sect
1680 && dtp->stretch == setprm.stretch ) {
1681 if (UD.flags & FTD_MSG)
1682 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1683 drive, dtp->name, dtp);
1684 UDT = dtp;
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001685 set_capacity(disk, UDT->blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 if (cmd == FDDEFPRM) {
1688 /* save settings as permanent default type */
1689 default_params[drive].name = dtp->name;
1690 default_params[drive].spt = dtp->spt;
1691 default_params[drive].blocks = dtp->blocks;
1692 default_params[drive].fdc_speed = dtp->fdc_speed;
1693 default_params[drive].stretch = dtp->stretch;
1694 }
1695
1696 return 0;
1697 }
1698
1699 }
1700
1701 /* no matching disk type found above - setting user_params */
1702
1703 if (cmd == FDDEFPRM) {
1704 /* set permanent type */
1705 dtp = &default_params[drive];
1706 } else
1707 /* set user type (reset by disk change!) */
1708 dtp = &user_params[drive];
1709
1710 dtp->name = "user format";
1711 dtp->blocks = setprm.size;
1712 dtp->spt = setprm.sect;
1713 if (setprm.sect > 14)
1714 dtp->fdc_speed = 3;
1715 else
1716 dtp->fdc_speed = 0;
1717 dtp->stretch = setprm.stretch;
1718
1719 if (UD.flags & FTD_MSG)
1720 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1721 drive, dtp->blocks, dtp->spt, dtp->stretch);
1722
1723 /* sanity check */
Julia Lawall8f474282009-07-13 22:43:41 +02001724 if (setprm.track != dtp->blocks/dtp->spt/2 ||
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001725 setprm.head != 2) {
1726 finish_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return -EINVAL;
Michael Schmitzd28e4df2021-10-24 13:20:13 +13001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 UDT = dtp;
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001731 set_capacity(disk, UDT->blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 return 0;
1734 case FDMSGON:
1735 UD.flags |= FTD_MSG;
1736 return 0;
1737 case FDMSGOFF:
1738 UD.flags &= ~FTD_MSG;
1739 return 0;
1740 case FDSETEMSGTRESH:
1741 return -EINVAL;
1742 case FDFMTBEG:
1743 return 0;
1744 case FDFMTTRK:
1745 if (floppy->ref != 1 && floppy->ref != -1)
1746 return -EBUSY;
Al Viro2c7af512006-01-12 01:06:29 -08001747 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return -EFAULT;
1749 return do_format(drive, type, &fmt_desc);
1750 case FDCLRPRM:
1751 UDT = NULL;
1752 /* MSch: invalidate default_params */
1753 default_params[drive].blocks = 0;
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001754 set_capacity(disk, MAX_DISK_SIZE * 2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001755 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 case FDFMTEND:
1757 case FDFLUSH:
1758 /* invalidate the buffer track to force a reread */
1759 BufferDrive = -1;
1760 set_bit(drive, &fake_change);
Christoph Hellwig22f97162020-09-08 16:53:31 +02001761 if (bdev_check_media_change(bdev))
1762 floppy_revalidate(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 0;
1764 default:
1765 return -EINVAL;
1766 }
1767}
1768
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001769static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1770 unsigned int cmd, unsigned long arg)
1771{
1772 int ret;
1773
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001774 mutex_lock(&ataflop_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001775 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001776 mutex_unlock(&ataflop_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001777
1778 return ret;
1779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781/* Initialize the 'unit' variable for drive 'drive' */
1782
1783static void __init fd_probe( int drive )
1784{
1785 UD.connected = 0;
1786 UDT = NULL;
1787
1788 if (!fd_test_drive_present( drive ))
1789 return;
1790
1791 UD.connected = 1;
1792 UD.track = 0;
1793 switch( UserSteprate[drive] ) {
1794 case 2:
1795 UD.steprate = FDCSTEP_2;
1796 break;
1797 case 3:
1798 UD.steprate = FDCSTEP_3;
1799 break;
1800 case 6:
1801 UD.steprate = FDCSTEP_6;
1802 break;
1803 case 12:
1804 UD.steprate = FDCSTEP_12;
1805 break;
1806 default: /* should be -1 for "not set by user" */
1807 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1808 UD.steprate = FDCSTEP_3;
1809 else
1810 UD.steprate = FDCSTEP_6;
1811 break;
1812 }
1813 MotorOn = 1; /* from probe restore operation! */
1814}
1815
1816
1817/* This function tests the physical presence of a floppy drive (not
1818 * whether a disk is inserted). This is done by issuing a restore
1819 * command, waiting max. 2 seconds (that should be enough to move the
1820 * head across the whole disk) and looking at the state of the "TR00"
1821 * signal. This should now be raised if there is a drive connected
1822 * (and there is no hardware failure :-) Otherwise, the drive is
1823 * declared absent.
1824 */
1825
1826static int __init fd_test_drive_present( int drive )
1827{
1828 unsigned long timeout;
1829 unsigned char status;
1830 int ok;
1831
1832 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1833 fd_select_drive( drive );
1834
1835 /* disable interrupt temporarily */
1836 atari_turnoff_irq( IRQ_MFP_FDC );
1837 FDC_WRITE (FDCREG_TRACK, 0xff00);
1838 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1839
1840 timeout = jiffies + 2*HZ+HZ/2;
1841 while (time_before(jiffies, timeout))
Geert Uytterhoeven3d92e8f2009-02-22 09:38:47 +01001842 if (!(st_mfp.par_dt_reg & 0x20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844
1845 status = FDC_READ( FDCREG_STATUS );
1846 ok = (status & FDCSTAT_TR00) != 0;
1847
1848 /* force interrupt to abort restore operation (FDC would try
1849 * about 50 seconds!) */
1850 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1851 udelay(500);
1852 status = FDC_READ( FDCREG_STATUS );
1853 udelay(20);
1854
1855 if (ok) {
1856 /* dummy seek command to make WP bit accessible */
1857 FDC_WRITE( FDCREG_DATA, 0 );
1858 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
Geert Uytterhoeven3d92e8f2009-02-22 09:38:47 +01001859 while( st_mfp.par_dt_reg & 0x20 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 ;
1861 status = FDC_READ( FDCREG_STATUS );
1862 }
1863
1864 atari_turnon_irq( IRQ_MFP_FDC );
1865 return( ok );
1866}
1867
1868
1869/* Look how many and which kind of drives are connected. If there are
1870 * floppies, additionally start the disk-change and motor-off timers.
1871 */
1872
1873static void __init config_types( void )
1874{
1875 int drive, cnt = 0;
1876
1877 /* for probing drives, set the FDC speed to 8 MHz */
1878 if (ATARIHW_PRESENT(FDCSPEED))
1879 dma_wd.fdc_speed = 0;
1880
1881 printk(KERN_INFO "Probing floppy drive(s):\n");
1882 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1883 fd_probe( drive );
1884 if (UD.connected) {
1885 printk(KERN_INFO "fd%d\n", drive);
1886 ++cnt;
1887 }
1888 }
1889
1890 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1891 /* If FDC is still busy from probing, give it another FORCI
1892 * command to abort the operation. If this isn't done, the FDC
1893 * will interrupt later and its IRQ line stays low, because
1894 * the status register isn't read. And this will block any
1895 * interrupts on this IRQ line :-(
1896 */
1897 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1898 udelay(500);
1899 FDC_READ( FDCREG_STATUS );
1900 udelay(20);
1901 }
1902
1903 if (cnt > 0) {
1904 start_motor_off_timer();
1905 if (cnt == 1) fd_select_drive( 0 );
1906 start_check_change_timer();
1907 }
1908}
1909
1910/*
1911 * floppy_open check for aliasing (/dev/fd0 can be the same as
1912 * /dev/PS0 etc), and disallows simultaneous access to the same
1913 * drive with different device numbers.
1914 */
1915
Al Viro60ad2342008-03-02 09:23:54 -05001916static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Al Viro60ad2342008-03-02 09:23:54 -05001918 struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1919 int type = MINOR(bdev->bd_dev) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 DPRINT(("fd_open: type=%d\n",type));
1922 if (p->ref && p->type != type)
1923 return -EBUSY;
1924
Al Viro60ad2342008-03-02 09:23:54 -05001925 if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return -EBUSY;
1927
Al Viro60ad2342008-03-02 09:23:54 -05001928 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 p->ref = -1;
1930 else
1931 p->ref++;
1932
1933 p->type = type;
1934
Al Viro60ad2342008-03-02 09:23:54 -05001935 if (mode & FMODE_NDELAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937
Al Viro60ad2342008-03-02 09:23:54 -05001938 if (mode & (FMODE_READ|FMODE_WRITE)) {
Christoph Hellwig22f97162020-09-08 16:53:31 +02001939 if (bdev_check_media_change(bdev))
1940 floppy_revalidate(bdev->bd_disk);
Al Viro60ad2342008-03-02 09:23:54 -05001941 if (mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (p->wpstat) {
1943 if (p->ref < 0)
1944 p->ref = 0;
1945 else
1946 p->ref--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return -EROFS;
1948 }
1949 }
1950 }
1951 return 0;
1952}
1953
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001954static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1955{
1956 int ret;
1957
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001958 mutex_lock(&ataflop_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001959 ret = floppy_open(bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001960 mutex_unlock(&ataflop_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001961
1962 return ret;
1963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Al Virodb2a1442013-05-05 21:52:57 -04001965static void floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Al Viro60ad2342008-03-02 09:23:54 -05001967 struct atari_floppy_struct *p = disk->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001968 mutex_lock(&ataflop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (p->ref < 0)
1970 p->ref = 0;
1971 else if (!p->ref--) {
1972 printk(KERN_ERR "floppy_release with fd_ref == 0");
1973 p->ref = 0;
1974 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001975 mutex_unlock(&ataflop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001978static const struct block_device_operations floppy_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001980 .open = floppy_unlocked_open,
Al Viro60ad2342008-03-02 09:23:54 -05001981 .release = floppy_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001982 .ioctl = fd_ioctl,
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001983 .check_events = floppy_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984};
1985
Omar Sandoval6ec39382018-10-15 09:18:24 -06001986static const struct blk_mq_ops ataflop_mq_ops = {
1987 .queue_rq = ataflop_queue_rq,
1988};
1989
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001990static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001992 struct gendisk *disk;
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001993
Christoph Hellwigfd71c8a2021-06-02 09:53:44 +03001994 disk = blk_mq_alloc_disk(&unit[drive].tag_set, NULL);
1995 if (IS_ERR(disk))
1996 return PTR_ERR(disk);
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01001997
1998 disk->major = FLOPPY_MAJOR;
1999 disk->first_minor = drive + (type << 2);
Christoph Hellwigfd71c8a2021-06-02 09:53:44 +03002000 disk->minors = 1;
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01002001 sprintf(disk->disk_name, "fd%d", drive);
2002 disk->fops = &floppy_fops;
2003 disk->events = DISK_EVENT_MEDIA_CHANGE;
2004 disk->private_data = &unit[drive];
2005 set_capacity(disk, MAX_DISK_SIZE * 2);
2006
2007 unit[drive].disk[type] = disk;
2008 return 0;
2009}
2010
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01002011static void ataflop_probe(dev_t dev)
2012{
2013 int drive = MINOR(dev) & 3;
2014 int type = MINOR(dev) >> 2;
2015
Dan Carpenterb777f4c2021-04-21 13:19:45 +03002016 if (type)
2017 type--;
2018
2019 if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01002020 return;
Luis Chamberlain46a7db42021-11-03 16:04:35 -07002021 if (unit[drive].disk[type])
Geert Uytterhoeven38987a82021-11-06 19:55:49 +01002022 return;
Luis Chamberlain46a7db42021-11-03 16:04:35 -07002023 if (ataflop_alloc_disk(drive, type))
2024 return;
2025 if (add_disk(unit[drive].disk[type]))
2026 goto cleanup_disk;
2027 unit[drive].registered[type] = true;
2028 return;
2029
2030cleanup_disk:
2031 blk_cleanup_disk(unit[drive].disk[type]);
2032 unit[drive].disk[type] = NULL;
Tetsuo Handa4ddb85d2021-11-03 16:04:33 -07002033}
2034
2035static void atari_floppy_cleanup(void)
2036{
2037 int i;
2038 int type;
2039
2040 for (i = 0; i < FD_MAX_UNITS; i++) {
2041 for (type = 0; type < NUM_DISK_MINORS; type++) {
2042 if (!unit[i].disk[type])
2043 continue;
2044 del_gendisk(unit[i].disk[type]);
2045 blk_cleanup_queue(unit[i].disk[type]->queue);
2046 put_disk(unit[i].disk[type]);
2047 }
2048 blk_mq_free_tag_set(&unit[i].tag_set);
2049 }
2050
2051 del_timer_sync(&fd_timer);
2052 atari_stram_free(DMABuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Luis Chamberlaindeae1132021-09-27 15:03:01 -07002055static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs)
2056{
2057 int type;
2058
2059 for (type = 0; type < NUM_DISK_MINORS; type++) {
2060 if (!fs->disk[type])
2061 continue;
2062 if (fs->registered[type])
2063 del_gendisk(fs->disk[type]);
2064 blk_cleanup_disk(fs->disk[type]);
2065 }
2066 blk_mq_free_tag_set(&fs->tag_set);
2067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069static int __init atari_floppy_init (void)
2070{
2071 int i;
Omar Sandoval6ec39382018-10-15 09:18:24 -06002072 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 if (!MACH_IS_ATARI)
2075 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
Geert Uytterhoevene945b562008-07-17 21:16:10 +02002076 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 for (i = 0; i < FD_MAX_UNITS; i++) {
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01002079 memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
2080 unit[i].tag_set.ops = &ataflop_mq_ops;
2081 unit[i].tag_set.nr_hw_queues = 1;
2082 unit[i].tag_set.nr_maps = 1;
2083 unit[i].tag_set.queue_depth = 2;
2084 unit[i].tag_set.numa_node = NUMA_NO_NODE;
2085 unit[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2086 ret = blk_mq_alloc_tag_set(&unit[i].tag_set);
2087 if (ret)
Omar Sandoval6ec39382018-10-15 09:18:24 -06002088 goto err;
Omar Sandoval71327f52018-10-11 12:20:49 -07002089
Christoph Hellwigbf9c0532020-10-29 15:58:38 +01002090 ret = ataflop_alloc_disk(i, 0);
2091 if (ret) {
2092 blk_mq_free_tag_set(&unit[i].tag_set);
Omar Sandoval6ec39382018-10-15 09:18:24 -06002093 goto err;
2094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
2097 if (UseTrackbuffer < 0)
2098 /* not set by user -> use default: for now, we turn
2099 track buffering off for all Medusas, though it
2100 could be used with ones that have a counter
2101 card. But the test is too hard :-( */
2102 UseTrackbuffer = !MACH_IS_MEDUSA;
2103
2104 /* initialize variables */
2105 SelectedDrive = -1;
2106 BufferDrive = -1;
2107
2108 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2109 if (!DMABuffer) {
2110 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
Omar Sandoval6ec39382018-10-15 09:18:24 -06002111 ret = -ENOMEM;
2112 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 TrackBuffer = DMABuffer + 512;
Michael Schmitza4de73f2014-03-31 21:06:07 +13002115 PhysDMABuffer = atari_stram_to_phys(DMABuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 PhysTrackBuffer = virt_to_phys(TrackBuffer);
2117 BufferDrive = BufferSide = BufferTrack = -1;
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 for (i = 0; i < FD_MAX_UNITS; i++) {
2120 unit[i].track = -1;
2121 unit[i].flags = 0;
Luis Chamberlain2f151072021-09-27 15:03:02 -07002122 ret = add_disk(unit[i].disk[0]);
2123 if (ret)
2124 goto err_out_dma;
Luis Chamberlain573effb2021-09-27 15:03:00 -07002125 unit[i].registered[0] = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2129 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2130 UseTrackbuffer ? "" : "no ");
2131 config_types();
2132
Tetsuo Handa4ddb85d2021-11-03 16:04:33 -07002133 ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2134 if (ret) {
2135 printk(KERN_ERR "atari_floppy_init: cannot register block device\n");
2136 atari_floppy_cleanup();
2137 }
2138 return ret;
Omar Sandoval6ec39382018-10-15 09:18:24 -06002139
Luis Chamberlain2f151072021-09-27 15:03:02 -07002140err_out_dma:
2141 atari_stram_free(DMABuffer);
Omar Sandoval6ec39382018-10-15 09:18:24 -06002142err:
Luis Chamberlaindeae1132021-09-27 15:03:01 -07002143 while (--i >= 0)
2144 atari_cleanup_floppy_disk(&unit[i]);
Vivek Goyal639e2f22010-09-24 20:35:45 +02002145
Omar Sandoval6ec39382018-10-15 09:18:24 -06002146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Al Viro63907432006-01-12 01:06:36 -08002149#ifndef MODULE
2150static int __init atari_floppy_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Al Viro63907432006-01-12 01:06:36 -08002152 int ints[3 + FD_MAX_UNITS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int i;
Al Viro63907432006-01-12 01:06:36 -08002154
2155 if (!MACH_IS_ATARI)
2156 return 0;
2157
2158 str = get_options(str, 3 + FD_MAX_UNITS, ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 if (ints[0] < 1) {
2161 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
Al Viro63907432006-01-12 01:06:36 -08002162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164 else if (ints[0] > 2+FD_MAX_UNITS) {
2165 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2166 }
2167
2168 if (ints[1] < 0 || ints[1] > 2)
2169 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2170 else
2171 DriveType = ints[1];
2172
2173 if (ints[0] >= 2)
2174 UseTrackbuffer = (ints[2] > 0);
2175
2176 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2177 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2178 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2179 else
2180 UserSteprate[i-3] = ints[i];
2181 }
Al Viro63907432006-01-12 01:06:36 -08002182 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Al Viro63907432006-01-12 01:06:36 -08002185__setup("floppy=", atari_floppy_setup);
2186#endif
2187
2188static void __exit atari_floppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 unregister_blkdev(FLOPPY_MAJOR, "fd");
Tetsuo Handa4ddb85d2021-11-03 16:04:33 -07002191 atari_floppy_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
2194module_init(atari_floppy_init)
2195module_exit(atari_floppy_exit)
2196
2197MODULE_LICENSE("GPL");