amiflop.c 46 KB

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