ataflop.c 51 KB

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