floppy.c 117 KB

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