amiflop.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. /*
  2. * linux/amiga/amiflop.c
  3. *
  4. * Copyright (C) 1993 Greg Harp
  5. * Portions of this driver are based on code contributed by Brad Pepers
  6. *
  7. * revised 28.5.95 by Joerg Dorchain
  8. * - now no bugs(?) any more for both HD & DD
  9. * - added support for 40 Track 5.25" drives, 80-track hopefully behaves
  10. * like 3.5" dd (no way to test - are there any 5.25" drives out there
  11. * that work on an A4000?)
  12. * - wrote formatting routine (maybe dirty, but works)
  13. *
  14. * june/july 1995 added ms-dos support by Joerg Dorchain
  15. * (portions based on messydos.device and various contributors)
  16. * - currently only 9 and 18 sector disks
  17. *
  18. * - fixed a bug with the internal trackbuffer when using multiple
  19. * disks the same time
  20. * - made formatting a bit safer
  21. * - added command line and machine based default for "silent" df0
  22. *
  23. * december 1995 adapted for 1.2.13pl4 by Joerg Dorchain
  24. * - works but I think it's inefficient. (look in redo_fd_request)
  25. * But the changes were very efficient. (only three and a half lines)
  26. *
  27. * january 1996 added special ioctl for tracking down read/write problems
  28. * - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data
  29. * is copied to area. (area should be large enough since no checking is
  30. * done - 30K is currently sufficient). return the actual size of the
  31. * trackbuffer
  32. * - replaced udelays() by a timer (CIAA timer B) for the waits
  33. * needed for the disk mechanic.
  34. *
  35. * february 1996 fixed error recovery and multiple disk access
  36. * - both got broken the first time I tampered with the driver :-(
  37. * - still not safe, but better than before
  38. *
  39. * revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel.
  40. * - Minor changes to accept the kdev_t.
  41. * - Replaced some more udelays with ms_delays. Udelay is just a loop,
  42. * and so the delay will be different depending on the given
  43. * processor :-(
  44. * - The driver could use a major cleanup because of the new
  45. * major/minor handling that came with kdev_t. It seems to work for
  46. * the time being, but I can't guarantee that it will stay like
  47. * that when we start using 16 (24?) bit minors.
  48. *
  49. * restructured jan 1997 by Joerg Dorchain
  50. * - Fixed Bug accessing multiple disks
  51. * - some code cleanup
  52. * - added trackbuffer for each drive to speed things up
  53. * - fixed some race conditions (who finds the next may send it to me ;-)
  54. */
  55. #include <linux/module.h>
  56. #include <linux/fd.h>
  57. #include <linux/hdreg.h>
  58. #include <linux/delay.h>
  59. #include <linux/init.h>
  60. #include <linux/amifdreg.h>
  61. #include <linux/amifd.h>
  62. #include <linux/buffer_head.h>
  63. #include <linux/blkdev.h>
  64. #include <linux/elevator.h>
  65. #include <linux/interrupt.h>
  66. #include <asm/setup.h>
  67. #include <asm/uaccess.h>
  68. #include <asm/amigahw.h>
  69. #include <asm/amigaints.h>
  70. #include <asm/irq.h>
  71. #undef DEBUG /* print _LOTS_ of infos */
  72. #define RAW_IOCTL
  73. #ifdef RAW_IOCTL
  74. #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
  75. #endif
  76. /*
  77. * Defines
  78. */
  79. /*
  80. * Error codes
  81. */
  82. #define FD_OK 0 /* operation succeeded */
  83. #define FD_ERROR -1 /* general error (seek, read, write, etc) */
  84. #define FD_NOUNIT 1 /* unit does not exist */
  85. #define FD_UNITBUSY 2 /* unit already active */
  86. #define FD_NOTACTIVE 3 /* unit is not active */
  87. #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
  88. #define MFM_NOSYNC 1
  89. #define MFM_HEADER 2
  90. #define MFM_DATA 3
  91. #define MFM_TRACK 4
  92. /*
  93. * Floppy ID values
  94. */
  95. #define FD_NODRIVE 0x00000000 /* response when no unit is present */
  96. #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
  97. #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
  98. #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
  99. static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
  100. module_param(fd_def_df0, ulong, 0);
  101. MODULE_LICENSE("GPL");
  102. static struct request_queue *floppy_queue;
  103. #define QUEUE (floppy_queue)
  104. #define CURRENT elv_next_request(floppy_queue)
  105. /*
  106. * Macros
  107. */
  108. #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
  109. #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
  110. #define SELECT(mask) (ciab.prb &= ~mask)
  111. #define DESELECT(mask) (ciab.prb |= mask)
  112. #define SELMASK(drive) (1 << (3 + (drive & 3)))
  113. static struct fd_drive_type drive_types[] = {
  114. /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
  115. /* warning: times are now in milliseconds (ms) */
  116. { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
  117. { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
  118. { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
  119. { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  120. };
  121. static int num_dr_types = ARRAY_SIZE(drive_types);
  122. static int amiga_read(int), dos_read(int);
  123. static void amiga_write(int), dos_write(int);
  124. static struct fd_data_type data_types[] = {
  125. { "Amiga", 11 , amiga_read, amiga_write},
  126. { "MS-Dos", 9, dos_read, dos_write}
  127. };
  128. /* current info on each unit */
  129. static struct amiga_floppy_struct unit[FD_MAX_UNITS];
  130. static struct timer_list flush_track_timer[FD_MAX_UNITS];
  131. static struct timer_list post_write_timer;
  132. static struct timer_list motor_on_timer;
  133. static struct timer_list motor_off_timer[FD_MAX_UNITS];
  134. static int on_attempts;
  135. /* Synchronization of FDC access */
  136. /* request loop (trackbuffer) */
  137. static volatile int fdc_busy = -1;
  138. static volatile int fdc_nested;
  139. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  140. static DECLARE_WAIT_QUEUE_HEAD(motor_wait);
  141. static volatile int selected = -1; /* currently selected drive */
  142. static int writepending;
  143. static int writefromint;
  144. static char *raw_buf;
  145. static DEFINE_SPINLOCK(amiflop_lock);
  146. #define RAW_BUF_SIZE 30000 /* size of raw disk data */
  147. /*
  148. * These are global variables, as that's the easiest way to give
  149. * information to interrupts. They are the data used for the current
  150. * request.
  151. */
  152. static volatile char block_flag;
  153. static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
  154. /* MS-Dos MFM Coding tables (should go quick and easy) */
  155. static unsigned char mfmencode[16]={
  156. 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
  157. 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
  158. };
  159. static unsigned char mfmdecode[128];
  160. /* floppy internal millisecond timer stuff */
  161. static volatile int ms_busy = -1;
  162. static DECLARE_WAIT_QUEUE_HEAD(ms_wait);
  163. #define MS_TICKS ((amiga_eclock+50)/1000)
  164. /*
  165. * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
  166. * max X times - some types of errors increase the errorcount by 2 or
  167. * even 3, so we might actually retry only X/2 times before giving up.
  168. */
  169. #define MAX_ERRORS 12
  170. #define custom amiga_custom
  171. /* Prevent "aliased" accesses. */
  172. static int fd_ref[4] = { 0,0,0,0 };
  173. static int fd_device[4] = { 0, 0, 0, 0 };
  174. /*
  175. * Here come the actual hardware access and helper functions.
  176. * They are not reentrant and single threaded because all drives
  177. * share the same hardware and the same trackbuffer.
  178. */
  179. /* Milliseconds timer */
  180. static irqreturn_t ms_isr(int irq, void *dummy)
  181. {
  182. ms_busy = -1;
  183. wake_up(&ms_wait);
  184. return IRQ_HANDLED;
  185. }
  186. /* all waits are queued up
  187. A more generic routine would do a schedule a la timer.device */
  188. static void ms_delay(int ms)
  189. {
  190. unsigned long flags;
  191. int ticks;
  192. if (ms > 0) {
  193. local_irq_save(flags);
  194. while (ms_busy == 0)
  195. sleep_on(&ms_wait);
  196. ms_busy = 0;
  197. local_irq_restore(flags);
  198. ticks = MS_TICKS*ms-1;
  199. ciaa.tblo=ticks%256;
  200. ciaa.tbhi=ticks/256;
  201. ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
  202. sleep_on(&ms_wait);
  203. }
  204. }
  205. /* Hardware semaphore */
  206. /* returns true when we would get the semaphore */
  207. static inline int try_fdc(int drive)
  208. {
  209. drive &= 3;
  210. return ((fdc_busy < 0) || (fdc_busy == drive));
  211. }
  212. static void get_fdc(int drive)
  213. {
  214. unsigned long flags;
  215. drive &= 3;
  216. #ifdef DEBUG
  217. printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
  218. #endif
  219. local_irq_save(flags);
  220. while (!try_fdc(drive))
  221. sleep_on(&fdc_wait);
  222. fdc_busy = drive;
  223. fdc_nested++;
  224. local_irq_restore(flags);
  225. }
  226. static inline void rel_fdc(void)
  227. {
  228. #ifdef DEBUG
  229. if (fdc_nested == 0)
  230. printk("fd: unmatched rel_fdc\n");
  231. printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
  232. #endif
  233. fdc_nested--;
  234. if (fdc_nested == 0) {
  235. fdc_busy = -1;
  236. wake_up(&fdc_wait);
  237. }
  238. }
  239. static void fd_select (int drive)
  240. {
  241. unsigned char prb = ~0;
  242. drive&=3;
  243. #ifdef DEBUG
  244. printk("selecting %d\n",drive);
  245. #endif
  246. if (drive == selected)
  247. return;
  248. get_fdc(drive);
  249. selected = drive;
  250. if (unit[drive].track % 2 != 0)
  251. prb &= ~DSKSIDE;
  252. if (unit[drive].motor == 1)
  253. prb &= ~DSKMOTOR;
  254. ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
  255. ciab.prb = prb;
  256. prb &= ~SELMASK(drive);
  257. ciab.prb = prb;
  258. rel_fdc();
  259. }
  260. static void fd_deselect (int drive)
  261. {
  262. unsigned char prb;
  263. unsigned long flags;
  264. drive&=3;
  265. #ifdef DEBUG
  266. printk("deselecting %d\n",drive);
  267. #endif
  268. if (drive != selected) {
  269. printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
  270. return;
  271. }
  272. get_fdc(drive);
  273. local_irq_save(flags);
  274. selected = -1;
  275. prb = ciab.prb;
  276. prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
  277. ciab.prb = prb;
  278. local_irq_restore (flags);
  279. rel_fdc();
  280. }
  281. static void motor_on_callback(unsigned long nr)
  282. {
  283. if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
  284. wake_up (&motor_wait);
  285. } else {
  286. motor_on_timer.expires = jiffies + HZ/10;
  287. add_timer(&motor_on_timer);
  288. }
  289. }
  290. static int fd_motor_on(int nr)
  291. {
  292. nr &= 3;
  293. del_timer(motor_off_timer + nr);
  294. if (!unit[nr].motor) {
  295. unit[nr].motor = 1;
  296. fd_select(nr);
  297. motor_on_timer.data = nr;
  298. mod_timer(&motor_on_timer, jiffies + HZ/2);
  299. on_attempts = 10;
  300. sleep_on (&motor_wait);
  301. fd_deselect(nr);
  302. }
  303. if (on_attempts == 0) {
  304. on_attempts = -1;
  305. #if 0
  306. printk (KERN_ERR "motor_on failed, turning motor off\n");
  307. fd_motor_off (nr);
  308. return 0;
  309. #else
  310. printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
  311. #endif
  312. }
  313. return 1;
  314. }
  315. static void fd_motor_off(unsigned long drive)
  316. {
  317. long calledfromint;
  318. #ifdef MODULE
  319. long decusecount;
  320. decusecount = drive & 0x40000000;
  321. #endif
  322. calledfromint = drive & 0x80000000;
  323. drive&=3;
  324. if (calledfromint && !try_fdc(drive)) {
  325. /* We would be blocked in an interrupt, so try again later */
  326. motor_off_timer[drive].expires = jiffies + 1;
  327. add_timer(motor_off_timer + drive);
  328. return;
  329. }
  330. unit[drive].motor = 0;
  331. fd_select(drive);
  332. udelay (1);
  333. fd_deselect(drive);
  334. }
  335. static void floppy_off (unsigned int nr)
  336. {
  337. int drive;
  338. drive = nr & 3;
  339. /* called this way it is always from interrupt */
  340. motor_off_timer[drive].data = nr | 0x80000000;
  341. mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
  342. }
  343. static int fd_calibrate(int drive)
  344. {
  345. unsigned char prb;
  346. int n;
  347. drive &= 3;
  348. get_fdc(drive);
  349. if (!fd_motor_on (drive))
  350. return 0;
  351. fd_select (drive);
  352. prb = ciab.prb;
  353. prb |= DSKSIDE;
  354. prb &= ~DSKDIREC;
  355. ciab.prb = prb;
  356. for (n = unit[drive].type->tracks/2; n != 0; --n) {
  357. if (ciaa.pra & DSKTRACK0)
  358. break;
  359. prb &= ~DSKSTEP;
  360. ciab.prb = prb;
  361. prb |= DSKSTEP;
  362. udelay (2);
  363. ciab.prb = prb;
  364. ms_delay(unit[drive].type->step_delay);
  365. }
  366. ms_delay (unit[drive].type->settle_time);
  367. prb |= DSKDIREC;
  368. n = unit[drive].type->tracks + 20;
  369. for (;;) {
  370. prb &= ~DSKSTEP;
  371. ciab.prb = prb;
  372. prb |= DSKSTEP;
  373. udelay (2);
  374. ciab.prb = prb;
  375. ms_delay(unit[drive].type->step_delay + 1);
  376. if ((ciaa.pra & DSKTRACK0) == 0)
  377. break;
  378. if (--n == 0) {
  379. printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
  380. fd_motor_off (drive);
  381. unit[drive].track = -1;
  382. rel_fdc();
  383. return 0;
  384. }
  385. }
  386. unit[drive].track = 0;
  387. ms_delay(unit[drive].type->settle_time);
  388. rel_fdc();
  389. fd_deselect(drive);
  390. return 1;
  391. }
  392. static int fd_seek(int drive, int track)
  393. {
  394. unsigned char prb;
  395. int cnt;
  396. #ifdef DEBUG
  397. printk("seeking drive %d to track %d\n",drive,track);
  398. #endif
  399. drive &= 3;
  400. get_fdc(drive);
  401. if (unit[drive].track == track) {
  402. rel_fdc();
  403. return 1;
  404. }
  405. if (!fd_motor_on(drive)) {
  406. rel_fdc();
  407. return 0;
  408. }
  409. if (unit[drive].track < 0 && !fd_calibrate(drive)) {
  410. rel_fdc();
  411. return 0;
  412. }
  413. fd_select (drive);
  414. cnt = unit[drive].track/2 - track/2;
  415. prb = ciab.prb;
  416. prb |= DSKSIDE | DSKDIREC;
  417. if (track % 2 != 0)
  418. prb &= ~DSKSIDE;
  419. if (cnt < 0) {
  420. cnt = - cnt;
  421. prb &= ~DSKDIREC;
  422. }
  423. ciab.prb = prb;
  424. if (track % 2 != unit[drive].track % 2)
  425. ms_delay (unit[drive].type->side_time);
  426. unit[drive].track = track;
  427. if (cnt == 0) {
  428. rel_fdc();
  429. fd_deselect(drive);
  430. return 1;
  431. }
  432. do {
  433. prb &= ~DSKSTEP;
  434. ciab.prb = prb;
  435. prb |= DSKSTEP;
  436. udelay (1);
  437. ciab.prb = prb;
  438. ms_delay (unit[drive].type->step_delay);
  439. } while (--cnt != 0);
  440. ms_delay (unit[drive].type->settle_time);
  441. rel_fdc();
  442. fd_deselect(drive);
  443. return 1;
  444. }
  445. static unsigned long fd_get_drive_id(int drive)
  446. {
  447. int i;
  448. ulong id = 0;
  449. drive&=3;
  450. get_fdc(drive);
  451. /* set up for ID */
  452. MOTOR_ON;
  453. udelay(2);
  454. SELECT(SELMASK(drive));
  455. udelay(2);
  456. DESELECT(SELMASK(drive));
  457. udelay(2);
  458. MOTOR_OFF;
  459. udelay(2);
  460. SELECT(SELMASK(drive));
  461. udelay(2);
  462. DESELECT(SELMASK(drive));
  463. udelay(2);
  464. /* loop and read disk ID */
  465. for (i=0; i<32; i++) {
  466. SELECT(SELMASK(drive));
  467. udelay(2);
  468. /* read and store value of DSKRDY */
  469. id <<= 1;
  470. id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
  471. DESELECT(SELMASK(drive));
  472. }
  473. rel_fdc();
  474. /*
  475. * RB: At least A500/A2000's df0: don't identify themselves.
  476. * As every (real) Amiga has at least a 3.5" DD drive as df0:
  477. * we default to that if df0: doesn't identify as a certain
  478. * type.
  479. */
  480. if(drive == 0 && id == FD_NODRIVE)
  481. {
  482. id = fd_def_df0;
  483. printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
  484. }
  485. /* return the ID value */
  486. return (id);
  487. }
  488. static irqreturn_t fd_block_done(int irq, void *dummy)
  489. {
  490. if (block_flag)
  491. custom.dsklen = 0x4000;
  492. if (block_flag == 2) { /* writing */
  493. writepending = 2;
  494. post_write_timer.expires = jiffies + 1; /* at least 2 ms */
  495. post_write_timer.data = selected;
  496. add_timer(&post_write_timer);
  497. }
  498. else { /* reading */
  499. block_flag = 0;
  500. wake_up (&wait_fd_block);
  501. }
  502. return IRQ_HANDLED;
  503. }
  504. static void raw_read(int drive)
  505. {
  506. drive&=3;
  507. get_fdc(drive);
  508. while (block_flag)
  509. sleep_on(&wait_fd_block);
  510. fd_select(drive);
  511. /* setup adkcon bits correctly */
  512. custom.adkcon = ADK_MSBSYNC;
  513. custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
  514. custom.dsksync = MFM_SYNC;
  515. custom.dsklen = 0;
  516. custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
  517. custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
  518. custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
  519. block_flag = 1;
  520. while (block_flag)
  521. sleep_on (&wait_fd_block);
  522. custom.dsklen = 0;
  523. fd_deselect(drive);
  524. rel_fdc();
  525. }
  526. static int raw_write(int drive)
  527. {
  528. ushort adk;
  529. drive&=3;
  530. get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
  531. if ((ciaa.pra & DSKPROT) == 0) {
  532. rel_fdc();
  533. return 0;
  534. }
  535. while (block_flag)
  536. sleep_on(&wait_fd_block);
  537. fd_select(drive);
  538. /* clear adkcon bits */
  539. custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
  540. /* set appropriate adkcon bits */
  541. adk = ADK_SETCLR|ADK_FAST;
  542. if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
  543. adk |= ADK_PRECOMP1;
  544. else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
  545. adk |= ADK_PRECOMP0;
  546. custom.adkcon = adk;
  547. custom.dsklen = DSKLEN_WRITE;
  548. custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
  549. custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
  550. custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
  551. block_flag = 2;
  552. return 1;
  553. }
  554. /*
  555. * to be called at least 2ms after the write has finished but before any
  556. * other access to the hardware.
  557. */
  558. static void post_write (unsigned long drive)
  559. {
  560. #ifdef DEBUG
  561. printk("post_write for drive %ld\n",drive);
  562. #endif
  563. drive &= 3;
  564. custom.dsklen = 0;
  565. block_flag = 0;
  566. writepending = 0;
  567. writefromint = 0;
  568. unit[drive].dirty = 0;
  569. wake_up(&wait_fd_block);
  570. fd_deselect(drive);
  571. rel_fdc(); /* corresponds to get_fdc() in raw_write */
  572. }
  573. /*
  574. * The following functions are to convert the block contents into raw data
  575. * written to disk and vice versa.
  576. * (Add other formats here ;-))
  577. */
  578. static unsigned long scan_sync(unsigned long raw, unsigned long end)
  579. {
  580. ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
  581. while (ptr < endp && *ptr++ != 0x4489)
  582. ;
  583. if (ptr < endp) {
  584. while (*ptr == 0x4489 && ptr < endp)
  585. ptr++;
  586. return (ulong)ptr;
  587. }
  588. return 0;
  589. }
  590. static inline unsigned long checksum(unsigned long *addr, int len)
  591. {
  592. unsigned long csum = 0;
  593. len /= sizeof(*addr);
  594. while (len-- > 0)
  595. csum ^= *addr++;
  596. csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
  597. return csum;
  598. }
  599. static unsigned long decode (unsigned long *data, unsigned long *raw,
  600. int len)
  601. {
  602. ulong *odd, *even;
  603. /* convert length from bytes to longwords */
  604. len >>= 2;
  605. odd = raw;
  606. even = odd + len;
  607. /* prepare return pointer */
  608. raw += len * 2;
  609. do {
  610. *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
  611. } while (--len != 0);
  612. return (ulong)raw;
  613. }
  614. struct header {
  615. unsigned char magic;
  616. unsigned char track;
  617. unsigned char sect;
  618. unsigned char ord;
  619. unsigned char labels[16];
  620. unsigned long hdrchk;
  621. unsigned long datachk;
  622. };
  623. static int amiga_read(int drive)
  624. {
  625. unsigned long raw;
  626. unsigned long end;
  627. int scnt;
  628. unsigned long csum;
  629. struct header hdr;
  630. drive&=3;
  631. raw = (long) raw_buf;
  632. end = raw + unit[drive].type->read_size;
  633. for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
  634. if (!(raw = scan_sync(raw, end))) {
  635. printk (KERN_INFO "can't find sync for sector %d\n", scnt);
  636. return MFM_NOSYNC;
  637. }
  638. raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
  639. raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
  640. raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
  641. raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
  642. csum = checksum((ulong *)&hdr,
  643. (char *)&hdr.hdrchk-(char *)&hdr);
  644. #ifdef DEBUG
  645. printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
  646. hdr.magic, hdr.track, hdr.sect, hdr.ord,
  647. *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
  648. *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
  649. hdr.hdrchk, hdr.datachk);
  650. #endif
  651. if (hdr.hdrchk != csum) {
  652. printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
  653. return MFM_HEADER;
  654. }
  655. /* verify track */
  656. if (hdr.track != unit[drive].track) {
  657. printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
  658. return MFM_TRACK;
  659. }
  660. raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
  661. (ulong *)raw, 512);
  662. csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
  663. if (hdr.datachk != csum) {
  664. printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
  665. hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
  666. hdr.datachk, csum);
  667. printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
  668. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
  669. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
  670. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
  671. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
  672. return MFM_DATA;
  673. }
  674. }
  675. return 0;
  676. }
  677. static void encode(unsigned long data, unsigned long *dest)
  678. {
  679. unsigned long data2;
  680. data &= 0x55555555;
  681. data2 = data ^ 0x55555555;
  682. data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
  683. if (*(dest - 1) & 0x00000001)
  684. data &= 0x7FFFFFFF;
  685. *dest = data;
  686. }
  687. static void encode_block(unsigned long *dest, unsigned long *src, int len)
  688. {
  689. int cnt, to_cnt = 0;
  690. unsigned long data;
  691. /* odd bits */
  692. for (cnt = 0; cnt < len / 4; cnt++) {
  693. data = src[cnt] >> 1;
  694. encode(data, dest + to_cnt++);
  695. }
  696. /* even bits */
  697. for (cnt = 0; cnt < len / 4; cnt++) {
  698. data = src[cnt];
  699. encode(data, dest + to_cnt++);
  700. }
  701. }
  702. static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
  703. {
  704. struct header hdr;
  705. int i;
  706. disk&=3;
  707. *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
  708. raw++;
  709. *raw++ = 0x44894489;
  710. hdr.magic = 0xFF;
  711. hdr.track = unit[disk].track;
  712. hdr.sect = cnt;
  713. hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
  714. for (i = 0; i < 16; i++)
  715. hdr.labels[i] = 0;
  716. hdr.hdrchk = checksum((ulong *)&hdr,
  717. (char *)&hdr.hdrchk-(char *)&hdr);
  718. hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
  719. encode_block(raw, (ulong *)&hdr.magic, 4);
  720. raw += 2;
  721. encode_block(raw, (ulong *)&hdr.labels, 16);
  722. raw += 8;
  723. encode_block(raw, (ulong *)&hdr.hdrchk, 4);
  724. raw += 2;
  725. encode_block(raw, (ulong *)&hdr.datachk, 4);
  726. raw += 2;
  727. encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
  728. raw += 256;
  729. return raw;
  730. }
  731. static void amiga_write(int disk)
  732. {
  733. unsigned int cnt;
  734. unsigned long *ptr = (unsigned long *)raw_buf;
  735. disk&=3;
  736. /* gap space */
  737. for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
  738. *ptr++ = 0xaaaaaaaa;
  739. /* sectors */
  740. for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
  741. ptr = putsec (disk, ptr, cnt);
  742. *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
  743. }
  744. struct dos_header {
  745. unsigned char track, /* 0-80 */
  746. side, /* 0-1 */
  747. sec, /* 0-...*/
  748. len_desc;/* 2 */
  749. unsigned short crc; /* on 68000 we got an alignment problem,
  750. but this compiler solves it by adding silently
  751. adding a pad byte so data won't fit
  752. and this took about 3h to discover.... */
  753. unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
  754. };
  755. /* crc routines are borrowed from the messydos-handler */
  756. /* excerpt from the messydos-device
  757. ; The CRC is computed not only over the actual data, but including
  758. ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
  759. ; As we don't read or encode these fields into our buffers, we have to
  760. ; preload the registers containing the CRC with the values they would have
  761. ; after stepping over these fields.
  762. ;
  763. ; How CRCs "really" work:
  764. ;
  765. ; First, you should regard a bitstring as a series of coefficients of
  766. ; polynomials. We calculate with these polynomials in modulo-2
  767. ; arithmetic, in which both add and subtract are done the same as
  768. ; exclusive-or. Now, we modify our data (a very long polynomial) in
  769. ; such a way that it becomes divisible by the CCITT-standard 16-bit
  770. ; 16 12 5
  771. ; polynomial: x + x + x + 1, represented by $11021. The easiest
  772. ; way to do this would be to multiply (using proper arithmetic) our
  773. ; datablock with $11021. So we have:
  774. ; data * $11021 =
  775. ; data * ($10000 + $1021) =
  776. ; data * $10000 + data * $1021
  777. ; The left part of this is simple: Just add two 0 bytes. But then
  778. ; the right part (data $1021) remains difficult and even could have
  779. ; a carry into the left part. The solution is to use a modified
  780. ; multiplication, which has a result that is not correct, but with
  781. ; a difference of any multiple of $11021. We then only need to keep
  782. ; the 16 least significant bits of the result.
  783. ;
  784. ; The following algorithm does this for us:
  785. ;
  786. ; unsigned char *data, c, crclo, crchi;
  787. ; while (not done) {
  788. ; c = *data++ + crchi;
  789. ; crchi = (@ c) >> 8 + crclo;
  790. ; crclo = @ c;
  791. ; }
  792. ;
  793. ; Remember, + is done with EOR, the @ operator is in two tables (high
  794. ; and low byte separately), which is calculated as
  795. ;
  796. ; $1021 * (c & $F0)
  797. ; xor $1021 * (c & $0F)
  798. ; xor $1021 * (c >> 4) (* is regular multiplication)
  799. ;
  800. ;
  801. ; Anyway, the end result is the same as the remainder of the division of
  802. ; the data by $11021. I am afraid I need to study theory a bit more...
  803. my only works was to code this from manx to C....
  804. */
  805. static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
  806. {
  807. static unsigned char CRCTable1[] = {
  808. 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
  809. 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
  810. 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
  811. 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
  812. 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
  813. 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
  814. 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
  815. 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
  816. 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
  817. 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
  818. 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
  819. 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
  820. 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
  821. 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
  822. 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
  823. 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
  824. };
  825. static unsigned char CRCTable2[] = {
  826. 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
  827. 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
  828. 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
  829. 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
  830. 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
  831. 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
  832. 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
  833. 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
  834. 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
  835. 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
  836. 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
  837. 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
  838. 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
  839. 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
  840. 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
  841. 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
  842. };
  843. /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
  844. register int i;
  845. register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
  846. CRCT1=CRCTable1;
  847. CRCT2=CRCTable2;
  848. data=data_a3;
  849. crcl=data_d1;
  850. crch=data_d0;
  851. for (i=data_d3; i>=0; i--) {
  852. c = (*data++) ^ crch;
  853. crch = CRCT1[c] ^ crcl;
  854. crcl = CRCT2[c];
  855. }
  856. return (crch<<8)|crcl;
  857. }
  858. static inline ushort dos_hdr_crc (struct dos_header *hdr)
  859. {
  860. return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
  861. }
  862. static inline ushort dos_data_crc(unsigned char *data)
  863. {
  864. return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
  865. }
  866. static inline unsigned char dos_decode_byte(ushort word)
  867. {
  868. register ushort w2;
  869. register unsigned char byte;
  870. register unsigned char *dec = mfmdecode;
  871. w2=word;
  872. w2>>=8;
  873. w2&=127;
  874. byte = dec[w2];
  875. byte <<= 4;
  876. w2 = word & 127;
  877. byte |= dec[w2];
  878. return byte;
  879. }
  880. static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
  881. {
  882. int i;
  883. for (i = 0; i < len; i++)
  884. *data++=dos_decode_byte(*raw++);
  885. return ((ulong)raw);
  886. }
  887. #ifdef DEBUG
  888. static void dbg(unsigned long ptr)
  889. {
  890. printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
  891. ((ulong *)ptr)[0], ((ulong *)ptr)[1],
  892. ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
  893. }
  894. #endif
  895. static int dos_read(int drive)
  896. {
  897. unsigned long end;
  898. unsigned long raw;
  899. int scnt;
  900. unsigned short crc,data_crc[2];
  901. struct dos_header hdr;
  902. drive&=3;
  903. raw = (long) raw_buf;
  904. end = raw + unit[drive].type->read_size;
  905. for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
  906. do { /* search for the right sync of each sec-hdr */
  907. if (!(raw = scan_sync (raw, end))) {
  908. printk(KERN_INFO "dos_read: no hdr sync on "
  909. "track %d, unit %d for sector %d\n",
  910. unit[drive].track,drive,scnt);
  911. return MFM_NOSYNC;
  912. }
  913. #ifdef DEBUG
  914. dbg(raw);
  915. #endif
  916. } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
  917. raw+=2; /* skip over headermark */
  918. raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
  919. crc = dos_hdr_crc(&hdr);
  920. #ifdef DEBUG
  921. printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
  922. hdr.sec, hdr.len_desc, hdr.crc);
  923. #endif
  924. if (crc != hdr.crc) {
  925. printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
  926. hdr.crc, crc);
  927. return MFM_HEADER;
  928. }
  929. if (hdr.track != unit[drive].track/unit[drive].type->heads) {
  930. printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
  931. hdr.track,
  932. unit[drive].track/unit[drive].type->heads);
  933. return MFM_TRACK;
  934. }
  935. if (hdr.side != unit[drive].track%unit[drive].type->heads) {
  936. printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
  937. hdr.side,
  938. unit[drive].track%unit[drive].type->heads);
  939. return MFM_TRACK;
  940. }
  941. if (hdr.len_desc != 2) {
  942. printk(KERN_INFO "dos_read: unknown sector len "
  943. "descriptor %d\n", hdr.len_desc);
  944. return MFM_DATA;
  945. }
  946. #ifdef DEBUG
  947. printk("hdr accepted\n");
  948. #endif
  949. if (!(raw = scan_sync (raw, end))) {
  950. printk(KERN_INFO "dos_read: no data sync on track "
  951. "%d, unit %d for sector%d, disk sector %d\n",
  952. unit[drive].track, drive, scnt, hdr.sec);
  953. return MFM_NOSYNC;
  954. }
  955. #ifdef DEBUG
  956. dbg(raw);
  957. #endif
  958. if (*((ushort *)raw)!=0x5545) {
  959. printk(KERN_INFO "dos_read: no data mark after "
  960. "sync (%d,%d,%d,%d) sc=%d\n",
  961. hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
  962. return MFM_NOSYNC;
  963. }
  964. raw+=2; /* skip data mark (included in checksum) */
  965. raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
  966. raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
  967. crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
  968. if (crc != data_crc[0]) {
  969. printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
  970. "sc=%d, %x %x\n", hdr.track, hdr.side,
  971. hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
  972. printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
  973. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
  974. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
  975. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
  976. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
  977. return MFM_DATA;
  978. }
  979. }
  980. return 0;
  981. }
  982. static inline ushort dos_encode_byte(unsigned char byte)
  983. {
  984. register unsigned char *enc, b2, b1;
  985. register ushort word;
  986. enc=mfmencode;
  987. b1=byte;
  988. b2=b1>>4;
  989. b1&=15;
  990. word=enc[b2] <<8 | enc [b1];
  991. return (word|((word&(256|64)) ? 0: 128));
  992. }
  993. static void dos_encode_block(ushort *dest, unsigned char *src, int len)
  994. {
  995. int i;
  996. for (i = 0; i < len; i++) {
  997. *dest=dos_encode_byte(*src++);
  998. *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
  999. dest++;
  1000. }
  1001. }
  1002. static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
  1003. {
  1004. static struct dos_header hdr={0,0,0,2,0,
  1005. {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
  1006. int i;
  1007. static ushort crc[2]={0,0x4e4e};
  1008. drive&=3;
  1009. /* id gap 1 */
  1010. /* the MFM word before is always 9254 */
  1011. for(i=0;i<6;i++)
  1012. *raw++=0xaaaaaaaa;
  1013. /* 3 sync + 1 headermark */
  1014. *raw++=0x44894489;
  1015. *raw++=0x44895554;
  1016. /* fill in the variable parts of the header */
  1017. hdr.track=unit[drive].track/unit[drive].type->heads;
  1018. hdr.side=unit[drive].track%unit[drive].type->heads;
  1019. hdr.sec=cnt+1;
  1020. hdr.crc=dos_hdr_crc(&hdr);
  1021. /* header (without "magic") and id gap 2*/
  1022. dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
  1023. raw+=14;
  1024. /*id gap 3 */
  1025. for(i=0;i<6;i++)
  1026. *raw++=0xaaaaaaaa;
  1027. /* 3 syncs and 1 datamark */
  1028. *raw++=0x44894489;
  1029. *raw++=0x44895545;
  1030. /* data */
  1031. dos_encode_block((ushort *)raw,
  1032. (unsigned char *)unit[drive].trackbuf+cnt*512,512);
  1033. raw+=256;
  1034. /*data crc + jd's special gap (long words :-/) */
  1035. crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
  1036. dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
  1037. raw+=2;
  1038. /* data gap */
  1039. for(i=0;i<38;i++)
  1040. *raw++=0x92549254;
  1041. return raw; /* wrote 652 MFM words */
  1042. }
  1043. static void dos_write(int disk)
  1044. {
  1045. int cnt;
  1046. unsigned long raw = (unsigned long) raw_buf;
  1047. unsigned long *ptr=(unsigned long *)raw;
  1048. disk&=3;
  1049. /* really gap4 + indexgap , but we write it first and round it up */
  1050. for (cnt=0;cnt<425;cnt++)
  1051. *ptr++=0x92549254;
  1052. /* the following is just guessed */
  1053. if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
  1054. for(cnt=0;cnt<473;cnt++)
  1055. *ptr++=0x92549254;
  1056. /* now the index marks...*/
  1057. for (cnt=0;cnt<20;cnt++)
  1058. *ptr++=0x92549254;
  1059. for (cnt=0;cnt<6;cnt++)
  1060. *ptr++=0xaaaaaaaa;
  1061. *ptr++=0x52245224;
  1062. *ptr++=0x52245552;
  1063. for (cnt=0;cnt<20;cnt++)
  1064. *ptr++=0x92549254;
  1065. /* sectors */
  1066. for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
  1067. ptr=ms_putsec(disk,ptr,cnt);
  1068. *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
  1069. }
  1070. /*
  1071. * Here comes the high level stuff (i.e. the filesystem interface)
  1072. * and helper functions.
  1073. * Normally this should be the only part that has to be adapted to
  1074. * different kernel versions.
  1075. */
  1076. /* FIXME: this assumes the drive is still spinning -
  1077. * which is only true if we complete writing a track within three seconds
  1078. */
  1079. static void flush_track_callback(unsigned long nr)
  1080. {
  1081. nr&=3;
  1082. writefromint = 1;
  1083. if (!try_fdc(nr)) {
  1084. /* we might block in an interrupt, so try again later */
  1085. flush_track_timer[nr].expires = jiffies + 1;
  1086. add_timer(flush_track_timer + nr);
  1087. return;
  1088. }
  1089. get_fdc(nr);
  1090. (*unit[nr].dtype->write_fkt)(nr);
  1091. if (!raw_write(nr)) {
  1092. printk (KERN_NOTICE "floppy disk write protected\n");
  1093. writefromint = 0;
  1094. writepending = 0;
  1095. }
  1096. rel_fdc();
  1097. }
  1098. static int non_int_flush_track (unsigned long nr)
  1099. {
  1100. unsigned long flags;
  1101. nr&=3;
  1102. writefromint = 0;
  1103. del_timer(&post_write_timer);
  1104. get_fdc(nr);
  1105. if (!fd_motor_on(nr)) {
  1106. writepending = 0;
  1107. rel_fdc();
  1108. return 0;
  1109. }
  1110. local_irq_save(flags);
  1111. if (writepending != 2) {
  1112. local_irq_restore(flags);
  1113. (*unit[nr].dtype->write_fkt)(nr);
  1114. if (!raw_write(nr)) {
  1115. printk (KERN_NOTICE "floppy disk write protected "
  1116. "in write!\n");
  1117. writepending = 0;
  1118. return 0;
  1119. }
  1120. while (block_flag == 2)
  1121. sleep_on (&wait_fd_block);
  1122. }
  1123. else {
  1124. local_irq_restore(flags);
  1125. ms_delay(2); /* 2 ms post_write delay */
  1126. post_write(nr);
  1127. }
  1128. rel_fdc();
  1129. return 1;
  1130. }
  1131. static int get_track(int drive, int track)
  1132. {
  1133. int error, errcnt;
  1134. drive&=3;
  1135. if (unit[drive].track == track)
  1136. return 0;
  1137. get_fdc(drive);
  1138. if (!fd_motor_on(drive)) {
  1139. rel_fdc();
  1140. return -1;
  1141. }
  1142. if (unit[drive].dirty == 1) {
  1143. del_timer (flush_track_timer + drive);
  1144. non_int_flush_track (drive);
  1145. }
  1146. errcnt = 0;
  1147. while (errcnt < MAX_ERRORS) {
  1148. if (!fd_seek(drive, track))
  1149. return -1;
  1150. raw_read(drive);
  1151. error = (*unit[drive].dtype->read_fkt)(drive);
  1152. if (error == 0) {
  1153. rel_fdc();
  1154. return 0;
  1155. }
  1156. /* Read Error Handling: recalibrate and try again */
  1157. unit[drive].track = -1;
  1158. errcnt++;
  1159. }
  1160. rel_fdc();
  1161. return -1;
  1162. }
  1163. static void redo_fd_request(void)
  1164. {
  1165. unsigned int cnt, block, track, sector;
  1166. int drive;
  1167. struct amiga_floppy_struct *floppy;
  1168. char *data;
  1169. unsigned long flags;
  1170. repeat:
  1171. if (!CURRENT) {
  1172. /* Nothing left to do */
  1173. return;
  1174. }
  1175. floppy = CURRENT->rq_disk->private_data;
  1176. drive = floppy - unit;
  1177. /* Here someone could investigate to be more efficient */
  1178. for (cnt = 0; cnt < CURRENT->current_nr_sectors; cnt++) {
  1179. #ifdef DEBUG
  1180. printk("fd: sector %ld + %d requested for %s\n",
  1181. CURRENT->sector,cnt,
  1182. (rq_data_dir(CURRENT) == READ) ? "read" : "write");
  1183. #endif
  1184. block = CURRENT->sector + cnt;
  1185. if ((int)block > floppy->blocks) {
  1186. end_request(CURRENT, 0);
  1187. goto repeat;
  1188. }
  1189. track = block / (floppy->dtype->sects * floppy->type->sect_mult);
  1190. sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
  1191. data = CURRENT->buffer + 512 * cnt;
  1192. #ifdef DEBUG
  1193. printk("access to track %d, sector %d, with buffer at "
  1194. "0x%08lx\n", track, sector, data);
  1195. #endif
  1196. if ((rq_data_dir(CURRENT) != READ) && (rq_data_dir(CURRENT) != WRITE)) {
  1197. printk(KERN_WARNING "do_fd_request: unknown command\n");
  1198. end_request(CURRENT, 0);
  1199. goto repeat;
  1200. }
  1201. if (get_track(drive, track) == -1) {
  1202. end_request(CURRENT, 0);
  1203. goto repeat;
  1204. }
  1205. switch (rq_data_dir(CURRENT)) {
  1206. case READ:
  1207. memcpy(data, floppy->trackbuf + sector * 512, 512);
  1208. break;
  1209. case WRITE:
  1210. memcpy(floppy->trackbuf + sector * 512, data, 512);
  1211. /* keep the drive spinning while writes are scheduled */
  1212. if (!fd_motor_on(drive)) {
  1213. end_request(CURRENT, 0);
  1214. goto repeat;
  1215. }
  1216. /*
  1217. * setup a callback to write the track buffer
  1218. * after a short (1 tick) delay.
  1219. */
  1220. local_irq_save(flags);
  1221. floppy->dirty = 1;
  1222. /* reset the timer */
  1223. mod_timer (flush_track_timer + drive, jiffies + 1);
  1224. local_irq_restore(flags);
  1225. break;
  1226. }
  1227. }
  1228. CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
  1229. CURRENT->sector += CURRENT->current_nr_sectors;
  1230. end_request(CURRENT, 1);
  1231. goto repeat;
  1232. }
  1233. static void do_fd_request(struct request_queue * q)
  1234. {
  1235. redo_fd_request();
  1236. }
  1237. static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1238. {
  1239. int drive = MINOR(bdev->bd_dev) & 3;
  1240. geo->heads = unit[drive].type->heads;
  1241. geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
  1242. geo->cylinders = unit[drive].type->tracks;
  1243. return 0;
  1244. }
  1245. static int fd_ioctl(struct inode *inode, struct file *filp,
  1246. unsigned int cmd, unsigned long param)
  1247. {
  1248. int drive = iminor(inode) & 3;
  1249. static struct floppy_struct getprm;
  1250. void __user *argp = (void __user *)param;
  1251. switch(cmd){
  1252. case FDFMTBEG:
  1253. get_fdc(drive);
  1254. if (fd_ref[drive] > 1) {
  1255. rel_fdc();
  1256. return -EBUSY;
  1257. }
  1258. fsync_bdev(inode->i_bdev);
  1259. if (fd_motor_on(drive) == 0) {
  1260. rel_fdc();
  1261. return -ENODEV;
  1262. }
  1263. if (fd_calibrate(drive) == 0) {
  1264. rel_fdc();
  1265. return -ENXIO;
  1266. }
  1267. floppy_off(drive);
  1268. rel_fdc();
  1269. break;
  1270. case FDFMTTRK:
  1271. if (param < unit[drive].type->tracks * unit[drive].type->heads)
  1272. {
  1273. get_fdc(drive);
  1274. if (fd_seek(drive,param) != 0){
  1275. memset(unit[drive].trackbuf, FD_FILL_BYTE,
  1276. unit[drive].dtype->sects * unit[drive].type->sect_mult * 512);
  1277. non_int_flush_track(drive);
  1278. }
  1279. floppy_off(drive);
  1280. rel_fdc();
  1281. }
  1282. else
  1283. return -EINVAL;
  1284. break;
  1285. case FDFMTEND:
  1286. floppy_off(drive);
  1287. invalidate_bdev(inode->i_bdev);
  1288. break;
  1289. case FDGETPRM:
  1290. memset((void *)&getprm, 0, sizeof (getprm));
  1291. getprm.track=unit[drive].type->tracks;
  1292. getprm.head=unit[drive].type->heads;
  1293. getprm.sect=unit[drive].dtype->sects * unit[drive].type->sect_mult;
  1294. getprm.size=unit[drive].blocks;
  1295. if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
  1296. return -EFAULT;
  1297. break;
  1298. case FDSETPRM:
  1299. case FDDEFPRM:
  1300. return -EINVAL;
  1301. case FDFLUSH: /* unconditionally, even if not needed */
  1302. del_timer (flush_track_timer + drive);
  1303. non_int_flush_track(drive);
  1304. break;
  1305. #ifdef RAW_IOCTL
  1306. case IOCTL_RAW_TRACK:
  1307. if (copy_to_user(argp, raw_buf, unit[drive].type->read_size))
  1308. return -EFAULT;
  1309. else
  1310. return unit[drive].type->read_size;
  1311. #endif
  1312. default:
  1313. printk(KERN_DEBUG "fd_ioctl: unknown cmd %d for drive %d.",
  1314. cmd, drive);
  1315. return -ENOSYS;
  1316. }
  1317. return 0;
  1318. }
  1319. static void fd_probe(int dev)
  1320. {
  1321. unsigned long code;
  1322. int type;
  1323. int drive;
  1324. drive = dev & 3;
  1325. code = fd_get_drive_id(drive);
  1326. /* get drive type */
  1327. for (type = 0; type < num_dr_types; type++)
  1328. if (drive_types[type].code == code)
  1329. break;
  1330. if (type >= num_dr_types) {
  1331. printk(KERN_WARNING "fd_probe: unsupported drive type "
  1332. "%08lx found\n", code);
  1333. unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
  1334. return;
  1335. }
  1336. unit[drive].type = drive_types + type;
  1337. unit[drive].track = -1;
  1338. unit[drive].disk = -1;
  1339. unit[drive].motor = 0;
  1340. unit[drive].busy = 0;
  1341. unit[drive].status = -1;
  1342. }
  1343. /*
  1344. * floppy_open check for aliasing (/dev/fd0 can be the same as
  1345. * /dev/PS0 etc), and disallows simultaneous access to the same
  1346. * drive with different device numbers.
  1347. */
  1348. static int floppy_open(struct inode *inode, struct file *filp)
  1349. {
  1350. int drive = iminor(inode) & 3;
  1351. int system = (iminor(inode) & 4) >> 2;
  1352. int old_dev;
  1353. unsigned long flags;
  1354. old_dev = fd_device[drive];
  1355. if (fd_ref[drive] && old_dev != system)
  1356. return -EBUSY;
  1357. if (filp && filp->f_mode & 3) {
  1358. check_disk_change(inode->i_bdev);
  1359. if (filp->f_mode & 2 ) {
  1360. int wrprot;
  1361. get_fdc(drive);
  1362. fd_select (drive);
  1363. wrprot = !(ciaa.pra & DSKPROT);
  1364. fd_deselect (drive);
  1365. rel_fdc();
  1366. if (wrprot)
  1367. return -EROFS;
  1368. }
  1369. }
  1370. local_irq_save(flags);
  1371. fd_ref[drive]++;
  1372. fd_device[drive] = system;
  1373. local_irq_restore(flags);
  1374. unit[drive].dtype=&data_types[system];
  1375. unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
  1376. data_types[system].sects*unit[drive].type->sect_mult;
  1377. set_capacity(unit[drive].gendisk, unit[drive].blocks);
  1378. printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
  1379. unit[drive].type->name, data_types[system].name);
  1380. return 0;
  1381. }
  1382. static int floppy_release(struct inode * inode, struct file * filp)
  1383. {
  1384. int drive = iminor(inode) & 3;
  1385. if (unit[drive].dirty == 1) {
  1386. del_timer (flush_track_timer + drive);
  1387. non_int_flush_track (drive);
  1388. }
  1389. if (!fd_ref[drive]--) {
  1390. printk(KERN_CRIT "floppy_release with fd_ref == 0");
  1391. fd_ref[drive] = 0;
  1392. }
  1393. #ifdef MODULE
  1394. /* the mod_use counter is handled this way */
  1395. floppy_off (drive | 0x40000000);
  1396. #endif
  1397. return 0;
  1398. }
  1399. /*
  1400. * floppy-change is never called from an interrupt, so we can relax a bit
  1401. * here, sleep etc. Note that floppy-on tries to set current_DOR to point
  1402. * to the desired drive, but it will probably not survive the sleep if
  1403. * several floppies are used at the same time: thus the loop.
  1404. */
  1405. static int amiga_floppy_change(struct gendisk *disk)
  1406. {
  1407. struct amiga_floppy_struct *p = disk->private_data;
  1408. int drive = p - unit;
  1409. int changed;
  1410. static int first_time = 1;
  1411. if (first_time)
  1412. changed = first_time--;
  1413. else {
  1414. get_fdc(drive);
  1415. fd_select (drive);
  1416. changed = !(ciaa.pra & DSKCHANGE);
  1417. fd_deselect (drive);
  1418. rel_fdc();
  1419. }
  1420. if (changed) {
  1421. fd_probe(drive);
  1422. p->track = -1;
  1423. p->dirty = 0;
  1424. writepending = 0; /* if this was true before, too bad! */
  1425. writefromint = 0;
  1426. return 1;
  1427. }
  1428. return 0;
  1429. }
  1430. static struct block_device_operations floppy_fops = {
  1431. .owner = THIS_MODULE,
  1432. .open = floppy_open,
  1433. .release = floppy_release,
  1434. .ioctl = fd_ioctl,
  1435. .getgeo = fd_getgeo,
  1436. .media_changed = amiga_floppy_change,
  1437. };
  1438. static int __init fd_probe_drives(void)
  1439. {
  1440. int drive,drives,nomem;
  1441. printk(KERN_INFO "FD: probing units\n" KERN_INFO "found ");
  1442. drives=0;
  1443. nomem=0;
  1444. for(drive=0;drive<FD_MAX_UNITS;drive++) {
  1445. struct gendisk *disk;
  1446. fd_probe(drive);
  1447. if (unit[drive].type->code == FD_NODRIVE)
  1448. continue;
  1449. disk = alloc_disk(1);
  1450. if (!disk) {
  1451. unit[drive].type->code = FD_NODRIVE;
  1452. continue;
  1453. }
  1454. unit[drive].gendisk = disk;
  1455. drives++;
  1456. if ((unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL)) == NULL) {
  1457. printk("no mem for ");
  1458. unit[drive].type = &drive_types[num_dr_types - 1]; /* FD_NODRIVE */
  1459. drives--;
  1460. nomem = 1;
  1461. }
  1462. printk("fd%d ",drive);
  1463. disk->major = FLOPPY_MAJOR;
  1464. disk->first_minor = drive;
  1465. disk->fops = &floppy_fops;
  1466. sprintf(disk->disk_name, "fd%d", drive);
  1467. disk->private_data = &unit[drive];
  1468. disk->queue = floppy_queue;
  1469. set_capacity(disk, 880*2);
  1470. add_disk(disk);
  1471. }
  1472. if ((drives > 0) || (nomem == 0)) {
  1473. if (drives == 0)
  1474. printk("no drives");
  1475. printk("\n");
  1476. return drives;
  1477. }
  1478. printk("\n");
  1479. return -ENOMEM;
  1480. }
  1481. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  1482. {
  1483. int drive = *part & 3;
  1484. if (unit[drive].type->code == FD_NODRIVE)
  1485. return NULL;
  1486. *part = 0;
  1487. return get_disk(unit[drive].gendisk);
  1488. }
  1489. static int __init amiga_floppy_init(void)
  1490. {
  1491. int i, ret;
  1492. if (!MACH_IS_AMIGA)
  1493. return -ENXIO;
  1494. if (!AMIGAHW_PRESENT(AMI_FLOPPY))
  1495. return -ENXIO;
  1496. if (register_blkdev(FLOPPY_MAJOR,"fd"))
  1497. return -EBUSY;
  1498. /*
  1499. * We request DSKPTR, DSKLEN and DSKDATA only, because the other
  1500. * floppy registers are too spreaded over the custom register space
  1501. */
  1502. ret = -EBUSY;
  1503. if (!request_mem_region(CUSTOM_PHYSADDR+0x20, 8, "amiflop [Paula]")) {
  1504. printk("fd: cannot get floppy registers\n");
  1505. goto out_blkdev;
  1506. }
  1507. ret = -ENOMEM;
  1508. if ((raw_buf = (char *)amiga_chip_alloc (RAW_BUF_SIZE, "Floppy")) ==
  1509. NULL) {
  1510. printk("fd: cannot get chip mem buffer\n");
  1511. goto out_memregion;
  1512. }
  1513. ret = -EBUSY;
  1514. if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
  1515. printk("fd: cannot get irq for dma\n");
  1516. goto out_irq;
  1517. }
  1518. if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
  1519. printk("fd: cannot get irq for timer\n");
  1520. goto out_irq2;
  1521. }
  1522. ret = -ENOMEM;
  1523. floppy_queue = blk_init_queue(do_fd_request, &amiflop_lock);
  1524. if (!floppy_queue)
  1525. goto out_queue;
  1526. ret = -ENXIO;
  1527. if (fd_probe_drives() < 1) /* No usable drives */
  1528. goto out_probe;
  1529. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  1530. floppy_find, NULL, NULL);
  1531. /* initialize variables */
  1532. init_timer(&motor_on_timer);
  1533. motor_on_timer.expires = 0;
  1534. motor_on_timer.data = 0;
  1535. motor_on_timer.function = motor_on_callback;
  1536. for (i = 0; i < FD_MAX_UNITS; i++) {
  1537. init_timer(&motor_off_timer[i]);
  1538. motor_off_timer[i].expires = 0;
  1539. motor_off_timer[i].data = i|0x80000000;
  1540. motor_off_timer[i].function = fd_motor_off;
  1541. init_timer(&flush_track_timer[i]);
  1542. flush_track_timer[i].expires = 0;
  1543. flush_track_timer[i].data = i;
  1544. flush_track_timer[i].function = flush_track_callback;
  1545. unit[i].track = -1;
  1546. }
  1547. init_timer(&post_write_timer);
  1548. post_write_timer.expires = 0;
  1549. post_write_timer.data = 0;
  1550. post_write_timer.function = post_write;
  1551. for (i = 0; i < 128; i++)
  1552. mfmdecode[i]=255;
  1553. for (i = 0; i < 16; i++)
  1554. mfmdecode[mfmencode[i]]=i;
  1555. /* make sure that disk DMA is enabled */
  1556. custom.dmacon = DMAF_SETCLR | DMAF_DISK;
  1557. /* init ms timer */
  1558. ciaa.crb = 8; /* one-shot, stop */
  1559. return 0;
  1560. out_probe:
  1561. blk_cleanup_queue(floppy_queue);
  1562. out_queue:
  1563. free_irq(IRQ_AMIGA_CIAA_TB, NULL);
  1564. out_irq2:
  1565. free_irq(IRQ_AMIGA_DSKBLK, NULL);
  1566. out_irq:
  1567. amiga_chip_free(raw_buf);
  1568. out_memregion:
  1569. release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
  1570. out_blkdev:
  1571. unregister_blkdev(FLOPPY_MAJOR,"fd");
  1572. return ret;
  1573. }
  1574. module_init(amiga_floppy_init);
  1575. #ifdef MODULE
  1576. #if 0 /* not safe to unload */
  1577. void cleanup_module(void)
  1578. {
  1579. int i;
  1580. for( i = 0; i < FD_MAX_UNITS; i++) {
  1581. if (unit[i].type->code != FD_NODRIVE) {
  1582. del_gendisk(unit[i].gendisk);
  1583. put_disk(unit[i].gendisk);
  1584. kfree(unit[i].trackbuf);
  1585. }
  1586. }
  1587. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  1588. free_irq(IRQ_AMIGA_CIAA_TB, NULL);
  1589. free_irq(IRQ_AMIGA_DSKBLK, NULL);
  1590. custom.dmacon = DMAF_DISK; /* disable DMA */
  1591. amiga_chip_free(raw_buf);
  1592. blk_cleanup_queue(floppy_queue);
  1593. release_mem_region(CUSTOM_PHYSADDR+0x20, 8);
  1594. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1595. }
  1596. #endif
  1597. #else
  1598. static int __init amiga_floppy_setup (char *str)
  1599. {
  1600. int n;
  1601. if (!MACH_IS_AMIGA)
  1602. return 0;
  1603. if (!get_option(&str, &n))
  1604. return 0;
  1605. printk (KERN_INFO "amiflop: Setting default df0 to %x\n", n);
  1606. fd_def_df0 = n;
  1607. return 1;
  1608. }
  1609. __setup("floppy=", amiga_floppy_setup);
  1610. #endif