amiflop.c 46 KB

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