floppy.c 117 KB

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