amiflop.c 46 KB

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