amiflop.c 46 KB

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