ataflop.c 51 KB

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