floppy.c 119 KB

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