floppy.c 117 KB

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