floppy.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622
  1. /*
  2. * linux/drivers/block/floppy.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 1993, 1994 Alain Knaff
  6. * Copyright (C) 1998 Alan Cox
  7. */
  8. /*
  9. * 02.12.91 - Changed to static variables to indicate need for reset
  10. * and recalibrate. This makes some things easier (output_byte reset
  11. * checking etc), and means less interrupt jumping in case of errors,
  12. * so the code is hopefully easier to understand.
  13. */
  14. /*
  15. * This file is certainly a mess. I've tried my best to get it working,
  16. * but I don't like programming floppies, and I have only one anyway.
  17. * Urgel. I should check for more errors, and do more graceful error
  18. * recovery. Seems there are problems with several drives. I've tried to
  19. * correct them. No promises.
  20. */
  21. /*
  22. * As with hd.c, all routines within this file can (and will) be called
  23. * by interrupts, so extreme caution is needed. A hardware interrupt
  24. * handler may not sleep, or a kernel panic will happen. Thus I cannot
  25. * call "floppy-on" directly, but have to set a special timer interrupt
  26. * etc.
  27. */
  28. /*
  29. * 28.02.92 - made track-buffering routines, based on the routines written
  30. * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
  31. */
  32. /*
  33. * Automatic floppy-detection and formatting written by Werner Almesberger
  34. * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
  35. * the floppy-change signal detection.
  36. */
  37. /*
  38. * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
  39. * FDC data overrun bug, added some preliminary stuff for vertical
  40. * recording support.
  41. *
  42. * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
  43. *
  44. * TODO: Errors are still not counted properly.
  45. */
  46. /* 1992/9/20
  47. * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
  48. * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
  49. * Christoph H. Hochst\"atter.
  50. * I have fixed the shift values to the ones I always use. Maybe a new
  51. * ioctl() should be created to be able to modify them.
  52. * There is a bug in the driver that makes it impossible to format a
  53. * floppy as the first thing after bootup.
  54. */
  55. /*
  56. * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
  57. * this helped the floppy driver as well. Much cleaner, and still seems to
  58. * work.
  59. */
  60. /* 1994/6/24 --bbroad-- added the floppy table entries and made
  61. * minor modifications to allow 2.88 floppies to be run.
  62. */
  63. /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
  64. * disk types.
  65. */
  66. /*
  67. * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
  68. * format bug fixes, but unfortunately some new bugs too...
  69. */
  70. /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
  71. * errors to allow safe writing by specialized programs.
  72. */
  73. /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
  74. * by defining bit 1 of the "stretch" parameter to mean put sectors on the
  75. * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
  76. * drives are "upside-down").
  77. */
  78. /*
  79. * 1995/8/26 -- Andreas Busse -- added Mips support.
  80. */
  81. /*
  82. * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
  83. * features to asm/floppy.h.
  84. */
  85. /*
  86. * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
  87. */
  88. /*
  89. * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
  90. * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
  91. * use of '0' for NULL.
  92. */
  93. /*
  94. * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
  95. * failures.
  96. */
  97. /*
  98. * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
  99. */
  100. /*
  101. * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
  102. * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
  103. * being used to store jiffies, which are unsigned longs).
  104. */
  105. /*
  106. * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  107. * - get rid of check_region
  108. * - s/suser/capable/
  109. */
  110. /*
  111. * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
  112. * floppy controller (lingering task on list after module is gone... boom.)
  113. */
  114. /*
  115. * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
  116. * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
  117. * requires many non-obvious changes in arch dependent code.
  118. */
  119. /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
  120. * Better audit of register_blkdev.
  121. */
  122. #define FLOPPY_SANITY_CHECK
  123. #undef FLOPPY_SILENT_DCL_CLEAR
  124. #define REALLY_SLOW_IO
  125. #define DEBUGT 2
  126. #define DCL_DEBUG /* debug disk change line */
  127. /* do print messages for unexpected interrupts */
  128. static int print_unex = 1;
  129. #include <linux/module.h>
  130. #include <linux/sched.h>
  131. #include <linux/fs.h>
  132. #include <linux/kernel.h>
  133. #include <linux/timer.h>
  134. #include <linux/workqueue.h>
  135. #define FDPATCHES
  136. #include <linux/fdreg.h>
  137. #include <linux/fd.h>
  138. #include <linux/hdreg.h>
  139. #include <linux/errno.h>
  140. #include <linux/slab.h>
  141. #include <linux/mm.h>
  142. #include <linux/bio.h>
  143. #include <linux/string.h>
  144. #include <linux/jiffies.h>
  145. #include <linux/fcntl.h>
  146. #include <linux/delay.h>
  147. #include <linux/mc146818rtc.h> /* CMOS defines */
  148. #include <linux/ioport.h>
  149. #include <linux/interrupt.h>
  150. #include <linux/init.h>
  151. #include <linux/devfs_fs_kernel.h>
  152. #include <linux/platform_device.h>
  153. #include <linux/buffer_head.h> /* for invalidate_buffers() */
  154. #include <linux/mutex.h>
  155. /*
  156. * PS/2 floppies have much slower step rates than regular floppies.
  157. * It's been recommended that take about 1/4 of the default speed
  158. * in some more extreme cases.
  159. */
  160. static int slow_floppy;
  161. #include <asm/dma.h>
  162. #include <asm/irq.h>
  163. #include <asm/system.h>
  164. #include <asm/io.h>
  165. #include <asm/uaccess.h>
  166. static int FLOPPY_IRQ = 6;
  167. static int FLOPPY_DMA = 2;
  168. static int can_use_virtual_dma = 2;
  169. /* =======
  170. * can use virtual DMA:
  171. * 0 = use of virtual DMA disallowed by config
  172. * 1 = use of virtual DMA prescribed by config
  173. * 2 = no virtual DMA preference configured. By default try hard DMA,
  174. * but fall back on virtual DMA when not enough memory available
  175. */
  176. static int use_virtual_dma;
  177. /* =======
  178. * use virtual DMA
  179. * 0 using hard DMA
  180. * 1 using virtual DMA
  181. * This variable is set to virtual when a DMA mem problem arises, and
  182. * reset back in floppy_grab_irq_and_dma.
  183. * It is not safe to reset it in other circumstances, because the floppy
  184. * driver may have several buffers in use at once, and we do currently not
  185. * record each buffers capabilities
  186. */
  187. static DEFINE_SPINLOCK(floppy_lock);
  188. static struct completion device_release;
  189. static unsigned short virtual_dma_port = 0x3f0;
  190. irqreturn_t floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  191. static int set_dor(int fdc, char mask, char data);
  192. #define K_64 0x10000 /* 64KB */
  193. /* the following is the mask of allowed drives. By default units 2 and
  194. * 3 of both floppy controllers are disabled, because switching on the
  195. * motor of these drives causes system hangs on some PCI computers. drive
  196. * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
  197. * a drive is allowed.
  198. *
  199. * NOTE: This must come before we include the arch floppy header because
  200. * some ports reference this variable from there. -DaveM
  201. */
  202. static int allowed_drive_mask = 0x33;
  203. #include <asm/floppy.h>
  204. static int irqdma_allocated;
  205. #define DEVICE_NAME "floppy"
  206. #include <linux/blkdev.h>
  207. #include <linux/blkpg.h>
  208. #include <linux/cdrom.h> /* for the compatibility eject ioctl */
  209. #include <linux/completion.h>
  210. /*
  211. * Interrupt freeing also means /proc VFS work - dont do it
  212. * from interrupt context. We push this work into keventd:
  213. */
  214. static void fd_free_irq_fn(void *data)
  215. {
  216. fd_free_irq();
  217. }
  218. static DECLARE_WORK(fd_free_irq_work, fd_free_irq_fn, NULL);
  219. static struct request *current_req;
  220. static struct request_queue *floppy_queue;
  221. static void do_fd_request(request_queue_t * q);
  222. #ifndef fd_get_dma_residue
  223. #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
  224. #endif
  225. /* Dma Memory related stuff */
  226. #ifndef fd_dma_mem_free
  227. #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
  228. #endif
  229. #ifndef fd_dma_mem_alloc
  230. #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
  231. #endif
  232. static inline void fallback_on_nodma_alloc(char **addr, size_t l)
  233. {
  234. #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
  235. if (*addr)
  236. return; /* we have the memory */
  237. if (can_use_virtual_dma != 2)
  238. return; /* no fallback allowed */
  239. printk
  240. ("DMA memory shortage. Temporarily falling back on virtual DMA\n");
  241. *addr = (char *)nodma_mem_alloc(l);
  242. #else
  243. return;
  244. #endif
  245. }
  246. /* End dma memory related stuff */
  247. static unsigned long fake_change;
  248. static int initialising = 1;
  249. #define ITYPE(x) (((x)>>2) & 0x1f)
  250. #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
  251. #define UNIT(x) ((x) & 0x03) /* drive on fdc */
  252. #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
  253. #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
  254. /* reverse mapping from unit and fdc to drive */
  255. #define DP (&drive_params[current_drive])
  256. #define DRS (&drive_state[current_drive])
  257. #define DRWE (&write_errors[current_drive])
  258. #define FDCS (&fdc_state[fdc])
  259. #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
  260. #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
  261. #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
  262. #define UDP (&drive_params[drive])
  263. #define UDRS (&drive_state[drive])
  264. #define UDRWE (&write_errors[drive])
  265. #define UFDCS (&fdc_state[FDC(drive)])
  266. #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
  267. #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
  268. #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
  269. #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
  270. #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
  271. #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
  272. #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
  273. /* read/write */
  274. #define COMMAND raw_cmd->cmd[0]
  275. #define DR_SELECT raw_cmd->cmd[1]
  276. #define TRACK raw_cmd->cmd[2]
  277. #define HEAD raw_cmd->cmd[3]
  278. #define SECTOR raw_cmd->cmd[4]
  279. #define SIZECODE raw_cmd->cmd[5]
  280. #define SECT_PER_TRACK raw_cmd->cmd[6]
  281. #define GAP raw_cmd->cmd[7]
  282. #define SIZECODE2 raw_cmd->cmd[8]
  283. #define NR_RW 9
  284. /* format */
  285. #define F_SIZECODE raw_cmd->cmd[2]
  286. #define F_SECT_PER_TRACK raw_cmd->cmd[3]
  287. #define F_GAP raw_cmd->cmd[4]
  288. #define F_FILL raw_cmd->cmd[5]
  289. #define NR_F 6
  290. /*
  291. * Maximum disk size (in kilobytes). This default is used whenever the
  292. * current disk size is unknown.
  293. * [Now it is rather a minimum]
  294. */
  295. #define MAX_DISK_SIZE 4 /* 3984 */
  296. /*
  297. * globals used by 'result()'
  298. */
  299. #define MAX_REPLIES 16
  300. static unsigned char reply_buffer[MAX_REPLIES];
  301. static int inr; /* size of reply buffer, when called from interrupt */
  302. #define ST0 (reply_buffer[0])
  303. #define ST1 (reply_buffer[1])
  304. #define ST2 (reply_buffer[2])
  305. #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
  306. #define R_TRACK (reply_buffer[3])
  307. #define R_HEAD (reply_buffer[4])
  308. #define R_SECTOR (reply_buffer[5])
  309. #define R_SIZECODE (reply_buffer[6])
  310. #define SEL_DLY (2*HZ/100)
  311. /*
  312. * this struct defines the different floppy drive types.
  313. */
  314. static struct {
  315. struct floppy_drive_params params;
  316. const char *name; /* name printed while booting */
  317. } default_drive_params[] = {
  318. /* NOTE: the time values in jiffies should be in msec!
  319. CMOS drive type
  320. | Maximum data rate supported by drive type
  321. | | Head load time, msec
  322. | | | Head unload time, msec (not used)
  323. | | | | Step rate interval, usec
  324. | | | | | Time needed for spinup time (jiffies)
  325. | | | | | | Timeout for spinning down (jiffies)
  326. | | | | | | | Spindown offset (where disk stops)
  327. | | | | | | | | Select delay
  328. | | | | | | | | | RPS
  329. | | | | | | | | | | Max number of tracks
  330. | | | | | | | | | | | Interrupt timeout
  331. | | | | | | | | | | | | Max nonintlv. sectors
  332. | | | | | | | | | | | | | -Max Errors- flags */
  333. {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
  334. 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
  335. {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
  336. 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
  337. {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
  338. 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
  339. {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
  340. 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
  341. {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
  342. 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
  343. {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
  344. 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
  345. {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
  346. 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
  347. /* | --autodetected formats--- | | |
  348. * read_track | | Name printed when booting
  349. * | Native format
  350. * Frequency of disk change checks */
  351. };
  352. static struct floppy_drive_params drive_params[N_DRIVE];
  353. static struct floppy_drive_struct drive_state[N_DRIVE];
  354. static struct floppy_write_errors write_errors[N_DRIVE];
  355. static struct timer_list motor_off_timer[N_DRIVE];
  356. static struct gendisk *disks[N_DRIVE];
  357. static struct block_device *opened_bdev[N_DRIVE];
  358. static DEFINE_MUTEX(open_lock);
  359. static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
  360. /*
  361. * This struct defines the different floppy types.
  362. *
  363. * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
  364. * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
  365. * tells if the disk is in Commodore 1581 format, which means side 0 sectors
  366. * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
  367. * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
  368. * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
  369. * side 0 is on physical side 0 (but with the misnamed sector IDs).
  370. * 'stretch' should probably be renamed to something more general, like
  371. * 'options'. Other parameters should be self-explanatory (see also
  372. * setfdprm(8)).
  373. */
  374. /*
  375. Size
  376. | Sectors per track
  377. | | Head
  378. | | | Tracks
  379. | | | | Stretch
  380. | | | | | Gap 1 size
  381. | | | | | | Data rate, | 0x40 for perp
  382. | | | | | | | Spec1 (stepping rate, head unload
  383. | | | | | | | | /fmt gap (gap2) */
  384. static struct floppy_struct floppy_type[32] = {
  385. { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
  386. { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
  387. { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
  388. { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
  389. { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
  390. { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
  391. { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
  392. { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
  393. { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
  394. { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
  395. { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
  396. { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
  397. { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
  398. { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
  399. { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
  400. { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
  401. { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
  402. { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
  403. { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
  404. { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
  405. { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
  406. { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
  407. { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
  408. { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
  409. { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
  410. { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
  411. { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
  412. { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
  413. { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
  414. { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
  415. { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
  416. { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
  417. };
  418. #define SECTSIZE (_FD_SECTSIZE(*floppy))
  419. /* Auto-detection: Disk type used until the next media change occurs. */
  420. static struct floppy_struct *current_type[N_DRIVE];
  421. /*
  422. * User-provided type information. current_type points to
  423. * the respective entry of this array.
  424. */
  425. static struct floppy_struct user_params[N_DRIVE];
  426. static sector_t floppy_sizes[256];
  427. static char floppy_device_name[] = "floppy";
  428. /*
  429. * The driver is trying to determine the correct media format
  430. * while probing is set. rw_interrupt() clears it after a
  431. * successful access.
  432. */
  433. static int probing;
  434. /* Synchronization of FDC access. */
  435. #define FD_COMMAND_NONE -1
  436. #define FD_COMMAND_ERROR 2
  437. #define FD_COMMAND_OKAY 3
  438. static volatile int command_status = FD_COMMAND_NONE;
  439. static unsigned long fdc_busy;
  440. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  441. static DECLARE_WAIT_QUEUE_HEAD(command_done);
  442. #define NO_SIGNAL (!interruptible || !signal_pending(current))
  443. #define CALL(x) if ((x) == -EINTR) return -EINTR
  444. #define ECALL(x) if ((ret = (x))) return ret;
  445. #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
  446. #define WAIT(x) _WAIT((x),interruptible)
  447. #define IWAIT(x) _WAIT((x),1)
  448. /* Errors during formatting are counted here. */
  449. static int format_errors;
  450. /* Format request descriptor. */
  451. static struct format_descr format_req;
  452. /*
  453. * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
  454. * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
  455. * H is head unload time (1=16ms, 2=32ms, etc)
  456. */
  457. /*
  458. * Track buffer
  459. * Because these are written to by the DMA controller, they must
  460. * not contain a 64k byte boundary crossing, or data will be
  461. * corrupted/lost.
  462. */
  463. static char *floppy_track_buffer;
  464. static int max_buffer_sectors;
  465. static int *errors;
  466. typedef void (*done_f) (int);
  467. static struct cont_t {
  468. void (*interrupt) (void); /* this is called after the interrupt of the
  469. * main command */
  470. void (*redo) (void); /* this is called to retry the operation */
  471. void (*error) (void); /* this is called to tally an error */
  472. done_f done; /* this is called to say if the operation has
  473. * succeeded/failed */
  474. } *cont;
  475. static void floppy_ready(void);
  476. static void floppy_start(void);
  477. static void process_fd_request(void);
  478. static void recalibrate_floppy(void);
  479. static void floppy_shutdown(unsigned long);
  480. static int floppy_grab_irq_and_dma(void);
  481. static void floppy_release_irq_and_dma(void);
  482. /*
  483. * The "reset" variable should be tested whenever an interrupt is scheduled,
  484. * after the commands have been sent. This is to ensure that the driver doesn't
  485. * get wedged when the interrupt doesn't come because of a failed command.
  486. * reset doesn't need to be tested before sending commands, because
  487. * output_byte is automatically disabled when reset is set.
  488. */
  489. #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
  490. static void reset_fdc(void);
  491. /*
  492. * These are global variables, as that's the easiest way to give
  493. * information to interrupts. They are the data used for the current
  494. * request.
  495. */
  496. #define NO_TRACK -1
  497. #define NEED_1_RECAL -2
  498. #define NEED_2_RECAL -3
  499. static int usage_count;
  500. /* buffer related variables */
  501. static int buffer_track = -1;
  502. static int buffer_drive = -1;
  503. static int buffer_min = -1;
  504. static int buffer_max = -1;
  505. /* fdc related variables, should end up in a struct */
  506. static struct floppy_fdc_state fdc_state[N_FDC];
  507. static int fdc; /* current fdc */
  508. static struct floppy_struct *_floppy = floppy_type;
  509. static unsigned char current_drive;
  510. static long current_count_sectors;
  511. static unsigned char fsector_t; /* sector in track */
  512. static unsigned char in_sector_offset; /* offset within physical sector,
  513. * expressed in units of 512 bytes */
  514. #ifndef fd_eject
  515. static inline int fd_eject(int drive)
  516. {
  517. return -EINVAL;
  518. }
  519. #endif
  520. /*
  521. * Debugging
  522. * =========
  523. */
  524. #ifdef DEBUGT
  525. static long unsigned debugtimer;
  526. static inline void set_debugt(void)
  527. {
  528. debugtimer = jiffies;
  529. }
  530. static inline void debugt(const char *message)
  531. {
  532. if (DP->flags & DEBUGT)
  533. printk("%s dtime=%lu\n", message, jiffies - debugtimer);
  534. }
  535. #else
  536. static inline void set_debugt(void) { }
  537. static inline void debugt(const char *message) { }
  538. #endif /* DEBUGT */
  539. typedef void (*timeout_fn) (unsigned long);
  540. static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
  541. static const char *timeout_message;
  542. #ifdef FLOPPY_SANITY_CHECK
  543. static void is_alive(const char *message)
  544. {
  545. /* this routine checks whether the floppy driver is "alive" */
  546. if (test_bit(0, &fdc_busy) && command_status < 2
  547. && !timer_pending(&fd_timeout)) {
  548. DPRINT("timeout handler died: %s\n", message);
  549. }
  550. }
  551. #endif
  552. static void (*do_floppy) (void) = NULL;
  553. #ifdef FLOPPY_SANITY_CHECK
  554. #define OLOGSIZE 20
  555. static void (*lasthandler) (void);
  556. static unsigned long interruptjiffies;
  557. static unsigned long resultjiffies;
  558. static int resultsize;
  559. static unsigned long lastredo;
  560. static struct output_log {
  561. unsigned char data;
  562. unsigned char status;
  563. unsigned long jiffies;
  564. } output_log[OLOGSIZE];
  565. static int output_log_pos;
  566. #endif
  567. #define current_reqD -1
  568. #define MAXTIMEOUT -2
  569. static void __reschedule_timeout(int drive, const char *message, int marg)
  570. {
  571. if (drive == current_reqD)
  572. drive = current_drive;
  573. del_timer(&fd_timeout);
  574. if (drive < 0 || drive > N_DRIVE) {
  575. fd_timeout.expires = jiffies + 20UL * HZ;
  576. drive = 0;
  577. } else
  578. fd_timeout.expires = jiffies + UDP->timeout;
  579. add_timer(&fd_timeout);
  580. if (UDP->flags & FD_DEBUG) {
  581. DPRINT("reschedule timeout ");
  582. printk(message, marg);
  583. printk("\n");
  584. }
  585. timeout_message = message;
  586. }
  587. static void reschedule_timeout(int drive, const char *message, int marg)
  588. {
  589. unsigned long flags;
  590. spin_lock_irqsave(&floppy_lock, flags);
  591. __reschedule_timeout(drive, message, marg);
  592. spin_unlock_irqrestore(&floppy_lock, flags);
  593. }
  594. #define INFBOUND(a,b) (a)=max_t(int, a, b)
  595. #define SUPBOUND(a,b) (a)=min_t(int, a, b)
  596. /*
  597. * Bottom half floppy driver.
  598. * ==========================
  599. *
  600. * This part of the file contains the code talking directly to the hardware,
  601. * and also the main service loop (seek-configure-spinup-command)
  602. */
  603. /*
  604. * disk change.
  605. * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
  606. * and the last_checked date.
  607. *
  608. * last_checked is the date of the last check which showed 'no disk change'
  609. * FD_DISK_CHANGE is set under two conditions:
  610. * 1. The floppy has been changed after some i/o to that floppy already
  611. * took place.
  612. * 2. No floppy disk is in the drive. This is done in order to ensure that
  613. * requests are quickly flushed in case there is no disk in the drive. It
  614. * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
  615. * the drive.
  616. *
  617. * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
  618. * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
  619. * each seek. If a disk is present, the disk change line should also be
  620. * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
  621. * change line is set, this means either that no disk is in the drive, or
  622. * that it has been removed since the last seek.
  623. *
  624. * This means that we really have a third possibility too:
  625. * The floppy has been changed after the last seek.
  626. */
  627. static int disk_change(int drive)
  628. {
  629. int fdc = FDC(drive);
  630. #ifdef FLOPPY_SANITY_CHECK
  631. if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
  632. DPRINT("WARNING disk change called early\n");
  633. if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
  634. (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
  635. DPRINT("probing disk change on unselected drive\n");
  636. DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
  637. (unsigned int)FDCS->dor);
  638. }
  639. #endif
  640. #ifdef DCL_DEBUG
  641. if (UDP->flags & FD_DEBUG) {
  642. DPRINT("checking disk change line for drive %d\n", drive);
  643. DPRINT("jiffies=%lu\n", jiffies);
  644. DPRINT("disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
  645. DPRINT("flags=%lx\n", UDRS->flags);
  646. }
  647. #endif
  648. if (UDP->flags & FD_BROKEN_DCL)
  649. return UTESTF(FD_DISK_CHANGED);
  650. if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
  651. USETF(FD_VERIFY); /* verify write protection */
  652. if (UDRS->maxblock) {
  653. /* mark it changed */
  654. USETF(FD_DISK_CHANGED);
  655. }
  656. /* invalidate its geometry */
  657. if (UDRS->keep_data >= 0) {
  658. if ((UDP->flags & FTD_MSG) &&
  659. current_type[drive] != NULL)
  660. DPRINT("Disk type is undefined after "
  661. "disk change\n");
  662. current_type[drive] = NULL;
  663. floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
  664. }
  665. /*USETF(FD_DISK_NEWCHANGE); */
  666. return 1;
  667. } else {
  668. UDRS->last_checked = jiffies;
  669. UCLEARF(FD_DISK_NEWCHANGE);
  670. }
  671. return 0;
  672. }
  673. static inline int is_selected(int dor, int unit)
  674. {
  675. return ((dor & (0x10 << unit)) && (dor & 3) == unit);
  676. }
  677. static int set_dor(int fdc, char mask, char data)
  678. {
  679. register unsigned char drive, unit, newdor, olddor;
  680. if (FDCS->address == -1)
  681. return -1;
  682. olddor = FDCS->dor;
  683. newdor = (olddor & mask) | data;
  684. if (newdor != olddor) {
  685. unit = olddor & 0x3;
  686. if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
  687. drive = REVDRIVE(fdc, unit);
  688. #ifdef DCL_DEBUG
  689. if (UDP->flags & FD_DEBUG) {
  690. DPRINT("calling disk change from set_dor\n");
  691. }
  692. #endif
  693. disk_change(drive);
  694. }
  695. FDCS->dor = newdor;
  696. fd_outb(newdor, FD_DOR);
  697. unit = newdor & 0x3;
  698. if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
  699. drive = REVDRIVE(fdc, unit);
  700. UDRS->select_date = jiffies;
  701. }
  702. }
  703. /*
  704. * We should propagate failures to grab the resources back
  705. * nicely from here. Actually we ought to rewrite the fd
  706. * driver some day too.
  707. */
  708. if (newdor & FLOPPY_MOTOR_MASK)
  709. floppy_grab_irq_and_dma();
  710. if (olddor & FLOPPY_MOTOR_MASK)
  711. floppy_release_irq_and_dma();
  712. return olddor;
  713. }
  714. static void twaddle(void)
  715. {
  716. if (DP->select_delay)
  717. return;
  718. fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
  719. fd_outb(FDCS->dor, FD_DOR);
  720. DRS->select_date = jiffies;
  721. }
  722. /* reset all driver information about the current fdc. This is needed after
  723. * a reset, and after a raw command. */
  724. static void reset_fdc_info(int mode)
  725. {
  726. int drive;
  727. FDCS->spec1 = FDCS->spec2 = -1;
  728. FDCS->need_configure = 1;
  729. FDCS->perp_mode = 1;
  730. FDCS->rawcmd = 0;
  731. for (drive = 0; drive < N_DRIVE; drive++)
  732. if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
  733. UDRS->track = NEED_2_RECAL;
  734. }
  735. /* selects the fdc and drive, and enables the fdc's input/dma. */
  736. static void set_fdc(int drive)
  737. {
  738. if (drive >= 0 && drive < N_DRIVE) {
  739. fdc = FDC(drive);
  740. current_drive = drive;
  741. }
  742. if (fdc != 1 && fdc != 0) {
  743. printk("bad fdc value\n");
  744. return;
  745. }
  746. set_dor(fdc, ~0, 8);
  747. #if N_FDC > 1
  748. set_dor(1 - fdc, ~8, 0);
  749. #endif
  750. if (FDCS->rawcmd == 2)
  751. reset_fdc_info(1);
  752. if (fd_inb(FD_STATUS) != STATUS_READY)
  753. FDCS->reset = 1;
  754. }
  755. /* locks the driver */
  756. static int _lock_fdc(int drive, int interruptible, int line)
  757. {
  758. if (!usage_count) {
  759. printk(KERN_ERR
  760. "Trying to lock fdc while usage count=0 at line %d\n",
  761. line);
  762. return -1;
  763. }
  764. if (floppy_grab_irq_and_dma() == -1)
  765. return -EBUSY;
  766. if (test_and_set_bit(0, &fdc_busy)) {
  767. DECLARE_WAITQUEUE(wait, current);
  768. add_wait_queue(&fdc_wait, &wait);
  769. for (;;) {
  770. set_current_state(TASK_INTERRUPTIBLE);
  771. if (!test_and_set_bit(0, &fdc_busy))
  772. break;
  773. schedule();
  774. if (!NO_SIGNAL) {
  775. remove_wait_queue(&fdc_wait, &wait);
  776. return -EINTR;
  777. }
  778. }
  779. set_current_state(TASK_RUNNING);
  780. remove_wait_queue(&fdc_wait, &wait);
  781. }
  782. command_status = FD_COMMAND_NONE;
  783. __reschedule_timeout(drive, "lock fdc", 0);
  784. set_fdc(drive);
  785. return 0;
  786. }
  787. #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
  788. #define LOCK_FDC(drive,interruptible) \
  789. if (lock_fdc(drive,interruptible)) return -EINTR;
  790. /* unlocks the driver */
  791. static inline void unlock_fdc(void)
  792. {
  793. unsigned long flags;
  794. raw_cmd = NULL;
  795. if (!test_bit(0, &fdc_busy))
  796. DPRINT("FDC access conflict!\n");
  797. if (do_floppy)
  798. DPRINT("device interrupt still active at FDC release: %p!\n",
  799. do_floppy);
  800. command_status = FD_COMMAND_NONE;
  801. spin_lock_irqsave(&floppy_lock, flags);
  802. del_timer(&fd_timeout);
  803. cont = NULL;
  804. clear_bit(0, &fdc_busy);
  805. if (elv_next_request(floppy_queue))
  806. do_fd_request(floppy_queue);
  807. spin_unlock_irqrestore(&floppy_lock, flags);
  808. floppy_release_irq_and_dma();
  809. wake_up(&fdc_wait);
  810. }
  811. /* switches the motor off after a given timeout */
  812. static void motor_off_callback(unsigned long nr)
  813. {
  814. unsigned char mask = ~(0x10 << UNIT(nr));
  815. set_dor(FDC(nr), mask, 0);
  816. }
  817. /* schedules motor off */
  818. static void floppy_off(unsigned int drive)
  819. {
  820. unsigned long volatile delta;
  821. register int fdc = FDC(drive);
  822. if (!(FDCS->dor & (0x10 << UNIT(drive))))
  823. return;
  824. del_timer(motor_off_timer + drive);
  825. /* make spindle stop in a position which minimizes spinup time
  826. * next time */
  827. if (UDP->rps) {
  828. delta = jiffies - UDRS->first_read_date + HZ -
  829. UDP->spindown_offset;
  830. delta = ((delta * UDP->rps) % HZ) / UDP->rps;
  831. motor_off_timer[drive].expires =
  832. jiffies + UDP->spindown - delta;
  833. }
  834. add_timer(motor_off_timer + drive);
  835. }
  836. /*
  837. * cycle through all N_DRIVE floppy drives, for disk change testing.
  838. * stopping at current drive. This is done before any long operation, to
  839. * be sure to have up to date disk change information.
  840. */
  841. static void scandrives(void)
  842. {
  843. int i, drive, saved_drive;
  844. if (DP->select_delay)
  845. return;
  846. saved_drive = current_drive;
  847. for (i = 0; i < N_DRIVE; i++) {
  848. drive = (saved_drive + i + 1) % N_DRIVE;
  849. if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
  850. continue; /* skip closed drives */
  851. set_fdc(drive);
  852. if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
  853. (0x10 << UNIT(drive))))
  854. /* switch the motor off again, if it was off to
  855. * begin with */
  856. set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
  857. }
  858. set_fdc(saved_drive);
  859. }
  860. static void empty(void)
  861. {
  862. }
  863. static DECLARE_WORK(floppy_work, NULL, NULL);
  864. static void schedule_bh(void (*handler) (void))
  865. {
  866. PREPARE_WORK(&floppy_work, (void (*)(void *))handler, NULL);
  867. schedule_work(&floppy_work);
  868. }
  869. static DEFINE_TIMER(fd_timer, NULL, 0, 0);
  870. static void cancel_activity(void)
  871. {
  872. unsigned long flags;
  873. spin_lock_irqsave(&floppy_lock, flags);
  874. do_floppy = NULL;
  875. PREPARE_WORK(&floppy_work, (void *)empty, NULL);
  876. del_timer(&fd_timer);
  877. spin_unlock_irqrestore(&floppy_lock, flags);
  878. }
  879. /* this function makes sure that the disk stays in the drive during the
  880. * transfer */
  881. static void fd_watchdog(void)
  882. {
  883. #ifdef DCL_DEBUG
  884. if (DP->flags & FD_DEBUG) {
  885. DPRINT("calling disk change from watchdog\n");
  886. }
  887. #endif
  888. if (disk_change(current_drive)) {
  889. DPRINT("disk removed during i/o\n");
  890. cancel_activity();
  891. cont->done(0);
  892. reset_fdc();
  893. } else {
  894. del_timer(&fd_timer);
  895. fd_timer.function = (timeout_fn) fd_watchdog;
  896. fd_timer.expires = jiffies + HZ / 10;
  897. add_timer(&fd_timer);
  898. }
  899. }
  900. static void main_command_interrupt(void)
  901. {
  902. del_timer(&fd_timer);
  903. cont->interrupt();
  904. }
  905. /* waits for a delay (spinup or select) to pass */
  906. static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
  907. {
  908. if (FDCS->reset) {
  909. reset_fdc(); /* do the reset during sleep to win time
  910. * if we don't need to sleep, it's a good
  911. * occasion anyways */
  912. return 1;
  913. }
  914. if (time_before(jiffies, delay)) {
  915. del_timer(&fd_timer);
  916. fd_timer.function = function;
  917. fd_timer.expires = delay;
  918. add_timer(&fd_timer);
  919. return 1;
  920. }
  921. return 0;
  922. }
  923. static DEFINE_SPINLOCK(floppy_hlt_lock);
  924. static int hlt_disabled;
  925. static void floppy_disable_hlt(void)
  926. {
  927. unsigned long flags;
  928. spin_lock_irqsave(&floppy_hlt_lock, flags);
  929. if (!hlt_disabled) {
  930. hlt_disabled = 1;
  931. #ifdef HAVE_DISABLE_HLT
  932. disable_hlt();
  933. #endif
  934. }
  935. spin_unlock_irqrestore(&floppy_hlt_lock, flags);
  936. }
  937. static void floppy_enable_hlt(void)
  938. {
  939. unsigned long flags;
  940. spin_lock_irqsave(&floppy_hlt_lock, flags);
  941. if (hlt_disabled) {
  942. hlt_disabled = 0;
  943. #ifdef HAVE_DISABLE_HLT
  944. enable_hlt();
  945. #endif
  946. }
  947. spin_unlock_irqrestore(&floppy_hlt_lock, flags);
  948. }
  949. static void setup_DMA(void)
  950. {
  951. unsigned long f;
  952. #ifdef FLOPPY_SANITY_CHECK
  953. if (raw_cmd->length == 0) {
  954. int i;
  955. printk("zero dma transfer size:");
  956. for (i = 0; i < raw_cmd->cmd_count; i++)
  957. printk("%x,", raw_cmd->cmd[i]);
  958. printk("\n");
  959. cont->done(0);
  960. FDCS->reset = 1;
  961. return;
  962. }
  963. if (((unsigned long)raw_cmd->kernel_data) % 512) {
  964. printk("non aligned address: %p\n", raw_cmd->kernel_data);
  965. cont->done(0);
  966. FDCS->reset = 1;
  967. return;
  968. }
  969. #endif
  970. f = claim_dma_lock();
  971. fd_disable_dma();
  972. #ifdef fd_dma_setup
  973. if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
  974. (raw_cmd->flags & FD_RAW_READ) ?
  975. DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
  976. release_dma_lock(f);
  977. cont->done(0);
  978. FDCS->reset = 1;
  979. return;
  980. }
  981. release_dma_lock(f);
  982. #else
  983. fd_clear_dma_ff();
  984. fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
  985. fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
  986. DMA_MODE_READ : DMA_MODE_WRITE);
  987. fd_set_dma_addr(raw_cmd->kernel_data);
  988. fd_set_dma_count(raw_cmd->length);
  989. virtual_dma_port = FDCS->address;
  990. fd_enable_dma();
  991. release_dma_lock(f);
  992. #endif
  993. floppy_disable_hlt();
  994. }
  995. static void show_floppy(void);
  996. /* waits until the fdc becomes ready */
  997. static int wait_til_ready(void)
  998. {
  999. int counter, status;
  1000. if (FDCS->reset)
  1001. return -1;
  1002. for (counter = 0; counter < 10000; counter++) {
  1003. status = fd_inb(FD_STATUS);
  1004. if (status & STATUS_READY)
  1005. return status;
  1006. }
  1007. if (!initialising) {
  1008. DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
  1009. show_floppy();
  1010. }
  1011. FDCS->reset = 1;
  1012. return -1;
  1013. }
  1014. /* sends a command byte to the fdc */
  1015. static int output_byte(char byte)
  1016. {
  1017. int status;
  1018. if ((status = wait_til_ready()) < 0)
  1019. return -1;
  1020. if ((status & (STATUS_READY | STATUS_DIR | STATUS_DMA)) == STATUS_READY) {
  1021. fd_outb(byte, FD_DATA);
  1022. #ifdef FLOPPY_SANITY_CHECK
  1023. output_log[output_log_pos].data = byte;
  1024. output_log[output_log_pos].status = status;
  1025. output_log[output_log_pos].jiffies = jiffies;
  1026. output_log_pos = (output_log_pos + 1) % OLOGSIZE;
  1027. #endif
  1028. return 0;
  1029. }
  1030. FDCS->reset = 1;
  1031. if (!initialising) {
  1032. DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
  1033. byte, fdc, status);
  1034. show_floppy();
  1035. }
  1036. return -1;
  1037. }
  1038. #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
  1039. /* gets the response from the fdc */
  1040. static int result(void)
  1041. {
  1042. int i, status = 0;
  1043. for (i = 0; i < MAX_REPLIES; i++) {
  1044. if ((status = wait_til_ready()) < 0)
  1045. break;
  1046. status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
  1047. if ((status & ~STATUS_BUSY) == STATUS_READY) {
  1048. #ifdef FLOPPY_SANITY_CHECK
  1049. resultjiffies = jiffies;
  1050. resultsize = i;
  1051. #endif
  1052. return i;
  1053. }
  1054. if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
  1055. reply_buffer[i] = fd_inb(FD_DATA);
  1056. else
  1057. break;
  1058. }
  1059. if (!initialising) {
  1060. DPRINT
  1061. ("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
  1062. fdc, status, i);
  1063. show_floppy();
  1064. }
  1065. FDCS->reset = 1;
  1066. return -1;
  1067. }
  1068. #define MORE_OUTPUT -2
  1069. /* does the fdc need more output? */
  1070. static int need_more_output(void)
  1071. {
  1072. int status;
  1073. if ((status = wait_til_ready()) < 0)
  1074. return -1;
  1075. if ((status & (STATUS_READY | STATUS_DIR | STATUS_DMA)) == STATUS_READY)
  1076. return MORE_OUTPUT;
  1077. return result();
  1078. }
  1079. /* Set perpendicular mode as required, based on data rate, if supported.
  1080. * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
  1081. */
  1082. static inline void perpendicular_mode(void)
  1083. {
  1084. unsigned char perp_mode;
  1085. if (raw_cmd->rate & 0x40) {
  1086. switch (raw_cmd->rate & 3) {
  1087. case 0:
  1088. perp_mode = 2;
  1089. break;
  1090. case 3:
  1091. perp_mode = 3;
  1092. break;
  1093. default:
  1094. DPRINT("Invalid data rate for perpendicular mode!\n");
  1095. cont->done(0);
  1096. FDCS->reset = 1; /* convenient way to return to
  1097. * redo without to much hassle (deep
  1098. * stack et al. */
  1099. return;
  1100. }
  1101. } else
  1102. perp_mode = 0;
  1103. if (FDCS->perp_mode == perp_mode)
  1104. return;
  1105. if (FDCS->version >= FDC_82077_ORIG) {
  1106. output_byte(FD_PERPENDICULAR);
  1107. output_byte(perp_mode);
  1108. FDCS->perp_mode = perp_mode;
  1109. } else if (perp_mode) {
  1110. DPRINT("perpendicular mode not supported by this FDC.\n");
  1111. }
  1112. } /* perpendicular_mode */
  1113. static int fifo_depth = 0xa;
  1114. static int no_fifo;
  1115. static int fdc_configure(void)
  1116. {
  1117. /* Turn on FIFO */
  1118. output_byte(FD_CONFIGURE);
  1119. if (need_more_output() != MORE_OUTPUT)
  1120. return 0;
  1121. output_byte(0);
  1122. output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
  1123. output_byte(0); /* pre-compensation from track
  1124. 0 upwards */
  1125. return 1;
  1126. }
  1127. #define NOMINAL_DTR 500
  1128. /* Issue a "SPECIFY" command to set the step rate time, head unload time,
  1129. * head load time, and DMA disable flag to values needed by floppy.
  1130. *
  1131. * The value "dtr" is the data transfer rate in Kbps. It is needed
  1132. * to account for the data rate-based scaling done by the 82072 and 82077
  1133. * FDC types. This parameter is ignored for other types of FDCs (i.e.
  1134. * 8272a).
  1135. *
  1136. * Note that changing the data transfer rate has a (probably deleterious)
  1137. * effect on the parameters subject to scaling for 82072/82077 FDCs, so
  1138. * fdc_specify is called again after each data transfer rate
  1139. * change.
  1140. *
  1141. * srt: 1000 to 16000 in microseconds
  1142. * hut: 16 to 240 milliseconds
  1143. * hlt: 2 to 254 milliseconds
  1144. *
  1145. * These values are rounded up to the next highest available delay time.
  1146. */
  1147. static void fdc_specify(void)
  1148. {
  1149. unsigned char spec1, spec2;
  1150. unsigned long srt, hlt, hut;
  1151. unsigned long dtr = NOMINAL_DTR;
  1152. unsigned long scale_dtr = NOMINAL_DTR;
  1153. int hlt_max_code = 0x7f;
  1154. int hut_max_code = 0xf;
  1155. if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
  1156. fdc_configure();
  1157. FDCS->need_configure = 0;
  1158. /*DPRINT("FIFO enabled\n"); */
  1159. }
  1160. switch (raw_cmd->rate & 0x03) {
  1161. case 3:
  1162. dtr = 1000;
  1163. break;
  1164. case 1:
  1165. dtr = 300;
  1166. if (FDCS->version >= FDC_82078) {
  1167. /* chose the default rate table, not the one
  1168. * where 1 = 2 Mbps */
  1169. output_byte(FD_DRIVESPEC);
  1170. if (need_more_output() == MORE_OUTPUT) {
  1171. output_byte(UNIT(current_drive));
  1172. output_byte(0xc0);
  1173. }
  1174. }
  1175. break;
  1176. case 2:
  1177. dtr = 250;
  1178. break;
  1179. }
  1180. if (FDCS->version >= FDC_82072) {
  1181. scale_dtr = dtr;
  1182. hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
  1183. hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
  1184. }
  1185. /* Convert step rate from microseconds to milliseconds and 4 bits */
  1186. srt = 16 - (DP->srt * scale_dtr / 1000 + NOMINAL_DTR - 1) / NOMINAL_DTR;
  1187. if (slow_floppy) {
  1188. srt = srt / 4;
  1189. }
  1190. SUPBOUND(srt, 0xf);
  1191. INFBOUND(srt, 0);
  1192. hlt = (DP->hlt * scale_dtr / 2 + NOMINAL_DTR - 1) / NOMINAL_DTR;
  1193. if (hlt < 0x01)
  1194. hlt = 0x01;
  1195. else if (hlt > 0x7f)
  1196. hlt = hlt_max_code;
  1197. hut = (DP->hut * scale_dtr / 16 + NOMINAL_DTR - 1) / NOMINAL_DTR;
  1198. if (hut < 0x1)
  1199. hut = 0x1;
  1200. else if (hut > 0xf)
  1201. hut = hut_max_code;
  1202. spec1 = (srt << 4) | hut;
  1203. spec2 = (hlt << 1) | (use_virtual_dma & 1);
  1204. /* If these parameters did not change, just return with success */
  1205. if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
  1206. /* Go ahead and set spec1 and spec2 */
  1207. output_byte(FD_SPECIFY);
  1208. output_byte(FDCS->spec1 = spec1);
  1209. output_byte(FDCS->spec2 = spec2);
  1210. }
  1211. } /* fdc_specify */
  1212. /* Set the FDC's data transfer rate on behalf of the specified drive.
  1213. * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
  1214. * of the specify command (i.e. using the fdc_specify function).
  1215. */
  1216. static int fdc_dtr(void)
  1217. {
  1218. /* If data rate not already set to desired value, set it. */
  1219. if ((raw_cmd->rate & 3) == FDCS->dtr)
  1220. return 0;
  1221. /* Set dtr */
  1222. fd_outb(raw_cmd->rate & 3, FD_DCR);
  1223. /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
  1224. * need a stabilization period of several milliseconds to be
  1225. * enforced after data rate changes before R/W operations.
  1226. * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
  1227. */
  1228. FDCS->dtr = raw_cmd->rate & 3;
  1229. return (fd_wait_for_completion(jiffies + 2UL * HZ / 100,
  1230. (timeout_fn) floppy_ready));
  1231. } /* fdc_dtr */
  1232. static void tell_sector(void)
  1233. {
  1234. printk(": track %d, head %d, sector %d, size %d",
  1235. R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
  1236. } /* tell_sector */
  1237. /*
  1238. * OK, this error interpreting routine is called after a
  1239. * DMA read/write has succeeded
  1240. * or failed, so we check the results, and copy any buffers.
  1241. * hhb: Added better error reporting.
  1242. * ak: Made this into a separate routine.
  1243. */
  1244. static int interpret_errors(void)
  1245. {
  1246. char bad;
  1247. if (inr != 7) {
  1248. DPRINT("-- FDC reply error");
  1249. FDCS->reset = 1;
  1250. return 1;
  1251. }
  1252. /* check IC to find cause of interrupt */
  1253. switch (ST0 & ST0_INTR) {
  1254. case 0x40: /* error occurred during command execution */
  1255. if (ST1 & ST1_EOC)
  1256. return 0; /* occurs with pseudo-DMA */
  1257. bad = 1;
  1258. if (ST1 & ST1_WP) {
  1259. DPRINT("Drive is write protected\n");
  1260. CLEARF(FD_DISK_WRITABLE);
  1261. cont->done(0);
  1262. bad = 2;
  1263. } else if (ST1 & ST1_ND) {
  1264. SETF(FD_NEED_TWADDLE);
  1265. } else if (ST1 & ST1_OR) {
  1266. if (DP->flags & FTD_MSG)
  1267. DPRINT("Over/Underrun - retrying\n");
  1268. bad = 0;
  1269. } else if (*errors >= DP->max_errors.reporting) {
  1270. DPRINT("");
  1271. if (ST0 & ST0_ECE) {
  1272. printk("Recalibrate failed!");
  1273. } else if (ST2 & ST2_CRC) {
  1274. printk("data CRC error");
  1275. tell_sector();
  1276. } else if (ST1 & ST1_CRC) {
  1277. printk("CRC error");
  1278. tell_sector();
  1279. } else if ((ST1 & (ST1_MAM | ST1_ND))
  1280. || (ST2 & ST2_MAM)) {
  1281. if (!probing) {
  1282. printk("sector not found");
  1283. tell_sector();
  1284. } else
  1285. printk("probe failed...");
  1286. } else if (ST2 & ST2_WC) { /* seek error */
  1287. printk("wrong cylinder");
  1288. } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
  1289. printk("bad cylinder");
  1290. } else {
  1291. printk
  1292. ("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
  1293. ST0, ST1, ST2);
  1294. tell_sector();
  1295. }
  1296. printk("\n");
  1297. }
  1298. if (ST2 & ST2_WC || ST2 & ST2_BC)
  1299. /* wrong cylinder => recal */
  1300. DRS->track = NEED_2_RECAL;
  1301. return bad;
  1302. case 0x80: /* invalid command given */
  1303. DPRINT("Invalid FDC command given!\n");
  1304. cont->done(0);
  1305. return 2;
  1306. case 0xc0:
  1307. DPRINT("Abnormal termination caused by polling\n");
  1308. cont->error();
  1309. return 2;
  1310. default: /* (0) Normal command termination */
  1311. return 0;
  1312. }
  1313. }
  1314. /*
  1315. * This routine is called when everything should be correctly set up
  1316. * for the transfer (i.e. floppy motor is on, the correct floppy is
  1317. * selected, and the head is sitting on the right track).
  1318. */
  1319. static void setup_rw_floppy(void)
  1320. {
  1321. int i, r, flags, dflags;
  1322. unsigned long ready_date;
  1323. timeout_fn function;
  1324. flags = raw_cmd->flags;
  1325. if (flags & (FD_RAW_READ | FD_RAW_WRITE))
  1326. flags |= FD_RAW_INTR;
  1327. if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
  1328. ready_date = DRS->spinup_date + DP->spinup;
  1329. /* If spinup will take a long time, rerun scandrives
  1330. * again just before spinup completion. Beware that
  1331. * after scandrives, we must again wait for selection.
  1332. */
  1333. if (time_after(ready_date, jiffies + DP->select_delay)) {
  1334. ready_date -= DP->select_delay;
  1335. function = (timeout_fn) floppy_start;
  1336. } else
  1337. function = (timeout_fn) setup_rw_floppy;
  1338. /* wait until the floppy is spinning fast enough */
  1339. if (fd_wait_for_completion(ready_date, function))
  1340. return;
  1341. }
  1342. dflags = DRS->flags;
  1343. if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
  1344. setup_DMA();
  1345. if (flags & FD_RAW_INTR)
  1346. do_floppy = main_command_interrupt;
  1347. r = 0;
  1348. for (i = 0; i < raw_cmd->cmd_count; i++)
  1349. r |= output_byte(raw_cmd->cmd[i]);
  1350. debugt("rw_command: ");
  1351. if (r) {
  1352. cont->error();
  1353. reset_fdc();
  1354. return;
  1355. }
  1356. if (!(flags & FD_RAW_INTR)) {
  1357. inr = result();
  1358. cont->interrupt();
  1359. } else if (flags & FD_RAW_NEED_DISK)
  1360. fd_watchdog();
  1361. }
  1362. static int blind_seek;
  1363. /*
  1364. * This is the routine called after every seek (or recalibrate) interrupt
  1365. * from the floppy controller.
  1366. */
  1367. static void seek_interrupt(void)
  1368. {
  1369. debugt("seek interrupt:");
  1370. if (inr != 2 || (ST0 & 0xF8) != 0x20) {
  1371. DPRINT("seek failed\n");
  1372. DRS->track = NEED_2_RECAL;
  1373. cont->error();
  1374. cont->redo();
  1375. return;
  1376. }
  1377. if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
  1378. #ifdef DCL_DEBUG
  1379. if (DP->flags & FD_DEBUG) {
  1380. DPRINT
  1381. ("clearing NEWCHANGE flag because of effective seek\n");
  1382. DPRINT("jiffies=%lu\n", jiffies);
  1383. }
  1384. #endif
  1385. CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
  1386. DRS->select_date = jiffies;
  1387. }
  1388. DRS->track = ST1;
  1389. floppy_ready();
  1390. }
  1391. static void check_wp(void)
  1392. {
  1393. if (TESTF(FD_VERIFY)) {
  1394. /* check write protection */
  1395. output_byte(FD_GETSTATUS);
  1396. output_byte(UNIT(current_drive));
  1397. if (result() != 1) {
  1398. FDCS->reset = 1;
  1399. return;
  1400. }
  1401. CLEARF(FD_VERIFY);
  1402. CLEARF(FD_NEED_TWADDLE);
  1403. #ifdef DCL_DEBUG
  1404. if (DP->flags & FD_DEBUG) {
  1405. DPRINT("checking whether disk is write protected\n");
  1406. DPRINT("wp=%x\n", ST3 & 0x40);
  1407. }
  1408. #endif
  1409. if (!(ST3 & 0x40))
  1410. SETF(FD_DISK_WRITABLE);
  1411. else
  1412. CLEARF(FD_DISK_WRITABLE);
  1413. }
  1414. }
  1415. static void seek_floppy(void)
  1416. {
  1417. int track;
  1418. blind_seek = 0;
  1419. #ifdef DCL_DEBUG
  1420. if (DP->flags & FD_DEBUG) {
  1421. DPRINT("calling disk change from seek\n");
  1422. }
  1423. #endif
  1424. if (!TESTF(FD_DISK_NEWCHANGE) &&
  1425. disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
  1426. /* the media changed flag should be cleared after the seek.
  1427. * If it isn't, this means that there is really no disk in
  1428. * the drive.
  1429. */
  1430. SETF(FD_DISK_CHANGED);
  1431. cont->done(0);
  1432. cont->redo();
  1433. return;
  1434. }
  1435. if (DRS->track <= NEED_1_RECAL) {
  1436. recalibrate_floppy();
  1437. return;
  1438. } else if (TESTF(FD_DISK_NEWCHANGE) &&
  1439. (raw_cmd->flags & FD_RAW_NEED_DISK) &&
  1440. (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
  1441. /* we seek to clear the media-changed condition. Does anybody
  1442. * know a more elegant way, which works on all drives? */
  1443. if (raw_cmd->track)
  1444. track = raw_cmd->track - 1;
  1445. else {
  1446. if (DP->flags & FD_SILENT_DCL_CLEAR) {
  1447. set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
  1448. blind_seek = 1;
  1449. raw_cmd->flags |= FD_RAW_NEED_SEEK;
  1450. }
  1451. track = 1;
  1452. }
  1453. } else {
  1454. check_wp();
  1455. if (raw_cmd->track != DRS->track &&
  1456. (raw_cmd->flags & FD_RAW_NEED_SEEK))
  1457. track = raw_cmd->track;
  1458. else {
  1459. setup_rw_floppy();
  1460. return;
  1461. }
  1462. }
  1463. do_floppy = seek_interrupt;
  1464. output_byte(FD_SEEK);
  1465. output_byte(UNIT(current_drive));
  1466. LAST_OUT(track);
  1467. debugt("seek command:");
  1468. }
  1469. static void recal_interrupt(void)
  1470. {
  1471. debugt("recal interrupt:");
  1472. if (inr != 2)
  1473. FDCS->reset = 1;
  1474. else if (ST0 & ST0_ECE) {
  1475. switch (DRS->track) {
  1476. case NEED_1_RECAL:
  1477. debugt("recal interrupt need 1 recal:");
  1478. /* after a second recalibrate, we still haven't
  1479. * reached track 0. Probably no drive. Raise an
  1480. * error, as failing immediately might upset
  1481. * computers possessed by the Devil :-) */
  1482. cont->error();
  1483. cont->redo();
  1484. return;
  1485. case NEED_2_RECAL:
  1486. debugt("recal interrupt need 2 recal:");
  1487. /* If we already did a recalibrate,
  1488. * and we are not at track 0, this
  1489. * means we have moved. (The only way
  1490. * not to move at recalibration is to
  1491. * be already at track 0.) Clear the
  1492. * new change flag */
  1493. #ifdef DCL_DEBUG
  1494. if (DP->flags & FD_DEBUG) {
  1495. DPRINT
  1496. ("clearing NEWCHANGE flag because of second recalibrate\n");
  1497. }
  1498. #endif
  1499. CLEARF(FD_DISK_NEWCHANGE);
  1500. DRS->select_date = jiffies;
  1501. /* fall through */
  1502. default:
  1503. debugt("recal interrupt default:");
  1504. /* Recalibrate moves the head by at
  1505. * most 80 steps. If after one
  1506. * recalibrate we don't have reached
  1507. * track 0, this might mean that we
  1508. * started beyond track 80. Try
  1509. * again. */
  1510. DRS->track = NEED_1_RECAL;
  1511. break;
  1512. }
  1513. } else
  1514. DRS->track = ST1;
  1515. floppy_ready();
  1516. }
  1517. static void print_result(char *message, int inr)
  1518. {
  1519. int i;
  1520. DPRINT("%s ", message);
  1521. if (inr >= 0)
  1522. for (i = 0; i < inr; i++)
  1523. printk("repl[%d]=%x ", i, reply_buffer[i]);
  1524. printk("\n");
  1525. }
  1526. /* interrupt handler. Note that this can be called externally on the Sparc */
  1527. irqreturn_t floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1528. {
  1529. void (*handler) (void) = do_floppy;
  1530. int do_print;
  1531. unsigned long f;
  1532. lasthandler = handler;
  1533. interruptjiffies = jiffies;
  1534. f = claim_dma_lock();
  1535. fd_disable_dma();
  1536. release_dma_lock(f);
  1537. floppy_enable_hlt();
  1538. do_floppy = NULL;
  1539. if (fdc >= N_FDC || FDCS->address == -1) {
  1540. /* we don't even know which FDC is the culprit */
  1541. printk("DOR0=%x\n", fdc_state[0].dor);
  1542. printk("floppy interrupt on bizarre fdc %d\n", fdc);
  1543. printk("handler=%p\n", handler);
  1544. is_alive("bizarre fdc");
  1545. return IRQ_NONE;
  1546. }
  1547. FDCS->reset = 0;
  1548. /* We have to clear the reset flag here, because apparently on boxes
  1549. * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
  1550. * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
  1551. * emission of the SENSEI's.
  1552. * It is OK to emit floppy commands because we are in an interrupt
  1553. * handler here, and thus we have to fear no interference of other
  1554. * activity.
  1555. */
  1556. do_print = !handler && print_unex && !initialising;
  1557. inr = result();
  1558. if (do_print)
  1559. print_result("unexpected interrupt", inr);
  1560. if (inr == 0) {
  1561. int max_sensei = 4;
  1562. do {
  1563. output_byte(FD_SENSEI);
  1564. inr = result();
  1565. if (do_print)
  1566. print_result("sensei", inr);
  1567. max_sensei--;
  1568. } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2
  1569. && max_sensei);
  1570. }
  1571. if (!handler) {
  1572. FDCS->reset = 1;
  1573. return IRQ_NONE;
  1574. }
  1575. schedule_bh(handler);
  1576. is_alive("normal interrupt end");
  1577. /* FIXME! Was it really for us? */
  1578. return IRQ_HANDLED;
  1579. }
  1580. static void recalibrate_floppy(void)
  1581. {
  1582. debugt("recalibrate floppy:");
  1583. do_floppy = recal_interrupt;
  1584. output_byte(FD_RECALIBRATE);
  1585. LAST_OUT(UNIT(current_drive));
  1586. }
  1587. /*
  1588. * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
  1589. */
  1590. static void reset_interrupt(void)
  1591. {
  1592. debugt("reset interrupt:");
  1593. result(); /* get the status ready for set_fdc */
  1594. if (FDCS->reset) {
  1595. printk("reset set in interrupt, calling %p\n", cont->error);
  1596. cont->error(); /* a reset just after a reset. BAD! */
  1597. }
  1598. cont->redo();
  1599. }
  1600. /*
  1601. * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
  1602. * or by setting the self clearing bit 7 of STATUS (newer FDCs)
  1603. */
  1604. static void reset_fdc(void)
  1605. {
  1606. unsigned long flags;
  1607. do_floppy = reset_interrupt;
  1608. FDCS->reset = 0;
  1609. reset_fdc_info(0);
  1610. /* Pseudo-DMA may intercept 'reset finished' interrupt. */
  1611. /* Irrelevant for systems with true DMA (i386). */
  1612. flags = claim_dma_lock();
  1613. fd_disable_dma();
  1614. release_dma_lock(flags);
  1615. if (FDCS->version >= FDC_82072A)
  1616. fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
  1617. else {
  1618. fd_outb(FDCS->dor & ~0x04, FD_DOR);
  1619. udelay(FD_RESET_DELAY);
  1620. fd_outb(FDCS->dor, FD_DOR);
  1621. }
  1622. }
  1623. static void show_floppy(void)
  1624. {
  1625. int i;
  1626. printk("\n");
  1627. printk("floppy driver state\n");
  1628. printk("-------------------\n");
  1629. printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
  1630. jiffies, interruptjiffies, jiffies - interruptjiffies,
  1631. lasthandler);
  1632. #ifdef FLOPPY_SANITY_CHECK
  1633. printk("timeout_message=%s\n", timeout_message);
  1634. printk("last output bytes:\n");
  1635. for (i = 0; i < OLOGSIZE; i++)
  1636. printk("%2x %2x %lu\n",
  1637. output_log[(i + output_log_pos) % OLOGSIZE].data,
  1638. output_log[(i + output_log_pos) % OLOGSIZE].status,
  1639. output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
  1640. printk("last result at %lu\n", resultjiffies);
  1641. printk("last redo_fd_request at %lu\n", lastredo);
  1642. for (i = 0; i < resultsize; i++) {
  1643. printk("%2x ", reply_buffer[i]);
  1644. }
  1645. printk("\n");
  1646. #endif
  1647. printk("status=%x\n", fd_inb(FD_STATUS));
  1648. printk("fdc_busy=%lu\n", fdc_busy);
  1649. if (do_floppy)
  1650. printk("do_floppy=%p\n", do_floppy);
  1651. if (floppy_work.pending)
  1652. printk("floppy_work.func=%p\n", floppy_work.func);
  1653. if (timer_pending(&fd_timer))
  1654. printk("fd_timer.function=%p\n", fd_timer.function);
  1655. if (timer_pending(&fd_timeout)) {
  1656. printk("timer_function=%p\n", fd_timeout.function);
  1657. printk("expires=%lu\n", fd_timeout.expires - jiffies);
  1658. printk("now=%lu\n", jiffies);
  1659. }
  1660. printk("cont=%p\n", cont);
  1661. printk("current_req=%p\n", current_req);
  1662. printk("command_status=%d\n", command_status);
  1663. printk("\n");
  1664. }
  1665. static void floppy_shutdown(unsigned long data)
  1666. {
  1667. unsigned long flags;
  1668. if (!initialising)
  1669. show_floppy();
  1670. cancel_activity();
  1671. floppy_enable_hlt();
  1672. flags = claim_dma_lock();
  1673. fd_disable_dma();
  1674. release_dma_lock(flags);
  1675. /* avoid dma going to a random drive after shutdown */
  1676. if (!initialising)
  1677. DPRINT("floppy timeout called\n");
  1678. FDCS->reset = 1;
  1679. if (cont) {
  1680. cont->done(0);
  1681. cont->redo(); /* this will recall reset when needed */
  1682. } else {
  1683. printk("no cont in shutdown!\n");
  1684. process_fd_request();
  1685. }
  1686. is_alive("floppy shutdown");
  1687. }
  1688. /*typedef void (*timeout_fn)(unsigned long);*/
  1689. /* start motor, check media-changed condition and write protection */
  1690. static int start_motor(void (*function) (void))
  1691. {
  1692. int mask, data;
  1693. mask = 0xfc;
  1694. data = UNIT(current_drive);
  1695. if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
  1696. if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
  1697. set_debugt();
  1698. /* no read since this drive is running */
  1699. DRS->first_read_date = 0;
  1700. /* note motor start time if motor is not yet running */
  1701. DRS->spinup_date = jiffies;
  1702. data |= (0x10 << UNIT(current_drive));
  1703. }
  1704. } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
  1705. mask &= ~(0x10 << UNIT(current_drive));
  1706. /* starts motor and selects floppy */
  1707. del_timer(motor_off_timer + current_drive);
  1708. set_dor(fdc, mask, data);
  1709. /* wait_for_completion also schedules reset if needed. */
  1710. return (fd_wait_for_completion(DRS->select_date + DP->select_delay,
  1711. (timeout_fn) function));
  1712. }
  1713. static void floppy_ready(void)
  1714. {
  1715. CHECK_RESET;
  1716. if (start_motor(floppy_ready))
  1717. return;
  1718. if (fdc_dtr())
  1719. return;
  1720. #ifdef DCL_DEBUG
  1721. if (DP->flags & FD_DEBUG) {
  1722. DPRINT("calling disk change from floppy_ready\n");
  1723. }
  1724. #endif
  1725. if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
  1726. disk_change(current_drive) && !DP->select_delay)
  1727. twaddle(); /* this clears the dcl on certain drive/controller
  1728. * combinations */
  1729. #ifdef fd_chose_dma_mode
  1730. if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
  1731. unsigned long flags = claim_dma_lock();
  1732. fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
  1733. release_dma_lock(flags);
  1734. }
  1735. #endif
  1736. if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
  1737. perpendicular_mode();
  1738. fdc_specify(); /* must be done here because of hut, hlt ... */
  1739. seek_floppy();
  1740. } else {
  1741. if ((raw_cmd->flags & FD_RAW_READ) ||
  1742. (raw_cmd->flags & FD_RAW_WRITE))
  1743. fdc_specify();
  1744. setup_rw_floppy();
  1745. }
  1746. }
  1747. static void floppy_start(void)
  1748. {
  1749. reschedule_timeout(current_reqD, "floppy start", 0);
  1750. scandrives();
  1751. #ifdef DCL_DEBUG
  1752. if (DP->flags & FD_DEBUG) {
  1753. DPRINT("setting NEWCHANGE in floppy_start\n");
  1754. }
  1755. #endif
  1756. SETF(FD_DISK_NEWCHANGE);
  1757. floppy_ready();
  1758. }
  1759. /*
  1760. * ========================================================================
  1761. * here ends the bottom half. Exported routines are:
  1762. * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
  1763. * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
  1764. * Initialization also uses output_byte, result, set_dor, floppy_interrupt
  1765. * and set_dor.
  1766. * ========================================================================
  1767. */
  1768. /*
  1769. * General purpose continuations.
  1770. * ==============================
  1771. */
  1772. static void do_wakeup(void)
  1773. {
  1774. reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
  1775. cont = NULL;
  1776. command_status += 2;
  1777. wake_up(&command_done);
  1778. }
  1779. static struct cont_t wakeup_cont = {
  1780. .interrupt = empty,
  1781. .redo = do_wakeup,
  1782. .error = empty,
  1783. .done = (done_f) empty
  1784. };
  1785. static struct cont_t intr_cont = {
  1786. .interrupt = empty,
  1787. .redo = process_fd_request,
  1788. .error = empty,
  1789. .done = (done_f) empty
  1790. };
  1791. static int wait_til_done(void (*handler) (void), int interruptible)
  1792. {
  1793. int ret;
  1794. schedule_bh(handler);
  1795. if (command_status < 2 && NO_SIGNAL) {
  1796. DECLARE_WAITQUEUE(wait, current);
  1797. add_wait_queue(&command_done, &wait);
  1798. for (;;) {
  1799. set_current_state(interruptible ?
  1800. TASK_INTERRUPTIBLE :
  1801. TASK_UNINTERRUPTIBLE);
  1802. if (command_status >= 2 || !NO_SIGNAL)
  1803. break;
  1804. is_alive("wait_til_done");
  1805. schedule();
  1806. }
  1807. set_current_state(TASK_RUNNING);
  1808. remove_wait_queue(&command_done, &wait);
  1809. }
  1810. if (command_status < 2) {
  1811. cancel_activity();
  1812. cont = &intr_cont;
  1813. reset_fdc();
  1814. return -EINTR;
  1815. }
  1816. if (FDCS->reset)
  1817. command_status = FD_COMMAND_ERROR;
  1818. if (command_status == FD_COMMAND_OKAY)
  1819. ret = 0;
  1820. else
  1821. ret = -EIO;
  1822. command_status = FD_COMMAND_NONE;
  1823. return ret;
  1824. }
  1825. static void generic_done(int result)
  1826. {
  1827. command_status = result;
  1828. cont = &wakeup_cont;
  1829. }
  1830. static void generic_success(void)
  1831. {
  1832. cont->done(1);
  1833. }
  1834. static void generic_failure(void)
  1835. {
  1836. cont->done(0);
  1837. }
  1838. static void success_and_wakeup(void)
  1839. {
  1840. generic_success();
  1841. cont->redo();
  1842. }
  1843. /*
  1844. * formatting and rw support.
  1845. * ==========================
  1846. */
  1847. static int next_valid_format(void)
  1848. {
  1849. int probed_format;
  1850. probed_format = DRS->probed_format;
  1851. while (1) {
  1852. if (probed_format >= 8 || !DP->autodetect[probed_format]) {
  1853. DRS->probed_format = 0;
  1854. return 1;
  1855. }
  1856. if (floppy_type[DP->autodetect[probed_format]].sect) {
  1857. DRS->probed_format = probed_format;
  1858. return 0;
  1859. }
  1860. probed_format++;
  1861. }
  1862. }
  1863. static void bad_flp_intr(void)
  1864. {
  1865. int err_count;
  1866. if (probing) {
  1867. DRS->probed_format++;
  1868. if (!next_valid_format())
  1869. return;
  1870. }
  1871. err_count = ++(*errors);
  1872. INFBOUND(DRWE->badness, err_count);
  1873. if (err_count > DP->max_errors.abort)
  1874. cont->done(0);
  1875. if (err_count > DP->max_errors.reset)
  1876. FDCS->reset = 1;
  1877. else if (err_count > DP->max_errors.recal)
  1878. DRS->track = NEED_2_RECAL;
  1879. }
  1880. static void set_floppy(int drive)
  1881. {
  1882. int type = ITYPE(UDRS->fd_device);
  1883. if (type)
  1884. _floppy = floppy_type + type;
  1885. else
  1886. _floppy = current_type[drive];
  1887. }
  1888. /*
  1889. * formatting support.
  1890. * ===================
  1891. */
  1892. static void format_interrupt(void)
  1893. {
  1894. switch (interpret_errors()) {
  1895. case 1:
  1896. cont->error();
  1897. case 2:
  1898. break;
  1899. case 0:
  1900. cont->done(1);
  1901. }
  1902. cont->redo();
  1903. }
  1904. #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
  1905. #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
  1906. #define CT(x) ((x) | 0xc0)
  1907. static void setup_format_params(int track)
  1908. {
  1909. struct fparm {
  1910. unsigned char track, head, sect, size;
  1911. } *here = (struct fparm *)floppy_track_buffer;
  1912. int il, n;
  1913. int count, head_shift, track_shift;
  1914. raw_cmd = &default_raw_cmd;
  1915. raw_cmd->track = track;
  1916. raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
  1917. FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
  1918. raw_cmd->rate = _floppy->rate & 0x43;
  1919. raw_cmd->cmd_count = NR_F;
  1920. COMMAND = FM_MODE(_floppy, FD_FORMAT);
  1921. DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
  1922. F_SIZECODE = FD_SIZECODE(_floppy);
  1923. F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
  1924. F_GAP = _floppy->fmt_gap;
  1925. F_FILL = FD_FILL_BYTE;
  1926. raw_cmd->kernel_data = floppy_track_buffer;
  1927. raw_cmd->length = 4 * F_SECT_PER_TRACK;
  1928. /* allow for about 30ms for data transport per track */
  1929. head_shift = (F_SECT_PER_TRACK + 5) / 6;
  1930. /* a ``cylinder'' is two tracks plus a little stepping time */
  1931. track_shift = 2 * head_shift + 3;
  1932. /* position of logical sector 1 on this track */
  1933. n = (track_shift * format_req.track + head_shift * format_req.head)
  1934. % F_SECT_PER_TRACK;
  1935. /* determine interleave */
  1936. il = 1;
  1937. if (_floppy->fmt_gap < 0x22)
  1938. il++;
  1939. /* initialize field */
  1940. for (count = 0; count < F_SECT_PER_TRACK; ++count) {
  1941. here[count].track = format_req.track;
  1942. here[count].head = format_req.head;
  1943. here[count].sect = 0;
  1944. here[count].size = F_SIZECODE;
  1945. }
  1946. /* place logical sectors */
  1947. for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
  1948. here[n].sect = count;
  1949. n = (n + il) % F_SECT_PER_TRACK;
  1950. if (here[n].sect) { /* sector busy, find next free sector */
  1951. ++n;
  1952. if (n >= F_SECT_PER_TRACK) {
  1953. n -= F_SECT_PER_TRACK;
  1954. while (here[n].sect)
  1955. ++n;
  1956. }
  1957. }
  1958. }
  1959. if (_floppy->stretch & FD_ZEROBASED) {
  1960. for (count = 0; count < F_SECT_PER_TRACK; count++)
  1961. here[count].sect--;
  1962. }
  1963. }
  1964. static void redo_format(void)
  1965. {
  1966. buffer_track = -1;
  1967. setup_format_params(format_req.track << STRETCH(_floppy));
  1968. floppy_start();
  1969. debugt("queue format request");
  1970. }
  1971. static struct cont_t format_cont = {
  1972. .interrupt = format_interrupt,
  1973. .redo = redo_format,
  1974. .error = bad_flp_intr,
  1975. .done = generic_done
  1976. };
  1977. static int do_format(int drive, struct format_descr *tmp_format_req)
  1978. {
  1979. int ret;
  1980. LOCK_FDC(drive, 1);
  1981. set_floppy(drive);
  1982. if (!_floppy ||
  1983. _floppy->track > DP->tracks ||
  1984. tmp_format_req->track >= _floppy->track ||
  1985. tmp_format_req->head >= _floppy->head ||
  1986. (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
  1987. !_floppy->fmt_gap) {
  1988. process_fd_request();
  1989. return -EINVAL;
  1990. }
  1991. format_req = *tmp_format_req;
  1992. format_errors = 0;
  1993. cont = &format_cont;
  1994. errors = &format_errors;
  1995. IWAIT(redo_format);
  1996. process_fd_request();
  1997. return ret;
  1998. }
  1999. /*
  2000. * Buffer read/write and support
  2001. * =============================
  2002. */
  2003. static void floppy_end_request(struct request *req, int uptodate)
  2004. {
  2005. unsigned int nr_sectors = current_count_sectors;
  2006. /* current_count_sectors can be zero if transfer failed */
  2007. if (!uptodate)
  2008. nr_sectors = req->current_nr_sectors;
  2009. if (end_that_request_first(req, uptodate, nr_sectors))
  2010. return;
  2011. add_disk_randomness(req->rq_disk);
  2012. floppy_off((long)req->rq_disk->private_data);
  2013. blkdev_dequeue_request(req);
  2014. end_that_request_last(req, uptodate);
  2015. /* We're done with the request */
  2016. current_req = NULL;
  2017. }
  2018. /* new request_done. Can handle physical sectors which are smaller than a
  2019. * logical buffer */
  2020. static void request_done(int uptodate)
  2021. {
  2022. struct request_queue *q = floppy_queue;
  2023. struct request *req = current_req;
  2024. unsigned long flags;
  2025. int block;
  2026. probing = 0;
  2027. reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
  2028. if (!req) {
  2029. printk("floppy.c: no request in request_done\n");
  2030. return;
  2031. }
  2032. if (uptodate) {
  2033. /* maintain values for invalidation on geometry
  2034. * change */
  2035. block = current_count_sectors + req->sector;
  2036. INFBOUND(DRS->maxblock, block);
  2037. if (block > _floppy->sect)
  2038. DRS->maxtrack = 1;
  2039. /* unlock chained buffers */
  2040. spin_lock_irqsave(q->queue_lock, flags);
  2041. floppy_end_request(req, 1);
  2042. spin_unlock_irqrestore(q->queue_lock, flags);
  2043. } else {
  2044. if (rq_data_dir(req) == WRITE) {
  2045. /* record write error information */
  2046. DRWE->write_errors++;
  2047. if (DRWE->write_errors == 1) {
  2048. DRWE->first_error_sector = req->sector;
  2049. DRWE->first_error_generation = DRS->generation;
  2050. }
  2051. DRWE->last_error_sector = req->sector;
  2052. DRWE->last_error_generation = DRS->generation;
  2053. }
  2054. spin_lock_irqsave(q->queue_lock, flags);
  2055. floppy_end_request(req, 0);
  2056. spin_unlock_irqrestore(q->queue_lock, flags);
  2057. }
  2058. }
  2059. /* Interrupt handler evaluating the result of the r/w operation */
  2060. static void rw_interrupt(void)
  2061. {
  2062. int nr_sectors, ssize, eoc, heads;
  2063. if (R_HEAD >= 2) {
  2064. /* some Toshiba floppy controllers occasionnally seem to
  2065. * return bogus interrupts after read/write operations, which
  2066. * can be recognized by a bad head number (>= 2) */
  2067. return;
  2068. }
  2069. if (!DRS->first_read_date)
  2070. DRS->first_read_date = jiffies;
  2071. nr_sectors = 0;
  2072. CODE2SIZE;
  2073. if (ST1 & ST1_EOC)
  2074. eoc = 1;
  2075. else
  2076. eoc = 0;
  2077. if (COMMAND & 0x80)
  2078. heads = 2;
  2079. else
  2080. heads = 1;
  2081. nr_sectors = (((R_TRACK - TRACK) * heads +
  2082. R_HEAD - HEAD) * SECT_PER_TRACK +
  2083. R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
  2084. #ifdef FLOPPY_SANITY_CHECK
  2085. if (nr_sectors / ssize >
  2086. (in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
  2087. DPRINT("long rw: %x instead of %lx\n",
  2088. nr_sectors, current_count_sectors);
  2089. printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
  2090. printk("rh=%d h=%d\n", R_HEAD, HEAD);
  2091. printk("rt=%d t=%d\n", R_TRACK, TRACK);
  2092. printk("heads=%d eoc=%d\n", heads, eoc);
  2093. printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
  2094. fsector_t, ssize);
  2095. printk("in_sector_offset=%d\n", in_sector_offset);
  2096. }
  2097. #endif
  2098. nr_sectors -= in_sector_offset;
  2099. INFBOUND(nr_sectors, 0);
  2100. SUPBOUND(current_count_sectors, nr_sectors);
  2101. switch (interpret_errors()) {
  2102. case 2:
  2103. cont->redo();
  2104. return;
  2105. case 1:
  2106. if (!current_count_sectors) {
  2107. cont->error();
  2108. cont->redo();
  2109. return;
  2110. }
  2111. break;
  2112. case 0:
  2113. if (!current_count_sectors) {
  2114. cont->redo();
  2115. return;
  2116. }
  2117. current_type[current_drive] = _floppy;
  2118. floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  2119. break;
  2120. }
  2121. if (probing) {
  2122. if (DP->flags & FTD_MSG)
  2123. DPRINT("Auto-detected floppy type %s in fd%d\n",
  2124. _floppy->name, current_drive);
  2125. current_type[current_drive] = _floppy;
  2126. floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  2127. probing = 0;
  2128. }
  2129. if (CT(COMMAND) != FD_READ ||
  2130. raw_cmd->kernel_data == current_req->buffer) {
  2131. /* transfer directly from buffer */
  2132. cont->done(1);
  2133. } else if (CT(COMMAND) == FD_READ) {
  2134. buffer_track = raw_cmd->track;
  2135. buffer_drive = current_drive;
  2136. INFBOUND(buffer_max, nr_sectors + fsector_t);
  2137. }
  2138. cont->redo();
  2139. }
  2140. /* Compute maximal contiguous buffer size. */
  2141. static int buffer_chain_size(void)
  2142. {
  2143. struct bio *bio;
  2144. struct bio_vec *bv;
  2145. int size, i;
  2146. char *base;
  2147. base = bio_data(current_req->bio);
  2148. size = 0;
  2149. rq_for_each_bio(bio, current_req) {
  2150. bio_for_each_segment(bv, bio, i) {
  2151. if (page_address(bv->bv_page) + bv->bv_offset !=
  2152. base + size)
  2153. break;
  2154. size += bv->bv_len;
  2155. }
  2156. }
  2157. return size >> 9;
  2158. }
  2159. /* Compute the maximal transfer size */
  2160. static int transfer_size(int ssize, int max_sector, int max_size)
  2161. {
  2162. SUPBOUND(max_sector, fsector_t + max_size);
  2163. /* alignment */
  2164. max_sector -= (max_sector % _floppy->sect) % ssize;
  2165. /* transfer size, beginning not aligned */
  2166. current_count_sectors = max_sector - fsector_t;
  2167. return max_sector;
  2168. }
  2169. /*
  2170. * Move data from/to the track buffer to/from the buffer cache.
  2171. */
  2172. static void copy_buffer(int ssize, int max_sector, int max_sector_2)
  2173. {
  2174. int remaining; /* number of transferred 512-byte sectors */
  2175. struct bio_vec *bv;
  2176. struct bio *bio;
  2177. char *buffer, *dma_buffer;
  2178. int size, i;
  2179. max_sector = transfer_size(ssize,
  2180. min(max_sector, max_sector_2),
  2181. current_req->nr_sectors);
  2182. if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
  2183. buffer_max > fsector_t + current_req->nr_sectors)
  2184. current_count_sectors = min_t(int, buffer_max - fsector_t,
  2185. current_req->nr_sectors);
  2186. remaining = current_count_sectors << 9;
  2187. #ifdef FLOPPY_SANITY_CHECK
  2188. if ((remaining >> 9) > current_req->nr_sectors &&
  2189. CT(COMMAND) == FD_WRITE) {
  2190. DPRINT("in copy buffer\n");
  2191. printk("current_count_sectors=%ld\n", current_count_sectors);
  2192. printk("remaining=%d\n", remaining >> 9);
  2193. printk("current_req->nr_sectors=%ld\n",
  2194. current_req->nr_sectors);
  2195. printk("current_req->current_nr_sectors=%u\n",
  2196. current_req->current_nr_sectors);
  2197. printk("max_sector=%d\n", max_sector);
  2198. printk("ssize=%d\n", ssize);
  2199. }
  2200. #endif
  2201. buffer_max = max(max_sector, buffer_max);
  2202. dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
  2203. size = current_req->current_nr_sectors << 9;
  2204. rq_for_each_bio(bio, current_req) {
  2205. bio_for_each_segment(bv, bio, i) {
  2206. if (!remaining)
  2207. break;
  2208. size = bv->bv_len;
  2209. SUPBOUND(size, remaining);
  2210. buffer = page_address(bv->bv_page) + bv->bv_offset;
  2211. #ifdef FLOPPY_SANITY_CHECK
  2212. if (dma_buffer + size >
  2213. floppy_track_buffer + (max_buffer_sectors << 10) ||
  2214. dma_buffer < floppy_track_buffer) {
  2215. DPRINT("buffer overrun in copy buffer %d\n",
  2216. (int)((floppy_track_buffer -
  2217. dma_buffer) >> 9));
  2218. printk("fsector_t=%d buffer_min=%d\n",
  2219. fsector_t, buffer_min);
  2220. printk("current_count_sectors=%ld\n",
  2221. current_count_sectors);
  2222. if (CT(COMMAND) == FD_READ)
  2223. printk("read\n");
  2224. if (CT(COMMAND) == FD_WRITE)
  2225. printk("write\n");
  2226. break;
  2227. }
  2228. if (((unsigned long)buffer) % 512)
  2229. DPRINT("%p buffer not aligned\n", buffer);
  2230. #endif
  2231. if (CT(COMMAND) == FD_READ)
  2232. memcpy(buffer, dma_buffer, size);
  2233. else
  2234. memcpy(dma_buffer, buffer, size);
  2235. remaining -= size;
  2236. dma_buffer += size;
  2237. }
  2238. }
  2239. #ifdef FLOPPY_SANITY_CHECK
  2240. if (remaining) {
  2241. if (remaining > 0)
  2242. max_sector -= remaining >> 9;
  2243. DPRINT("weirdness: remaining %d\n", remaining >> 9);
  2244. }
  2245. #endif
  2246. }
  2247. #if 0
  2248. static inline int check_dma_crossing(char *start,
  2249. unsigned long length, char *message)
  2250. {
  2251. if (CROSS_64KB(start, length)) {
  2252. printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
  2253. message, start, start + length);
  2254. return 1;
  2255. } else
  2256. return 0;
  2257. }
  2258. #endif
  2259. /* work around a bug in pseudo DMA
  2260. * (on some FDCs) pseudo DMA does not stop when the CPU stops
  2261. * sending data. Hence we need a different way to signal the
  2262. * transfer length: We use SECT_PER_TRACK. Unfortunately, this
  2263. * does not work with MT, hence we can only transfer one head at
  2264. * a time
  2265. */
  2266. static void virtualdmabug_workaround(void)
  2267. {
  2268. int hard_sectors, end_sector;
  2269. if (CT(COMMAND) == FD_WRITE) {
  2270. COMMAND &= ~0x80; /* switch off multiple track mode */
  2271. hard_sectors = raw_cmd->length >> (7 + SIZECODE);
  2272. end_sector = SECTOR + hard_sectors - 1;
  2273. #ifdef FLOPPY_SANITY_CHECK
  2274. if (end_sector > SECT_PER_TRACK) {
  2275. printk("too many sectors %d > %d\n",
  2276. end_sector, SECT_PER_TRACK);
  2277. return;
  2278. }
  2279. #endif
  2280. SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
  2281. * to end of transfer */
  2282. }
  2283. }
  2284. /*
  2285. * Formulate a read/write request.
  2286. * this routine decides where to load the data (directly to buffer, or to
  2287. * tmp floppy area), how much data to load (the size of the buffer, the whole
  2288. * track, or a single sector)
  2289. * All floppy_track_buffer handling goes in here. If we ever add track buffer
  2290. * allocation on the fly, it should be done here. No other part should need
  2291. * modification.
  2292. */
  2293. static int make_raw_rw_request(void)
  2294. {
  2295. int aligned_sector_t;
  2296. int max_sector, max_size, tracksize, ssize;
  2297. if (max_buffer_sectors == 0) {
  2298. printk("VFS: Block I/O scheduled on unopened device\n");
  2299. return 0;
  2300. }
  2301. set_fdc((long)current_req->rq_disk->private_data);
  2302. raw_cmd = &default_raw_cmd;
  2303. raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
  2304. FD_RAW_NEED_SEEK;
  2305. raw_cmd->cmd_count = NR_RW;
  2306. if (rq_data_dir(current_req) == READ) {
  2307. raw_cmd->flags |= FD_RAW_READ;
  2308. COMMAND = FM_MODE(_floppy, FD_READ);
  2309. } else if (rq_data_dir(current_req) == WRITE) {
  2310. raw_cmd->flags |= FD_RAW_WRITE;
  2311. COMMAND = FM_MODE(_floppy, FD_WRITE);
  2312. } else {
  2313. DPRINT("make_raw_rw_request: unknown command\n");
  2314. return 0;
  2315. }
  2316. max_sector = _floppy->sect * _floppy->head;
  2317. TRACK = (int)current_req->sector / max_sector;
  2318. fsector_t = (int)current_req->sector % max_sector;
  2319. if (_floppy->track && TRACK >= _floppy->track) {
  2320. if (current_req->current_nr_sectors & 1) {
  2321. current_count_sectors = 1;
  2322. return 1;
  2323. } else
  2324. return 0;
  2325. }
  2326. HEAD = fsector_t / _floppy->sect;
  2327. if (((_floppy->stretch & (FD_SWAPSIDES | FD_ZEROBASED)) ||
  2328. TESTF(FD_NEED_TWADDLE)) && fsector_t < _floppy->sect)
  2329. max_sector = _floppy->sect;
  2330. /* 2M disks have phantom sectors on the first track */
  2331. if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
  2332. max_sector = 2 * _floppy->sect / 3;
  2333. if (fsector_t >= max_sector) {
  2334. current_count_sectors =
  2335. min_t(int, _floppy->sect - fsector_t,
  2336. current_req->nr_sectors);
  2337. return 1;
  2338. }
  2339. SIZECODE = 2;
  2340. } else
  2341. SIZECODE = FD_SIZECODE(_floppy);
  2342. raw_cmd->rate = _floppy->rate & 0x43;
  2343. if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
  2344. raw_cmd->rate = 1;
  2345. if (SIZECODE)
  2346. SIZECODE2 = 0xff;
  2347. else
  2348. SIZECODE2 = 0x80;
  2349. raw_cmd->track = TRACK << STRETCH(_floppy);
  2350. DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
  2351. GAP = _floppy->gap;
  2352. CODE2SIZE;
  2353. SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
  2354. SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
  2355. ((_floppy->stretch & FD_ZEROBASED) ? 0 : 1);
  2356. /* tracksize describes the size which can be filled up with sectors
  2357. * of size ssize.
  2358. */
  2359. tracksize = _floppy->sect - _floppy->sect % ssize;
  2360. if (tracksize < _floppy->sect) {
  2361. SECT_PER_TRACK++;
  2362. if (tracksize <= fsector_t % _floppy->sect)
  2363. SECTOR--;
  2364. /* if we are beyond tracksize, fill up using smaller sectors */
  2365. while (tracksize <= fsector_t % _floppy->sect) {
  2366. while (tracksize + ssize > _floppy->sect) {
  2367. SIZECODE--;
  2368. ssize >>= 1;
  2369. }
  2370. SECTOR++;
  2371. SECT_PER_TRACK++;
  2372. tracksize += ssize;
  2373. }
  2374. max_sector = HEAD * _floppy->sect + tracksize;
  2375. } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
  2376. max_sector = _floppy->sect;
  2377. } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
  2378. /* for virtual DMA bug workaround */
  2379. max_sector = _floppy->sect;
  2380. }
  2381. in_sector_offset = (fsector_t % _floppy->sect) % ssize;
  2382. aligned_sector_t = fsector_t - in_sector_offset;
  2383. max_size = current_req->nr_sectors;
  2384. if ((raw_cmd->track == buffer_track) &&
  2385. (current_drive == buffer_drive) &&
  2386. (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
  2387. /* data already in track buffer */
  2388. if (CT(COMMAND) == FD_READ) {
  2389. copy_buffer(1, max_sector, buffer_max);
  2390. return 1;
  2391. }
  2392. } else if (in_sector_offset || current_req->nr_sectors < ssize) {
  2393. if (CT(COMMAND) == FD_WRITE) {
  2394. if (fsector_t + current_req->nr_sectors > ssize &&
  2395. fsector_t + current_req->nr_sectors < ssize + ssize)
  2396. max_size = ssize + ssize;
  2397. else
  2398. max_size = ssize;
  2399. }
  2400. raw_cmd->flags &= ~FD_RAW_WRITE;
  2401. raw_cmd->flags |= FD_RAW_READ;
  2402. COMMAND = FM_MODE(_floppy, FD_READ);
  2403. } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
  2404. unsigned long dma_limit;
  2405. int direct, indirect;
  2406. indirect =
  2407. transfer_size(ssize, max_sector,
  2408. max_buffer_sectors * 2) - fsector_t;
  2409. /*
  2410. * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
  2411. * on a 64 bit machine!
  2412. */
  2413. max_size = buffer_chain_size();
  2414. dma_limit =
  2415. (MAX_DMA_ADDRESS -
  2416. ((unsigned long)current_req->buffer)) >> 9;
  2417. if ((unsigned long)max_size > dma_limit) {
  2418. max_size = dma_limit;
  2419. }
  2420. /* 64 kb boundaries */
  2421. if (CROSS_64KB(current_req->buffer, max_size << 9))
  2422. max_size = (K_64 -
  2423. ((unsigned long)current_req->buffer) %
  2424. K_64) >> 9;
  2425. direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
  2426. /*
  2427. * We try to read tracks, but if we get too many errors, we
  2428. * go back to reading just one sector at a time.
  2429. *
  2430. * This means we should be able to read a sector even if there
  2431. * are other bad sectors on this track.
  2432. */
  2433. if (!direct ||
  2434. (indirect * 2 > direct * 3 &&
  2435. *errors < DP->max_errors.read_track &&
  2436. /*!TESTF(FD_NEED_TWADDLE) && */
  2437. ((!probing
  2438. || (DP->read_track & (1 << DRS->probed_format)))))) {
  2439. max_size = current_req->nr_sectors;
  2440. } else {
  2441. raw_cmd->kernel_data = current_req->buffer;
  2442. raw_cmd->length = current_count_sectors << 9;
  2443. if (raw_cmd->length == 0) {
  2444. DPRINT
  2445. ("zero dma transfer attempted from make_raw_request\n");
  2446. DPRINT("indirect=%d direct=%d fsector_t=%d",
  2447. indirect, direct, fsector_t);
  2448. return 0;
  2449. }
  2450. /* check_dma_crossing(raw_cmd->kernel_data,
  2451. raw_cmd->length,
  2452. "end of make_raw_request [1]");*/
  2453. virtualdmabug_workaround();
  2454. return 2;
  2455. }
  2456. }
  2457. if (CT(COMMAND) == FD_READ)
  2458. max_size = max_sector; /* unbounded */
  2459. /* claim buffer track if needed */
  2460. if (buffer_track != raw_cmd->track || /* bad track */
  2461. buffer_drive != current_drive || /* bad drive */
  2462. fsector_t > buffer_max ||
  2463. fsector_t < buffer_min ||
  2464. ((CT(COMMAND) == FD_READ ||
  2465. (!in_sector_offset && current_req->nr_sectors >= ssize)) &&
  2466. max_sector > 2 * max_buffer_sectors + buffer_min &&
  2467. max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)
  2468. /* not enough space */
  2469. ) {
  2470. buffer_track = -1;
  2471. buffer_drive = current_drive;
  2472. buffer_max = buffer_min = aligned_sector_t;
  2473. }
  2474. raw_cmd->kernel_data = floppy_track_buffer +
  2475. ((aligned_sector_t - buffer_min) << 9);
  2476. if (CT(COMMAND) == FD_WRITE) {
  2477. /* copy write buffer to track buffer.
  2478. * if we get here, we know that the write
  2479. * is either aligned or the data already in the buffer
  2480. * (buffer will be overwritten) */
  2481. #ifdef FLOPPY_SANITY_CHECK
  2482. if (in_sector_offset && buffer_track == -1)
  2483. DPRINT("internal error offset !=0 on write\n");
  2484. #endif
  2485. buffer_track = raw_cmd->track;
  2486. buffer_drive = current_drive;
  2487. copy_buffer(ssize, max_sector,
  2488. 2 * max_buffer_sectors + buffer_min);
  2489. } else
  2490. transfer_size(ssize, max_sector,
  2491. 2 * max_buffer_sectors + buffer_min -
  2492. aligned_sector_t);
  2493. /* round up current_count_sectors to get dma xfer size */
  2494. raw_cmd->length = in_sector_offset + current_count_sectors;
  2495. raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
  2496. raw_cmd->length <<= 9;
  2497. #ifdef FLOPPY_SANITY_CHECK
  2498. /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
  2499. "end of make_raw_request"); */
  2500. if ((raw_cmd->length < current_count_sectors << 9) ||
  2501. (raw_cmd->kernel_data != current_req->buffer &&
  2502. CT(COMMAND) == FD_WRITE &&
  2503. (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
  2504. aligned_sector_t < buffer_min)) ||
  2505. raw_cmd->length % (128 << SIZECODE) ||
  2506. raw_cmd->length <= 0 || current_count_sectors <= 0) {
  2507. DPRINT("fractionary current count b=%lx s=%lx\n",
  2508. raw_cmd->length, current_count_sectors);
  2509. if (raw_cmd->kernel_data != current_req->buffer)
  2510. printk("addr=%d, length=%ld\n",
  2511. (int)((raw_cmd->kernel_data -
  2512. floppy_track_buffer) >> 9),
  2513. current_count_sectors);
  2514. printk("st=%d ast=%d mse=%d msi=%d\n",
  2515. fsector_t, aligned_sector_t, max_sector, max_size);
  2516. printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
  2517. printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
  2518. COMMAND, SECTOR, HEAD, TRACK);
  2519. printk("buffer drive=%d\n", buffer_drive);
  2520. printk("buffer track=%d\n", buffer_track);
  2521. printk("buffer_min=%d\n", buffer_min);
  2522. printk("buffer_max=%d\n", buffer_max);
  2523. return 0;
  2524. }
  2525. if (raw_cmd->kernel_data != current_req->buffer) {
  2526. if (raw_cmd->kernel_data < floppy_track_buffer ||
  2527. current_count_sectors < 0 ||
  2528. raw_cmd->length < 0 ||
  2529. raw_cmd->kernel_data + raw_cmd->length >
  2530. floppy_track_buffer + (max_buffer_sectors << 10)) {
  2531. DPRINT("buffer overrun in schedule dma\n");
  2532. printk("fsector_t=%d buffer_min=%d current_count=%ld\n",
  2533. fsector_t, buffer_min, raw_cmd->length >> 9);
  2534. printk("current_count_sectors=%ld\n",
  2535. current_count_sectors);
  2536. if (CT(COMMAND) == FD_READ)
  2537. printk("read\n");
  2538. if (CT(COMMAND) == FD_WRITE)
  2539. printk("write\n");
  2540. return 0;
  2541. }
  2542. } else if (raw_cmd->length > current_req->nr_sectors << 9 ||
  2543. current_count_sectors > current_req->nr_sectors) {
  2544. DPRINT("buffer overrun in direct transfer\n");
  2545. return 0;
  2546. } else if (raw_cmd->length < current_count_sectors << 9) {
  2547. DPRINT("more sectors than bytes\n");
  2548. printk("bytes=%ld\n", raw_cmd->length >> 9);
  2549. printk("sectors=%ld\n", current_count_sectors);
  2550. }
  2551. if (raw_cmd->length == 0) {
  2552. DPRINT("zero dma transfer attempted from make_raw_request\n");
  2553. return 0;
  2554. }
  2555. #endif
  2556. virtualdmabug_workaround();
  2557. return 2;
  2558. }
  2559. static void redo_fd_request(void)
  2560. {
  2561. #define REPEAT {request_done(0); continue; }
  2562. int drive;
  2563. int tmp;
  2564. lastredo = jiffies;
  2565. if (current_drive < N_DRIVE)
  2566. floppy_off(current_drive);
  2567. for (;;) {
  2568. if (!current_req) {
  2569. struct request *req;
  2570. spin_lock_irq(floppy_queue->queue_lock);
  2571. req = elv_next_request(floppy_queue);
  2572. spin_unlock_irq(floppy_queue->queue_lock);
  2573. if (!req) {
  2574. do_floppy = NULL;
  2575. unlock_fdc();
  2576. return;
  2577. }
  2578. current_req = req;
  2579. }
  2580. drive = (long)current_req->rq_disk->private_data;
  2581. set_fdc(drive);
  2582. reschedule_timeout(current_reqD, "redo fd request", 0);
  2583. set_floppy(drive);
  2584. raw_cmd = &default_raw_cmd;
  2585. raw_cmd->flags = 0;
  2586. if (start_motor(redo_fd_request))
  2587. return;
  2588. disk_change(current_drive);
  2589. if (test_bit(current_drive, &fake_change) ||
  2590. TESTF(FD_DISK_CHANGED)) {
  2591. DPRINT("disk absent or changed during operation\n");
  2592. REPEAT;
  2593. }
  2594. if (!_floppy) { /* Autodetection */
  2595. if (!probing) {
  2596. DRS->probed_format = 0;
  2597. if (next_valid_format()) {
  2598. DPRINT("no autodetectable formats\n");
  2599. _floppy = NULL;
  2600. REPEAT;
  2601. }
  2602. }
  2603. probing = 1;
  2604. _floppy =
  2605. floppy_type + DP->autodetect[DRS->probed_format];
  2606. } else
  2607. probing = 0;
  2608. errors = &(current_req->errors);
  2609. tmp = make_raw_rw_request();
  2610. if (tmp < 2) {
  2611. request_done(tmp);
  2612. continue;
  2613. }
  2614. if (TESTF(FD_NEED_TWADDLE))
  2615. twaddle();
  2616. schedule_bh(floppy_start);
  2617. debugt("queue fd request");
  2618. return;
  2619. }
  2620. #undef REPEAT
  2621. }
  2622. static struct cont_t rw_cont = {
  2623. .interrupt = rw_interrupt,
  2624. .redo = redo_fd_request,
  2625. .error = bad_flp_intr,
  2626. .done = request_done
  2627. };
  2628. static void process_fd_request(void)
  2629. {
  2630. cont = &rw_cont;
  2631. schedule_bh(redo_fd_request);
  2632. }
  2633. static void do_fd_request(request_queue_t * q)
  2634. {
  2635. if (max_buffer_sectors == 0) {
  2636. printk("VFS: do_fd_request called on non-open device\n");
  2637. return;
  2638. }
  2639. if (usage_count == 0) {
  2640. printk("warning: usage count=0, current_req=%p exiting\n",
  2641. current_req);
  2642. printk("sect=%ld flags=%lx\n", (long)current_req->sector,
  2643. current_req->flags);
  2644. return;
  2645. }
  2646. if (test_bit(0, &fdc_busy)) {
  2647. /* fdc busy, this new request will be treated when the
  2648. current one is done */
  2649. is_alive("do fd request, old request running");
  2650. return;
  2651. }
  2652. lock_fdc(MAXTIMEOUT, 0);
  2653. process_fd_request();
  2654. is_alive("do fd request");
  2655. }
  2656. static struct cont_t poll_cont = {
  2657. .interrupt = success_and_wakeup,
  2658. .redo = floppy_ready,
  2659. .error = generic_failure,
  2660. .done = generic_done
  2661. };
  2662. static int poll_drive(int interruptible, int flag)
  2663. {
  2664. int ret;
  2665. /* no auto-sense, just clear dcl */
  2666. raw_cmd = &default_raw_cmd;
  2667. raw_cmd->flags = flag;
  2668. raw_cmd->track = 0;
  2669. raw_cmd->cmd_count = 0;
  2670. cont = &poll_cont;
  2671. #ifdef DCL_DEBUG
  2672. if (DP->flags & FD_DEBUG) {
  2673. DPRINT("setting NEWCHANGE in poll_drive\n");
  2674. }
  2675. #endif
  2676. SETF(FD_DISK_NEWCHANGE);
  2677. WAIT(floppy_ready);
  2678. return ret;
  2679. }
  2680. /*
  2681. * User triggered reset
  2682. * ====================
  2683. */
  2684. static void reset_intr(void)
  2685. {
  2686. printk("weird, reset interrupt called\n");
  2687. }
  2688. static struct cont_t reset_cont = {
  2689. .interrupt = reset_intr,
  2690. .redo = success_and_wakeup,
  2691. .error = generic_failure,
  2692. .done = generic_done
  2693. };
  2694. static int user_reset_fdc(int drive, int arg, int interruptible)
  2695. {
  2696. int ret;
  2697. ret = 0;
  2698. LOCK_FDC(drive, interruptible);
  2699. if (arg == FD_RESET_ALWAYS)
  2700. FDCS->reset = 1;
  2701. if (FDCS->reset) {
  2702. cont = &reset_cont;
  2703. WAIT(reset_fdc);
  2704. }
  2705. process_fd_request();
  2706. return ret;
  2707. }
  2708. /*
  2709. * Misc Ioctl's and support
  2710. * ========================
  2711. */
  2712. static inline int fd_copyout(void __user *param, const void *address,
  2713. unsigned long size)
  2714. {
  2715. return copy_to_user(param, address, size) ? -EFAULT : 0;
  2716. }
  2717. static inline int fd_copyin(void __user *param, void *address, unsigned long size)
  2718. {
  2719. return copy_from_user(address, param, size) ? -EFAULT : 0;
  2720. }
  2721. #define _COPYOUT(x) (copy_to_user((void __user *)param, &(x), sizeof(x)) ? -EFAULT : 0)
  2722. #define _COPYIN(x) (copy_from_user(&(x), (void __user *)param, sizeof(x)) ? -EFAULT : 0)
  2723. #define COPYOUT(x) ECALL(_COPYOUT(x))
  2724. #define COPYIN(x) ECALL(_COPYIN(x))
  2725. static inline const char *drive_name(int type, int drive)
  2726. {
  2727. struct floppy_struct *floppy;
  2728. if (type)
  2729. floppy = floppy_type + type;
  2730. else {
  2731. if (UDP->native_format)
  2732. floppy = floppy_type + UDP->native_format;
  2733. else
  2734. return "(null)";
  2735. }
  2736. if (floppy->name)
  2737. return floppy->name;
  2738. else
  2739. return "(null)";
  2740. }
  2741. /* raw commands */
  2742. static void raw_cmd_done(int flag)
  2743. {
  2744. int i;
  2745. if (!flag) {
  2746. raw_cmd->flags |= FD_RAW_FAILURE;
  2747. raw_cmd->flags |= FD_RAW_HARDFAILURE;
  2748. } else {
  2749. raw_cmd->reply_count = inr;
  2750. if (raw_cmd->reply_count > MAX_REPLIES)
  2751. raw_cmd->reply_count = 0;
  2752. for (i = 0; i < raw_cmd->reply_count; i++)
  2753. raw_cmd->reply[i] = reply_buffer[i];
  2754. if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  2755. unsigned long flags;
  2756. flags = claim_dma_lock();
  2757. raw_cmd->length = fd_get_dma_residue();
  2758. release_dma_lock(flags);
  2759. }
  2760. if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
  2761. (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
  2762. raw_cmd->flags |= FD_RAW_FAILURE;
  2763. if (disk_change(current_drive))
  2764. raw_cmd->flags |= FD_RAW_DISK_CHANGE;
  2765. else
  2766. raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
  2767. if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
  2768. motor_off_callback(current_drive);
  2769. if (raw_cmd->next &&
  2770. (!(raw_cmd->flags & FD_RAW_FAILURE) ||
  2771. !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
  2772. ((raw_cmd->flags & FD_RAW_FAILURE) ||
  2773. !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
  2774. raw_cmd = raw_cmd->next;
  2775. return;
  2776. }
  2777. }
  2778. generic_done(flag);
  2779. }
  2780. static struct cont_t raw_cmd_cont = {
  2781. .interrupt = success_and_wakeup,
  2782. .redo = floppy_start,
  2783. .error = generic_failure,
  2784. .done = raw_cmd_done
  2785. };
  2786. static inline int raw_cmd_copyout(int cmd, char __user *param,
  2787. struct floppy_raw_cmd *ptr)
  2788. {
  2789. int ret;
  2790. while (ptr) {
  2791. COPYOUT(*ptr);
  2792. param += sizeof(struct floppy_raw_cmd);
  2793. if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
  2794. if (ptr->length >= 0
  2795. && ptr->length <= ptr->buffer_length)
  2796. ECALL(fd_copyout
  2797. (ptr->data, ptr->kernel_data,
  2798. ptr->buffer_length - ptr->length));
  2799. }
  2800. ptr = ptr->next;
  2801. }
  2802. return 0;
  2803. }
  2804. static void raw_cmd_free(struct floppy_raw_cmd **ptr)
  2805. {
  2806. struct floppy_raw_cmd *next, *this;
  2807. this = *ptr;
  2808. *ptr = NULL;
  2809. while (this) {
  2810. if (this->buffer_length) {
  2811. fd_dma_mem_free((unsigned long)this->kernel_data,
  2812. this->buffer_length);
  2813. this->buffer_length = 0;
  2814. }
  2815. next = this->next;
  2816. kfree(this);
  2817. this = next;
  2818. }
  2819. }
  2820. static inline int raw_cmd_copyin(int cmd, char __user *param,
  2821. struct floppy_raw_cmd **rcmd)
  2822. {
  2823. struct floppy_raw_cmd *ptr;
  2824. int ret;
  2825. int i;
  2826. *rcmd = NULL;
  2827. while (1) {
  2828. ptr = (struct floppy_raw_cmd *)
  2829. kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
  2830. if (!ptr)
  2831. return -ENOMEM;
  2832. *rcmd = ptr;
  2833. COPYIN(*ptr);
  2834. ptr->next = NULL;
  2835. ptr->buffer_length = 0;
  2836. param += sizeof(struct floppy_raw_cmd);
  2837. if (ptr->cmd_count > 33)
  2838. /* the command may now also take up the space
  2839. * initially intended for the reply & the
  2840. * reply count. Needed for long 82078 commands
  2841. * such as RESTORE, which takes ... 17 command
  2842. * bytes. Murphy's law #137: When you reserve
  2843. * 16 bytes for a structure, you'll one day
  2844. * discover that you really need 17...
  2845. */
  2846. return -EINVAL;
  2847. for (i = 0; i < 16; i++)
  2848. ptr->reply[i] = 0;
  2849. ptr->resultcode = 0;
  2850. ptr->kernel_data = NULL;
  2851. if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  2852. if (ptr->length <= 0)
  2853. return -EINVAL;
  2854. ptr->kernel_data =
  2855. (char *)fd_dma_mem_alloc(ptr->length);
  2856. fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
  2857. if (!ptr->kernel_data)
  2858. return -ENOMEM;
  2859. ptr->buffer_length = ptr->length;
  2860. }
  2861. if (ptr->flags & FD_RAW_WRITE)
  2862. ECALL(fd_copyin(ptr->data, ptr->kernel_data,
  2863. ptr->length));
  2864. rcmd = &(ptr->next);
  2865. if (!(ptr->flags & FD_RAW_MORE))
  2866. return 0;
  2867. ptr->rate &= 0x43;
  2868. }
  2869. }
  2870. static int raw_cmd_ioctl(int cmd, void __user *param)
  2871. {
  2872. int drive, ret, ret2;
  2873. struct floppy_raw_cmd *my_raw_cmd;
  2874. if (FDCS->rawcmd <= 1)
  2875. FDCS->rawcmd = 1;
  2876. for (drive = 0; drive < N_DRIVE; drive++) {
  2877. if (FDC(drive) != fdc)
  2878. continue;
  2879. if (drive == current_drive) {
  2880. if (UDRS->fd_ref > 1) {
  2881. FDCS->rawcmd = 2;
  2882. break;
  2883. }
  2884. } else if (UDRS->fd_ref) {
  2885. FDCS->rawcmd = 2;
  2886. break;
  2887. }
  2888. }
  2889. if (FDCS->reset)
  2890. return -EIO;
  2891. ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
  2892. if (ret) {
  2893. raw_cmd_free(&my_raw_cmd);
  2894. return ret;
  2895. }
  2896. raw_cmd = my_raw_cmd;
  2897. cont = &raw_cmd_cont;
  2898. ret = wait_til_done(floppy_start, 1);
  2899. #ifdef DCL_DEBUG
  2900. if (DP->flags & FD_DEBUG) {
  2901. DPRINT("calling disk change from raw_cmd ioctl\n");
  2902. }
  2903. #endif
  2904. if (ret != -EINTR && FDCS->reset)
  2905. ret = -EIO;
  2906. DRS->track = NO_TRACK;
  2907. ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
  2908. if (!ret)
  2909. ret = ret2;
  2910. raw_cmd_free(&my_raw_cmd);
  2911. return ret;
  2912. }
  2913. static int invalidate_drive(struct block_device *bdev)
  2914. {
  2915. /* invalidate the buffer track to force a reread */
  2916. set_bit((long)bdev->bd_disk->private_data, &fake_change);
  2917. process_fd_request();
  2918. check_disk_change(bdev);
  2919. return 0;
  2920. }
  2921. static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
  2922. int drive, int type, struct block_device *bdev)
  2923. {
  2924. int cnt;
  2925. /* sanity checking for parameters. */
  2926. if (g->sect <= 0 ||
  2927. g->head <= 0 ||
  2928. g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
  2929. /* check if reserved bits are set */
  2930. (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_ZEROBASED)) != 0)
  2931. return -EINVAL;
  2932. if (type) {
  2933. if (!capable(CAP_SYS_ADMIN))
  2934. return -EPERM;
  2935. mutex_lock(&open_lock);
  2936. LOCK_FDC(drive, 1);
  2937. floppy_type[type] = *g;
  2938. floppy_type[type].name = "user format";
  2939. for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
  2940. floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
  2941. floppy_type[type].size + 1;
  2942. process_fd_request();
  2943. for (cnt = 0; cnt < N_DRIVE; cnt++) {
  2944. struct block_device *bdev = opened_bdev[cnt];
  2945. if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
  2946. continue;
  2947. __invalidate_device(bdev);
  2948. }
  2949. mutex_unlock(&open_lock);
  2950. } else {
  2951. int oldStretch;
  2952. LOCK_FDC(drive, 1);
  2953. if (cmd != FDDEFPRM)
  2954. /* notice a disk change immediately, else
  2955. * we lose our settings immediately*/
  2956. CALL(poll_drive(1, FD_RAW_NEED_DISK));
  2957. oldStretch = g->stretch;
  2958. user_params[drive] = *g;
  2959. if (buffer_drive == drive)
  2960. SUPBOUND(buffer_max, user_params[drive].sect);
  2961. current_type[drive] = &user_params[drive];
  2962. floppy_sizes[drive] = user_params[drive].size;
  2963. if (cmd == FDDEFPRM)
  2964. DRS->keep_data = -1;
  2965. else
  2966. DRS->keep_data = 1;
  2967. /* invalidation. Invalidate only when needed, i.e.
  2968. * when there are already sectors in the buffer cache
  2969. * whose number will change. This is useful, because
  2970. * mtools often changes the geometry of the disk after
  2971. * looking at the boot block */
  2972. if (DRS->maxblock > user_params[drive].sect ||
  2973. DRS->maxtrack ||
  2974. ((user_params[drive].sect ^ oldStretch) &
  2975. (FD_SWAPSIDES | FD_ZEROBASED)))
  2976. invalidate_drive(bdev);
  2977. else
  2978. process_fd_request();
  2979. }
  2980. return 0;
  2981. }
  2982. /* handle obsolete ioctl's */
  2983. static int ioctl_table[] = {
  2984. FDCLRPRM,
  2985. FDSETPRM,
  2986. FDDEFPRM,
  2987. FDGETPRM,
  2988. FDMSGON,
  2989. FDMSGOFF,
  2990. FDFMTBEG,
  2991. FDFMTTRK,
  2992. FDFMTEND,
  2993. FDSETEMSGTRESH,
  2994. FDFLUSH,
  2995. FDSETMAXERRS,
  2996. FDGETMAXERRS,
  2997. FDGETDRVTYP,
  2998. FDSETDRVPRM,
  2999. FDGETDRVPRM,
  3000. FDGETDRVSTAT,
  3001. FDPOLLDRVSTAT,
  3002. FDRESET,
  3003. FDGETFDCSTAT,
  3004. FDWERRORCLR,
  3005. FDWERRORGET,
  3006. FDRAWCMD,
  3007. FDEJECT,
  3008. FDTWADDLE
  3009. };
  3010. static inline int normalize_ioctl(int *cmd, int *size)
  3011. {
  3012. int i;
  3013. for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
  3014. if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
  3015. *size = _IOC_SIZE(*cmd);
  3016. *cmd = ioctl_table[i];
  3017. if (*size > _IOC_SIZE(*cmd)) {
  3018. printk("ioctl not yet supported\n");
  3019. return -EFAULT;
  3020. }
  3021. return 0;
  3022. }
  3023. }
  3024. return -EINVAL;
  3025. }
  3026. static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
  3027. {
  3028. if (type)
  3029. *g = &floppy_type[type];
  3030. else {
  3031. LOCK_FDC(drive, 0);
  3032. CALL(poll_drive(0, 0));
  3033. process_fd_request();
  3034. *g = current_type[drive];
  3035. }
  3036. if (!*g)
  3037. return -ENODEV;
  3038. return 0;
  3039. }
  3040. static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  3041. {
  3042. int drive = (long)bdev->bd_disk->private_data;
  3043. int type = ITYPE(drive_state[drive].fd_device);
  3044. struct floppy_struct *g;
  3045. int ret;
  3046. ret = get_floppy_geometry(drive, type, &g);
  3047. if (ret)
  3048. return ret;
  3049. geo->heads = g->head;
  3050. geo->sectors = g->sect;
  3051. geo->cylinders = g->track;
  3052. return 0;
  3053. }
  3054. static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  3055. unsigned long param)
  3056. {
  3057. #define FD_IOCTL_ALLOWED ((filp) && (filp)->private_data)
  3058. #define OUT(c,x) case c: outparam = (const char *) (x); break
  3059. #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
  3060. int drive = (long)inode->i_bdev->bd_disk->private_data;
  3061. int i, type = ITYPE(UDRS->fd_device);
  3062. int ret;
  3063. int size;
  3064. union inparam {
  3065. struct floppy_struct g; /* geometry */
  3066. struct format_descr f;
  3067. struct floppy_max_errors max_errors;
  3068. struct floppy_drive_params dp;
  3069. } inparam; /* parameters coming from user space */
  3070. const char *outparam; /* parameters passed back to user space */
  3071. /* convert compatibility eject ioctls into floppy eject ioctl.
  3072. * We do this in order to provide a means to eject floppy disks before
  3073. * installing the new fdutils package */
  3074. if (cmd == CDROMEJECT || /* CD-ROM eject */
  3075. cmd == 0x6470 /* SunOS floppy eject */ ) {
  3076. DPRINT("obsolete eject ioctl\n");
  3077. DPRINT("please use floppycontrol --eject\n");
  3078. cmd = FDEJECT;
  3079. }
  3080. /* convert the old style command into a new style command */
  3081. if ((cmd & 0xff00) == 0x0200) {
  3082. ECALL(normalize_ioctl(&cmd, &size));
  3083. } else
  3084. return -EINVAL;
  3085. /* permission checks */
  3086. if (((cmd & 0x40) && !FD_IOCTL_ALLOWED) ||
  3087. ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
  3088. return -EPERM;
  3089. /* copyin */
  3090. CLEARSTRUCT(&inparam);
  3091. if (_IOC_DIR(cmd) & _IOC_WRITE)
  3092. ECALL(fd_copyin((void __user *)param, &inparam, size))
  3093. switch (cmd) {
  3094. case FDEJECT:
  3095. if (UDRS->fd_ref != 1)
  3096. /* somebody else has this drive open */
  3097. return -EBUSY;
  3098. LOCK_FDC(drive, 1);
  3099. /* do the actual eject. Fails on
  3100. * non-Sparc architectures */
  3101. ret = fd_eject(UNIT(drive));
  3102. USETF(FD_DISK_CHANGED);
  3103. USETF(FD_VERIFY);
  3104. process_fd_request();
  3105. return ret;
  3106. case FDCLRPRM:
  3107. LOCK_FDC(drive, 1);
  3108. current_type[drive] = NULL;
  3109. floppy_sizes[drive] = MAX_DISK_SIZE << 1;
  3110. UDRS->keep_data = 0;
  3111. return invalidate_drive(inode->i_bdev);
  3112. case FDSETPRM:
  3113. case FDDEFPRM:
  3114. return set_geometry(cmd, &inparam.g,
  3115. drive, type, inode->i_bdev);
  3116. case FDGETPRM:
  3117. ECALL(get_floppy_geometry(drive, type,
  3118. (struct floppy_struct **)
  3119. &outparam));
  3120. break;
  3121. case FDMSGON:
  3122. UDP->flags |= FTD_MSG;
  3123. return 0;
  3124. case FDMSGOFF:
  3125. UDP->flags &= ~FTD_MSG;
  3126. return 0;
  3127. case FDFMTBEG:
  3128. LOCK_FDC(drive, 1);
  3129. CALL(poll_drive(1, FD_RAW_NEED_DISK));
  3130. ret = UDRS->flags;
  3131. process_fd_request();
  3132. if (ret & FD_VERIFY)
  3133. return -ENODEV;
  3134. if (!(ret & FD_DISK_WRITABLE))
  3135. return -EROFS;
  3136. return 0;
  3137. case FDFMTTRK:
  3138. if (UDRS->fd_ref != 1)
  3139. return -EBUSY;
  3140. return do_format(drive, &inparam.f);
  3141. case FDFMTEND:
  3142. case FDFLUSH:
  3143. LOCK_FDC(drive, 1);
  3144. return invalidate_drive(inode->i_bdev);
  3145. case FDSETEMSGTRESH:
  3146. UDP->max_errors.reporting =
  3147. (unsigned short)(param & 0x0f);
  3148. return 0;
  3149. OUT(FDGETMAXERRS, &UDP->max_errors);
  3150. IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
  3151. case FDGETDRVTYP:
  3152. outparam = drive_name(type, drive);
  3153. SUPBOUND(size, strlen(outparam) + 1);
  3154. break;
  3155. IN(FDSETDRVPRM, UDP, dp);
  3156. OUT(FDGETDRVPRM, UDP);
  3157. case FDPOLLDRVSTAT:
  3158. LOCK_FDC(drive, 1);
  3159. CALL(poll_drive(1, FD_RAW_NEED_DISK));
  3160. process_fd_request();
  3161. /* fall through */
  3162. OUT(FDGETDRVSTAT, UDRS);
  3163. case FDRESET:
  3164. return user_reset_fdc(drive, (int)param, 1);
  3165. OUT(FDGETFDCSTAT, UFDCS);
  3166. case FDWERRORCLR:
  3167. CLEARSTRUCT(UDRWE);
  3168. return 0;
  3169. OUT(FDWERRORGET, UDRWE);
  3170. case FDRAWCMD:
  3171. if (type)
  3172. return -EINVAL;
  3173. LOCK_FDC(drive, 1);
  3174. set_floppy(drive);
  3175. CALL(i = raw_cmd_ioctl(cmd, (void __user *)param));
  3176. process_fd_request();
  3177. return i;
  3178. case FDTWADDLE:
  3179. LOCK_FDC(drive, 1);
  3180. twaddle();
  3181. process_fd_request();
  3182. return 0;
  3183. default:
  3184. return -EINVAL;
  3185. }
  3186. if (_IOC_DIR(cmd) & _IOC_READ)
  3187. return fd_copyout((void __user *)param, outparam, size);
  3188. else
  3189. return 0;
  3190. #undef OUT
  3191. #undef IN
  3192. }
  3193. static void __init config_types(void)
  3194. {
  3195. int first = 1;
  3196. int drive;
  3197. /* read drive info out of physical CMOS */
  3198. drive = 0;
  3199. if (!UDP->cmos)
  3200. UDP->cmos = FLOPPY0_TYPE;
  3201. drive = 1;
  3202. if (!UDP->cmos && FLOPPY1_TYPE)
  3203. UDP->cmos = FLOPPY1_TYPE;
  3204. /* XXX */
  3205. /* additional physical CMOS drive detection should go here */
  3206. for (drive = 0; drive < N_DRIVE; drive++) {
  3207. unsigned int type = UDP->cmos;
  3208. struct floppy_drive_params *params;
  3209. const char *name = NULL;
  3210. static char temparea[32];
  3211. if (type < ARRAY_SIZE(default_drive_params)) {
  3212. params = &default_drive_params[type].params;
  3213. if (type) {
  3214. name = default_drive_params[type].name;
  3215. allowed_drive_mask |= 1 << drive;
  3216. } else
  3217. allowed_drive_mask &= ~(1 << drive);
  3218. } else {
  3219. params = &default_drive_params[0].params;
  3220. sprintf(temparea, "unknown type %d (usb?)", type);
  3221. name = temparea;
  3222. }
  3223. if (name) {
  3224. const char *prepend = ",";
  3225. if (first) {
  3226. prepend = KERN_INFO "Floppy drive(s):";
  3227. first = 0;
  3228. }
  3229. printk("%s fd%d is %s", prepend, drive, name);
  3230. }
  3231. *UDP = *params;
  3232. }
  3233. if (!first)
  3234. printk("\n");
  3235. }
  3236. static int floppy_release(struct inode *inode, struct file *filp)
  3237. {
  3238. int drive = (long)inode->i_bdev->bd_disk->private_data;
  3239. mutex_lock(&open_lock);
  3240. if (UDRS->fd_ref < 0)
  3241. UDRS->fd_ref = 0;
  3242. else if (!UDRS->fd_ref--) {
  3243. DPRINT("floppy_release with fd_ref == 0");
  3244. UDRS->fd_ref = 0;
  3245. }
  3246. if (!UDRS->fd_ref)
  3247. opened_bdev[drive] = NULL;
  3248. floppy_release_irq_and_dma();
  3249. mutex_unlock(&open_lock);
  3250. return 0;
  3251. }
  3252. /*
  3253. * floppy_open check for aliasing (/dev/fd0 can be the same as
  3254. * /dev/PS0 etc), and disallows simultaneous access to the same
  3255. * drive with different device numbers.
  3256. */
  3257. static int floppy_open(struct inode *inode, struct file *filp)
  3258. {
  3259. int drive = (long)inode->i_bdev->bd_disk->private_data;
  3260. int old_dev;
  3261. int try;
  3262. int res = -EBUSY;
  3263. char *tmp;
  3264. filp->private_data = (void *)0;
  3265. mutex_lock(&open_lock);
  3266. old_dev = UDRS->fd_device;
  3267. if (opened_bdev[drive] && opened_bdev[drive] != inode->i_bdev)
  3268. goto out2;
  3269. if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
  3270. USETF(FD_DISK_CHANGED);
  3271. USETF(FD_VERIFY);
  3272. }
  3273. if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
  3274. goto out2;
  3275. if (floppy_grab_irq_and_dma())
  3276. goto out2;
  3277. if (filp->f_flags & O_EXCL)
  3278. UDRS->fd_ref = -1;
  3279. else
  3280. UDRS->fd_ref++;
  3281. opened_bdev[drive] = inode->i_bdev;
  3282. res = -ENXIO;
  3283. if (!floppy_track_buffer) {
  3284. /* if opening an ED drive, reserve a big buffer,
  3285. * else reserve a small one */
  3286. if ((UDP->cmos == 6) || (UDP->cmos == 5))
  3287. try = 64; /* Only 48 actually useful */
  3288. else
  3289. try = 32; /* Only 24 actually useful */
  3290. tmp = (char *)fd_dma_mem_alloc(1024 * try);
  3291. if (!tmp && !floppy_track_buffer) {
  3292. try >>= 1; /* buffer only one side */
  3293. INFBOUND(try, 16);
  3294. tmp = (char *)fd_dma_mem_alloc(1024 * try);
  3295. }
  3296. if (!tmp && !floppy_track_buffer) {
  3297. fallback_on_nodma_alloc(&tmp, 2048 * try);
  3298. }
  3299. if (!tmp && !floppy_track_buffer) {
  3300. DPRINT("Unable to allocate DMA memory\n");
  3301. goto out;
  3302. }
  3303. if (floppy_track_buffer) {
  3304. if (tmp)
  3305. fd_dma_mem_free((unsigned long)tmp, try * 1024);
  3306. } else {
  3307. buffer_min = buffer_max = -1;
  3308. floppy_track_buffer = tmp;
  3309. max_buffer_sectors = try;
  3310. }
  3311. }
  3312. UDRS->fd_device = iminor(inode);
  3313. set_capacity(disks[drive], floppy_sizes[iminor(inode)]);
  3314. if (old_dev != -1 && old_dev != iminor(inode)) {
  3315. if (buffer_drive == drive)
  3316. buffer_track = -1;
  3317. }
  3318. /* Allow ioctls if we have write-permissions even if read-only open.
  3319. * Needed so that programs such as fdrawcmd still can work on write
  3320. * protected disks */
  3321. if ((filp->f_mode & FMODE_WRITE) || !file_permission(filp, MAY_WRITE))
  3322. filp->private_data = (void *)8;
  3323. if (UFDCS->rawcmd == 1)
  3324. UFDCS->rawcmd = 2;
  3325. if (!(filp->f_flags & O_NDELAY)) {
  3326. if (filp->f_mode & 3) {
  3327. UDRS->last_checked = 0;
  3328. check_disk_change(inode->i_bdev);
  3329. if (UTESTF(FD_DISK_CHANGED))
  3330. goto out;
  3331. }
  3332. res = -EROFS;
  3333. if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
  3334. goto out;
  3335. }
  3336. mutex_unlock(&open_lock);
  3337. return 0;
  3338. out:
  3339. if (UDRS->fd_ref < 0)
  3340. UDRS->fd_ref = 0;
  3341. else
  3342. UDRS->fd_ref--;
  3343. if (!UDRS->fd_ref)
  3344. opened_bdev[drive] = NULL;
  3345. floppy_release_irq_and_dma();
  3346. out2:
  3347. mutex_unlock(&open_lock);
  3348. return res;
  3349. }
  3350. /*
  3351. * Check if the disk has been changed or if a change has been faked.
  3352. */
  3353. static int check_floppy_change(struct gendisk *disk)
  3354. {
  3355. int drive = (long)disk->private_data;
  3356. if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
  3357. return 1;
  3358. if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
  3359. if (floppy_grab_irq_and_dma()) {
  3360. return 1;
  3361. }
  3362. lock_fdc(drive, 0);
  3363. poll_drive(0, 0);
  3364. process_fd_request();
  3365. floppy_release_irq_and_dma();
  3366. }
  3367. if (UTESTF(FD_DISK_CHANGED) ||
  3368. UTESTF(FD_VERIFY) ||
  3369. test_bit(drive, &fake_change) ||
  3370. (!ITYPE(UDRS->fd_device) && !current_type[drive]))
  3371. return 1;
  3372. return 0;
  3373. }
  3374. /*
  3375. * This implements "read block 0" for floppy_revalidate().
  3376. * Needed for format autodetection, checking whether there is
  3377. * a disk in the drive, and whether that disk is writable.
  3378. */
  3379. static int floppy_rb0_complete(struct bio *bio, unsigned int bytes_done,
  3380. int err)
  3381. {
  3382. if (bio->bi_size)
  3383. return 1;
  3384. complete((struct completion *)bio->bi_private);
  3385. return 0;
  3386. }
  3387. static int __floppy_read_block_0(struct block_device *bdev)
  3388. {
  3389. struct bio bio;
  3390. struct bio_vec bio_vec;
  3391. struct completion complete;
  3392. struct page *page;
  3393. size_t size;
  3394. page = alloc_page(GFP_NOIO);
  3395. if (!page) {
  3396. process_fd_request();
  3397. return -ENOMEM;
  3398. }
  3399. size = bdev->bd_block_size;
  3400. if (!size)
  3401. size = 1024;
  3402. bio_init(&bio);
  3403. bio.bi_io_vec = &bio_vec;
  3404. bio_vec.bv_page = page;
  3405. bio_vec.bv_len = size;
  3406. bio_vec.bv_offset = 0;
  3407. bio.bi_vcnt = 1;
  3408. bio.bi_idx = 0;
  3409. bio.bi_size = size;
  3410. bio.bi_bdev = bdev;
  3411. bio.bi_sector = 0;
  3412. init_completion(&complete);
  3413. bio.bi_private = &complete;
  3414. bio.bi_end_io = floppy_rb0_complete;
  3415. submit_bio(READ, &bio);
  3416. generic_unplug_device(bdev_get_queue(bdev));
  3417. process_fd_request();
  3418. wait_for_completion(&complete);
  3419. __free_page(page);
  3420. return 0;
  3421. }
  3422. /* revalidate the floppy disk, i.e. trigger format autodetection by reading
  3423. * the bootblock (block 0). "Autodetection" is also needed to check whether
  3424. * there is a disk in the drive at all... Thus we also do it for fixed
  3425. * geometry formats */
  3426. static int floppy_revalidate(struct gendisk *disk)
  3427. {
  3428. int drive = (long)disk->private_data;
  3429. #define NO_GEOM (!current_type[drive] && !ITYPE(UDRS->fd_device))
  3430. int cf;
  3431. int res = 0;
  3432. if (UTESTF(FD_DISK_CHANGED) ||
  3433. UTESTF(FD_VERIFY) || test_bit(drive, &fake_change) || NO_GEOM) {
  3434. if (usage_count == 0) {
  3435. printk("VFS: revalidate called on non-open device.\n");
  3436. return -EFAULT;
  3437. }
  3438. lock_fdc(drive, 0);
  3439. cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
  3440. if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)) {
  3441. process_fd_request(); /*already done by another thread */
  3442. return 0;
  3443. }
  3444. UDRS->maxblock = 0;
  3445. UDRS->maxtrack = 0;
  3446. if (buffer_drive == drive)
  3447. buffer_track = -1;
  3448. clear_bit(drive, &fake_change);
  3449. UCLEARF(FD_DISK_CHANGED);
  3450. if (cf)
  3451. UDRS->generation++;
  3452. if (NO_GEOM) {
  3453. /* auto-sensing */
  3454. res = __floppy_read_block_0(opened_bdev[drive]);
  3455. } else {
  3456. if (cf)
  3457. poll_drive(0, FD_RAW_NEED_DISK);
  3458. process_fd_request();
  3459. }
  3460. }
  3461. set_capacity(disk, floppy_sizes[UDRS->fd_device]);
  3462. return res;
  3463. }
  3464. static struct block_device_operations floppy_fops = {
  3465. .owner = THIS_MODULE,
  3466. .open = floppy_open,
  3467. .release = floppy_release,
  3468. .ioctl = fd_ioctl,
  3469. .getgeo = fd_getgeo,
  3470. .media_changed = check_floppy_change,
  3471. .revalidate_disk = floppy_revalidate,
  3472. };
  3473. /*
  3474. * Floppy Driver initialization
  3475. * =============================
  3476. */
  3477. /* Determine the floppy disk controller type */
  3478. /* This routine was written by David C. Niemi */
  3479. static char __init get_fdc_version(void)
  3480. {
  3481. int r;
  3482. output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
  3483. if (FDCS->reset)
  3484. return FDC_NONE;
  3485. if ((r = result()) <= 0x00)
  3486. return FDC_NONE; /* No FDC present ??? */
  3487. if ((r == 1) && (reply_buffer[0] == 0x80)) {
  3488. printk(KERN_INFO "FDC %d is an 8272A\n", fdc);
  3489. return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
  3490. }
  3491. if (r != 10) {
  3492. printk
  3493. ("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
  3494. fdc, r);
  3495. return FDC_UNKNOWN;
  3496. }
  3497. if (!fdc_configure()) {
  3498. printk(KERN_INFO "FDC %d is an 82072\n", fdc);
  3499. return FDC_82072; /* 82072 doesn't know CONFIGURE */
  3500. }
  3501. output_byte(FD_PERPENDICULAR);
  3502. if (need_more_output() == MORE_OUTPUT) {
  3503. output_byte(0);
  3504. } else {
  3505. printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
  3506. return FDC_82072A; /* 82072A as found on Sparcs. */
  3507. }
  3508. output_byte(FD_UNLOCK);
  3509. r = result();
  3510. if ((r == 1) && (reply_buffer[0] == 0x80)) {
  3511. printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
  3512. return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
  3513. * LOCK/UNLOCK */
  3514. }
  3515. if ((r != 1) || (reply_buffer[0] != 0x00)) {
  3516. printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
  3517. fdc, r);
  3518. return FDC_UNKNOWN;
  3519. }
  3520. output_byte(FD_PARTID);
  3521. r = result();
  3522. if (r != 1) {
  3523. printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
  3524. fdc, r);
  3525. return FDC_UNKNOWN;
  3526. }
  3527. if (reply_buffer[0] == 0x80) {
  3528. printk(KERN_INFO "FDC %d is a post-1991 82077\n", fdc);
  3529. return FDC_82077; /* Revised 82077AA passes all the tests */
  3530. }
  3531. switch (reply_buffer[0] >> 5) {
  3532. case 0x0:
  3533. /* Either a 82078-1 or a 82078SL running at 5Volt */
  3534. printk(KERN_INFO "FDC %d is an 82078.\n", fdc);
  3535. return FDC_82078;
  3536. case 0x1:
  3537. printk(KERN_INFO "FDC %d is a 44pin 82078\n", fdc);
  3538. return FDC_82078;
  3539. case 0x2:
  3540. printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
  3541. return FDC_S82078B;
  3542. case 0x3:
  3543. printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n",
  3544. fdc);
  3545. return FDC_87306;
  3546. default:
  3547. printk(KERN_INFO
  3548. "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
  3549. fdc, reply_buffer[0] >> 5);
  3550. return FDC_82078_UNKN;
  3551. }
  3552. } /* get_fdc_version */
  3553. /* lilo configuration */
  3554. static void __init floppy_set_flags(int *ints, int param, int param2)
  3555. {
  3556. int i;
  3557. for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  3558. if (param)
  3559. default_drive_params[i].params.flags |= param2;
  3560. else
  3561. default_drive_params[i].params.flags &= ~param2;
  3562. }
  3563. DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
  3564. }
  3565. static void __init daring(int *ints, int param, int param2)
  3566. {
  3567. int i;
  3568. for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  3569. if (param) {
  3570. default_drive_params[i].params.select_delay = 0;
  3571. default_drive_params[i].params.flags |=
  3572. FD_SILENT_DCL_CLEAR;
  3573. } else {
  3574. default_drive_params[i].params.select_delay =
  3575. 2 * HZ / 100;
  3576. default_drive_params[i].params.flags &=
  3577. ~FD_SILENT_DCL_CLEAR;
  3578. }
  3579. }
  3580. DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
  3581. }
  3582. static void __init set_cmos(int *ints, int dummy, int dummy2)
  3583. {
  3584. int current_drive = 0;
  3585. if (ints[0] != 2) {
  3586. DPRINT("wrong number of parameters for CMOS\n");
  3587. return;
  3588. }
  3589. current_drive = ints[1];
  3590. if (current_drive < 0 || current_drive >= 8) {
  3591. DPRINT("bad drive for set_cmos\n");
  3592. return;
  3593. }
  3594. #if N_FDC > 1
  3595. if (current_drive >= 4 && !FDC2)
  3596. FDC2 = 0x370;
  3597. #endif
  3598. DP->cmos = ints[2];
  3599. DPRINT("setting CMOS code to %d\n", ints[2]);
  3600. }
  3601. static struct param_table {
  3602. const char *name;
  3603. void (*fn) (int *ints, int param, int param2);
  3604. int *var;
  3605. int def_param;
  3606. int param2;
  3607. } config_params[] __initdata = {
  3608. {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  3609. {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  3610. {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
  3611. {"irq", NULL, &FLOPPY_IRQ, 6, 0},
  3612. {"dma", NULL, &FLOPPY_DMA, 2, 0},
  3613. {"daring", daring, NULL, 1, 0},
  3614. #if N_FDC > 1
  3615. {"two_fdc", NULL, &FDC2, 0x370, 0},
  3616. {"one_fdc", NULL, &FDC2, 0, 0},
  3617. #endif
  3618. {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
  3619. {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
  3620. {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
  3621. {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
  3622. {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
  3623. {"nodma", NULL, &can_use_virtual_dma, 1, 0},
  3624. {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
  3625. {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
  3626. {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
  3627. {"nofifo", NULL, &no_fifo, 0x20, 0},
  3628. {"usefifo", NULL, &no_fifo, 0, 0},
  3629. {"cmos", set_cmos, NULL, 0, 0},
  3630. {"slow", NULL, &slow_floppy, 1, 0},
  3631. {"unexpected_interrupts", NULL, &print_unex, 1, 0},
  3632. {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
  3633. {"L40SX", NULL, &print_unex, 0, 0}
  3634. EXTRA_FLOPPY_PARAMS
  3635. };
  3636. static int __init floppy_setup(char *str)
  3637. {
  3638. int i;
  3639. int param;
  3640. int ints[11];
  3641. str = get_options(str, ARRAY_SIZE(ints), ints);
  3642. if (str) {
  3643. for (i = 0; i < ARRAY_SIZE(config_params); i++) {
  3644. if (strcmp(str, config_params[i].name) == 0) {
  3645. if (ints[0])
  3646. param = ints[1];
  3647. else
  3648. param = config_params[i].def_param;
  3649. if (config_params[i].fn)
  3650. config_params[i].
  3651. fn(ints, param,
  3652. config_params[i].param2);
  3653. if (config_params[i].var) {
  3654. DPRINT("%s=%d\n", str, param);
  3655. *config_params[i].var = param;
  3656. }
  3657. return 1;
  3658. }
  3659. }
  3660. }
  3661. if (str) {
  3662. DPRINT("unknown floppy option [%s]\n", str);
  3663. DPRINT("allowed options are:");
  3664. for (i = 0; i < ARRAY_SIZE(config_params); i++)
  3665. printk(" %s", config_params[i].name);
  3666. printk("\n");
  3667. } else
  3668. DPRINT("botched floppy option\n");
  3669. DPRINT("Read Documentation/floppy.txt\n");
  3670. return 0;
  3671. }
  3672. static int have_no_fdc = -ENODEV;
  3673. static ssize_t floppy_cmos_show(struct device *dev,
  3674. struct device_attribute *attr, char *buf)
  3675. {
  3676. struct platform_device *p;
  3677. int drive;
  3678. p = container_of(dev, struct platform_device,dev);
  3679. drive = p->id;
  3680. return sprintf(buf, "%X\n", UDP->cmos);
  3681. }
  3682. DEVICE_ATTR(cmos,S_IRUGO,floppy_cmos_show,NULL);
  3683. static void floppy_device_release(struct device *dev)
  3684. {
  3685. complete(&device_release);
  3686. }
  3687. static struct platform_device floppy_device[N_DRIVE];
  3688. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  3689. {
  3690. int drive = (*part & 3) | ((*part & 0x80) >> 5);
  3691. if (drive >= N_DRIVE ||
  3692. !(allowed_drive_mask & (1 << drive)) ||
  3693. fdc_state[FDC(drive)].version == FDC_NONE)
  3694. return NULL;
  3695. if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
  3696. return NULL;
  3697. *part = 0;
  3698. return get_disk(disks[drive]);
  3699. }
  3700. static int __init floppy_init(void)
  3701. {
  3702. int i, unit, drive;
  3703. int err, dr;
  3704. raw_cmd = NULL;
  3705. for (dr = 0; dr < N_DRIVE; dr++) {
  3706. disks[dr] = alloc_disk(1);
  3707. if (!disks[dr]) {
  3708. err = -ENOMEM;
  3709. goto out_put_disk;
  3710. }
  3711. disks[dr]->major = FLOPPY_MAJOR;
  3712. disks[dr]->first_minor = TOMINOR(dr);
  3713. disks[dr]->fops = &floppy_fops;
  3714. sprintf(disks[dr]->disk_name, "fd%d", dr);
  3715. init_timer(&motor_off_timer[dr]);
  3716. motor_off_timer[dr].data = dr;
  3717. motor_off_timer[dr].function = motor_off_callback;
  3718. }
  3719. err = register_blkdev(FLOPPY_MAJOR, "fd");
  3720. if (err)
  3721. goto out_put_disk;
  3722. floppy_queue = blk_init_queue(do_fd_request, &floppy_lock);
  3723. if (!floppy_queue) {
  3724. err = -ENOMEM;
  3725. goto out_unreg_blkdev;
  3726. }
  3727. blk_queue_max_sectors(floppy_queue, 64);
  3728. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  3729. floppy_find, NULL, NULL);
  3730. for (i = 0; i < 256; i++)
  3731. if (ITYPE(i))
  3732. floppy_sizes[i] = floppy_type[ITYPE(i)].size;
  3733. else
  3734. floppy_sizes[i] = MAX_DISK_SIZE << 1;
  3735. reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
  3736. config_types();
  3737. for (i = 0; i < N_FDC; i++) {
  3738. fdc = i;
  3739. CLEARSTRUCT(FDCS);
  3740. FDCS->dtr = -1;
  3741. FDCS->dor = 0x4;
  3742. #if defined(__sparc__) || defined(__mc68000__)
  3743. /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
  3744. #ifdef __mc68000__
  3745. if (MACH_IS_SUN3X)
  3746. #endif
  3747. FDCS->version = FDC_82072A;
  3748. #endif
  3749. }
  3750. use_virtual_dma = can_use_virtual_dma & 1;
  3751. #if defined(CONFIG_PPC_MERGE)
  3752. if (check_legacy_ioport(FDC1)) {
  3753. del_timer(&fd_timeout);
  3754. err = -ENODEV;
  3755. goto out_unreg_region;
  3756. }
  3757. #endif
  3758. fdc_state[0].address = FDC1;
  3759. if (fdc_state[0].address == -1) {
  3760. del_timer(&fd_timeout);
  3761. err = -ENODEV;
  3762. goto out_unreg_region;
  3763. }
  3764. #if N_FDC > 1
  3765. fdc_state[1].address = FDC2;
  3766. #endif
  3767. fdc = 0; /* reset fdc in case of unexpected interrupt */
  3768. err = floppy_grab_irq_and_dma();
  3769. if (err) {
  3770. del_timer(&fd_timeout);
  3771. err = -EBUSY;
  3772. goto out_unreg_region;
  3773. }
  3774. /* initialise drive state */
  3775. for (drive = 0; drive < N_DRIVE; drive++) {
  3776. CLEARSTRUCT(UDRS);
  3777. CLEARSTRUCT(UDRWE);
  3778. USETF(FD_DISK_NEWCHANGE);
  3779. USETF(FD_DISK_CHANGED);
  3780. USETF(FD_VERIFY);
  3781. UDRS->fd_device = -1;
  3782. floppy_track_buffer = NULL;
  3783. max_buffer_sectors = 0;
  3784. }
  3785. /*
  3786. * Small 10 msec delay to let through any interrupt that
  3787. * initialization might have triggered, to not
  3788. * confuse detection:
  3789. */
  3790. msleep(10);
  3791. for (i = 0; i < N_FDC; i++) {
  3792. fdc = i;
  3793. FDCS->driver_version = FD_DRIVER_VERSION;
  3794. for (unit = 0; unit < 4; unit++)
  3795. FDCS->track[unit] = 0;
  3796. if (FDCS->address == -1)
  3797. continue;
  3798. FDCS->rawcmd = 2;
  3799. if (user_reset_fdc(-1, FD_RESET_ALWAYS, 0)) {
  3800. /* free ioports reserved by floppy_grab_irq_and_dma() */
  3801. release_region(FDCS->address + 2, 4);
  3802. release_region(FDCS->address + 7, 1);
  3803. FDCS->address = -1;
  3804. FDCS->version = FDC_NONE;
  3805. continue;
  3806. }
  3807. /* Try to determine the floppy controller type */
  3808. FDCS->version = get_fdc_version();
  3809. if (FDCS->version == FDC_NONE) {
  3810. /* free ioports reserved by floppy_grab_irq_and_dma() */
  3811. release_region(FDCS->address + 2, 4);
  3812. release_region(FDCS->address + 7, 1);
  3813. FDCS->address = -1;
  3814. continue;
  3815. }
  3816. if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
  3817. can_use_virtual_dma = 0;
  3818. have_no_fdc = 0;
  3819. /* Not all FDCs seem to be able to handle the version command
  3820. * properly, so force a reset for the standard FDC clones,
  3821. * to avoid interrupt garbage.
  3822. */
  3823. user_reset_fdc(-1, FD_RESET_ALWAYS, 0);
  3824. }
  3825. fdc = 0;
  3826. del_timer(&fd_timeout);
  3827. current_drive = 0;
  3828. floppy_release_irq_and_dma();
  3829. initialising = 0;
  3830. if (have_no_fdc) {
  3831. DPRINT("no floppy controllers found\n");
  3832. err = have_no_fdc;
  3833. goto out_flush_work;
  3834. }
  3835. for (drive = 0; drive < N_DRIVE; drive++) {
  3836. if (!(allowed_drive_mask & (1 << drive)))
  3837. continue;
  3838. if (fdc_state[FDC(drive)].version == FDC_NONE)
  3839. continue;
  3840. floppy_device[drive].name = floppy_device_name;
  3841. floppy_device[drive].id = drive;
  3842. floppy_device[drive].dev.release = floppy_device_release;
  3843. err = platform_device_register(&floppy_device[drive]);
  3844. if (err)
  3845. goto out_flush_work;
  3846. device_create_file(&floppy_device[drive].dev,&dev_attr_cmos);
  3847. /* to be cleaned up... */
  3848. disks[drive]->private_data = (void *)(long)drive;
  3849. disks[drive]->queue = floppy_queue;
  3850. disks[drive]->flags |= GENHD_FL_REMOVABLE;
  3851. disks[drive]->driverfs_dev = &floppy_device[drive].dev;
  3852. add_disk(disks[drive]);
  3853. }
  3854. return 0;
  3855. out_flush_work:
  3856. flush_scheduled_work();
  3857. if (usage_count)
  3858. floppy_release_irq_and_dma();
  3859. out_unreg_region:
  3860. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  3861. blk_cleanup_queue(floppy_queue);
  3862. out_unreg_blkdev:
  3863. unregister_blkdev(FLOPPY_MAJOR, "fd");
  3864. out_put_disk:
  3865. while (dr--) {
  3866. del_timer(&motor_off_timer[dr]);
  3867. put_disk(disks[dr]);
  3868. }
  3869. return err;
  3870. }
  3871. static DEFINE_SPINLOCK(floppy_usage_lock);
  3872. static int floppy_grab_irq_and_dma(void)
  3873. {
  3874. unsigned long flags;
  3875. spin_lock_irqsave(&floppy_usage_lock, flags);
  3876. if (usage_count++) {
  3877. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3878. return 0;
  3879. }
  3880. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3881. /*
  3882. * We might have scheduled a free_irq(), wait it to
  3883. * drain first:
  3884. */
  3885. flush_scheduled_work();
  3886. if (fd_request_irq()) {
  3887. DPRINT("Unable to grab IRQ%d for the floppy driver\n",
  3888. FLOPPY_IRQ);
  3889. spin_lock_irqsave(&floppy_usage_lock, flags);
  3890. usage_count--;
  3891. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3892. return -1;
  3893. }
  3894. if (fd_request_dma()) {
  3895. DPRINT("Unable to grab DMA%d for the floppy driver\n",
  3896. FLOPPY_DMA);
  3897. fd_free_irq();
  3898. spin_lock_irqsave(&floppy_usage_lock, flags);
  3899. usage_count--;
  3900. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3901. return -1;
  3902. }
  3903. for (fdc = 0; fdc < N_FDC; fdc++) {
  3904. if (FDCS->address != -1) {
  3905. if (!request_region(FDCS->address + 2, 4, "floppy")) {
  3906. DPRINT("Floppy io-port 0x%04lx in use\n",
  3907. FDCS->address + 2);
  3908. goto cleanup1;
  3909. }
  3910. if (!request_region(FDCS->address + 7, 1, "floppy DIR")) {
  3911. DPRINT("Floppy io-port 0x%04lx in use\n",
  3912. FDCS->address + 7);
  3913. goto cleanup2;
  3914. }
  3915. /* address + 6 is reserved, and may be taken by IDE.
  3916. * Unfortunately, Adaptec doesn't know this :-(, */
  3917. }
  3918. }
  3919. for (fdc = 0; fdc < N_FDC; fdc++) {
  3920. if (FDCS->address != -1) {
  3921. reset_fdc_info(1);
  3922. fd_outb(FDCS->dor, FD_DOR);
  3923. }
  3924. }
  3925. fdc = 0;
  3926. set_dor(0, ~0, 8); /* avoid immediate interrupt */
  3927. for (fdc = 0; fdc < N_FDC; fdc++)
  3928. if (FDCS->address != -1)
  3929. fd_outb(FDCS->dor, FD_DOR);
  3930. /*
  3931. * The driver will try and free resources and relies on us
  3932. * to know if they were allocated or not.
  3933. */
  3934. fdc = 0;
  3935. irqdma_allocated = 1;
  3936. return 0;
  3937. cleanup2:
  3938. release_region(FDCS->address + 2, 4);
  3939. cleanup1:
  3940. fd_free_irq();
  3941. fd_free_dma();
  3942. while (--fdc >= 0) {
  3943. release_region(FDCS->address + 2, 4);
  3944. release_region(FDCS->address + 7, 1);
  3945. }
  3946. spin_lock_irqsave(&floppy_usage_lock, flags);
  3947. usage_count--;
  3948. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3949. return -1;
  3950. }
  3951. static void floppy_release_irq_and_dma(void)
  3952. {
  3953. int old_fdc;
  3954. #ifdef FLOPPY_SANITY_CHECK
  3955. #ifndef __sparc__
  3956. int drive;
  3957. #endif
  3958. #endif
  3959. long tmpsize;
  3960. unsigned long tmpaddr;
  3961. unsigned long flags;
  3962. spin_lock_irqsave(&floppy_usage_lock, flags);
  3963. if (--usage_count) {
  3964. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3965. return;
  3966. }
  3967. spin_unlock_irqrestore(&floppy_usage_lock, flags);
  3968. if (irqdma_allocated) {
  3969. fd_disable_dma();
  3970. fd_free_dma();
  3971. schedule_work(&fd_free_irq_work);
  3972. irqdma_allocated = 0;
  3973. }
  3974. set_dor(0, ~0, 8);
  3975. #if N_FDC > 1
  3976. set_dor(1, ~8, 0);
  3977. #endif
  3978. floppy_enable_hlt();
  3979. if (floppy_track_buffer && max_buffer_sectors) {
  3980. tmpsize = max_buffer_sectors * 1024;
  3981. tmpaddr = (unsigned long)floppy_track_buffer;
  3982. floppy_track_buffer = NULL;
  3983. max_buffer_sectors = 0;
  3984. buffer_min = buffer_max = -1;
  3985. fd_dma_mem_free(tmpaddr, tmpsize);
  3986. }
  3987. #ifdef FLOPPY_SANITY_CHECK
  3988. #ifndef __sparc__
  3989. for (drive = 0; drive < N_FDC * 4; drive++)
  3990. if (timer_pending(motor_off_timer + drive))
  3991. printk("motor off timer %d still active\n", drive);
  3992. #endif
  3993. if (timer_pending(&fd_timeout))
  3994. printk("floppy timer still active:%s\n", timeout_message);
  3995. if (timer_pending(&fd_timer))
  3996. printk("auxiliary floppy timer still active\n");
  3997. if (floppy_work.pending)
  3998. printk("work still pending\n");
  3999. #endif
  4000. old_fdc = fdc;
  4001. for (fdc = 0; fdc < N_FDC; fdc++)
  4002. if (FDCS->address != -1) {
  4003. release_region(FDCS->address + 2, 4);
  4004. release_region(FDCS->address + 7, 1);
  4005. }
  4006. fdc = old_fdc;
  4007. }
  4008. #ifdef MODULE
  4009. static char *floppy;
  4010. static void __init parse_floppy_cfg_string(char *cfg)
  4011. {
  4012. char *ptr;
  4013. while (*cfg) {
  4014. for (ptr = cfg; *cfg && *cfg != ' ' && *cfg != '\t'; cfg++) ;
  4015. if (*cfg) {
  4016. *cfg = '\0';
  4017. cfg++;
  4018. }
  4019. if (*ptr)
  4020. floppy_setup(ptr);
  4021. }
  4022. }
  4023. int __init init_module(void)
  4024. {
  4025. if (floppy)
  4026. parse_floppy_cfg_string(floppy);
  4027. return floppy_init();
  4028. }
  4029. void cleanup_module(void)
  4030. {
  4031. int drive;
  4032. init_completion(&device_release);
  4033. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  4034. unregister_blkdev(FLOPPY_MAJOR, "fd");
  4035. for (drive = 0; drive < N_DRIVE; drive++) {
  4036. del_timer_sync(&motor_off_timer[drive]);
  4037. if ((allowed_drive_mask & (1 << drive)) &&
  4038. fdc_state[FDC(drive)].version != FDC_NONE) {
  4039. del_gendisk(disks[drive]);
  4040. device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
  4041. platform_device_unregister(&floppy_device[drive]);
  4042. }
  4043. put_disk(disks[drive]);
  4044. }
  4045. del_timer_sync(&fd_timeout);
  4046. del_timer_sync(&fd_timer);
  4047. blk_cleanup_queue(floppy_queue);
  4048. if (usage_count)
  4049. floppy_release_irq_and_dma();
  4050. /* eject disk, if any */
  4051. fd_eject(0);
  4052. flush_scheduled_work(); /* fd_free_irq() might be pending */
  4053. wait_for_completion(&device_release);
  4054. }
  4055. module_param(floppy, charp, 0);
  4056. module_param(FLOPPY_IRQ, int, 0);
  4057. module_param(FLOPPY_DMA, int, 0);
  4058. MODULE_AUTHOR("Alain L. Knaff");
  4059. MODULE_SUPPORTED_DEVICE("fd");
  4060. MODULE_LICENSE("GPL");
  4061. #else
  4062. __setup("floppy=", floppy_setup);
  4063. module_init(floppy_init)
  4064. #endif
  4065. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);