maestro.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832
  1. /*****************************************************************************
  2. *
  3. * ESS Maestro/Maestro-2/Maestro-2E driver for Linux 2.[23].x
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * (c) Copyright 1999 Alan Cox <alan.cox@linux.org>
  20. *
  21. * Based heavily on SonicVibes.c:
  22. * Copyright (C) 1998-1999 Thomas Sailer (sailer@ife.ee.ethz.ch)
  23. *
  24. * Heavily modified by Zach Brown <zab@zabbo.net> based on lunch
  25. * with ESS engineers. Many thanks to Howard Kim for providing
  26. * contacts and hardware. Honorable mention goes to Eric
  27. * Brombaugh for all sorts of things. Best regards to the
  28. * proprietors of Hack Central for fine lodging.
  29. *
  30. * Supported devices:
  31. * /dev/dsp0-3 standard /dev/dsp device, (mostly) OSS compatible
  32. * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
  33. *
  34. * Hardware Description
  35. *
  36. * A working Maestro setup contains the Maestro chip wired to a
  37. * codec or 2. In the Maestro we have the APUs, the ASSP, and the
  38. * Wavecache. The APUs can be though of as virtual audio routing
  39. * channels. They can take data from a number of sources and perform
  40. * basic encodings of the data. The wavecache is a storehouse for
  41. * PCM data. Typically it deals with PCI and interracts with the
  42. * APUs. The ASSP is a wacky DSP like device that ESS is loth
  43. * to release docs on. Thankfully it isn't required on the Maestro
  44. * until you start doing insane things like FM emulation and surround
  45. * encoding. The codecs are almost always AC-97 compliant codecs,
  46. * but it appears that early Maestros may have had PT101 (an ESS
  47. * part?) wired to them. The only real difference in the Maestro
  48. * families is external goop like docking capability, memory for
  49. * the ASSP, and initialization differences.
  50. *
  51. * Driver Operation
  52. *
  53. * We only drive the APU/Wavecache as typical DACs and drive the
  54. * mixers in the codecs. There are 64 APUs. We assign 6 to each
  55. * /dev/dsp? device. 2 channels for output, and 4 channels for
  56. * input.
  57. *
  58. * Each APU can do a number of things, but we only really use
  59. * 3 basic functions. For playback we use them to convert PCM
  60. * data fetched over PCI by the wavecahche into analog data that
  61. * is handed to the codec. One APU for mono, and a pair for stereo.
  62. * When in stereo, the combination of smarts in the APU and Wavecache
  63. * decide which wavecache gets the left or right channel.
  64. *
  65. * For record we still use the old overly mono system. For each in
  66. * coming channel the data comes in from the codec, through a 'input'
  67. * APU, through another rate converter APU, and then into memory via
  68. * the wavecache and PCI. If its stereo, we mash it back into LRLR in
  69. * software. The pass between the 2 APUs is supposedly what requires us
  70. * to have a 512 byte buffer sitting around in wavecache/memory.
  71. *
  72. * The wavecache makes our life even more fun. First off, it can
  73. * only address the first 28 bits of PCI address space, making it
  74. * useless on quite a few architectures. Secondly, its insane.
  75. * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
  76. * But that doesn't really work. You can only use 1 region. So all our
  77. * allocations have to be in 4meg of each other. Booo. Hiss.
  78. * So we have a module parameter, dsps_order, that is the order of
  79. * the number of dsps to provide. All their buffer space is allocated
  80. * on open time. The sonicvibes OSS routines we inherited really want
  81. * power of 2 buffers, so we have all those next to each other, then
  82. * 512 byte regions for the recording wavecaches. This ends up
  83. * wasting quite a bit of memory. The only fixes I can see would be
  84. * getting a kernel allocator that could work in zones, or figuring out
  85. * just how to coerce the WP into doing what we want.
  86. *
  87. * The indirection of the various registers means we have to spinlock
  88. * nearly all register accesses. We have the main register indirection
  89. * like the wave cache, maestro registers, etc. Then we have beasts
  90. * like the APU interface that is indirect registers gotten at through
  91. * the main maestro indirection. Ouch. We spinlock around the actual
  92. * ports on a per card basis. This means spinlock activity at each IO
  93. * operation, but the only IO operation clusters are in non critical
  94. * paths and it makes the code far easier to follow. Interrupts are
  95. * blocked while holding the locks because the int handler has to
  96. * get at some of them :(. The mixer interface doesn't, however.
  97. * We also have an OSS state lock that is thrown around in a few
  98. * places.
  99. *
  100. * This driver has brute force APM suspend support. We catch suspend
  101. * notifications and stop all work being done on the chip. Any people
  102. * that try between this shutdown and the real suspend operation will
  103. * be put to sleep. When we resume we restore our software state on
  104. * the chip and wake up the people that were using it. The code thats
  105. * being used now is quite dirty and assumes we're on a uni-processor
  106. * machine. Much of it will need to be cleaned up for SMP ACPI or
  107. * similar.
  108. *
  109. * We also pay attention to PCI power management now. The driver
  110. * will power down units of the chip that it knows aren't needed.
  111. * The WaveProcessor and company are only powered on when people
  112. * have /dev/dsp*s open. On removal the driver will
  113. * power down the maestro entirely. There could still be
  114. * trouble with BIOSen that magically change power states
  115. * themselves, but we'll see.
  116. *
  117. * History
  118. * v0.15 - May 21 2001 - Marcus Meissner <mm@caldera.de>
  119. * Ported to Linux 2.4 PCI API. Some clean ups, global devs list
  120. * removed (now using pci device driver data).
  121. * PM needs to be polished still. Bumped version.
  122. * (still kind of v0.14) May 13 2001 - Ben Pfaff <pfaffben@msu.edu>
  123. * Add support for 978 docking and basic hardware volume control
  124. * (still kind of v0.14) Nov 23 - Alan Cox <alan@redhat.com>
  125. * Add clocking= for people with seriously warped hardware
  126. * (still v0.14) Nov 10 2000 - Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
  127. * add __init to maestro_ac97_init() and maestro_install()
  128. * (still based on v0.14) Mar 29 2000 - Zach Brown <zab@redhat.com>
  129. * move to 2.3 power management interface, which
  130. * required hacking some suspend/resume/check paths
  131. * make static compilation work
  132. * v0.14 - Jan 28 2000 - Zach Brown <zab@redhat.com>
  133. * add PCI power management through ACPI regs.
  134. * we now shut down on machine reboot/halt
  135. * leave scary PCI config items alone (isa stuff, mostly)
  136. * enable 1921s, it seems only mine was broke.
  137. * fix swapped left/right pcm dac. har har.
  138. * up bob freq, increase buffers, fix pointers at underflow
  139. * silly compilation problems
  140. * v0.13 - Nov 18 1999 - Zach Brown <zab@redhat.com>
  141. * fix nec Versas? man would that be cool.
  142. * v0.12 - Nov 12 1999 - Zach Brown <zab@redhat.com>
  143. * brown bag volume max fix..
  144. * v0.11 - Nov 11 1999 - Zach Brown <zab@redhat.com>
  145. * use proper stereo apu decoding, mmap/write should work.
  146. * make volume sliders more useful, tweak rate calculation.
  147. * fix lame 8bit format reporting bug. duh. apm apu saving buglet also
  148. * fix maestro 1 clock freq "bug", remove pt101 support
  149. * v0.10 - Oct 28 1999 - Zach Brown <zab@redhat.com>
  150. * aha, so, sometimes the WP writes a status word to offset 0
  151. * from one of the PCMBARs. rearrange allocation accordingly..
  152. * cheers again to Eric for being a good hacker in investigating this.
  153. * Jeroen Hoogervorst submits 7500 fix out of nowhere. yay. :)
  154. * v0.09 - Oct 23 1999 - Zach Brown <zab@redhat.com>
  155. * added APM support.
  156. * re-order something such that some 2Es now work. Magic!
  157. * new codec reset routine. made some codecs come to life.
  158. * fix clear_advance, sync some control with ESS.
  159. * now write to all base regs to be paranoid.
  160. * v0.08 - Oct 20 1999 - Zach Brown <zab@redhat.com>
  161. * Fix initial buflen bug. I am so smart. also smp compiling..
  162. * I owe Eric yet another beer: fixed recmask, igain,
  163. * muting, and adc sync consistency. Go Team.
  164. * v0.07 - Oct 4 1999 - Zach Brown <zab@redhat.com>
  165. * tweak adc/dac, formating, and stuff to allow full duplex
  166. * allocate dsps memory at open() so we can fit in the wavecache window
  167. * fix wavecache braindamage. again. no more scribbling?
  168. * fix ess 1921 codec bug on some laptops.
  169. * fix dumb pci scanning bug
  170. * started 2.3 cleanup, redid spinlocks, little cleanups
  171. * v0.06 - Sep 20 1999 - Zach Brown <zab@redhat.com>
  172. * fix wavecache thinkos. limit to 1 /dev/dsp.
  173. * eric is wearing his thinking toque this week.
  174. * spotted apu mode bugs and gain ramping problem
  175. * don't touch weird mixer regs, make recmask optional
  176. * fixed igain inversion, defaults for mixers, clean up rec_start
  177. * make mono recording work.
  178. * report subsystem stuff, please send reports.
  179. * littles: parallel out, amp now
  180. * v0.05 - Sep 17 1999 - Zach Brown <zab@redhat.com>
  181. * merged and fixed up Eric's initial recording code
  182. * munged format handling to catch misuse, needs rewrite.
  183. * revert ring bus init, fixup shared int, add pci busmaster setting
  184. * fix mixer oss interface, fix mic mute and recmask
  185. * mask off unsupported mixers, reset with all 1s, modularize defaults
  186. * make sure bob is running while we need it
  187. * got rid of device limit, initial minimal apm hooks
  188. * pull out dead code/includes, only allow multimedia/audio maestros
  189. * v0.04 - Sep 01 1999 - Zach Brown <zab@redhat.com>
  190. * copied memory leak fix from sonicvibes driver
  191. * different ac97 reset, play with 2.0 ac97, simplify ring bus setup
  192. * bob freq code, region sanity, jitter sync fix; all from Eric
  193. *
  194. * TODO
  195. * fix bob frequency
  196. * endianness
  197. * do smart things with ac97 2.0 bits.
  198. * dual codecs
  199. * leave 54->61 open
  200. *
  201. * it also would be fun to have a mode that would not use pci dma at all
  202. * but would copy into the wavecache on board memory and use that
  203. * on architectures that don't like the maestro's pci dma ickiness.
  204. */
  205. /*****************************************************************************/
  206. #include <linux/module.h>
  207. #include <linux/sched.h>
  208. #include <linux/smp_lock.h>
  209. #include <linux/string.h>
  210. #include <linux/ctype.h>
  211. #include <linux/ioport.h>
  212. #include <linux/delay.h>
  213. #include <linux/sound.h>
  214. #include <linux/slab.h>
  215. #include <linux/soundcard.h>
  216. #include <linux/pci.h>
  217. #include <linux/spinlock.h>
  218. #include <linux/init.h>
  219. #include <linux/interrupt.h>
  220. #include <linux/poll.h>
  221. #include <linux/reboot.h>
  222. #include <linux/bitops.h>
  223. #include <linux/wait.h>
  224. #include <asm/current.h>
  225. #include <asm/dma.h>
  226. #include <asm/io.h>
  227. #include <asm/page.h>
  228. #include <asm/uaccess.h>
  229. #include <linux/pm.h>
  230. static int maestro_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *d);
  231. #include "maestro.h"
  232. static struct pci_driver maestro_pci_driver;
  233. /* --------------------------------------------------------------------- */
  234. #define M_DEBUG 1
  235. #ifdef M_DEBUG
  236. static int debug;
  237. #define M_printk(args...) {if (debug) printk(args);}
  238. #else
  239. #define M_printk(x)
  240. #endif
  241. /* we try to setup 2^(dsps_order) /dev/dsp devices */
  242. static int dsps_order;
  243. /* whether or not we mess around with power management */
  244. static int use_pm=2; /* set to 1 for force */
  245. /* clocking for broken hardware - a few laptops seem to use a 50Khz clock
  246. ie insmod with clocking=50000 or so */
  247. static int clocking=48000;
  248. MODULE_AUTHOR("Zach Brown <zab@zabbo.net>, Alan Cox <alan@redhat.com>");
  249. MODULE_DESCRIPTION("ESS Maestro Driver");
  250. MODULE_LICENSE("GPL");
  251. #ifdef M_DEBUG
  252. module_param(debug, bool, 0644);
  253. #endif
  254. module_param(dsps_order, int, 0);
  255. module_param(use_pm, int, 0);
  256. module_param(clocking, int, 0);
  257. /* --------------------------------------------------------------------- */
  258. #define DRIVER_VERSION "0.15"
  259. #ifndef PCI_VENDOR_ESS
  260. #define PCI_VENDOR_ESS 0x125D
  261. #define PCI_DEVICE_ID_ESS_ESS1968 0x1968 /* Maestro 2 */
  262. #define PCI_DEVICE_ID_ESS_ESS1978 0x1978 /* Maestro 2E */
  263. #define PCI_VENDOR_ESS_OLD 0x1285 /* Platform Tech,
  264. the people the maestro
  265. was bought from */
  266. #define PCI_DEVICE_ID_ESS_ESS0100 0x0100 /* maestro 1 */
  267. #endif /* PCI_VENDOR_ESS */
  268. #define ESS_CHAN_HARD 0x100
  269. /* NEC Versas ? */
  270. #define NEC_VERSA_SUBID1 0x80581033
  271. #define NEC_VERSA_SUBID2 0x803c1033
  272. /* changed so that I could actually find all the
  273. references and fix them up. it's a little more readable now. */
  274. #define ESS_FMT_STEREO 0x01
  275. #define ESS_FMT_16BIT 0x02
  276. #define ESS_FMT_MASK 0x03
  277. #define ESS_DAC_SHIFT 0
  278. #define ESS_ADC_SHIFT 4
  279. #define ESS_STATE_MAGIC 0x125D1968
  280. #define ESS_CARD_MAGIC 0x19283746
  281. #define DAC_RUNNING 1
  282. #define ADC_RUNNING 2
  283. #define MAX_DSP_ORDER 2
  284. #define MAX_DSPS (1<<MAX_DSP_ORDER)
  285. #define NR_DSPS (1<<dsps_order)
  286. #define NR_IDRS 32
  287. #define NR_APUS 64
  288. #define NR_APU_REGS 16
  289. /* acpi states */
  290. enum {
  291. ACPI_D0=0,
  292. ACPI_D1,
  293. ACPI_D2,
  294. ACPI_D3
  295. };
  296. /* bits in the acpi masks */
  297. #define ACPI_12MHZ ( 1 << 15)
  298. #define ACPI_24MHZ ( 1 << 14)
  299. #define ACPI_978 ( 1 << 13)
  300. #define ACPI_SPDIF ( 1 << 12)
  301. #define ACPI_GLUE ( 1 << 11)
  302. #define ACPI__10 ( 1 << 10) /* reserved */
  303. #define ACPI_PCIINT ( 1 << 9)
  304. #define ACPI_HV ( 1 << 8) /* hardware volume */
  305. #define ACPI_GPIO ( 1 << 7)
  306. #define ACPI_ASSP ( 1 << 6)
  307. #define ACPI_SB ( 1 << 5) /* sb emul */
  308. #define ACPI_FM ( 1 << 4) /* fm emul */
  309. #define ACPI_RB ( 1 << 3) /* ringbus / aclink */
  310. #define ACPI_MIDI ( 1 << 2)
  311. #define ACPI_GP ( 1 << 1) /* game port */
  312. #define ACPI_WP ( 1 << 0) /* wave processor */
  313. #define ACPI_ALL (0xffff)
  314. #define ACPI_SLEEP (~(ACPI_SPDIF|ACPI_ASSP|ACPI_SB|ACPI_FM| \
  315. ACPI_MIDI|ACPI_GP|ACPI_WP))
  316. #define ACPI_NONE (ACPI__10)
  317. /* these masks indicate which units we care about at
  318. which states */
  319. static u16 acpi_state_mask[] = {
  320. [ACPI_D0] = ACPI_ALL,
  321. [ACPI_D1] = ACPI_SLEEP,
  322. [ACPI_D2] = ACPI_SLEEP,
  323. [ACPI_D3] = ACPI_NONE
  324. };
  325. static char version[] __devinitdata =
  326. KERN_INFO "maestro: version " DRIVER_VERSION " time " __TIME__ " " __DATE__ "\n";
  327. static const unsigned sample_size[] = { 1, 2, 2, 4 };
  328. static const unsigned sample_shift[] = { 0, 1, 1, 2 };
  329. enum card_types_t {
  330. TYPE_MAESTRO,
  331. TYPE_MAESTRO2,
  332. TYPE_MAESTRO2E
  333. };
  334. static const char *card_names[]={
  335. [TYPE_MAESTRO] = "ESS Maestro",
  336. [TYPE_MAESTRO2] = "ESS Maestro 2",
  337. [TYPE_MAESTRO2E] = "ESS Maestro 2E"
  338. };
  339. static int clock_freq[]={
  340. [TYPE_MAESTRO] = (49152000L / 1024L),
  341. [TYPE_MAESTRO2] = (50000000L / 1024L),
  342. [TYPE_MAESTRO2E] = (50000000L / 1024L)
  343. };
  344. static int maestro_notifier(struct notifier_block *nb, unsigned long event, void *buf);
  345. static struct notifier_block maestro_nb = {maestro_notifier, NULL, 0};
  346. /* --------------------------------------------------------------------- */
  347. struct ess_state {
  348. unsigned int magic;
  349. /* FIXME: we probably want submixers in here, but only one record pair */
  350. u8 apu[6]; /* l/r output, l/r intput converters, l/r input apus */
  351. u8 apu_mode[6]; /* Running mode for this APU */
  352. u8 apu_pan[6]; /* Panning setup for this APU */
  353. u32 apu_base[6]; /* base address for this apu */
  354. struct ess_card *card; /* Card info */
  355. /* wave stuff */
  356. unsigned int rateadc, ratedac;
  357. unsigned char fmt, enable;
  358. int index;
  359. /* this locks around the oss state in the driver */
  360. spinlock_t lock;
  361. /* only let 1 be opening at a time */
  362. struct semaphore open_sem;
  363. wait_queue_head_t open_wait;
  364. mode_t open_mode;
  365. /* soundcore stuff */
  366. int dev_audio;
  367. struct dmabuf {
  368. void *rawbuf;
  369. unsigned buforder;
  370. unsigned numfrag;
  371. unsigned fragshift;
  372. /* XXX zab - swptr only in here so that it can be referenced by
  373. clear_advance, as far as I can tell :( */
  374. unsigned hwptr, swptr;
  375. unsigned total_bytes;
  376. int count;
  377. unsigned error; /* over/underrun */
  378. wait_queue_head_t wait;
  379. /* redundant, but makes calculations easier */
  380. unsigned fragsize;
  381. unsigned dmasize;
  382. unsigned fragsamples;
  383. /* OSS stuff */
  384. unsigned mapped:1;
  385. unsigned ready:1; /* our oss buffers are ready to go */
  386. unsigned endcleared:1;
  387. unsigned ossfragshift;
  388. int ossmaxfrags;
  389. unsigned subdivision;
  390. u16 base; /* Offset for ptr */
  391. } dma_dac, dma_adc;
  392. /* pointer to each dsp?s piece of the apu->src buffer page */
  393. void *mixbuf;
  394. };
  395. struct ess_card {
  396. unsigned int magic;
  397. /* We keep maestro cards in a linked list */
  398. struct ess_card *next;
  399. int dev_mixer;
  400. int card_type;
  401. /* as most of this is static,
  402. perhaps it should be a pointer to a global struct */
  403. struct mixer_goo {
  404. int modcnt;
  405. int supported_mixers;
  406. int stereo_mixers;
  407. int record_sources;
  408. /* the caller must guarantee arg sanity before calling these */
  409. /* int (*read_mixer)(struct ess_card *card, int index);*/
  410. void (*write_mixer)(struct ess_card *card,int mixer, unsigned int left,unsigned int right);
  411. int (*recmask_io)(struct ess_card *card,int rw,int mask);
  412. unsigned int mixer_state[SOUND_MIXER_NRDEVICES];
  413. } mix;
  414. int power_regs;
  415. int in_suspend;
  416. wait_queue_head_t suspend_queue;
  417. struct ess_state channels[MAX_DSPS];
  418. u16 maestro_map[NR_IDRS]; /* Register map */
  419. /* we have to store this junk so that we can come back from a
  420. suspend */
  421. u16 apu_map[NR_APUS][NR_APU_REGS]; /* contents of apu regs */
  422. /* this locks around the physical registers on the card */
  423. spinlock_t lock;
  424. /* memory for this card.. wavecache limited :(*/
  425. void *dmapages;
  426. int dmaorder;
  427. /* hardware resources */
  428. struct pci_dev *pcidev;
  429. u32 iobase;
  430. u32 irq;
  431. int bob_freq;
  432. char dsps_open;
  433. int dock_mute_vol;
  434. };
  435. static void set_mixer(struct ess_card *card,unsigned int mixer, unsigned int val );
  436. static unsigned
  437. ld2(unsigned int x)
  438. {
  439. unsigned r = 0;
  440. if (x >= 0x10000) {
  441. x >>= 16;
  442. r += 16;
  443. }
  444. if (x >= 0x100) {
  445. x >>= 8;
  446. r += 8;
  447. }
  448. if (x >= 0x10) {
  449. x >>= 4;
  450. r += 4;
  451. }
  452. if (x >= 4) {
  453. x >>= 2;
  454. r += 2;
  455. }
  456. if (x >= 2)
  457. r++;
  458. return r;
  459. }
  460. /* --------------------------------------------------------------------- */
  461. static void check_suspend(struct ess_card *card);
  462. /* --------------------------------------------------------------------- */
  463. /*
  464. * ESS Maestro AC97 codec programming interface.
  465. */
  466. static void maestro_ac97_set(struct ess_card *card, u8 cmd, u16 val)
  467. {
  468. int io = card->iobase;
  469. int i;
  470. /*
  471. * Wait for the codec bus to be free
  472. */
  473. check_suspend(card);
  474. for(i=0;i<10000;i++)
  475. {
  476. if(!(inb(io+ESS_AC97_INDEX)&1))
  477. break;
  478. }
  479. /*
  480. * Write the bus
  481. */
  482. outw(val, io+ESS_AC97_DATA);
  483. mdelay(1);
  484. outb(cmd, io+ESS_AC97_INDEX);
  485. mdelay(1);
  486. }
  487. static u16 maestro_ac97_get(struct ess_card *card, u8 cmd)
  488. {
  489. int io = card->iobase;
  490. int sanity=10000;
  491. u16 data;
  492. int i;
  493. check_suspend(card);
  494. /*
  495. * Wait for the codec bus to be free
  496. */
  497. for(i=0;i<10000;i++)
  498. {
  499. if(!(inb(io+ESS_AC97_INDEX)&1))
  500. break;
  501. }
  502. outb(cmd|0x80, io+ESS_AC97_INDEX);
  503. mdelay(1);
  504. while(inb(io+ESS_AC97_INDEX)&1)
  505. {
  506. sanity--;
  507. if(!sanity)
  508. {
  509. printk(KERN_ERR "maestro: ac97 codec timeout reading 0x%x.\n",cmd);
  510. return 0;
  511. }
  512. }
  513. data=inw(io+ESS_AC97_DATA);
  514. mdelay(1);
  515. return data;
  516. }
  517. /* OSS interface to the ac97s.. */
  518. #define AC97_STEREO_MASK (SOUND_MASK_VOLUME|\
  519. SOUND_MASK_PCM|SOUND_MASK_LINE|SOUND_MASK_CD|\
  520. SOUND_MASK_VIDEO|SOUND_MASK_LINE1|SOUND_MASK_IGAIN)
  521. #define AC97_SUPPORTED_MASK (AC97_STEREO_MASK | \
  522. SOUND_MASK_BASS|SOUND_MASK_TREBLE|SOUND_MASK_MIC|\
  523. SOUND_MASK_SPEAKER)
  524. #define AC97_RECORD_MASK (SOUND_MASK_MIC|\
  525. SOUND_MASK_CD| SOUND_MASK_VIDEO| SOUND_MASK_LINE1| SOUND_MASK_LINE|\
  526. SOUND_MASK_PHONEIN)
  527. #define supported_mixer(CARD,FOO) ( CARD->mix.supported_mixers & (1<<FOO) )
  528. /* this table has default mixer values for all OSS mixers.
  529. be sure to fill it in if you add oss mixers
  530. to anyone's supported mixer defines */
  531. static unsigned int mixer_defaults[SOUND_MIXER_NRDEVICES] = {
  532. [SOUND_MIXER_VOLUME] = 0x3232,
  533. [SOUND_MIXER_BASS] = 0x3232,
  534. [SOUND_MIXER_TREBLE] = 0x3232,
  535. [SOUND_MIXER_SPEAKER] = 0x3232,
  536. [SOUND_MIXER_MIC] = 0x8000, /* annoying */
  537. [SOUND_MIXER_LINE] = 0x3232,
  538. [SOUND_MIXER_CD] = 0x3232,
  539. [SOUND_MIXER_VIDEO] = 0x3232,
  540. [SOUND_MIXER_LINE1] = 0x3232,
  541. [SOUND_MIXER_PCM] = 0x3232,
  542. [SOUND_MIXER_IGAIN] = 0x3232
  543. };
  544. static struct ac97_mixer_hw {
  545. unsigned char offset;
  546. int scale;
  547. } ac97_hw[SOUND_MIXER_NRDEVICES]= {
  548. [SOUND_MIXER_VOLUME] = {0x02,63},
  549. [SOUND_MIXER_BASS] = {0x08,15},
  550. [SOUND_MIXER_TREBLE] = {0x08,15},
  551. [SOUND_MIXER_SPEAKER] = {0x0a,15},
  552. [SOUND_MIXER_MIC] = {0x0e,31},
  553. [SOUND_MIXER_LINE] = {0x10,31},
  554. [SOUND_MIXER_CD] = {0x12,31},
  555. [SOUND_MIXER_VIDEO] = {0x14,31},
  556. [SOUND_MIXER_LINE1] = {0x16,31},
  557. [SOUND_MIXER_PCM] = {0x18,31},
  558. [SOUND_MIXER_IGAIN] = {0x1c,15}
  559. };
  560. #if 0 /* *shrug* removed simply because we never used it.
  561. feel free to implement again if needed */
  562. /* reads the given OSS mixer from the ac97
  563. the caller must have insured that the ac97 knows
  564. about that given mixer, and should be holding a
  565. spinlock for the card */
  566. static int ac97_read_mixer(struct ess_card *card, int mixer)
  567. {
  568. u16 val;
  569. int ret=0;
  570. struct ac97_mixer_hw *mh = &ac97_hw[mixer];
  571. val = maestro_ac97_get(card, mh->offset);
  572. if(AC97_STEREO_MASK & (1<<mixer)) {
  573. /* nice stereo mixers .. */
  574. int left,right;
  575. left = (val >> 8) & 0x7f;
  576. right = val & 0x7f;
  577. if (mixer == SOUND_MIXER_IGAIN) {
  578. right = (right * 100) / mh->scale;
  579. left = (left * 100) / mh->scale;
  580. } else {
  581. right = 100 - ((right * 100) / mh->scale);
  582. left = 100 - ((left * 100) / mh->scale);
  583. }
  584. ret = left | (right << 8);
  585. } else if (mixer == SOUND_MIXER_SPEAKER) {
  586. ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
  587. } else if (mixer == SOUND_MIXER_MIC) {
  588. ret = 100 - (((val & 0x1f) * 100) / mh->scale);
  589. /* the low bit is optional in the tone sliders and masking
  590. it lets is avoid the 0xf 'bypass'.. */
  591. } else if (mixer == SOUND_MIXER_BASS) {
  592. ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
  593. } else if (mixer == SOUND_MIXER_TREBLE) {
  594. ret = 100 - (((val & 0xe) * 100) / mh->scale);
  595. }
  596. M_printk("read mixer %d (0x%x) %x -> %x\n",mixer,mh->offset,val,ret);
  597. return ret;
  598. }
  599. #endif
  600. /* write the OSS encoded volume to the given OSS encoded mixer,
  601. again caller's job to make sure all is well in arg land,
  602. call with spinlock held */
  603. /* linear scale -> log */
  604. static unsigned char lin2log[101] =
  605. {
  606. 0, 0 , 15 , 23 , 30 , 34 , 38 , 42 , 45 , 47 ,
  607. 50 , 52 , 53 , 55 , 57 , 58 , 60 , 61 , 62 ,
  608. 63 , 65 , 66 , 67 , 68 , 69 , 69 , 70 , 71 ,
  609. 72 , 73 , 73 , 74 , 75 , 75 , 76 , 77 , 77 ,
  610. 78 , 78 , 79 , 80 , 80 , 81 , 81 , 82 , 82 ,
  611. 83 , 83 , 84 , 84 , 84 , 85 , 85 , 86 , 86 ,
  612. 87 , 87 , 87 , 88 , 88 , 88 , 89 , 89 , 89 ,
  613. 90 , 90 , 90 , 91 , 91 , 91 , 92 , 92 , 92 ,
  614. 93 , 93 , 93 , 94 , 94 , 94 , 94 , 95 , 95 ,
  615. 95 , 95 , 96 , 96 , 96 , 96 , 97 , 97 , 97 ,
  616. 97 , 98 , 98 , 98 , 98 , 99 , 99 , 99 , 99 , 99
  617. };
  618. static void ac97_write_mixer(struct ess_card *card,int mixer, unsigned int left, unsigned int right)
  619. {
  620. u16 val=0;
  621. struct ac97_mixer_hw *mh = &ac97_hw[mixer];
  622. M_printk("wrote mixer %d (0x%x) %d,%d",mixer,mh->offset,left,right);
  623. if(AC97_STEREO_MASK & (1<<mixer)) {
  624. /* stereo mixers, mute them if we can */
  625. if (mixer == SOUND_MIXER_IGAIN) {
  626. /* igain's slider is reversed.. */
  627. right = (right * mh->scale) / 100;
  628. left = (left * mh->scale) / 100;
  629. if ((left == 0) && (right == 0))
  630. val |= 0x8000;
  631. } else if (mixer == SOUND_MIXER_PCM || mixer == SOUND_MIXER_CD) {
  632. /* log conversion seems bad for them */
  633. if ((left == 0) && (right == 0))
  634. val = 0x8000;
  635. right = ((100 - right) * mh->scale) / 100;
  636. left = ((100 - left) * mh->scale) / 100;
  637. } else {
  638. /* log conversion for the stereo controls */
  639. if((left == 0) && (right == 0))
  640. val = 0x8000;
  641. right = ((100 - lin2log[right]) * mh->scale) / 100;
  642. left = ((100 - lin2log[left]) * mh->scale) / 100;
  643. }
  644. val |= (left << 8) | right;
  645. } else if (mixer == SOUND_MIXER_SPEAKER) {
  646. val = (((100 - left) * mh->scale) / 100) << 1;
  647. } else if (mixer == SOUND_MIXER_MIC) {
  648. val = maestro_ac97_get(card, mh->offset) & ~0x801f;
  649. val |= (((100 - left) * mh->scale) / 100);
  650. /* the low bit is optional in the tone sliders and masking
  651. it lets is avoid the 0xf 'bypass'.. */
  652. } else if (mixer == SOUND_MIXER_BASS) {
  653. val = maestro_ac97_get(card , mh->offset) & ~0x0f00;
  654. val |= ((((100 - left) * mh->scale) / 100) << 8) & 0x0e00;
  655. } else if (mixer == SOUND_MIXER_TREBLE) {
  656. val = maestro_ac97_get(card , mh->offset) & ~0x000f;
  657. val |= (((100 - left) * mh->scale) / 100) & 0x000e;
  658. }
  659. maestro_ac97_set(card , mh->offset, val);
  660. M_printk(" -> %x\n",val);
  661. }
  662. /* the following tables allow us to go from
  663. OSS <-> ac97 quickly. */
  664. enum ac97_recsettings {
  665. AC97_REC_MIC=0,
  666. AC97_REC_CD,
  667. AC97_REC_VIDEO,
  668. AC97_REC_AUX,
  669. AC97_REC_LINE,
  670. AC97_REC_STEREO, /* combination of all enabled outputs.. */
  671. AC97_REC_MONO, /*.. or the mono equivalent */
  672. AC97_REC_PHONE
  673. };
  674. static unsigned int ac97_oss_mask[] = {
  675. [AC97_REC_MIC] = SOUND_MASK_MIC,
  676. [AC97_REC_CD] = SOUND_MASK_CD,
  677. [AC97_REC_VIDEO] = SOUND_MASK_VIDEO,
  678. [AC97_REC_AUX] = SOUND_MASK_LINE1,
  679. [AC97_REC_LINE] = SOUND_MASK_LINE,
  680. [AC97_REC_PHONE] = SOUND_MASK_PHONEIN
  681. };
  682. /* indexed by bit position */
  683. static unsigned int ac97_oss_rm[] = {
  684. [SOUND_MIXER_MIC] = AC97_REC_MIC,
  685. [SOUND_MIXER_CD] = AC97_REC_CD,
  686. [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
  687. [SOUND_MIXER_LINE1] = AC97_REC_AUX,
  688. [SOUND_MIXER_LINE] = AC97_REC_LINE,
  689. [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
  690. };
  691. /* read or write the recmask
  692. the ac97 can really have left and right recording
  693. inputs independently set, but OSS doesn't seem to
  694. want us to express that to the user.
  695. the caller guarantees that we have a supported bit set,
  696. and they must be holding the card's spinlock */
  697. static int
  698. ac97_recmask_io(struct ess_card *card, int read, int mask)
  699. {
  700. unsigned int val = ac97_oss_mask[ maestro_ac97_get(card, 0x1a) & 0x7 ];
  701. if (read) return val;
  702. /* oss can have many inputs, maestro can't. try
  703. to pick the 'new' one */
  704. if (mask != val) mask &= ~val;
  705. val = ffs(mask) - 1;
  706. val = ac97_oss_rm[val];
  707. val |= val << 8; /* set both channels */
  708. M_printk("maestro: setting ac97 recmask to 0x%x\n",val);
  709. maestro_ac97_set(card,0x1a,val);
  710. return 0;
  711. };
  712. /*
  713. * The Maestro can be wired to a standard AC97 compliant codec
  714. * (see www.intel.com for the pdf's on this), or to a PT101 codec
  715. * which appears to be the ES1918 (data sheet on the esstech.com.tw site)
  716. *
  717. * The PT101 setup is untested.
  718. */
  719. static u16 __init maestro_ac97_init(struct ess_card *card)
  720. {
  721. u16 vend1, vend2, caps;
  722. card->mix.supported_mixers = AC97_SUPPORTED_MASK;
  723. card->mix.stereo_mixers = AC97_STEREO_MASK;
  724. card->mix.record_sources = AC97_RECORD_MASK;
  725. /* card->mix.read_mixer = ac97_read_mixer;*/
  726. card->mix.write_mixer = ac97_write_mixer;
  727. card->mix.recmask_io = ac97_recmask_io;
  728. vend1 = maestro_ac97_get(card, 0x7c);
  729. vend2 = maestro_ac97_get(card, 0x7e);
  730. caps = maestro_ac97_get(card, 0x00);
  731. printk(KERN_INFO "maestro: AC97 Codec detected: v: 0x%2x%2x caps: 0x%x pwr: 0x%x\n",
  732. vend1,vend2,caps,maestro_ac97_get(card,0x26) & 0xf);
  733. if (! (caps & 0x4) ) {
  734. /* no bass/treble nobs */
  735. card->mix.supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
  736. }
  737. /* XXX endianness, dork head. */
  738. /* vendor specifc bits.. */
  739. switch ((long)(vend1 << 16) | vend2) {
  740. case 0x545200ff: /* TriTech */
  741. /* no idea what this does */
  742. maestro_ac97_set(card,0x2a,0x0001);
  743. maestro_ac97_set(card,0x2c,0x0000);
  744. maestro_ac97_set(card,0x2c,0xffff);
  745. break;
  746. #if 0 /* i thought the problems I was seeing were with
  747. the 1921, but apparently they were with the pci board
  748. it was on, so this code is commented out.
  749. lets see if this holds true. */
  750. case 0x83847609: /* ESS 1921 */
  751. /* writing to 0xe (mic) or 0x1a (recmask) seems
  752. to hang this codec */
  753. card->mix.supported_mixers &= ~(SOUND_MASK_MIC);
  754. card->mix.record_sources = 0;
  755. card->mix.recmask_io = NULL;
  756. #if 0 /* don't ask. I have yet to see what these actually do. */
  757. maestro_ac97_set(card,0x76,0xABBA); /* o/~ Take a chance on me o/~ */
  758. udelay(20);
  759. maestro_ac97_set(card,0x78,0x3002);
  760. udelay(20);
  761. maestro_ac97_set(card,0x78,0x3802);
  762. udelay(20);
  763. #endif
  764. break;
  765. #endif
  766. default: break;
  767. }
  768. maestro_ac97_set(card, 0x1E, 0x0404);
  769. /* null misc stuff */
  770. maestro_ac97_set(card, 0x20, 0x0000);
  771. return 0;
  772. }
  773. #if 0 /* there has been 1 person on the planet with a pt101 that we
  774. know of. If they care, they can put this back in :) */
  775. static u16 maestro_pt101_init(struct ess_card *card,int iobase)
  776. {
  777. printk(KERN_INFO "maestro: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
  778. /* who knows.. */
  779. maestro_ac97_set(iobase, 0x2A, 0x0001);
  780. maestro_ac97_set(iobase, 0x2C, 0x0000);
  781. maestro_ac97_set(iobase, 0x2C, 0xFFFF);
  782. maestro_ac97_set(iobase, 0x10, 0x9F1F);
  783. maestro_ac97_set(iobase, 0x12, 0x0808);
  784. maestro_ac97_set(iobase, 0x14, 0x9F1F);
  785. maestro_ac97_set(iobase, 0x16, 0x9F1F);
  786. maestro_ac97_set(iobase, 0x18, 0x0404);
  787. maestro_ac97_set(iobase, 0x1A, 0x0000);
  788. maestro_ac97_set(iobase, 0x1C, 0x0000);
  789. maestro_ac97_set(iobase, 0x02, 0x0404);
  790. maestro_ac97_set(iobase, 0x04, 0x0808);
  791. maestro_ac97_set(iobase, 0x0C, 0x801F);
  792. maestro_ac97_set(iobase, 0x0E, 0x801F);
  793. return 0;
  794. }
  795. #endif
  796. /* this is very magic, and very slow.. */
  797. static void
  798. maestro_ac97_reset(int ioaddr, struct pci_dev *pcidev)
  799. {
  800. u16 save_68;
  801. u16 w;
  802. u32 vend;
  803. outw( inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
  804. outw( inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
  805. outw( inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
  806. /* reset the first codec */
  807. outw(0x0000, ioaddr+0x36);
  808. save_68 = inw(ioaddr+0x68);
  809. pci_read_config_word(pcidev, 0x58, &w); /* something magical with gpio and bus arb. */
  810. pci_read_config_dword(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  811. if( w & 0x1)
  812. save_68 |= 0x10;
  813. outw(0xfffe, ioaddr + 0x64); /* tickly gpio 0.. */
  814. outw(0x0001, ioaddr + 0x68);
  815. outw(0x0000, ioaddr + 0x60);
  816. udelay(20);
  817. outw(0x0001, ioaddr + 0x60);
  818. mdelay(20);
  819. outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
  820. outw( (inw(ioaddr + 0x38) & 0xfffc)|0x1, ioaddr + 0x38);
  821. outw( (inw(ioaddr + 0x3a) & 0xfffc)|0x1, ioaddr + 0x3a);
  822. outw( (inw(ioaddr + 0x3c) & 0xfffc)|0x1, ioaddr + 0x3c);
  823. /* now the second codec */
  824. outw(0x0000, ioaddr+0x36);
  825. outw(0xfff7, ioaddr + 0x64);
  826. save_68 = inw(ioaddr+0x68);
  827. outw(0x0009, ioaddr + 0x68);
  828. outw(0x0001, ioaddr + 0x60);
  829. udelay(20);
  830. outw(0x0009, ioaddr + 0x60);
  831. mdelay(500); /* .. ouch.. */
  832. outw( inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
  833. outw( inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
  834. outw( inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
  835. #if 0 /* the loop here needs to be much better if we want it.. */
  836. M_printk("trying software reset\n");
  837. /* try and do a software reset */
  838. outb(0x80|0x7c, ioaddr + 0x30);
  839. for (w=0; ; w++) {
  840. if ((inw(ioaddr+ 0x30) & 1) == 0) {
  841. if(inb(ioaddr + 0x32) !=0) break;
  842. outb(0x80|0x7d, ioaddr + 0x30);
  843. if (((inw(ioaddr+ 0x30) & 1) == 0) && (inb(ioaddr + 0x32) !=0)) break;
  844. outb(0x80|0x7f, ioaddr + 0x30);
  845. if (((inw(ioaddr+ 0x30) & 1) == 0) && (inb(ioaddr + 0x32) !=0)) break;
  846. }
  847. if( w > 10000) {
  848. outb( inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
  849. mdelay(500); /* oh my.. */
  850. outb( inb(ioaddr + 0x37) & ~0x08, ioaddr + 0x37);
  851. udelay(1);
  852. outw( 0x80, ioaddr+0x30);
  853. for(w = 0 ; w < 10000; w++) {
  854. if((inw(ioaddr + 0x30) & 1) ==0) break;
  855. }
  856. }
  857. }
  858. #endif
  859. if ( vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
  860. /* turn on external amp? */
  861. outw(0xf9ff, ioaddr + 0x64);
  862. outw(inw(ioaddr+0x68) | 0x600, ioaddr + 0x68);
  863. outw(0x0209, ioaddr + 0x60);
  864. }
  865. /* Turn on the 978 docking chip.
  866. First frob the "master output enable" bit,
  867. then set most of the playback volume control registers to max. */
  868. outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
  869. outb(0xff, ioaddr+0xc3);
  870. outb(0xff, ioaddr+0xc4);
  871. outb(0xff, ioaddr+0xc6);
  872. outb(0xff, ioaddr+0xc8);
  873. outb(0x3f, ioaddr+0xcf);
  874. outb(0x3f, ioaddr+0xd0);
  875. }
  876. /*
  877. * Indirect register access. Not all registers are readable so we
  878. * need to keep register state ourselves
  879. */
  880. #define WRITEABLE_MAP 0xEFFFFF
  881. #define READABLE_MAP 0x64003F
  882. /*
  883. * The Maestro engineers were a little indirection happy. These indirected
  884. * registers themselves include indirect registers at another layer
  885. */
  886. static void __maestro_write(struct ess_card *card, u16 reg, u16 data)
  887. {
  888. long ioaddr = card->iobase;
  889. outw(reg, ioaddr+0x02);
  890. outw(data, ioaddr+0x00);
  891. if( reg >= NR_IDRS) printk("maestro: IDR %d out of bounds!\n",reg);
  892. else card->maestro_map[reg]=data;
  893. }
  894. static void maestro_write(struct ess_state *s, u16 reg, u16 data)
  895. {
  896. unsigned long flags;
  897. check_suspend(s->card);
  898. spin_lock_irqsave(&s->card->lock,flags);
  899. __maestro_write(s->card,reg,data);
  900. spin_unlock_irqrestore(&s->card->lock,flags);
  901. }
  902. static u16 __maestro_read(struct ess_card *card, u16 reg)
  903. {
  904. long ioaddr = card->iobase;
  905. outw(reg, ioaddr+0x02);
  906. return card->maestro_map[reg]=inw(ioaddr+0x00);
  907. }
  908. static u16 maestro_read(struct ess_state *s, u16 reg)
  909. {
  910. if(READABLE_MAP & (1<<reg))
  911. {
  912. unsigned long flags;
  913. check_suspend(s->card);
  914. spin_lock_irqsave(&s->card->lock,flags);
  915. __maestro_read(s->card,reg);
  916. spin_unlock_irqrestore(&s->card->lock,flags);
  917. }
  918. return s->card->maestro_map[reg];
  919. }
  920. /*
  921. * These routines handle accessing the second level indirections to the
  922. * wave ram.
  923. */
  924. /*
  925. * The register names are the ones ESS uses (see 104T31.ZIP)
  926. */
  927. #define IDR0_DATA_PORT 0x00
  928. #define IDR1_CRAM_POINTER 0x01
  929. #define IDR2_CRAM_DATA 0x02
  930. #define IDR3_WAVE_DATA 0x03
  931. #define IDR4_WAVE_PTR_LOW 0x04
  932. #define IDR5_WAVE_PTR_HI 0x05
  933. #define IDR6_TIMER_CTRL 0x06
  934. #define IDR7_WAVE_ROMRAM 0x07
  935. static void apu_index_set(struct ess_card *card, u16 index)
  936. {
  937. int i;
  938. __maestro_write(card, IDR1_CRAM_POINTER, index);
  939. for(i=0;i<1000;i++)
  940. if(__maestro_read(card, IDR1_CRAM_POINTER)==index)
  941. return;
  942. printk(KERN_WARNING "maestro: APU register select failed.\n");
  943. }
  944. static void apu_data_set(struct ess_card *card, u16 data)
  945. {
  946. int i;
  947. for(i=0;i<1000;i++)
  948. {
  949. if(__maestro_read(card, IDR0_DATA_PORT)==data)
  950. return;
  951. __maestro_write(card, IDR0_DATA_PORT, data);
  952. }
  953. }
  954. /*
  955. * This is the public interface for APU manipulation. It handles the
  956. * interlock to avoid two APU writes in parallel etc. Don't diddle
  957. * directly with the stuff above.
  958. */
  959. static void apu_set_register(struct ess_state *s, u16 channel, u8 reg, u16 data)
  960. {
  961. unsigned long flags;
  962. check_suspend(s->card);
  963. if(channel&ESS_CHAN_HARD)
  964. channel&=~ESS_CHAN_HARD;
  965. else
  966. {
  967. if(channel>5)
  968. printk("BAD CHANNEL %d.\n",channel);
  969. else
  970. channel = s->apu[channel];
  971. /* store based on real hardware apu/reg */
  972. s->card->apu_map[channel][reg]=data;
  973. }
  974. reg|=(channel<<4);
  975. /* hooray for double indirection!! */
  976. spin_lock_irqsave(&s->card->lock,flags);
  977. apu_index_set(s->card, reg);
  978. apu_data_set(s->card, data);
  979. spin_unlock_irqrestore(&s->card->lock,flags);
  980. }
  981. static u16 apu_get_register(struct ess_state *s, u16 channel, u8 reg)
  982. {
  983. unsigned long flags;
  984. u16 v;
  985. check_suspend(s->card);
  986. if(channel&ESS_CHAN_HARD)
  987. channel&=~ESS_CHAN_HARD;
  988. else
  989. channel = s->apu[channel];
  990. reg|=(channel<<4);
  991. spin_lock_irqsave(&s->card->lock,flags);
  992. apu_index_set(s->card, reg);
  993. v=__maestro_read(s->card, IDR0_DATA_PORT);
  994. spin_unlock_irqrestore(&s->card->lock,flags);
  995. return v;
  996. }
  997. /*
  998. * The wavecache buffers between the APUs and
  999. * pci bus mastering
  1000. */
  1001. static void wave_set_register(struct ess_state *s, u16 reg, u16 value)
  1002. {
  1003. long ioaddr = s->card->iobase;
  1004. unsigned long flags;
  1005. check_suspend(s->card);
  1006. spin_lock_irqsave(&s->card->lock,flags);
  1007. outw(reg, ioaddr+0x10);
  1008. outw(value, ioaddr+0x12);
  1009. spin_unlock_irqrestore(&s->card->lock,flags);
  1010. }
  1011. static u16 wave_get_register(struct ess_state *s, u16 reg)
  1012. {
  1013. long ioaddr = s->card->iobase;
  1014. unsigned long flags;
  1015. u16 value;
  1016. check_suspend(s->card);
  1017. spin_lock_irqsave(&s->card->lock,flags);
  1018. outw(reg, ioaddr+0x10);
  1019. value=inw(ioaddr+0x12);
  1020. spin_unlock_irqrestore(&s->card->lock,flags);
  1021. return value;
  1022. }
  1023. static void sound_reset(int ioaddr)
  1024. {
  1025. outw(0x2000, 0x18+ioaddr);
  1026. udelay(1);
  1027. outw(0x0000, 0x18+ioaddr);
  1028. udelay(1);
  1029. }
  1030. /* sets the play formats of these apus, should be passed the already shifted format */
  1031. static void set_apu_fmt(struct ess_state *s, int apu, int mode)
  1032. {
  1033. int apu_fmt = 0x10;
  1034. if(!(mode&ESS_FMT_16BIT)) apu_fmt+=0x20;
  1035. if((mode&ESS_FMT_STEREO)) apu_fmt+=0x10;
  1036. s->apu_mode[apu] = apu_fmt;
  1037. s->apu_mode[apu+1] = apu_fmt;
  1038. }
  1039. /* this only fixes the output apu mode to be later set by start_dac and
  1040. company. output apu modes are set in ess_rec_setup */
  1041. static void set_fmt(struct ess_state *s, unsigned char mask, unsigned char data)
  1042. {
  1043. s->fmt = (s->fmt & mask) | data;
  1044. set_apu_fmt(s, 0, (s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK);
  1045. }
  1046. /* this is off by a little bit.. */
  1047. static u32 compute_rate(struct ess_state *s, u32 freq)
  1048. {
  1049. u32 clock = clock_freq[s->card->card_type];
  1050. freq = (freq * clocking)/48000;
  1051. if (freq == 48000)
  1052. return 0x10000;
  1053. return ((freq / clock) <<16 )+
  1054. (((freq % clock) << 16) / clock);
  1055. }
  1056. static void set_dac_rate(struct ess_state *s, unsigned int rate)
  1057. {
  1058. u32 freq;
  1059. int fmt = (s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK;
  1060. if (rate > 48000)
  1061. rate = 48000;
  1062. if (rate < 4000)
  1063. rate = 4000;
  1064. s->ratedac = rate;
  1065. if(! (fmt & ESS_FMT_16BIT) && !(fmt & ESS_FMT_STEREO))
  1066. rate >>= 1;
  1067. /* M_printk("computing dac rate %d with mode %d\n",rate,s->fmt);*/
  1068. freq = compute_rate(s, rate);
  1069. /* Load the frequency, turn on 6dB */
  1070. apu_set_register(s, 0, 2,(apu_get_register(s, 0, 2)&0x00FF)|
  1071. ( ((freq&0xFF)<<8)|0x10 ));
  1072. apu_set_register(s, 0, 3, freq>>8);
  1073. apu_set_register(s, 1, 2,(apu_get_register(s, 1, 2)&0x00FF)|
  1074. ( ((freq&0xFF)<<8)|0x10 ));
  1075. apu_set_register(s, 1, 3, freq>>8);
  1076. }
  1077. static void set_adc_rate(struct ess_state *s, unsigned rate)
  1078. {
  1079. u32 freq;
  1080. /* Sample Rate conversion APUs don't like 0x10000 for their rate */
  1081. if (rate > 47999)
  1082. rate = 47999;
  1083. if (rate < 4000)
  1084. rate = 4000;
  1085. s->rateadc = rate;
  1086. freq = compute_rate(s, rate);
  1087. /* Load the frequency, turn on 6dB */
  1088. apu_set_register(s, 2, 2,(apu_get_register(s, 2, 2)&0x00FF)|
  1089. ( ((freq&0xFF)<<8)|0x10 ));
  1090. apu_set_register(s, 2, 3, freq>>8);
  1091. apu_set_register(s, 3, 2,(apu_get_register(s, 3, 2)&0x00FF)|
  1092. ( ((freq&0xFF)<<8)|0x10 ));
  1093. apu_set_register(s, 3, 3, freq>>8);
  1094. /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
  1095. freq = 0x10000;
  1096. apu_set_register(s, 4, 2,(apu_get_register(s, 4, 2)&0x00FF)|
  1097. ( ((freq&0xFF)<<8)|0x10 ));
  1098. apu_set_register(s, 4, 3, freq>>8);
  1099. apu_set_register(s, 5, 2,(apu_get_register(s, 5, 2)&0x00FF)|
  1100. ( ((freq&0xFF)<<8)|0x10 ));
  1101. apu_set_register(s, 5, 3, freq>>8);
  1102. }
  1103. /* Stop our host of recording apus */
  1104. static inline void stop_adc(struct ess_state *s)
  1105. {
  1106. /* XXX lets hope we don't have to lock around this */
  1107. if (! (s->enable & ADC_RUNNING)) return;
  1108. s->enable &= ~ADC_RUNNING;
  1109. apu_set_register(s, 2, 0, apu_get_register(s, 2, 0)&0xFF0F);
  1110. apu_set_register(s, 3, 0, apu_get_register(s, 3, 0)&0xFF0F);
  1111. apu_set_register(s, 4, 0, apu_get_register(s, 2, 0)&0xFF0F);
  1112. apu_set_register(s, 5, 0, apu_get_register(s, 3, 0)&0xFF0F);
  1113. }
  1114. /* stop output apus */
  1115. static void stop_dac(struct ess_state *s)
  1116. {
  1117. /* XXX have to lock around this? */
  1118. if (! (s->enable & DAC_RUNNING)) return;
  1119. s->enable &= ~DAC_RUNNING;
  1120. apu_set_register(s, 0, 0, apu_get_register(s, 0, 0)&0xFF0F);
  1121. apu_set_register(s, 1, 0, apu_get_register(s, 1, 0)&0xFF0F);
  1122. }
  1123. static void start_dac(struct ess_state *s)
  1124. {
  1125. /* XXX locks? */
  1126. if ( (s->dma_dac.mapped || s->dma_dac.count > 0) &&
  1127. s->dma_dac.ready &&
  1128. (! (s->enable & DAC_RUNNING)) ) {
  1129. s->enable |= DAC_RUNNING;
  1130. apu_set_register(s, 0, 0,
  1131. (apu_get_register(s, 0, 0)&0xFF0F)|s->apu_mode[0]);
  1132. if((s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_STEREO)
  1133. apu_set_register(s, 1, 0,
  1134. (apu_get_register(s, 1, 0)&0xFF0F)|s->apu_mode[1]);
  1135. }
  1136. }
  1137. static void start_adc(struct ess_state *s)
  1138. {
  1139. /* XXX locks? */
  1140. if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
  1141. && s->dma_adc.ready && (! (s->enable & ADC_RUNNING)) ) {
  1142. s->enable |= ADC_RUNNING;
  1143. apu_set_register(s, 2, 0,
  1144. (apu_get_register(s, 2, 0)&0xFF0F)|s->apu_mode[2]);
  1145. apu_set_register(s, 4, 0,
  1146. (apu_get_register(s, 4, 0)&0xFF0F)|s->apu_mode[4]);
  1147. if( s->fmt & (ESS_FMT_STEREO << ESS_ADC_SHIFT)) {
  1148. apu_set_register(s, 3, 0,
  1149. (apu_get_register(s, 3, 0)&0xFF0F)|s->apu_mode[3]);
  1150. apu_set_register(s, 5, 0,
  1151. (apu_get_register(s, 5, 0)&0xFF0F)|s->apu_mode[5]);
  1152. }
  1153. }
  1154. }
  1155. /*
  1156. * Native play back driver
  1157. */
  1158. /* the mode passed should be already shifted and masked */
  1159. static void
  1160. ess_play_setup(struct ess_state *ess, int mode, u32 rate, void *buffer, int size)
  1161. {
  1162. u32 pa;
  1163. u32 tmpval;
  1164. int high_apu = 0;
  1165. int channel;
  1166. M_printk("mode=%d rate=%d buf=%p len=%d.\n",
  1167. mode, rate, buffer, size);
  1168. /* all maestro sizes are in 16bit words */
  1169. size >>=1;
  1170. if(mode&ESS_FMT_STEREO) {
  1171. high_apu++;
  1172. /* only 16/stereo gets size divided */
  1173. if(mode&ESS_FMT_16BIT)
  1174. size>>=1;
  1175. }
  1176. for(channel=0; channel <= high_apu; channel++)
  1177. {
  1178. pa = virt_to_bus(buffer);
  1179. /* set the wavecache control reg */
  1180. tmpval = (pa - 0x10) & 0xFFF8;
  1181. if(!(mode & ESS_FMT_16BIT)) tmpval |= 4;
  1182. if(mode & ESS_FMT_STEREO) tmpval |= 2;
  1183. ess->apu_base[channel]=tmpval;
  1184. wave_set_register(ess, ess->apu[channel]<<3, tmpval);
  1185. pa -= virt_to_bus(ess->card->dmapages);
  1186. pa>>=1; /* words */
  1187. /* base offset of dma calcs when reading the pointer
  1188. on the left one */
  1189. if(!channel) ess->dma_dac.base = pa&0xFFFF;
  1190. pa|=0x00400000; /* System RAM */
  1191. /* XXX the 16bit here might not be needed.. */
  1192. if((mode & ESS_FMT_STEREO) && (mode & ESS_FMT_16BIT)) {
  1193. if(channel)
  1194. pa|=0x00800000; /* Stereo */
  1195. pa>>=1;
  1196. }
  1197. /* XXX think about endianess when writing these registers */
  1198. M_printk("maestro: ess_play_setup: APU[%d] pa = 0x%x\n", ess->apu[channel], pa);
  1199. /* start of sample */
  1200. apu_set_register(ess, channel, 4, ((pa>>16)&0xFF)<<8);
  1201. apu_set_register(ess, channel, 5, pa&0xFFFF);
  1202. /* sample end */
  1203. apu_set_register(ess, channel, 6, (pa+size)&0xFFFF);
  1204. /* setting loop len == sample len */
  1205. apu_set_register(ess, channel, 7, size);
  1206. /* clear effects/env.. */
  1207. apu_set_register(ess, channel, 8, 0x0000);
  1208. /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
  1209. apu_set_register(ess, channel, 9, 0xD000);
  1210. /* clear routing stuff */
  1211. apu_set_register(ess, channel, 11, 0x0000);
  1212. /* dma on, no envelopes, filter to all 1s) */
  1213. apu_set_register(ess, channel, 0, 0x400F);
  1214. if(mode&ESS_FMT_16BIT)
  1215. ess->apu_mode[channel]=0x10;
  1216. else
  1217. ess->apu_mode[channel]=0x30;
  1218. if(mode&ESS_FMT_STEREO) {
  1219. /* set panning: left or right */
  1220. apu_set_register(ess, channel, 10, 0x8F00 | (channel ? 0 : 0x10));
  1221. ess->apu_mode[channel] += 0x10;
  1222. } else
  1223. apu_set_register(ess, channel, 10, 0x8F08);
  1224. }
  1225. /* clear WP interrupts */
  1226. outw(1, ess->card->iobase+0x04);
  1227. /* enable WP ints */
  1228. outw(inw(ess->card->iobase+0x18)|4, ess->card->iobase+0x18);
  1229. /* go team! */
  1230. set_dac_rate(ess,rate);
  1231. start_dac(ess);
  1232. }
  1233. /*
  1234. * Native record driver
  1235. */
  1236. /* again, passed mode is alrady shifted/masked */
  1237. static void
  1238. ess_rec_setup(struct ess_state *ess, int mode, u32 rate, void *buffer, int size)
  1239. {
  1240. int apu_step = 2;
  1241. int channel;
  1242. M_printk("maestro: ess_rec_setup: mode=%d rate=%d buf=0x%p len=%d.\n",
  1243. mode, rate, buffer, size);
  1244. /* all maestro sizes are in 16bit words */
  1245. size >>=1;
  1246. /* we're given the full size of the buffer, but
  1247. in stereo each channel will only use its half */
  1248. if(mode&ESS_FMT_STEREO) {
  1249. size >>=1;
  1250. apu_step = 1;
  1251. }
  1252. /* APU assignments: 2 = mono/left SRC
  1253. 3 = right SRC
  1254. 4 = mono/left Input Mixer
  1255. 5 = right Input Mixer */
  1256. for(channel=2;channel<6;channel+=apu_step)
  1257. {
  1258. int i;
  1259. int bsize, route;
  1260. u32 pa;
  1261. u32 tmpval;
  1262. /* data seems to flow from the codec, through an apu into
  1263. the 'mixbuf' bit of page, then through the SRC apu
  1264. and out to the real 'buffer'. ok. sure. */
  1265. if(channel & 0x04) {
  1266. /* ok, we're an input mixer going from adc
  1267. through the mixbuf to the other apus */
  1268. if(!(channel & 0x01)) {
  1269. pa = virt_to_bus(ess->mixbuf);
  1270. } else {
  1271. pa = virt_to_bus(ess->mixbuf + (PAGE_SIZE >> 4));
  1272. }
  1273. /* we source from a 'magic' apu */
  1274. bsize = PAGE_SIZE >> 5; /* half of this channels alloc, in words */
  1275. route = 0x14 + (channel - 4); /* parallel in crap, see maestro reg 0xC [8-11] */
  1276. ess->apu_mode[channel] = 0x90; /* Input Mixer */
  1277. } else {
  1278. /* we're a rate converter taking
  1279. input from the input apus and outputing it to
  1280. system memory */
  1281. if(!(channel & 0x01)) {
  1282. pa = virt_to_bus(buffer);
  1283. } else {
  1284. /* right channel records its split half.
  1285. *2 accommodates for rampant shifting earlier */
  1286. pa = virt_to_bus(buffer + size*2);
  1287. }
  1288. ess->apu_mode[channel] = 0xB0; /* Sample Rate Converter */
  1289. bsize = size;
  1290. /* get input from inputing apu */
  1291. route = channel + 2;
  1292. }
  1293. M_printk("maestro: ess_rec_setup: getting pa 0x%x from %d\n",pa,channel);
  1294. /* set the wavecache control reg */
  1295. tmpval = (pa - 0x10) & 0xFFF8;
  1296. ess->apu_base[channel]=tmpval;
  1297. wave_set_register(ess, ess->apu[channel]<<3, tmpval);
  1298. pa -= virt_to_bus(ess->card->dmapages);
  1299. pa>>=1; /* words */
  1300. /* base offset of dma calcs when reading the pointer
  1301. on this left one */
  1302. if(channel==2) ess->dma_adc.base = pa&0xFFFF;
  1303. pa|=0x00400000; /* bit 22 -> System RAM */
  1304. M_printk("maestro: ess_rec_setup: APU[%d] pa = 0x%x size = 0x%x route = 0x%x\n",
  1305. ess->apu[channel], pa, bsize, route);
  1306. /* Begin loading the APU */
  1307. for(i=0;i<15;i++) /* clear all PBRs */
  1308. apu_set_register(ess, channel, i, 0x0000);
  1309. apu_set_register(ess, channel, 0, 0x400F);
  1310. /* need to enable subgroups.. and we should probably
  1311. have different groups for different /dev/dsps.. */
  1312. apu_set_register(ess, channel, 2, 0x8);
  1313. /* Load the buffer into the wave engine */
  1314. apu_set_register(ess, channel, 4, ((pa>>16)&0xFF)<<8);
  1315. /* XXX reg is little endian.. */
  1316. apu_set_register(ess, channel, 5, pa&0xFFFF);
  1317. apu_set_register(ess, channel, 6, (pa+bsize)&0xFFFF);
  1318. apu_set_register(ess, channel, 7, bsize);
  1319. /* clear effects/env.. */
  1320. apu_set_register(ess, channel, 8, 0x00F0);
  1321. /* amplitude now? sure. why not. */
  1322. apu_set_register(ess, channel, 9, 0x0000);
  1323. /* set filter tune, radius, polar pan */
  1324. apu_set_register(ess, channel, 10, 0x8F08);
  1325. /* route input */
  1326. apu_set_register(ess, channel, 11, route);
  1327. }
  1328. /* clear WP interrupts */
  1329. outw(1, ess->card->iobase+0x04);
  1330. /* enable WP ints */
  1331. outw(inw(ess->card->iobase+0x18)|4, ess->card->iobase+0x18);
  1332. /* let 'er rip */
  1333. set_adc_rate(ess,rate);
  1334. start_adc(ess);
  1335. }
  1336. /* --------------------------------------------------------------------- */
  1337. static void set_dmaa(struct ess_state *s, unsigned int addr, unsigned int count)
  1338. {
  1339. M_printk("set_dmaa??\n");
  1340. }
  1341. static void set_dmac(struct ess_state *s, unsigned int addr, unsigned int count)
  1342. {
  1343. M_printk("set_dmac??\n");
  1344. }
  1345. /* Playback pointer */
  1346. static inline unsigned get_dmaa(struct ess_state *s)
  1347. {
  1348. int offset;
  1349. offset = apu_get_register(s,0,5);
  1350. /* M_printk("dmaa: offset: %d, base: %d\n",offset,s->dma_dac.base); */
  1351. offset-=s->dma_dac.base;
  1352. return (offset&0xFFFE)<<1; /* hardware is in words */
  1353. }
  1354. /* Record pointer */
  1355. static inline unsigned get_dmac(struct ess_state *s)
  1356. {
  1357. int offset;
  1358. offset = apu_get_register(s,2,5);
  1359. /* M_printk("dmac: offset: %d, base: %d\n",offset,s->dma_adc.base); */
  1360. /* The offset is an address not a position relative to base */
  1361. offset-=s->dma_adc.base;
  1362. return (offset&0xFFFE)<<1; /* hardware is in words */
  1363. }
  1364. /*
  1365. * Meet Bob, the timer...
  1366. */
  1367. static irqreturn_t ess_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  1368. static void stop_bob(struct ess_state *s)
  1369. {
  1370. /* Mask IDR 11,17 */
  1371. maestro_write(s, 0x11, maestro_read(s, 0x11)&~1);
  1372. maestro_write(s, 0x17, maestro_read(s, 0x17)&~1);
  1373. }
  1374. /* eventually we could be clever and limit bob ints
  1375. to the frequency at which our smallest duration
  1376. chunks may expire */
  1377. #define ESS_SYSCLK 50000000
  1378. static void start_bob(struct ess_state *s)
  1379. {
  1380. int prescale;
  1381. int divide;
  1382. /* XXX make freq selector much smarter, see calc_bob_rate */
  1383. int freq = 200;
  1384. /* compute ideal interrupt frequency for buffer size & play rate */
  1385. /* first, find best prescaler value to match freq */
  1386. for(prescale=5;prescale<12;prescale++)
  1387. if(freq > (ESS_SYSCLK>>(prescale+9)))
  1388. break;
  1389. /* next, back off prescaler whilst getting divider into optimum range */
  1390. divide=1;
  1391. while((prescale > 5) && (divide<32))
  1392. {
  1393. prescale--;
  1394. divide <<=1;
  1395. }
  1396. divide>>=1;
  1397. /* now fine-tune the divider for best match */
  1398. for(;divide<31;divide++)
  1399. if(freq >= ((ESS_SYSCLK>>(prescale+9))/(divide+1)))
  1400. break;
  1401. /* divide = 0 is illegal, but don't let prescale = 4! */
  1402. if(divide == 0)
  1403. {
  1404. divide++;
  1405. if(prescale>5)
  1406. prescale--;
  1407. }
  1408. maestro_write(s, 6, 0x9000 | (prescale<<5) | divide); /* set reg */
  1409. /* Now set IDR 11/17 */
  1410. maestro_write(s, 0x11, maestro_read(s, 0x11)|1);
  1411. maestro_write(s, 0x17, maestro_read(s, 0x17)|1);
  1412. }
  1413. /* --------------------------------------------------------------------- */
  1414. /* this quickly calculates the frequency needed for bob
  1415. and sets it if its different than what bob is
  1416. currently running at. its called often so
  1417. needs to be fairly quick. */
  1418. #define BOB_MIN 50
  1419. #define BOB_MAX 400
  1420. static void calc_bob_rate(struct ess_state *s) {
  1421. #if 0 /* this thing tries to set the frequency of bob such that
  1422. there are 2 interrupts / buffer walked by the dac/adc. That
  1423. is probably very wrong for people who actually care about
  1424. mid buffer positioning. it should be calculated as bytes/interrupt
  1425. and that needs to be decided :) so for now just use the static 150
  1426. in start_bob.*/
  1427. unsigned int dac_rate=2,adc_rate=1,newrate;
  1428. static int israte=-1;
  1429. if (s->dma_dac.fragsize == 0) dac_rate = BOB_MIN;
  1430. else {
  1431. dac_rate = (2 * s->ratedac * sample_size[(s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK]) /
  1432. (s->dma_dac.fragsize) ;
  1433. }
  1434. if (s->dma_adc.fragsize == 0) adc_rate = BOB_MIN;
  1435. else {
  1436. adc_rate = (2 * s->rateadc * sample_size[(s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK]) /
  1437. (s->dma_adc.fragsize) ;
  1438. }
  1439. if(dac_rate > adc_rate) newrate = adc_rate;
  1440. else newrate=dac_rate;
  1441. if(newrate > BOB_MAX) newrate = BOB_MAX;
  1442. else {
  1443. if(newrate < BOB_MIN)
  1444. newrate = BOB_MIN;
  1445. }
  1446. if( israte != newrate) {
  1447. printk("dac: %d adc: %d rate: %d\n",dac_rate,adc_rate,israte);
  1448. israte=newrate;
  1449. }
  1450. #endif
  1451. }
  1452. static int
  1453. prog_dmabuf(struct ess_state *s, unsigned rec)
  1454. {
  1455. struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
  1456. unsigned rate = rec ? s->rateadc : s->ratedac;
  1457. unsigned bytepersec;
  1458. unsigned bufs;
  1459. unsigned char fmt;
  1460. unsigned long flags;
  1461. spin_lock_irqsave(&s->lock, flags);
  1462. fmt = s->fmt;
  1463. if (rec) {
  1464. stop_adc(s);
  1465. fmt >>= ESS_ADC_SHIFT;
  1466. } else {
  1467. stop_dac(s);
  1468. fmt >>= ESS_DAC_SHIFT;
  1469. }
  1470. spin_unlock_irqrestore(&s->lock, flags);
  1471. fmt &= ESS_FMT_MASK;
  1472. db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
  1473. /* this algorithm is a little nuts.. where did /1000 come from? */
  1474. bytepersec = rate << sample_shift[fmt];
  1475. bufs = PAGE_SIZE << db->buforder;
  1476. if (db->ossfragshift) {
  1477. if ((1000 << db->ossfragshift) < bytepersec)
  1478. db->fragshift = ld2(bytepersec/1000);
  1479. else
  1480. db->fragshift = db->ossfragshift;
  1481. } else {
  1482. db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
  1483. if (db->fragshift < 3)
  1484. db->fragshift = 3;
  1485. }
  1486. db->numfrag = bufs >> db->fragshift;
  1487. while (db->numfrag < 4 && db->fragshift > 3) {
  1488. db->fragshift--;
  1489. db->numfrag = bufs >> db->fragshift;
  1490. }
  1491. db->fragsize = 1 << db->fragshift;
  1492. if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
  1493. db->numfrag = db->ossmaxfrags;
  1494. db->fragsamples = db->fragsize >> sample_shift[fmt];
  1495. db->dmasize = db->numfrag << db->fragshift;
  1496. M_printk("maestro: setup oss: numfrag: %d fragsize: %d dmasize: %d\n",db->numfrag,db->fragsize,db->dmasize);
  1497. memset(db->rawbuf, (fmt & ESS_FMT_16BIT) ? 0 : 0x80, db->dmasize);
  1498. spin_lock_irqsave(&s->lock, flags);
  1499. if (rec)
  1500. ess_rec_setup(s, fmt, s->rateadc, db->rawbuf, db->dmasize);
  1501. else
  1502. ess_play_setup(s, fmt, s->ratedac, db->rawbuf, db->dmasize);
  1503. spin_unlock_irqrestore(&s->lock, flags);
  1504. db->ready = 1;
  1505. return 0;
  1506. }
  1507. static __inline__ void
  1508. clear_advance(struct ess_state *s)
  1509. {
  1510. unsigned char c = ((s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_16BIT) ? 0 : 0x80;
  1511. unsigned char *buf = s->dma_dac.rawbuf;
  1512. unsigned bsize = s->dma_dac.dmasize;
  1513. unsigned bptr = s->dma_dac.swptr;
  1514. unsigned len = s->dma_dac.fragsize;
  1515. if (bptr + len > bsize) {
  1516. unsigned x = bsize - bptr;
  1517. memset(buf + bptr, c, x);
  1518. /* account for wrapping? */
  1519. bptr = 0;
  1520. len -= x;
  1521. }
  1522. memset(buf + bptr, c, len);
  1523. }
  1524. /* call with spinlock held! */
  1525. static void
  1526. ess_update_ptr(struct ess_state *s)
  1527. {
  1528. unsigned hwptr;
  1529. int diff;
  1530. /* update ADC pointer */
  1531. if (s->dma_adc.ready) {
  1532. /* oh boy should this all be re-written. everything in the current code paths think
  1533. that the various counters/pointers are expressed in bytes to the user but we have
  1534. two apus doing stereo stuff so we fix it up here.. it propagates to all the various
  1535. counters from here. */
  1536. if ( s->fmt & (ESS_FMT_STEREO << ESS_ADC_SHIFT)) {
  1537. hwptr = (get_dmac(s)*2) % s->dma_adc.dmasize;
  1538. } else {
  1539. hwptr = get_dmac(s) % s->dma_adc.dmasize;
  1540. }
  1541. diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
  1542. s->dma_adc.hwptr = hwptr;
  1543. s->dma_adc.total_bytes += diff;
  1544. s->dma_adc.count += diff;
  1545. if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
  1546. wake_up(&s->dma_adc.wait);
  1547. if (!s->dma_adc.mapped) {
  1548. if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
  1549. /* FILL ME
  1550. wrindir(s, SV_CIENABLE, s->enable); */
  1551. stop_adc(s);
  1552. /* brute force everyone back in sync, sigh */
  1553. s->dma_adc.count = 0;
  1554. s->dma_adc.swptr = 0;
  1555. s->dma_adc.hwptr = 0;
  1556. s->dma_adc.error++;
  1557. }
  1558. }
  1559. }
  1560. /* update DAC pointer */
  1561. if (s->dma_dac.ready) {
  1562. hwptr = get_dmaa(s) % s->dma_dac.dmasize;
  1563. /* the apu only reports the length it has seen, not the
  1564. length of the memory that has been used (the WP
  1565. knows that) */
  1566. if ( ((s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK) == (ESS_FMT_STEREO|ESS_FMT_16BIT))
  1567. hwptr<<=1;
  1568. diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
  1569. /* M_printk("updating dac: hwptr: %d diff: %d\n",hwptr,diff);*/
  1570. s->dma_dac.hwptr = hwptr;
  1571. s->dma_dac.total_bytes += diff;
  1572. if (s->dma_dac.mapped) {
  1573. s->dma_dac.count += diff;
  1574. if (s->dma_dac.count >= (signed)s->dma_dac.fragsize) {
  1575. wake_up(&s->dma_dac.wait);
  1576. }
  1577. } else {
  1578. s->dma_dac.count -= diff;
  1579. /* M_printk("maestro: ess_update_ptr: diff: %d, count: %d\n", diff, s->dma_dac.count); */
  1580. if (s->dma_dac.count <= 0) {
  1581. M_printk("underflow! diff: %d count: %d hw: %d sw: %d\n", diff, s->dma_dac.count,
  1582. hwptr, s->dma_dac.swptr);
  1583. /* FILL ME
  1584. wrindir(s, SV_CIENABLE, s->enable); */
  1585. /* XXX how on earth can calling this with the lock held work.. */
  1586. stop_dac(s);
  1587. /* brute force everyone back in sync, sigh */
  1588. s->dma_dac.count = 0;
  1589. s->dma_dac.swptr = hwptr;
  1590. s->dma_dac.error++;
  1591. } else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
  1592. clear_advance(s);
  1593. s->dma_dac.endcleared = 1;
  1594. }
  1595. if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize) {
  1596. wake_up(&s->dma_dac.wait);
  1597. /* printk("waking up DAC count: %d sw: %d hw: %d\n",s->dma_dac.count, s->dma_dac.swptr,
  1598. hwptr);*/
  1599. }
  1600. }
  1601. }
  1602. }
  1603. static irqreturn_t
  1604. ess_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1605. {
  1606. struct ess_state *s;
  1607. struct ess_card *c = (struct ess_card *)dev_id;
  1608. int i;
  1609. u32 event;
  1610. if ( ! (event = inb(c->iobase+0x1A)) )
  1611. return IRQ_NONE;
  1612. outw(inw(c->iobase+4)&1, c->iobase+4);
  1613. /* M_printk("maestro int: %x\n",event);*/
  1614. if(event&(1<<6))
  1615. {
  1616. int x;
  1617. enum {UP_EVT, DOWN_EVT, MUTE_EVT} vol_evt;
  1618. int volume;
  1619. /* Figure out which volume control button was pushed,
  1620. based on differences from the default register
  1621. values. */
  1622. x = inb(c->iobase+0x1c);
  1623. if (x&1) vol_evt = MUTE_EVT;
  1624. else if (((x>>1)&7) > 4) vol_evt = UP_EVT;
  1625. else vol_evt = DOWN_EVT;
  1626. /* Reset the volume control registers. */
  1627. outb(0x88, c->iobase+0x1c);
  1628. outb(0x88, c->iobase+0x1d);
  1629. outb(0x88, c->iobase+0x1e);
  1630. outb(0x88, c->iobase+0x1f);
  1631. /* Deal with the button press in a hammer-handed
  1632. manner by adjusting the master mixer volume. */
  1633. volume = c->mix.mixer_state[0] & 0xff;
  1634. if (vol_evt == UP_EVT) {
  1635. volume += 5;
  1636. if (volume > 100)
  1637. volume = 100;
  1638. }
  1639. else if (vol_evt == DOWN_EVT) {
  1640. volume -= 5;
  1641. if (volume < 0)
  1642. volume = 0;
  1643. } else {
  1644. /* vol_evt == MUTE_EVT */
  1645. if (volume == 0)
  1646. volume = c->dock_mute_vol;
  1647. else {
  1648. c->dock_mute_vol = volume;
  1649. volume = 0;
  1650. }
  1651. }
  1652. set_mixer (c, 0, (volume << 8) | volume);
  1653. }
  1654. /* Ack all the interrupts. */
  1655. outb(0xFF, c->iobase+0x1A);
  1656. /*
  1657. * Update the pointers for all APU's we are running.
  1658. */
  1659. for(i=0;i<NR_DSPS;i++)
  1660. {
  1661. s=&c->channels[i];
  1662. if(s->dev_audio == -1)
  1663. break;
  1664. spin_lock(&s->lock);
  1665. ess_update_ptr(s);
  1666. spin_unlock(&s->lock);
  1667. }
  1668. return IRQ_HANDLED;
  1669. }
  1670. /* --------------------------------------------------------------------- */
  1671. static const char invalid_magic[] = KERN_CRIT "maestro: invalid magic value in %s\n";
  1672. #define VALIDATE_MAGIC(FOO,MAG) \
  1673. ({ \
  1674. if (!(FOO) || (FOO)->magic != MAG) { \
  1675. printk(invalid_magic,__FUNCTION__); \
  1676. return -ENXIO; \
  1677. } \
  1678. })
  1679. #define VALIDATE_STATE(a) VALIDATE_MAGIC(a,ESS_STATE_MAGIC)
  1680. #define VALIDATE_CARD(a) VALIDATE_MAGIC(a,ESS_CARD_MAGIC)
  1681. static void set_mixer(struct ess_card *card,unsigned int mixer, unsigned int val )
  1682. {
  1683. unsigned int left,right;
  1684. /* cleanse input a little */
  1685. right = ((val >> 8) & 0xff) ;
  1686. left = (val & 0xff) ;
  1687. if(right > 100) right = 100;
  1688. if(left > 100) left = 100;
  1689. card->mix.mixer_state[mixer]=(right << 8) | left;
  1690. card->mix.write_mixer(card,mixer,left,right);
  1691. }
  1692. static void
  1693. mixer_push_state(struct ess_card *card)
  1694. {
  1695. int i;
  1696. for(i = 0 ; i < SOUND_MIXER_NRDEVICES ; i++) {
  1697. if( ! supported_mixer(card,i)) continue;
  1698. set_mixer(card,i,card->mix.mixer_state[i]);
  1699. }
  1700. }
  1701. static int mixer_ioctl(struct ess_card *card, unsigned int cmd, unsigned long arg)
  1702. {
  1703. int i, val=0;
  1704. unsigned long flags;
  1705. void __user *argp = (void __user *)arg;
  1706. int __user *p = argp;
  1707. VALIDATE_CARD(card);
  1708. if (cmd == SOUND_MIXER_INFO) {
  1709. mixer_info info;
  1710. memset(&info, 0, sizeof(info));
  1711. strlcpy(info.id, card_names[card->card_type], sizeof(info.id));
  1712. strlcpy(info.name, card_names[card->card_type], sizeof(info.name));
  1713. info.modify_counter = card->mix.modcnt;
  1714. if (copy_to_user(argp, &info, sizeof(info)))
  1715. return -EFAULT;
  1716. return 0;
  1717. }
  1718. if (cmd == SOUND_OLD_MIXER_INFO) {
  1719. _old_mixer_info info;
  1720. memset(&info, 0, sizeof(info));
  1721. strlcpy(info.id, card_names[card->card_type], sizeof(info.id));
  1722. strlcpy(info.name, card_names[card->card_type], sizeof(info.name));
  1723. if (copy_to_user(argp, &info, sizeof(info)))
  1724. return -EFAULT;
  1725. return 0;
  1726. }
  1727. if (cmd == OSS_GETVERSION)
  1728. return put_user(SOUND_VERSION, p);
  1729. if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
  1730. return -EINVAL;
  1731. if (_IOC_DIR(cmd) == _IOC_READ) {
  1732. switch (_IOC_NR(cmd)) {
  1733. case SOUND_MIXER_RECSRC: /* give them the current record source */
  1734. if(!card->mix.recmask_io) {
  1735. val = 0;
  1736. } else {
  1737. spin_lock_irqsave(&card->lock, flags);
  1738. val = card->mix.recmask_io(card,1,0);
  1739. spin_unlock_irqrestore(&card->lock, flags);
  1740. }
  1741. break;
  1742. case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
  1743. val = card->mix.supported_mixers;
  1744. break;
  1745. case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
  1746. val = card->mix.record_sources;
  1747. break;
  1748. case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
  1749. val = card->mix.stereo_mixers;
  1750. break;
  1751. case SOUND_MIXER_CAPS:
  1752. val = SOUND_CAP_EXCL_INPUT;
  1753. break;
  1754. default: /* read a specific mixer */
  1755. i = _IOC_NR(cmd);
  1756. if ( ! supported_mixer(card,i))
  1757. return -EINVAL;
  1758. /* do we ever want to touch the hardware? */
  1759. /* spin_lock_irqsave(&card->lock, flags);
  1760. val = card->mix.read_mixer(card,i);
  1761. spin_unlock_irqrestore(&card->lock, flags);*/
  1762. val = card->mix.mixer_state[i];
  1763. /* M_printk("returned 0x%x for mixer %d\n",val,i);*/
  1764. break;
  1765. }
  1766. return put_user(val, p);
  1767. }
  1768. if (_IOC_DIR(cmd) != (_IOC_WRITE|_IOC_READ))
  1769. return -EINVAL;
  1770. card->mix.modcnt++;
  1771. if (get_user(val, p))
  1772. return -EFAULT;
  1773. switch (_IOC_NR(cmd)) {
  1774. case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
  1775. if (!card->mix.recmask_io) return -EINVAL;
  1776. if(!val) return 0;
  1777. if(! (val &= card->mix.record_sources)) return -EINVAL;
  1778. spin_lock_irqsave(&card->lock, flags);
  1779. card->mix.recmask_io(card,0,val);
  1780. spin_unlock_irqrestore(&card->lock, flags);
  1781. return 0;
  1782. default:
  1783. i = _IOC_NR(cmd);
  1784. if ( ! supported_mixer(card,i))
  1785. return -EINVAL;
  1786. spin_lock_irqsave(&card->lock, flags);
  1787. set_mixer(card,i,val);
  1788. spin_unlock_irqrestore(&card->lock, flags);
  1789. return 0;
  1790. }
  1791. }
  1792. /* --------------------------------------------------------------------- */
  1793. static int ess_open_mixdev(struct inode *inode, struct file *file)
  1794. {
  1795. unsigned int minor = iminor(inode);
  1796. struct ess_card *card = NULL;
  1797. struct pci_dev *pdev = NULL;
  1798. struct pci_driver *drvr;
  1799. while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
  1800. drvr = pci_dev_driver (pdev);
  1801. if (drvr == &maestro_pci_driver) {
  1802. card = (struct ess_card*)pci_get_drvdata (pdev);
  1803. if (!card)
  1804. continue;
  1805. if (card->dev_mixer == minor)
  1806. break;
  1807. }
  1808. }
  1809. if (!card)
  1810. return -ENODEV;
  1811. file->private_data = card;
  1812. return nonseekable_open(inode, file);
  1813. }
  1814. static int ess_release_mixdev(struct inode *inode, struct file *file)
  1815. {
  1816. struct ess_card *card = (struct ess_card *)file->private_data;
  1817. VALIDATE_CARD(card);
  1818. return 0;
  1819. }
  1820. static int ess_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1821. {
  1822. struct ess_card *card = (struct ess_card *)file->private_data;
  1823. VALIDATE_CARD(card);
  1824. return mixer_ioctl(card, cmd, arg);
  1825. }
  1826. static /*const*/ struct file_operations ess_mixer_fops = {
  1827. .owner = THIS_MODULE,
  1828. .llseek = no_llseek,
  1829. .ioctl = ess_ioctl_mixdev,
  1830. .open = ess_open_mixdev,
  1831. .release = ess_release_mixdev,
  1832. };
  1833. /* --------------------------------------------------------------------- */
  1834. static int drain_dac(struct ess_state *s, int nonblock)
  1835. {
  1836. DECLARE_WAITQUEUE(wait,current);
  1837. unsigned long flags;
  1838. int count;
  1839. signed long tmo;
  1840. if (s->dma_dac.mapped || !s->dma_dac.ready)
  1841. return 0;
  1842. current->state = TASK_INTERRUPTIBLE;
  1843. add_wait_queue(&s->dma_dac.wait, &wait);
  1844. for (;;) {
  1845. /* XXX uhm.. questionable locking*/
  1846. spin_lock_irqsave(&s->lock, flags);
  1847. count = s->dma_dac.count;
  1848. spin_unlock_irqrestore(&s->lock, flags);
  1849. if (count <= 0)
  1850. break;
  1851. if (signal_pending(current))
  1852. break;
  1853. if (nonblock) {
  1854. remove_wait_queue(&s->dma_dac.wait, &wait);
  1855. current->state = TASK_RUNNING;
  1856. return -EBUSY;
  1857. }
  1858. tmo = (count * HZ) / s->ratedac;
  1859. tmo >>= sample_shift[(s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK];
  1860. /* XXX this is just broken. someone is waking us up alot, or schedule_timeout is broken.
  1861. or something. who cares. - zach */
  1862. if (!schedule_timeout(tmo ? tmo : 1) && tmo)
  1863. M_printk(KERN_DEBUG "maestro: dma timed out?? %ld\n",jiffies);
  1864. }
  1865. remove_wait_queue(&s->dma_dac.wait, &wait);
  1866. current->state = TASK_RUNNING;
  1867. if (signal_pending(current))
  1868. return -ERESTARTSYS;
  1869. return 0;
  1870. }
  1871. /* --------------------------------------------------------------------- */
  1872. /* Zach sez: "god this is gross.." */
  1873. static int
  1874. comb_stereo(unsigned char *real_buffer,unsigned char *tmp_buffer, int offset,
  1875. int count, int bufsize)
  1876. {
  1877. /* No such thing as stereo recording, so we
  1878. use dual input mixers. which means we have to
  1879. combine mono to stereo buffer. yuck.
  1880. but we don't have to be able to work a byte at a time..*/
  1881. unsigned char *so,*left,*right;
  1882. int i;
  1883. so = tmp_buffer;
  1884. left = real_buffer + offset;
  1885. right = real_buffer + bufsize/2 + offset;
  1886. /* M_printk("comb_stereo writing %d to %p from %p and %p, offset: %d size: %d\n",count/2, tmp_buffer,left,right,offset,bufsize);*/
  1887. for(i=count/4; i ; i--) {
  1888. (*(so+2)) = *(right++);
  1889. (*(so+3)) = *(right++);
  1890. (*so) = *(left++);
  1891. (*(so+1)) = *(left++);
  1892. so+=4;
  1893. }
  1894. return 0;
  1895. }
  1896. /* in this loop, dma_adc.count signifies the amount of data thats waiting
  1897. to be copied to the user's buffer. it is filled by the interrupt
  1898. handler and drained by this loop. */
  1899. static ssize_t
  1900. ess_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  1901. {
  1902. struct ess_state *s = (struct ess_state *)file->private_data;
  1903. ssize_t ret;
  1904. unsigned long flags;
  1905. unsigned swptr;
  1906. int cnt;
  1907. unsigned char *combbuf = NULL;
  1908. VALIDATE_STATE(s);
  1909. if (s->dma_adc.mapped)
  1910. return -ENXIO;
  1911. if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
  1912. return ret;
  1913. if (!access_ok(VERIFY_WRITE, buffer, count))
  1914. return -EFAULT;
  1915. if(!(combbuf = kmalloc(count,GFP_KERNEL)))
  1916. return -ENOMEM;
  1917. ret = 0;
  1918. calc_bob_rate(s);
  1919. while (count > 0) {
  1920. spin_lock_irqsave(&s->lock, flags);
  1921. /* remember, all these things are expressed in bytes to be
  1922. sent to the user.. hence the evil / 2 down below */
  1923. swptr = s->dma_adc.swptr;
  1924. cnt = s->dma_adc.dmasize-swptr;
  1925. if (s->dma_adc.count < cnt)
  1926. cnt = s->dma_adc.count;
  1927. spin_unlock_irqrestore(&s->lock, flags);
  1928. if (cnt > count)
  1929. cnt = count;
  1930. if ( cnt > 0 ) cnt &= ~3;
  1931. if (cnt <= 0) {
  1932. start_adc(s);
  1933. if (file->f_flags & O_NONBLOCK)
  1934. {
  1935. ret = ret ? ret : -EAGAIN;
  1936. goto rec_return_free;
  1937. }
  1938. if (!interruptible_sleep_on_timeout(&s->dma_adc.wait, HZ)) {
  1939. if(! s->card->in_suspend) printk(KERN_DEBUG "maestro: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
  1940. s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
  1941. s->dma_adc.hwptr, s->dma_adc.swptr);
  1942. stop_adc(s);
  1943. spin_lock_irqsave(&s->lock, flags);
  1944. set_dmac(s, virt_to_bus(s->dma_adc.rawbuf), s->dma_adc.numfrag << s->dma_adc.fragshift);
  1945. /* program enhanced mode registers */
  1946. /* FILL ME */
  1947. /* wrindir(s, SV_CIDMACBASECOUNT1, (s->dma_adc.fragsamples-1) >> 8);
  1948. wrindir(s, SV_CIDMACBASECOUNT0, s->dma_adc.fragsamples-1); */
  1949. s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
  1950. spin_unlock_irqrestore(&s->lock, flags);
  1951. }
  1952. if (signal_pending(current))
  1953. {
  1954. ret = ret ? ret : -ERESTARTSYS;
  1955. goto rec_return_free;
  1956. }
  1957. continue;
  1958. }
  1959. if(s->fmt & (ESS_FMT_STEREO << ESS_ADC_SHIFT)) {
  1960. /* swptr/2 so that we know the real offset in each apu's buffer */
  1961. comb_stereo(s->dma_adc.rawbuf,combbuf,swptr/2,cnt,s->dma_adc.dmasize);
  1962. if (copy_to_user(buffer, combbuf, cnt)) {
  1963. ret = ret ? ret : -EFAULT;
  1964. goto rec_return_free;
  1965. }
  1966. } else {
  1967. if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
  1968. ret = ret ? ret : -EFAULT;
  1969. goto rec_return_free;
  1970. }
  1971. }
  1972. swptr = (swptr + cnt) % s->dma_adc.dmasize;
  1973. spin_lock_irqsave(&s->lock, flags);
  1974. s->dma_adc.swptr = swptr;
  1975. s->dma_adc.count -= cnt;
  1976. spin_unlock_irqrestore(&s->lock, flags);
  1977. count -= cnt;
  1978. buffer += cnt;
  1979. ret += cnt;
  1980. start_adc(s);
  1981. }
  1982. rec_return_free:
  1983. kfree(combbuf);
  1984. return ret;
  1985. }
  1986. static ssize_t
  1987. ess_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  1988. {
  1989. struct ess_state *s = (struct ess_state *)file->private_data;
  1990. ssize_t ret;
  1991. unsigned long flags;
  1992. unsigned swptr;
  1993. int cnt;
  1994. VALIDATE_STATE(s);
  1995. if (s->dma_dac.mapped)
  1996. return -ENXIO;
  1997. if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
  1998. return ret;
  1999. if (!access_ok(VERIFY_READ, buffer, count))
  2000. return -EFAULT;
  2001. ret = 0;
  2002. calc_bob_rate(s);
  2003. while (count > 0) {
  2004. spin_lock_irqsave(&s->lock, flags);
  2005. if (s->dma_dac.count < 0) {
  2006. s->dma_dac.count = 0;
  2007. s->dma_dac.swptr = s->dma_dac.hwptr;
  2008. }
  2009. swptr = s->dma_dac.swptr;
  2010. cnt = s->dma_dac.dmasize-swptr;
  2011. if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
  2012. cnt = s->dma_dac.dmasize - s->dma_dac.count;
  2013. spin_unlock_irqrestore(&s->lock, flags);
  2014. if (cnt > count)
  2015. cnt = count;
  2016. if (cnt <= 0) {
  2017. start_dac(s);
  2018. if (file->f_flags & O_NONBLOCK) {
  2019. if(!ret) ret = -EAGAIN;
  2020. goto return_free;
  2021. }
  2022. if (!interruptible_sleep_on_timeout(&s->dma_dac.wait, HZ)) {
  2023. if(! s->card->in_suspend) printk(KERN_DEBUG "maestro: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
  2024. s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
  2025. s->dma_dac.hwptr, s->dma_dac.swptr);
  2026. stop_dac(s);
  2027. spin_lock_irqsave(&s->lock, flags);
  2028. set_dmaa(s, virt_to_bus(s->dma_dac.rawbuf), s->dma_dac.numfrag << s->dma_dac.fragshift);
  2029. /* program enhanced mode registers */
  2030. /* wrindir(s, SV_CIDMAABASECOUNT1, (s->dma_dac.fragsamples-1) >> 8);
  2031. wrindir(s, SV_CIDMAABASECOUNT0, s->dma_dac.fragsamples-1); */
  2032. /* FILL ME */
  2033. s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
  2034. spin_unlock_irqrestore(&s->lock, flags);
  2035. }
  2036. if (signal_pending(current)) {
  2037. if (!ret) ret = -ERESTARTSYS;
  2038. goto return_free;
  2039. }
  2040. continue;
  2041. }
  2042. if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt)) {
  2043. if (!ret) ret = -EFAULT;
  2044. goto return_free;
  2045. }
  2046. /* printk("wrote %d bytes at sw: %d cnt: %d while hw: %d\n",cnt, swptr, s->dma_dac.count, s->dma_dac.hwptr);*/
  2047. swptr = (swptr + cnt) % s->dma_dac.dmasize;
  2048. spin_lock_irqsave(&s->lock, flags);
  2049. s->dma_dac.swptr = swptr;
  2050. s->dma_dac.count += cnt;
  2051. s->dma_dac.endcleared = 0;
  2052. spin_unlock_irqrestore(&s->lock, flags);
  2053. count -= cnt;
  2054. buffer += cnt;
  2055. ret += cnt;
  2056. start_dac(s);
  2057. }
  2058. return_free:
  2059. return ret;
  2060. }
  2061. /* No kernel lock - we have our own spinlock */
  2062. static unsigned int ess_poll(struct file *file, struct poll_table_struct *wait)
  2063. {
  2064. struct ess_state *s = (struct ess_state *)file->private_data;
  2065. unsigned long flags;
  2066. unsigned int mask = 0;
  2067. VALIDATE_STATE(s);
  2068. /* In 0.14 prog_dmabuf always returns success anyway ... */
  2069. if (file->f_mode & FMODE_WRITE) {
  2070. if (!s->dma_dac.ready && prog_dmabuf(s, 0))
  2071. return 0;
  2072. }
  2073. if (file->f_mode & FMODE_READ) {
  2074. if (!s->dma_adc.ready && prog_dmabuf(s, 1))
  2075. return 0;
  2076. }
  2077. if (file->f_mode & FMODE_WRITE)
  2078. poll_wait(file, &s->dma_dac.wait, wait);
  2079. if (file->f_mode & FMODE_READ)
  2080. poll_wait(file, &s->dma_adc.wait, wait);
  2081. spin_lock_irqsave(&s->lock, flags);
  2082. ess_update_ptr(s);
  2083. if (file->f_mode & FMODE_READ) {
  2084. if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
  2085. mask |= POLLIN | POLLRDNORM;
  2086. }
  2087. if (file->f_mode & FMODE_WRITE) {
  2088. if (s->dma_dac.mapped) {
  2089. if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
  2090. mask |= POLLOUT | POLLWRNORM;
  2091. } else {
  2092. if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
  2093. mask |= POLLOUT | POLLWRNORM;
  2094. }
  2095. }
  2096. spin_unlock_irqrestore(&s->lock, flags);
  2097. return mask;
  2098. }
  2099. static int ess_mmap(struct file *file, struct vm_area_struct *vma)
  2100. {
  2101. struct ess_state *s = (struct ess_state *)file->private_data;
  2102. struct dmabuf *db;
  2103. int ret = -EINVAL;
  2104. unsigned long size;
  2105. VALIDATE_STATE(s);
  2106. lock_kernel();
  2107. if (vma->vm_flags & VM_WRITE) {
  2108. if ((ret = prog_dmabuf(s, 1)) != 0)
  2109. goto out;
  2110. db = &s->dma_dac;
  2111. } else
  2112. #if 0
  2113. /* if we can have the wp/wc do the combining
  2114. we can turn this back on. */
  2115. if (vma->vm_flags & VM_READ) {
  2116. if ((ret = prog_dmabuf(s, 0)) != 0)
  2117. goto out;
  2118. db = &s->dma_adc;
  2119. } else
  2120. #endif
  2121. goto out;
  2122. ret = -EINVAL;
  2123. if (vma->vm_pgoff != 0)
  2124. goto out;
  2125. size = vma->vm_end - vma->vm_start;
  2126. if (size > (PAGE_SIZE << db->buforder))
  2127. goto out;
  2128. ret = -EAGAIN;
  2129. if (remap_pfn_range(vma, vma->vm_start,
  2130. virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
  2131. size, vma->vm_page_prot))
  2132. goto out;
  2133. db->mapped = 1;
  2134. ret = 0;
  2135. out:
  2136. unlock_kernel();
  2137. return ret;
  2138. }
  2139. static int ess_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  2140. {
  2141. struct ess_state *s = (struct ess_state *)file->private_data;
  2142. unsigned long flags;
  2143. audio_buf_info abinfo;
  2144. count_info cinfo;
  2145. int val, mapped, ret;
  2146. unsigned char fmtm, fmtd;
  2147. void __user *argp = (void __user *)arg;
  2148. int __user *p = argp;
  2149. /* printk("maestro: ess_ioctl: cmd %d\n", cmd);*/
  2150. VALIDATE_STATE(s);
  2151. mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
  2152. ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
  2153. switch (cmd) {
  2154. case OSS_GETVERSION:
  2155. return put_user(SOUND_VERSION, p);
  2156. case SNDCTL_DSP_SYNC:
  2157. if (file->f_mode & FMODE_WRITE)
  2158. return drain_dac(s, file->f_flags & O_NONBLOCK);
  2159. return 0;
  2160. case SNDCTL_DSP_SETDUPLEX:
  2161. /* XXX fix */
  2162. return 0;
  2163. case SNDCTL_DSP_GETCAPS:
  2164. return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, p);
  2165. case SNDCTL_DSP_RESET:
  2166. if (file->f_mode & FMODE_WRITE) {
  2167. stop_dac(s);
  2168. synchronize_irq(s->card->pcidev->irq);
  2169. s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
  2170. }
  2171. if (file->f_mode & FMODE_READ) {
  2172. stop_adc(s);
  2173. synchronize_irq(s->card->pcidev->irq);
  2174. s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
  2175. }
  2176. return 0;
  2177. case SNDCTL_DSP_SPEED:
  2178. if (get_user(val, p))
  2179. return -EFAULT;
  2180. if (val >= 0) {
  2181. if (file->f_mode & FMODE_READ) {
  2182. stop_adc(s);
  2183. s->dma_adc.ready = 0;
  2184. set_adc_rate(s, val);
  2185. }
  2186. if (file->f_mode & FMODE_WRITE) {
  2187. stop_dac(s);
  2188. s->dma_dac.ready = 0;
  2189. set_dac_rate(s, val);
  2190. }
  2191. }
  2192. return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
  2193. case SNDCTL_DSP_STEREO:
  2194. if (get_user(val, p))
  2195. return -EFAULT;
  2196. fmtd = 0;
  2197. fmtm = ~0;
  2198. if (file->f_mode & FMODE_READ) {
  2199. stop_adc(s);
  2200. s->dma_adc.ready = 0;
  2201. if (val)
  2202. fmtd |= ESS_FMT_STEREO << ESS_ADC_SHIFT;
  2203. else
  2204. fmtm &= ~(ESS_FMT_STEREO << ESS_ADC_SHIFT);
  2205. }
  2206. if (file->f_mode & FMODE_WRITE) {
  2207. stop_dac(s);
  2208. s->dma_dac.ready = 0;
  2209. if (val)
  2210. fmtd |= ESS_FMT_STEREO << ESS_DAC_SHIFT;
  2211. else
  2212. fmtm &= ~(ESS_FMT_STEREO << ESS_DAC_SHIFT);
  2213. }
  2214. set_fmt(s, fmtm, fmtd);
  2215. return 0;
  2216. case SNDCTL_DSP_CHANNELS:
  2217. if (get_user(val, p))
  2218. return -EFAULT;
  2219. if (val != 0) {
  2220. fmtd = 0;
  2221. fmtm = ~0;
  2222. if (file->f_mode & FMODE_READ) {
  2223. stop_adc(s);
  2224. s->dma_adc.ready = 0;
  2225. if (val >= 2)
  2226. fmtd |= ESS_FMT_STEREO << ESS_ADC_SHIFT;
  2227. else
  2228. fmtm &= ~(ESS_FMT_STEREO << ESS_ADC_SHIFT);
  2229. }
  2230. if (file->f_mode & FMODE_WRITE) {
  2231. stop_dac(s);
  2232. s->dma_dac.ready = 0;
  2233. if (val >= 2)
  2234. fmtd |= ESS_FMT_STEREO << ESS_DAC_SHIFT;
  2235. else
  2236. fmtm &= ~(ESS_FMT_STEREO << ESS_DAC_SHIFT);
  2237. }
  2238. set_fmt(s, fmtm, fmtd);
  2239. }
  2240. return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (ESS_FMT_STEREO << ESS_ADC_SHIFT)
  2241. : (ESS_FMT_STEREO << ESS_DAC_SHIFT))) ? 2 : 1, p);
  2242. case SNDCTL_DSP_GETFMTS: /* Returns a mask */
  2243. return put_user(AFMT_U8|AFMT_S16_LE, p);
  2244. case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
  2245. if (get_user(val, p))
  2246. return -EFAULT;
  2247. if (val != AFMT_QUERY) {
  2248. fmtd = 0;
  2249. fmtm = ~0;
  2250. if (file->f_mode & FMODE_READ) {
  2251. stop_adc(s);
  2252. s->dma_adc.ready = 0;
  2253. /* fixed at 16bit for now */
  2254. fmtd |= ESS_FMT_16BIT << ESS_ADC_SHIFT;
  2255. #if 0
  2256. if (val == AFMT_S16_LE)
  2257. fmtd |= ESS_FMT_16BIT << ESS_ADC_SHIFT;
  2258. else
  2259. fmtm &= ~(ESS_FMT_16BIT << ESS_ADC_SHIFT);
  2260. #endif
  2261. }
  2262. if (file->f_mode & FMODE_WRITE) {
  2263. stop_dac(s);
  2264. s->dma_dac.ready = 0;
  2265. if (val == AFMT_S16_LE)
  2266. fmtd |= ESS_FMT_16BIT << ESS_DAC_SHIFT;
  2267. else
  2268. fmtm &= ~(ESS_FMT_16BIT << ESS_DAC_SHIFT);
  2269. }
  2270. set_fmt(s, fmtm, fmtd);
  2271. }
  2272. return put_user((s->fmt & ((file->f_mode & FMODE_READ) ?
  2273. (ESS_FMT_16BIT << ESS_ADC_SHIFT)
  2274. : (ESS_FMT_16BIT << ESS_DAC_SHIFT))) ?
  2275. AFMT_S16_LE :
  2276. AFMT_U8,
  2277. p);
  2278. case SNDCTL_DSP_POST:
  2279. return 0;
  2280. case SNDCTL_DSP_GETTRIGGER:
  2281. val = 0;
  2282. if ((file->f_mode & FMODE_READ) && (s->enable & ADC_RUNNING))
  2283. val |= PCM_ENABLE_INPUT;
  2284. if ((file->f_mode & FMODE_WRITE) && (s->enable & DAC_RUNNING))
  2285. val |= PCM_ENABLE_OUTPUT;
  2286. return put_user(val, p);
  2287. case SNDCTL_DSP_SETTRIGGER:
  2288. if (get_user(val, p))
  2289. return -EFAULT;
  2290. if (file->f_mode & FMODE_READ) {
  2291. if (val & PCM_ENABLE_INPUT) {
  2292. if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
  2293. return ret;
  2294. start_adc(s);
  2295. } else
  2296. stop_adc(s);
  2297. }
  2298. if (file->f_mode & FMODE_WRITE) {
  2299. if (val & PCM_ENABLE_OUTPUT) {
  2300. if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
  2301. return ret;
  2302. start_dac(s);
  2303. } else
  2304. stop_dac(s);
  2305. }
  2306. return 0;
  2307. case SNDCTL_DSP_GETOSPACE:
  2308. if (!(file->f_mode & FMODE_WRITE))
  2309. return -EINVAL;
  2310. if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
  2311. return ret;
  2312. spin_lock_irqsave(&s->lock, flags);
  2313. ess_update_ptr(s);
  2314. abinfo.fragsize = s->dma_dac.fragsize;
  2315. abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
  2316. abinfo.fragstotal = s->dma_dac.numfrag;
  2317. abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
  2318. spin_unlock_irqrestore(&s->lock, flags);
  2319. return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
  2320. case SNDCTL_DSP_GETISPACE:
  2321. if (!(file->f_mode & FMODE_READ))
  2322. return -EINVAL;
  2323. if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
  2324. return ret;
  2325. spin_lock_irqsave(&s->lock, flags);
  2326. ess_update_ptr(s);
  2327. abinfo.fragsize = s->dma_adc.fragsize;
  2328. abinfo.bytes = s->dma_adc.count;
  2329. abinfo.fragstotal = s->dma_adc.numfrag;
  2330. abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
  2331. spin_unlock_irqrestore(&s->lock, flags);
  2332. return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
  2333. case SNDCTL_DSP_NONBLOCK:
  2334. file->f_flags |= O_NONBLOCK;
  2335. return 0;
  2336. case SNDCTL_DSP_GETODELAY:
  2337. if (!(file->f_mode & FMODE_WRITE))
  2338. return -EINVAL;
  2339. if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
  2340. return ret;
  2341. spin_lock_irqsave(&s->lock, flags);
  2342. ess_update_ptr(s);
  2343. val = s->dma_dac.count;
  2344. spin_unlock_irqrestore(&s->lock, flags);
  2345. return put_user(val, p);
  2346. case SNDCTL_DSP_GETIPTR:
  2347. if (!(file->f_mode & FMODE_READ))
  2348. return -EINVAL;
  2349. if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
  2350. return ret;
  2351. spin_lock_irqsave(&s->lock, flags);
  2352. ess_update_ptr(s);
  2353. cinfo.bytes = s->dma_adc.total_bytes;
  2354. cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
  2355. cinfo.ptr = s->dma_adc.hwptr;
  2356. if (s->dma_adc.mapped)
  2357. s->dma_adc.count &= s->dma_adc.fragsize-1;
  2358. spin_unlock_irqrestore(&s->lock, flags);
  2359. if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
  2360. return -EFAULT;
  2361. return 0;
  2362. case SNDCTL_DSP_GETOPTR:
  2363. if (!(file->f_mode & FMODE_WRITE))
  2364. return -EINVAL;
  2365. if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
  2366. return ret;
  2367. spin_lock_irqsave(&s->lock, flags);
  2368. ess_update_ptr(s);
  2369. cinfo.bytes = s->dma_dac.total_bytes;
  2370. cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
  2371. cinfo.ptr = s->dma_dac.hwptr;
  2372. if (s->dma_dac.mapped)
  2373. s->dma_dac.count &= s->dma_dac.fragsize-1;
  2374. spin_unlock_irqrestore(&s->lock, flags);
  2375. if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
  2376. return -EFAULT;
  2377. return 0;
  2378. case SNDCTL_DSP_GETBLKSIZE:
  2379. if (file->f_mode & FMODE_WRITE) {
  2380. if ((val = prog_dmabuf(s, 0)))
  2381. return val;
  2382. return put_user(s->dma_dac.fragsize, p);
  2383. }
  2384. if ((val = prog_dmabuf(s, 1)))
  2385. return val;
  2386. return put_user(s->dma_adc.fragsize, p);
  2387. case SNDCTL_DSP_SETFRAGMENT:
  2388. if (get_user(val, p))
  2389. return -EFAULT;
  2390. M_printk("maestro: SETFRAGMENT: %0x\n",val);
  2391. if (file->f_mode & FMODE_READ) {
  2392. s->dma_adc.ossfragshift = val & 0xffff;
  2393. s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
  2394. if (s->dma_adc.ossfragshift < 4)
  2395. s->dma_adc.ossfragshift = 4;
  2396. if (s->dma_adc.ossfragshift > 15)
  2397. s->dma_adc.ossfragshift = 15;
  2398. if (s->dma_adc.ossmaxfrags < 4)
  2399. s->dma_adc.ossmaxfrags = 4;
  2400. }
  2401. if (file->f_mode & FMODE_WRITE) {
  2402. s->dma_dac.ossfragshift = val & 0xffff;
  2403. s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
  2404. if (s->dma_dac.ossfragshift < 4)
  2405. s->dma_dac.ossfragshift = 4;
  2406. if (s->dma_dac.ossfragshift > 15)
  2407. s->dma_dac.ossfragshift = 15;
  2408. if (s->dma_dac.ossmaxfrags < 4)
  2409. s->dma_dac.ossmaxfrags = 4;
  2410. }
  2411. return 0;
  2412. case SNDCTL_DSP_SUBDIVIDE:
  2413. if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
  2414. (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
  2415. return -EINVAL;
  2416. if (get_user(val, p))
  2417. return -EFAULT;
  2418. if (val != 1 && val != 2 && val != 4)
  2419. return -EINVAL;
  2420. if (file->f_mode & FMODE_READ)
  2421. s->dma_adc.subdivision = val;
  2422. if (file->f_mode & FMODE_WRITE)
  2423. s->dma_dac.subdivision = val;
  2424. return 0;
  2425. case SOUND_PCM_READ_RATE:
  2426. return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
  2427. case SOUND_PCM_READ_CHANNELS:
  2428. return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (ESS_FMT_STEREO << ESS_ADC_SHIFT)
  2429. : (ESS_FMT_STEREO << ESS_DAC_SHIFT))) ? 2 : 1, p);
  2430. case SOUND_PCM_READ_BITS:
  2431. return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (ESS_FMT_16BIT << ESS_ADC_SHIFT)
  2432. : (ESS_FMT_16BIT << ESS_DAC_SHIFT))) ? 16 : 8, p);
  2433. case SOUND_PCM_WRITE_FILTER:
  2434. case SNDCTL_DSP_SETSYNCRO:
  2435. case SOUND_PCM_READ_FILTER:
  2436. return -EINVAL;
  2437. }
  2438. return -EINVAL;
  2439. }
  2440. static void
  2441. set_base_registers(struct ess_state *s,void *vaddr)
  2442. {
  2443. unsigned long packed_phys = virt_to_bus(vaddr)>>12;
  2444. wave_set_register(s, 0x01FC , packed_phys);
  2445. wave_set_register(s, 0x01FD , packed_phys);
  2446. wave_set_register(s, 0x01FE , packed_phys);
  2447. wave_set_register(s, 0x01FF , packed_phys);
  2448. }
  2449. /*
  2450. * this guy makes sure we're in the right power
  2451. * state for what we want to be doing
  2452. */
  2453. static void maestro_power(struct ess_card *card, int tostate)
  2454. {
  2455. u16 active_mask = acpi_state_mask[tostate];
  2456. u8 state;
  2457. if(!use_pm) return;
  2458. pci_read_config_byte(card->pcidev, card->power_regs+0x4, &state);
  2459. state&=3;
  2460. /* make sure we're in the right state */
  2461. if(state != tostate) {
  2462. M_printk(KERN_WARNING "maestro: dev %02x:%02x.%x switching from D%d to D%d\n",
  2463. card->pcidev->bus->number,
  2464. PCI_SLOT(card->pcidev->devfn),
  2465. PCI_FUNC(card->pcidev->devfn),
  2466. state,tostate);
  2467. pci_write_config_byte(card->pcidev, card->power_regs+0x4, tostate);
  2468. }
  2469. /* and make sure the units we care about are on
  2470. XXX we might want to do this before state flipping? */
  2471. pci_write_config_word(card->pcidev, 0x54, ~ active_mask);
  2472. pci_write_config_word(card->pcidev, 0x56, ~ active_mask);
  2473. }
  2474. /* we allocate a large power of two for all our memory.
  2475. this is cut up into (not to scale :):
  2476. |silly fifo word | 512byte mixbuf per adc | dac/adc * channels |
  2477. */
  2478. static int
  2479. allocate_buffers(struct ess_state *s)
  2480. {
  2481. void *rawbuf=NULL;
  2482. int order,i;
  2483. struct page *page, *pend;
  2484. /* alloc as big a chunk as we can */
  2485. for (order = (dsps_order + (16-PAGE_SHIFT) + 1); order >= (dsps_order + 2 + 1); order--)
  2486. if((rawbuf = (void *)__get_free_pages(GFP_KERNEL|GFP_DMA, order)))
  2487. break;
  2488. if (!rawbuf)
  2489. return 1;
  2490. M_printk("maestro: allocated %ld (%d) bytes at %p\n",PAGE_SIZE<<order,order, rawbuf);
  2491. if ((virt_to_bus(rawbuf) + (PAGE_SIZE << order) - 1) & ~((1<<28)-1)) {
  2492. printk(KERN_ERR "maestro: DMA buffer beyond 256MB! busaddr 0x%lx size %ld\n",
  2493. virt_to_bus(rawbuf), PAGE_SIZE << order);
  2494. kfree(rawbuf);
  2495. return 1;
  2496. }
  2497. s->card->dmapages = rawbuf;
  2498. s->card->dmaorder = order;
  2499. for(i=0;i<NR_DSPS;i++) {
  2500. struct ess_state *ess = &s->card->channels[i];
  2501. if(ess->dev_audio == -1)
  2502. continue;
  2503. ess->dma_dac.ready = s->dma_dac.mapped = 0;
  2504. ess->dma_adc.ready = s->dma_adc.mapped = 0;
  2505. ess->dma_adc.buforder = ess->dma_dac.buforder = order - 1 - dsps_order - 1;
  2506. /* offset dac and adc buffers starting half way through and then at each [da][ad]c's
  2507. order's intervals.. */
  2508. ess->dma_dac.rawbuf = rawbuf + (PAGE_SIZE<<(order-1)) + (i * ( PAGE_SIZE << (ess->dma_dac.buforder + 1 )));
  2509. ess->dma_adc.rawbuf = ess->dma_dac.rawbuf + ( PAGE_SIZE << ess->dma_dac.buforder);
  2510. /* offset mixbuf by a mixbuf so that the lame status fifo can
  2511. happily scribble away.. */
  2512. ess->mixbuf = rawbuf + (512 * (i+1));
  2513. M_printk("maestro: setup apu %d: dac: %p adc: %p mix: %p\n",i,ess->dma_dac.rawbuf,
  2514. ess->dma_adc.rawbuf, ess->mixbuf);
  2515. }
  2516. /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
  2517. pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
  2518. for (page = virt_to_page(rawbuf); page <= pend; page++)
  2519. SetPageReserved(page);
  2520. return 0;
  2521. }
  2522. static void
  2523. free_buffers(struct ess_state *s)
  2524. {
  2525. struct page *page, *pend;
  2526. s->dma_dac.rawbuf = s->dma_adc.rawbuf = NULL;
  2527. s->dma_dac.mapped = s->dma_adc.mapped = 0;
  2528. s->dma_dac.ready = s->dma_adc.ready = 0;
  2529. M_printk("maestro: freeing %p\n",s->card->dmapages);
  2530. /* undo marking the pages as reserved */
  2531. pend = virt_to_page(s->card->dmapages + (PAGE_SIZE << s->card->dmaorder) - 1);
  2532. for (page = virt_to_page(s->card->dmapages); page <= pend; page++)
  2533. ClearPageReserved(page);
  2534. free_pages((unsigned long)s->card->dmapages,s->card->dmaorder);
  2535. s->card->dmapages = NULL;
  2536. }
  2537. static int
  2538. ess_open(struct inode *inode, struct file *file)
  2539. {
  2540. unsigned int minor = iminor(inode);
  2541. struct ess_state *s = NULL;
  2542. unsigned char fmtm = ~0, fmts = 0;
  2543. struct pci_dev *pdev = NULL;
  2544. /*
  2545. * Scan the cards and find the channel. We only
  2546. * do this at open time so it is ok
  2547. */
  2548. while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
  2549. struct ess_card *c;
  2550. struct pci_driver *drvr;
  2551. drvr = pci_dev_driver (pdev);
  2552. if (drvr == &maestro_pci_driver) {
  2553. int i;
  2554. struct ess_state *sp;
  2555. c = (struct ess_card*)pci_get_drvdata (pdev);
  2556. if (!c)
  2557. continue;
  2558. for(i=0;i<NR_DSPS;i++)
  2559. {
  2560. sp=&c->channels[i];
  2561. if(sp->dev_audio < 0)
  2562. continue;
  2563. if((sp->dev_audio ^ minor) & ~0xf)
  2564. continue;
  2565. s=sp;
  2566. }
  2567. }
  2568. }
  2569. if (!s)
  2570. return -ENODEV;
  2571. VALIDATE_STATE(s);
  2572. file->private_data = s;
  2573. /* wait for device to become free */
  2574. down(&s->open_sem);
  2575. while (s->open_mode & file->f_mode) {
  2576. if (file->f_flags & O_NONBLOCK) {
  2577. up(&s->open_sem);
  2578. return -EWOULDBLOCK;
  2579. }
  2580. up(&s->open_sem);
  2581. interruptible_sleep_on(&s->open_wait);
  2582. if (signal_pending(current))
  2583. return -ERESTARTSYS;
  2584. down(&s->open_sem);
  2585. }
  2586. /* under semaphore.. */
  2587. if ((s->card->dmapages==NULL) && allocate_buffers(s)) {
  2588. up(&s->open_sem);
  2589. return -ENOMEM;
  2590. }
  2591. /* we're covered by the open_sem */
  2592. if( ! s->card->dsps_open ) {
  2593. maestro_power(s->card,ACPI_D0);
  2594. start_bob(s);
  2595. }
  2596. s->card->dsps_open++;
  2597. M_printk("maestro: open, %d bobs now\n",s->card->dsps_open);
  2598. /* ok, lets write WC base regs now that we've
  2599. powered up the chip */
  2600. M_printk("maestro: writing 0x%lx (bus 0x%lx) to the wp\n",virt_to_bus(s->card->dmapages),
  2601. ((virt_to_bus(s->card->dmapages))&0xFFE00000)>>12);
  2602. set_base_registers(s,s->card->dmapages);
  2603. if (file->f_mode & FMODE_READ) {
  2604. /*
  2605. fmtm &= ~((ESS_FMT_STEREO | ESS_FMT_16BIT) << ESS_ADC_SHIFT);
  2606. if ((minor & 0xf) == SND_DEV_DSP16)
  2607. fmts |= ESS_FMT_16BIT << ESS_ADC_SHIFT; */
  2608. fmtm &= ~((ESS_FMT_STEREO|ESS_FMT_16BIT) << ESS_ADC_SHIFT);
  2609. fmts = (ESS_FMT_STEREO|ESS_FMT_16BIT) << ESS_ADC_SHIFT;
  2610. s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
  2611. set_adc_rate(s, 8000);
  2612. }
  2613. if (file->f_mode & FMODE_WRITE) {
  2614. fmtm &= ~((ESS_FMT_STEREO | ESS_FMT_16BIT) << ESS_DAC_SHIFT);
  2615. if ((minor & 0xf) == SND_DEV_DSP16)
  2616. fmts |= ESS_FMT_16BIT << ESS_DAC_SHIFT;
  2617. s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
  2618. set_dac_rate(s, 8000);
  2619. }
  2620. set_fmt(s, fmtm, fmts);
  2621. s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
  2622. up(&s->open_sem);
  2623. return nonseekable_open(inode, file);
  2624. }
  2625. static int
  2626. ess_release(struct inode *inode, struct file *file)
  2627. {
  2628. struct ess_state *s = (struct ess_state *)file->private_data;
  2629. VALIDATE_STATE(s);
  2630. lock_kernel();
  2631. if (file->f_mode & FMODE_WRITE)
  2632. drain_dac(s, file->f_flags & O_NONBLOCK);
  2633. down(&s->open_sem);
  2634. if (file->f_mode & FMODE_WRITE) {
  2635. stop_dac(s);
  2636. }
  2637. if (file->f_mode & FMODE_READ) {
  2638. stop_adc(s);
  2639. }
  2640. s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
  2641. /* we're covered by the open_sem */
  2642. M_printk("maestro: %d dsps now alive\n",s->card->dsps_open-1);
  2643. if( --s->card->dsps_open <= 0) {
  2644. s->card->dsps_open = 0;
  2645. stop_bob(s);
  2646. free_buffers(s);
  2647. maestro_power(s->card,ACPI_D2);
  2648. }
  2649. up(&s->open_sem);
  2650. wake_up(&s->open_wait);
  2651. unlock_kernel();
  2652. return 0;
  2653. }
  2654. static struct file_operations ess_audio_fops = {
  2655. .owner = THIS_MODULE,
  2656. .llseek = no_llseek,
  2657. .read = ess_read,
  2658. .write = ess_write,
  2659. .poll = ess_poll,
  2660. .ioctl = ess_ioctl,
  2661. .mmap = ess_mmap,
  2662. .open = ess_open,
  2663. .release = ess_release,
  2664. };
  2665. static int
  2666. maestro_config(struct ess_card *card)
  2667. {
  2668. struct pci_dev *pcidev = card->pcidev;
  2669. struct ess_state *ess = &card->channels[0];
  2670. int apu,iobase = card->iobase;
  2671. u16 w;
  2672. u32 n;
  2673. /* We used to muck around with pci config space that
  2674. * we had no business messing with. We don't know enough
  2675. * about the machine to know which DMA mode is appropriate,
  2676. * etc. We were guessing wrong on some machines and making
  2677. * them unhappy. We now trust in the BIOS to do things right,
  2678. * which almost certainly means a new host of problems will
  2679. * arise with broken BIOS implementations. screw 'em.
  2680. * We're already intolerant of machines that don't assign
  2681. * IRQs.
  2682. */
  2683. /* do config work at full power */
  2684. maestro_power(card,ACPI_D0);
  2685. pci_read_config_word(pcidev, 0x50, &w);
  2686. w&=~(1<<5); /* Don't swap left/right (undoc)*/
  2687. pci_write_config_word(pcidev, 0x50, w);
  2688. pci_read_config_word(pcidev, 0x52, &w);
  2689. w&=~(1<<15); /* Turn off internal clock multiplier */
  2690. /* XXX how do we know which to use? */
  2691. w&=~(1<<14); /* External clock */
  2692. w|= (1<<7); /* Hardware volume control on */
  2693. w|= (1<<6); /* Debounce off: easier to push the HWV buttons. */
  2694. w&=~(1<<5); /* GPIO 4:5 */
  2695. w|= (1<<4); /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
  2696. w&=~(1<<2); /* MIDI fix off (undoc) */
  2697. w&=~(1<<1); /* reserved, always write 0 */
  2698. pci_write_config_word(pcidev, 0x52, w);
  2699. /*
  2700. * Legacy mode
  2701. */
  2702. pci_read_config_word(pcidev, 0x40, &w);
  2703. w|=(1<<15); /* legacy decode off */
  2704. w&=~(1<<14); /* Disable SIRQ */
  2705. w&=~(0x1f); /* disable mpu irq/io, game port, fm, SB */
  2706. pci_write_config_word(pcidev, 0x40, w);
  2707. /* Set up 978 docking control chip. */
  2708. pci_read_config_word(pcidev, 0x58, &w);
  2709. w|=1<<2; /* Enable 978. */
  2710. w|=1<<3; /* Turn on 978 hardware volume control. */
  2711. w&=~(1<<11); /* Turn on 978 mixer volume control. */
  2712. pci_write_config_word(pcidev, 0x58, w);
  2713. sound_reset(iobase);
  2714. /*
  2715. * Ring Bus Setup
  2716. */
  2717. /* setup usual 0x34 stuff.. 0x36 may be chip specific */
  2718. outw(0xC090, iobase+0x34); /* direct sound, stereo */
  2719. udelay(20);
  2720. outw(0x3000, iobase+0x36); /* direct sound, stereo */
  2721. udelay(20);
  2722. /*
  2723. * Reset the CODEC
  2724. */
  2725. maestro_ac97_reset(iobase,pcidev);
  2726. /*
  2727. * Ring Bus Setup
  2728. */
  2729. n=inl(iobase+0x34);
  2730. n&=~0xF000;
  2731. n|=12<<12; /* Direct Sound, Stereo */
  2732. outl(n, iobase+0x34);
  2733. n=inl(iobase+0x34);
  2734. n&=~0x0F00; /* Modem off */
  2735. outl(n, iobase+0x34);
  2736. n=inl(iobase+0x34);
  2737. n&=~0x00F0;
  2738. n|=9<<4; /* DAC, Stereo */
  2739. outl(n, iobase+0x34);
  2740. n=inl(iobase+0x34);
  2741. n&=~0x000F; /* ASSP off */
  2742. outl(n, iobase+0x34);
  2743. n=inl(iobase+0x34);
  2744. n|=(1<<29); /* Enable ring bus */
  2745. outl(n, iobase+0x34);
  2746. n=inl(iobase+0x34);
  2747. n|=(1<<28); /* Enable serial bus */
  2748. outl(n, iobase+0x34);
  2749. n=inl(iobase+0x34);
  2750. n&=~0x00F00000; /* MIC off */
  2751. outl(n, iobase+0x34);
  2752. n=inl(iobase+0x34);
  2753. n&=~0x000F0000; /* I2S off */
  2754. outl(n, iobase+0x34);
  2755. w=inw(iobase+0x18);
  2756. w&=~(1<<7); /* ClkRun off */
  2757. outw(w, iobase+0x18);
  2758. w=inw(iobase+0x18);
  2759. w&=~(1<<6); /* Hardware volume control interrupt off... for now. */
  2760. outw(w, iobase+0x18);
  2761. w=inw(iobase+0x18);
  2762. w&=~(1<<4); /* ASSP irq off */
  2763. outw(w, iobase+0x18);
  2764. w=inw(iobase+0x18);
  2765. w&=~(1<<3); /* ISDN irq off */
  2766. outw(w, iobase+0x18);
  2767. w=inw(iobase+0x18);
  2768. w|=(1<<2); /* Direct Sound IRQ on */
  2769. outw(w, iobase+0x18);
  2770. w=inw(iobase+0x18);
  2771. w&=~(1<<1); /* MPU401 IRQ off */
  2772. outw(w, iobase+0x18);
  2773. w=inw(iobase+0x18);
  2774. w|=(1<<0); /* SB IRQ on */
  2775. outw(w, iobase+0x18);
  2776. /* Set hardware volume control registers to midpoints.
  2777. We can tell which button was pushed based on how they change. */
  2778. outb(0x88, iobase+0x1c);
  2779. outb(0x88, iobase+0x1d);
  2780. outb(0x88, iobase+0x1e);
  2781. outb(0x88, iobase+0x1f);
  2782. /* it appears some maestros (dell 7500) only work if these are set,
  2783. regardless of whether we use the assp or not. */
  2784. outb(0, iobase+0xA4);
  2785. outb(3, iobase+0xA2);
  2786. outb(0, iobase+0xA6);
  2787. for(apu=0;apu<16;apu++)
  2788. {
  2789. /* Write 0 into the buffer area 0x1E0->1EF */
  2790. outw(0x01E0+apu, 0x10+iobase);
  2791. outw(0x0000, 0x12+iobase);
  2792. /*
  2793. * The 1.10 test program seem to write 0 into the buffer area
  2794. * 0x1D0-0x1DF too.
  2795. */
  2796. outw(0x01D0+apu, 0x10+iobase);
  2797. outw(0x0000, 0x12+iobase);
  2798. }
  2799. #if 1
  2800. wave_set_register(ess, IDR7_WAVE_ROMRAM,
  2801. (wave_get_register(ess, IDR7_WAVE_ROMRAM)&0xFF00));
  2802. wave_set_register(ess, IDR7_WAVE_ROMRAM,
  2803. wave_get_register(ess, IDR7_WAVE_ROMRAM)|0x100);
  2804. wave_set_register(ess, IDR7_WAVE_ROMRAM,
  2805. wave_get_register(ess, IDR7_WAVE_ROMRAM)&~0x200);
  2806. wave_set_register(ess, IDR7_WAVE_ROMRAM,
  2807. wave_get_register(ess, IDR7_WAVE_ROMRAM)|~0x400);
  2808. #else
  2809. maestro_write(ess, IDR7_WAVE_ROMRAM,
  2810. (maestro_read(ess, IDR7_WAVE_ROMRAM)&0xFF00));
  2811. maestro_write(ess, IDR7_WAVE_ROMRAM,
  2812. maestro_read(ess, IDR7_WAVE_ROMRAM)|0x100);
  2813. maestro_write(ess, IDR7_WAVE_ROMRAM,
  2814. maestro_read(ess, IDR7_WAVE_ROMRAM)&~0x200);
  2815. maestro_write(ess, IDR7_WAVE_ROMRAM,
  2816. maestro_read(ess, IDR7_WAVE_ROMRAM)|0x400);
  2817. #endif
  2818. maestro_write(ess, IDR2_CRAM_DATA, 0x0000);
  2819. maestro_write(ess, 0x08, 0xB004);
  2820. /* Now back to the DirectSound stuff */
  2821. maestro_write(ess, 0x09, 0x001B);
  2822. maestro_write(ess, 0x0A, 0x8000);
  2823. maestro_write(ess, 0x0B, 0x3F37);
  2824. maestro_write(ess, 0x0C, 0x0098);
  2825. /* parallel out ?? */
  2826. maestro_write(ess, 0x0C,
  2827. (maestro_read(ess, 0x0C)&~0xF000)|0x8000);
  2828. /* parallel in, has something to do with recording :) */
  2829. maestro_write(ess, 0x0C,
  2830. (maestro_read(ess, 0x0C)&~0x0F00)|0x0500);
  2831. maestro_write(ess, 0x0D, 0x7632);
  2832. /* Wave cache control on - test off, sg off,
  2833. enable, enable extra chans 1Mb */
  2834. outw(inw(0x14+iobase)|(1<<8),0x14+iobase);
  2835. outw(inw(0x14+iobase)&0xFE03,0x14+iobase);
  2836. outw((inw(0x14+iobase)&0xFFFC), 0x14+iobase);
  2837. outw(inw(0x14+iobase)|(1<<7),0x14+iobase);
  2838. outw(0xA1A0, 0x14+iobase); /* 0300 ? */
  2839. /* Now clear the APU control ram */
  2840. for(apu=0;apu<NR_APUS;apu++)
  2841. {
  2842. for(w=0;w<NR_APU_REGS;w++)
  2843. apu_set_register(ess, apu|ESS_CHAN_HARD, w, 0);
  2844. }
  2845. return 0;
  2846. }
  2847. /* this guy tries to find the pci power management
  2848. * register bank. this should really be in core
  2849. * code somewhere. 1 on success. */
  2850. static int
  2851. parse_power(struct ess_card *card, struct pci_dev *pcidev)
  2852. {
  2853. u32 n;
  2854. u16 w;
  2855. u8 next;
  2856. int max = 64; /* an a 8bit guy pointing to 32bit guys
  2857. can only express so much. */
  2858. card->power_regs = 0;
  2859. /* check to see if we have a capabilities list in
  2860. the config register */
  2861. pci_read_config_word(pcidev, PCI_STATUS, &w);
  2862. if(!(w & PCI_STATUS_CAP_LIST)) return 0;
  2863. /* walk the list, starting at the head. */
  2864. pci_read_config_byte(pcidev,PCI_CAPABILITY_LIST,&next);
  2865. while(next && max--) {
  2866. pci_read_config_dword(pcidev, next & ~3, &n);
  2867. if((n & 0xff) == PCI_CAP_ID_PM) {
  2868. card->power_regs = next;
  2869. break;
  2870. }
  2871. next = ((n>>8) & 0xff);
  2872. }
  2873. return card->power_regs ? 1 : 0;
  2874. }
  2875. static int __init
  2876. maestro_probe(struct pci_dev *pcidev,const struct pci_device_id *pdid)
  2877. {
  2878. int card_type = pdid->driver_data;
  2879. u32 n;
  2880. int iobase;
  2881. int i, ret;
  2882. struct ess_card *card;
  2883. struct ess_state *ess;
  2884. struct pm_dev *pmdev;
  2885. int num = 0;
  2886. /* when built into the kernel, we only print version if device is found */
  2887. #ifndef MODULE
  2888. static int printed_version;
  2889. if (!printed_version++)
  2890. printk(version);
  2891. #endif
  2892. /* don't pick up weird modem maestros */
  2893. if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
  2894. return -ENODEV;
  2895. if ((ret=pci_enable_device(pcidev)))
  2896. return ret;
  2897. iobase = pci_resource_start(pcidev,0);
  2898. if (!iobase || !(pci_resource_flags(pcidev, 0 ) & IORESOURCE_IO))
  2899. return -ENODEV;
  2900. if(pcidev->irq == 0)
  2901. return -ENODEV;
  2902. /* stake our claim on the iospace */
  2903. if( request_region(iobase, 256, card_names[card_type]) == NULL )
  2904. {
  2905. printk(KERN_WARNING "maestro: can't allocate 256 bytes I/O at 0x%4.4x\n", iobase);
  2906. return -EBUSY;
  2907. }
  2908. /* just to be sure */
  2909. pci_set_master(pcidev);
  2910. card = kmalloc(sizeof(struct ess_card), GFP_KERNEL);
  2911. if(card == NULL)
  2912. {
  2913. printk(KERN_WARNING "maestro: out of memory\n");
  2914. release_region(iobase, 256);
  2915. return -ENOMEM;
  2916. }
  2917. memset(card, 0, sizeof(*card));
  2918. card->pcidev = pcidev;
  2919. pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev),
  2920. maestro_pm_callback);
  2921. if (pmdev)
  2922. pmdev->data = card;
  2923. card->iobase = iobase;
  2924. card->card_type = card_type;
  2925. card->irq = pcidev->irq;
  2926. card->magic = ESS_CARD_MAGIC;
  2927. spin_lock_init(&card->lock);
  2928. init_waitqueue_head(&card->suspend_queue);
  2929. card->dock_mute_vol = 50;
  2930. /* init our groups of 6 apus */
  2931. for(i=0;i<NR_DSPS;i++)
  2932. {
  2933. struct ess_state *s=&card->channels[i];
  2934. s->index = i;
  2935. s->card = card;
  2936. init_waitqueue_head(&s->dma_adc.wait);
  2937. init_waitqueue_head(&s->dma_dac.wait);
  2938. init_waitqueue_head(&s->open_wait);
  2939. spin_lock_init(&s->lock);
  2940. init_MUTEX(&s->open_sem);
  2941. s->magic = ESS_STATE_MAGIC;
  2942. s->apu[0] = 6*i;
  2943. s->apu[1] = (6*i)+1;
  2944. s->apu[2] = (6*i)+2;
  2945. s->apu[3] = (6*i)+3;
  2946. s->apu[4] = (6*i)+4;
  2947. s->apu[5] = (6*i)+5;
  2948. if(s->dma_adc.ready || s->dma_dac.ready || s->dma_adc.rawbuf)
  2949. printk("maestro: BOTCH!\n");
  2950. /* register devices */
  2951. if ((s->dev_audio = register_sound_dsp(&ess_audio_fops, -1)) < 0)
  2952. break;
  2953. }
  2954. num = i;
  2955. /* clear the rest if we ran out of slots to register */
  2956. for(;i<NR_DSPS;i++)
  2957. {
  2958. struct ess_state *s=&card->channels[i];
  2959. s->dev_audio = -1;
  2960. }
  2961. ess = &card->channels[0];
  2962. /*
  2963. * Ok card ready. Begin setup proper
  2964. */
  2965. printk(KERN_INFO "maestro: Configuring %s found at IO 0x%04X IRQ %d\n",
  2966. card_names[card_type],iobase,card->irq);
  2967. pci_read_config_dword(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &n);
  2968. printk(KERN_INFO "maestro: subvendor id: 0x%08x\n",n);
  2969. /* turn off power management unless:
  2970. * - the user explicitly asks for it
  2971. * or
  2972. * - we're not a 2e, lesser chipps seem to have problems.
  2973. * - we're not on our _very_ small whitelist. some implemenetations
  2974. * really don't like the pm code, others require it.
  2975. * feel free to expand this as required.
  2976. */
  2977. #define SUBSYSTEM_VENDOR(x) (x&0xffff)
  2978. if( (use_pm != 1) &&
  2979. ((card_type != TYPE_MAESTRO2E) || (SUBSYSTEM_VENDOR(n) != 0x1028)))
  2980. use_pm = 0;
  2981. if(!use_pm)
  2982. printk(KERN_INFO "maestro: not attempting power management.\n");
  2983. else {
  2984. if(!parse_power(card,pcidev))
  2985. printk(KERN_INFO "maestro: no PCI power management interface found.\n");
  2986. else {
  2987. pci_read_config_dword(pcidev, card->power_regs, &n);
  2988. printk(KERN_INFO "maestro: PCI power management capability: 0x%x\n",n>>16);
  2989. }
  2990. }
  2991. maestro_config(card);
  2992. if(maestro_ac97_get(card, 0x00)==0x0080) {
  2993. printk(KERN_ERR "maestro: my goodness! you seem to have a pt101 codec, which is quite rare.\n"
  2994. "\tyou should tell someone about this.\n");
  2995. } else {
  2996. maestro_ac97_init(card);
  2997. }
  2998. if ((card->dev_mixer = register_sound_mixer(&ess_mixer_fops, -1)) < 0) {
  2999. printk("maestro: couldn't register mixer!\n");
  3000. } else {
  3001. memcpy(card->mix.mixer_state,mixer_defaults,sizeof(card->mix.mixer_state));
  3002. mixer_push_state(card);
  3003. }
  3004. if((ret=request_irq(card->irq, ess_interrupt, SA_SHIRQ, card_names[card_type], card)))
  3005. {
  3006. printk(KERN_ERR "maestro: unable to allocate irq %d,\n", card->irq);
  3007. unregister_sound_mixer(card->dev_mixer);
  3008. for(i=0;i<NR_DSPS;i++)
  3009. {
  3010. struct ess_state *s = &card->channels[i];
  3011. if(s->dev_audio != -1)
  3012. unregister_sound_dsp(s->dev_audio);
  3013. }
  3014. release_region(card->iobase, 256);
  3015. unregister_reboot_notifier(&maestro_nb);
  3016. kfree(card);
  3017. return ret;
  3018. }
  3019. /* Turn on hardware volume control interrupt.
  3020. This has to come after we grab the IRQ above,
  3021. or a crash will result on installation if a button has been pressed,
  3022. because in that case we'll get an immediate interrupt. */
  3023. n = inw(iobase+0x18);
  3024. n|=(1<<6);
  3025. outw(n, iobase+0x18);
  3026. pci_set_drvdata(pcidev,card);
  3027. /* now go to sleep 'till something interesting happens */
  3028. maestro_power(card,ACPI_D2);
  3029. printk(KERN_INFO "maestro: %d channels configured.\n", num);
  3030. return 0;
  3031. }
  3032. static void maestro_remove(struct pci_dev *pcidev) {
  3033. struct ess_card *card = pci_get_drvdata(pcidev);
  3034. int i;
  3035. u32 n;
  3036. /* XXX maybe should force stop bob, but should be all
  3037. stopped by _release by now */
  3038. /* Turn off hardware volume control interrupt.
  3039. This has to come before we leave the IRQ below,
  3040. or a crash results if a button is pressed ! */
  3041. n = inw(card->iobase+0x18);
  3042. n&=~(1<<6);
  3043. outw(n, card->iobase+0x18);
  3044. free_irq(card->irq, card);
  3045. unregister_sound_mixer(card->dev_mixer);
  3046. for(i=0;i<NR_DSPS;i++)
  3047. {
  3048. struct ess_state *ess = &card->channels[i];
  3049. if(ess->dev_audio != -1)
  3050. unregister_sound_dsp(ess->dev_audio);
  3051. }
  3052. /* Goodbye, Mr. Bond. */
  3053. maestro_power(card,ACPI_D3);
  3054. release_region(card->iobase, 256);
  3055. kfree(card);
  3056. pci_set_drvdata(pcidev,NULL);
  3057. }
  3058. static struct pci_device_id maestro_pci_tbl[] = {
  3059. {PCI_VENDOR_ESS, PCI_DEVICE_ID_ESS_ESS1968, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_MAESTRO2},
  3060. {PCI_VENDOR_ESS, PCI_DEVICE_ID_ESS_ESS1978, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_MAESTRO2E},
  3061. {PCI_VENDOR_ESS_OLD, PCI_DEVICE_ID_ESS_ESS0100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_MAESTRO},
  3062. {0,}
  3063. };
  3064. MODULE_DEVICE_TABLE(pci, maestro_pci_tbl);
  3065. static struct pci_driver maestro_pci_driver = {
  3066. .name = "maestro",
  3067. .id_table = maestro_pci_tbl,
  3068. .probe = maestro_probe,
  3069. .remove = maestro_remove,
  3070. };
  3071. static int __init init_maestro(void)
  3072. {
  3073. int rc;
  3074. rc = pci_module_init(&maestro_pci_driver);
  3075. if (rc < 0)
  3076. return rc;
  3077. if (register_reboot_notifier(&maestro_nb))
  3078. printk(KERN_WARNING "maestro: reboot notifier registration failed; may not reboot properly.\n");
  3079. #ifdef MODULE
  3080. printk(version);
  3081. #endif
  3082. if (dsps_order < 0) {
  3083. dsps_order = 1;
  3084. printk(KERN_WARNING "maestro: clipping dsps_order to %d\n",dsps_order);
  3085. }
  3086. else if (dsps_order > MAX_DSP_ORDER) {
  3087. dsps_order = MAX_DSP_ORDER;
  3088. printk(KERN_WARNING "maestro: clipping dsps_order to %d\n",dsps_order);
  3089. }
  3090. return 0;
  3091. }
  3092. static int maestro_notifier(struct notifier_block *nb, unsigned long event, void *buf)
  3093. {
  3094. /* this notifier is called when the kernel is really shut down. */
  3095. M_printk("maestro: shutting down\n");
  3096. /* this will remove all card instances too */
  3097. pci_unregister_driver(&maestro_pci_driver);
  3098. /* XXX dunno about power management */
  3099. return NOTIFY_OK;
  3100. }
  3101. /* --------------------------------------------------------------------- */
  3102. static void cleanup_maestro(void) {
  3103. M_printk("maestro: unloading\n");
  3104. pci_unregister_driver(&maestro_pci_driver);
  3105. pm_unregister_all(maestro_pm_callback);
  3106. unregister_reboot_notifier(&maestro_nb);
  3107. }
  3108. /* --------------------------------------------------------------------- */
  3109. void
  3110. check_suspend(struct ess_card *card)
  3111. {
  3112. DECLARE_WAITQUEUE(wait, current);
  3113. if(!card->in_suspend) return;
  3114. card->in_suspend++;
  3115. add_wait_queue(&(card->suspend_queue), &wait);
  3116. current->state = TASK_UNINTERRUPTIBLE;
  3117. schedule();
  3118. remove_wait_queue(&(card->suspend_queue), &wait);
  3119. current->state = TASK_RUNNING;
  3120. }
  3121. static int
  3122. maestro_suspend(struct ess_card *card)
  3123. {
  3124. unsigned long flags;
  3125. int i,j;
  3126. spin_lock_irqsave(&card->lock,flags); /* over-kill */
  3127. M_printk("maestro: apm in dev %p\n",card);
  3128. /* we have to read from the apu regs, need
  3129. to power it up */
  3130. maestro_power(card,ACPI_D0);
  3131. for(i=0;i<NR_DSPS;i++) {
  3132. struct ess_state *s = &card->channels[i];
  3133. if(s->dev_audio == -1)
  3134. continue;
  3135. M_printk("maestro: stopping apus for device %d\n",i);
  3136. stop_dac(s);
  3137. stop_adc(s);
  3138. for(j=0;j<6;j++)
  3139. card->apu_map[s->apu[j]][5]=apu_get_register(s,j,5);
  3140. }
  3141. /* get rid of interrupts? */
  3142. if( card->dsps_open > 0)
  3143. stop_bob(&card->channels[0]);
  3144. card->in_suspend++;
  3145. spin_unlock_irqrestore(&card->lock,flags);
  3146. /* we trust in the bios to power down the chip on suspend.
  3147. * XXX I'm also not sure that in_suspend will protect
  3148. * against all reg accesses from here on out.
  3149. */
  3150. return 0;
  3151. }
  3152. static int
  3153. maestro_resume(struct ess_card *card)
  3154. {
  3155. unsigned long flags;
  3156. int i;
  3157. spin_lock_irqsave(&card->lock,flags); /* over-kill */
  3158. card->in_suspend = 0;
  3159. M_printk("maestro: resuming card at %p\n",card);
  3160. /* restore all our config */
  3161. maestro_config(card);
  3162. /* need to restore the base pointers.. */
  3163. if(card->dmapages)
  3164. set_base_registers(&card->channels[0],card->dmapages);
  3165. mixer_push_state(card);
  3166. /* set each channels' apu control registers before
  3167. * restoring audio
  3168. */
  3169. for(i=0;i<NR_DSPS;i++) {
  3170. struct ess_state *s = &card->channels[i];
  3171. int chan,reg;
  3172. if(s->dev_audio == -1)
  3173. continue;
  3174. for(chan = 0 ; chan < 6 ; chan++) {
  3175. wave_set_register(s,s->apu[chan]<<3,s->apu_base[chan]);
  3176. for(reg = 1 ; reg < NR_APU_REGS ; reg++)
  3177. apu_set_register(s,chan,reg,s->card->apu_map[s->apu[chan]][reg]);
  3178. }
  3179. for(chan = 0 ; chan < 6 ; chan++)
  3180. apu_set_register(s,chan,0,s->card->apu_map[s->apu[chan]][0] & 0xFF0F);
  3181. }
  3182. /* now we flip on the music */
  3183. if( card->dsps_open <= 0) {
  3184. /* this card's idle */
  3185. maestro_power(card,ACPI_D2);
  3186. } else {
  3187. /* ok, we're actually playing things on
  3188. this card */
  3189. maestro_power(card,ACPI_D0);
  3190. start_bob(&card->channels[0]);
  3191. for(i=0;i<NR_DSPS;i++) {
  3192. struct ess_state *s = &card->channels[i];
  3193. /* these use the apu_mode, and can handle
  3194. spurious calls */
  3195. start_dac(s);
  3196. start_adc(s);
  3197. }
  3198. }
  3199. spin_unlock_irqrestore(&card->lock,flags);
  3200. /* all right, we think things are ready,
  3201. wake up people who were using the device
  3202. when we suspended */
  3203. wake_up(&(card->suspend_queue));
  3204. return 0;
  3205. }
  3206. int
  3207. maestro_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
  3208. {
  3209. struct ess_card *card = (struct ess_card*) dev->data;
  3210. if ( ! card ) goto out;
  3211. M_printk("maestro: pm event 0x%x received for card %p\n", rqst, card);
  3212. switch (rqst) {
  3213. case PM_SUSPEND:
  3214. maestro_suspend(card);
  3215. break;
  3216. case PM_RESUME:
  3217. maestro_resume(card);
  3218. break;
  3219. /*
  3220. * we'd also like to find out about
  3221. * power level changes because some biosen
  3222. * do mean things to the maestro when they
  3223. * change their power state.
  3224. */
  3225. }
  3226. out:
  3227. return 0;
  3228. }
  3229. module_init(init_maestro);
  3230. module_exit(cleanup_maestro);