floppy.c 116 KB

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