via82cxxx_audio.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  1. /*
  2. * Support for VIA 82Cxxx Audio Codecs
  3. * Copyright 1999,2000 Jeff Garzik
  4. *
  5. * Updated to support the VIA 8233/8235 audio subsystem
  6. * Alan Cox <alan@redhat.com> (C) Copyright 2002, 2003 Red Hat Inc
  7. *
  8. * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
  9. * See the "COPYING" file distributed with this software for more info.
  10. * NO WARRANTY
  11. *
  12. * For a list of known bugs (errata) and documentation,
  13. * see via-audio.pdf in Documentation/DocBook.
  14. * If this documentation does not exist, run "make pdfdocs".
  15. */
  16. #define VIA_VERSION "1.9.1-ac4-2.5"
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/pci.h>
  22. #include <linux/poison.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/sound.h>
  28. #include <linux/poll.h>
  29. #include <linux/soundcard.h>
  30. #include <linux/ac97_codec.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/ioport.h>
  33. #include <linux/delay.h>
  34. #include <linux/dma-mapping.h>
  35. #include <asm/io.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/mutex.h>
  38. #include "sound_config.h"
  39. #include "dev_table.h"
  40. #include "mpu401.h"
  41. #undef VIA_DEBUG /* define to enable debugging output and checks */
  42. #ifdef VIA_DEBUG
  43. /* note: prints function name for you */
  44. #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
  45. #else
  46. #define DPRINTK(fmt, args...)
  47. #endif
  48. #undef VIA_NDEBUG /* define to disable lightweight runtime checks */
  49. #ifdef VIA_NDEBUG
  50. #define assert(expr)
  51. #else
  52. #define assert(expr) \
  53. if(!(expr)) { \
  54. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  55. #expr,__FILE__,__FUNCTION__,__LINE__); \
  56. }
  57. #endif
  58. #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
  59. #define MAX_CARDS 1
  60. #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
  61. #define VIA_MODULE_NAME "via82cxxx"
  62. #define PFX VIA_MODULE_NAME ": "
  63. #define VIA_COUNTER_LIMIT 100000
  64. /* size of DMA buffers */
  65. #define VIA_MAX_BUFFER_DMA_PAGES 32
  66. /* buffering default values in ms */
  67. #define VIA_DEFAULT_FRAG_TIME 20
  68. #define VIA_DEFAULT_BUFFER_TIME 500
  69. /* the hardware has a 256 fragment limit */
  70. #define VIA_MIN_FRAG_NUMBER 2
  71. #define VIA_MAX_FRAG_NUMBER 128
  72. #define VIA_MAX_FRAG_SIZE PAGE_SIZE
  73. #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
  74. /* 82C686 function 5 (audio codec) PCI configuration registers */
  75. #define VIA_ACLINK_STATUS 0x40
  76. #define VIA_ACLINK_CTRL 0x41
  77. #define VIA_FUNC_ENABLE 0x42
  78. #define VIA_PNP_CONTROL 0x43
  79. #define VIA_FM_NMI_CTRL 0x48
  80. /*
  81. * controller base 0 (scatter-gather) registers
  82. *
  83. * NOTE: Via datasheet lists first channel as "read"
  84. * channel and second channel as "write" channel.
  85. * I changed the naming of the constants to be more
  86. * clear than I felt the datasheet to be.
  87. */
  88. #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
  89. #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
  90. #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
  91. #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
  92. #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
  93. #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
  94. #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
  95. #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
  96. /* offsets from base */
  97. #define VIA_PCM_STATUS 0x00
  98. #define VIA_PCM_CONTROL 0x01
  99. #define VIA_PCM_TYPE 0x02
  100. #define VIA_PCM_LEFTVOL 0x02
  101. #define VIA_PCM_RIGHTVOL 0x03
  102. #define VIA_PCM_TABLE_ADDR 0x04
  103. #define VIA_PCM_STOPRATE 0x08 /* 8233+ */
  104. #define VIA_PCM_BLOCK_COUNT 0x0C
  105. /* XXX unused DMA channel for FM PCM data */
  106. #define VIA_BASE0_FM_OUT_CHAN 0x20
  107. #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
  108. #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
  109. #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
  110. /* Six channel audio output on 8233 */
  111. #define VIA_BASE0_MULTI_OUT_CHAN 0x40
  112. #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
  113. #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
  114. #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
  115. #define VIA_BASE0_AC97_CTRL 0x80
  116. #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
  117. #define VIA_BASE0_GPI_INT_ENABLE 0x8C
  118. #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
  119. #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
  120. #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
  121. #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
  122. /* Newer VIA we need to monitor the low 3 bits of each channel. This
  123. mask covers the channels we don't yet use as well
  124. */
  125. #define VIA_NEW_INTR_MASK 0x77077777UL
  126. /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
  127. #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
  128. #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
  129. #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
  130. #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
  131. #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
  132. #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
  133. #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
  134. #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
  135. #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
  136. #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
  137. #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
  138. VIA_IRQ_ON_FLAG | \
  139. VIA_IRQ_ON_EOL)
  140. #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
  141. /* PCI configuration register bits and masks */
  142. #define VIA_CR40_AC97_READY 0x01
  143. #define VIA_CR40_AC97_LOW_POWER 0x02
  144. #define VIA_CR40_SECONDARY_READY 0x04
  145. #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
  146. #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
  147. #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
  148. #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
  149. #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
  150. #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
  151. #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
  152. #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
  153. #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
  154. VIA_CR41_AC97_WAKEUP | \
  155. VIA_CR41_AC97_SDO)
  156. #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
  157. VIA_CR41_AC97_RESET | \
  158. VIA_CR41_VRA | \
  159. VIA_CR41_PCM_ENABLE)
  160. #define VIA_CR42_SB_ENABLE 0x01
  161. #define VIA_CR42_MIDI_ENABLE 0x02
  162. #define VIA_CR42_FM_ENABLE 0x04
  163. #define VIA_CR42_GAME_ENABLE 0x08
  164. #define VIA_CR42_MIDI_IRQMASK 0x40
  165. #define VIA_CR42_MIDI_PNP 0x80
  166. #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
  167. #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
  168. #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
  169. /* controller base 0 register bitmasks */
  170. #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
  171. #define VIA_SGD_STOPPED (1 << 2)
  172. #define VIA_SGD_PAUSED (1 << 6)
  173. #define VIA_SGD_ACTIVE (1 << 7)
  174. #define VIA_SGD_TERMINATE (1 << 6)
  175. #define VIA_SGD_FLAG (1 << 0)
  176. #define VIA_SGD_EOL (1 << 1)
  177. #define VIA_SGD_START (1 << 7)
  178. #define VIA_CR80_FIRST_CODEC 0
  179. #define VIA_CR80_SECOND_CODEC (1 << 30)
  180. #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
  181. #define VIA_CR80_VALID (1 << 25)
  182. #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
  183. #define VIA_CR80_BUSY (1 << 24)
  184. #define VIA_CR83_BUSY (1)
  185. #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
  186. #define VIA_CR80_READ (1 << 23)
  187. #define VIA_CR80_WRITE_MODE 0
  188. #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
  189. /* capabilities we announce */
  190. #ifdef VIA_SUPPORT_MMAP
  191. #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
  192. DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
  193. #else
  194. #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
  195. DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
  196. #endif
  197. /* scatter-gather DMA table entry, exactly as passed to hardware */
  198. struct via_sgd_table {
  199. u32 addr;
  200. u32 count; /* includes additional VIA_xxx bits also */
  201. };
  202. #define VIA_EOL (1 << 31)
  203. #define VIA_FLAG (1 << 30)
  204. #define VIA_STOP (1 << 29)
  205. enum via_channel_states {
  206. sgd_stopped = 0,
  207. sgd_in_progress = 1,
  208. };
  209. struct via_buffer_pgtbl {
  210. dma_addr_t handle;
  211. void *cpuaddr;
  212. };
  213. struct via_channel {
  214. atomic_t n_frags;
  215. atomic_t hw_ptr;
  216. wait_queue_head_t wait;
  217. unsigned int sw_ptr;
  218. unsigned int slop_len;
  219. unsigned int n_irqs;
  220. int bytes;
  221. unsigned is_active : 1;
  222. unsigned is_record : 1;
  223. unsigned is_mapped : 1;
  224. unsigned is_enabled : 1;
  225. unsigned is_multi: 1; /* 8233 6 channel */
  226. u8 pcm_fmt; /* VIA_PCM_FMT_xxx */
  227. u8 channels; /* Channel count */
  228. unsigned rate; /* sample rate */
  229. unsigned int frag_size;
  230. unsigned int frag_number;
  231. unsigned char intmask;
  232. volatile struct via_sgd_table *sgtable;
  233. dma_addr_t sgt_handle;
  234. unsigned int page_number;
  235. struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
  236. long iobase;
  237. const char *name;
  238. };
  239. /* data stored for each chip */
  240. struct via_info {
  241. struct pci_dev *pdev;
  242. long baseaddr;
  243. struct ac97_codec *ac97;
  244. spinlock_t ac97_lock;
  245. spinlock_t lock;
  246. int card_num; /* unique card number, from 0 */
  247. int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
  248. unsigned rev_h : 1;
  249. unsigned legacy: 1; /* Has legacy ports */
  250. unsigned intmask: 1; /* Needs int bits */
  251. unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
  252. unsigned volume: 1;
  253. unsigned locked_rate : 1;
  254. int mixer_vol; /* 8233/35 volume - not yet implemented */
  255. struct mutex syscall_mutex;
  256. struct mutex open_mutex;
  257. /* The 8233/8235 have 4 DX audio channels, two record and
  258. one six channel out. We bind ch_in to DX 1, ch_out to multichannel
  259. and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
  260. moment */
  261. struct via_channel ch_in;
  262. struct via_channel ch_out;
  263. struct via_channel ch_fm;
  264. #ifdef CONFIG_MIDI_VIA82CXXX
  265. void *midi_devc;
  266. struct address_info midi_info;
  267. #endif
  268. };
  269. /* number of cards, used for assigning unique numbers to cards */
  270. static unsigned via_num_cards;
  271. /****************************************************************
  272. *
  273. * prototypes
  274. *
  275. *
  276. */
  277. static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
  278. static void __devexit via_remove_one (struct pci_dev *pdev);
  279. static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
  280. static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
  281. static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
  282. static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
  283. static int via_dsp_open (struct inode *inode, struct file *file);
  284. static int via_dsp_release(struct inode *inode, struct file *file);
  285. static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
  286. static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
  287. static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
  288. static u8 via_ac97_wait_idle (struct via_info *card);
  289. static void via_chan_free (struct via_info *card, struct via_channel *chan);
  290. static void via_chan_clear (struct via_info *card, struct via_channel *chan);
  291. static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
  292. static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
  293. /****************************************************************
  294. *
  295. * Various data the driver needs
  296. *
  297. *
  298. */
  299. static struct pci_device_id via_pci_tbl[] = {
  300. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
  301. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  302. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
  303. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  304. { 0, }
  305. };
  306. MODULE_DEVICE_TABLE(pci,via_pci_tbl);
  307. static struct pci_driver via_driver = {
  308. .name = VIA_MODULE_NAME,
  309. .id_table = via_pci_tbl,
  310. .probe = via_init_one,
  311. .remove = __devexit_p(via_remove_one),
  312. };
  313. /****************************************************************
  314. *
  315. * Low-level base 0 register read/write helpers
  316. *
  317. *
  318. */
  319. /**
  320. * via_chan_stop - Terminate DMA on specified PCM channel
  321. * @iobase: PCI base address for SGD channel registers
  322. *
  323. * Terminate scatter-gather DMA operation for given
  324. * channel (derived from @iobase), if DMA is active.
  325. *
  326. * Note that @iobase is not the PCI base address,
  327. * but the PCI base address plus an offset to
  328. * one of three PCM channels supported by the chip.
  329. *
  330. */
  331. static inline void via_chan_stop (long iobase)
  332. {
  333. if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
  334. outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
  335. }
  336. /**
  337. * via_chan_status_clear - Clear status flags on specified DMA channel
  338. * @iobase: PCI base address for SGD channel registers
  339. *
  340. * Clear any pending status flags for the given
  341. * DMA channel (derived from @iobase), if any
  342. * flags are asserted.
  343. *
  344. * Note that @iobase is not the PCI base address,
  345. * but the PCI base address plus an offset to
  346. * one of three PCM channels supported by the chip.
  347. *
  348. */
  349. static inline void via_chan_status_clear (long iobase)
  350. {
  351. u8 tmp = inb (iobase + VIA_PCM_STATUS);
  352. if (tmp != 0)
  353. outb (tmp, iobase + VIA_PCM_STATUS);
  354. }
  355. /**
  356. * sg_begin - Begin recording or playback on a PCM channel
  357. * @chan: Channel for which DMA operation shall begin
  358. *
  359. * Start scatter-gather DMA for the given channel.
  360. *
  361. */
  362. static inline void sg_begin (struct via_channel *chan)
  363. {
  364. DPRINTK("Start with intmask %d\n", chan->intmask);
  365. DPRINTK("About to start from %d to %d\n",
  366. inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
  367. inb(chan->iobase + VIA_PCM_STOPRATE + 3));
  368. outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
  369. DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
  370. DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
  371. }
  372. static int sg_active (long iobase)
  373. {
  374. u8 tmp = inb (iobase + VIA_PCM_STATUS);
  375. if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
  376. printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
  377. return 0;
  378. }
  379. if (tmp & VIA_SGD_ACTIVE)
  380. return 1;
  381. return 0;
  382. }
  383. static int via_sg_offset(struct via_channel *chan)
  384. {
  385. return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
  386. }
  387. /****************************************************************
  388. *
  389. * Miscellaneous debris
  390. *
  391. *
  392. */
  393. /**
  394. * via_syscall_down - down the card-specific syscell semaphore
  395. * @card: Private info for specified board
  396. * @nonblock: boolean, non-zero if O_NONBLOCK is set
  397. *
  398. * Encapsulates standard method of acquiring the syscall sem.
  399. *
  400. * Returns negative errno on error, or zero for success.
  401. */
  402. static inline int via_syscall_down (struct via_info *card, int nonblock)
  403. {
  404. /* Thomas Sailer:
  405. * EAGAIN is supposed to be used if IO is pending,
  406. * not if there is contention on some internal
  407. * synchronization primitive which should be
  408. * held only for a short time anyway
  409. */
  410. nonblock = 0;
  411. if (nonblock) {
  412. if (!mutex_trylock(&card->syscall_mutex))
  413. return -EAGAIN;
  414. } else {
  415. if (mutex_lock_interruptible(&card->syscall_mutex))
  416. return -ERESTARTSYS;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * via_stop_everything - Stop all audio operations
  422. * @card: Private info for specified board
  423. *
  424. * Stops all DMA operations and interrupts, and clear
  425. * any pending status bits resulting from those operations.
  426. */
  427. static void via_stop_everything (struct via_info *card)
  428. {
  429. u8 tmp, new_tmp;
  430. DPRINTK ("ENTER\n");
  431. assert (card != NULL);
  432. /*
  433. * terminate any existing operations on audio read/write channels
  434. */
  435. via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
  436. via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
  437. via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
  438. if(card->sixchannel)
  439. via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
  440. /*
  441. * clear any existing stops / flags (sanity check mainly)
  442. */
  443. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
  444. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
  445. via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
  446. if(card->sixchannel)
  447. via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
  448. /*
  449. * clear any enabled interrupt bits
  450. */
  451. tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
  452. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  453. if (tmp != new_tmp)
  454. outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
  455. tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
  456. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  457. if (tmp != new_tmp)
  458. outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
  459. tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
  460. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  461. if (tmp != new_tmp)
  462. outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
  463. if(card->sixchannel)
  464. {
  465. tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
  466. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  467. if (tmp != new_tmp)
  468. outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
  469. }
  470. udelay(10);
  471. /*
  472. * clear any existing flags
  473. */
  474. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
  475. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
  476. via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
  477. DPRINTK ("EXIT\n");
  478. }
  479. /**
  480. * via_set_rate - Set PCM rate for given channel
  481. * @ac97: Pointer to generic codec info struct
  482. * @chan: Private info for specified channel
  483. * @rate: Desired PCM sample rate, in Khz
  484. *
  485. * Sets the PCM sample rate for a channel.
  486. *
  487. * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
  488. * due to hardware constraints.
  489. */
  490. static int via_set_rate (struct ac97_codec *ac97,
  491. struct via_channel *chan, unsigned rate)
  492. {
  493. struct via_info *card = ac97->private_data;
  494. int rate_reg;
  495. u32 dacp;
  496. u32 mast_vol, phone_vol, mono_vol, pcm_vol;
  497. u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */
  498. DPRINTK ("ENTER, rate = %d\n", rate);
  499. if (chan->rate == rate)
  500. goto out;
  501. if (card->locked_rate) {
  502. chan->rate = 48000;
  503. goto out;
  504. }
  505. if (rate > 48000) rate = 48000;
  506. if (rate < 4000) rate = 4000;
  507. rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
  508. AC97_PCM_FRONT_DAC_RATE;
  509. /* Save current state */
  510. dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
  511. mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
  512. mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
  513. phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
  514. pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
  515. /* Mute - largely reduces popping */
  516. via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
  517. via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
  518. via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
  519. via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
  520. /* Power down the DAC */
  521. via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
  522. /* Set new rate */
  523. via_ac97_write_reg (ac97, rate_reg, rate);
  524. /* Power DAC back up */
  525. via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
  526. udelay (200); /* reduces popping */
  527. /* Restore volumes */
  528. via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
  529. via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
  530. via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
  531. via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
  532. /* the hardware might return a value different than what we
  533. * passed to it, so read the rate value back from hardware
  534. * to see what we came up with
  535. */
  536. chan->rate = via_ac97_read_reg (ac97, rate_reg);
  537. if (chan->rate == 0) {
  538. card->locked_rate = 1;
  539. chan->rate = 48000;
  540. printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
  541. }
  542. out:
  543. DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
  544. return chan->rate;
  545. }
  546. /****************************************************************
  547. *
  548. * Channel-specific operations
  549. *
  550. *
  551. */
  552. /**
  553. * via_chan_init_defaults - Initialize a struct via_channel
  554. * @card: Private audio chip info
  555. * @chan: Channel to be initialized
  556. *
  557. * Zero @chan, and then set all static defaults for the structure.
  558. */
  559. static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
  560. {
  561. memset (chan, 0, sizeof (*chan));
  562. if(card->intmask)
  563. chan->intmask = 0x23; /* Turn on the IRQ bits */
  564. if (chan == &card->ch_out) {
  565. chan->name = "PCM-OUT";
  566. if(card->sixchannel)
  567. {
  568. chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
  569. chan->is_multi = 1;
  570. DPRINTK("Using multichannel for pcm out\n");
  571. }
  572. else
  573. chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
  574. } else if (chan == &card->ch_in) {
  575. chan->name = "PCM-IN";
  576. chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
  577. chan->is_record = 1;
  578. } else if (chan == &card->ch_fm) {
  579. chan->name = "PCM-OUT-FM";
  580. chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
  581. } else {
  582. BUG();
  583. }
  584. init_waitqueue_head (&chan->wait);
  585. chan->pcm_fmt = VIA_PCM_FMT_MASK;
  586. chan->is_enabled = 1;
  587. chan->frag_number = 0;
  588. chan->frag_size = 0;
  589. atomic_set(&chan->n_frags, 0);
  590. atomic_set (&chan->hw_ptr, 0);
  591. }
  592. /**
  593. * via_chan_init - Initialize PCM channel
  594. * @card: Private audio chip info
  595. * @chan: Channel to be initialized
  596. *
  597. * Performs some of the preparations necessary to begin
  598. * using a PCM channel.
  599. *
  600. * Currently the preparations consist of
  601. * setting the PCM channel to a known state.
  602. */
  603. static void via_chan_init (struct via_info *card, struct via_channel *chan)
  604. {
  605. DPRINTK ("ENTER\n");
  606. /* bzero channel structure, and init members to defaults */
  607. via_chan_init_defaults (card, chan);
  608. /* stop any existing channel output */
  609. via_chan_clear (card, chan);
  610. via_chan_status_clear (chan->iobase);
  611. via_chan_pcm_fmt (chan, 1);
  612. DPRINTK ("EXIT\n");
  613. }
  614. /**
  615. * via_chan_buffer_init - Initialize PCM channel buffer
  616. * @card: Private audio chip info
  617. * @chan: Channel to be initialized
  618. *
  619. * Performs some of the preparations necessary to begin
  620. * using a PCM channel.
  621. *
  622. * Currently the preparations include allocating the
  623. * scatter-gather DMA table and buffers,
  624. * and passing the
  625. * address of the DMA table to the hardware.
  626. *
  627. * Note that special care is taken when passing the
  628. * DMA table address to hardware, because it was found
  629. * during driver development that the hardware did not
  630. * always "take" the address.
  631. */
  632. static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
  633. {
  634. int page, offset;
  635. int i;
  636. DPRINTK ("ENTER\n");
  637. chan->intmask = 0;
  638. if(card->intmask)
  639. chan->intmask = 0x23; /* Turn on the IRQ bits */
  640. if (chan->sgtable != NULL) {
  641. DPRINTK ("EXIT\n");
  642. return 0;
  643. }
  644. /* alloc DMA-able memory for scatter-gather table */
  645. chan->sgtable = pci_alloc_consistent (card->pdev,
  646. (sizeof (struct via_sgd_table) * chan->frag_number),
  647. &chan->sgt_handle);
  648. if (!chan->sgtable) {
  649. printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
  650. DPRINTK ("EXIT\n");
  651. return -ENOMEM;
  652. }
  653. memset ((void*)chan->sgtable, 0,
  654. (sizeof (struct via_sgd_table) * chan->frag_number));
  655. /* alloc DMA-able memory for scatter-gather buffers */
  656. chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
  657. (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
  658. for (i = 0; i < chan->page_number; i++) {
  659. chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
  660. &chan->pgtbl[i].handle);
  661. if (!chan->pgtbl[i].cpuaddr) {
  662. chan->page_number = i;
  663. goto err_out_nomem;
  664. }
  665. #ifndef VIA_NDEBUG
  666. memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
  667. #endif
  668. #if 1
  669. DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
  670. i, (long)chan->pgtbl[i].handle,
  671. virt_to_phys(chan->pgtbl[i].cpuaddr),
  672. chan->pgtbl[i].cpuaddr);
  673. #endif
  674. }
  675. for (i = 0; i < chan->frag_number; i++) {
  676. page = i / (PAGE_SIZE / chan->frag_size);
  677. offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
  678. chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
  679. chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
  680. #if 1
  681. DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
  682. i,
  683. (long)chan->sgtable[i].addr);
  684. #endif
  685. }
  686. /* overwrite the last buffer information */
  687. chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
  688. /* set location of DMA-able scatter-gather info table */
  689. DPRINTK ("outl (0x%X, 0x%04lX)\n",
  690. chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
  691. via_ac97_wait_idle (card);
  692. outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
  693. udelay (20);
  694. via_ac97_wait_idle (card);
  695. /* load no rate adaption, stereo 16bit, set up ring slots */
  696. if(card->sixchannel)
  697. {
  698. if(!chan->is_multi)
  699. {
  700. outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
  701. udelay (20);
  702. via_ac97_wait_idle (card);
  703. }
  704. }
  705. DPRINTK ("inl (0x%lX) = %x\n",
  706. chan->iobase + VIA_PCM_TABLE_ADDR,
  707. inl(chan->iobase + VIA_PCM_TABLE_ADDR));
  708. DPRINTK ("EXIT\n");
  709. return 0;
  710. err_out_nomem:
  711. printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
  712. via_chan_buffer_free (card, chan);
  713. DPRINTK ("EXIT\n");
  714. return -ENOMEM;
  715. }
  716. /**
  717. * via_chan_free - Release a PCM channel
  718. * @card: Private audio chip info
  719. * @chan: Channel to be released
  720. *
  721. * Performs all the functions necessary to clean up
  722. * an initialized channel.
  723. *
  724. * Currently these functions include disabled any
  725. * active DMA operations, setting the PCM channel
  726. * back to a known state, and releasing any allocated
  727. * sound buffers.
  728. */
  729. static void via_chan_free (struct via_info *card, struct via_channel *chan)
  730. {
  731. DPRINTK ("ENTER\n");
  732. spin_lock_irq (&card->lock);
  733. /* stop any existing channel output */
  734. via_chan_status_clear (chan->iobase);
  735. via_chan_stop (chan->iobase);
  736. via_chan_status_clear (chan->iobase);
  737. spin_unlock_irq (&card->lock);
  738. synchronize_irq(card->pdev->irq);
  739. DPRINTK ("EXIT\n");
  740. }
  741. static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
  742. {
  743. int i;
  744. DPRINTK ("ENTER\n");
  745. /* zero location of DMA-able scatter-gather info table */
  746. via_ac97_wait_idle(card);
  747. outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
  748. for (i = 0; i < chan->page_number; i++)
  749. if (chan->pgtbl[i].cpuaddr) {
  750. pci_free_consistent (card->pdev, PAGE_SIZE,
  751. chan->pgtbl[i].cpuaddr,
  752. chan->pgtbl[i].handle);
  753. chan->pgtbl[i].cpuaddr = NULL;
  754. chan->pgtbl[i].handle = 0;
  755. }
  756. chan->page_number = 0;
  757. if (chan->sgtable) {
  758. pci_free_consistent (card->pdev,
  759. (sizeof (struct via_sgd_table) * chan->frag_number),
  760. (void*)chan->sgtable, chan->sgt_handle);
  761. chan->sgtable = NULL;
  762. }
  763. DPRINTK ("EXIT\n");
  764. }
  765. /**
  766. * via_chan_pcm_fmt - Update PCM channel settings
  767. * @chan: Channel to be updated
  768. * @reset: Boolean. If non-zero, channel will be reset
  769. * to 8-bit mono mode.
  770. *
  771. * Stores the settings of the current PCM format,
  772. * 8-bit or 16-bit, and mono/stereo, into the
  773. * hardware settings for the specified channel.
  774. * If @reset is non-zero, the channel is reset
  775. * to 8-bit mono mode. Otherwise, the channel
  776. * is set to the values stored in the channel
  777. * information struct @chan.
  778. */
  779. static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
  780. {
  781. DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
  782. chan->pcm_fmt, reset ? "yes" : "no");
  783. assert (chan != NULL);
  784. if (reset)
  785. {
  786. /* reset to 8-bit mono mode */
  787. chan->pcm_fmt = 0;
  788. chan->channels = 1;
  789. }
  790. /* enable interrupts on FLAG and EOL */
  791. chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
  792. /* if we are recording, enable recording fifo bit */
  793. if (chan->is_record)
  794. chan->pcm_fmt |= VIA_PCM_REC_FIFO;
  795. /* set interrupt select bits where applicable (PCM in & out channels) */
  796. if (!chan->is_record)
  797. chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
  798. DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
  799. if(chan->intmask)
  800. {
  801. u32 m;
  802. /*
  803. * Channel 0x4 is up to 6 x 16bit and has to be
  804. * programmed differently
  805. */
  806. if(chan->is_multi)
  807. {
  808. u8 c = 0;
  809. /*
  810. * Load the type bit for num channels
  811. * and 8/16bit
  812. */
  813. if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
  814. c = 1 << 7;
  815. if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
  816. c |= (2<<4);
  817. else
  818. c |= (1<<4);
  819. outb(c, chan->iobase + VIA_PCM_TYPE);
  820. /*
  821. * Set the channel steering
  822. * Mono
  823. * Channel 0 to slot 3
  824. * Channel 0 to slot 4
  825. * Stereo
  826. * Channel 0 to slot 3
  827. * Channel 1 to slot 4
  828. */
  829. switch(chan->channels)
  830. {
  831. case 1:
  832. outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
  833. break;
  834. case 2:
  835. outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
  836. break;
  837. case 4:
  838. outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
  839. break;
  840. case 6:
  841. outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
  842. break;
  843. }
  844. }
  845. else
  846. {
  847. /*
  848. * New style, turn off channel volume
  849. * control, set bits in the right register
  850. */
  851. outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
  852. outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
  853. m = inl(chan->iobase + VIA_PCM_STOPRATE);
  854. m &= ~(3<<20);
  855. if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
  856. m |= (1 << 20);
  857. if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
  858. m |= (1 << 21);
  859. outl(m, chan->iobase + VIA_PCM_STOPRATE);
  860. }
  861. }
  862. else
  863. outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
  864. DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
  865. chan->pcm_fmt,
  866. inb (chan->iobase + VIA_PCM_TYPE));
  867. }
  868. /**
  869. * via_chan_clear - Stop DMA channel operation, and reset pointers
  870. * @card: the chip to accessed
  871. * @chan: Channel to be cleared
  872. *
  873. * Call via_chan_stop to halt DMA operations, and then resets
  874. * all software pointers which track DMA operation.
  875. */
  876. static void via_chan_clear (struct via_info *card, struct via_channel *chan)
  877. {
  878. DPRINTK ("ENTER\n");
  879. via_chan_stop (chan->iobase);
  880. via_chan_buffer_free(card, chan);
  881. chan->is_active = 0;
  882. chan->is_mapped = 0;
  883. chan->is_enabled = 1;
  884. chan->slop_len = 0;
  885. chan->sw_ptr = 0;
  886. chan->n_irqs = 0;
  887. atomic_set (&chan->hw_ptr, 0);
  888. DPRINTK ("EXIT\n");
  889. }
  890. /**
  891. * via_chan_set_speed - Set PCM sample rate for given channel
  892. * @card: Private info for specified board
  893. * @chan: Channel whose sample rate will be adjusted
  894. * @val: New sample rate, in Khz
  895. *
  896. * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
  897. * demand that all audio operations halt (if they are not already
  898. * halted) when the %SNDCTL_DSP_SPEED is given.
  899. *
  900. * This function halts all audio operations for the given channel
  901. * @chan, and then calls via_set_rate to set the audio hardware
  902. * to the new rate.
  903. */
  904. static int via_chan_set_speed (struct via_info *card,
  905. struct via_channel *chan, int val)
  906. {
  907. DPRINTK ("ENTER, requested rate = %d\n", val);
  908. via_chan_clear (card, chan);
  909. val = via_set_rate (card->ac97, chan, val);
  910. DPRINTK ("EXIT, returning %d\n", val);
  911. return val;
  912. }
  913. /**
  914. * via_chan_set_fmt - Set PCM sample size for given channel
  915. * @card: Private info for specified board
  916. * @chan: Channel whose sample size will be adjusted
  917. * @val: New sample size, use the %AFMT_xxx constants
  918. *
  919. * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
  920. * demand that all audio operations halt (if they are not already
  921. * halted) when the %SNDCTL_DSP_SETFMT is given.
  922. *
  923. * This function halts all audio operations for the given channel
  924. * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
  925. * to the new sample size, either 8-bit or 16-bit.
  926. */
  927. static int via_chan_set_fmt (struct via_info *card,
  928. struct via_channel *chan, int val)
  929. {
  930. DPRINTK ("ENTER, val=%s\n",
  931. val == AFMT_U8 ? "AFMT_U8" :
  932. val == AFMT_S16_LE ? "AFMT_S16_LE" :
  933. "unknown");
  934. via_chan_clear (card, chan);
  935. assert (val != AFMT_QUERY); /* this case is handled elsewhere */
  936. switch (val) {
  937. case AFMT_S16_LE:
  938. if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
  939. chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
  940. via_chan_pcm_fmt (chan, 0);
  941. }
  942. break;
  943. case AFMT_U8:
  944. if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
  945. chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
  946. via_chan_pcm_fmt (chan, 0);
  947. }
  948. break;
  949. default:
  950. DPRINTK ("unknown AFMT: 0x%X\n", val);
  951. val = AFMT_S16_LE;
  952. }
  953. DPRINTK ("EXIT\n");
  954. return val;
  955. }
  956. /**
  957. * via_chan_set_stereo - Enable or disable stereo for a DMA channel
  958. * @card: Private info for specified board
  959. * @chan: Channel whose stereo setting will be adjusted
  960. * @val: New sample size, use the %AFMT_xxx constants
  961. *
  962. * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
  963. * demand that all audio operations halt (if they are not already
  964. * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
  965. *
  966. * This function halts all audio operations for the given channel
  967. * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
  968. * to enable or disable stereo.
  969. */
  970. static int via_chan_set_stereo (struct via_info *card,
  971. struct via_channel *chan, int val)
  972. {
  973. DPRINTK ("ENTER, channels = %d\n", val);
  974. via_chan_clear (card, chan);
  975. switch (val) {
  976. /* mono */
  977. case 1:
  978. chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
  979. chan->channels = 1;
  980. via_chan_pcm_fmt (chan, 0);
  981. break;
  982. /* stereo */
  983. case 2:
  984. chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
  985. chan->channels = 2;
  986. via_chan_pcm_fmt (chan, 0);
  987. break;
  988. case 4:
  989. case 6:
  990. if(chan->is_multi)
  991. {
  992. chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
  993. chan->channels = val;
  994. break;
  995. }
  996. /* unknown */
  997. default:
  998. val = -EINVAL;
  999. break;
  1000. }
  1001. DPRINTK ("EXIT, returning %d\n", val);
  1002. return val;
  1003. }
  1004. static int via_chan_set_buffering (struct via_info *card,
  1005. struct via_channel *chan, int val)
  1006. {
  1007. int shift;
  1008. DPRINTK ("ENTER\n");
  1009. /* in both cases the buffer cannot be changed */
  1010. if (chan->is_active || chan->is_mapped) {
  1011. DPRINTK ("EXIT\n");
  1012. return -EINVAL;
  1013. }
  1014. /* called outside SETFRAGMENT */
  1015. /* set defaults or do nothing */
  1016. if (val < 0) {
  1017. if (chan->frag_size && chan->frag_number)
  1018. goto out;
  1019. DPRINTK ("\n");
  1020. chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
  1021. * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
  1022. shift = 0;
  1023. while (chan->frag_size) {
  1024. chan->frag_size >>= 1;
  1025. shift++;
  1026. }
  1027. chan->frag_size = 1 << shift;
  1028. chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
  1029. DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
  1030. } else {
  1031. chan->frag_size = 1 << (val & 0xFFFF);
  1032. chan->frag_number = (val >> 16) & 0xFFFF;
  1033. DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
  1034. }
  1035. /* quake3 wants frag_number to be a power of two */
  1036. shift = 0;
  1037. while (chan->frag_number) {
  1038. chan->frag_number >>= 1;
  1039. shift++;
  1040. }
  1041. chan->frag_number = 1 << shift;
  1042. if (chan->frag_size > VIA_MAX_FRAG_SIZE)
  1043. chan->frag_size = VIA_MAX_FRAG_SIZE;
  1044. else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
  1045. chan->frag_size = VIA_MIN_FRAG_SIZE;
  1046. if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
  1047. chan->frag_number = VIA_MIN_FRAG_NUMBER;
  1048. if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
  1049. chan->frag_number = VIA_MAX_FRAG_NUMBER;
  1050. if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
  1051. chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
  1052. out:
  1053. if (chan->is_record)
  1054. atomic_set (&chan->n_frags, 0);
  1055. else
  1056. atomic_set (&chan->n_frags, chan->frag_number);
  1057. DPRINTK ("EXIT\n");
  1058. return 0;
  1059. }
  1060. #ifdef VIA_CHAN_DUMP_BUFS
  1061. /**
  1062. * via_chan_dump_bufs - Display DMA table contents
  1063. * @chan: Channel whose DMA table will be displayed
  1064. *
  1065. * Debugging function which displays the contents of the
  1066. * scatter-gather DMA table for the given channel @chan.
  1067. */
  1068. static void via_chan_dump_bufs (struct via_channel *chan)
  1069. {
  1070. int i;
  1071. for (i = 0; i < chan->frag_number; i++) {
  1072. DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
  1073. i, chan->sgtable[i].addr,
  1074. chan->sgtable[i].count & 0x00FFFFFF,
  1075. chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
  1076. chan->sgtable[i].count & VIA_EOL ? 1 : 0);
  1077. }
  1078. DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
  1079. atomic_read (&chan->buf_in_use),
  1080. atomic_read (&chan->sw_ptr));
  1081. }
  1082. #endif /* VIA_CHAN_DUMP_BUFS */
  1083. /**
  1084. * via_chan_flush_frag - Flush partially-full playback buffer to hardware
  1085. * @chan: Channel whose DMA table will be flushed
  1086. *
  1087. * Flushes partially-full playback buffer to hardware.
  1088. */
  1089. static void via_chan_flush_frag (struct via_channel *chan)
  1090. {
  1091. DPRINTK ("ENTER\n");
  1092. assert (chan->slop_len > 0);
  1093. if (chan->sw_ptr == (chan->frag_number - 1))
  1094. chan->sw_ptr = 0;
  1095. else
  1096. chan->sw_ptr++;
  1097. chan->slop_len = 0;
  1098. assert (atomic_read (&chan->n_frags) > 0);
  1099. atomic_dec (&chan->n_frags);
  1100. DPRINTK ("EXIT\n");
  1101. }
  1102. /**
  1103. * via_chan_maybe_start - Initiate audio hardware DMA operation
  1104. * @chan: Channel whose DMA is to be started
  1105. *
  1106. * Initiate DMA operation, if the DMA engine for the given
  1107. * channel @chan is not already active.
  1108. */
  1109. static inline void via_chan_maybe_start (struct via_channel *chan)
  1110. {
  1111. assert (chan->is_active == sg_active(chan->iobase));
  1112. DPRINTK ("MAYBE START %s\n", chan->name);
  1113. if (!chan->is_active && chan->is_enabled) {
  1114. chan->is_active = 1;
  1115. sg_begin (chan);
  1116. DPRINTK ("starting channel %s\n", chan->name);
  1117. }
  1118. }
  1119. /****************************************************************
  1120. *
  1121. * Interface to ac97-codec module
  1122. *
  1123. *
  1124. */
  1125. /**
  1126. * via_ac97_wait_idle - Wait until AC97 codec is not busy
  1127. * @card: Private info for specified board
  1128. *
  1129. * Sleep until the AC97 codec is no longer busy.
  1130. * Returns the final value read from the SGD
  1131. * register being polled.
  1132. */
  1133. static u8 via_ac97_wait_idle (struct via_info *card)
  1134. {
  1135. u8 tmp8;
  1136. int counter = VIA_COUNTER_LIMIT;
  1137. DPRINTK ("ENTER/EXIT\n");
  1138. assert (card != NULL);
  1139. assert (card->pdev != NULL);
  1140. do {
  1141. udelay (15);
  1142. tmp8 = inb (card->baseaddr + 0x83);
  1143. } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
  1144. if (tmp8 & VIA_CR83_BUSY)
  1145. printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
  1146. return tmp8;
  1147. }
  1148. /**
  1149. * via_ac97_read_reg - Read AC97 standard register
  1150. * @codec: Pointer to generic AC97 codec info
  1151. * @reg: Index of AC97 register to be read
  1152. *
  1153. * Read the value of a single AC97 codec register,
  1154. * as defined by the Intel AC97 specification.
  1155. *
  1156. * Defines the standard AC97 read-register operation
  1157. * required by the kernel's ac97_codec interface.
  1158. *
  1159. * Returns the 16-bit value stored in the specified
  1160. * register.
  1161. */
  1162. static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
  1163. {
  1164. unsigned long data;
  1165. struct via_info *card;
  1166. int counter;
  1167. DPRINTK ("ENTER\n");
  1168. assert (codec != NULL);
  1169. assert (codec->private_data != NULL);
  1170. card = codec->private_data;
  1171. spin_lock(&card->ac97_lock);
  1172. /* Every time we write to register 80 we cause a transaction.
  1173. The only safe way to clear the valid bit is to write it at
  1174. the same time as the command */
  1175. data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
  1176. outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
  1177. udelay (20);
  1178. for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
  1179. udelay (1);
  1180. if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
  1181. (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
  1182. goto out;
  1183. }
  1184. printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
  1185. goto err_out;
  1186. out:
  1187. /* Once the valid bit has become set, we must wait a complete AC97
  1188. frame before the data has settled. */
  1189. udelay(25);
  1190. data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
  1191. outb (0x02, card->baseaddr + 0x83);
  1192. if (((data & 0x007F0000) >> 16) == reg) {
  1193. DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
  1194. data, data & 0x0000FFFF);
  1195. spin_unlock(&card->ac97_lock);
  1196. return data & 0x0000FFFF;
  1197. }
  1198. printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
  1199. reg, ((data & 0x007F0000) >> 16));
  1200. err_out:
  1201. spin_unlock(&card->ac97_lock);
  1202. DPRINTK ("EXIT, returning 0\n");
  1203. return 0;
  1204. }
  1205. /**
  1206. * via_ac97_write_reg - Write AC97 standard register
  1207. * @codec: Pointer to generic AC97 codec info
  1208. * @reg: Index of AC97 register to be written
  1209. * @value: Value to be written to AC97 register
  1210. *
  1211. * Write the value of a single AC97 codec register,
  1212. * as defined by the Intel AC97 specification.
  1213. *
  1214. * Defines the standard AC97 write-register operation
  1215. * required by the kernel's ac97_codec interface.
  1216. */
  1217. static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
  1218. {
  1219. u32 data;
  1220. struct via_info *card;
  1221. int counter;
  1222. DPRINTK ("ENTER\n");
  1223. assert (codec != NULL);
  1224. assert (codec->private_data != NULL);
  1225. card = codec->private_data;
  1226. spin_lock(&card->ac97_lock);
  1227. data = (reg << 16) + value;
  1228. outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
  1229. udelay (10);
  1230. for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
  1231. if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
  1232. goto out;
  1233. udelay (15);
  1234. }
  1235. printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
  1236. out:
  1237. spin_unlock(&card->ac97_lock);
  1238. DPRINTK ("EXIT\n");
  1239. }
  1240. static int via_mixer_open (struct inode *inode, struct file *file)
  1241. {
  1242. int minor = iminor(inode);
  1243. struct via_info *card;
  1244. struct pci_dev *pdev = NULL;
  1245. struct pci_driver *drvr;
  1246. DPRINTK ("ENTER\n");
  1247. while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
  1248. drvr = pci_dev_driver (pdev);
  1249. if (drvr == &via_driver) {
  1250. assert (pci_get_drvdata (pdev) != NULL);
  1251. card = pci_get_drvdata (pdev);
  1252. if (card->ac97->dev_mixer == minor)
  1253. goto match;
  1254. }
  1255. }
  1256. DPRINTK ("EXIT, returning -ENODEV\n");
  1257. return -ENODEV;
  1258. match:
  1259. file->private_data = card->ac97;
  1260. DPRINTK ("EXIT, returning 0\n");
  1261. return nonseekable_open(inode, file);
  1262. }
  1263. static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
  1264. unsigned long arg)
  1265. {
  1266. struct ac97_codec *codec = file->private_data;
  1267. struct via_info *card;
  1268. int nonblock = (file->f_flags & O_NONBLOCK);
  1269. int rc;
  1270. DPRINTK ("ENTER\n");
  1271. assert (codec != NULL);
  1272. card = codec->private_data;
  1273. assert (card != NULL);
  1274. rc = via_syscall_down (card, nonblock);
  1275. if (rc) goto out;
  1276. #if 0
  1277. /*
  1278. * Intercept volume control on 8233 and 8235
  1279. */
  1280. if(card->volume)
  1281. {
  1282. switch(cmd)
  1283. {
  1284. case SOUND_MIXER_READ_VOLUME:
  1285. return card->mixer_vol;
  1286. case SOUND_MIXER_WRITE_VOLUME:
  1287. {
  1288. int v;
  1289. if(get_user(v, (int *)arg))
  1290. {
  1291. rc = -EFAULT;
  1292. goto out;
  1293. }
  1294. card->mixer_vol = v;
  1295. }
  1296. }
  1297. }
  1298. #endif
  1299. rc = codec->mixer_ioctl(codec, cmd, arg);
  1300. mutex_unlock(&card->syscall_mutex);
  1301. out:
  1302. DPRINTK ("EXIT, returning %d\n", rc);
  1303. return rc;
  1304. }
  1305. static struct file_operations via_mixer_fops = {
  1306. .owner = THIS_MODULE,
  1307. .open = via_mixer_open,
  1308. .llseek = no_llseek,
  1309. .ioctl = via_mixer_ioctl,
  1310. };
  1311. static int __devinit via_ac97_reset (struct via_info *card)
  1312. {
  1313. struct pci_dev *pdev = card->pdev;
  1314. u8 tmp8;
  1315. u16 tmp16;
  1316. DPRINTK ("ENTER\n");
  1317. assert (pdev != NULL);
  1318. #ifndef NDEBUG
  1319. {
  1320. u8 r40,r41,r42,r43,r44,r48;
  1321. pci_read_config_byte (card->pdev, 0x40, &r40);
  1322. pci_read_config_byte (card->pdev, 0x41, &r41);
  1323. pci_read_config_byte (card->pdev, 0x42, &r42);
  1324. pci_read_config_byte (card->pdev, 0x43, &r43);
  1325. pci_read_config_byte (card->pdev, 0x44, &r44);
  1326. pci_read_config_byte (card->pdev, 0x48, &r48);
  1327. DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
  1328. r40,r41,r42,r43,r44,r48);
  1329. spin_lock_irq (&card->lock);
  1330. DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
  1331. inb (card->baseaddr + 0x00),
  1332. inb (card->baseaddr + 0x01),
  1333. inb (card->baseaddr + 0x02),
  1334. inl (card->baseaddr + 0x04),
  1335. inl (card->baseaddr + 0x0C),
  1336. inl (card->baseaddr + 0x80),
  1337. inl (card->baseaddr + 0x84));
  1338. spin_unlock_irq (&card->lock);
  1339. }
  1340. #endif
  1341. /*
  1342. * Reset AC97 controller: enable, disable, enable,
  1343. * pausing after each command for good luck. Only
  1344. * do this if the codec is not ready, because it causes
  1345. * loud pops and such due to such a hard codec reset.
  1346. */
  1347. pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8);
  1348. if ((tmp8 & VIA_CR40_AC97_READY) == 0) {
  1349. pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
  1350. VIA_CR41_AC97_ENABLE |
  1351. VIA_CR41_AC97_RESET |
  1352. VIA_CR41_AC97_WAKEUP);
  1353. udelay (100);
  1354. pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
  1355. udelay (100);
  1356. pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
  1357. VIA_CR41_AC97_ENABLE |
  1358. VIA_CR41_PCM_ENABLE |
  1359. VIA_CR41_VRA | VIA_CR41_AC97_RESET);
  1360. udelay (100);
  1361. }
  1362. /* Make sure VRA is enabled, in case we didn't do a
  1363. * complete codec reset, above
  1364. */
  1365. pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
  1366. if (((tmp8 & VIA_CR41_VRA) == 0) ||
  1367. ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) ||
  1368. ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) ||
  1369. ((tmp8 & VIA_CR41_AC97_RESET) == 0)) {
  1370. pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
  1371. VIA_CR41_AC97_ENABLE |
  1372. VIA_CR41_PCM_ENABLE |
  1373. VIA_CR41_VRA | VIA_CR41_AC97_RESET);
  1374. udelay (100);
  1375. }
  1376. if(card->legacy)
  1377. {
  1378. #if 0 /* this breaks on K7M */
  1379. /* disable legacy stuff */
  1380. pci_write_config_byte (pdev, 0x42, 0x00);
  1381. udelay(10);
  1382. #endif
  1383. /* route FM trap to IRQ, disable FM trap */
  1384. pci_write_config_byte (pdev, 0x48, 0x05);
  1385. udelay(10);
  1386. }
  1387. /* disable all codec GPI interrupts */
  1388. outl (0, pci_resource_start (pdev, 0) + 0x8C);
  1389. /* WARNING: this line is magic. Remove this
  1390. * and things break. */
  1391. /* enable variable rate */
  1392. tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
  1393. if ((tmp16 & 1) == 0)
  1394. via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
  1395. DPRINTK ("EXIT, returning 0\n");
  1396. return 0;
  1397. }
  1398. static void via_ac97_codec_wait (struct ac97_codec *codec)
  1399. {
  1400. assert (codec->private_data != NULL);
  1401. via_ac97_wait_idle (codec->private_data);
  1402. }
  1403. static int __devinit via_ac97_init (struct via_info *card)
  1404. {
  1405. int rc;
  1406. u16 tmp16;
  1407. DPRINTK ("ENTER\n");
  1408. assert (card != NULL);
  1409. card->ac97 = ac97_alloc_codec();
  1410. if(card->ac97 == NULL)
  1411. return -ENOMEM;
  1412. card->ac97->private_data = card;
  1413. card->ac97->codec_read = via_ac97_read_reg;
  1414. card->ac97->codec_write = via_ac97_write_reg;
  1415. card->ac97->codec_wait = via_ac97_codec_wait;
  1416. card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
  1417. if (card->ac97->dev_mixer < 0) {
  1418. printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
  1419. DPRINTK ("EXIT, returning -EIO\n");
  1420. ac97_release_codec(card->ac97);
  1421. return -EIO;
  1422. }
  1423. rc = via_ac97_reset (card);
  1424. if (rc) {
  1425. printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
  1426. goto err_out;
  1427. }
  1428. mdelay(10);
  1429. if (ac97_probe_codec (card->ac97) == 0) {
  1430. printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
  1431. rc = -EIO;
  1432. goto err_out;
  1433. }
  1434. /* enable variable rate */
  1435. tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
  1436. via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
  1437. /*
  1438. * If we cannot enable VRA, we have a locked-rate codec.
  1439. * We try again to enable VRA before assuming so, however.
  1440. */
  1441. tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
  1442. if ((tmp16 & 1) == 0) {
  1443. via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
  1444. tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
  1445. if ((tmp16 & 1) == 0) {
  1446. card->locked_rate = 1;
  1447. printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
  1448. }
  1449. }
  1450. DPRINTK ("EXIT, returning 0\n");
  1451. return 0;
  1452. err_out:
  1453. unregister_sound_mixer (card->ac97->dev_mixer);
  1454. DPRINTK ("EXIT, returning %d\n", rc);
  1455. ac97_release_codec(card->ac97);
  1456. return rc;
  1457. }
  1458. static void via_ac97_cleanup (struct via_info *card)
  1459. {
  1460. DPRINTK ("ENTER\n");
  1461. assert (card != NULL);
  1462. assert (card->ac97->dev_mixer >= 0);
  1463. unregister_sound_mixer (card->ac97->dev_mixer);
  1464. ac97_release_codec(card->ac97);
  1465. DPRINTK ("EXIT\n");
  1466. }
  1467. /****************************************************************
  1468. *
  1469. * Interrupt-related code
  1470. *
  1471. */
  1472. /**
  1473. * via_intr_channel - handle an interrupt for a single channel
  1474. * @card: unused
  1475. * @chan: handle interrupt for this channel
  1476. *
  1477. * This is the "meat" of the interrupt handler,
  1478. * containing the actions taken each time an interrupt
  1479. * occurs. All communication and coordination with
  1480. * userspace takes place here.
  1481. *
  1482. * Locking: inside card->lock
  1483. */
  1484. static void via_intr_channel (struct via_info *card, struct via_channel *chan)
  1485. {
  1486. u8 status;
  1487. int n;
  1488. /* check pertinent bits of status register for action bits */
  1489. status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
  1490. if (!status)
  1491. return;
  1492. /* acknowledge any flagged bits ASAP */
  1493. outb (status, chan->iobase);
  1494. if (!chan->sgtable) /* XXX: temporary solution */
  1495. return;
  1496. /* grab current h/w ptr value */
  1497. n = atomic_read (&chan->hw_ptr);
  1498. /* sanity check: make sure our h/w ptr doesn't have a weird value */
  1499. assert (n >= 0);
  1500. assert (n < chan->frag_number);
  1501. /* reset SGD data structure in memory to reflect a full buffer,
  1502. * and advance the h/w ptr, wrapping around to zero if needed
  1503. */
  1504. if (n == (chan->frag_number - 1)) {
  1505. chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL);
  1506. atomic_set (&chan->hw_ptr, 0);
  1507. } else {
  1508. chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG);
  1509. atomic_inc (&chan->hw_ptr);
  1510. }
  1511. /* accounting crap for SNDCTL_DSP_GETxPTR */
  1512. chan->n_irqs++;
  1513. chan->bytes += chan->frag_size;
  1514. /* FIXME - signed overflow is undefined */
  1515. if (chan->bytes < 0) /* handle overflow of 31-bit value */
  1516. chan->bytes = chan->frag_size;
  1517. /* all following checks only occur when not in mmap(2) mode */
  1518. if (!chan->is_mapped)
  1519. {
  1520. /* If we are recording, then n_frags represents the number
  1521. * of fragments waiting to be handled by userspace.
  1522. * If we are playback, then n_frags represents the number
  1523. * of fragments remaining to be filled by userspace.
  1524. * We increment here. If we reach max number of fragments,
  1525. * this indicates an underrun/overrun. For this case under OSS,
  1526. * we stop the record/playback process.
  1527. */
  1528. if (atomic_read (&chan->n_frags) < chan->frag_number)
  1529. atomic_inc (&chan->n_frags);
  1530. assert (atomic_read (&chan->n_frags) <= chan->frag_number);
  1531. if (atomic_read (&chan->n_frags) == chan->frag_number) {
  1532. chan->is_active = 0;
  1533. via_chan_stop (chan->iobase);
  1534. }
  1535. }
  1536. /* wake up anyone listening to see when interrupts occur */
  1537. wake_up_all (&chan->wait);
  1538. DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
  1539. chan->name, status, (long) inl (chan->iobase + 0x04),
  1540. atomic_read (&chan->hw_ptr));
  1541. DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name,
  1542. atomic_read (&chan->n_frags), missed);
  1543. }
  1544. static irqreturn_t via_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1545. {
  1546. struct via_info *card = dev_id;
  1547. u32 status32;
  1548. /* to minimize interrupt sharing costs, we use the SGD status
  1549. * shadow register to check the status of all inputs and
  1550. * outputs with a single 32-bit bus read. If no interrupt
  1551. * conditions are flagged, we exit immediately
  1552. */
  1553. status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
  1554. if (!(status32 & VIA_INTR_MASK))
  1555. {
  1556. #ifdef CONFIG_MIDI_VIA82CXXX
  1557. if (card->midi_devc)
  1558. uart401intr(irq, card->midi_devc, regs);
  1559. #endif
  1560. return IRQ_HANDLED;
  1561. }
  1562. DPRINTK ("intr, status32 == 0x%08X\n", status32);
  1563. /* synchronize interrupt handling under SMP. this spinlock
  1564. * goes away completely on UP
  1565. */
  1566. spin_lock (&card->lock);
  1567. if (status32 & VIA_INTR_OUT)
  1568. via_intr_channel (card, &card->ch_out);
  1569. if (status32 & VIA_INTR_IN)
  1570. via_intr_channel (card, &card->ch_in);
  1571. if (status32 & VIA_INTR_FM)
  1572. via_intr_channel (card, &card->ch_fm);
  1573. spin_unlock (&card->lock);
  1574. return IRQ_HANDLED;
  1575. }
  1576. static irqreturn_t via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1577. {
  1578. struct via_info *card = dev_id;
  1579. u32 status32;
  1580. /* to minimize interrupt sharing costs, we use the SGD status
  1581. * shadow register to check the status of all inputs and
  1582. * outputs with a single 32-bit bus read. If no interrupt
  1583. * conditions are flagged, we exit immediately
  1584. */
  1585. status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
  1586. if (!(status32 & VIA_NEW_INTR_MASK))
  1587. return IRQ_NONE;
  1588. /*
  1589. * goes away completely on UP
  1590. */
  1591. spin_lock (&card->lock);
  1592. via_intr_channel (card, &card->ch_out);
  1593. via_intr_channel (card, &card->ch_in);
  1594. via_intr_channel (card, &card->ch_fm);
  1595. spin_unlock (&card->lock);
  1596. return IRQ_HANDLED;
  1597. }
  1598. /**
  1599. * via_interrupt_init - Initialize interrupt handling
  1600. * @card: Private info for specified board
  1601. *
  1602. * Obtain and reserve IRQ for using in handling audio events.
  1603. * Also, disable any IRQ-generating resources, to make sure
  1604. * we don't get interrupts before we want them.
  1605. */
  1606. static int via_interrupt_init (struct via_info *card)
  1607. {
  1608. u8 tmp8;
  1609. DPRINTK ("ENTER\n");
  1610. assert (card != NULL);
  1611. assert (card->pdev != NULL);
  1612. /* check for sane IRQ number. can this ever happen? */
  1613. if (card->pdev->irq < 2) {
  1614. printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
  1615. card->pdev->irq);
  1616. DPRINTK ("EXIT, returning -EIO\n");
  1617. return -EIO;
  1618. }
  1619. /* VIA requires this is done */
  1620. pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
  1621. if(card->legacy)
  1622. {
  1623. /* make sure FM irq is not routed to us */
  1624. pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
  1625. if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) {
  1626. tmp8 |= VIA_CR48_FM_TRAP_TO_NMI;
  1627. pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8);
  1628. }
  1629. if (request_irq (card->pdev->irq, via_interrupt, IRQF_SHARED, VIA_MODULE_NAME, card)) {
  1630. printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
  1631. card->pdev->irq);
  1632. DPRINTK ("EXIT, returning -EBUSY\n");
  1633. return -EBUSY;
  1634. }
  1635. }
  1636. else
  1637. {
  1638. if (request_irq (card->pdev->irq, via_new_interrupt, IRQF_SHARED, VIA_MODULE_NAME, card)) {
  1639. printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
  1640. card->pdev->irq);
  1641. DPRINTK ("EXIT, returning -EBUSY\n");
  1642. return -EBUSY;
  1643. }
  1644. }
  1645. DPRINTK ("EXIT, returning 0\n");
  1646. return 0;
  1647. }
  1648. /****************************************************************
  1649. *
  1650. * OSS DSP device
  1651. *
  1652. */
  1653. static struct file_operations via_dsp_fops = {
  1654. .owner = THIS_MODULE,
  1655. .open = via_dsp_open,
  1656. .release = via_dsp_release,
  1657. .read = via_dsp_read,
  1658. .write = via_dsp_write,
  1659. .poll = via_dsp_poll,
  1660. .llseek = no_llseek,
  1661. .ioctl = via_dsp_ioctl,
  1662. .mmap = via_dsp_mmap,
  1663. };
  1664. static int __devinit via_dsp_init (struct via_info *card)
  1665. {
  1666. u8 tmp8;
  1667. DPRINTK ("ENTER\n");
  1668. assert (card != NULL);
  1669. if(card->legacy)
  1670. {
  1671. /* turn off legacy features, if not already */
  1672. pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
  1673. if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE)) {
  1674. tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE);
  1675. pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
  1676. }
  1677. }
  1678. via_stop_everything (card);
  1679. card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1);
  1680. if (card->dev_dsp < 0) {
  1681. DPRINTK ("EXIT, returning -ENODEV\n");
  1682. return -ENODEV;
  1683. }
  1684. DPRINTK ("EXIT, returning 0\n");
  1685. return 0;
  1686. }
  1687. static void via_dsp_cleanup (struct via_info *card)
  1688. {
  1689. DPRINTK ("ENTER\n");
  1690. assert (card != NULL);
  1691. assert (card->dev_dsp >= 0);
  1692. via_stop_everything (card);
  1693. unregister_sound_dsp (card->dev_dsp);
  1694. DPRINTK ("EXIT\n");
  1695. }
  1696. static struct page * via_mm_nopage (struct vm_area_struct * vma,
  1697. unsigned long address, int *type)
  1698. {
  1699. struct via_info *card = vma->vm_private_data;
  1700. struct via_channel *chan = &card->ch_out;
  1701. struct page *dmapage;
  1702. unsigned long pgoff;
  1703. int rd, wr;
  1704. DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
  1705. vma->vm_start,
  1706. address - vma->vm_start,
  1707. (address - vma->vm_start) >> PAGE_SHIFT,
  1708. address);
  1709. if (address > vma->vm_end) {
  1710. DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
  1711. return NOPAGE_SIGBUS; /* Disallow mremap */
  1712. }
  1713. if (!card) {
  1714. DPRINTK ("EXIT, returning NOPAGE_OOM\n");
  1715. return NOPAGE_OOM; /* Nothing allocated */
  1716. }
  1717. pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
  1718. rd = card->ch_in.is_mapped;
  1719. wr = card->ch_out.is_mapped;
  1720. #ifndef VIA_NDEBUG
  1721. {
  1722. unsigned long max_bufs = chan->frag_number;
  1723. if (rd && wr) max_bufs *= 2;
  1724. /* via_dsp_mmap() should ensure this */
  1725. assert (pgoff < max_bufs);
  1726. }
  1727. #endif
  1728. /* if full-duplex (read+write) and we have two sets of bufs,
  1729. * then the playback buffers come first, sez soundcard.c */
  1730. if (pgoff >= chan->page_number) {
  1731. pgoff -= chan->page_number;
  1732. chan = &card->ch_in;
  1733. } else if (!wr)
  1734. chan = &card->ch_in;
  1735. assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0);
  1736. dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr);
  1737. DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
  1738. dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr);
  1739. get_page (dmapage);
  1740. if (type)
  1741. *type = VM_FAULT_MINOR;
  1742. return dmapage;
  1743. }
  1744. #ifndef VM_RESERVED
  1745. static int via_mm_swapout (struct page *page, struct file *filp)
  1746. {
  1747. return 0;
  1748. }
  1749. #endif /* VM_RESERVED */
  1750. static struct vm_operations_struct via_mm_ops = {
  1751. .nopage = via_mm_nopage,
  1752. #ifndef VM_RESERVED
  1753. .swapout = via_mm_swapout,
  1754. #endif
  1755. };
  1756. static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
  1757. {
  1758. struct via_info *card;
  1759. int nonblock = (file->f_flags & O_NONBLOCK);
  1760. int rc = -EINVAL, rd=0, wr=0;
  1761. unsigned long max_size, size, start, offset;
  1762. assert (file != NULL);
  1763. assert (vma != NULL);
  1764. card = file->private_data;
  1765. assert (card != NULL);
  1766. DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
  1767. vma->vm_start,
  1768. vma->vm_end - vma->vm_start,
  1769. vma->vm_pgoff);
  1770. max_size = 0;
  1771. if (vma->vm_flags & VM_READ) {
  1772. rd = 1;
  1773. via_chan_set_buffering(card, &card->ch_in, -1);
  1774. via_chan_buffer_init (card, &card->ch_in);
  1775. max_size += card->ch_in.page_number << PAGE_SHIFT;
  1776. }
  1777. if (vma->vm_flags & VM_WRITE) {
  1778. wr = 1;
  1779. via_chan_set_buffering(card, &card->ch_out, -1);
  1780. via_chan_buffer_init (card, &card->ch_out);
  1781. max_size += card->ch_out.page_number << PAGE_SHIFT;
  1782. }
  1783. start = vma->vm_start;
  1784. offset = (vma->vm_pgoff << PAGE_SHIFT);
  1785. size = vma->vm_end - vma->vm_start;
  1786. /* some basic size/offset sanity checks */
  1787. if (size > max_size)
  1788. goto out;
  1789. if (offset > max_size - size)
  1790. goto out;
  1791. rc = via_syscall_down (card, nonblock);
  1792. if (rc) goto out;
  1793. vma->vm_ops = &via_mm_ops;
  1794. vma->vm_private_data = card;
  1795. #ifdef VM_RESERVED
  1796. vma->vm_flags |= VM_RESERVED;
  1797. #endif
  1798. if (rd)
  1799. card->ch_in.is_mapped = 1;
  1800. if (wr)
  1801. card->ch_out.is_mapped = 1;
  1802. mutex_unlock(&card->syscall_mutex);
  1803. rc = 0;
  1804. out:
  1805. DPRINTK ("EXIT, returning %d\n", rc);
  1806. return rc;
  1807. }
  1808. static ssize_t via_dsp_do_read (struct via_info *card,
  1809. char __user *userbuf, size_t count,
  1810. int nonblock)
  1811. {
  1812. DECLARE_WAITQUEUE(wait, current);
  1813. const char __user *orig_userbuf = userbuf;
  1814. struct via_channel *chan = &card->ch_in;
  1815. size_t size;
  1816. int n, tmp;
  1817. ssize_t ret = 0;
  1818. /* if SGD has not yet been started, start it */
  1819. via_chan_maybe_start (chan);
  1820. handle_one_block:
  1821. /* just to be a nice neighbor */
  1822. /* Thomas Sailer:
  1823. * But also to ourselves, release semaphore if we do so */
  1824. if (need_resched()) {
  1825. mutex_unlock(&card->syscall_mutex);
  1826. schedule ();
  1827. ret = via_syscall_down (card, nonblock);
  1828. if (ret)
  1829. goto out;
  1830. }
  1831. /* grab current channel software pointer. In the case of
  1832. * recording, this is pointing to the next buffer that
  1833. * will receive data from the audio hardware.
  1834. */
  1835. n = chan->sw_ptr;
  1836. /* n_frags represents the number of fragments waiting
  1837. * to be copied to userland. sleep until at least
  1838. * one buffer has been read from the audio hardware.
  1839. */
  1840. add_wait_queue(&chan->wait, &wait);
  1841. for (;;) {
  1842. __set_current_state(TASK_INTERRUPTIBLE);
  1843. tmp = atomic_read (&chan->n_frags);
  1844. assert (tmp >= 0);
  1845. assert (tmp <= chan->frag_number);
  1846. if (tmp)
  1847. break;
  1848. if (nonblock || !chan->is_active) {
  1849. ret = -EAGAIN;
  1850. break;
  1851. }
  1852. mutex_unlock(&card->syscall_mutex);
  1853. DPRINTK ("Sleeping on block %d\n", n);
  1854. schedule();
  1855. ret = via_syscall_down (card, nonblock);
  1856. if (ret)
  1857. break;
  1858. if (signal_pending (current)) {
  1859. ret = -ERESTARTSYS;
  1860. break;
  1861. }
  1862. }
  1863. set_current_state(TASK_RUNNING);
  1864. remove_wait_queue(&chan->wait, &wait);
  1865. if (ret)
  1866. goto out;
  1867. /* Now that we have a buffer we can read from, send
  1868. * as much as sample data possible to userspace.
  1869. */
  1870. while ((count > 0) && (chan->slop_len < chan->frag_size)) {
  1871. size_t slop_left = chan->frag_size - chan->slop_len;
  1872. void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr;
  1873. unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
  1874. size = (count < slop_left) ? count : slop_left;
  1875. if (copy_to_user (userbuf,
  1876. base + ofs + chan->slop_len,
  1877. size)) {
  1878. ret = -EFAULT;
  1879. goto out;
  1880. }
  1881. count -= size;
  1882. chan->slop_len += size;
  1883. userbuf += size;
  1884. }
  1885. /* If we didn't copy the buffer completely to userspace,
  1886. * stop now.
  1887. */
  1888. if (chan->slop_len < chan->frag_size)
  1889. goto out;
  1890. /*
  1891. * If we get to this point, we copied one buffer completely
  1892. * to userspace, give the buffer back to the hardware.
  1893. */
  1894. /* advance channel software pointer to point to
  1895. * the next buffer from which we will copy
  1896. */
  1897. if (chan->sw_ptr == (chan->frag_number - 1))
  1898. chan->sw_ptr = 0;
  1899. else
  1900. chan->sw_ptr++;
  1901. /* mark one less buffer waiting to be processed */
  1902. assert (atomic_read (&chan->n_frags) > 0);
  1903. atomic_dec (&chan->n_frags);
  1904. /* we are at a block boundary, there is no fragment data */
  1905. chan->slop_len = 0;
  1906. DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
  1907. n, chan->sw_ptr, atomic_read (&chan->n_frags));
  1908. DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
  1909. inb (card->baseaddr + 0x00),
  1910. inb (card->baseaddr + 0x01),
  1911. inb (card->baseaddr + 0x02),
  1912. inl (card->baseaddr + 0x04),
  1913. inl (card->baseaddr + 0x0C),
  1914. inl (card->baseaddr + 0x80),
  1915. inl (card->baseaddr + 0x84));
  1916. if (count > 0)
  1917. goto handle_one_block;
  1918. out:
  1919. return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret;
  1920. }
  1921. static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  1922. {
  1923. struct via_info *card;
  1924. int nonblock = (file->f_flags & O_NONBLOCK);
  1925. int rc;
  1926. DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
  1927. file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
  1928. assert (file != NULL);
  1929. card = file->private_data;
  1930. assert (card != NULL);
  1931. rc = via_syscall_down (card, nonblock);
  1932. if (rc) goto out;
  1933. if (card->ch_in.is_mapped) {
  1934. rc = -ENXIO;
  1935. goto out_up;
  1936. }
  1937. via_chan_set_buffering(card, &card->ch_in, -1);
  1938. rc = via_chan_buffer_init (card, &card->ch_in);
  1939. if (rc)
  1940. goto out_up;
  1941. rc = via_dsp_do_read (card, buffer, count, nonblock);
  1942. out_up:
  1943. mutex_unlock(&card->syscall_mutex);
  1944. out:
  1945. DPRINTK ("EXIT, returning %ld\n",(long) rc);
  1946. return rc;
  1947. }
  1948. static ssize_t via_dsp_do_write (struct via_info *card,
  1949. const char __user *userbuf, size_t count,
  1950. int nonblock)
  1951. {
  1952. DECLARE_WAITQUEUE(wait, current);
  1953. const char __user *orig_userbuf = userbuf;
  1954. struct via_channel *chan = &card->ch_out;
  1955. volatile struct via_sgd_table *sgtable = chan->sgtable;
  1956. size_t size;
  1957. int n, tmp;
  1958. ssize_t ret = 0;
  1959. handle_one_block:
  1960. /* just to be a nice neighbor */
  1961. /* Thomas Sailer:
  1962. * But also to ourselves, release semaphore if we do so */
  1963. if (need_resched()) {
  1964. mutex_unlock(&card->syscall_mutex);
  1965. schedule ();
  1966. ret = via_syscall_down (card, nonblock);
  1967. if (ret)
  1968. goto out;
  1969. }
  1970. /* grab current channel fragment pointer. In the case of
  1971. * playback, this is pointing to the next fragment that
  1972. * should receive data from userland.
  1973. */
  1974. n = chan->sw_ptr;
  1975. /* n_frags represents the number of fragments remaining
  1976. * to be filled by userspace. Sleep until
  1977. * at least one fragment is available for our use.
  1978. */
  1979. add_wait_queue(&chan->wait, &wait);
  1980. for (;;) {
  1981. __set_current_state(TASK_INTERRUPTIBLE);
  1982. tmp = atomic_read (&chan->n_frags);
  1983. assert (tmp >= 0);
  1984. assert (tmp <= chan->frag_number);
  1985. if (tmp)
  1986. break;
  1987. if (nonblock || !chan->is_active) {
  1988. ret = -EAGAIN;
  1989. break;
  1990. }
  1991. mutex_unlock(&card->syscall_mutex);
  1992. DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
  1993. schedule();
  1994. ret = via_syscall_down (card, nonblock);
  1995. if (ret)
  1996. break;
  1997. if (signal_pending (current)) {
  1998. ret = -ERESTARTSYS;
  1999. break;
  2000. }
  2001. }
  2002. set_current_state(TASK_RUNNING);
  2003. remove_wait_queue(&chan->wait, &wait);
  2004. if (ret)
  2005. goto out;
  2006. /* Now that we have at least one fragment we can write to, fill the buffer
  2007. * as much as possible with data from userspace.
  2008. */
  2009. while ((count > 0) && (chan->slop_len < chan->frag_size)) {
  2010. size_t slop_left = chan->frag_size - chan->slop_len;
  2011. size = (count < slop_left) ? count : slop_left;
  2012. if (copy_from_user (chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr + (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size + chan->slop_len,
  2013. userbuf, size)) {
  2014. ret = -EFAULT;
  2015. goto out;
  2016. }
  2017. count -= size;
  2018. chan->slop_len += size;
  2019. userbuf += size;
  2020. }
  2021. /* If we didn't fill up the buffer with data, stop now.
  2022. * Put a 'stop' marker in the DMA table too, to tell the
  2023. * audio hardware to stop if it gets here.
  2024. */
  2025. if (chan->slop_len < chan->frag_size) {
  2026. sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
  2027. goto out;
  2028. }
  2029. /*
  2030. * If we get to this point, we have filled a buffer with
  2031. * audio data, flush the buffer to audio hardware.
  2032. */
  2033. /* Record the true size for the audio hardware to notice */
  2034. if (n == (chan->frag_number - 1))
  2035. sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
  2036. else
  2037. sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
  2038. /* advance channel software pointer to point to
  2039. * the next buffer we will fill with data
  2040. */
  2041. if (chan->sw_ptr == (chan->frag_number - 1))
  2042. chan->sw_ptr = 0;
  2043. else
  2044. chan->sw_ptr++;
  2045. /* mark one less buffer as being available for userspace consumption */
  2046. assert (atomic_read (&chan->n_frags) > 0);
  2047. atomic_dec (&chan->n_frags);
  2048. /* we are at a block boundary, there is no fragment data */
  2049. chan->slop_len = 0;
  2050. /* if SGD has not yet been started, start it */
  2051. via_chan_maybe_start (chan);
  2052. DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
  2053. n, chan->sw_ptr, atomic_read (&chan->n_frags));
  2054. DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
  2055. inb (card->baseaddr + 0x00),
  2056. inb (card->baseaddr + 0x01),
  2057. inb (card->baseaddr + 0x02),
  2058. inl (card->baseaddr + 0x04),
  2059. inl (card->baseaddr + 0x08),
  2060. inl (card->baseaddr + 0x0C),
  2061. inl (card->baseaddr + 0x80),
  2062. inl (card->baseaddr + 0x84));
  2063. if (count > 0)
  2064. goto handle_one_block;
  2065. out:
  2066. if (userbuf - orig_userbuf)
  2067. return userbuf - orig_userbuf;
  2068. else
  2069. return ret;
  2070. }
  2071. static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  2072. {
  2073. struct via_info *card;
  2074. ssize_t rc;
  2075. int nonblock = (file->f_flags & O_NONBLOCK);
  2076. DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
  2077. file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
  2078. assert (file != NULL);
  2079. card = file->private_data;
  2080. assert (card != NULL);
  2081. rc = via_syscall_down (card, nonblock);
  2082. if (rc) goto out;
  2083. if (card->ch_out.is_mapped) {
  2084. rc = -ENXIO;
  2085. goto out_up;
  2086. }
  2087. via_chan_set_buffering(card, &card->ch_out, -1);
  2088. rc = via_chan_buffer_init (card, &card->ch_out);
  2089. if (rc)
  2090. goto out_up;
  2091. rc = via_dsp_do_write (card, buffer, count, nonblock);
  2092. out_up:
  2093. mutex_unlock(&card->syscall_mutex);
  2094. out:
  2095. DPRINTK ("EXIT, returning %ld\n",(long) rc);
  2096. return rc;
  2097. }
  2098. static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
  2099. {
  2100. struct via_info *card;
  2101. struct via_channel *chan;
  2102. unsigned int mask = 0;
  2103. DPRINTK ("ENTER\n");
  2104. assert (file != NULL);
  2105. card = file->private_data;
  2106. assert (card != NULL);
  2107. if (file->f_mode & FMODE_READ) {
  2108. chan = &card->ch_in;
  2109. if (sg_active (chan->iobase))
  2110. poll_wait(file, &chan->wait, wait);
  2111. if (atomic_read (&chan->n_frags) > 0)
  2112. mask |= POLLIN | POLLRDNORM;
  2113. }
  2114. if (file->f_mode & FMODE_WRITE) {
  2115. chan = &card->ch_out;
  2116. if (sg_active (chan->iobase))
  2117. poll_wait(file, &chan->wait, wait);
  2118. if (atomic_read (&chan->n_frags) > 0)
  2119. mask |= POLLOUT | POLLWRNORM;
  2120. }
  2121. DPRINTK ("EXIT, returning %u\n", mask);
  2122. return mask;
  2123. }
  2124. /**
  2125. * via_dsp_drain_playback - sleep until all playback samples are flushed
  2126. * @card: Private info for specified board
  2127. * @chan: Channel to drain
  2128. * @nonblock: boolean, non-zero if O_NONBLOCK is set
  2129. *
  2130. * Sleeps until all playback has been flushed to the audio
  2131. * hardware.
  2132. *
  2133. * Locking: inside card->syscall_mutex
  2134. */
  2135. static int via_dsp_drain_playback (struct via_info *card,
  2136. struct via_channel *chan, int nonblock)
  2137. {
  2138. DECLARE_WAITQUEUE(wait, current);
  2139. int ret = 0;
  2140. DPRINTK ("ENTER, nonblock = %d\n", nonblock);
  2141. if (chan->slop_len > 0)
  2142. via_chan_flush_frag (chan);
  2143. if (atomic_read (&chan->n_frags) == chan->frag_number)
  2144. goto out;
  2145. via_chan_maybe_start (chan);
  2146. add_wait_queue(&chan->wait, &wait);
  2147. for (;;) {
  2148. DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number);
  2149. __set_current_state(TASK_INTERRUPTIBLE);
  2150. if (atomic_read (&chan->n_frags) >= chan->frag_number)
  2151. break;
  2152. if (nonblock) {
  2153. DPRINTK ("EXIT, returning -EAGAIN\n");
  2154. ret = -EAGAIN;
  2155. break;
  2156. }
  2157. #ifdef VIA_DEBUG
  2158. {
  2159. u8 r40,r41,r42,r43,r44,r48;
  2160. pci_read_config_byte (card->pdev, 0x40, &r40);
  2161. pci_read_config_byte (card->pdev, 0x41, &r41);
  2162. pci_read_config_byte (card->pdev, 0x42, &r42);
  2163. pci_read_config_byte (card->pdev, 0x43, &r43);
  2164. pci_read_config_byte (card->pdev, 0x44, &r44);
  2165. pci_read_config_byte (card->pdev, 0x48, &r48);
  2166. DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
  2167. r40,r41,r42,r43,r44,r48);
  2168. DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
  2169. inb (card->baseaddr + 0x00),
  2170. inb (card->baseaddr + 0x01),
  2171. inb (card->baseaddr + 0x02),
  2172. inl (card->baseaddr + 0x04),
  2173. inl (card->baseaddr + 0x0C),
  2174. inl (card->baseaddr + 0x80),
  2175. inl (card->baseaddr + 0x84));
  2176. }
  2177. if (!chan->is_active)
  2178. printk (KERN_ERR "sleeping but not active\n");
  2179. #endif
  2180. mutex_unlock(&card->syscall_mutex);
  2181. DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
  2182. schedule();
  2183. if ((ret = via_syscall_down (card, nonblock)))
  2184. break;
  2185. if (signal_pending (current)) {
  2186. DPRINTK ("EXIT, returning -ERESTARTSYS\n");
  2187. ret = -ERESTARTSYS;
  2188. break;
  2189. }
  2190. }
  2191. set_current_state(TASK_RUNNING);
  2192. remove_wait_queue(&chan->wait, &wait);
  2193. #ifdef VIA_DEBUG
  2194. {
  2195. u8 r40,r41,r42,r43,r44,r48;
  2196. pci_read_config_byte (card->pdev, 0x40, &r40);
  2197. pci_read_config_byte (card->pdev, 0x41, &r41);
  2198. pci_read_config_byte (card->pdev, 0x42, &r42);
  2199. pci_read_config_byte (card->pdev, 0x43, &r43);
  2200. pci_read_config_byte (card->pdev, 0x44, &r44);
  2201. pci_read_config_byte (card->pdev, 0x48, &r48);
  2202. DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
  2203. r40,r41,r42,r43,r44,r48);
  2204. DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
  2205. inb (card->baseaddr + 0x00),
  2206. inb (card->baseaddr + 0x01),
  2207. inb (card->baseaddr + 0x02),
  2208. inl (card->baseaddr + 0x04),
  2209. inl (card->baseaddr + 0x0C),
  2210. inl (card->baseaddr + 0x80),
  2211. inl (card->baseaddr + 0x84));
  2212. DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags));
  2213. }
  2214. #endif
  2215. out:
  2216. DPRINTK ("EXIT, returning %d\n", ret);
  2217. return ret;
  2218. }
  2219. /**
  2220. * via_dsp_ioctl_space - get information about channel buffering
  2221. * @card: Private info for specified board
  2222. * @chan: pointer to channel-specific info
  2223. * @arg: user buffer for returned information
  2224. *
  2225. * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
  2226. *
  2227. * Locking: inside card->syscall_mutex
  2228. */
  2229. static int via_dsp_ioctl_space (struct via_info *card,
  2230. struct via_channel *chan,
  2231. void __user *arg)
  2232. {
  2233. audio_buf_info info;
  2234. via_chan_set_buffering(card, chan, -1);
  2235. info.fragstotal = chan->frag_number;
  2236. info.fragsize = chan->frag_size;
  2237. /* number of full fragments we can read/write without blocking */
  2238. info.fragments = atomic_read (&chan->n_frags);
  2239. if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0))
  2240. info.fragments--;
  2241. /* number of bytes that can be read or written immediately
  2242. * without blocking.
  2243. */
  2244. info.bytes = (info.fragments * chan->frag_size);
  2245. if (chan->slop_len % chan->frag_size > 0)
  2246. info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size);
  2247. DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
  2248. info.fragstotal,
  2249. info.fragsize,
  2250. info.fragments,
  2251. info.bytes);
  2252. return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
  2253. }
  2254. /**
  2255. * via_dsp_ioctl_ptr - get information about hardware buffer ptr
  2256. * @card: Private info for specified board
  2257. * @chan: pointer to channel-specific info
  2258. * @arg: user buffer for returned information
  2259. *
  2260. * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
  2261. *
  2262. * Locking: inside card->syscall_mutex
  2263. */
  2264. static int via_dsp_ioctl_ptr (struct via_info *card,
  2265. struct via_channel *chan,
  2266. void __user *arg)
  2267. {
  2268. count_info info;
  2269. spin_lock_irq (&card->lock);
  2270. info.bytes = chan->bytes;
  2271. info.blocks = chan->n_irqs;
  2272. chan->n_irqs = 0;
  2273. spin_unlock_irq (&card->lock);
  2274. if (chan->is_active) {
  2275. unsigned long extra;
  2276. info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size;
  2277. extra = chan->frag_size - via_sg_offset(chan);
  2278. info.ptr += extra;
  2279. info.bytes += extra;
  2280. } else {
  2281. info.ptr = 0;
  2282. }
  2283. DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
  2284. info.bytes,
  2285. info.blocks,
  2286. info.ptr);
  2287. return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
  2288. }
  2289. static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
  2290. {
  2291. int enable, do_something;
  2292. if (chan->is_record)
  2293. enable = (val & PCM_ENABLE_INPUT);
  2294. else
  2295. enable = (val & PCM_ENABLE_OUTPUT);
  2296. if (!chan->is_enabled && enable) {
  2297. do_something = 1;
  2298. } else if (chan->is_enabled && !enable) {
  2299. do_something = -1;
  2300. } else {
  2301. do_something = 0;
  2302. }
  2303. DPRINTK ("enable=%d, do_something=%d\n",
  2304. enable, do_something);
  2305. if (chan->is_active && do_something)
  2306. return -EINVAL;
  2307. if (do_something == 1) {
  2308. chan->is_enabled = 1;
  2309. via_chan_maybe_start (chan);
  2310. DPRINTK ("Triggering input\n");
  2311. }
  2312. else if (do_something == -1) {
  2313. chan->is_enabled = 0;
  2314. DPRINTK ("Setup input trigger\n");
  2315. }
  2316. return 0;
  2317. }
  2318. static int via_dsp_ioctl (struct inode *inode, struct file *file,
  2319. unsigned int cmd, unsigned long arg)
  2320. {
  2321. int rc, rd=0, wr=0, val=0;
  2322. struct via_info *card;
  2323. struct via_channel *chan;
  2324. int nonblock = (file->f_flags & O_NONBLOCK);
  2325. int __user *ip = (int __user *)arg;
  2326. void __user *p = (void __user *)arg;
  2327. assert (file != NULL);
  2328. card = file->private_data;
  2329. assert (card != NULL);
  2330. if (file->f_mode & FMODE_WRITE)
  2331. wr = 1;
  2332. if (file->f_mode & FMODE_READ)
  2333. rd = 1;
  2334. rc = via_syscall_down (card, nonblock);
  2335. if (rc)
  2336. return rc;
  2337. rc = -EINVAL;
  2338. switch (cmd) {
  2339. /* OSS API version. XXX unverified */
  2340. case OSS_GETVERSION:
  2341. DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
  2342. rc = put_user (SOUND_VERSION, ip);
  2343. break;
  2344. /* list of supported PCM data formats */
  2345. case SNDCTL_DSP_GETFMTS:
  2346. DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
  2347. rc = put_user (AFMT_U8 | AFMT_S16_LE, ip);
  2348. break;
  2349. /* query or set current channel's PCM data format */
  2350. case SNDCTL_DSP_SETFMT:
  2351. if (get_user(val, ip)) {
  2352. rc = -EFAULT;
  2353. break;
  2354. }
  2355. DPRINTK ("DSP_SETFMT, val==%d\n", val);
  2356. if (val != AFMT_QUERY) {
  2357. rc = 0;
  2358. if (rd)
  2359. rc = via_chan_set_fmt (card, &card->ch_in, val);
  2360. if (rc >= 0 && wr)
  2361. rc = via_chan_set_fmt (card, &card->ch_out, val);
  2362. if (rc < 0)
  2363. break;
  2364. val = rc;
  2365. } else {
  2366. if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
  2367. (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
  2368. val = AFMT_S16_LE;
  2369. else
  2370. val = AFMT_U8;
  2371. }
  2372. DPRINTK ("SETFMT EXIT, returning %d\n", val);
  2373. rc = put_user (val, ip);
  2374. break;
  2375. /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
  2376. case SNDCTL_DSP_CHANNELS:
  2377. if (get_user(val, ip)) {
  2378. rc = -EFAULT;
  2379. break;
  2380. }
  2381. DPRINTK ("DSP_CHANNELS, val==%d\n", val);
  2382. if (val != 0) {
  2383. rc = 0;
  2384. if (rd)
  2385. rc = via_chan_set_stereo (card, &card->ch_in, val);
  2386. if (rc >= 0 && wr)
  2387. rc = via_chan_set_stereo (card, &card->ch_out, val);
  2388. if (rc < 0)
  2389. break;
  2390. val = rc;
  2391. } else {
  2392. if (rd)
  2393. val = card->ch_in.channels;
  2394. else
  2395. val = card->ch_out.channels;
  2396. }
  2397. DPRINTK ("CHANNELS EXIT, returning %d\n", val);
  2398. rc = put_user (val, ip);
  2399. break;
  2400. /* enable (val is not zero) or disable (val == 0) stereo */
  2401. case SNDCTL_DSP_STEREO:
  2402. if (get_user(val, ip)) {
  2403. rc = -EFAULT;
  2404. break;
  2405. }
  2406. DPRINTK ("DSP_STEREO, val==%d\n", val);
  2407. rc = 0;
  2408. if (rd)
  2409. rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
  2410. if (rc >= 0 && wr)
  2411. rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
  2412. if (rc < 0)
  2413. break;
  2414. val = rc - 1;
  2415. DPRINTK ("STEREO EXIT, returning %d\n", val);
  2416. rc = put_user(val, ip);
  2417. break;
  2418. /* query or set sampling rate */
  2419. case SNDCTL_DSP_SPEED:
  2420. if (get_user(val, ip)) {
  2421. rc = -EFAULT;
  2422. break;
  2423. }
  2424. DPRINTK ("DSP_SPEED, val==%d\n", val);
  2425. if (val < 0) {
  2426. rc = -EINVAL;
  2427. break;
  2428. }
  2429. if (val > 0) {
  2430. rc = 0;
  2431. if (rd)
  2432. rc = via_chan_set_speed (card, &card->ch_in, val);
  2433. if (rc >= 0 && wr)
  2434. rc = via_chan_set_speed (card, &card->ch_out, val);
  2435. if (rc < 0)
  2436. break;
  2437. val = rc;
  2438. } else {
  2439. if (rd)
  2440. val = card->ch_in.rate;
  2441. else if (wr)
  2442. val = card->ch_out.rate;
  2443. else
  2444. val = 0;
  2445. }
  2446. DPRINTK ("SPEED EXIT, returning %d\n", val);
  2447. rc = put_user (val, ip);
  2448. break;
  2449. /* wait until all buffers have been played, and then stop device */
  2450. case SNDCTL_DSP_SYNC:
  2451. DPRINTK ("DSP_SYNC\n");
  2452. rc = 0;
  2453. if (wr) {
  2454. DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
  2455. rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
  2456. }
  2457. break;
  2458. /* stop recording/playback immediately */
  2459. case SNDCTL_DSP_RESET:
  2460. DPRINTK ("DSP_RESET\n");
  2461. if (rd) {
  2462. via_chan_clear (card, &card->ch_in);
  2463. card->ch_in.frag_number = 0;
  2464. card->ch_in.frag_size = 0;
  2465. atomic_set(&card->ch_in.n_frags, 0);
  2466. }
  2467. if (wr) {
  2468. via_chan_clear (card, &card->ch_out);
  2469. card->ch_out.frag_number = 0;
  2470. card->ch_out.frag_size = 0;
  2471. atomic_set(&card->ch_out.n_frags, 0);
  2472. }
  2473. rc = 0;
  2474. break;
  2475. case SNDCTL_DSP_NONBLOCK:
  2476. file->f_flags |= O_NONBLOCK;
  2477. rc = 0;
  2478. break;
  2479. /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
  2480. case SNDCTL_DSP_GETCAPS:
  2481. DPRINTK ("DSP_GETCAPS\n");
  2482. rc = put_user(VIA_DSP_CAP, ip);
  2483. break;
  2484. /* obtain buffer fragment size */
  2485. case SNDCTL_DSP_GETBLKSIZE:
  2486. DPRINTK ("DSP_GETBLKSIZE\n");
  2487. if (rd) {
  2488. via_chan_set_buffering(card, &card->ch_in, -1);
  2489. rc = put_user(card->ch_in.frag_size, ip);
  2490. } else if (wr) {
  2491. via_chan_set_buffering(card, &card->ch_out, -1);
  2492. rc = put_user(card->ch_out.frag_size, ip);
  2493. }
  2494. break;
  2495. /* obtain information about input buffering */
  2496. case SNDCTL_DSP_GETISPACE:
  2497. DPRINTK ("DSP_GETISPACE\n");
  2498. if (rd)
  2499. rc = via_dsp_ioctl_space (card, &card->ch_in, p);
  2500. break;
  2501. /* obtain information about output buffering */
  2502. case SNDCTL_DSP_GETOSPACE:
  2503. DPRINTK ("DSP_GETOSPACE\n");
  2504. if (wr)
  2505. rc = via_dsp_ioctl_space (card, &card->ch_out, p);
  2506. break;
  2507. /* obtain information about input hardware pointer */
  2508. case SNDCTL_DSP_GETIPTR:
  2509. DPRINTK ("DSP_GETIPTR\n");
  2510. if (rd)
  2511. rc = via_dsp_ioctl_ptr (card, &card->ch_in, p);
  2512. break;
  2513. /* obtain information about output hardware pointer */
  2514. case SNDCTL_DSP_GETOPTR:
  2515. DPRINTK ("DSP_GETOPTR\n");
  2516. if (wr)
  2517. rc = via_dsp_ioctl_ptr (card, &card->ch_out, p);
  2518. break;
  2519. /* return number of bytes remaining to be played by DMA engine */
  2520. case SNDCTL_DSP_GETODELAY:
  2521. {
  2522. DPRINTK ("DSP_GETODELAY\n");
  2523. chan = &card->ch_out;
  2524. if (!wr)
  2525. break;
  2526. if (chan->is_active) {
  2527. val = chan->frag_number - atomic_read (&chan->n_frags);
  2528. assert(val >= 0);
  2529. if (val > 0) {
  2530. val *= chan->frag_size;
  2531. val -= chan->frag_size - via_sg_offset(chan);
  2532. }
  2533. val += chan->slop_len % chan->frag_size;
  2534. } else
  2535. val = 0;
  2536. assert (val <= (chan->frag_size * chan->frag_number));
  2537. DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
  2538. rc = put_user (val, ip);
  2539. break;
  2540. }
  2541. /* handle the quick-start of a channel,
  2542. * or the notification that a quick-start will
  2543. * occur in the future
  2544. */
  2545. case SNDCTL_DSP_SETTRIGGER:
  2546. if (get_user(val, ip)) {
  2547. rc = -EFAULT;
  2548. break;
  2549. }
  2550. DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
  2551. rd, wr, card->ch_in.is_active, card->ch_out.is_active,
  2552. card->ch_in.is_enabled, card->ch_out.is_enabled);
  2553. rc = 0;
  2554. if (rd)
  2555. rc = via_dsp_ioctl_trigger (&card->ch_in, val);
  2556. if (!rc && wr)
  2557. rc = via_dsp_ioctl_trigger (&card->ch_out, val);
  2558. break;
  2559. case SNDCTL_DSP_GETTRIGGER:
  2560. val = 0;
  2561. if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled)
  2562. val |= PCM_ENABLE_INPUT;
  2563. if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled)
  2564. val |= PCM_ENABLE_OUTPUT;
  2565. rc = put_user(val, ip);
  2566. break;
  2567. /* Enable full duplex. Since we do this as soon as we are opened
  2568. * with O_RDWR, this is mainly a no-op that always returns success.
  2569. */
  2570. case SNDCTL_DSP_SETDUPLEX:
  2571. DPRINTK ("DSP_SETDUPLEX\n");
  2572. if (!rd || !wr)
  2573. break;
  2574. rc = 0;
  2575. break;
  2576. /* set fragment size. implemented as a successful no-op for now */
  2577. case SNDCTL_DSP_SETFRAGMENT:
  2578. if (get_user(val, ip)) {
  2579. rc = -EFAULT;
  2580. break;
  2581. }
  2582. DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
  2583. if (rd)
  2584. rc = via_chan_set_buffering(card, &card->ch_in, val);
  2585. if (wr)
  2586. rc = via_chan_set_buffering(card, &card->ch_out, val);
  2587. DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
  2588. val & 0xFFFF,
  2589. val & 0xFFFF,
  2590. (val >> 16) & 0xFFFF,
  2591. (val >> 16) & 0xFFFF);
  2592. rc = 0;
  2593. break;
  2594. /* inform device of an upcoming pause in input (or output). */
  2595. case SNDCTL_DSP_POST:
  2596. DPRINTK ("DSP_POST\n");
  2597. if (wr) {
  2598. if (card->ch_out.slop_len > 0)
  2599. via_chan_flush_frag (&card->ch_out);
  2600. via_chan_maybe_start (&card->ch_out);
  2601. }
  2602. rc = 0;
  2603. break;
  2604. /* not implemented */
  2605. default:
  2606. DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
  2607. cmd, p);
  2608. break;
  2609. }
  2610. mutex_unlock(&card->syscall_mutex);
  2611. DPRINTK ("EXIT, returning %d\n", rc);
  2612. return rc;
  2613. }
  2614. static int via_dsp_open (struct inode *inode, struct file *file)
  2615. {
  2616. int minor = iminor(inode);
  2617. struct via_info *card;
  2618. struct pci_dev *pdev = NULL;
  2619. struct via_channel *chan;
  2620. struct pci_driver *drvr;
  2621. int nonblock = (file->f_flags & O_NONBLOCK);
  2622. DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
  2623. if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
  2624. DPRINTK ("EXIT, returning -EINVAL\n");
  2625. return -EINVAL;
  2626. }
  2627. card = NULL;
  2628. while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
  2629. drvr = pci_dev_driver (pdev);
  2630. if (drvr == &via_driver) {
  2631. assert (pci_get_drvdata (pdev) != NULL);
  2632. card = pci_get_drvdata (pdev);
  2633. DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
  2634. card->dev_dsp, minor,
  2635. (card->dev_dsp ^ minor) & ~0xf);
  2636. if (((card->dev_dsp ^ minor) & ~0xf) == 0)
  2637. goto match;
  2638. }
  2639. }
  2640. DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
  2641. return -ENODEV;
  2642. match:
  2643. if (nonblock) {
  2644. if (!mutex_trylock(&card->open_mutex)) {
  2645. DPRINTK ("EXIT, returning -EAGAIN\n");
  2646. return -EAGAIN;
  2647. }
  2648. } else {
  2649. if (mutex_lock_interruptible(&card->open_mutex)) {
  2650. DPRINTK ("EXIT, returning -ERESTARTSYS\n");
  2651. return -ERESTARTSYS;
  2652. }
  2653. }
  2654. file->private_data = card;
  2655. DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
  2656. /* handle input from analog source */
  2657. if (file->f_mode & FMODE_READ) {
  2658. chan = &card->ch_in;
  2659. via_chan_init (card, chan);
  2660. /* why is this forced to 16-bit stereo in all drivers? */
  2661. chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
  2662. chan->channels = 2;
  2663. // TO DO - use FIFO: via_capture_fifo(card, 1);
  2664. via_chan_pcm_fmt (chan, 0);
  2665. via_set_rate (card->ac97, chan, 44100);
  2666. }
  2667. /* handle output to analog source */
  2668. if (file->f_mode & FMODE_WRITE) {
  2669. chan = &card->ch_out;
  2670. via_chan_init (card, chan);
  2671. if (file->f_mode & FMODE_READ) {
  2672. /* if in duplex mode make the recording and playback channels
  2673. have the same settings */
  2674. chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
  2675. chan->channels = 2;
  2676. via_chan_pcm_fmt (chan, 0);
  2677. via_set_rate (card->ac97, chan, 44100);
  2678. } else {
  2679. if ((minor & 0xf) == SND_DEV_DSP16) {
  2680. chan->pcm_fmt = VIA_PCM_FMT_16BIT;
  2681. via_chan_pcm_fmt (chan, 0);
  2682. via_set_rate (card->ac97, chan, 44100);
  2683. } else {
  2684. via_chan_pcm_fmt (chan, 1);
  2685. via_set_rate (card->ac97, chan, 8000);
  2686. }
  2687. }
  2688. }
  2689. DPRINTK ("EXIT, returning 0\n");
  2690. return nonseekable_open(inode, file);
  2691. }
  2692. static int via_dsp_release(struct inode *inode, struct file *file)
  2693. {
  2694. struct via_info *card;
  2695. int nonblock = (file->f_flags & O_NONBLOCK);
  2696. int rc;
  2697. DPRINTK ("ENTER\n");
  2698. assert (file != NULL);
  2699. card = file->private_data;
  2700. assert (card != NULL);
  2701. rc = via_syscall_down (card, nonblock);
  2702. if (rc) {
  2703. DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
  2704. return rc;
  2705. }
  2706. if (file->f_mode & FMODE_WRITE) {
  2707. rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
  2708. if (rc && rc != -ERESTARTSYS) /* Nobody needs to know about ^C */
  2709. printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
  2710. via_chan_free (card, &card->ch_out);
  2711. via_chan_buffer_free(card, &card->ch_out);
  2712. }
  2713. if (file->f_mode & FMODE_READ) {
  2714. via_chan_free (card, &card->ch_in);
  2715. via_chan_buffer_free (card, &card->ch_in);
  2716. }
  2717. mutex_unlock(&card->syscall_mutex);
  2718. mutex_unlock(&card->open_mutex);
  2719. DPRINTK ("EXIT, returning 0\n");
  2720. return 0;
  2721. }
  2722. /****************************************************************
  2723. *
  2724. * Chip setup and kernel registration
  2725. *
  2726. *
  2727. */
  2728. static int __devinit via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  2729. {
  2730. #ifdef CONFIG_MIDI_VIA82CXXX
  2731. u8 r42;
  2732. #endif
  2733. int rc;
  2734. struct via_info *card;
  2735. static int printed_version;
  2736. DPRINTK ("ENTER\n");
  2737. if (printed_version++ == 0)
  2738. printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
  2739. rc = pci_enable_device (pdev);
  2740. if (rc)
  2741. goto err_out;
  2742. rc = pci_request_regions (pdev, "via82cxxx_audio");
  2743. if (rc)
  2744. goto err_out_disable;
  2745. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  2746. if (rc)
  2747. goto err_out_res;
  2748. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  2749. if (rc)
  2750. goto err_out_res;
  2751. card = kmalloc (sizeof (*card), GFP_KERNEL);
  2752. if (!card) {
  2753. printk (KERN_ERR PFX "out of memory, aborting\n");
  2754. rc = -ENOMEM;
  2755. goto err_out_res;
  2756. }
  2757. pci_set_drvdata (pdev, card);
  2758. memset (card, 0, sizeof (*card));
  2759. card->pdev = pdev;
  2760. card->baseaddr = pci_resource_start (pdev, 0);
  2761. card->card_num = via_num_cards++;
  2762. spin_lock_init (&card->lock);
  2763. spin_lock_init (&card->ac97_lock);
  2764. mutex_init(&card->syscall_mutex);
  2765. mutex_init(&card->open_mutex);
  2766. /* we must init these now, in case the intr handler needs them */
  2767. via_chan_init_defaults (card, &card->ch_out);
  2768. via_chan_init_defaults (card, &card->ch_in);
  2769. via_chan_init_defaults (card, &card->ch_fm);
  2770. /* if BAR 2 is present, chip is Rev H or later,
  2771. * which means it has a few extra features */
  2772. if (pci_resource_start (pdev, 2) > 0)
  2773. card->rev_h = 1;
  2774. /* Overkill for now, but more flexible done right */
  2775. card->intmask = id->driver_data;
  2776. card->legacy = !card->intmask;
  2777. card->sixchannel = id->driver_data;
  2778. if(card->sixchannel)
  2779. printk(KERN_INFO PFX "Six channel audio available\n");
  2780. if (pdev->irq < 1) {
  2781. printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
  2782. rc = -ENODEV;
  2783. goto err_out_kfree;
  2784. }
  2785. if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
  2786. printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
  2787. rc = -ENODEV;
  2788. goto err_out_kfree;
  2789. }
  2790. pci_set_master(pdev);
  2791. /*
  2792. * init AC97 mixer and codec
  2793. */
  2794. rc = via_ac97_init (card);
  2795. if (rc) {
  2796. printk (KERN_ERR PFX "AC97 init failed, aborting\n");
  2797. goto err_out_kfree;
  2798. }
  2799. /*
  2800. * init DSP device
  2801. */
  2802. rc = via_dsp_init (card);
  2803. if (rc) {
  2804. printk (KERN_ERR PFX "DSP device init failed, aborting\n");
  2805. goto err_out_have_mixer;
  2806. }
  2807. /*
  2808. * init and turn on interrupts, as the last thing we do
  2809. */
  2810. rc = via_interrupt_init (card);
  2811. if (rc) {
  2812. printk (KERN_ERR PFX "interrupt init failed, aborting\n");
  2813. goto err_out_have_dsp;
  2814. }
  2815. printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
  2816. card->card_num + 1, card->baseaddr, pdev->irq);
  2817. #ifdef CONFIG_MIDI_VIA82CXXX
  2818. /* Disable by default */
  2819. card->midi_info.io_base = 0;
  2820. if(card->legacy)
  2821. {
  2822. pci_read_config_byte (pdev, 0x42, &r42);
  2823. /* Disable MIDI interrupt */
  2824. pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
  2825. if (r42 & VIA_CR42_MIDI_ENABLE)
  2826. {
  2827. if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
  2828. card->midi_info.io_base = pci_resource_start (pdev, 2);
  2829. else /* Address selected by byte 0x43 */
  2830. {
  2831. u8 r43;
  2832. pci_read_config_byte (pdev, 0x43, &r43);
  2833. card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
  2834. }
  2835. card->midi_info.irq = -pdev->irq;
  2836. if (probe_uart401(& card->midi_info, THIS_MODULE))
  2837. {
  2838. card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
  2839. pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
  2840. printk("Enabled Via MIDI\n");
  2841. }
  2842. }
  2843. }
  2844. #endif
  2845. DPRINTK ("EXIT, returning 0\n");
  2846. return 0;
  2847. err_out_have_dsp:
  2848. via_dsp_cleanup (card);
  2849. err_out_have_mixer:
  2850. via_ac97_cleanup (card);
  2851. err_out_kfree:
  2852. #ifndef VIA_NDEBUG
  2853. memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
  2854. #endif
  2855. kfree (card);
  2856. err_out_res:
  2857. pci_release_regions (pdev);
  2858. err_out_disable:
  2859. pci_disable_device (pdev);
  2860. err_out:
  2861. pci_set_drvdata (pdev, NULL);
  2862. DPRINTK ("EXIT - returning %d\n", rc);
  2863. return rc;
  2864. }
  2865. static void __devexit via_remove_one (struct pci_dev *pdev)
  2866. {
  2867. struct via_info *card;
  2868. DPRINTK ("ENTER\n");
  2869. assert (pdev != NULL);
  2870. card = pci_get_drvdata (pdev);
  2871. assert (card != NULL);
  2872. #ifdef CONFIG_MIDI_VIA82CXXX
  2873. if (card->midi_info.io_base)
  2874. unload_uart401(&card->midi_info);
  2875. #endif
  2876. free_irq (card->pdev->irq, card);
  2877. via_dsp_cleanup (card);
  2878. via_ac97_cleanup (card);
  2879. #ifndef VIA_NDEBUG
  2880. memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
  2881. #endif
  2882. kfree (card);
  2883. pci_set_drvdata (pdev, NULL);
  2884. pci_release_regions (pdev);
  2885. pci_disable_device (pdev);
  2886. pci_set_power_state (pdev, 3); /* ...zzzzzz */
  2887. DPRINTK ("EXIT\n");
  2888. return;
  2889. }
  2890. /****************************************************************
  2891. *
  2892. * Driver initialization and cleanup
  2893. *
  2894. *
  2895. */
  2896. static int __init init_via82cxxx_audio(void)
  2897. {
  2898. int rc;
  2899. DPRINTK ("ENTER\n");
  2900. rc = pci_register_driver (&via_driver);
  2901. if (rc) {
  2902. DPRINTK ("EXIT, returning %d\n", rc);
  2903. return rc;
  2904. }
  2905. DPRINTK ("EXIT, returning 0\n");
  2906. return 0;
  2907. }
  2908. static void __exit cleanup_via82cxxx_audio(void)
  2909. {
  2910. DPRINTK ("ENTER\n");
  2911. pci_unregister_driver (&via_driver);
  2912. DPRINTK ("EXIT\n");
  2913. }
  2914. module_init(init_via82cxxx_audio);
  2915. module_exit(cleanup_via82cxxx_audio);
  2916. MODULE_AUTHOR("Jeff Garzik");
  2917. MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
  2918. MODULE_LICENSE("GPL");