amiflop.c 46 KB

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