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