via82cxxx_audio.c 89 KB

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