ataflop.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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. } 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 inode *inode, struct file *filp, 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 inode *inode, struct file *filp );
  325. static int floppy_release( struct inode * inode, struct file * filp );
  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. end_request(CURRENT, 0);
  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 = &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. CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
  612. CURRENT->sector += CURRENT->current_nr_sectors;
  613. end_request(CURRENT, 1);
  614. redo_fd_request();
  615. return;
  616. }
  617. }
  618. else {
  619. /* cmd == WRITE, pay attention to track buffer
  620. * consistency! */
  621. copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
  622. }
  623. }
  624. }
  625. if (SelectedDrive != drive)
  626. fd_select_drive( drive );
  627. if (UD.track == -1)
  628. fd_calibrate();
  629. else if (UD.track != ReqTrack << UDT->stretch)
  630. fd_seek();
  631. else if (IsFormatting)
  632. fd_writetrack();
  633. else
  634. fd_rwsec();
  635. }
  636. /* Seek to track 0 if the current track is unknown */
  637. static void fd_calibrate( void )
  638. {
  639. if (SUD.track >= 0) {
  640. fd_calibrate_done( 0 );
  641. return;
  642. }
  643. if (ATARIHW_PRESENT(FDCSPEED))
  644. dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
  645. DPRINT(("fd_calibrate\n"));
  646. SET_IRQ_HANDLER( fd_calibrate_done );
  647. /* we can't verify, since the speed may be incorrect */
  648. FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
  649. NeedSeek = 1;
  650. MotorOn = 1;
  651. start_timeout();
  652. /* wait for IRQ */
  653. }
  654. static void fd_calibrate_done( int status )
  655. {
  656. DPRINT(("fd_calibrate_done()\n"));
  657. stop_timeout();
  658. /* set the correct speed now */
  659. if (ATARIHW_PRESENT(FDCSPEED))
  660. dma_wd.fdc_speed = SUDT->fdc_speed;
  661. if (status & FDCSTAT_RECNF) {
  662. printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
  663. fd_error();
  664. }
  665. else {
  666. SUD.track = 0;
  667. fd_seek();
  668. }
  669. }
  670. /* Seek the drive to the requested track. The drive must have been
  671. * calibrated at some point before this.
  672. */
  673. static void fd_seek( void )
  674. {
  675. if (SUD.track == ReqTrack << SUDT->stretch) {
  676. fd_seek_done( 0 );
  677. return;
  678. }
  679. if (ATARIHW_PRESENT(FDCSPEED)) {
  680. dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
  681. MFPDELAY();
  682. }
  683. DPRINT(("fd_seek() to track %d\n",ReqTrack));
  684. FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
  685. udelay(25);
  686. SET_IRQ_HANDLER( fd_seek_done );
  687. FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
  688. MotorOn = 1;
  689. set_head_settle_flag();
  690. start_timeout();
  691. /* wait for IRQ */
  692. }
  693. static void fd_seek_done( int status )
  694. {
  695. DPRINT(("fd_seek_done()\n"));
  696. stop_timeout();
  697. /* set the correct speed */
  698. if (ATARIHW_PRESENT(FDCSPEED))
  699. dma_wd.fdc_speed = SUDT->fdc_speed;
  700. if (status & FDCSTAT_RECNF) {
  701. printk(KERN_ERR "fd%d: seek error (to track %d)\n",
  702. SelectedDrive, ReqTrack );
  703. /* we don't know exactly which track we are on now! */
  704. SUD.track = -1;
  705. fd_error();
  706. }
  707. else {
  708. SUD.track = ReqTrack << SUDT->stretch;
  709. NeedSeek = 0;
  710. if (IsFormatting)
  711. fd_writetrack();
  712. else
  713. fd_rwsec();
  714. }
  715. }
  716. /* This does the actual reading/writing after positioning the head
  717. * over the correct track.
  718. */
  719. static int MultReadInProgress = 0;
  720. static void fd_rwsec( void )
  721. {
  722. unsigned long paddr, flags;
  723. unsigned int rwflag, old_motoron;
  724. unsigned int track;
  725. DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
  726. if (ReqCmd == WRITE) {
  727. if (ATARIHW_PRESENT(EXTD_DMA)) {
  728. paddr = virt_to_phys(ReqData);
  729. }
  730. else {
  731. copy_buffer( ReqData, DMABuffer );
  732. paddr = PhysDMABuffer;
  733. }
  734. dma_cache_maintenance( paddr, 512, 1 );
  735. rwflag = 0x100;
  736. }
  737. else {
  738. if (read_track)
  739. paddr = PhysTrackBuffer;
  740. else
  741. paddr = ATARIHW_PRESENT(EXTD_DMA) ?
  742. virt_to_phys(ReqData) : PhysDMABuffer;
  743. rwflag = 0;
  744. }
  745. fd_select_side( ReqSide );
  746. /* Start sector of this operation */
  747. FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
  748. MFPDELAY();
  749. /* Cheat for track if stretch != 0 */
  750. if (SUDT->stretch) {
  751. track = FDC_READ( FDCREG_TRACK);
  752. MFPDELAY();
  753. FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
  754. }
  755. udelay(25);
  756. /* Setup DMA */
  757. local_irq_save(flags);
  758. dma_wd.dma_lo = (unsigned char)paddr;
  759. MFPDELAY();
  760. paddr >>= 8;
  761. dma_wd.dma_md = (unsigned char)paddr;
  762. MFPDELAY();
  763. paddr >>= 8;
  764. if (ATARIHW_PRESENT(EXTD_DMA))
  765. st_dma_ext_dmahi = (unsigned short)paddr;
  766. else
  767. dma_wd.dma_hi = (unsigned char)paddr;
  768. MFPDELAY();
  769. local_irq_restore(flags);
  770. /* Clear FIFO and switch DMA to correct mode */
  771. dma_wd.dma_mode_status = 0x90 | rwflag;
  772. MFPDELAY();
  773. dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
  774. MFPDELAY();
  775. dma_wd.dma_mode_status = 0x90 | rwflag;
  776. MFPDELAY();
  777. /* How many sectors for DMA */
  778. dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
  779. udelay(25);
  780. /* Start operation */
  781. dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
  782. udelay(25);
  783. SET_IRQ_HANDLER( fd_rwsec_done );
  784. dma_wd.fdc_acces_seccount =
  785. (get_head_settle_flag() |
  786. (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
  787. old_motoron = MotorOn;
  788. MotorOn = 1;
  789. NeedSeek = 1;
  790. /* wait for interrupt */
  791. if (read_track) {
  792. /* If reading a whole track, wait about one disk rotation and
  793. * then check if all sectors are read. The FDC will even
  794. * search for the first non-existent sector and need 1 sec to
  795. * recognise that it isn't present :-(
  796. */
  797. MultReadInProgress = 1;
  798. mod_timer(&readtrack_timer,
  799. /* 1 rot. + 5 rot.s if motor was off */
  800. jiffies + HZ/5 + (old_motoron ? 0 : HZ));
  801. }
  802. start_timeout();
  803. }
  804. static void fd_readtrack_check( unsigned long dummy )
  805. {
  806. unsigned long flags, addr, addr2;
  807. local_irq_save(flags);
  808. if (!MultReadInProgress) {
  809. /* This prevents a race condition that could arise if the
  810. * interrupt is triggered while the calling of this timer
  811. * callback function takes place. The IRQ function then has
  812. * already cleared 'MultReadInProgress' when flow of control
  813. * gets here.
  814. */
  815. local_irq_restore(flags);
  816. return;
  817. }
  818. /* get the current DMA address */
  819. /* ++ f.a. read twice to avoid being fooled by switcher */
  820. addr = 0;
  821. do {
  822. addr2 = addr;
  823. addr = dma_wd.dma_lo & 0xff;
  824. MFPDELAY();
  825. addr |= (dma_wd.dma_md & 0xff) << 8;
  826. MFPDELAY();
  827. if (ATARIHW_PRESENT( EXTD_DMA ))
  828. addr |= (st_dma_ext_dmahi & 0xffff) << 16;
  829. else
  830. addr |= (dma_wd.dma_hi & 0xff) << 16;
  831. MFPDELAY();
  832. } while(addr != addr2);
  833. if (addr >= PhysTrackBuffer + SUDT->spt*512) {
  834. /* already read enough data, force an FDC interrupt to stop
  835. * the read operation
  836. */
  837. SET_IRQ_HANDLER( NULL );
  838. MultReadInProgress = 0;
  839. local_irq_restore(flags);
  840. DPRINT(("fd_readtrack_check(): done\n"));
  841. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  842. udelay(25);
  843. /* No error until now -- the FDC would have interrupted
  844. * otherwise!
  845. */
  846. fd_rwsec_done1(0);
  847. }
  848. else {
  849. /* not yet finished, wait another tenth rotation */
  850. local_irq_restore(flags);
  851. DPRINT(("fd_readtrack_check(): not yet finished\n"));
  852. mod_timer(&readtrack_timer, jiffies + HZ/5/10);
  853. }
  854. }
  855. static void fd_rwsec_done( int status )
  856. {
  857. DPRINT(("fd_rwsec_done()\n"));
  858. if (read_track) {
  859. del_timer(&readtrack_timer);
  860. if (!MultReadInProgress)
  861. return;
  862. MultReadInProgress = 0;
  863. }
  864. fd_rwsec_done1(status);
  865. }
  866. static void fd_rwsec_done1(int status)
  867. {
  868. unsigned int track;
  869. stop_timeout();
  870. /* Correct the track if stretch != 0 */
  871. if (SUDT->stretch) {
  872. track = FDC_READ( FDCREG_TRACK);
  873. MFPDELAY();
  874. FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
  875. }
  876. if (!UseTrackbuffer) {
  877. dma_wd.dma_mode_status = 0x90;
  878. MFPDELAY();
  879. if (!(dma_wd.dma_mode_status & 0x01)) {
  880. printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
  881. goto err_end;
  882. }
  883. }
  884. MFPDELAY();
  885. if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
  886. printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
  887. goto err_end;
  888. }
  889. if ((status & FDCSTAT_RECNF) &&
  890. /* RECNF is no error after a multiple read when the FDC
  891. searched for a non-existent sector! */
  892. !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
  893. if (Probing) {
  894. if (SUDT > disk_type) {
  895. if (SUDT[-1].blocks > ReqBlock) {
  896. /* try another disk type */
  897. SUDT--;
  898. set_capacity(unit[SelectedDrive].disk,
  899. SUDT->blocks);
  900. } else
  901. Probing = 0;
  902. }
  903. else {
  904. if (SUD.flags & FTD_MSG)
  905. printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
  906. SelectedDrive, SUDT->name );
  907. Probing=0;
  908. }
  909. } else {
  910. /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
  911. if (SUD.autoprobe) {
  912. SUDT = disk_type + StartDiskType[DriveType];
  913. set_capacity(unit[SelectedDrive].disk,
  914. SUDT->blocks);
  915. Probing = 1;
  916. }
  917. }
  918. if (Probing) {
  919. if (ATARIHW_PRESENT(FDCSPEED)) {
  920. dma_wd.fdc_speed = SUDT->fdc_speed;
  921. MFPDELAY();
  922. }
  923. setup_req_params( SelectedDrive );
  924. BufferDrive = -1;
  925. do_fd_action( SelectedDrive );
  926. return;
  927. }
  928. printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
  929. SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
  930. goto err_end;
  931. }
  932. if (status & FDCSTAT_CRC) {
  933. printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
  934. SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
  935. goto err_end;
  936. }
  937. if (status & FDCSTAT_LOST) {
  938. printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
  939. SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
  940. goto err_end;
  941. }
  942. Probing = 0;
  943. if (ReqCmd == READ) {
  944. if (!read_track) {
  945. void *addr;
  946. addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
  947. dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
  948. if (!ATARIHW_PRESENT( EXTD_DMA ))
  949. copy_buffer (addr, ReqData);
  950. } else {
  951. dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
  952. BufferDrive = SelectedDrive;
  953. BufferSide = ReqSide;
  954. BufferTrack = ReqTrack;
  955. copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
  956. }
  957. }
  958. if (++ReqCnt < CURRENT->current_nr_sectors) {
  959. /* read next sector */
  960. setup_req_params( SelectedDrive );
  961. do_fd_action( SelectedDrive );
  962. }
  963. else {
  964. /* all sectors finished */
  965. CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
  966. CURRENT->sector += CURRENT->current_nr_sectors;
  967. end_request(CURRENT, 1);
  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 && CURRENT->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: CURRENT=%p dev=%s CURRENT->sector=%ld\n",
  1179. CURRENT, CURRENT ? CURRENT->rq_disk->disk_name : "",
  1180. CURRENT ? CURRENT->sector : 0 ));
  1181. IsFormatting = 0;
  1182. repeat:
  1183. if (!CURRENT)
  1184. goto the_end;
  1185. floppy = CURRENT->rq_disk->private_data;
  1186. drive = floppy - unit;
  1187. type = floppy->type;
  1188. if (!UD.connected) {
  1189. /* drive not connected */
  1190. printk(KERN_ERR "Unknown Device: fd%d\n", drive );
  1191. end_request(CURRENT, 0);
  1192. goto repeat;
  1193. }
  1194. if (type == 0) {
  1195. if (!UDT) {
  1196. Probing = 1;
  1197. UDT = disk_type + StartDiskType[DriveType];
  1198. set_capacity(floppy->disk, UDT->blocks);
  1199. UD.autoprobe = 1;
  1200. }
  1201. }
  1202. else {
  1203. /* user supplied disk type */
  1204. if (--type >= NUM_DISK_MINORS) {
  1205. printk(KERN_WARNING "fd%d: invalid disk format", drive );
  1206. end_request(CURRENT, 0);
  1207. goto repeat;
  1208. }
  1209. if (minor2disktype[type].drive_types > DriveType) {
  1210. printk(KERN_WARNING "fd%d: unsupported disk format", drive );
  1211. end_request(CURRENT, 0);
  1212. goto repeat;
  1213. }
  1214. type = minor2disktype[type].index;
  1215. UDT = &disk_type[type];
  1216. set_capacity(floppy->disk, UDT->blocks);
  1217. UD.autoprobe = 0;
  1218. }
  1219. if (CURRENT->sector + 1 > UDT->blocks) {
  1220. end_request(CURRENT, 0);
  1221. goto repeat;
  1222. }
  1223. /* stop deselect timer */
  1224. del_timer( &motor_off_timer );
  1225. ReqCnt = 0;
  1226. ReqCmd = rq_data_dir(CURRENT);
  1227. ReqBlock = CURRENT->sector;
  1228. ReqBuffer = CURRENT->buffer;
  1229. setup_req_params( drive );
  1230. do_fd_action( drive );
  1231. return;
  1232. the_end:
  1233. finish_fdc();
  1234. }
  1235. void do_fd_request(request_queue_t * q)
  1236. {
  1237. unsigned long flags;
  1238. DPRINT(("do_fd_request for pid %d\n",current->pid));
  1239. while( fdc_busy ) sleep_on( &fdc_wait );
  1240. fdc_busy = 1;
  1241. stdma_lock(floppy_irq, NULL);
  1242. atari_disable_irq( IRQ_MFP_FDC );
  1243. local_save_flags(flags); /* The request function is called with ints
  1244. local_irq_disable(); * disabled... so must save the IPL for later */
  1245. redo_fd_request();
  1246. local_irq_restore(flags);
  1247. atari_enable_irq( IRQ_MFP_FDC );
  1248. }
  1249. static int fd_ioctl(struct inode *inode, struct file *filp,
  1250. unsigned int cmd, unsigned long param)
  1251. {
  1252. struct gendisk *disk = inode->i_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 = &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 = &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 (!dtp || 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(inode->i_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 (!(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( 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 inode *inode, struct file *filp )
  1529. {
  1530. struct atari_floppy_struct *p = inode->i_bdev->bd_disk->private_data;
  1531. int type = iminor(inode) >> 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 && filp->f_flags & O_EXCL))
  1536. return -EBUSY;
  1537. if (filp->f_flags & O_EXCL)
  1538. p->ref = -1;
  1539. else
  1540. p->ref++;
  1541. p->type = type;
  1542. if (filp->f_flags & O_NDELAY)
  1543. return 0;
  1544. if (filp->f_mode & 3) {
  1545. check_disk_change(inode->i_bdev);
  1546. if (filp->f_mode & 2) {
  1547. if (p->wpstat) {
  1548. if (p->ref < 0)
  1549. p->ref = 0;
  1550. else
  1551. p->ref--;
  1552. floppy_release(inode, filp);
  1553. return -EROFS;
  1554. }
  1555. }
  1556. }
  1557. return 0;
  1558. }
  1559. static int floppy_release( struct inode * inode, struct file * filp )
  1560. {
  1561. struct atari_floppy_struct *p = inode->i_bdev->bd_disk->private_data;
  1562. if (p->ref < 0)
  1563. p->ref = 0;
  1564. else if (!p->ref--) {
  1565. printk(KERN_ERR "floppy_release with fd_ref == 0");
  1566. p->ref = 0;
  1567. }
  1568. return 0;
  1569. }
  1570. static struct block_device_operations floppy_fops = {
  1571. .owner = THIS_MODULE,
  1572. .open = floppy_open,
  1573. .release = floppy_release,
  1574. .ioctl = fd_ioctl,
  1575. .media_changed = check_floppy_change,
  1576. .revalidate_disk= floppy_revalidate,
  1577. };
  1578. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  1579. {
  1580. int drive = *part & 3;
  1581. int type = *part >> 2;
  1582. if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
  1583. return NULL;
  1584. *part = 0;
  1585. return get_disk(unit[drive].disk);
  1586. }
  1587. static int __init atari_floppy_init (void)
  1588. {
  1589. int i;
  1590. if (!MACH_IS_ATARI)
  1591. /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
  1592. return -ENXIO;
  1593. if (MACH_IS_HADES)
  1594. /* Hades doesn't have Atari-compatible floppy */
  1595. return -ENXIO;
  1596. if (register_blkdev(FLOPPY_MAJOR,"fd"))
  1597. return -EBUSY;
  1598. for (i = 0; i < FD_MAX_UNITS; i++) {
  1599. unit[i].disk = alloc_disk(1);
  1600. if (!unit[i].disk)
  1601. goto Enomem;
  1602. }
  1603. if (UseTrackbuffer < 0)
  1604. /* not set by user -> use default: for now, we turn
  1605. track buffering off for all Medusas, though it
  1606. could be used with ones that have a counter
  1607. card. But the test is too hard :-( */
  1608. UseTrackbuffer = !MACH_IS_MEDUSA;
  1609. /* initialize variables */
  1610. SelectedDrive = -1;
  1611. BufferDrive = -1;
  1612. DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
  1613. if (!DMABuffer) {
  1614. printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
  1615. goto Enomem;
  1616. }
  1617. TrackBuffer = DMABuffer + 512;
  1618. PhysDMABuffer = virt_to_phys(DMABuffer);
  1619. PhysTrackBuffer = virt_to_phys(TrackBuffer);
  1620. BufferDrive = BufferSide = BufferTrack = -1;
  1621. floppy_queue = blk_init_queue(do_fd_request, &ataflop_lock);
  1622. if (!floppy_queue)
  1623. goto Enomem;
  1624. for (i = 0; i < FD_MAX_UNITS; i++) {
  1625. unit[i].track = -1;
  1626. unit[i].flags = 0;
  1627. unit[i].disk->major = FLOPPY_MAJOR;
  1628. unit[i].disk->first_minor = i;
  1629. sprintf(unit[i].disk->disk_name, "fd%d", i);
  1630. unit[i].disk->fops = &floppy_fops;
  1631. unit[i].disk->private_data = &unit[i];
  1632. unit[i].disk->queue = floppy_queue;
  1633. set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
  1634. add_disk(unit[i].disk);
  1635. }
  1636. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  1637. floppy_find, NULL, NULL);
  1638. printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
  1639. DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
  1640. UseTrackbuffer ? "" : "no ");
  1641. config_types();
  1642. return 0;
  1643. Enomem:
  1644. while (i--)
  1645. put_disk(unit[i].disk);
  1646. if (floppy_queue)
  1647. blk_cleanup_queue(floppy_queue);
  1648. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1649. return -ENOMEM;
  1650. }
  1651. #ifndef MODULE
  1652. static int __init atari_floppy_setup(char *str)
  1653. {
  1654. int ints[3 + FD_MAX_UNITS];
  1655. int i;
  1656. if (!MACH_IS_ATARI)
  1657. return 0;
  1658. str = get_options(str, 3 + FD_MAX_UNITS, ints);
  1659. if (ints[0] < 1) {
  1660. printk(KERN_ERR "ataflop_setup: no arguments!\n" );
  1661. return 0;
  1662. }
  1663. else if (ints[0] > 2+FD_MAX_UNITS) {
  1664. printk(KERN_ERR "ataflop_setup: too many arguments\n" );
  1665. }
  1666. if (ints[1] < 0 || ints[1] > 2)
  1667. printk(KERN_ERR "ataflop_setup: bad drive type\n" );
  1668. else
  1669. DriveType = ints[1];
  1670. if (ints[0] >= 2)
  1671. UseTrackbuffer = (ints[2] > 0);
  1672. for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
  1673. if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
  1674. printk(KERN_ERR "ataflop_setup: bad steprate\n" );
  1675. else
  1676. UserSteprate[i-3] = ints[i];
  1677. }
  1678. return 1;
  1679. }
  1680. __setup("floppy=", atari_floppy_setup);
  1681. #endif
  1682. static void __exit atari_floppy_exit(void)
  1683. {
  1684. int i;
  1685. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  1686. for (i = 0; i < FD_MAX_UNITS; i++) {
  1687. del_gendisk(unit[i].disk);
  1688. put_disk(unit[i].disk);
  1689. }
  1690. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1691. blk_cleanup_queue(floppy_queue);
  1692. del_timer_sync(&fd_timer);
  1693. atari_stram_free( DMABuffer );
  1694. }
  1695. module_init(atari_floppy_init)
  1696. module_exit(atari_floppy_exit)
  1697. MODULE_LICENSE("GPL");