floppy.c 117 KB

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