floppy.c 118 KB

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