floppy.c 117 KB

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