floppy.c 117 KB

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