amiflop.c 46 KB

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