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