via82cxxx_audio.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618
  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/ioport.h>
  32. #include <linux/delay.h>
  33. #include <linux/dma-mapping.h>
  34. #include <asm/io.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/mutex.h>
  37. #include "sound_config.h"
  38. #include "dev_table.h"
  39. #include "mpu401.h"
  40. #undef VIA_DEBUG /* define to enable debugging output and checks */
  41. #ifdef VIA_DEBUG
  42. /* note: prints function name for you */
  43. #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
  44. #else
  45. #define DPRINTK(fmt, args...)
  46. #endif
  47. #undef VIA_NDEBUG /* define to disable lightweight runtime checks */
  48. #ifdef VIA_NDEBUG
  49. #define assert(expr)
  50. #else
  51. #define assert(expr) \
  52. if(!(expr)) { \
  53. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  54. #expr,__FILE__,__FUNCTION__,__LINE__); \
  55. }
  56. #endif
  57. #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
  58. #define MAX_CARDS 1
  59. #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
  60. #define VIA_MODULE_NAME "via82cxxx"
  61. #define PFX VIA_MODULE_NAME ": "
  62. #define VIA_COUNTER_LIMIT 100000
  63. /* size of DMA buffers */
  64. #define VIA_MAX_BUFFER_DMA_PAGES 32
  65. /* buffering default values in ms */
  66. #define VIA_DEFAULT_FRAG_TIME 20
  67. #define VIA_DEFAULT_BUFFER_TIME 500
  68. /* the hardware has a 256 fragment limit */
  69. #define VIA_MIN_FRAG_NUMBER 2
  70. #define VIA_MAX_FRAG_NUMBER 128
  71. #define VIA_MAX_FRAG_SIZE PAGE_SIZE
  72. #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
  73. /* 82C686 function 5 (audio codec) PCI configuration registers */
  74. #define VIA_ACLINK_STATUS 0x40
  75. #define VIA_ACLINK_CTRL 0x41
  76. #define VIA_FUNC_ENABLE 0x42
  77. #define VIA_PNP_CONTROL 0x43
  78. #define VIA_FM_NMI_CTRL 0x48
  79. /*
  80. * controller base 0 (scatter-gather) registers
  81. *
  82. * NOTE: Via datasheet lists first channel as "read"
  83. * channel and second channel as "write" channel.
  84. * I changed the naming of the constants to be more
  85. * clear than I felt the datasheet to be.
  86. */
  87. #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
  88. #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
  89. #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
  90. #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
  91. #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
  92. #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
  93. #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
  94. #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
  95. /* offsets from base */
  96. #define VIA_PCM_STATUS 0x00
  97. #define VIA_PCM_CONTROL 0x01
  98. #define VIA_PCM_TYPE 0x02
  99. #define VIA_PCM_LEFTVOL 0x02
  100. #define VIA_PCM_RIGHTVOL 0x03
  101. #define VIA_PCM_TABLE_ADDR 0x04
  102. #define VIA_PCM_STOPRATE 0x08 /* 8233+ */
  103. #define VIA_PCM_BLOCK_COUNT 0x0C
  104. /* XXX unused DMA channel for FM PCM data */
  105. #define VIA_BASE0_FM_OUT_CHAN 0x20
  106. #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
  107. #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
  108. #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
  109. /* Six channel audio output on 8233 */
  110. #define VIA_BASE0_MULTI_OUT_CHAN 0x40
  111. #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
  112. #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
  113. #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
  114. #define VIA_BASE0_AC97_CTRL 0x80
  115. #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
  116. #define VIA_BASE0_GPI_INT_ENABLE 0x8C
  117. #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
  118. #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
  119. #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
  120. #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
  121. /* Newer VIA we need to monitor the low 3 bits of each channel. This
  122. mask covers the channels we don't yet use as well
  123. */
  124. #define VIA_NEW_INTR_MASK 0x77077777UL
  125. /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
  126. #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
  127. #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
  128. #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
  129. #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
  130. #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
  131. #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
  132. #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
  133. #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
  134. #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
  135. #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
  136. #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
  137. VIA_IRQ_ON_FLAG | \
  138. VIA_IRQ_ON_EOL)
  139. #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
  140. /* PCI configuration register bits and masks */
  141. #define VIA_CR40_AC97_READY 0x01
  142. #define VIA_CR40_AC97_LOW_POWER 0x02
  143. #define VIA_CR40_SECONDARY_READY 0x04
  144. #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
  145. #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
  146. #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
  147. #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
  148. #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
  149. #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
  150. #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
  151. #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
  152. #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
  153. VIA_CR41_AC97_WAKEUP | \
  154. VIA_CR41_AC97_SDO)
  155. #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
  156. VIA_CR41_AC97_RESET | \
  157. VIA_CR41_VRA | \
  158. VIA_CR41_PCM_ENABLE)
  159. #define VIA_CR42_SB_ENABLE 0x01
  160. #define VIA_CR42_MIDI_ENABLE 0x02
  161. #define VIA_CR42_FM_ENABLE 0x04
  162. #define VIA_CR42_GAME_ENABLE 0x08
  163. #define VIA_CR42_MIDI_IRQMASK 0x40
  164. #define VIA_CR42_MIDI_PNP 0x80
  165. #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
  166. #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
  167. #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
  168. /* controller base 0 register bitmasks */
  169. #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
  170. #define VIA_SGD_STOPPED (1 << 2)
  171. #define VIA_SGD_PAUSED (1 << 6)
  172. #define VIA_SGD_ACTIVE (1 << 7)
  173. #define VIA_SGD_TERMINATE (1 << 6)
  174. #define VIA_SGD_FLAG (1 << 0)
  175. #define VIA_SGD_EOL (1 << 1)
  176. #define VIA_SGD_START (1 << 7)
  177. #define VIA_CR80_FIRST_CODEC 0
  178. #define VIA_CR80_SECOND_CODEC (1 << 30)
  179. #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
  180. #define VIA_CR80_VALID (1 << 25)
  181. #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
  182. #define VIA_CR80_BUSY (1 << 24)
  183. #define VIA_CR83_BUSY (1)
  184. #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
  185. #define VIA_CR80_READ (1 << 23)
  186. #define VIA_CR80_WRITE_MODE 0
  187. #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
  188. /* capabilities we announce */
  189. #ifdef VIA_SUPPORT_MMAP
  190. #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
  191. DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
  192. #else
  193. #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
  194. DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
  195. #endif
  196. /* scatter-gather DMA table entry, exactly as passed to hardware */
  197. struct via_sgd_table {
  198. u32 addr;
  199. u32 count; /* includes additional VIA_xxx bits also */
  200. };
  201. #define VIA_EOL (1 << 31)
  202. #define VIA_FLAG (1 << 30)
  203. #define VIA_STOP (1 << 29)
  204. enum via_channel_states {
  205. sgd_stopped = 0,
  206. sgd_in_progress = 1,
  207. };
  208. struct via_buffer_pgtbl {
  209. dma_addr_t handle;
  210. void *cpuaddr;
  211. };
  212. struct via_channel {
  213. atomic_t n_frags;
  214. atomic_t hw_ptr;
  215. wait_queue_head_t wait;
  216. unsigned int sw_ptr;
  217. unsigned int slop_len;
  218. unsigned int n_irqs;
  219. int bytes;
  220. unsigned is_active : 1;
  221. unsigned is_record : 1;
  222. unsigned is_mapped : 1;
  223. unsigned is_enabled : 1;
  224. unsigned is_multi: 1; /* 8233 6 channel */
  225. u8 pcm_fmt; /* VIA_PCM_FMT_xxx */
  226. u8 channels; /* Channel count */
  227. unsigned rate; /* sample rate */
  228. unsigned int frag_size;
  229. unsigned int frag_number;
  230. unsigned char intmask;
  231. volatile struct via_sgd_table *sgtable;
  232. dma_addr_t sgt_handle;
  233. unsigned int page_number;
  234. struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
  235. long iobase;
  236. const char *name;
  237. };
  238. /* data stored for each chip */
  239. struct via_info {
  240. struct pci_dev *pdev;
  241. long baseaddr;
  242. struct ac97_codec *ac97;
  243. spinlock_t ac97_lock;
  244. spinlock_t lock;
  245. int card_num; /* unique card number, from 0 */
  246. int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
  247. unsigned rev_h : 1;
  248. unsigned legacy: 1; /* Has legacy ports */
  249. unsigned intmask: 1; /* Needs int bits */
  250. unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
  251. unsigned volume: 1;
  252. unsigned locked_rate : 1;
  253. int mixer_vol; /* 8233/35 volume - not yet implemented */
  254. struct mutex syscall_mutex;
  255. struct mutex open_mutex;
  256. /* The 8233/8235 have 4 DX audio channels, two record and
  257. one six channel out. We bind ch_in to DX 1, ch_out to multichannel
  258. and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
  259. moment */
  260. struct via_channel ch_in;
  261. struct via_channel ch_out;
  262. struct via_channel ch_fm;
  263. #ifdef CONFIG_MIDI_VIA82CXXX
  264. void *midi_devc;
  265. struct address_info midi_info;
  266. #endif
  267. };
  268. /* number of cards, used for assigning unique numbers to cards */
  269. static unsigned via_num_cards;
  270. /****************************************************************
  271. *
  272. * prototypes
  273. *
  274. *
  275. */
  276. static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
  277. static void __devexit via_remove_one (struct pci_dev *pdev);
  278. static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
  279. static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
  280. static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
  281. static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
  282. static int via_dsp_open (struct inode *inode, struct file *file);
  283. static int via_dsp_release(struct inode *inode, struct file *file);
  284. static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
  285. static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
  286. static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
  287. static u8 via_ac97_wait_idle (struct via_info *card);
  288. static void via_chan_free (struct via_info *card, struct via_channel *chan);
  289. static void via_chan_clear (struct via_info *card, struct via_channel *chan);
  290. static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
  291. static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
  292. /****************************************************************
  293. *
  294. * Various data the driver needs
  295. *
  296. *
  297. */
  298. static struct pci_device_id via_pci_tbl[] = {
  299. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
  300. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  301. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
  302. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  303. { 0, }
  304. };
  305. MODULE_DEVICE_TABLE(pci,via_pci_tbl);
  306. static struct pci_driver via_driver = {
  307. .name = VIA_MODULE_NAME,
  308. .id_table = via_pci_tbl,
  309. .probe = via_init_one,
  310. .remove = __devexit_p(via_remove_one),
  311. };
  312. /****************************************************************
  313. *
  314. * Low-level base 0 register read/write helpers
  315. *
  316. *
  317. */
  318. /**
  319. * via_chan_stop - Terminate DMA on specified PCM channel
  320. * @iobase: PCI base address for SGD channel registers
  321. *
  322. * Terminate scatter-gather DMA operation for given
  323. * channel (derived from @iobase), if DMA is active.
  324. *
  325. * Note that @iobase is not the PCI base address,
  326. * but the PCI base address plus an offset to
  327. * one of three PCM channels supported by the chip.
  328. *
  329. */
  330. static inline void via_chan_stop (long iobase)
  331. {
  332. if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
  333. outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
  334. }
  335. /**
  336. * via_chan_status_clear - Clear status flags on specified DMA channel
  337. * @iobase: PCI base address for SGD channel registers
  338. *
  339. * Clear any pending status flags for the given
  340. * DMA channel (derived from @iobase), if any
  341. * flags are asserted.
  342. *
  343. * Note that @iobase is not the PCI base address,
  344. * but the PCI base address plus an offset to
  345. * one of three PCM channels supported by the chip.
  346. *
  347. */
  348. static inline void via_chan_status_clear (long iobase)
  349. {
  350. u8 tmp = inb (iobase + VIA_PCM_STATUS);
  351. if (tmp != 0)
  352. outb (tmp, iobase + VIA_PCM_STATUS);
  353. }
  354. /**
  355. * sg_begin - Begin recording or playback on a PCM channel
  356. * @chan: Channel for which DMA operation shall begin
  357. *
  358. * Start scatter-gather DMA for the given channel.
  359. *
  360. */
  361. static inline void sg_begin (struct via_channel *chan)
  362. {
  363. DPRINTK("Start with intmask %d\n", chan->intmask);
  364. DPRINTK("About to start from %d to %d\n",
  365. inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
  366. inb(chan->iobase + VIA_PCM_STOPRATE + 3));
  367. outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
  368. DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
  369. DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
  370. }
  371. static int sg_active (long iobase)
  372. {
  373. u8 tmp = inb (iobase + VIA_PCM_STATUS);
  374. if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
  375. printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
  376. return 0;
  377. }
  378. if (tmp & VIA_SGD_ACTIVE)
  379. return 1;
  380. return 0;
  381. }
  382. static int via_sg_offset(struct via_channel *chan)
  383. {
  384. return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
  385. }
  386. /****************************************************************
  387. *
  388. * Miscellaneous debris
  389. *
  390. *
  391. */
  392. /**
  393. * via_syscall_down - down the card-specific syscell semaphore
  394. * @card: Private info for specified board
  395. * @nonblock: boolean, non-zero if O_NONBLOCK is set
  396. *
  397. * Encapsulates standard method of acquiring the syscall sem.
  398. *
  399. * Returns negative errno on error, or zero for success.
  400. */
  401. static inline int via_syscall_down (struct via_info *card, int nonblock)
  402. {
  403. /* Thomas Sailer:
  404. * EAGAIN is supposed to be used if IO is pending,
  405. * not if there is contention on some internal
  406. * synchronization primitive which should be
  407. * held only for a short time anyway
  408. */
  409. nonblock = 0;
  410. if (nonblock) {
  411. if (!mutex_trylock(&card->syscall_mutex))
  412. return -EAGAIN;
  413. } else {
  414. if (mutex_lock_interruptible(&card->syscall_mutex))
  415. return -ERESTARTSYS;
  416. }
  417. return 0;
  418. }
  419. /**
  420. * via_stop_everything - Stop all audio operations
  421. * @card: Private info for specified board
  422. *
  423. * Stops all DMA operations and interrupts, and clear
  424. * any pending status bits resulting from those operations.
  425. */
  426. static void via_stop_everything (struct via_info *card)
  427. {
  428. u8 tmp, new_tmp;
  429. DPRINTK ("ENTER\n");
  430. assert (card != NULL);
  431. /*
  432. * terminate any existing operations on audio read/write channels
  433. */
  434. via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
  435. via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
  436. via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
  437. if(card->sixchannel)
  438. via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
  439. /*
  440. * clear any existing stops / flags (sanity check mainly)
  441. */
  442. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
  443. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
  444. via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
  445. if(card->sixchannel)
  446. via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
  447. /*
  448. * clear any enabled interrupt bits
  449. */
  450. tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
  451. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  452. if (tmp != new_tmp)
  453. outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
  454. tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
  455. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  456. if (tmp != new_tmp)
  457. outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
  458. tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
  459. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  460. if (tmp != new_tmp)
  461. outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
  462. if(card->sixchannel)
  463. {
  464. tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
  465. new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
  466. if (tmp != new_tmp)
  467. outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
  468. }
  469. udelay(10);
  470. /*
  471. * clear any existing flags
  472. */
  473. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
  474. via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
  475. via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
  476. DPRINTK ("EXIT\n");
  477. }
  478. /**
  479. * via_set_rate - Set PCM rate for given channel
  480. * @ac97: Pointer to generic codec info struct
  481. * @chan: Private info for specified channel
  482. * @rate: Desired PCM sample rate, in Khz
  483. *
  484. * Sets the PCM sample rate for a channel.
  485. *
  486. * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
  487. * due to hardware constraints.
  488. */
  489. static int via_set_rate (struct ac97_codec *ac97,
  490. struct via_channel *chan, unsigned rate)
  491. {
  492. struct via_info *card = ac97->private_data;
  493. int rate_reg;
  494. u32 dacp;
  495. u32 mast_vol, phone_vol, mono_vol, pcm_vol;
  496. u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */
  497. DPRINTK ("ENTER, rate = %d\n", rate);
  498. if (chan->rate == rate)
  499. goto out;
  500. if (card->locked_rate) {
  501. chan->rate = 48000;
  502. goto out;
  503. }
  504. if (rate > 48000) rate = 48000;
  505. if (rate < 4000) rate = 4000;
  506. rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
  507. AC97_PCM_FRONT_DAC_RATE;
  508. /* Save current state */
  509. dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
  510. mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
  511. mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
  512. phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
  513. pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
  514. /* Mute - largely reduces popping */
  515. via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
  516. via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
  517. via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
  518. via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
  519. /* Power down the DAC */
  520. via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
  521. /* Set new rate */
  522. via_ac97_write_reg (ac97, rate_reg, rate);
  523. /* Power DAC back up */
  524. via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
  525. udelay (200); /* reduces popping */
  526. /* Restore volumes */
  527. via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
  528. via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
  529. via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
  530. via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
  531. /* the hardware might return a value different than what we
  532. * passed to it, so read the rate value back from hardware
  533. * to see what we came up with
  534. */
  535. chan->rate = via_ac97_read_reg (ac97, rate_reg);
  536. if (chan->rate == 0) {
  537. card->locked_rate = 1;
  538. chan->rate = 48000;
  539. printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
  540. }
  541. out:
  542. DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
  543. return chan->rate;
  544. }
  545. /****************************************************************
  546. *
  547. * Channel-specific operations
  548. *
  549. *
  550. */
  551. /**
  552. * via_chan_init_defaults - Initialize a struct via_channel
  553. * @card: Private audio chip info
  554. * @chan: Channel to be initialized
  555. *
  556. * Zero @chan, and then set all static defaults for the structure.
  557. */
  558. static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
  559. {
  560. memset (chan, 0, sizeof (*chan));
  561. if(card->intmask)
  562. chan->intmask = 0x23; /* Turn on the IRQ bits */
  563. if (chan == &card->ch_out) {
  564. chan->name = "PCM-OUT";
  565. if(card->sixchannel)
  566. {
  567. chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
  568. chan->is_multi = 1;
  569. DPRINTK("Using multichannel for pcm out\n");
  570. }
  571. else
  572. chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
  573. } else if (chan == &card->ch_in) {
  574. chan->name = "PCM-IN";
  575. chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
  576. chan->is_record = 1;
  577. } else if (chan == &card->ch_fm) {
  578. chan->name = "PCM-OUT-FM";
  579. chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
  580. } else {
  581. BUG();
  582. }
  583. init_waitqueue_head (&chan->wait);
  584. chan->pcm_fmt = VIA_PCM_FMT_MASK;
  585. chan->is_enabled = 1;
  586. chan->frag_number = 0;
  587. chan->frag_size = 0;
  588. atomic_set(&chan->n_frags, 0);
  589. atomic_set (&chan->hw_ptr, 0);
  590. }
  591. /**
  592. * via_chan_init - Initialize PCM channel
  593. * @card: Private audio chip info
  594. * @chan: Channel to be initialized
  595. *
  596. * Performs some of the preparations necessary to begin
  597. * using a PCM channel.
  598. *
  599. * Currently the preparations consist of
  600. * setting the PCM channel to a known state.
  601. */
  602. static void via_chan_init (struct via_info *card, struct via_channel *chan)
  603. {
  604. DPRINTK ("ENTER\n");
  605. /* bzero channel structure, and init members to defaults */
  606. via_chan_init_defaults (card, chan);
  607. /* stop any existing channel output */
  608. via_chan_clear (card, chan);
  609. via_chan_status_clear (chan->iobase);
  610. via_chan_pcm_fmt (chan, 1);
  611. DPRINTK ("EXIT\n");
  612. }
  613. /**
  614. * via_chan_buffer_init - Initialize PCM channel buffer
  615. * @card: Private audio chip info
  616. * @chan: Channel to be initialized
  617. *
  618. * Performs some of the preparations necessary to begin
  619. * using a PCM channel.
  620. *
  621. * Currently the preparations include allocating the
  622. * scatter-gather DMA table and buffers,
  623. * and passing the
  624. * address of the DMA table to the hardware.
  625. *
  626. * Note that special care is taken when passing the
  627. * DMA table address to hardware, because it was found
  628. * during driver development that the hardware did not
  629. * always "take" the address.
  630. */
  631. static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
  632. {
  633. int page, offset;
  634. int i;
  635. DPRINTK ("ENTER\n");
  636. chan->intmask = 0;
  637. if(card->intmask)
  638. chan->intmask = 0x23; /* Turn on the IRQ bits */
  639. if (chan->sgtable != NULL) {
  640. DPRINTK ("EXIT\n");
  641. return 0;
  642. }
  643. /* alloc DMA-able memory for scatter-gather table */
  644. chan->sgtable = pci_alloc_consistent (card->pdev,
  645. (sizeof (struct via_sgd_table) * chan->frag_number),
  646. &chan->sgt_handle);
  647. if (!chan->sgtable) {
  648. printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
  649. DPRINTK ("EXIT\n");
  650. return -ENOMEM;
  651. }
  652. memset ((void*)chan->sgtable, 0,
  653. (sizeof (struct via_sgd_table) * chan->frag_number));
  654. /* alloc DMA-able memory for scatter-gather buffers */
  655. chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
  656. (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
  657. for (i = 0; i < chan->page_number; i++) {
  658. chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
  659. &chan->pgtbl[i].handle);
  660. if (!chan->pgtbl[i].cpuaddr) {
  661. chan->page_number = i;
  662. goto err_out_nomem;
  663. }
  664. #ifndef VIA_NDEBUG
  665. memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
  666. #endif
  667. #if 1
  668. DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
  669. i, (long)chan->pgtbl[i].handle,
  670. virt_to_phys(chan->pgtbl[i].cpuaddr),
  671. chan->pgtbl[i].cpuaddr);
  672. #endif
  673. }
  674. for (i = 0; i < chan->frag_number; i++) {
  675. page = i / (PAGE_SIZE / chan->frag_size);
  676. offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
  677. chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
  678. chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
  679. #if 1
  680. DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
  681. i,
  682. (long)chan->sgtable[i].addr);
  683. #endif
  684. }
  685. /* overwrite the last buffer information */
  686. chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
  687. /* set location of DMA-able scatter-gather info table */
  688. DPRINTK ("outl (0x%X, 0x%04lX)\n",
  689. chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
  690. via_ac97_wait_idle (card);
  691. outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
  692. udelay (20);
  693. via_ac97_wait_idle (card);
  694. /* load no rate adaption, stereo 16bit, set up ring slots */
  695. if(card->sixchannel)
  696. {
  697. if(!chan->is_multi)
  698. {
  699. outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
  700. udelay (20);
  701. via_ac97_wait_idle (card);
  702. }
  703. }
  704. DPRINTK ("inl (0x%lX) = %x\n",
  705. chan->iobase + VIA_PCM_TABLE_ADDR,
  706. inl(chan->iobase + VIA_PCM_TABLE_ADDR));
  707. DPRINTK ("EXIT\n");
  708. return 0;
  709. err_out_nomem:
  710. printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
  711. via_chan_buffer_free (card, chan);
  712. DPRINTK ("EXIT\n");
  713. return -ENOMEM;
  714. }
  715. /**
  716. * via_chan_free - Release a PCM channel
  717. * @card: Private audio chip info
  718. * @chan: Channel to be released
  719. *
  720. * Performs all the functions necessary to clean up
  721. * an initialized channel.
  722. *
  723. * Currently these functions include disabled any
  724. * active DMA operations, setting the PCM channel
  725. * back to a known state, and releasing any allocated
  726. * sound buffers.
  727. */
  728. static void via_chan_free (struct via_info *card, struct via_channel *chan)
  729. {
  730. DPRINTK ("ENTER\n");
  731. spin_lock_irq (&card->lock);
  732. /* stop any existing channel output */
  733. via_chan_status_clear (chan->iobase);
  734. via_chan_stop (chan->iobase);
  735. via_chan_status_clear (chan->iobase);
  736. spin_unlock_irq (&card->lock);
  737. synchronize_irq(card->pdev->irq);
  738. DPRINTK ("EXIT\n");
  739. }
  740. static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
  741. {
  742. int i;
  743. DPRINTK ("ENTER\n");
  744. /* zero location of DMA-able scatter-gather info table */
  745. via_ac97_wait_idle(card);
  746. outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
  747. for (i = 0; i < chan->page_number; i++)
  748. if (chan->pgtbl[i].cpuaddr) {
  749. pci_free_consistent (card->pdev, PAGE_SIZE,
  750. chan->pgtbl[i].cpuaddr,
  751. chan->pgtbl[i].handle);
  752. chan->pgtbl[i].cpuaddr = NULL;
  753. chan->pgtbl[i].handle = 0;
  754. }
  755. chan->page_number = 0;
  756. if (chan->sgtable) {
  757. pci_free_consistent (card->pdev,
  758. (sizeof (struct via_sgd_table) * chan->frag_number),
  759. (void*)chan->sgtable, chan->sgt_handle);
  760. chan->sgtable = NULL;
  761. }
  762. DPRINTK ("EXIT\n");
  763. }
  764. /**
  765. * via_chan_pcm_fmt - Update PCM channel settings
  766. * @chan: Channel to be updated
  767. * @reset: Boolean. If non-zero, channel will be reset
  768. * to 8-bit mono mode.
  769. *
  770. * Stores the settings of the current PCM format,
  771. * 8-bit or 16-bit, and mono/stereo, into the
  772. * hardware settings for the specified channel.
  773. * If @reset is non-zero, the channel is reset
  774. * to 8-bit mono mode. Otherwise, the channel
  775. * is set to the values stored in the channel
  776. * information struct @chan.
  777. */
  778. static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
  779. {
  780. DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
  781. chan->pcm_fmt, reset ? "yes" : "no");
  782. assert (chan != NULL);
  783. if (reset)
  784. {
  785. /* reset to 8-bit mono mode */
  786. chan->pcm_fmt = 0;
  787. chan->channels = 1;
  788. }
  789. /* enable interrupts on FLAG and EOL */
  790. chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
  791. /* if we are recording, enable recording fifo bit */
  792. if (chan->is_record)
  793. chan->pcm_fmt |= VIA_PCM_REC_FIFO;
  794. /* set interrupt select bits where applicable (PCM in & out channels) */
  795. if (!chan->is_record)
  796. chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
  797. DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
  798. if(chan->intmask)
  799. {
  800. u32 m;
  801. /*
  802. * Channel 0x4 is up to 6 x 16bit and has to be
  803. * programmed differently
  804. */
  805. if(chan->is_multi)
  806. {
  807. u8 c = 0;
  808. /*
  809. * Load the type bit for num channels
  810. * and 8/16bit
  811. */
  812. if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
  813. c = 1 << 7;
  814. if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
  815. c |= (2<<4);
  816. else
  817. c |= (1<<4);
  818. outb(c, chan->iobase + VIA_PCM_TYPE);
  819. /*
  820. * Set the channel steering
  821. * Mono
  822. * Channel 0 to slot 3
  823. * Channel 0 to slot 4
  824. * Stereo
  825. * Channel 0 to slot 3
  826. * Channel 1 to slot 4
  827. */
  828. switch(chan->channels)
  829. {
  830. case 1:
  831. outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
  832. break;
  833. case 2:
  834. outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
  835. break;
  836. case 4:
  837. outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
  838. break;
  839. case 6:
  840. outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
  841. break;
  842. }
  843. }
  844. else
  845. {
  846. /*
  847. * New style, turn off channel volume
  848. * control, set bits in the right register
  849. */
  850. outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
  851. outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
  852. m = inl(chan->iobase + VIA_PCM_STOPRATE);
  853. m &= ~(3<<20);
  854. if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
  855. m |= (1 << 20);
  856. if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
  857. m |= (1 << 21);
  858. outl(m, chan->iobase + VIA_PCM_STOPRATE);
  859. }
  860. }
  861. else
  862. outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
  863. DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
  864. chan->pcm_fmt,
  865. inb (chan->iobase + VIA_PCM_TYPE));
  866. }
  867. /**
  868. * via_chan_clear - Stop DMA channel operation, and reset pointers
  869. * @card: the chip to accessed
  870. * @chan: Channel to be cleared
  871. *
  872. * Call via_chan_stop to halt DMA operations, and then resets
  873. * all software pointers which track DMA operation.
  874. */
  875. static void via_chan_clear (struct via_info *card, struct via_channel *chan)
  876. {
  877. DPRINTK ("ENTER\n");
  878. via_chan_stop (chan->iobase);
  879. via_chan_buffer_free(card, chan);
  880. chan->is_active = 0;
  881. chan->is_mapped = 0;
  882. chan->is_enabled = 1;
  883. chan->slop_len = 0;
  884. chan->sw_ptr = 0;
  885. chan->n_irqs = 0;
  886. atomic_set (&chan->hw_ptr, 0);
  887. DPRINTK ("EXIT\n");
  888. }
  889. /**
  890. * via_chan_set_speed - Set PCM sample rate for given channel
  891. * @card: Private info for specified board
  892. * @chan: Channel whose sample rate will be adjusted
  893. * @val: New sample rate, in Khz
  894. *
  895. * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
  896. * demand that all audio operations halt (if they are not already
  897. * halted) when the %SNDCTL_DSP_SPEED is given.
  898. *
  899. * This function halts all audio operations for the given channel
  900. * @chan, and then calls via_set_rate to set the audio hardware
  901. * to the new rate.
  902. */
  903. static int via_chan_set_speed (struct via_info *card,
  904. struct via_channel *chan, int val)
  905. {
  906. DPRINTK ("ENTER, requested rate = %d\n", val);
  907. via_chan_clear (card, chan);
  908. val = via_set_rate (card->ac97, chan, val);
  909. DPRINTK ("EXIT, returning %d\n", val);
  910. return val;
  911. }
  912. /**
  913. * via_chan_set_fmt - Set PCM sample size for given channel
  914. * @card: Private info for specified board
  915. * @chan: Channel whose sample size will be adjusted
  916. * @val: New sample size, use the %AFMT_xxx constants
  917. *
  918. * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
  919. * demand that all audio operations halt (if they are not already
  920. * halted) when the %SNDCTL_DSP_SETFMT is given.
  921. *
  922. * This function halts all audio operations for the given channel
  923. * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
  924. * to the new sample size, either 8-bit or 16-bit.
  925. */
  926. static int via_chan_set_fmt (struct via_info *card,
  927. struct via_channel *chan, int val)
  928. {
  929. DPRINTK ("ENTER, val=%s\n",
  930. val == AFMT_U8 ? "AFMT_U8" :
  931. val == AFMT_S16_LE ? "AFMT_S16_LE" :
  932. "unknown");
  933. via_chan_clear (card, chan);
  934. assert (val != AFMT_QUERY); /* this case is handled elsewhere */
  935. switch (val) {
  936. case AFMT_S16_LE:
  937. if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
  938. chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
  939. via_chan_pcm_fmt (chan, 0);
  940. }
  941. break;
  942. case AFMT_U8:
  943. if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
  944. chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
  945. via_chan_pcm_fmt (chan, 0);
  946. }
  947. break;
  948. default:
  949. DPRINTK ("unknown AFMT: 0x%X\n", val);
  950. val = AFMT_S16_LE;
  951. }
  952. DPRINTK ("EXIT\n");
  953. return val;
  954. }
  955. /**
  956. * via_chan_set_stereo - Enable or disable stereo for a DMA channel
  957. * @card: Private info for specified board
  958. * @chan: Channel whose stereo setting will be adjusted
  959. * @val: New sample size, use the %AFMT_xxx constants
  960. *
  961. * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
  962. * demand that all audio operations halt (if they are not already
  963. * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
  964. *
  965. * This function halts all audio operations for the given channel
  966. * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
  967. * to enable or disable stereo.
  968. */
  969. static int via_chan_set_stereo (struct via_info *card,
  970. struct via_channel *chan, int val)
  971. {
  972. DPRINTK ("ENTER, channels = %d\n", val);
  973. via_chan_clear (card, chan);
  974. switch (val) {
  975. /* mono */
  976. case 1:
  977. chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
  978. chan->channels = 1;
  979. via_chan_pcm_fmt (chan, 0);
  980. break;
  981. /* stereo */
  982. case 2:
  983. chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
  984. chan->channels = 2;
  985. via_chan_pcm_fmt (chan, 0);
  986. break;
  987. case 4:
  988. case 6:
  989. if(chan->is_multi)
  990. {
  991. chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
  992. chan->channels = val;
  993. break;
  994. }
  995. /* unknown */
  996. default:
  997. val = -EINVAL;
  998. break;
  999. }
  1000. DPRINTK ("EXIT, returning %d\n", val);
  1001. return val;
  1002. }
  1003. static int via_chan_set_buffering (struct via_info *card,
  1004. struct via_channel *chan, int val)
  1005. {
  1006. int shift;
  1007. DPRINTK ("ENTER\n");
  1008. /* in both cases the buffer cannot be changed */
  1009. if (chan->is_active || chan->is_mapped) {
  1010. DPRINTK ("EXIT\n");
  1011. return -EINVAL;
  1012. }
  1013. /* called outside SETFRAGMENT */
  1014. /* set defaults or do nothing */
  1015. if (val < 0) {
  1016. if (chan->frag_size && chan->frag_number)
  1017. goto out;
  1018. DPRINTK ("\n");
  1019. chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
  1020. * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
  1021. shift = 0;
  1022. while (chan->frag_size) {
  1023. chan->frag_size >>= 1;
  1024. shift++;
  1025. }
  1026. chan->frag_size = 1 << shift;
  1027. chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
  1028. DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
  1029. } else {
  1030. chan->frag_size = 1 << (val & 0xFFFF);
  1031. chan->frag_number = (val >> 16) & 0xFFFF;
  1032. DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
  1033. }
  1034. /* quake3 wants frag_number to be a power of two */
  1035. shift = 0;
  1036. while (chan->frag_number) {
  1037. chan->frag_number >>= 1;
  1038. shift++;
  1039. }
  1040. chan->frag_number = 1 << shift;
  1041. if (chan->frag_size > VIA_MAX_FRAG_SIZE)
  1042. chan->frag_size = VIA_MAX_FRAG_SIZE;
  1043. else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
  1044. chan->frag_size = VIA_MIN_FRAG_SIZE;
  1045. if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
  1046. chan->frag_number = VIA_MIN_FRAG_NUMBER;
  1047. if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
  1048. chan->frag_number = VIA_MAX_FRAG_NUMBER;
  1049. if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
  1050. chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
  1051. out:
  1052. if (chan->is_record)
  1053. atomic_set (&chan->n_frags, 0);
  1054. else
  1055. atomic_set (&chan->n_frags, chan->frag_number);
  1056. DPRINTK ("EXIT\n");
  1057. return 0;
  1058. }
  1059. #ifdef VIA_CHAN_DUMP_BUFS
  1060. /**
  1061. * via_chan_dump_bufs - Display DMA table contents
  1062. * @chan: Channel whose DMA table will be displayed
  1063. *
  1064. * Debugging function which displays the contents of the
  1065. * scatter-gather DMA table for the given channel @chan.
  1066. */
  1067. static void via_chan_dump_bufs (struct via_channel *chan)
  1068. {
  1069. int i;
  1070. for (i = 0; i < chan->frag_number; i++) {
  1071. DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
  1072. i, chan->sgtable[i].addr,
  1073. chan->sgtable[i].count & 0x00FFFFFF,
  1074. chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
  1075. chan->sgtable[i].count & VIA_EOL ? 1 : 0);
  1076. }
  1077. DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
  1078. atomic_read (&chan->buf_in_use),
  1079. atomic_read (&chan->sw_ptr));
  1080. }
  1081. #endif /* VIA_CHAN_DUMP_BUFS */
  1082. /**
  1083. * via_chan_flush_frag - Flush partially-full playback buffer to hardware
  1084. * @chan: Channel whose DMA table will be flushed
  1085. *
  1086. * Flushes partially-full playback buffer to hardware.
  1087. */
  1088. static void via_chan_flush_frag (struct via_channel *chan)
  1089. {
  1090. DPRINTK ("ENTER\n");
  1091. assert (chan->slop_len > 0);
  1092. if (chan->sw_ptr == (chan->frag_number - 1))
  1093. chan->sw_ptr = 0;
  1094. else
  1095. chan->sw_ptr++;
  1096. chan->slop_len = 0;
  1097. assert (atomic_read (&chan->n_frags) > 0);
  1098. atomic_dec (&chan->n_frags);
  1099. DPRINTK ("EXIT\n");
  1100. }
  1101. /**
  1102. * via_chan_maybe_start - Initiate audio hardware DMA operation
  1103. * @chan: Channel whose DMA is to be started
  1104. *
  1105. * Initiate DMA operation, if the DMA engine for the given
  1106. * channel @chan is not already active.
  1107. */
  1108. static inline void via_chan_maybe_start (struct via_channel *chan)
  1109. {
  1110. assert (chan->is_active == sg_active(chan->iobase));
  1111. DPRINTK ("MAYBE START %s\n", chan->name);
  1112. if (!chan->is_active && chan->is_enabled) {
  1113. chan->is_active = 1;
  1114. sg_begin (chan);
  1115. DPRINTK ("starting channel %s\n", chan->name);
  1116. }
  1117. }
  1118. /****************************************************************
  1119. *
  1120. * Interface to ac97-codec module
  1121. *
  1122. *
  1123. */
  1124. /**
  1125. * via_ac97_wait_idle - Wait until AC97 codec is not busy
  1126. * @card: Private info for specified board
  1127. *
  1128. * Sleep until the AC97 codec is no longer busy.
  1129. * Returns the final value read from the SGD
  1130. * register being polled.
  1131. */
  1132. static u8 via_ac97_wait_idle (struct via_info *card)
  1133. {
  1134. u8 tmp8;
  1135. int counter = VIA_COUNTER_LIMIT;
  1136. DPRINTK ("ENTER/EXIT\n");
  1137. assert (card != NULL);
  1138. assert (card->pdev != NULL);
  1139. do {
  1140. udelay (15);
  1141. tmp8 = inb (card->baseaddr + 0x83);
  1142. } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
  1143. if (tmp8 & VIA_CR83_BUSY)
  1144. printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
  1145. return tmp8;
  1146. }
  1147. /**
  1148. * via_ac97_read_reg - Read AC97 standard register
  1149. * @codec: Pointer to generic AC97 codec info
  1150. * @reg: Index of AC97 register to be read
  1151. *
  1152. * Read the value of a single AC97 codec register,
  1153. * as defined by the Intel AC97 specification.
  1154. *
  1155. * Defines the standard AC97 read-register operation
  1156. * required by the kernel's ac97_codec interface.
  1157. *
  1158. * Returns the 16-bit value stored in the specified
  1159. * register.
  1160. */
  1161. static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
  1162. {
  1163. unsigned long data;
  1164. struct via_info *card;
  1165. int counter;
  1166. DPRINTK ("ENTER\n");
  1167. assert (codec != NULL);
  1168. assert (codec->private_data != NULL);
  1169. card = codec->private_data;
  1170. spin_lock(&card->ac97_lock);
  1171. /* Every time we write to register 80 we cause a transaction.
  1172. The only safe way to clear the valid bit is to write it at
  1173. the same time as the command */
  1174. data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
  1175. outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
  1176. udelay (20);
  1177. for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
  1178. udelay (1);
  1179. if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
  1180. (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
  1181. goto out;
  1182. }
  1183. printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
  1184. goto err_out;
  1185. out:
  1186. /* Once the valid bit has become set, we must wait a complete AC97
  1187. frame before the data has settled. */
  1188. udelay(25);
  1189. data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
  1190. outb (0x02, card->baseaddr + 0x83);
  1191. if (((data & 0x007F0000) >> 16) == reg) {
  1192. DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
  1193. data, data & 0x0000FFFF);
  1194. spin_unlock(&card->ac97_lock);
  1195. return data & 0x0000FFFF;
  1196. }
  1197. printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
  1198. reg, ((data & 0x007F0000) >> 16));
  1199. err_out:
  1200. spin_unlock(&card->ac97_lock);
  1201. DPRINTK ("EXIT, returning 0\n");
  1202. return 0;
  1203. }
  1204. /**
  1205. * via_ac97_write_reg - Write AC97 standard register
  1206. * @codec: Pointer to generic AC97 codec info
  1207. * @reg: Index of AC97 register to be written
  1208. * @value: Value to be written to AC97 register
  1209. *
  1210. * Write the value of a single AC97 codec register,
  1211. * as defined by the Intel AC97 specification.
  1212. *
  1213. * Defines the standard AC97 write-register operation
  1214. * required by the kernel's ac97_codec interface.
  1215. */
  1216. static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
  1217. {
  1218. u32 data;
  1219. struct via_info *card;
  1220. int counter;
  1221. DPRINTK ("ENTER\n");
  1222. assert (codec != NULL);
  1223. assert (codec->private_data != NULL);
  1224. card = codec->private_data;
  1225. spin_lock(&card->ac97_lock);
  1226. data = (reg << 16) + value;
  1227. outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
  1228. udelay (10);
  1229. for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
  1230. if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
  1231. goto out;
  1232. udelay (15);
  1233. }
  1234. printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
  1235. out:
  1236. spin_unlock(&card->ac97_lock);
  1237. DPRINTK ("EXIT\n");
  1238. }
  1239. static int via_mixer_open (struct inode *inode, struct file *file)
  1240. {
  1241. int minor = iminor(inode);
  1242. struct via_info *card;
  1243. struct pci_dev *pdev = NULL;
  1244. struct pci_driver *drvr;
  1245. DPRINTK ("ENTER\n");
  1246. while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
  1247. drvr = pci_dev_driver (pdev);
  1248. if (drvr == &via_driver) {
  1249. assert (pci_get_drvdata (pdev) != NULL);
  1250. card = pci_get_drvdata (pdev);
  1251. if (card->ac97->dev_mixer == minor)
  1252. goto match;
  1253. }
  1254. }
  1255. DPRINTK ("EXIT, returning -ENODEV\n");
  1256. return -ENODEV;
  1257. match:
  1258. pci_dev_put(pdev);
  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 const 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)
  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);
  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)
  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 const 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_SIGBUS\n");
  1715. return NOPAGE_SIGBUS; /* 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_get_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. pci_dev_put(pdev);
  2644. if (nonblock) {
  2645. if (!mutex_trylock(&card->open_mutex)) {
  2646. DPRINTK ("EXIT, returning -EAGAIN\n");
  2647. return -EAGAIN;
  2648. }
  2649. } else {
  2650. if (mutex_lock_interruptible(&card->open_mutex)) {
  2651. DPRINTK ("EXIT, returning -ERESTARTSYS\n");
  2652. return -ERESTARTSYS;
  2653. }
  2654. }
  2655. file->private_data = card;
  2656. DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
  2657. /* handle input from analog source */
  2658. if (file->f_mode & FMODE_READ) {
  2659. chan = &card->ch_in;
  2660. via_chan_init (card, chan);
  2661. /* why is this forced to 16-bit stereo in all drivers? */
  2662. chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
  2663. chan->channels = 2;
  2664. // TO DO - use FIFO: via_capture_fifo(card, 1);
  2665. via_chan_pcm_fmt (chan, 0);
  2666. via_set_rate (card->ac97, chan, 44100);
  2667. }
  2668. /* handle output to analog source */
  2669. if (file->f_mode & FMODE_WRITE) {
  2670. chan = &card->ch_out;
  2671. via_chan_init (card, chan);
  2672. if (file->f_mode & FMODE_READ) {
  2673. /* if in duplex mode make the recording and playback channels
  2674. have the same settings */
  2675. chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
  2676. chan->channels = 2;
  2677. via_chan_pcm_fmt (chan, 0);
  2678. via_set_rate (card->ac97, chan, 44100);
  2679. } else {
  2680. if ((minor & 0xf) == SND_DEV_DSP16) {
  2681. chan->pcm_fmt = VIA_PCM_FMT_16BIT;
  2682. via_chan_pcm_fmt (chan, 0);
  2683. via_set_rate (card->ac97, chan, 44100);
  2684. } else {
  2685. via_chan_pcm_fmt (chan, 1);
  2686. via_set_rate (card->ac97, chan, 8000);
  2687. }
  2688. }
  2689. }
  2690. DPRINTK ("EXIT, returning 0\n");
  2691. return nonseekable_open(inode, file);
  2692. }
  2693. static int via_dsp_release(struct inode *inode, struct file *file)
  2694. {
  2695. struct via_info *card;
  2696. int nonblock = (file->f_flags & O_NONBLOCK);
  2697. int rc;
  2698. DPRINTK ("ENTER\n");
  2699. assert (file != NULL);
  2700. card = file->private_data;
  2701. assert (card != NULL);
  2702. rc = via_syscall_down (card, nonblock);
  2703. if (rc) {
  2704. DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
  2705. return rc;
  2706. }
  2707. if (file->f_mode & FMODE_WRITE) {
  2708. rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
  2709. if (rc && rc != -ERESTARTSYS) /* Nobody needs to know about ^C */
  2710. printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
  2711. via_chan_free (card, &card->ch_out);
  2712. via_chan_buffer_free(card, &card->ch_out);
  2713. }
  2714. if (file->f_mode & FMODE_READ) {
  2715. via_chan_free (card, &card->ch_in);
  2716. via_chan_buffer_free (card, &card->ch_in);
  2717. }
  2718. mutex_unlock(&card->syscall_mutex);
  2719. mutex_unlock(&card->open_mutex);
  2720. DPRINTK ("EXIT, returning 0\n");
  2721. return 0;
  2722. }
  2723. /****************************************************************
  2724. *
  2725. * Chip setup and kernel registration
  2726. *
  2727. *
  2728. */
  2729. static int __devinit via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  2730. {
  2731. #ifdef CONFIG_MIDI_VIA82CXXX
  2732. u8 r42;
  2733. #endif
  2734. int rc;
  2735. struct via_info *card;
  2736. static int printed_version;
  2737. DPRINTK ("ENTER\n");
  2738. if (printed_version++ == 0)
  2739. printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
  2740. rc = pci_enable_device (pdev);
  2741. if (rc)
  2742. goto err_out;
  2743. rc = pci_request_regions (pdev, "via82cxxx_audio");
  2744. if (rc)
  2745. goto err_out_disable;
  2746. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  2747. if (rc)
  2748. goto err_out_res;
  2749. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  2750. if (rc)
  2751. goto err_out_res;
  2752. card = kmalloc (sizeof (*card), GFP_KERNEL);
  2753. if (!card) {
  2754. printk (KERN_ERR PFX "out of memory, aborting\n");
  2755. rc = -ENOMEM;
  2756. goto err_out_res;
  2757. }
  2758. pci_set_drvdata (pdev, card);
  2759. memset (card, 0, sizeof (*card));
  2760. card->pdev = pdev;
  2761. card->baseaddr = pci_resource_start (pdev, 0);
  2762. card->card_num = via_num_cards++;
  2763. spin_lock_init (&card->lock);
  2764. spin_lock_init (&card->ac97_lock);
  2765. mutex_init(&card->syscall_mutex);
  2766. mutex_init(&card->open_mutex);
  2767. /* we must init these now, in case the intr handler needs them */
  2768. via_chan_init_defaults (card, &card->ch_out);
  2769. via_chan_init_defaults (card, &card->ch_in);
  2770. via_chan_init_defaults (card, &card->ch_fm);
  2771. /* if BAR 2 is present, chip is Rev H or later,
  2772. * which means it has a few extra features */
  2773. if (pci_resource_start (pdev, 2) > 0)
  2774. card->rev_h = 1;
  2775. /* Overkill for now, but more flexible done right */
  2776. card->intmask = id->driver_data;
  2777. card->legacy = !card->intmask;
  2778. card->sixchannel = id->driver_data;
  2779. if(card->sixchannel)
  2780. printk(KERN_INFO PFX "Six channel audio available\n");
  2781. if (pdev->irq < 1) {
  2782. printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
  2783. rc = -ENODEV;
  2784. goto err_out_kfree;
  2785. }
  2786. if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
  2787. printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
  2788. rc = -ENODEV;
  2789. goto err_out_kfree;
  2790. }
  2791. pci_set_master(pdev);
  2792. /*
  2793. * init AC97 mixer and codec
  2794. */
  2795. rc = via_ac97_init (card);
  2796. if (rc) {
  2797. printk (KERN_ERR PFX "AC97 init failed, aborting\n");
  2798. goto err_out_kfree;
  2799. }
  2800. /*
  2801. * init DSP device
  2802. */
  2803. rc = via_dsp_init (card);
  2804. if (rc) {
  2805. printk (KERN_ERR PFX "DSP device init failed, aborting\n");
  2806. goto err_out_have_mixer;
  2807. }
  2808. /*
  2809. * init and turn on interrupts, as the last thing we do
  2810. */
  2811. rc = via_interrupt_init (card);
  2812. if (rc) {
  2813. printk (KERN_ERR PFX "interrupt init failed, aborting\n");
  2814. goto err_out_have_dsp;
  2815. }
  2816. printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
  2817. card->card_num + 1, card->baseaddr, pdev->irq);
  2818. #ifdef CONFIG_MIDI_VIA82CXXX
  2819. /* Disable by default */
  2820. card->midi_info.io_base = 0;
  2821. if(card->legacy)
  2822. {
  2823. pci_read_config_byte (pdev, 0x42, &r42);
  2824. /* Disable MIDI interrupt */
  2825. pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
  2826. if (r42 & VIA_CR42_MIDI_ENABLE)
  2827. {
  2828. if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
  2829. card->midi_info.io_base = pci_resource_start (pdev, 2);
  2830. else /* Address selected by byte 0x43 */
  2831. {
  2832. u8 r43;
  2833. pci_read_config_byte (pdev, 0x43, &r43);
  2834. card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
  2835. }
  2836. card->midi_info.irq = -pdev->irq;
  2837. if (probe_uart401(& card->midi_info, THIS_MODULE))
  2838. {
  2839. card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
  2840. pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
  2841. printk("Enabled Via MIDI\n");
  2842. }
  2843. }
  2844. }
  2845. #endif
  2846. DPRINTK ("EXIT, returning 0\n");
  2847. return 0;
  2848. err_out_have_dsp:
  2849. via_dsp_cleanup (card);
  2850. err_out_have_mixer:
  2851. via_ac97_cleanup (card);
  2852. err_out_kfree:
  2853. #ifndef VIA_NDEBUG
  2854. memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
  2855. #endif
  2856. kfree (card);
  2857. err_out_res:
  2858. pci_release_regions (pdev);
  2859. err_out_disable:
  2860. pci_disable_device (pdev);
  2861. err_out:
  2862. pci_set_drvdata (pdev, NULL);
  2863. DPRINTK ("EXIT - returning %d\n", rc);
  2864. return rc;
  2865. }
  2866. static void __devexit via_remove_one (struct pci_dev *pdev)
  2867. {
  2868. struct via_info *card;
  2869. DPRINTK ("ENTER\n");
  2870. assert (pdev != NULL);
  2871. card = pci_get_drvdata (pdev);
  2872. assert (card != NULL);
  2873. #ifdef CONFIG_MIDI_VIA82CXXX
  2874. if (card->midi_info.io_base)
  2875. unload_uart401(&card->midi_info);
  2876. #endif
  2877. free_irq (card->pdev->irq, card);
  2878. via_dsp_cleanup (card);
  2879. via_ac97_cleanup (card);
  2880. #ifndef VIA_NDEBUG
  2881. memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
  2882. #endif
  2883. kfree (card);
  2884. pci_set_drvdata (pdev, NULL);
  2885. pci_release_regions (pdev);
  2886. pci_disable_device (pdev);
  2887. pci_set_power_state (pdev, 3); /* ...zzzzzz */
  2888. DPRINTK ("EXIT\n");
  2889. return;
  2890. }
  2891. /****************************************************************
  2892. *
  2893. * Driver initialization and cleanup
  2894. *
  2895. *
  2896. */
  2897. static int __init init_via82cxxx_audio(void)
  2898. {
  2899. int rc;
  2900. DPRINTK ("ENTER\n");
  2901. rc = pci_register_driver (&via_driver);
  2902. if (rc) {
  2903. DPRINTK ("EXIT, returning %d\n", rc);
  2904. return rc;
  2905. }
  2906. DPRINTK ("EXIT, returning 0\n");
  2907. return 0;
  2908. }
  2909. static void __exit cleanup_via82cxxx_audio(void)
  2910. {
  2911. DPRINTK ("ENTER\n");
  2912. pci_unregister_driver (&via_driver);
  2913. DPRINTK ("EXIT\n");
  2914. }
  2915. module_init(init_via82cxxx_audio);
  2916. module_exit(cleanup_via82cxxx_audio);
  2917. MODULE_AUTHOR("Jeff Garzik");
  2918. MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
  2919. MODULE_LICENSE("GPL");