amiflop.c 46 KB

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