floppy.c 116 KB

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