i810_audio.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656
  1. /*
  2. * Intel i810 and friends ICH driver for Linux
  3. * Alan Cox <alan@redhat.com>
  4. *
  5. * Built from:
  6. * Low level code: Zach Brown (original nonworking i810 OSS driver)
  7. * Jaroslav Kysela <perex@suse.cz> (working ALSA driver)
  8. *
  9. * Framework: Thomas Sailer <sailer@ife.ee.ethz.ch>
  10. * Extended by: Zach Brown <zab@redhat.com>
  11. * and others..
  12. *
  13. * Hardware Provided By:
  14. * Analog Devices (A major AC97 codec maker)
  15. * Intel Corp (you've probably heard of them already)
  16. *
  17. * AC97 clues and assistance provided by
  18. * Analog Devices
  19. * Zach 'Fufu' Brown
  20. * Jeff Garzik
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. *
  36. *
  37. * Intel 810 theory of operation
  38. *
  39. * The chipset provides three DMA channels that talk to an AC97
  40. * CODEC (AC97 is a digital/analog mixer standard). At its simplest
  41. * you get 48Khz audio with basic volume and mixer controls. At the
  42. * best you get rate adaption in the codec. We set the card up so
  43. * that we never take completion interrupts but instead keep the card
  44. * chasing its tail around a ring buffer. This is needed for mmap
  45. * mode audio and happens to work rather well for non-mmap modes too.
  46. *
  47. * The board has one output channel for PCM audio (supported) and
  48. * a stereo line in and mono microphone input. Again these are normally
  49. * locked to 48Khz only. Right now recording is not finished.
  50. *
  51. * There is no midi support, no synth support. Use timidity. To get
  52. * esd working you need to use esd -r 48000 as it won't probe 48KHz
  53. * by default. mpg123 can't handle 48Khz only audio so use xmms.
  54. *
  55. * Fix The Sound On Dell
  56. *
  57. * Not everyone uses 48KHz. We know of no way to detect this reliably
  58. * and certainly not to get the right data. If your i810 audio sounds
  59. * stupid you may need to investigate other speeds. According to Analog
  60. * they tend to use a 14.318MHz clock which gives you a base rate of
  61. * 41194Hz.
  62. *
  63. * This is available via the 'ftsodell=1' option.
  64. *
  65. * If you need to force a specific rate set the clocking= option
  66. *
  67. * This driver is cursed. (Ben LaHaise)
  68. *
  69. * ICH 3 caveats
  70. * Intel errata #7 for ICH3 IO. We need to disable SMI stuff
  71. * when codec probing. [Not Yet Done]
  72. *
  73. * ICH 4 caveats
  74. *
  75. * The ICH4 has the feature, that the codec ID doesn't have to be
  76. * congruent with the IO connection.
  77. *
  78. * Therefore, from driver version 0.23 on, there is a "codec ID" <->
  79. * "IO register base offset" mapping (card->ac97_id_map) field.
  80. *
  81. * Juergen "George" Sawinski (jsaw)
  82. */
  83. #include <linux/module.h>
  84. #include <linux/string.h>
  85. #include <linux/ctype.h>
  86. #include <linux/ioport.h>
  87. #include <linux/sched.h>
  88. #include <linux/delay.h>
  89. #include <linux/sound.h>
  90. #include <linux/slab.h>
  91. #include <linux/soundcard.h>
  92. #include <linux/pci.h>
  93. #include <linux/interrupt.h>
  94. #include <asm/io.h>
  95. #include <asm/dma.h>
  96. #include <linux/init.h>
  97. #include <linux/poll.h>
  98. #include <linux/spinlock.h>
  99. #include <linux/smp_lock.h>
  100. #include <linux/ac97_codec.h>
  101. #include <linux/bitops.h>
  102. #include <linux/mutex.h>
  103. #include <linux/mm.h>
  104. #include <asm/uaccess.h>
  105. #define DRIVER_VERSION "1.01"
  106. #define MODULOP2(a, b) ((a) & ((b) - 1))
  107. #define MASKP2(a, b) ((a) & ~((b) - 1))
  108. static int ftsodell;
  109. static int strict_clocking;
  110. static unsigned int clocking;
  111. static int spdif_locked;
  112. static int ac97_quirk = AC97_TUNE_DEFAULT;
  113. //#define DEBUG
  114. //#define DEBUG2
  115. //#define DEBUG_INTERRUPTS
  116. //#define DEBUG_MMAP
  117. //#define DEBUG_MMIO
  118. #define ADC_RUNNING 1
  119. #define DAC_RUNNING 2
  120. #define I810_FMT_16BIT 1
  121. #define I810_FMT_STEREO 2
  122. #define I810_FMT_MASK 3
  123. #define SPDIF_ON 0x0004
  124. #define SURR_ON 0x0010
  125. #define CENTER_LFE_ON 0x0020
  126. #define VOL_MUTED 0x8000
  127. /* the 810's array of pointers to data buffers */
  128. struct sg_item {
  129. #define BUSADDR_MASK 0xFFFFFFFE
  130. u32 busaddr;
  131. #define CON_IOC 0x80000000 /* interrupt on completion */
  132. #define CON_BUFPAD 0x40000000 /* pad underrun with last sample, else 0 */
  133. #define CON_BUFLEN_MASK 0x0000ffff /* buffer length in samples */
  134. u32 control;
  135. };
  136. /* an instance of the i810 channel */
  137. #define SG_LEN 32
  138. struct i810_channel
  139. {
  140. /* these sg guys should probably be allocated
  141. separately as nocache. Must be 8 byte aligned */
  142. struct sg_item sg[SG_LEN]; /* 32*8 */
  143. u32 offset; /* 4 */
  144. u32 port; /* 4 */
  145. u32 used;
  146. u32 num;
  147. };
  148. /*
  149. * we have 3 separate dma engines. pcm in, pcm out, and mic.
  150. * each dma engine has controlling registers. These goofy
  151. * names are from the datasheet, but make it easy to write
  152. * code while leafing through it.
  153. *
  154. * ICH4 has 6 dma engines, pcm in, pcm out, mic, pcm in 2,
  155. * mic in 2, s/pdif. Of special interest is the fact that
  156. * the upper 3 DMA engines on the ICH4 *must* be accessed
  157. * via mmio access instead of pio access.
  158. */
  159. #define ENUM_ENGINE(PRE,DIG) \
  160. enum { \
  161. PRE##_BASE = 0x##DIG##0, /* Base Address */ \
  162. PRE##_BDBAR = 0x##DIG##0, /* Buffer Descriptor list Base Address */ \
  163. PRE##_CIV = 0x##DIG##4, /* Current Index Value */ \
  164. PRE##_LVI = 0x##DIG##5, /* Last Valid Index */ \
  165. PRE##_SR = 0x##DIG##6, /* Status Register */ \
  166. PRE##_PICB = 0x##DIG##8, /* Position In Current Buffer */ \
  167. PRE##_PIV = 0x##DIG##a, /* Prefetched Index Value */ \
  168. PRE##_CR = 0x##DIG##b /* Control Register */ \
  169. }
  170. ENUM_ENGINE(OFF,0); /* Offsets */
  171. ENUM_ENGINE(PI,0); /* PCM In */
  172. ENUM_ENGINE(PO,1); /* PCM Out */
  173. ENUM_ENGINE(MC,2); /* Mic In */
  174. enum {
  175. GLOB_CNT = 0x2c, /* Global Control */
  176. GLOB_STA = 0x30, /* Global Status */
  177. CAS = 0x34 /* Codec Write Semaphore Register */
  178. };
  179. ENUM_ENGINE(MC2,4); /* Mic In 2 */
  180. ENUM_ENGINE(PI2,5); /* PCM In 2 */
  181. ENUM_ENGINE(SP,6); /* S/PDIF */
  182. enum {
  183. SDM = 0x80 /* SDATA_IN Map Register */
  184. };
  185. /* interrupts for a dma engine */
  186. #define DMA_INT_FIFO (1<<4) /* fifo under/over flow */
  187. #define DMA_INT_COMPLETE (1<<3) /* buffer read/write complete and ioc set */
  188. #define DMA_INT_LVI (1<<2) /* last valid done */
  189. #define DMA_INT_CELV (1<<1) /* last valid is current */
  190. #define DMA_INT_DCH (1) /* DMA Controller Halted (happens on LVI interrupts) */
  191. #define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
  192. /* interrupts for the whole chip */
  193. #define INT_SEC (1<<11)
  194. #define INT_PRI (1<<10)
  195. #define INT_MC (1<<7)
  196. #define INT_PO (1<<6)
  197. #define INT_PI (1<<5)
  198. #define INT_MO (1<<2)
  199. #define INT_NI (1<<1)
  200. #define INT_GPI (1<<0)
  201. #define INT_MASK (INT_SEC|INT_PRI|INT_MC|INT_PO|INT_PI|INT_MO|INT_NI|INT_GPI)
  202. /* magic numbers to protect our data structures */
  203. #define I810_CARD_MAGIC 0x5072696E /* "Prin" */
  204. #define I810_STATE_MAGIC 0x63657373 /* "cess" */
  205. #define I810_DMA_MASK 0xffffffff /* DMA buffer mask for pci_alloc_consist */
  206. #define NR_HW_CH 3
  207. /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
  208. #define NR_AC97 4
  209. /* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
  210. /* stream at a minimum for this card to be happy */
  211. static const unsigned sample_size[] = { 1, 2, 2, 4 };
  212. /* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
  213. /* values are one less than might be expected */
  214. static const unsigned sample_shift[] = { -1, 0, 0, 1 };
  215. enum {
  216. ICH82801AA = 0,
  217. ICH82901AB,
  218. INTEL440MX,
  219. INTELICH2,
  220. INTELICH3,
  221. INTELICH4,
  222. INTELICH5,
  223. SI7012,
  224. NVIDIA_NFORCE,
  225. AMD768,
  226. AMD8111
  227. };
  228. static char * card_names[] = {
  229. "Intel ICH 82801AA",
  230. "Intel ICH 82901AB",
  231. "Intel 440MX",
  232. "Intel ICH2",
  233. "Intel ICH3",
  234. "Intel ICH4",
  235. "Intel ICH5",
  236. "SiS 7012",
  237. "NVIDIA nForce Audio",
  238. "AMD 768",
  239. "AMD-8111 IOHub"
  240. };
  241. /* These are capabilities (and bugs) the chipsets _can_ have */
  242. static struct {
  243. int16_t nr_ac97;
  244. #define CAP_MMIO 0x0001
  245. #define CAP_20BIT_AUDIO_SUPPORT 0x0002
  246. u_int16_t flags;
  247. } card_cap[] = {
  248. { 1, 0x0000 }, /* ICH82801AA */
  249. { 1, 0x0000 }, /* ICH82901AB */
  250. { 1, 0x0000 }, /* INTEL440MX */
  251. { 1, 0x0000 }, /* INTELICH2 */
  252. { 2, 0x0000 }, /* INTELICH3 */
  253. { 3, 0x0003 }, /* INTELICH4 */
  254. { 3, 0x0003 }, /* INTELICH5 */
  255. /*@FIXME to be verified*/ { 2, 0x0000 }, /* SI7012 */
  256. /*@FIXME to be verified*/ { 2, 0x0000 }, /* NVIDIA_NFORCE */
  257. /*@FIXME to be verified*/ { 2, 0x0000 }, /* AMD768 */
  258. /*@FIXME to be verified*/ { 3, 0x0001 }, /* AMD8111 */
  259. };
  260. static struct pci_device_id i810_pci_tbl [] = {
  261. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_5,
  262. PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82801AA},
  263. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_5,
  264. PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82901AB},
  265. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_440MX,
  266. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTEL440MX},
  267. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_4,
  268. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH2},
  269. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_5,
  270. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH3},
  271. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_5,
  272. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
  273. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_5,
  274. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH5},
  275. {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7012,
  276. PCI_ANY_ID, PCI_ANY_ID, 0, 0, SI7012},
  277. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO,
  278. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  279. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO,
  280. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  281. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO,
  282. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  283. {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7445,
  284. PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD768},
  285. {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_AUDIO,
  286. PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD8111},
  287. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_5,
  288. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
  289. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_18,
  290. PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
  291. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_AUDIO,
  292. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  293. {0,}
  294. };
  295. MODULE_DEVICE_TABLE (pci, i810_pci_tbl);
  296. #ifdef CONFIG_PM
  297. #define PM_SUSPENDED(card) (card->pm_suspended)
  298. #else
  299. #define PM_SUSPENDED(card) (0)
  300. #endif
  301. /* "software" or virtual channel, an instance of opened /dev/dsp */
  302. struct i810_state {
  303. unsigned int magic;
  304. struct i810_card *card; /* Card info */
  305. /* single open lock mechanism, only used for recording */
  306. struct mutex open_mutex;
  307. wait_queue_head_t open_wait;
  308. /* file mode */
  309. mode_t open_mode;
  310. /* virtual channel number */
  311. int virt;
  312. #ifdef CONFIG_PM
  313. unsigned int pm_saved_dac_rate,pm_saved_adc_rate;
  314. #endif
  315. struct dmabuf {
  316. /* wave sample stuff */
  317. unsigned int rate;
  318. unsigned char fmt, enable, trigger;
  319. /* hardware channel */
  320. struct i810_channel *read_channel;
  321. struct i810_channel *write_channel;
  322. /* OSS buffer management stuff */
  323. void *rawbuf;
  324. dma_addr_t dma_handle;
  325. unsigned buforder;
  326. unsigned numfrag;
  327. unsigned fragshift;
  328. /* our buffer acts like a circular ring */
  329. unsigned hwptr; /* where dma last started, updated by update_ptr */
  330. unsigned swptr; /* where driver last clear/filled, updated by read/write */
  331. int count; /* bytes to be consumed or been generated by dma machine */
  332. unsigned total_bytes; /* total bytes dmaed by hardware */
  333. unsigned error; /* number of over/underruns */
  334. wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
  335. /* redundant, but makes calculations easier */
  336. /* what the hardware uses */
  337. unsigned dmasize;
  338. unsigned fragsize;
  339. unsigned fragsamples;
  340. /* what we tell the user to expect */
  341. unsigned userfrags;
  342. unsigned userfragsize;
  343. /* OSS stuff */
  344. unsigned mapped:1;
  345. unsigned ready:1;
  346. unsigned update_flag;
  347. unsigned ossfragsize;
  348. unsigned ossmaxfrags;
  349. unsigned subdivision;
  350. } dmabuf;
  351. };
  352. struct i810_card {
  353. unsigned int magic;
  354. /* We keep i810 cards in a linked list */
  355. struct i810_card *next;
  356. /* The i810 has a certain amount of cross channel interaction
  357. so we use a single per card lock */
  358. spinlock_t lock;
  359. /* Control AC97 access serialization */
  360. spinlock_t ac97_lock;
  361. /* PCI device stuff */
  362. struct pci_dev * pci_dev;
  363. u16 pci_id;
  364. u16 pci_id_internal; /* used to access card_cap[] */
  365. #ifdef CONFIG_PM
  366. u16 pm_suspended;
  367. int pm_saved_mixer_settings[SOUND_MIXER_NRDEVICES][NR_AC97];
  368. #endif
  369. /* soundcore stuff */
  370. int dev_audio;
  371. /* structures for abstraction of hardware facilities, codecs, banks and channels*/
  372. u16 ac97_id_map[NR_AC97];
  373. struct ac97_codec *ac97_codec[NR_AC97];
  374. struct i810_state *states[NR_HW_CH];
  375. struct i810_channel *channel; /* 1:1 to states[] but diff. lifetime */
  376. dma_addr_t chandma;
  377. u16 ac97_features;
  378. u16 ac97_status;
  379. u16 channels;
  380. /* hardware resources */
  381. unsigned long ac97base;
  382. unsigned long iobase;
  383. u32 irq;
  384. unsigned long ac97base_mmio_phys;
  385. unsigned long iobase_mmio_phys;
  386. u_int8_t __iomem *ac97base_mmio;
  387. u_int8_t __iomem *iobase_mmio;
  388. int use_mmio;
  389. /* Function support */
  390. struct i810_channel *(*alloc_pcm_channel)(struct i810_card *);
  391. struct i810_channel *(*alloc_rec_pcm_channel)(struct i810_card *);
  392. struct i810_channel *(*alloc_rec_mic_channel)(struct i810_card *);
  393. void (*free_pcm_channel)(struct i810_card *, int chan);
  394. /* We have a *very* long init time possibly, so use this to block */
  395. /* attempts to open our devices before we are ready (stops oops'es) */
  396. int initializing;
  397. };
  398. /* extract register offset from codec struct */
  399. #define IO_REG_OFF(codec) (((struct i810_card *) codec->private_data)->ac97_id_map[codec->id])
  400. #define I810_IOREAD(size, type, card, off) \
  401. ({ \
  402. type val; \
  403. if (card->use_mmio) \
  404. val=read##size(card->iobase_mmio+off); \
  405. else \
  406. val=in##size(card->iobase+off); \
  407. val; \
  408. })
  409. #define I810_IOREADL(card, off) I810_IOREAD(l, u32, card, off)
  410. #define I810_IOREADW(card, off) I810_IOREAD(w, u16, card, off)
  411. #define I810_IOREADB(card, off) I810_IOREAD(b, u8, card, off)
  412. #define I810_IOWRITE(size, val, card, off) \
  413. ({ \
  414. if (card->use_mmio) \
  415. write##size(val, card->iobase_mmio+off); \
  416. else \
  417. out##size(val, card->iobase+off); \
  418. })
  419. #define I810_IOWRITEL(val, card, off) I810_IOWRITE(l, val, card, off)
  420. #define I810_IOWRITEW(val, card, off) I810_IOWRITE(w, val, card, off)
  421. #define I810_IOWRITEB(val, card, off) I810_IOWRITE(b, val, card, off)
  422. #define GET_CIV(card, port) MODULOP2(I810_IOREADB((card), (port) + OFF_CIV), SG_LEN)
  423. #define GET_LVI(card, port) MODULOP2(I810_IOREADB((card), (port) + OFF_LVI), SG_LEN)
  424. /* set LVI from CIV */
  425. #define CIV_TO_LVI(card, port, off) \
  426. I810_IOWRITEB(MODULOP2(GET_CIV((card), (port)) + (off), SG_LEN), (card), (port) + OFF_LVI)
  427. static struct ac97_quirk ac97_quirks[] __devinitdata = {
  428. {
  429. .vendor = 0x0e11,
  430. .device = 0x00b8,
  431. .name = "Compaq Evo D510C",
  432. .type = AC97_TUNE_HP_ONLY
  433. },
  434. {
  435. .vendor = 0x1028,
  436. .device = 0x00d8,
  437. .name = "Dell Precision 530", /* AD1885 */
  438. .type = AC97_TUNE_HP_ONLY
  439. },
  440. {
  441. .vendor = 0x1028,
  442. .device = 0x0126,
  443. .name = "Dell Optiplex GX260", /* AD1981A */
  444. .type = AC97_TUNE_HP_ONLY
  445. },
  446. {
  447. .vendor = 0x1028,
  448. .device = 0x012d,
  449. .name = "Dell Precision 450", /* AD1981B*/
  450. .type = AC97_TUNE_HP_ONLY
  451. },
  452. { /* FIXME: which codec? */
  453. .vendor = 0x103c,
  454. .device = 0x00c3,
  455. .name = "Hewlett-Packard onboard",
  456. .type = AC97_TUNE_HP_ONLY
  457. },
  458. {
  459. .vendor = 0x103c,
  460. .device = 0x12f1,
  461. .name = "HP xw8200", /* AD1981B*/
  462. .type = AC97_TUNE_HP_ONLY
  463. },
  464. {
  465. .vendor = 0x103c,
  466. .device = 0x3008,
  467. .name = "HP xw4200", /* AD1981B*/
  468. .type = AC97_TUNE_HP_ONLY
  469. },
  470. {
  471. .vendor = 0x10f1,
  472. .device = 0x2665,
  473. .name = "Fujitsu-Siemens Celsius", /* AD1981? */
  474. .type = AC97_TUNE_HP_ONLY
  475. },
  476. {
  477. .vendor = 0x10f1,
  478. .device = 0x2885,
  479. .name = "AMD64 Mobo", /* ALC650 */
  480. .type = AC97_TUNE_HP_ONLY
  481. },
  482. {
  483. .vendor = 0x110a,
  484. .device = 0x0056,
  485. .name = "Fujitsu-Siemens Scenic", /* AD1981? */
  486. .type = AC97_TUNE_HP_ONLY
  487. },
  488. {
  489. .vendor = 0x11d4,
  490. .device = 0x5375,
  491. .name = "ADI AD1985 (discrete)",
  492. .type = AC97_TUNE_HP_ONLY
  493. },
  494. {
  495. .vendor = 0x1462,
  496. .device = 0x5470,
  497. .name = "MSI P4 ATX 645 Ultra",
  498. .type = AC97_TUNE_HP_ONLY
  499. },
  500. {
  501. .vendor = 0x1734,
  502. .device = 0x0088,
  503. .name = "Fujitsu-Siemens D1522", /* AD1981 */
  504. .type = AC97_TUNE_HP_ONLY
  505. },
  506. {
  507. .vendor = 0x8086,
  508. .device = 0x4856,
  509. .name = "Intel D845WN (82801BA)",
  510. .type = AC97_TUNE_SWAP_HP
  511. },
  512. {
  513. .vendor = 0x8086,
  514. .device = 0x4d44,
  515. .name = "Intel D850EMV2", /* AD1885 */
  516. .type = AC97_TUNE_HP_ONLY
  517. },
  518. {
  519. .vendor = 0x8086,
  520. .device = 0x4d56,
  521. .name = "Intel ICH/AD1885",
  522. .type = AC97_TUNE_HP_ONLY
  523. },
  524. {
  525. .vendor = 0x1028,
  526. .device = 0x012d,
  527. .name = "Dell Precision 450", /* AD1981B*/
  528. .type = AC97_TUNE_HP_ONLY
  529. },
  530. {
  531. .vendor = 0x103c,
  532. .device = 0x3008,
  533. .name = "HP xw4200", /* AD1981B*/
  534. .type = AC97_TUNE_HP_ONLY
  535. },
  536. {
  537. .vendor = 0x103c,
  538. .device = 0x12f1,
  539. .name = "HP xw8200", /* AD1981B*/
  540. .type = AC97_TUNE_HP_ONLY
  541. },
  542. { } /* terminator */
  543. };
  544. static struct i810_card *devs = NULL;
  545. static int i810_open_mixdev(struct inode *inode, struct file *file);
  546. static int i810_ioctl_mixdev(struct inode *inode, struct file *file,
  547. unsigned int cmd, unsigned long arg);
  548. static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg);
  549. static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
  550. static u16 i810_ac97_get_mmio(struct ac97_codec *dev, u8 reg);
  551. static void i810_ac97_set_mmio(struct ac97_codec *dev, u8 reg, u16 data);
  552. static u16 i810_ac97_get_io(struct ac97_codec *dev, u8 reg);
  553. static void i810_ac97_set_io(struct ac97_codec *dev, u8 reg, u16 data);
  554. static struct i810_channel *i810_alloc_pcm_channel(struct i810_card *card)
  555. {
  556. if(card->channel[1].used==1)
  557. return NULL;
  558. card->channel[1].used=1;
  559. return &card->channel[1];
  560. }
  561. static struct i810_channel *i810_alloc_rec_pcm_channel(struct i810_card *card)
  562. {
  563. if(card->channel[0].used==1)
  564. return NULL;
  565. card->channel[0].used=1;
  566. return &card->channel[0];
  567. }
  568. static struct i810_channel *i810_alloc_rec_mic_channel(struct i810_card *card)
  569. {
  570. if(card->channel[2].used==1)
  571. return NULL;
  572. card->channel[2].used=1;
  573. return &card->channel[2];
  574. }
  575. static void i810_free_pcm_channel(struct i810_card *card, int channel)
  576. {
  577. card->channel[channel].used=0;
  578. }
  579. static int i810_valid_spdif_rate ( struct ac97_codec *codec, int rate )
  580. {
  581. unsigned long id = 0L;
  582. id = (i810_ac97_get(codec, AC97_VENDOR_ID1) << 16);
  583. id |= i810_ac97_get(codec, AC97_VENDOR_ID2) & 0xffff;
  584. #ifdef DEBUG
  585. printk ( "i810_audio: codec = %s, codec_id = 0x%08lx\n", codec->name, id);
  586. #endif
  587. switch ( id ) {
  588. case 0x41445361: /* AD1886 */
  589. if (rate == 48000) {
  590. return 1;
  591. }
  592. break;
  593. default: /* all other codecs, until we know otherwiae */
  594. if (rate == 48000 || rate == 44100 || rate == 32000) {
  595. return 1;
  596. }
  597. break;
  598. }
  599. return (0);
  600. }
  601. /* i810_set_spdif_output
  602. *
  603. * Configure the S/PDIF output transmitter. When we turn on
  604. * S/PDIF, we turn off the analog output. This may not be
  605. * the right thing to do.
  606. *
  607. * Assumptions:
  608. * The DSP sample rate must already be set to a supported
  609. * S/PDIF rate (32kHz, 44.1kHz, or 48kHz) or we abort.
  610. */
  611. static int i810_set_spdif_output(struct i810_state *state, int slots, int rate)
  612. {
  613. int vol;
  614. int aud_reg;
  615. int r = 0;
  616. struct ac97_codec *codec = state->card->ac97_codec[0];
  617. if(!codec->codec_ops->digital) {
  618. state->card->ac97_status &= ~SPDIF_ON;
  619. } else {
  620. if ( slots == -1 ) { /* Turn off S/PDIF */
  621. codec->codec_ops->digital(codec, 0, 0, 0);
  622. /* If the volume wasn't muted before we turned on S/PDIF, unmute it */
  623. if ( !(state->card->ac97_status & VOL_MUTED) ) {
  624. aud_reg = i810_ac97_get(codec, AC97_MASTER_VOL_STEREO);
  625. i810_ac97_set(codec, AC97_MASTER_VOL_STEREO, (aud_reg & ~VOL_MUTED));
  626. }
  627. state->card->ac97_status &= ~(VOL_MUTED | SPDIF_ON);
  628. return 0;
  629. }
  630. vol = i810_ac97_get(codec, AC97_MASTER_VOL_STEREO);
  631. state->card->ac97_status = vol & VOL_MUTED;
  632. r = codec->codec_ops->digital(codec, slots, rate, 0);
  633. if(r)
  634. state->card->ac97_status |= SPDIF_ON;
  635. else
  636. state->card->ac97_status &= ~SPDIF_ON;
  637. /* Mute the analog output */
  638. /* Should this only mute the PCM volume??? */
  639. i810_ac97_set(codec, AC97_MASTER_VOL_STEREO, (vol | VOL_MUTED));
  640. }
  641. return r;
  642. }
  643. /* i810_set_dac_channels
  644. *
  645. * Configure the codec's multi-channel DACs
  646. *
  647. * The logic is backwards. Setting the bit to 1 turns off the DAC.
  648. *
  649. * What about the ICH? We currently configure it using the
  650. * SNDCTL_DSP_CHANNELS ioctl. If we're turnning on the DAC,
  651. * does that imply that we want the ICH set to support
  652. * these channels?
  653. *
  654. * TODO:
  655. * vailidate that the codec really supports these DACs
  656. * before turning them on.
  657. */
  658. static void i810_set_dac_channels(struct i810_state *state, int channel)
  659. {
  660. int aud_reg;
  661. struct ac97_codec *codec = state->card->ac97_codec[0];
  662. /* No codec, no setup */
  663. if(codec == NULL)
  664. return;
  665. aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
  666. aud_reg |= AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK;
  667. state->card->ac97_status &= ~(SURR_ON | CENTER_LFE_ON);
  668. switch ( channel ) {
  669. case 2: /* always enabled */
  670. break;
  671. case 4:
  672. aud_reg &= ~AC97_EA_PRJ;
  673. state->card->ac97_status |= SURR_ON;
  674. break;
  675. case 6:
  676. aud_reg &= ~(AC97_EA_PRJ | AC97_EA_PRI | AC97_EA_PRK);
  677. state->card->ac97_status |= SURR_ON | CENTER_LFE_ON;
  678. break;
  679. default:
  680. break;
  681. }
  682. i810_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
  683. }
  684. /* set playback sample rate */
  685. static unsigned int i810_set_dac_rate(struct i810_state * state, unsigned int rate)
  686. {
  687. struct dmabuf *dmabuf = &state->dmabuf;
  688. u32 new_rate;
  689. struct ac97_codec *codec=state->card->ac97_codec[0];
  690. if(!(state->card->ac97_features&0x0001))
  691. {
  692. dmabuf->rate = clocking;
  693. #ifdef DEBUG
  694. printk("Asked for %d Hz, but ac97_features says we only do %dHz. Sorry!\n",
  695. rate,clocking);
  696. #endif
  697. return clocking;
  698. }
  699. if (rate > 48000)
  700. rate = 48000;
  701. if (rate < 8000)
  702. rate = 8000;
  703. dmabuf->rate = rate;
  704. /*
  705. * Adjust for misclocked crap
  706. */
  707. rate = ( rate * clocking)/48000;
  708. if(strict_clocking && rate < 8000) {
  709. rate = 8000;
  710. dmabuf->rate = (rate * 48000)/clocking;
  711. }
  712. new_rate=ac97_set_dac_rate(codec, rate);
  713. if(new_rate != rate) {
  714. dmabuf->rate = (new_rate * 48000)/clocking;
  715. }
  716. #ifdef DEBUG
  717. printk("i810_audio: called i810_set_dac_rate : asked for %d, got %d\n", rate, dmabuf->rate);
  718. #endif
  719. rate = new_rate;
  720. return dmabuf->rate;
  721. }
  722. /* set recording sample rate */
  723. static unsigned int i810_set_adc_rate(struct i810_state * state, unsigned int rate)
  724. {
  725. struct dmabuf *dmabuf = &state->dmabuf;
  726. u32 new_rate;
  727. struct ac97_codec *codec=state->card->ac97_codec[0];
  728. if(!(state->card->ac97_features&0x0001))
  729. {
  730. dmabuf->rate = clocking;
  731. return clocking;
  732. }
  733. if (rate > 48000)
  734. rate = 48000;
  735. if (rate < 8000)
  736. rate = 8000;
  737. dmabuf->rate = rate;
  738. /*
  739. * Adjust for misclocked crap
  740. */
  741. rate = ( rate * clocking)/48000;
  742. if(strict_clocking && rate < 8000) {
  743. rate = 8000;
  744. dmabuf->rate = (rate * 48000)/clocking;
  745. }
  746. new_rate = ac97_set_adc_rate(codec, rate);
  747. if(new_rate != rate) {
  748. dmabuf->rate = (new_rate * 48000)/clocking;
  749. rate = new_rate;
  750. }
  751. #ifdef DEBUG
  752. printk("i810_audio: called i810_set_adc_rate : rate = %d/%d\n", dmabuf->rate, rate);
  753. #endif
  754. return dmabuf->rate;
  755. }
  756. /* get current playback/recording dma buffer pointer (byte offset from LBA),
  757. called with spinlock held! */
  758. static inline unsigned i810_get_dma_addr(struct i810_state *state, int rec)
  759. {
  760. struct dmabuf *dmabuf = &state->dmabuf;
  761. unsigned int civ, offset, port, port_picb, bytes = 2;
  762. if (!dmabuf->enable)
  763. return 0;
  764. if (rec)
  765. port = dmabuf->read_channel->port;
  766. else
  767. port = dmabuf->write_channel->port;
  768. if(state->card->pci_id == PCI_DEVICE_ID_SI_7012) {
  769. port_picb = port + OFF_SR;
  770. bytes = 1;
  771. } else
  772. port_picb = port + OFF_PICB;
  773. do {
  774. civ = GET_CIV(state->card, port);
  775. offset = I810_IOREADW(state->card, port_picb);
  776. /* Must have a delay here! */
  777. if(offset == 0)
  778. udelay(1);
  779. /* Reread both registers and make sure that that total
  780. * offset from the first reading to the second is 0.
  781. * There is an issue with SiS hardware where it will count
  782. * picb down to 0, then update civ to the next value,
  783. * then set the new picb to fragsize bytes. We can catch
  784. * it between the civ update and the picb update, making
  785. * it look as though we are 1 fragsize ahead of where we
  786. * are. The next to we get the address though, it will
  787. * be back in the right place, and we will suddenly think
  788. * we just went forward dmasize - fragsize bytes, causing
  789. * totally stupid *huge* dma overrun messages. We are
  790. * assuming that the 1us delay is more than long enough
  791. * that we won't have to worry about the chip still being
  792. * out of sync with reality ;-)
  793. */
  794. } while (civ != GET_CIV(state->card, port) || offset != I810_IOREADW(state->card, port_picb));
  795. return (((civ + 1) * dmabuf->fragsize - (bytes * offset))
  796. % dmabuf->dmasize);
  797. }
  798. /* Stop recording (lock held) */
  799. static inline void __stop_adc(struct i810_state *state)
  800. {
  801. struct dmabuf *dmabuf = &state->dmabuf;
  802. struct i810_card *card = state->card;
  803. dmabuf->enable &= ~ADC_RUNNING;
  804. I810_IOWRITEB(0, card, PI_CR);
  805. // wait for the card to acknowledge shutdown
  806. while( I810_IOREADB(card, PI_CR) != 0 ) ;
  807. // now clear any latent interrupt bits (like the halt bit)
  808. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  809. I810_IOWRITEB( I810_IOREADB(card, PI_PICB), card, PI_PICB );
  810. else
  811. I810_IOWRITEB( I810_IOREADB(card, PI_SR), card, PI_SR );
  812. I810_IOWRITEL( I810_IOREADL(card, GLOB_STA) & INT_PI, card, GLOB_STA);
  813. }
  814. static void stop_adc(struct i810_state *state)
  815. {
  816. struct i810_card *card = state->card;
  817. unsigned long flags;
  818. spin_lock_irqsave(&card->lock, flags);
  819. __stop_adc(state);
  820. spin_unlock_irqrestore(&card->lock, flags);
  821. }
  822. static inline void __start_adc(struct i810_state *state)
  823. {
  824. struct dmabuf *dmabuf = &state->dmabuf;
  825. if (dmabuf->count < dmabuf->dmasize && dmabuf->ready && !dmabuf->enable &&
  826. (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  827. dmabuf->enable |= ADC_RUNNING;
  828. // Interrupt enable, LVI enable, DMA enable
  829. I810_IOWRITEB(0x10 | 0x04 | 0x01, state->card, PI_CR);
  830. }
  831. }
  832. static void start_adc(struct i810_state *state)
  833. {
  834. struct i810_card *card = state->card;
  835. unsigned long flags;
  836. spin_lock_irqsave(&card->lock, flags);
  837. __start_adc(state);
  838. spin_unlock_irqrestore(&card->lock, flags);
  839. }
  840. /* stop playback (lock held) */
  841. static inline void __stop_dac(struct i810_state *state)
  842. {
  843. struct dmabuf *dmabuf = &state->dmabuf;
  844. struct i810_card *card = state->card;
  845. dmabuf->enable &= ~DAC_RUNNING;
  846. I810_IOWRITEB(0, card, PO_CR);
  847. // wait for the card to acknowledge shutdown
  848. while( I810_IOREADB(card, PO_CR) != 0 ) ;
  849. // now clear any latent interrupt bits (like the halt bit)
  850. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  851. I810_IOWRITEB( I810_IOREADB(card, PO_PICB), card, PO_PICB );
  852. else
  853. I810_IOWRITEB( I810_IOREADB(card, PO_SR), card, PO_SR );
  854. I810_IOWRITEL( I810_IOREADL(card, GLOB_STA) & INT_PO, card, GLOB_STA);
  855. }
  856. static void stop_dac(struct i810_state *state)
  857. {
  858. struct i810_card *card = state->card;
  859. unsigned long flags;
  860. spin_lock_irqsave(&card->lock, flags);
  861. __stop_dac(state);
  862. spin_unlock_irqrestore(&card->lock, flags);
  863. }
  864. static inline void __start_dac(struct i810_state *state)
  865. {
  866. struct dmabuf *dmabuf = &state->dmabuf;
  867. if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
  868. (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  869. dmabuf->enable |= DAC_RUNNING;
  870. // Interrupt enable, LVI enable, DMA enable
  871. I810_IOWRITEB(0x10 | 0x04 | 0x01, state->card, PO_CR);
  872. }
  873. }
  874. static void start_dac(struct i810_state *state)
  875. {
  876. struct i810_card *card = state->card;
  877. unsigned long flags;
  878. spin_lock_irqsave(&card->lock, flags);
  879. __start_dac(state);
  880. spin_unlock_irqrestore(&card->lock, flags);
  881. }
  882. #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
  883. #define DMABUF_MINORDER 1
  884. /* allocate DMA buffer, playback and recording buffer should be allocated separately */
  885. static int alloc_dmabuf(struct i810_state *state)
  886. {
  887. struct dmabuf *dmabuf = &state->dmabuf;
  888. void *rawbuf= NULL;
  889. int order, size;
  890. struct page *page, *pend;
  891. /* If we don't have any oss frag params, then use our default ones */
  892. if(dmabuf->ossmaxfrags == 0)
  893. dmabuf->ossmaxfrags = 4;
  894. if(dmabuf->ossfragsize == 0)
  895. dmabuf->ossfragsize = (PAGE_SIZE<<DMABUF_DEFAULTORDER)/dmabuf->ossmaxfrags;
  896. size = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
  897. if(dmabuf->rawbuf && (PAGE_SIZE << dmabuf->buforder) == size)
  898. return 0;
  899. /* alloc enough to satisfy the oss params */
  900. for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
  901. if ( (PAGE_SIZE<<order) > size )
  902. continue;
  903. if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
  904. PAGE_SIZE << order,
  905. &dmabuf->dma_handle)))
  906. break;
  907. }
  908. if (!rawbuf)
  909. return -ENOMEM;
  910. #ifdef DEBUG
  911. printk("i810_audio: allocated %ld (order = %d) bytes at %p\n",
  912. PAGE_SIZE << order, order, rawbuf);
  913. #endif
  914. dmabuf->ready = dmabuf->mapped = 0;
  915. dmabuf->rawbuf = rawbuf;
  916. dmabuf->buforder = order;
  917. /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
  918. pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
  919. for (page = virt_to_page(rawbuf); page <= pend; page++)
  920. SetPageReserved(page);
  921. return 0;
  922. }
  923. /* free DMA buffer */
  924. static void dealloc_dmabuf(struct i810_state *state)
  925. {
  926. struct dmabuf *dmabuf = &state->dmabuf;
  927. struct page *page, *pend;
  928. if (dmabuf->rawbuf) {
  929. /* undo marking the pages as reserved */
  930. pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
  931. for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
  932. ClearPageReserved(page);
  933. pci_free_consistent(state->card->pci_dev, PAGE_SIZE << dmabuf->buforder,
  934. dmabuf->rawbuf, dmabuf->dma_handle);
  935. }
  936. dmabuf->rawbuf = NULL;
  937. dmabuf->mapped = dmabuf->ready = 0;
  938. }
  939. static int prog_dmabuf(struct i810_state *state, unsigned rec)
  940. {
  941. struct dmabuf *dmabuf = &state->dmabuf;
  942. struct i810_channel *c;
  943. struct sg_item *sg;
  944. unsigned long flags;
  945. int ret;
  946. unsigned fragint;
  947. int i;
  948. spin_lock_irqsave(&state->card->lock, flags);
  949. if(dmabuf->enable & DAC_RUNNING)
  950. __stop_dac(state);
  951. if(dmabuf->enable & ADC_RUNNING)
  952. __stop_adc(state);
  953. dmabuf->total_bytes = 0;
  954. dmabuf->count = dmabuf->error = 0;
  955. dmabuf->swptr = dmabuf->hwptr = 0;
  956. spin_unlock_irqrestore(&state->card->lock, flags);
  957. /* allocate DMA buffer, let alloc_dmabuf determine if we are already
  958. * allocated well enough or if we should replace the current buffer
  959. * (assuming one is already allocated, if it isn't, then allocate it).
  960. */
  961. if ((ret = alloc_dmabuf(state)))
  962. return ret;
  963. /* FIXME: figure out all this OSS fragment stuff */
  964. /* I did, it now does what it should according to the OSS API. DL */
  965. /* We may not have realloced our dmabuf, but the fragment size to
  966. * fragment number ratio may have changed, so go ahead and reprogram
  967. * things
  968. */
  969. dmabuf->dmasize = PAGE_SIZE << dmabuf->buforder;
  970. dmabuf->numfrag = SG_LEN;
  971. dmabuf->fragsize = dmabuf->dmasize/dmabuf->numfrag;
  972. dmabuf->fragsamples = dmabuf->fragsize >> 1;
  973. dmabuf->fragshift = ffs(dmabuf->fragsize) - 1;
  974. dmabuf->userfragsize = dmabuf->ossfragsize;
  975. dmabuf->userfrags = dmabuf->dmasize/dmabuf->ossfragsize;
  976. memset(dmabuf->rawbuf, 0, dmabuf->dmasize);
  977. if(dmabuf->ossmaxfrags == 4) {
  978. fragint = 8;
  979. } else if (dmabuf->ossmaxfrags == 8) {
  980. fragint = 4;
  981. } else if (dmabuf->ossmaxfrags == 16) {
  982. fragint = 2;
  983. } else {
  984. fragint = 1;
  985. }
  986. /*
  987. * Now set up the ring
  988. */
  989. if(dmabuf->read_channel)
  990. c = dmabuf->read_channel;
  991. else
  992. c = dmabuf->write_channel;
  993. while(c != NULL) {
  994. sg=&c->sg[0];
  995. /*
  996. * Load up 32 sg entries and take an interrupt at half
  997. * way (we might want more interrupts later..)
  998. */
  999. for(i=0;i<dmabuf->numfrag;i++)
  1000. {
  1001. sg->busaddr=(u32)dmabuf->dma_handle+dmabuf->fragsize*i;
  1002. // the card will always be doing 16bit stereo
  1003. sg->control=dmabuf->fragsamples;
  1004. if(state->card->pci_id == PCI_DEVICE_ID_SI_7012)
  1005. sg->control <<= 1;
  1006. sg->control|=CON_BUFPAD;
  1007. // set us up to get IOC interrupts as often as needed to
  1008. // satisfy numfrag requirements, no more
  1009. if( ((i+1) % fragint) == 0) {
  1010. sg->control|=CON_IOC;
  1011. }
  1012. sg++;
  1013. }
  1014. spin_lock_irqsave(&state->card->lock, flags);
  1015. I810_IOWRITEB(2, state->card, c->port+OFF_CR); /* reset DMA machine */
  1016. while( I810_IOREADB(state->card, c->port+OFF_CR) & 0x02 ) ;
  1017. I810_IOWRITEL((u32)state->card->chandma +
  1018. c->num*sizeof(struct i810_channel),
  1019. state->card, c->port+OFF_BDBAR);
  1020. CIV_TO_LVI(state->card, c->port, 0);
  1021. spin_unlock_irqrestore(&state->card->lock, flags);
  1022. if(c != dmabuf->write_channel)
  1023. c = dmabuf->write_channel;
  1024. else
  1025. c = NULL;
  1026. }
  1027. /* set the ready flag for the dma buffer */
  1028. dmabuf->ready = 1;
  1029. #ifdef DEBUG
  1030. printk("i810_audio: prog_dmabuf, sample rate = %d, format = %d,\n\tnumfrag = %d, "
  1031. "fragsize = %d dmasize = %d\n",
  1032. dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
  1033. dmabuf->fragsize, dmabuf->dmasize);
  1034. #endif
  1035. return 0;
  1036. }
  1037. static void __i810_update_lvi(struct i810_state *state, int rec)
  1038. {
  1039. struct dmabuf *dmabuf = &state->dmabuf;
  1040. int x, port;
  1041. int trigger;
  1042. int count, fragsize;
  1043. void (*start)(struct i810_state *);
  1044. count = dmabuf->count;
  1045. if (rec) {
  1046. port = dmabuf->read_channel->port;
  1047. trigger = PCM_ENABLE_INPUT;
  1048. start = __start_adc;
  1049. count = dmabuf->dmasize - count;
  1050. } else {
  1051. port = dmabuf->write_channel->port;
  1052. trigger = PCM_ENABLE_OUTPUT;
  1053. start = __start_dac;
  1054. }
  1055. /* Do not process partial fragments. */
  1056. fragsize = dmabuf->fragsize;
  1057. if (count < fragsize)
  1058. return;
  1059. /* if we are currently stopped, then our CIV is actually set to our
  1060. * *last* sg segment and we are ready to wrap to the next. However,
  1061. * if we set our LVI to the last sg segment, then it won't wrap to
  1062. * the next sg segment, it won't even get a start. So, instead, when
  1063. * we are stopped, we set both the LVI value and also we increment
  1064. * the CIV value to the next sg segment to be played so that when
  1065. * we call start, things will operate properly. Since the CIV can't
  1066. * be written to directly for this purpose, we set the LVI to CIV + 1
  1067. * temporarily. Once the engine has started we set the LVI to its
  1068. * final value.
  1069. */
  1070. if (!dmabuf->enable && dmabuf->ready) {
  1071. if (!(dmabuf->trigger & trigger))
  1072. return;
  1073. CIV_TO_LVI(state->card, port, 1);
  1074. start(state);
  1075. while (!(I810_IOREADB(state->card, port + OFF_CR) & ((1<<4) | (1<<2))))
  1076. ;
  1077. }
  1078. /* MASKP2(swptr, fragsize) - 1 is the tail of our transfer */
  1079. x = MODULOP2(MASKP2(dmabuf->swptr, fragsize) - 1, dmabuf->dmasize);
  1080. x >>= dmabuf->fragshift;
  1081. I810_IOWRITEB(x, state->card, port + OFF_LVI);
  1082. }
  1083. static void i810_update_lvi(struct i810_state *state, int rec)
  1084. {
  1085. struct dmabuf *dmabuf = &state->dmabuf;
  1086. unsigned long flags;
  1087. if(!dmabuf->ready)
  1088. return;
  1089. spin_lock_irqsave(&state->card->lock, flags);
  1090. __i810_update_lvi(state, rec);
  1091. spin_unlock_irqrestore(&state->card->lock, flags);
  1092. }
  1093. /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
  1094. static void i810_update_ptr(struct i810_state *state)
  1095. {
  1096. struct dmabuf *dmabuf = &state->dmabuf;
  1097. unsigned hwptr;
  1098. unsigned fragmask, dmamask;
  1099. int diff;
  1100. fragmask = MASKP2(~0, dmabuf->fragsize);
  1101. dmamask = MODULOP2(~0, dmabuf->dmasize);
  1102. /* error handling and process wake up for ADC */
  1103. if (dmabuf->enable == ADC_RUNNING) {
  1104. /* update hardware pointer */
  1105. hwptr = i810_get_dma_addr(state, 1) & fragmask;
  1106. diff = (hwptr - dmabuf->hwptr) & dmamask;
  1107. #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
  1108. printk("ADC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
  1109. #endif
  1110. dmabuf->hwptr = hwptr;
  1111. dmabuf->total_bytes += diff;
  1112. dmabuf->count += diff;
  1113. if (dmabuf->count > dmabuf->dmasize) {
  1114. /* buffer underrun or buffer overrun */
  1115. /* this is normal for the end of a read */
  1116. /* only give an error if we went past the */
  1117. /* last valid sg entry */
  1118. if (GET_CIV(state->card, PI_BASE) !=
  1119. GET_LVI(state->card, PI_BASE)) {
  1120. printk(KERN_WARNING "i810_audio: DMA overrun on read\n");
  1121. dmabuf->error++;
  1122. }
  1123. }
  1124. if (diff)
  1125. wake_up(&dmabuf->wait);
  1126. }
  1127. /* error handling and process wake up for DAC */
  1128. if (dmabuf->enable == DAC_RUNNING) {
  1129. /* update hardware pointer */
  1130. hwptr = i810_get_dma_addr(state, 0) & fragmask;
  1131. diff = (hwptr - dmabuf->hwptr) & dmamask;
  1132. #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
  1133. printk("DAC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
  1134. #endif
  1135. dmabuf->hwptr = hwptr;
  1136. dmabuf->total_bytes += diff;
  1137. dmabuf->count -= diff;
  1138. if (dmabuf->count < 0) {
  1139. /* buffer underrun or buffer overrun */
  1140. /* this is normal for the end of a write */
  1141. /* only give an error if we went past the */
  1142. /* last valid sg entry */
  1143. if (GET_CIV(state->card, PO_BASE) !=
  1144. GET_LVI(state->card, PO_BASE)) {
  1145. printk(KERN_WARNING "i810_audio: DMA overrun on write\n");
  1146. printk("i810_audio: CIV %d, LVI %d, hwptr %x, "
  1147. "count %d\n",
  1148. GET_CIV(state->card, PO_BASE),
  1149. GET_LVI(state->card, PO_BASE),
  1150. dmabuf->hwptr, dmabuf->count);
  1151. dmabuf->error++;
  1152. }
  1153. }
  1154. if (diff)
  1155. wake_up(&dmabuf->wait);
  1156. }
  1157. }
  1158. static inline int i810_get_free_write_space(struct i810_state *state)
  1159. {
  1160. struct dmabuf *dmabuf = &state->dmabuf;
  1161. int free;
  1162. i810_update_ptr(state);
  1163. // catch underruns during playback
  1164. if (dmabuf->count < 0) {
  1165. dmabuf->count = 0;
  1166. dmabuf->swptr = dmabuf->hwptr;
  1167. }
  1168. free = dmabuf->dmasize - dmabuf->count;
  1169. if(free < 0)
  1170. return(0);
  1171. return(free);
  1172. }
  1173. static inline int i810_get_available_read_data(struct i810_state *state)
  1174. {
  1175. struct dmabuf *dmabuf = &state->dmabuf;
  1176. int avail;
  1177. i810_update_ptr(state);
  1178. // catch overruns during record
  1179. if (dmabuf->count > dmabuf->dmasize) {
  1180. dmabuf->count = dmabuf->dmasize;
  1181. dmabuf->swptr = dmabuf->hwptr;
  1182. }
  1183. avail = dmabuf->count;
  1184. if(avail < 0)
  1185. return(0);
  1186. return(avail);
  1187. }
  1188. static inline void fill_partial_frag(struct dmabuf *dmabuf)
  1189. {
  1190. unsigned fragsize;
  1191. unsigned swptr, len;
  1192. fragsize = dmabuf->fragsize;
  1193. swptr = dmabuf->swptr;
  1194. len = fragsize - MODULOP2(dmabuf->swptr, fragsize);
  1195. if (len == fragsize)
  1196. return;
  1197. memset(dmabuf->rawbuf + swptr, '\0', len);
  1198. dmabuf->swptr = MODULOP2(swptr + len, dmabuf->dmasize);
  1199. dmabuf->count += len;
  1200. }
  1201. static int drain_dac(struct i810_state *state, int signals_allowed)
  1202. {
  1203. DECLARE_WAITQUEUE(wait, current);
  1204. struct dmabuf *dmabuf = &state->dmabuf;
  1205. unsigned long flags;
  1206. unsigned long tmo;
  1207. int count;
  1208. if (!dmabuf->ready)
  1209. return 0;
  1210. if(dmabuf->mapped) {
  1211. stop_dac(state);
  1212. return 0;
  1213. }
  1214. spin_lock_irqsave(&state->card->lock, flags);
  1215. fill_partial_frag(dmabuf);
  1216. /*
  1217. * This will make sure that our LVI is correct, that our
  1218. * pointer is updated, and that the DAC is running. We
  1219. * have to force the setting of dmabuf->trigger to avoid
  1220. * any possible deadlocks.
  1221. */
  1222. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  1223. __i810_update_lvi(state, 0);
  1224. spin_unlock_irqrestore(&state->card->lock, flags);
  1225. add_wait_queue(&dmabuf->wait, &wait);
  1226. for (;;) {
  1227. spin_lock_irqsave(&state->card->lock, flags);
  1228. i810_update_ptr(state);
  1229. count = dmabuf->count;
  1230. /* It seems that we have to set the current state to
  1231. * TASK_INTERRUPTIBLE every time to make the process
  1232. * really go to sleep. This also has to be *after* the
  1233. * update_ptr() call because update_ptr is likely to
  1234. * do a wake_up() which will unset this before we ever
  1235. * try to sleep, resuling in a tight loop in this code
  1236. * instead of actually sleeping and waiting for an
  1237. * interrupt to wake us up!
  1238. */
  1239. __set_current_state(signals_allowed ?
  1240. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  1241. spin_unlock_irqrestore(&state->card->lock, flags);
  1242. if (count <= 0)
  1243. break;
  1244. if (signal_pending(current) && signals_allowed) {
  1245. break;
  1246. }
  1247. /*
  1248. * set the timeout to significantly longer than it *should*
  1249. * take for the DAC to drain the DMA buffer
  1250. */
  1251. tmo = (count * HZ) / (dmabuf->rate);
  1252. if (!schedule_timeout(tmo >= 2 ? tmo : 2)){
  1253. printk(KERN_ERR "i810_audio: drain_dac, dma timeout?\n");
  1254. count = 0;
  1255. break;
  1256. }
  1257. }
  1258. set_current_state(TASK_RUNNING);
  1259. remove_wait_queue(&dmabuf->wait, &wait);
  1260. if(count > 0 && signal_pending(current) && signals_allowed)
  1261. return -ERESTARTSYS;
  1262. stop_dac(state);
  1263. return 0;
  1264. }
  1265. static void i810_channel_interrupt(struct i810_card *card)
  1266. {
  1267. int i, count;
  1268. #ifdef DEBUG_INTERRUPTS
  1269. printk("CHANNEL ");
  1270. #endif
  1271. for(i=0;i<NR_HW_CH;i++)
  1272. {
  1273. struct i810_state *state = card->states[i];
  1274. struct i810_channel *c;
  1275. struct dmabuf *dmabuf;
  1276. unsigned long port;
  1277. u16 status;
  1278. if(!state)
  1279. continue;
  1280. if(!state->dmabuf.ready)
  1281. continue;
  1282. dmabuf = &state->dmabuf;
  1283. if(dmabuf->enable & DAC_RUNNING) {
  1284. c=dmabuf->write_channel;
  1285. } else if(dmabuf->enable & ADC_RUNNING) {
  1286. c=dmabuf->read_channel;
  1287. } else /* This can occur going from R/W to close */
  1288. continue;
  1289. port = c->port;
  1290. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  1291. status = I810_IOREADW(card, port + OFF_PICB);
  1292. else
  1293. status = I810_IOREADW(card, port + OFF_SR);
  1294. #ifdef DEBUG_INTERRUPTS
  1295. printk("NUM %d PORT %X IRQ ( ST%d ", c->num, c->port, status);
  1296. #endif
  1297. if(status & DMA_INT_COMPLETE)
  1298. {
  1299. /* only wake_up() waiters if this interrupt signals
  1300. * us being beyond a userfragsize of data open or
  1301. * available, and i810_update_ptr() does that for
  1302. * us
  1303. */
  1304. i810_update_ptr(state);
  1305. #ifdef DEBUG_INTERRUPTS
  1306. printk("COMP %d ", dmabuf->hwptr /
  1307. dmabuf->fragsize);
  1308. #endif
  1309. }
  1310. if(status & (DMA_INT_LVI | DMA_INT_DCH))
  1311. {
  1312. /* wake_up() unconditionally on LVI and DCH */
  1313. i810_update_ptr(state);
  1314. wake_up(&dmabuf->wait);
  1315. #ifdef DEBUG_INTERRUPTS
  1316. if(status & DMA_INT_LVI)
  1317. printk("LVI ");
  1318. if(status & DMA_INT_DCH)
  1319. printk("DCH -");
  1320. #endif
  1321. count = dmabuf->count;
  1322. if(dmabuf->enable & ADC_RUNNING)
  1323. count = dmabuf->dmasize - count;
  1324. if (count >= (int)dmabuf->fragsize) {
  1325. I810_IOWRITEB(I810_IOREADB(card, port+OFF_CR) | 1, card, port+OFF_CR);
  1326. #ifdef DEBUG_INTERRUPTS
  1327. printk(" CONTINUE ");
  1328. #endif
  1329. } else {
  1330. if (dmabuf->enable & DAC_RUNNING)
  1331. __stop_dac(state);
  1332. if (dmabuf->enable & ADC_RUNNING)
  1333. __stop_adc(state);
  1334. dmabuf->enable = 0;
  1335. #ifdef DEBUG_INTERRUPTS
  1336. printk(" STOP ");
  1337. #endif
  1338. }
  1339. }
  1340. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  1341. I810_IOWRITEW(status & DMA_INT_MASK, card, port + OFF_PICB);
  1342. else
  1343. I810_IOWRITEW(status & DMA_INT_MASK, card, port + OFF_SR);
  1344. }
  1345. #ifdef DEBUG_INTERRUPTS
  1346. printk(")\n");
  1347. #endif
  1348. }
  1349. static irqreturn_t i810_interrupt(int irq, void *dev_id)
  1350. {
  1351. struct i810_card *card = dev_id;
  1352. u32 status;
  1353. spin_lock(&card->lock);
  1354. status = I810_IOREADL(card, GLOB_STA);
  1355. if(!(status & INT_MASK))
  1356. {
  1357. spin_unlock(&card->lock);
  1358. return IRQ_NONE; /* not for us */
  1359. }
  1360. if(status & (INT_PO|INT_PI|INT_MC))
  1361. i810_channel_interrupt(card);
  1362. /* clear 'em */
  1363. I810_IOWRITEL(status & INT_MASK, card, GLOB_STA);
  1364. spin_unlock(&card->lock);
  1365. return IRQ_HANDLED;
  1366. }
  1367. /* in this loop, dmabuf.count signifies the amount of data that is
  1368. waiting to be copied to the user's buffer. It is filled by the dma
  1369. machine and drained by this loop. */
  1370. static ssize_t i810_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  1371. {
  1372. struct i810_state *state = (struct i810_state *)file->private_data;
  1373. struct i810_card *card=state ? state->card : NULL;
  1374. struct dmabuf *dmabuf = &state->dmabuf;
  1375. ssize_t ret;
  1376. unsigned long flags;
  1377. unsigned int swptr;
  1378. int cnt;
  1379. int pending;
  1380. DECLARE_WAITQUEUE(waita, current);
  1381. #ifdef DEBUG2
  1382. printk("i810_audio: i810_read called, count = %d\n", count);
  1383. #endif
  1384. if (dmabuf->mapped)
  1385. return -ENXIO;
  1386. if (dmabuf->enable & DAC_RUNNING)
  1387. return -ENODEV;
  1388. if (!dmabuf->read_channel) {
  1389. dmabuf->ready = 0;
  1390. dmabuf->read_channel = card->alloc_rec_pcm_channel(card);
  1391. if (!dmabuf->read_channel) {
  1392. return -EBUSY;
  1393. }
  1394. }
  1395. if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
  1396. return ret;
  1397. if (!access_ok(VERIFY_WRITE, buffer, count))
  1398. return -EFAULT;
  1399. ret = 0;
  1400. pending = 0;
  1401. add_wait_queue(&dmabuf->wait, &waita);
  1402. while (count > 0) {
  1403. set_current_state(TASK_INTERRUPTIBLE);
  1404. spin_lock_irqsave(&card->lock, flags);
  1405. if (PM_SUSPENDED(card)) {
  1406. spin_unlock_irqrestore(&card->lock, flags);
  1407. schedule();
  1408. if (signal_pending(current)) {
  1409. if (!ret) ret = -EAGAIN;
  1410. break;
  1411. }
  1412. continue;
  1413. }
  1414. cnt = i810_get_available_read_data(state);
  1415. swptr = dmabuf->swptr;
  1416. // this is to make the copy_to_user simpler below
  1417. if(cnt > (dmabuf->dmasize - swptr))
  1418. cnt = dmabuf->dmasize - swptr;
  1419. spin_unlock_irqrestore(&card->lock, flags);
  1420. if (cnt > count)
  1421. cnt = count;
  1422. if (cnt <= 0) {
  1423. unsigned long tmo;
  1424. /*
  1425. * Don't let us deadlock. The ADC won't start if
  1426. * dmabuf->trigger isn't set. A call to SETTRIGGER
  1427. * could have turned it off after we set it to on
  1428. * previously.
  1429. */
  1430. dmabuf->trigger = PCM_ENABLE_INPUT;
  1431. /*
  1432. * This does three things. Updates LVI to be correct,
  1433. * makes sure the ADC is running, and updates the
  1434. * hwptr.
  1435. */
  1436. i810_update_lvi(state,1);
  1437. if (file->f_flags & O_NONBLOCK) {
  1438. if (!ret) ret = -EAGAIN;
  1439. goto done;
  1440. }
  1441. /* Set the timeout to how long it would take to fill
  1442. * two of our buffers. If we haven't been woke up
  1443. * by then, then we know something is wrong.
  1444. */
  1445. tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
  1446. /* There are two situations when sleep_on_timeout returns, one is when
  1447. the interrupt is serviced correctly and the process is waked up by
  1448. ISR ON TIME. Another is when timeout is expired, which means that
  1449. either interrupt is NOT serviced correctly (pending interrupt) or it
  1450. is TOO LATE for the process to be scheduled to run (scheduler latency)
  1451. which results in a (potential) buffer overrun. And worse, there is
  1452. NOTHING we can do to prevent it. */
  1453. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1454. #ifdef DEBUG
  1455. printk(KERN_ERR "i810_audio: recording schedule timeout, "
  1456. "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
  1457. dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
  1458. dmabuf->hwptr, dmabuf->swptr);
  1459. #endif
  1460. /* a buffer overrun, we delay the recovery until next time the
  1461. while loop begin and we REALLY have space to record */
  1462. }
  1463. if (signal_pending(current)) {
  1464. ret = ret ? ret : -ERESTARTSYS;
  1465. goto done;
  1466. }
  1467. continue;
  1468. }
  1469. if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
  1470. if (!ret) ret = -EFAULT;
  1471. goto done;
  1472. }
  1473. swptr = MODULOP2(swptr + cnt, dmabuf->dmasize);
  1474. spin_lock_irqsave(&card->lock, flags);
  1475. if (PM_SUSPENDED(card)) {
  1476. spin_unlock_irqrestore(&card->lock, flags);
  1477. continue;
  1478. }
  1479. dmabuf->swptr = swptr;
  1480. pending = dmabuf->count -= cnt;
  1481. spin_unlock_irqrestore(&card->lock, flags);
  1482. count -= cnt;
  1483. buffer += cnt;
  1484. ret += cnt;
  1485. }
  1486. done:
  1487. pending = dmabuf->dmasize - pending;
  1488. if (dmabuf->enable || pending >= dmabuf->userfragsize)
  1489. i810_update_lvi(state, 1);
  1490. set_current_state(TASK_RUNNING);
  1491. remove_wait_queue(&dmabuf->wait, &waita);
  1492. return ret;
  1493. }
  1494. /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
  1495. the soundcard. it is drained by the dma machine and filled by this loop. */
  1496. static ssize_t i810_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  1497. {
  1498. struct i810_state *state = (struct i810_state *)file->private_data;
  1499. struct i810_card *card=state ? state->card : NULL;
  1500. struct dmabuf *dmabuf = &state->dmabuf;
  1501. ssize_t ret;
  1502. unsigned long flags;
  1503. unsigned int swptr = 0;
  1504. int pending;
  1505. int cnt;
  1506. DECLARE_WAITQUEUE(waita, current);
  1507. #ifdef DEBUG2
  1508. printk("i810_audio: i810_write called, count = %d\n", count);
  1509. #endif
  1510. if (dmabuf->mapped)
  1511. return -ENXIO;
  1512. if (dmabuf->enable & ADC_RUNNING)
  1513. return -ENODEV;
  1514. if (!dmabuf->write_channel) {
  1515. dmabuf->ready = 0;
  1516. dmabuf->write_channel = card->alloc_pcm_channel(card);
  1517. if(!dmabuf->write_channel)
  1518. return -EBUSY;
  1519. }
  1520. if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
  1521. return ret;
  1522. if (!access_ok(VERIFY_READ, buffer, count))
  1523. return -EFAULT;
  1524. ret = 0;
  1525. pending = 0;
  1526. add_wait_queue(&dmabuf->wait, &waita);
  1527. while (count > 0) {
  1528. set_current_state(TASK_INTERRUPTIBLE);
  1529. spin_lock_irqsave(&state->card->lock, flags);
  1530. if (PM_SUSPENDED(card)) {
  1531. spin_unlock_irqrestore(&card->lock, flags);
  1532. schedule();
  1533. if (signal_pending(current)) {
  1534. if (!ret) ret = -EAGAIN;
  1535. break;
  1536. }
  1537. continue;
  1538. }
  1539. cnt = i810_get_free_write_space(state);
  1540. swptr = dmabuf->swptr;
  1541. /* Bound the maximum size to how much we can copy to the
  1542. * dma buffer before we hit the end. If we have more to
  1543. * copy then it will get done in a second pass of this
  1544. * loop starting from the beginning of the buffer.
  1545. */
  1546. if(cnt > (dmabuf->dmasize - swptr))
  1547. cnt = dmabuf->dmasize - swptr;
  1548. spin_unlock_irqrestore(&state->card->lock, flags);
  1549. #ifdef DEBUG2
  1550. printk(KERN_INFO "i810_audio: i810_write: %d bytes available space\n", cnt);
  1551. #endif
  1552. if (cnt > count)
  1553. cnt = count;
  1554. if (cnt <= 0) {
  1555. unsigned long tmo;
  1556. // There is data waiting to be played
  1557. /*
  1558. * Force the trigger setting since we would
  1559. * deadlock with it set any other way
  1560. */
  1561. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  1562. i810_update_lvi(state,0);
  1563. if (file->f_flags & O_NONBLOCK) {
  1564. if (!ret) ret = -EAGAIN;
  1565. goto ret;
  1566. }
  1567. /* Not strictly correct but works */
  1568. tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
  1569. /* There are two situations when sleep_on_timeout returns, one is when
  1570. the interrupt is serviced correctly and the process is waked up by
  1571. ISR ON TIME. Another is when timeout is expired, which means that
  1572. either interrupt is NOT serviced correctly (pending interrupt) or it
  1573. is TOO LATE for the process to be scheduled to run (scheduler latency)
  1574. which results in a (potential) buffer underrun. And worse, there is
  1575. NOTHING we can do to prevent it. */
  1576. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1577. #ifdef DEBUG
  1578. printk(KERN_ERR "i810_audio: playback schedule timeout, "
  1579. "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
  1580. dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
  1581. dmabuf->hwptr, dmabuf->swptr);
  1582. #endif
  1583. /* a buffer underrun, we delay the recovery until next time the
  1584. while loop begin and we REALLY have data to play */
  1585. //return ret;
  1586. }
  1587. if (signal_pending(current)) {
  1588. if (!ret) ret = -ERESTARTSYS;
  1589. goto ret;
  1590. }
  1591. continue;
  1592. }
  1593. if (copy_from_user(dmabuf->rawbuf+swptr,buffer,cnt)) {
  1594. if (!ret) ret = -EFAULT;
  1595. goto ret;
  1596. }
  1597. swptr = MODULOP2(swptr + cnt, dmabuf->dmasize);
  1598. spin_lock_irqsave(&state->card->lock, flags);
  1599. if (PM_SUSPENDED(card)) {
  1600. spin_unlock_irqrestore(&card->lock, flags);
  1601. continue;
  1602. }
  1603. dmabuf->swptr = swptr;
  1604. pending = dmabuf->count += cnt;
  1605. count -= cnt;
  1606. buffer += cnt;
  1607. ret += cnt;
  1608. spin_unlock_irqrestore(&state->card->lock, flags);
  1609. }
  1610. ret:
  1611. if (dmabuf->enable || pending >= dmabuf->userfragsize)
  1612. i810_update_lvi(state, 0);
  1613. set_current_state(TASK_RUNNING);
  1614. remove_wait_queue(&dmabuf->wait, &waita);
  1615. return ret;
  1616. }
  1617. /* No kernel lock - we have our own spinlock */
  1618. static unsigned int i810_poll(struct file *file, struct poll_table_struct *wait)
  1619. {
  1620. struct i810_state *state = (struct i810_state *)file->private_data;
  1621. struct dmabuf *dmabuf = &state->dmabuf;
  1622. unsigned long flags;
  1623. unsigned int mask = 0;
  1624. if(!dmabuf->ready)
  1625. return 0;
  1626. poll_wait(file, &dmabuf->wait, wait);
  1627. spin_lock_irqsave(&state->card->lock, flags);
  1628. if (dmabuf->enable & ADC_RUNNING ||
  1629. dmabuf->trigger & PCM_ENABLE_INPUT) {
  1630. if (i810_get_available_read_data(state) >=
  1631. (signed)dmabuf->userfragsize)
  1632. mask |= POLLIN | POLLRDNORM;
  1633. }
  1634. if (dmabuf->enable & DAC_RUNNING ||
  1635. dmabuf->trigger & PCM_ENABLE_OUTPUT) {
  1636. if (i810_get_free_write_space(state) >=
  1637. (signed)dmabuf->userfragsize)
  1638. mask |= POLLOUT | POLLWRNORM;
  1639. }
  1640. spin_unlock_irqrestore(&state->card->lock, flags);
  1641. return mask;
  1642. }
  1643. static int i810_mmap(struct file *file, struct vm_area_struct *vma)
  1644. {
  1645. struct i810_state *state = (struct i810_state *)file->private_data;
  1646. struct dmabuf *dmabuf = &state->dmabuf;
  1647. int ret = -EINVAL;
  1648. unsigned long size;
  1649. lock_kernel();
  1650. if (vma->vm_flags & VM_WRITE) {
  1651. if (!dmabuf->write_channel &&
  1652. (dmabuf->write_channel =
  1653. state->card->alloc_pcm_channel(state->card)) == NULL) {
  1654. ret = -EBUSY;
  1655. goto out;
  1656. }
  1657. }
  1658. if (vma->vm_flags & VM_READ) {
  1659. if (!dmabuf->read_channel &&
  1660. (dmabuf->read_channel =
  1661. state->card->alloc_rec_pcm_channel(state->card)) == NULL) {
  1662. ret = -EBUSY;
  1663. goto out;
  1664. }
  1665. }
  1666. if ((ret = prog_dmabuf(state, 0)) != 0)
  1667. goto out;
  1668. ret = -EINVAL;
  1669. if (vma->vm_pgoff != 0)
  1670. goto out;
  1671. size = vma->vm_end - vma->vm_start;
  1672. if (size > (PAGE_SIZE << dmabuf->buforder))
  1673. goto out;
  1674. ret = -EAGAIN;
  1675. if (remap_pfn_range(vma, vma->vm_start,
  1676. virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
  1677. size, vma->vm_page_prot))
  1678. goto out;
  1679. dmabuf->mapped = 1;
  1680. dmabuf->trigger = 0;
  1681. ret = 0;
  1682. #ifdef DEBUG_MMAP
  1683. printk("i810_audio: mmap'ed %ld bytes of data space\n", size);
  1684. #endif
  1685. out:
  1686. unlock_kernel();
  1687. return ret;
  1688. }
  1689. static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1690. {
  1691. struct i810_state *state = (struct i810_state *)file->private_data;
  1692. struct i810_channel *c = NULL;
  1693. struct dmabuf *dmabuf = &state->dmabuf;
  1694. unsigned long flags;
  1695. audio_buf_info abinfo;
  1696. count_info cinfo;
  1697. unsigned int i_glob_cnt;
  1698. int val = 0, ret;
  1699. struct ac97_codec *codec = state->card->ac97_codec[0];
  1700. void __user *argp = (void __user *)arg;
  1701. int __user *p = argp;
  1702. #ifdef DEBUG
  1703. printk("i810_audio: i810_ioctl, arg=0x%x, cmd=", arg ? *p : 0);
  1704. #endif
  1705. switch (cmd)
  1706. {
  1707. case OSS_GETVERSION:
  1708. #ifdef DEBUG
  1709. printk("OSS_GETVERSION\n");
  1710. #endif
  1711. return put_user(SOUND_VERSION, p);
  1712. case SNDCTL_DSP_RESET:
  1713. #ifdef DEBUG
  1714. printk("SNDCTL_DSP_RESET\n");
  1715. #endif
  1716. spin_lock_irqsave(&state->card->lock, flags);
  1717. if (dmabuf->enable == DAC_RUNNING) {
  1718. c = dmabuf->write_channel;
  1719. __stop_dac(state);
  1720. }
  1721. if (dmabuf->enable == ADC_RUNNING) {
  1722. c = dmabuf->read_channel;
  1723. __stop_adc(state);
  1724. }
  1725. if (c != NULL) {
  1726. I810_IOWRITEB(2, state->card, c->port+OFF_CR); /* reset DMA machine */
  1727. while ( I810_IOREADB(state->card, c->port+OFF_CR) & 2 )
  1728. cpu_relax();
  1729. I810_IOWRITEL((u32)state->card->chandma +
  1730. c->num*sizeof(struct i810_channel),
  1731. state->card, c->port+OFF_BDBAR);
  1732. CIV_TO_LVI(state->card, c->port, 0);
  1733. }
  1734. spin_unlock_irqrestore(&state->card->lock, flags);
  1735. synchronize_irq(state->card->pci_dev->irq);
  1736. dmabuf->ready = 0;
  1737. dmabuf->swptr = dmabuf->hwptr = 0;
  1738. dmabuf->count = dmabuf->total_bytes = 0;
  1739. return 0;
  1740. case SNDCTL_DSP_SYNC:
  1741. #ifdef DEBUG
  1742. printk("SNDCTL_DSP_SYNC\n");
  1743. #endif
  1744. if (dmabuf->enable != DAC_RUNNING || file->f_flags & O_NONBLOCK)
  1745. return 0;
  1746. if((val = drain_dac(state, 1)))
  1747. return val;
  1748. dmabuf->total_bytes = 0;
  1749. return 0;
  1750. case SNDCTL_DSP_SPEED: /* set smaple rate */
  1751. #ifdef DEBUG
  1752. printk("SNDCTL_DSP_SPEED\n");
  1753. #endif
  1754. if (get_user(val, p))
  1755. return -EFAULT;
  1756. if (val >= 0) {
  1757. if (file->f_mode & FMODE_WRITE) {
  1758. if ( (state->card->ac97_status & SPDIF_ON) ) { /* S/PDIF Enabled */
  1759. /* AD1886 only supports 48000, need to check that */
  1760. if ( i810_valid_spdif_rate ( codec, val ) ) {
  1761. /* Set DAC rate */
  1762. i810_set_spdif_output ( state, -1, 0 );
  1763. stop_dac(state);
  1764. dmabuf->ready = 0;
  1765. spin_lock_irqsave(&state->card->lock, flags);
  1766. i810_set_dac_rate(state, val);
  1767. spin_unlock_irqrestore(&state->card->lock, flags);
  1768. /* Set S/PDIF transmitter rate. */
  1769. i810_set_spdif_output ( state, AC97_EA_SPSA_3_4, val );
  1770. if ( ! (state->card->ac97_status & SPDIF_ON) ) {
  1771. val = dmabuf->rate;
  1772. }
  1773. } else { /* Not a valid rate for S/PDIF, ignore it */
  1774. val = dmabuf->rate;
  1775. }
  1776. } else {
  1777. stop_dac(state);
  1778. dmabuf->ready = 0;
  1779. spin_lock_irqsave(&state->card->lock, flags);
  1780. i810_set_dac_rate(state, val);
  1781. spin_unlock_irqrestore(&state->card->lock, flags);
  1782. }
  1783. }
  1784. if (file->f_mode & FMODE_READ) {
  1785. stop_adc(state);
  1786. dmabuf->ready = 0;
  1787. spin_lock_irqsave(&state->card->lock, flags);
  1788. i810_set_adc_rate(state, val);
  1789. spin_unlock_irqrestore(&state->card->lock, flags);
  1790. }
  1791. }
  1792. return put_user(dmabuf->rate, p);
  1793. case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
  1794. #ifdef DEBUG
  1795. printk("SNDCTL_DSP_STEREO\n");
  1796. #endif
  1797. if (dmabuf->enable & DAC_RUNNING) {
  1798. stop_dac(state);
  1799. }
  1800. if (dmabuf->enable & ADC_RUNNING) {
  1801. stop_adc(state);
  1802. }
  1803. return put_user(1, p);
  1804. case SNDCTL_DSP_GETBLKSIZE:
  1805. if (file->f_mode & FMODE_WRITE) {
  1806. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
  1807. return val;
  1808. }
  1809. if (file->f_mode & FMODE_READ) {
  1810. if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
  1811. return val;
  1812. }
  1813. #ifdef DEBUG
  1814. printk("SNDCTL_DSP_GETBLKSIZE %d\n", dmabuf->userfragsize);
  1815. #endif
  1816. return put_user(dmabuf->userfragsize, p);
  1817. case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
  1818. #ifdef DEBUG
  1819. printk("SNDCTL_DSP_GETFMTS\n");
  1820. #endif
  1821. return put_user(AFMT_S16_LE, p);
  1822. case SNDCTL_DSP_SETFMT: /* Select sample format */
  1823. #ifdef DEBUG
  1824. printk("SNDCTL_DSP_SETFMT\n");
  1825. #endif
  1826. return put_user(AFMT_S16_LE, p);
  1827. case SNDCTL_DSP_CHANNELS:
  1828. #ifdef DEBUG
  1829. printk("SNDCTL_DSP_CHANNELS\n");
  1830. #endif
  1831. if (get_user(val, p))
  1832. return -EFAULT;
  1833. if (val > 0) {
  1834. if (dmabuf->enable & DAC_RUNNING) {
  1835. stop_dac(state);
  1836. }
  1837. if (dmabuf->enable & ADC_RUNNING) {
  1838. stop_adc(state);
  1839. }
  1840. } else {
  1841. return put_user(state->card->channels, p);
  1842. }
  1843. /* ICH and ICH0 only support 2 channels */
  1844. if ( state->card->pci_id == PCI_DEVICE_ID_INTEL_82801AA_5
  1845. || state->card->pci_id == PCI_DEVICE_ID_INTEL_82801AB_5)
  1846. return put_user(2, p);
  1847. /* Multi-channel support was added with ICH2. Bits in */
  1848. /* Global Status and Global Control register are now */
  1849. /* used to indicate this. */
  1850. i_glob_cnt = I810_IOREADL(state->card, GLOB_CNT);
  1851. /* Current # of channels enabled */
  1852. if ( i_glob_cnt & 0x0100000 )
  1853. ret = 4;
  1854. else if ( i_glob_cnt & 0x0200000 )
  1855. ret = 6;
  1856. else
  1857. ret = 2;
  1858. switch ( val ) {
  1859. case 2: /* 2 channels is always supported */
  1860. I810_IOWRITEL(i_glob_cnt & 0xffcfffff,
  1861. state->card, GLOB_CNT);
  1862. /* Do we need to change mixer settings???? */
  1863. break;
  1864. case 4: /* Supported on some chipsets, better check first */
  1865. if ( state->card->channels >= 4 ) {
  1866. I810_IOWRITEL((i_glob_cnt & 0xffcfffff) | 0x100000,
  1867. state->card, GLOB_CNT);
  1868. /* Do we need to change mixer settings??? */
  1869. } else {
  1870. val = ret;
  1871. }
  1872. break;
  1873. case 6: /* Supported on some chipsets, better check first */
  1874. if ( state->card->channels >= 6 ) {
  1875. I810_IOWRITEL((i_glob_cnt & 0xffcfffff) | 0x200000,
  1876. state->card, GLOB_CNT);
  1877. /* Do we need to change mixer settings??? */
  1878. } else {
  1879. val = ret;
  1880. }
  1881. break;
  1882. default: /* nothing else is ever supported by the chipset */
  1883. val = ret;
  1884. break;
  1885. }
  1886. return put_user(val, p);
  1887. case SNDCTL_DSP_POST: /* the user has sent all data and is notifying us */
  1888. /* we update the swptr to the end of the last sg segment then return */
  1889. #ifdef DEBUG
  1890. printk("SNDCTL_DSP_POST\n");
  1891. #endif
  1892. if(!dmabuf->ready || (dmabuf->enable != DAC_RUNNING))
  1893. return 0;
  1894. if((dmabuf->swptr % dmabuf->fragsize) != 0) {
  1895. val = dmabuf->fragsize - (dmabuf->swptr % dmabuf->fragsize);
  1896. dmabuf->swptr += val;
  1897. dmabuf->count += val;
  1898. }
  1899. return 0;
  1900. case SNDCTL_DSP_SUBDIVIDE:
  1901. if (dmabuf->subdivision)
  1902. return -EINVAL;
  1903. if (get_user(val, p))
  1904. return -EFAULT;
  1905. if (val != 1 && val != 2 && val != 4)
  1906. return -EINVAL;
  1907. #ifdef DEBUG
  1908. printk("SNDCTL_DSP_SUBDIVIDE %d\n", val);
  1909. #endif
  1910. dmabuf->subdivision = val;
  1911. dmabuf->ready = 0;
  1912. return 0;
  1913. case SNDCTL_DSP_SETFRAGMENT:
  1914. if (get_user(val, p))
  1915. return -EFAULT;
  1916. dmabuf->ossfragsize = 1<<(val & 0xffff);
  1917. dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
  1918. if (!dmabuf->ossfragsize || !dmabuf->ossmaxfrags)
  1919. return -EINVAL;
  1920. /*
  1921. * Bound the frag size into our allowed range of 256 - 4096
  1922. */
  1923. if (dmabuf->ossfragsize < 256)
  1924. dmabuf->ossfragsize = 256;
  1925. else if (dmabuf->ossfragsize > 4096)
  1926. dmabuf->ossfragsize = 4096;
  1927. /*
  1928. * The numfrags could be something reasonable, or it could
  1929. * be 0xffff meaning "Give me as much as possible". So,
  1930. * we check the numfrags * fragsize doesn't exceed our
  1931. * 64k buffer limit, nor is it less than our 8k minimum.
  1932. * If it fails either one of these checks, then adjust the
  1933. * number of fragments, not the size of them. It's OK if
  1934. * our number of fragments doesn't equal 32 or anything
  1935. * like our hardware based number now since we are using
  1936. * a different frag count for the hardware. Before we get
  1937. * into this though, bound the maxfrags to avoid overflow
  1938. * issues. A reasonable bound would be 64k / 256 since our
  1939. * maximum buffer size is 64k and our minimum frag size is
  1940. * 256. On the other end, our minimum buffer size is 8k and
  1941. * our maximum frag size is 4k, so the lower bound should
  1942. * be 2.
  1943. */
  1944. if(dmabuf->ossmaxfrags > 256)
  1945. dmabuf->ossmaxfrags = 256;
  1946. else if (dmabuf->ossmaxfrags < 2)
  1947. dmabuf->ossmaxfrags = 2;
  1948. val = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
  1949. while (val < 8192) {
  1950. val <<= 1;
  1951. dmabuf->ossmaxfrags <<= 1;
  1952. }
  1953. while (val > 65536) {
  1954. val >>= 1;
  1955. dmabuf->ossmaxfrags >>= 1;
  1956. }
  1957. dmabuf->ready = 0;
  1958. #ifdef DEBUG
  1959. printk("SNDCTL_DSP_SETFRAGMENT 0x%x, %d, %d\n", val,
  1960. dmabuf->ossfragsize, dmabuf->ossmaxfrags);
  1961. #endif
  1962. return 0;
  1963. case SNDCTL_DSP_GETOSPACE:
  1964. if (!(file->f_mode & FMODE_WRITE))
  1965. return -EINVAL;
  1966. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  1967. return val;
  1968. spin_lock_irqsave(&state->card->lock, flags);
  1969. i810_update_ptr(state);
  1970. abinfo.fragsize = dmabuf->userfragsize;
  1971. abinfo.fragstotal = dmabuf->userfrags;
  1972. if (dmabuf->mapped)
  1973. abinfo.bytes = dmabuf->dmasize;
  1974. else
  1975. abinfo.bytes = i810_get_free_write_space(state);
  1976. abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
  1977. spin_unlock_irqrestore(&state->card->lock, flags);
  1978. #if defined(DEBUG) || defined(DEBUG_MMAP)
  1979. printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %d\n", abinfo.bytes,
  1980. abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
  1981. #endif
  1982. return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
  1983. case SNDCTL_DSP_GETOPTR:
  1984. if (!(file->f_mode & FMODE_WRITE))
  1985. return -EINVAL;
  1986. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  1987. return val;
  1988. spin_lock_irqsave(&state->card->lock, flags);
  1989. val = i810_get_free_write_space(state);
  1990. cinfo.bytes = dmabuf->total_bytes;
  1991. cinfo.ptr = dmabuf->hwptr;
  1992. cinfo.blocks = val/dmabuf->userfragsize;
  1993. if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  1994. dmabuf->count += val;
  1995. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  1996. __i810_update_lvi(state, 0);
  1997. }
  1998. spin_unlock_irqrestore(&state->card->lock, flags);
  1999. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2000. printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %d\n", cinfo.bytes,
  2001. cinfo.blocks, cinfo.ptr, dmabuf->count);
  2002. #endif
  2003. return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
  2004. case SNDCTL_DSP_GETISPACE:
  2005. if (!(file->f_mode & FMODE_READ))
  2006. return -EINVAL;
  2007. if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
  2008. return val;
  2009. spin_lock_irqsave(&state->card->lock, flags);
  2010. abinfo.bytes = i810_get_available_read_data(state);
  2011. abinfo.fragsize = dmabuf->userfragsize;
  2012. abinfo.fragstotal = dmabuf->userfrags;
  2013. abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
  2014. spin_unlock_irqrestore(&state->card->lock, flags);
  2015. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2016. printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %d\n", abinfo.bytes,
  2017. abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
  2018. #endif
  2019. return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
  2020. case SNDCTL_DSP_GETIPTR:
  2021. if (!(file->f_mode & FMODE_READ))
  2022. return -EINVAL;
  2023. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  2024. return val;
  2025. spin_lock_irqsave(&state->card->lock, flags);
  2026. val = i810_get_available_read_data(state);
  2027. cinfo.bytes = dmabuf->total_bytes;
  2028. cinfo.blocks = val/dmabuf->userfragsize;
  2029. cinfo.ptr = dmabuf->hwptr;
  2030. if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  2031. dmabuf->count -= val;
  2032. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  2033. __i810_update_lvi(state, 1);
  2034. }
  2035. spin_unlock_irqrestore(&state->card->lock, flags);
  2036. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2037. printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %d\n", cinfo.bytes,
  2038. cinfo.blocks, cinfo.ptr, dmabuf->count);
  2039. #endif
  2040. return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
  2041. case SNDCTL_DSP_NONBLOCK:
  2042. #ifdef DEBUG
  2043. printk("SNDCTL_DSP_NONBLOCK\n");
  2044. #endif
  2045. file->f_flags |= O_NONBLOCK;
  2046. return 0;
  2047. case SNDCTL_DSP_GETCAPS:
  2048. #ifdef DEBUG
  2049. printk("SNDCTL_DSP_GETCAPS\n");
  2050. #endif
  2051. return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP|DSP_CAP_BIND,
  2052. p);
  2053. case SNDCTL_DSP_GETTRIGGER:
  2054. val = 0;
  2055. #ifdef DEBUG
  2056. printk("SNDCTL_DSP_GETTRIGGER 0x%x\n", dmabuf->trigger);
  2057. #endif
  2058. return put_user(dmabuf->trigger, p);
  2059. case SNDCTL_DSP_SETTRIGGER:
  2060. if (get_user(val, p))
  2061. return -EFAULT;
  2062. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2063. printk("SNDCTL_DSP_SETTRIGGER 0x%x\n", val);
  2064. #endif
  2065. /* silently ignore invalid PCM_ENABLE_xxx bits,
  2066. * like the other drivers do
  2067. */
  2068. if (!(file->f_mode & FMODE_READ ))
  2069. val &= ~PCM_ENABLE_INPUT;
  2070. if (!(file->f_mode & FMODE_WRITE ))
  2071. val &= ~PCM_ENABLE_OUTPUT;
  2072. if((file->f_mode & FMODE_READ) && !(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
  2073. stop_adc(state);
  2074. }
  2075. if((file->f_mode & FMODE_WRITE) && !(val & PCM_ENABLE_OUTPUT) && dmabuf->enable == DAC_RUNNING) {
  2076. stop_dac(state);
  2077. }
  2078. dmabuf->trigger = val;
  2079. if((val & PCM_ENABLE_OUTPUT) && !(dmabuf->enable & DAC_RUNNING)) {
  2080. if (!dmabuf->write_channel) {
  2081. dmabuf->ready = 0;
  2082. dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
  2083. if (!dmabuf->write_channel)
  2084. return -EBUSY;
  2085. }
  2086. if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
  2087. return ret;
  2088. if (dmabuf->mapped) {
  2089. spin_lock_irqsave(&state->card->lock, flags);
  2090. i810_update_ptr(state);
  2091. dmabuf->count = 0;
  2092. dmabuf->swptr = dmabuf->hwptr;
  2093. dmabuf->count = i810_get_free_write_space(state);
  2094. dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
  2095. spin_unlock_irqrestore(&state->card->lock, flags);
  2096. }
  2097. i810_update_lvi(state, 0);
  2098. start_dac(state);
  2099. }
  2100. if((val & PCM_ENABLE_INPUT) && !(dmabuf->enable & ADC_RUNNING)) {
  2101. if (!dmabuf->read_channel) {
  2102. dmabuf->ready = 0;
  2103. dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
  2104. if (!dmabuf->read_channel)
  2105. return -EBUSY;
  2106. }
  2107. if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
  2108. return ret;
  2109. if (dmabuf->mapped) {
  2110. spin_lock_irqsave(&state->card->lock, flags);
  2111. i810_update_ptr(state);
  2112. dmabuf->swptr = dmabuf->hwptr;
  2113. dmabuf->count = 0;
  2114. spin_unlock_irqrestore(&state->card->lock, flags);
  2115. }
  2116. i810_update_lvi(state, 1);
  2117. start_adc(state);
  2118. }
  2119. return 0;
  2120. case SNDCTL_DSP_SETDUPLEX:
  2121. #ifdef DEBUG
  2122. printk("SNDCTL_DSP_SETDUPLEX\n");
  2123. #endif
  2124. return -EINVAL;
  2125. case SNDCTL_DSP_GETODELAY:
  2126. if (!(file->f_mode & FMODE_WRITE))
  2127. return -EINVAL;
  2128. spin_lock_irqsave(&state->card->lock, flags);
  2129. i810_update_ptr(state);
  2130. val = dmabuf->count;
  2131. spin_unlock_irqrestore(&state->card->lock, flags);
  2132. #ifdef DEBUG
  2133. printk("SNDCTL_DSP_GETODELAY %d\n", dmabuf->count);
  2134. #endif
  2135. return put_user(val, p);
  2136. case SOUND_PCM_READ_RATE:
  2137. #ifdef DEBUG
  2138. printk("SOUND_PCM_READ_RATE %d\n", dmabuf->rate);
  2139. #endif
  2140. return put_user(dmabuf->rate, p);
  2141. case SOUND_PCM_READ_CHANNELS:
  2142. #ifdef DEBUG
  2143. printk("SOUND_PCM_READ_CHANNELS\n");
  2144. #endif
  2145. return put_user(2, p);
  2146. case SOUND_PCM_READ_BITS:
  2147. #ifdef DEBUG
  2148. printk("SOUND_PCM_READ_BITS\n");
  2149. #endif
  2150. return put_user(AFMT_S16_LE, p);
  2151. case SNDCTL_DSP_SETSPDIF: /* Set S/PDIF Control register */
  2152. #ifdef DEBUG
  2153. printk("SNDCTL_DSP_SETSPDIF\n");
  2154. #endif
  2155. if (get_user(val, p))
  2156. return -EFAULT;
  2157. /* Check to make sure the codec supports S/PDIF transmitter */
  2158. if((state->card->ac97_features & 4)) {
  2159. /* mask out the transmitter speed bits so the user can't set them */
  2160. val &= ~0x3000;
  2161. /* Add the current transmitter speed bits to the passed value */
  2162. ret = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
  2163. val |= (ret & 0x3000);
  2164. i810_ac97_set(codec, AC97_SPDIF_CONTROL, val);
  2165. if(i810_ac97_get(codec, AC97_SPDIF_CONTROL) != val ) {
  2166. printk(KERN_ERR "i810_audio: Unable to set S/PDIF configuration to 0x%04x.\n", val);
  2167. return -EFAULT;
  2168. }
  2169. }
  2170. #ifdef DEBUG
  2171. else
  2172. printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.\n");
  2173. #endif
  2174. return put_user(val, p);
  2175. case SNDCTL_DSP_GETSPDIF: /* Get S/PDIF Control register */
  2176. #ifdef DEBUG
  2177. printk("SNDCTL_DSP_GETSPDIF\n");
  2178. #endif
  2179. if (get_user(val, p))
  2180. return -EFAULT;
  2181. /* Check to make sure the codec supports S/PDIF transmitter */
  2182. if(!(state->card->ac97_features & 4)) {
  2183. #ifdef DEBUG
  2184. printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.\n");
  2185. #endif
  2186. val = 0;
  2187. } else {
  2188. val = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
  2189. }
  2190. //return put_user((val & 0xcfff), p);
  2191. return put_user(val, p);
  2192. case SNDCTL_DSP_GETCHANNELMASK:
  2193. #ifdef DEBUG
  2194. printk("SNDCTL_DSP_GETCHANNELMASK\n");
  2195. #endif
  2196. if (get_user(val, p))
  2197. return -EFAULT;
  2198. /* Based on AC'97 DAC support, not ICH hardware */
  2199. val = DSP_BIND_FRONT;
  2200. if ( state->card->ac97_features & 0x0004 )
  2201. val |= DSP_BIND_SPDIF;
  2202. if ( state->card->ac97_features & 0x0080 )
  2203. val |= DSP_BIND_SURR;
  2204. if ( state->card->ac97_features & 0x0140 )
  2205. val |= DSP_BIND_CENTER_LFE;
  2206. return put_user(val, p);
  2207. case SNDCTL_DSP_BIND_CHANNEL:
  2208. #ifdef DEBUG
  2209. printk("SNDCTL_DSP_BIND_CHANNEL\n");
  2210. #endif
  2211. if (get_user(val, p))
  2212. return -EFAULT;
  2213. if ( val == DSP_BIND_QUERY ) {
  2214. val = DSP_BIND_FRONT; /* Always report this as being enabled */
  2215. if ( state->card->ac97_status & SPDIF_ON )
  2216. val |= DSP_BIND_SPDIF;
  2217. else {
  2218. if ( state->card->ac97_status & SURR_ON )
  2219. val |= DSP_BIND_SURR;
  2220. if ( state->card->ac97_status & CENTER_LFE_ON )
  2221. val |= DSP_BIND_CENTER_LFE;
  2222. }
  2223. } else { /* Not a query, set it */
  2224. if (!(file->f_mode & FMODE_WRITE))
  2225. return -EINVAL;
  2226. if ( dmabuf->enable == DAC_RUNNING ) {
  2227. stop_dac(state);
  2228. }
  2229. if ( val & DSP_BIND_SPDIF ) { /* Turn on SPDIF */
  2230. /* Ok, this should probably define what slots
  2231. * to use. For now, we'll only set it to the
  2232. * defaults:
  2233. *
  2234. * non multichannel codec maps to slots 3&4
  2235. * 2 channel codec maps to slots 7&8
  2236. * 4 channel codec maps to slots 6&9
  2237. * 6 channel codec maps to slots 10&11
  2238. *
  2239. * there should be some way for the app to
  2240. * select the slot assignment.
  2241. */
  2242. i810_set_spdif_output ( state, AC97_EA_SPSA_3_4, dmabuf->rate );
  2243. if ( !(state->card->ac97_status & SPDIF_ON) )
  2244. val &= ~DSP_BIND_SPDIF;
  2245. } else {
  2246. int mask;
  2247. int channels;
  2248. /* Turn off S/PDIF if it was on */
  2249. if ( state->card->ac97_status & SPDIF_ON )
  2250. i810_set_spdif_output ( state, -1, 0 );
  2251. mask = val & (DSP_BIND_FRONT | DSP_BIND_SURR | DSP_BIND_CENTER_LFE);
  2252. switch (mask) {
  2253. case DSP_BIND_FRONT:
  2254. channels = 2;
  2255. break;
  2256. case DSP_BIND_FRONT|DSP_BIND_SURR:
  2257. channels = 4;
  2258. break;
  2259. case DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE:
  2260. channels = 6;
  2261. break;
  2262. default:
  2263. val = DSP_BIND_FRONT;
  2264. channels = 2;
  2265. break;
  2266. }
  2267. i810_set_dac_channels ( state, channels );
  2268. /* check that they really got turned on */
  2269. if (!(state->card->ac97_status & SURR_ON))
  2270. val &= ~DSP_BIND_SURR;
  2271. if (!(state->card->ac97_status & CENTER_LFE_ON))
  2272. val &= ~DSP_BIND_CENTER_LFE;
  2273. }
  2274. }
  2275. return put_user(val, p);
  2276. case SNDCTL_DSP_MAPINBUF:
  2277. case SNDCTL_DSP_MAPOUTBUF:
  2278. case SNDCTL_DSP_SETSYNCRO:
  2279. case SOUND_PCM_WRITE_FILTER:
  2280. case SOUND_PCM_READ_FILTER:
  2281. #ifdef DEBUG
  2282. printk("SNDCTL_* -EINVAL\n");
  2283. #endif
  2284. return -EINVAL;
  2285. }
  2286. return -EINVAL;
  2287. }
  2288. static int i810_open(struct inode *inode, struct file *file)
  2289. {
  2290. int i = 0;
  2291. struct i810_card *card = devs;
  2292. struct i810_state *state = NULL;
  2293. struct dmabuf *dmabuf = NULL;
  2294. /* find an avaiable virtual channel (instance of /dev/dsp) */
  2295. while (card != NULL) {
  2296. /*
  2297. * If we are initializing and then fail, card could go
  2298. * away unuexpectedly while we are in the for() loop.
  2299. * So, check for card on each iteration before we check
  2300. * for card->initializing to avoid a possible oops.
  2301. * This usually only matters for times when the driver is
  2302. * autoloaded by kmod.
  2303. */
  2304. for (i = 0; i < 50 && card && card->initializing; i++) {
  2305. set_current_state(TASK_UNINTERRUPTIBLE);
  2306. schedule_timeout(HZ/20);
  2307. }
  2308. for (i = 0; i < NR_HW_CH && card && !card->initializing; i++) {
  2309. if (card->states[i] == NULL) {
  2310. state = card->states[i] = (struct i810_state *)
  2311. kzalloc(sizeof(struct i810_state), GFP_KERNEL);
  2312. if (state == NULL)
  2313. return -ENOMEM;
  2314. dmabuf = &state->dmabuf;
  2315. goto found_virt;
  2316. }
  2317. }
  2318. card = card->next;
  2319. }
  2320. /* no more virtual channel avaiable */
  2321. if (!state)
  2322. return -ENODEV;
  2323. found_virt:
  2324. /* initialize the virtual channel */
  2325. state->virt = i;
  2326. state->card = card;
  2327. state->magic = I810_STATE_MAGIC;
  2328. init_waitqueue_head(&dmabuf->wait);
  2329. mutex_init(&state->open_mutex);
  2330. file->private_data = state;
  2331. dmabuf->trigger = 0;
  2332. /* allocate hardware channels */
  2333. if(file->f_mode & FMODE_READ) {
  2334. if((dmabuf->read_channel = card->alloc_rec_pcm_channel(card)) == NULL) {
  2335. kfree (card->states[i]);
  2336. card->states[i] = NULL;
  2337. return -EBUSY;
  2338. }
  2339. dmabuf->trigger |= PCM_ENABLE_INPUT;
  2340. i810_set_adc_rate(state, 8000);
  2341. }
  2342. if(file->f_mode & FMODE_WRITE) {
  2343. if((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
  2344. /* make sure we free the record channel allocated above */
  2345. if(file->f_mode & FMODE_READ)
  2346. card->free_pcm_channel(card,dmabuf->read_channel->num);
  2347. kfree (card->states[i]);
  2348. card->states[i] = NULL;
  2349. return -EBUSY;
  2350. }
  2351. /* Initialize to 8kHz? What if we don't support 8kHz? */
  2352. /* Let's change this to check for S/PDIF stuff */
  2353. dmabuf->trigger |= PCM_ENABLE_OUTPUT;
  2354. if ( spdif_locked ) {
  2355. i810_set_dac_rate(state, spdif_locked);
  2356. i810_set_spdif_output(state, AC97_EA_SPSA_3_4, spdif_locked);
  2357. } else {
  2358. i810_set_dac_rate(state, 8000);
  2359. /* Put the ACLink in 2 channel mode by default */
  2360. i = I810_IOREADL(card, GLOB_CNT);
  2361. I810_IOWRITEL(i & 0xffcfffff, card, GLOB_CNT);
  2362. }
  2363. }
  2364. /* set default sample format. According to OSS Programmer's Guide /dev/dsp
  2365. should be default to unsigned 8-bits, mono, with sample rate 8kHz and
  2366. /dev/dspW will accept 16-bits sample, but we don't support those so we
  2367. set it immediately to stereo and 16bit, which is all we do support */
  2368. dmabuf->fmt |= I810_FMT_16BIT | I810_FMT_STEREO;
  2369. dmabuf->ossfragsize = 0;
  2370. dmabuf->ossmaxfrags = 0;
  2371. dmabuf->subdivision = 0;
  2372. state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
  2373. return nonseekable_open(inode, file);
  2374. }
  2375. static int i810_release(struct inode *inode, struct file *file)
  2376. {
  2377. struct i810_state *state = (struct i810_state *)file->private_data;
  2378. struct i810_card *card = state->card;
  2379. struct dmabuf *dmabuf = &state->dmabuf;
  2380. unsigned long flags;
  2381. lock_kernel();
  2382. /* stop DMA state machine and free DMA buffers/channels */
  2383. if(dmabuf->trigger & PCM_ENABLE_OUTPUT) {
  2384. drain_dac(state, 0);
  2385. }
  2386. if(dmabuf->trigger & PCM_ENABLE_INPUT) {
  2387. stop_adc(state);
  2388. }
  2389. spin_lock_irqsave(&card->lock, flags);
  2390. dealloc_dmabuf(state);
  2391. if (file->f_mode & FMODE_WRITE) {
  2392. state->card->free_pcm_channel(state->card, dmabuf->write_channel->num);
  2393. }
  2394. if (file->f_mode & FMODE_READ) {
  2395. state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
  2396. }
  2397. state->card->states[state->virt] = NULL;
  2398. kfree(state);
  2399. spin_unlock_irqrestore(&card->lock, flags);
  2400. unlock_kernel();
  2401. return 0;
  2402. }
  2403. static /*const*/ struct file_operations i810_audio_fops = {
  2404. .owner = THIS_MODULE,
  2405. .llseek = no_llseek,
  2406. .read = i810_read,
  2407. .write = i810_write,
  2408. .poll = i810_poll,
  2409. .ioctl = i810_ioctl,
  2410. .mmap = i810_mmap,
  2411. .open = i810_open,
  2412. .release = i810_release,
  2413. };
  2414. /* Write AC97 codec registers */
  2415. static u16 i810_ac97_get_mmio(struct ac97_codec *dev, u8 reg)
  2416. {
  2417. struct i810_card *card = dev->private_data;
  2418. int count = 100;
  2419. u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
  2420. while(count-- && (readb(card->iobase_mmio + CAS) & 1))
  2421. udelay(1);
  2422. #ifdef DEBUG_MMIO
  2423. {
  2424. u16 ans = readw(card->ac97base_mmio + reg_set);
  2425. printk(KERN_DEBUG "i810_audio: ac97_get_mmio(%d) -> 0x%04X\n", ((int) reg_set) & 0xffff, (u32) ans);
  2426. return ans;
  2427. }
  2428. #else
  2429. return readw(card->ac97base_mmio + reg_set);
  2430. #endif
  2431. }
  2432. static u16 i810_ac97_get_io(struct ac97_codec *dev, u8 reg)
  2433. {
  2434. struct i810_card *card = dev->private_data;
  2435. int count = 100;
  2436. u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
  2437. while(count-- && (I810_IOREADB(card, CAS) & 1))
  2438. udelay(1);
  2439. return inw(card->ac97base + reg_set);
  2440. }
  2441. static void i810_ac97_set_mmio(struct ac97_codec *dev, u8 reg, u16 data)
  2442. {
  2443. struct i810_card *card = dev->private_data;
  2444. int count = 100;
  2445. u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
  2446. while(count-- && (readb(card->iobase_mmio + CAS) & 1))
  2447. udelay(1);
  2448. writew(data, card->ac97base_mmio + reg_set);
  2449. #ifdef DEBUG_MMIO
  2450. printk(KERN_DEBUG "i810_audio: ac97_set_mmio(0x%04X, %d)\n", (u32) data, ((int) reg_set) & 0xffff);
  2451. #endif
  2452. }
  2453. static void i810_ac97_set_io(struct ac97_codec *dev, u8 reg, u16 data)
  2454. {
  2455. struct i810_card *card = dev->private_data;
  2456. int count = 100;
  2457. u16 reg_set = IO_REG_OFF(dev) | (reg&0x7f);
  2458. while(count-- && (I810_IOREADB(card, CAS) & 1))
  2459. udelay(1);
  2460. outw(data, card->ac97base + reg_set);
  2461. }
  2462. static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg)
  2463. {
  2464. struct i810_card *card = dev->private_data;
  2465. u16 ret;
  2466. spin_lock(&card->ac97_lock);
  2467. if (card->use_mmio) {
  2468. ret = i810_ac97_get_mmio(dev, reg);
  2469. }
  2470. else {
  2471. ret = i810_ac97_get_io(dev, reg);
  2472. }
  2473. spin_unlock(&card->ac97_lock);
  2474. return ret;
  2475. }
  2476. static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
  2477. {
  2478. struct i810_card *card = dev->private_data;
  2479. spin_lock(&card->ac97_lock);
  2480. if (card->use_mmio) {
  2481. i810_ac97_set_mmio(dev, reg, data);
  2482. }
  2483. else {
  2484. i810_ac97_set_io(dev, reg, data);
  2485. }
  2486. spin_unlock(&card->ac97_lock);
  2487. }
  2488. /* OSS /dev/mixer file operation methods */
  2489. static int i810_open_mixdev(struct inode *inode, struct file *file)
  2490. {
  2491. int i;
  2492. int minor = iminor(inode);
  2493. struct i810_card *card = devs;
  2494. for (card = devs; card != NULL; card = card->next) {
  2495. /*
  2496. * If we are initializing and then fail, card could go
  2497. * away unuexpectedly while we are in the for() loop.
  2498. * So, check for card on each iteration before we check
  2499. * for card->initializing to avoid a possible oops.
  2500. * This usually only matters for times when the driver is
  2501. * autoloaded by kmod.
  2502. */
  2503. for (i = 0; i < 50 && card && card->initializing; i++) {
  2504. set_current_state(TASK_UNINTERRUPTIBLE);
  2505. schedule_timeout(HZ/20);
  2506. }
  2507. for (i = 0; i < NR_AC97 && card && !card->initializing; i++)
  2508. if (card->ac97_codec[i] != NULL &&
  2509. card->ac97_codec[i]->dev_mixer == minor) {
  2510. file->private_data = card->ac97_codec[i];
  2511. return nonseekable_open(inode, file);
  2512. }
  2513. }
  2514. return -ENODEV;
  2515. }
  2516. static int i810_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
  2517. unsigned long arg)
  2518. {
  2519. struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
  2520. return codec->mixer_ioctl(codec, cmd, arg);
  2521. }
  2522. static /*const*/ struct file_operations i810_mixer_fops = {
  2523. .owner = THIS_MODULE,
  2524. .llseek = no_llseek,
  2525. .ioctl = i810_ioctl_mixdev,
  2526. .open = i810_open_mixdev,
  2527. };
  2528. /* AC97 codec initialisation. These small functions exist so we don't
  2529. duplicate code between module init and apm resume */
  2530. static inline int i810_ac97_exists(struct i810_card *card, int ac97_number)
  2531. {
  2532. u32 reg = I810_IOREADL(card, GLOB_STA);
  2533. switch (ac97_number) {
  2534. case 0:
  2535. return reg & (1<<8);
  2536. case 1:
  2537. return reg & (1<<9);
  2538. case 2:
  2539. return reg & (1<<28);
  2540. }
  2541. return 0;
  2542. }
  2543. static inline int i810_ac97_enable_variable_rate(struct ac97_codec *codec)
  2544. {
  2545. i810_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
  2546. i810_ac97_set(codec,AC97_EXTENDED_STATUS,
  2547. i810_ac97_get(codec, AC97_EXTENDED_STATUS)|0xE800);
  2548. return (i810_ac97_get(codec, AC97_EXTENDED_STATUS)&1);
  2549. }
  2550. static int i810_ac97_probe_and_powerup(struct i810_card *card,struct ac97_codec *codec)
  2551. {
  2552. /* Returns 0 on failure */
  2553. int i;
  2554. if (ac97_probe_codec(codec) == 0) return 0;
  2555. /* power it all up */
  2556. i810_ac97_set(codec, AC97_POWER_CONTROL,
  2557. i810_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
  2558. /* wait for analog ready */
  2559. for (i=100; i && ((i810_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf); i--)
  2560. {
  2561. set_current_state(TASK_UNINTERRUPTIBLE);
  2562. schedule_timeout(HZ/20);
  2563. }
  2564. return i;
  2565. }
  2566. static int is_new_ich(u16 pci_id)
  2567. {
  2568. switch (pci_id) {
  2569. case PCI_DEVICE_ID_INTEL_82801DB_5:
  2570. case PCI_DEVICE_ID_INTEL_82801EB_5:
  2571. case PCI_DEVICE_ID_INTEL_ESB_5:
  2572. case PCI_DEVICE_ID_INTEL_ICH6_18:
  2573. return 1;
  2574. default:
  2575. break;
  2576. }
  2577. return 0;
  2578. }
  2579. static inline int ich_use_mmio(struct i810_card *card)
  2580. {
  2581. return is_new_ich(card->pci_id) && card->use_mmio;
  2582. }
  2583. /**
  2584. * i810_ac97_power_up_bus - bring up AC97 link
  2585. * @card : ICH audio device to power up
  2586. *
  2587. * Bring up the ACLink AC97 codec bus
  2588. */
  2589. static int i810_ac97_power_up_bus(struct i810_card *card)
  2590. {
  2591. u32 reg = I810_IOREADL(card, GLOB_CNT);
  2592. int i;
  2593. int primary_codec_id = 0;
  2594. if((reg&2)==0) /* Cold required */
  2595. reg|=2;
  2596. else
  2597. reg|=4; /* Warm */
  2598. reg&=~8; /* ACLink on */
  2599. /* At this point we deassert AC_RESET # */
  2600. I810_IOWRITEL(reg , card, GLOB_CNT);
  2601. /* We must now allow time for the Codec initialisation.
  2602. 600mS is the specified time */
  2603. for(i=0;i<10;i++)
  2604. {
  2605. if((I810_IOREADL(card, GLOB_CNT)&4)==0)
  2606. break;
  2607. set_current_state(TASK_UNINTERRUPTIBLE);
  2608. schedule_timeout(HZ/20);
  2609. }
  2610. if(i==10)
  2611. {
  2612. printk(KERN_ERR "i810_audio: AC'97 reset failed.\n");
  2613. return 0;
  2614. }
  2615. set_current_state(TASK_UNINTERRUPTIBLE);
  2616. schedule_timeout(HZ/2);
  2617. /*
  2618. * See if the primary codec comes ready. This must happen
  2619. * before we start doing DMA stuff
  2620. */
  2621. /* see i810_ac97_init for the next 10 lines (jsaw) */
  2622. if (card->use_mmio)
  2623. readw(card->ac97base_mmio);
  2624. else
  2625. inw(card->ac97base);
  2626. if (ich_use_mmio(card)) {
  2627. primary_codec_id = (int) readl(card->iobase_mmio + SDM) & 0x3;
  2628. printk(KERN_INFO "i810_audio: Primary codec has ID %d\n",
  2629. primary_codec_id);
  2630. }
  2631. if(! i810_ac97_exists(card, primary_codec_id))
  2632. {
  2633. printk(KERN_INFO "i810_audio: Codec not ready.. wait.. ");
  2634. set_current_state(TASK_UNINTERRUPTIBLE);
  2635. schedule_timeout(HZ); /* actually 600mS by the spec */
  2636. if(i810_ac97_exists(card, primary_codec_id))
  2637. printk("OK\n");
  2638. else
  2639. printk("no response.\n");
  2640. }
  2641. if (card->use_mmio)
  2642. readw(card->ac97base_mmio);
  2643. else
  2644. inw(card->ac97base);
  2645. return 1;
  2646. }
  2647. static int __devinit i810_ac97_init(struct i810_card *card)
  2648. {
  2649. int num_ac97 = 0;
  2650. int ac97_id;
  2651. int total_channels = 0;
  2652. int nr_ac97_max = card_cap[card->pci_id_internal].nr_ac97;
  2653. struct ac97_codec *codec;
  2654. u16 eid;
  2655. u32 reg;
  2656. if(!i810_ac97_power_up_bus(card)) return 0;
  2657. /* Number of channels supported */
  2658. /* What about the codec? Just because the ICH supports */
  2659. /* multiple channels doesn't mean the codec does. */
  2660. /* we'll have to modify this in the codec section below */
  2661. /* to reflect what the codec has. */
  2662. /* ICH and ICH0 only support 2 channels so don't bother */
  2663. /* to check.... */
  2664. card->channels = 2;
  2665. reg = I810_IOREADL(card, GLOB_STA);
  2666. if ( reg & 0x0200000 )
  2667. card->channels = 6;
  2668. else if ( reg & 0x0100000 )
  2669. card->channels = 4;
  2670. printk(KERN_INFO "i810_audio: Audio Controller supports %d channels.\n", card->channels);
  2671. printk(KERN_INFO "i810_audio: Defaulting to base 2 channel mode.\n");
  2672. reg = I810_IOREADL(card, GLOB_CNT);
  2673. I810_IOWRITEL(reg & 0xffcfffff, card, GLOB_CNT);
  2674. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++)
  2675. card->ac97_codec[num_ac97] = NULL;
  2676. /*@FIXME I don't know, if I'm playing to safe here... (jsaw) */
  2677. if ((nr_ac97_max > 2) && !card->use_mmio) nr_ac97_max = 2;
  2678. for (num_ac97 = 0; num_ac97 < nr_ac97_max; num_ac97++) {
  2679. /* codec reset */
  2680. printk(KERN_INFO "i810_audio: Resetting connection %d\n", num_ac97);
  2681. if (card->use_mmio)
  2682. readw(card->ac97base_mmio + 0x80*num_ac97);
  2683. else
  2684. inw(card->ac97base + 0x80*num_ac97);
  2685. /* If we have the SDATA_IN Map Register, as on ICH4, we
  2686. do not loop thru all possible codec IDs but thru all
  2687. possible IO channels. Bit 0:1 of SDM then holds the
  2688. last codec ID spoken to.
  2689. */
  2690. if (ich_use_mmio(card)) {
  2691. ac97_id = (int) readl(card->iobase_mmio + SDM) & 0x3;
  2692. printk(KERN_INFO "i810_audio: Connection %d with codec id %d\n",
  2693. num_ac97, ac97_id);
  2694. }
  2695. else {
  2696. ac97_id = num_ac97;
  2697. }
  2698. /* The ICH programmer's reference says you should */
  2699. /* check the ready status before probing. So we chk */
  2700. /* What do we do if it's not ready? Wait and try */
  2701. /* again, or abort? */
  2702. if (!i810_ac97_exists(card, ac97_id)) {
  2703. if(num_ac97 == 0)
  2704. printk(KERN_ERR "i810_audio: Primary codec not ready.\n");
  2705. }
  2706. if ((codec = ac97_alloc_codec()) == NULL)
  2707. return -ENOMEM;
  2708. /* initialize some basic codec information, other fields will be filled
  2709. in ac97_probe_codec */
  2710. codec->private_data = card;
  2711. codec->id = ac97_id;
  2712. card->ac97_id_map[ac97_id] = num_ac97 * 0x80;
  2713. if (card->use_mmio) {
  2714. codec->codec_read = i810_ac97_get_mmio;
  2715. codec->codec_write = i810_ac97_set_mmio;
  2716. }
  2717. else {
  2718. codec->codec_read = i810_ac97_get_io;
  2719. codec->codec_write = i810_ac97_set_io;
  2720. }
  2721. if(!i810_ac97_probe_and_powerup(card,codec)) {
  2722. printk(KERN_ERR "i810_audio: timed out waiting for codec %d analog ready.\n", ac97_id);
  2723. ac97_release_codec(codec);
  2724. break; /* it didn't work */
  2725. }
  2726. /* Store state information about S/PDIF transmitter */
  2727. card->ac97_status = 0;
  2728. /* Don't attempt to get eid until powerup is complete */
  2729. eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
  2730. if(eid==0xFFFF)
  2731. {
  2732. printk(KERN_WARNING "i810_audio: no codec attached ?\n");
  2733. ac97_release_codec(codec);
  2734. break;
  2735. }
  2736. /* Check for an AC97 1.0 soft modem (ID1) */
  2737. if(codec->modem)
  2738. {
  2739. printk(KERN_WARNING "i810_audio: codec %d is a softmodem - skipping.\n", ac97_id);
  2740. ac97_release_codec(codec);
  2741. continue;
  2742. }
  2743. card->ac97_features = eid;
  2744. /* Now check the codec for useful features to make up for
  2745. the dumbness of the 810 hardware engine */
  2746. if(!(eid&0x0001))
  2747. printk(KERN_WARNING "i810_audio: only 48Khz playback available.\n");
  2748. else
  2749. {
  2750. if(!i810_ac97_enable_variable_rate(codec)) {
  2751. printk(KERN_WARNING "i810_audio: Codec refused to allow VRA, using 48Khz only.\n");
  2752. card->ac97_features&=~1;
  2753. }
  2754. }
  2755. /* Turn on the amplifier */
  2756. codec->codec_write(codec, AC97_POWER_CONTROL,
  2757. codec->codec_read(codec, AC97_POWER_CONTROL) & ~0x8000);
  2758. /* Determine how many channels the codec(s) support */
  2759. /* - The primary codec always supports 2 */
  2760. /* - If the codec supports AMAP, surround DACs will */
  2761. /* automaticlly get assigned to slots. */
  2762. /* * Check for surround DACs and increment if */
  2763. /* found. */
  2764. /* - Else check if the codec is revision 2.2 */
  2765. /* * If surround DACs exist, assign them to slots */
  2766. /* and increment channel count. */
  2767. /* All of this only applies to ICH2 and above. ICH */
  2768. /* and ICH0 only support 2 channels. ICH2 will only */
  2769. /* support multiple codecs in a "split audio" config. */
  2770. /* as described above. */
  2771. /* TODO: Remove all the debugging messages! */
  2772. if((eid & 0xc000) == 0) /* primary codec */
  2773. total_channels += 2;
  2774. if(eid & 0x200) { /* GOOD, AMAP support */
  2775. if (eid & 0x0080) /* L/R Surround channels */
  2776. total_channels += 2;
  2777. if (eid & 0x0140) /* LFE and Center channels */
  2778. total_channels += 2;
  2779. printk("i810_audio: AC'97 codec %d supports AMAP, total channels = %d\n", ac97_id, total_channels);
  2780. } else if (eid & 0x0400) { /* this only works on 2.2 compliant codecs */
  2781. eid &= 0xffcf;
  2782. if((eid & 0xc000) != 0) {
  2783. switch ( total_channels ) {
  2784. case 2:
  2785. /* Set dsa1, dsa0 to 01 */
  2786. eid |= 0x0010;
  2787. break;
  2788. case 4:
  2789. /* Set dsa1, dsa0 to 10 */
  2790. eid |= 0x0020;
  2791. break;
  2792. case 6:
  2793. /* Set dsa1, dsa0 to 11 */
  2794. eid |= 0x0030;
  2795. break;
  2796. }
  2797. total_channels += 2;
  2798. }
  2799. i810_ac97_set(codec, AC97_EXTENDED_ID, eid);
  2800. eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
  2801. printk("i810_audio: AC'97 codec %d, new EID value = 0x%04x\n", ac97_id, eid);
  2802. if (eid & 0x0080) /* L/R Surround channels */
  2803. total_channels += 2;
  2804. if (eid & 0x0140) /* LFE and Center channels */
  2805. total_channels += 2;
  2806. printk("i810_audio: AC'97 codec %d, DAC map configured, total channels = %d\n", ac97_id, total_channels);
  2807. } else {
  2808. printk("i810_audio: AC'97 codec %d Unable to map surround DAC's (or DAC's not present), total channels = %d\n", ac97_id, total_channels);
  2809. }
  2810. if ((codec->dev_mixer = register_sound_mixer(&i810_mixer_fops, -1)) < 0) {
  2811. printk(KERN_ERR "i810_audio: couldn't register mixer!\n");
  2812. ac97_release_codec(codec);
  2813. break;
  2814. }
  2815. card->ac97_codec[num_ac97] = codec;
  2816. }
  2817. /* tune up the primary codec */
  2818. ac97_tune_hardware(card->pci_dev, ac97_quirks, ac97_quirk);
  2819. /* pick the minimum of channels supported by ICHx or codec(s) */
  2820. card->channels = (card->channels > total_channels)?total_channels:card->channels;
  2821. return num_ac97;
  2822. }
  2823. static void __devinit i810_configure_clocking (void)
  2824. {
  2825. struct i810_card *card;
  2826. struct i810_state *state;
  2827. struct dmabuf *dmabuf;
  2828. unsigned int i, offset, new_offset;
  2829. unsigned long flags;
  2830. card = devs;
  2831. /* We could try to set the clocking for multiple cards, but can you even have
  2832. * more than one i810 in a machine? Besides, clocking is global, so unless
  2833. * someone actually thinks more than one i810 in a machine is possible and
  2834. * decides to rewrite that little bit, setting the rate for more than one card
  2835. * is a waste of time.
  2836. */
  2837. if(card != NULL) {
  2838. state = card->states[0] = (struct i810_state *)
  2839. kzalloc(sizeof(struct i810_state), GFP_KERNEL);
  2840. if (state == NULL)
  2841. return;
  2842. dmabuf = &state->dmabuf;
  2843. dmabuf->write_channel = card->alloc_pcm_channel(card);
  2844. state->virt = 0;
  2845. state->card = card;
  2846. state->magic = I810_STATE_MAGIC;
  2847. init_waitqueue_head(&dmabuf->wait);
  2848. mutex_init(&state->open_mutex);
  2849. dmabuf->fmt = I810_FMT_STEREO | I810_FMT_16BIT;
  2850. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  2851. i810_set_spdif_output(state, -1, 0);
  2852. i810_set_dac_channels(state, 2);
  2853. i810_set_dac_rate(state, 48000);
  2854. if(prog_dmabuf(state, 0) != 0) {
  2855. goto config_out_nodmabuf;
  2856. }
  2857. if(dmabuf->dmasize < 16384) {
  2858. goto config_out;
  2859. }
  2860. dmabuf->count = dmabuf->dmasize;
  2861. CIV_TO_LVI(card, dmabuf->write_channel->port, -1);
  2862. local_irq_save(flags);
  2863. start_dac(state);
  2864. offset = i810_get_dma_addr(state, 0);
  2865. mdelay(50);
  2866. new_offset = i810_get_dma_addr(state, 0);
  2867. stop_dac(state);
  2868. local_irq_restore(flags);
  2869. i = new_offset - offset;
  2870. #ifdef DEBUG_INTERRUPTS
  2871. printk("i810_audio: %d bytes in 50 milliseconds\n", i);
  2872. #endif
  2873. if(i == 0)
  2874. goto config_out;
  2875. i = i / 4 * 20;
  2876. if (i > 48500 || i < 47500) {
  2877. clocking = clocking * clocking / i;
  2878. printk("i810_audio: setting clocking to %d\n", clocking);
  2879. }
  2880. config_out:
  2881. dealloc_dmabuf(state);
  2882. config_out_nodmabuf:
  2883. state->card->free_pcm_channel(state->card,state->dmabuf.write_channel->num);
  2884. kfree(state);
  2885. card->states[0] = NULL;
  2886. }
  2887. }
  2888. /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
  2889. until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
  2890. static int __devinit i810_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
  2891. {
  2892. struct i810_card *card;
  2893. if (pci_enable_device(pci_dev))
  2894. return -EIO;
  2895. if (pci_set_dma_mask(pci_dev, I810_DMA_MASK)) {
  2896. printk(KERN_ERR "i810_audio: architecture does not support"
  2897. " 32bit PCI busmaster DMA\n");
  2898. return -ENODEV;
  2899. }
  2900. if ((card = kzalloc(sizeof(struct i810_card), GFP_KERNEL)) == NULL) {
  2901. printk(KERN_ERR "i810_audio: out of memory\n");
  2902. return -ENOMEM;
  2903. }
  2904. card->initializing = 1;
  2905. card->pci_dev = pci_dev;
  2906. card->pci_id = pci_id->device;
  2907. card->ac97base = pci_resource_start (pci_dev, 0);
  2908. card->iobase = pci_resource_start (pci_dev, 1);
  2909. if (!(card->ac97base) || !(card->iobase)) {
  2910. card->ac97base = 0;
  2911. card->iobase = 0;
  2912. }
  2913. /* if chipset could have mmio capability, check it */
  2914. if (card_cap[pci_id->driver_data].flags & CAP_MMIO) {
  2915. card->ac97base_mmio_phys = pci_resource_start (pci_dev, 2);
  2916. card->iobase_mmio_phys = pci_resource_start (pci_dev, 3);
  2917. if ((card->ac97base_mmio_phys) && (card->iobase_mmio_phys)) {
  2918. card->use_mmio = 1;
  2919. }
  2920. else {
  2921. card->ac97base_mmio_phys = 0;
  2922. card->iobase_mmio_phys = 0;
  2923. }
  2924. }
  2925. if (!(card->use_mmio) && (!(card->iobase) || !(card->ac97base))) {
  2926. printk(KERN_ERR "i810_audio: No I/O resources available.\n");
  2927. goto out_mem;
  2928. }
  2929. card->irq = pci_dev->irq;
  2930. card->next = devs;
  2931. card->magic = I810_CARD_MAGIC;
  2932. #ifdef CONFIG_PM
  2933. card->pm_suspended=0;
  2934. #endif
  2935. spin_lock_init(&card->lock);
  2936. spin_lock_init(&card->ac97_lock);
  2937. devs = card;
  2938. pci_set_master(pci_dev);
  2939. printk(KERN_INFO "i810: %s found at IO 0x%04lx and 0x%04lx, "
  2940. "MEM 0x%04lx and 0x%04lx, IRQ %d\n",
  2941. card_names[pci_id->driver_data],
  2942. card->iobase, card->ac97base,
  2943. card->ac97base_mmio_phys, card->iobase_mmio_phys,
  2944. card->irq);
  2945. card->alloc_pcm_channel = i810_alloc_pcm_channel;
  2946. card->alloc_rec_pcm_channel = i810_alloc_rec_pcm_channel;
  2947. card->alloc_rec_mic_channel = i810_alloc_rec_mic_channel;
  2948. card->free_pcm_channel = i810_free_pcm_channel;
  2949. if ((card->channel = pci_alloc_consistent(pci_dev,
  2950. sizeof(struct i810_channel)*NR_HW_CH, &card->chandma)) == NULL) {
  2951. printk(KERN_ERR "i810: cannot allocate channel DMA memory\n");
  2952. goto out_mem;
  2953. }
  2954. { /* We may dispose of this altogether some time soon, so... */
  2955. struct i810_channel *cp = card->channel;
  2956. cp[0].offset = 0;
  2957. cp[0].port = 0x00;
  2958. cp[0].num = 0;
  2959. cp[1].offset = 0;
  2960. cp[1].port = 0x10;
  2961. cp[1].num = 1;
  2962. cp[2].offset = 0;
  2963. cp[2].port = 0x20;
  2964. cp[2].num = 2;
  2965. }
  2966. /* claim our iospace and irq */
  2967. if (!request_region(card->iobase, 64, card_names[pci_id->driver_data])) {
  2968. printk(KERN_ERR "i810_audio: unable to allocate region %lx\n", card->iobase);
  2969. goto out_region1;
  2970. }
  2971. if (!request_region(card->ac97base, 256, card_names[pci_id->driver_data])) {
  2972. printk(KERN_ERR "i810_audio: unable to allocate region %lx\n", card->ac97base);
  2973. goto out_region2;
  2974. }
  2975. if (card->use_mmio) {
  2976. if (request_mem_region(card->ac97base_mmio_phys, 512, "ich_audio MMBAR")) {
  2977. if ((card->ac97base_mmio = ioremap(card->ac97base_mmio_phys, 512))) { /*@FIXME can ioremap fail? don't know (jsaw) */
  2978. if (request_mem_region(card->iobase_mmio_phys, 256, "ich_audio MBBAR")) {
  2979. if ((card->iobase_mmio = ioremap(card->iobase_mmio_phys, 256))) {
  2980. printk(KERN_INFO "i810: %s mmio at 0x%04lx and 0x%04lx\n",
  2981. card_names[pci_id->driver_data],
  2982. (unsigned long) card->ac97base_mmio,
  2983. (unsigned long) card->iobase_mmio);
  2984. }
  2985. else {
  2986. iounmap(card->ac97base_mmio);
  2987. release_mem_region(card->ac97base_mmio_phys, 512);
  2988. release_mem_region(card->iobase_mmio_phys, 512);
  2989. card->use_mmio = 0;
  2990. }
  2991. }
  2992. else {
  2993. iounmap(card->ac97base_mmio);
  2994. release_mem_region(card->ac97base_mmio_phys, 512);
  2995. card->use_mmio = 0;
  2996. }
  2997. }
  2998. }
  2999. else {
  3000. card->use_mmio = 0;
  3001. }
  3002. }
  3003. /* initialize AC97 codec and register /dev/mixer */
  3004. if (i810_ac97_init(card) <= 0)
  3005. goto out_iospace;
  3006. pci_set_drvdata(pci_dev, card);
  3007. if(clocking == 0) {
  3008. clocking = 48000;
  3009. i810_configure_clocking();
  3010. }
  3011. /* register /dev/dsp */
  3012. if ((card->dev_audio = register_sound_dsp(&i810_audio_fops, -1)) < 0) {
  3013. int i;
  3014. printk(KERN_ERR "i810_audio: couldn't register DSP device!\n");
  3015. for (i = 0; i < NR_AC97; i++)
  3016. if (card->ac97_codec[i] != NULL) {
  3017. unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
  3018. ac97_release_codec(card->ac97_codec[i]);
  3019. }
  3020. goto out_iospace;
  3021. }
  3022. if (request_irq(card->irq, &i810_interrupt, IRQF_SHARED,
  3023. card_names[pci_id->driver_data], card)) {
  3024. printk(KERN_ERR "i810_audio: unable to allocate irq %d\n", card->irq);
  3025. goto out_iospace;
  3026. }
  3027. card->initializing = 0;
  3028. return 0;
  3029. out_iospace:
  3030. if (card->use_mmio) {
  3031. iounmap(card->ac97base_mmio);
  3032. iounmap(card->iobase_mmio);
  3033. release_mem_region(card->ac97base_mmio_phys, 512);
  3034. release_mem_region(card->iobase_mmio_phys, 256);
  3035. }
  3036. release_region(card->ac97base, 256);
  3037. out_region2:
  3038. release_region(card->iobase, 64);
  3039. out_region1:
  3040. pci_free_consistent(pci_dev, sizeof(struct i810_channel)*NR_HW_CH,
  3041. card->channel, card->chandma);
  3042. out_mem:
  3043. kfree(card);
  3044. return -ENODEV;
  3045. }
  3046. static void __devexit i810_remove(struct pci_dev *pci_dev)
  3047. {
  3048. int i;
  3049. struct i810_card *card = pci_get_drvdata(pci_dev);
  3050. /* free hardware resources */
  3051. free_irq(card->irq, devs);
  3052. release_region(card->iobase, 64);
  3053. release_region(card->ac97base, 256);
  3054. pci_free_consistent(pci_dev, sizeof(struct i810_channel)*NR_HW_CH,
  3055. card->channel, card->chandma);
  3056. if (card->use_mmio) {
  3057. iounmap(card->ac97base_mmio);
  3058. iounmap(card->iobase_mmio);
  3059. release_mem_region(card->ac97base_mmio_phys, 512);
  3060. release_mem_region(card->iobase_mmio_phys, 256);
  3061. }
  3062. /* unregister audio devices */
  3063. for (i = 0; i < NR_AC97; i++)
  3064. if (card->ac97_codec[i] != NULL) {
  3065. unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
  3066. ac97_release_codec(card->ac97_codec[i]);
  3067. card->ac97_codec[i] = NULL;
  3068. }
  3069. unregister_sound_dsp(card->dev_audio);
  3070. kfree(card);
  3071. }
  3072. #ifdef CONFIG_PM
  3073. static int i810_pm_suspend(struct pci_dev *dev, pm_message_t pm_state)
  3074. {
  3075. struct i810_card *card = pci_get_drvdata(dev);
  3076. struct i810_state *state;
  3077. unsigned long flags;
  3078. struct dmabuf *dmabuf;
  3079. int i,num_ac97;
  3080. #ifdef DEBUG
  3081. printk("i810_audio: i810_pm_suspend called\n");
  3082. #endif
  3083. if(!card) return 0;
  3084. spin_lock_irqsave(&card->lock, flags);
  3085. card->pm_suspended=1;
  3086. for(i=0;i<NR_HW_CH;i++) {
  3087. state = card->states[i];
  3088. if(!state) continue;
  3089. /* this happens only if there are open files */
  3090. dmabuf = &state->dmabuf;
  3091. if(dmabuf->enable & DAC_RUNNING ||
  3092. (dmabuf->count && (dmabuf->trigger & PCM_ENABLE_OUTPUT))) {
  3093. state->pm_saved_dac_rate=dmabuf->rate;
  3094. stop_dac(state);
  3095. } else {
  3096. state->pm_saved_dac_rate=0;
  3097. }
  3098. if(dmabuf->enable & ADC_RUNNING) {
  3099. state->pm_saved_adc_rate=dmabuf->rate;
  3100. stop_adc(state);
  3101. } else {
  3102. state->pm_saved_adc_rate=0;
  3103. }
  3104. dmabuf->ready = 0;
  3105. dmabuf->swptr = dmabuf->hwptr = 0;
  3106. dmabuf->count = dmabuf->total_bytes = 0;
  3107. }
  3108. spin_unlock_irqrestore(&card->lock, flags);
  3109. /* save mixer settings */
  3110. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  3111. struct ac97_codec *codec = card->ac97_codec[num_ac97];
  3112. if(!codec) continue;
  3113. for(i=0;i< SOUND_MIXER_NRDEVICES ;i++) {
  3114. if((supported_mixer(codec,i)) &&
  3115. (codec->read_mixer)) {
  3116. card->pm_saved_mixer_settings[i][num_ac97]=
  3117. codec->read_mixer(codec,i);
  3118. }
  3119. }
  3120. }
  3121. pci_save_state(dev); /* XXX do we need this? */
  3122. pci_disable_device(dev); /* disable busmastering */
  3123. pci_set_power_state(dev,3); /* Zzz. */
  3124. return 0;
  3125. }
  3126. static int i810_pm_resume(struct pci_dev *dev)
  3127. {
  3128. int num_ac97,i=0;
  3129. struct i810_card *card=pci_get_drvdata(dev);
  3130. pci_enable_device(dev);
  3131. pci_restore_state (dev);
  3132. /* observation of a toshiba portege 3440ct suggests that the
  3133. hardware has to be more or less completely reinitialized from
  3134. scratch after an apm suspend. Works For Me. -dan */
  3135. i810_ac97_power_up_bus(card);
  3136. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  3137. struct ac97_codec *codec = card->ac97_codec[num_ac97];
  3138. /* check they haven't stolen the hardware while we were
  3139. away */
  3140. if(!codec || !i810_ac97_exists(card,num_ac97)) {
  3141. if(num_ac97) continue;
  3142. else BUG();
  3143. }
  3144. if(!i810_ac97_probe_and_powerup(card,codec)) BUG();
  3145. if((card->ac97_features&0x0001)) {
  3146. /* at probe time we found we could do variable
  3147. rates, but APM suspend has made it forget
  3148. its magical powers */
  3149. if(!i810_ac97_enable_variable_rate(codec)) BUG();
  3150. }
  3151. /* we lost our mixer settings, so restore them */
  3152. for(i=0;i< SOUND_MIXER_NRDEVICES ;i++) {
  3153. if(supported_mixer(codec,i)){
  3154. int val=card->
  3155. pm_saved_mixer_settings[i][num_ac97];
  3156. codec->mixer_state[i]=val;
  3157. codec->write_mixer(codec,i,
  3158. (val & 0xff) ,
  3159. ((val >> 8) & 0xff) );
  3160. }
  3161. }
  3162. }
  3163. /* we need to restore the sample rate from whatever it was */
  3164. for(i=0;i<NR_HW_CH;i++) {
  3165. struct i810_state * state=card->states[i];
  3166. if(state) {
  3167. if(state->pm_saved_adc_rate)
  3168. i810_set_adc_rate(state,state->pm_saved_adc_rate);
  3169. if(state->pm_saved_dac_rate)
  3170. i810_set_dac_rate(state,state->pm_saved_dac_rate);
  3171. }
  3172. }
  3173. card->pm_suspended = 0;
  3174. /* any processes that were reading/writing during the suspend
  3175. probably ended up here */
  3176. for(i=0;i<NR_HW_CH;i++) {
  3177. struct i810_state *state = card->states[i];
  3178. if(state) wake_up(&state->dmabuf.wait);
  3179. }
  3180. return 0;
  3181. }
  3182. #endif /* CONFIG_PM */
  3183. MODULE_AUTHOR("The Linux kernel team");
  3184. MODULE_DESCRIPTION("Intel 810 audio support");
  3185. MODULE_LICENSE("GPL");
  3186. module_param(ftsodell, int, 0444);
  3187. module_param(clocking, uint, 0444);
  3188. module_param(strict_clocking, int, 0444);
  3189. module_param(spdif_locked, int, 0444);
  3190. #define I810_MODULE_NAME "i810_audio"
  3191. static struct pci_driver i810_pci_driver = {
  3192. .name = I810_MODULE_NAME,
  3193. .id_table = i810_pci_tbl,
  3194. .probe = i810_probe,
  3195. .remove = __devexit_p(i810_remove),
  3196. #ifdef CONFIG_PM
  3197. .suspend = i810_pm_suspend,
  3198. .resume = i810_pm_resume,
  3199. #endif /* CONFIG_PM */
  3200. };
  3201. static int __init i810_init_module (void)
  3202. {
  3203. int retval;
  3204. printk(KERN_INFO "Intel 810 + AC97 Audio, version "
  3205. DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
  3206. retval = pci_register_driver(&i810_pci_driver);
  3207. if (retval)
  3208. return retval;
  3209. if(ftsodell != 0) {
  3210. printk("i810_audio: ftsodell is now a deprecated option.\n");
  3211. }
  3212. if(spdif_locked > 0 ) {
  3213. if(spdif_locked == 32000 || spdif_locked == 44100 || spdif_locked == 48000) {
  3214. printk("i810_audio: Enabling S/PDIF at sample rate %dHz.\n", spdif_locked);
  3215. } else {
  3216. printk("i810_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
  3217. spdif_locked = 0;
  3218. }
  3219. }
  3220. return 0;
  3221. }
  3222. static void __exit i810_cleanup_module (void)
  3223. {
  3224. pci_unregister_driver(&i810_pci_driver);
  3225. }
  3226. module_init(i810_init_module);
  3227. module_exit(i810_cleanup_module);
  3228. /*
  3229. Local Variables:
  3230. c-basic-offset: 8
  3231. End:
  3232. */