floppy.c 127 KB

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