maestro.c 102 KB

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