amiflop.c 47 KB

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