floppy.c 116 KB

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