floppy.c 119 KB

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