amiflop.c 46 KB

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