ali5455.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733
  1. /*
  2. * ALI ali5455 and friends ICH driver for Linux
  3. * LEI HU <Lei_Hu@ali.com.tw>
  4. *
  5. * Built from:
  6. * drivers/sound/i810_audio
  7. *
  8. * The ALi 5455 is similar but not quite identical to the Intel ICH
  9. * series of controllers. Its easier to keep the driver separated from
  10. * the i810 driver.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * ALi 5455 theory of operation
  28. *
  29. * The chipset provides three DMA channels that talk to an AC97
  30. * CODEC (AC97 is a digital/analog mixer standard). At its simplest
  31. * you get 48Khz audio with basic volume and mixer controls. At the
  32. * best you get rate adaption in the codec. We set the card up so
  33. * that we never take completion interrupts but instead keep the card
  34. * chasing its tail around a ring buffer. This is needed for mmap
  35. * mode audio and happens to work rather well for non-mmap modes too.
  36. *
  37. * The board has one output channel for PCM audio (supported) and
  38. * a stereo line in and mono microphone input. Again these are normally
  39. * locked to 48Khz only. Right now recording is not finished.
  40. *
  41. * There is no midi support, no synth support. Use timidity. To get
  42. * esd working you need to use esd -r 48000 as it won't probe 48KHz
  43. * by default. mpg123 can't handle 48Khz only audio so use xmms.
  44. *
  45. * If you need to force a specific rate set the clocking= option
  46. *
  47. */
  48. #include <linux/module.h>
  49. #include <linux/string.h>
  50. #include <linux/ctype.h>
  51. #include <linux/ioport.h>
  52. #include <linux/sched.h>
  53. #include <linux/delay.h>
  54. #include <linux/sound.h>
  55. #include <linux/slab.h>
  56. #include <linux/soundcard.h>
  57. #include <linux/pci.h>
  58. #include <asm/io.h>
  59. #include <asm/dma.h>
  60. #include <linux/init.h>
  61. #include <linux/poll.h>
  62. #include <linux/spinlock.h>
  63. #include <linux/smp_lock.h>
  64. #include <linux/ac97_codec.h>
  65. #include <linux/interrupt.h>
  66. #include <asm/uaccess.h>
  67. #ifndef PCI_DEVICE_ID_ALI_5455
  68. #define PCI_DEVICE_ID_ALI_5455 0x5455
  69. #endif
  70. #ifndef PCI_VENDOR_ID_ALI
  71. #define PCI_VENDOR_ID_ALI 0x10b9
  72. #endif
  73. static int strict_clocking = 0;
  74. static unsigned int clocking = 0;
  75. static unsigned int codec_pcmout_share_spdif_locked = 0;
  76. static unsigned int codec_independent_spdif_locked = 0;
  77. static unsigned int controller_pcmout_share_spdif_locked = 0;
  78. static unsigned int controller_independent_spdif_locked = 0;
  79. static unsigned int globel = 0;
  80. #define ADC_RUNNING 1
  81. #define DAC_RUNNING 2
  82. #define CODEC_SPDIFOUT_RUNNING 8
  83. #define CONTROLLER_SPDIFOUT_RUNNING 4
  84. #define SPDIF_ENABLE_OUTPUT 4 /* bits 0,1 are PCM */
  85. #define ALI5455_FMT_16BIT 1
  86. #define ALI5455_FMT_STEREO 2
  87. #define ALI5455_FMT_MASK 3
  88. #define SPDIF_ON 0x0004
  89. #define SURR_ON 0x0010
  90. #define CENTER_LFE_ON 0x0020
  91. #define VOL_MUTED 0x8000
  92. #define ALI_SPDIF_OUT_CH_STATUS 0xbf
  93. /* the 810's array of pointers to data buffers */
  94. struct sg_item {
  95. #define BUSADDR_MASK 0xFFFFFFFE
  96. u32 busaddr;
  97. #define CON_IOC 0x80000000 /* interrupt on completion */
  98. #define CON_BUFPAD 0x40000000 /* pad underrun with last sample, else 0 */
  99. #define CON_BUFLEN_MASK 0x0000ffff /* buffer length in samples */
  100. u32 control;
  101. };
  102. /* an instance of the ali channel */
  103. #define SG_LEN 32
  104. struct ali_channel {
  105. /* these sg guys should probably be allocated
  106. separately as nocache. Must be 8 byte aligned */
  107. struct sg_item sg[SG_LEN]; /* 32*8 */
  108. u32 offset; /* 4 */
  109. u32 port; /* 4 */
  110. u32 used;
  111. u32 num;
  112. };
  113. /*
  114. * we have 3 separate dma engines. pcm in, pcm out, and mic.
  115. * each dma engine has controlling registers. These goofy
  116. * names are from the datasheet, but make it easy to write
  117. * code while leafing through it.
  118. */
  119. #define ENUM_ENGINE(PRE,DIG) \
  120. enum { \
  121. PRE##_BDBAR = 0x##DIG##0, /* Buffer Descriptor list Base Address */ \
  122. PRE##_CIV = 0x##DIG##4, /* Current Index Value */ \
  123. PRE##_LVI = 0x##DIG##5, /* Last Valid Index */ \
  124. PRE##_SR = 0x##DIG##6, /* Status Register */ \
  125. PRE##_PICB = 0x##DIG##8, /* Position In Current Buffer */ \
  126. PRE##_CR = 0x##DIG##b /* Control Register */ \
  127. }
  128. ENUM_ENGINE(OFF, 0); /* Offsets */
  129. ENUM_ENGINE(PI, 4); /* PCM In */
  130. ENUM_ENGINE(PO, 5); /* PCM Out */
  131. ENUM_ENGINE(MC, 6); /* Mic In */
  132. ENUM_ENGINE(CODECSPDIFOUT, 7); /* CODEC SPDIF OUT */
  133. ENUM_ENGINE(CONTROLLERSPDIFIN, A); /* CONTROLLER SPDIF In */
  134. ENUM_ENGINE(CONTROLLERSPDIFOUT, B); /* CONTROLLER SPDIF OUT */
  135. enum {
  136. ALI_SCR = 0x00, /* System Control Register */
  137. ALI_SSR = 0x04, /* System Status Register */
  138. ALI_DMACR = 0x08, /* DMA Control Register */
  139. ALI_FIFOCR1 = 0x0c, /* FIFO Control Register 1 */
  140. ALI_INTERFACECR = 0x10, /* Interface Control Register */
  141. ALI_INTERRUPTCR = 0x14, /* Interrupt control Register */
  142. ALI_INTERRUPTSR = 0x18, /* Interrupt Status Register */
  143. ALI_FIFOCR2 = 0x1c, /* FIFO Control Register 2 */
  144. ALI_CPR = 0x20, /* Command Port Register */
  145. ALI_SPR = 0x24, /* Status Port Register */
  146. ALI_FIFOCR3 = 0x2c, /* FIFO Control Register 3 */
  147. ALI_TTSR = 0x30, /* Transmit Tag Slot Register */
  148. ALI_RTSR = 0x34, /* Receive Tag Slot Register */
  149. ALI_CSPSR = 0x38, /* Command/Status Port Status Register */
  150. ALI_CAS = 0x3c, /* Codec Write Semaphore Register */
  151. ALI_SPDIFCSR = 0xf8, /* spdif channel status register */
  152. ALI_SPDIFICS = 0xfc /* spdif interface control/status */
  153. };
  154. // x-status register(x:pcm in ,pcm out, mic in,)
  155. /* interrupts for a dma engine */
  156. #define DMA_INT_FIFO (1<<4) /* fifo under/over flow */
  157. #define DMA_INT_COMPLETE (1<<3) /* buffer read/write complete and ioc set */
  158. #define DMA_INT_LVI (1<<2) /* last valid done */
  159. #define DMA_INT_CELV (1<<1) /* last valid is current */
  160. #define DMA_INT_DCH (1) /* DMA Controller Halted (happens on LVI interrupts) */ //not eqult intel
  161. #define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
  162. /* interrupts for the whole chip */// by interrupt status register finish
  163. #define INT_SPDIFOUT (1<<23) /* controller spdif out INTERRUPT */
  164. #define INT_SPDIFIN (1<<22)
  165. #define INT_CODECSPDIFOUT (1<<19)
  166. #define INT_MICIN (1<<18)
  167. #define INT_PCMOUT (1<<17)
  168. #define INT_PCMIN (1<<16)
  169. #define INT_CPRAIS (1<<7)
  170. #define INT_SPRAIS (1<<5)
  171. #define INT_GPIO (1<<1)
  172. #define INT_MASK (INT_SPDIFOUT|INT_CODECSPDIFOUT|INT_MICIN|INT_PCMOUT|INT_PCMIN)
  173. #define DRIVER_VERSION "0.02ac"
  174. /* magic numbers to protect our data structures */
  175. #define ALI5455_CARD_MAGIC 0x5072696E /* "Prin" */
  176. #define ALI5455_STATE_MAGIC 0x63657373 /* "cess" */
  177. #define ALI5455_DMA_MASK 0xffffffff /* DMA buffer mask for pci_alloc_consist */
  178. #define NR_HW_CH 5 //I think 5 channel
  179. /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
  180. #define NR_AC97 2
  181. /* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
  182. /* stream at a minimum for this card to be happy */
  183. static const unsigned sample_size[] = { 1, 2, 2, 4 };
  184. /* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
  185. /* values are one less than might be expected */
  186. static const unsigned sample_shift[] = { -1, 0, 0, 1 };
  187. #define ALI5455
  188. static char *card_names[] = {
  189. "ALI 5455"
  190. };
  191. static struct pci_device_id ali_pci_tbl[] = {
  192. {PCI_VENDOR_ID_ALI, PCI_DEVICE_ID_ALI_5455,
  193. PCI_ANY_ID, PCI_ANY_ID, 0, 0, ALI5455},
  194. {0,}
  195. };
  196. MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
  197. #ifdef CONFIG_PM
  198. #define PM_SUSPENDED(card) (card->pm_suspended)
  199. #else
  200. #define PM_SUSPENDED(card) (0)
  201. #endif
  202. /* "software" or virtual channel, an instance of opened /dev/dsp */
  203. struct ali_state {
  204. unsigned int magic;
  205. struct ali_card *card; /* Card info */
  206. /* single open lock mechanism, only used for recording */
  207. struct semaphore open_sem;
  208. wait_queue_head_t open_wait;
  209. /* file mode */
  210. mode_t open_mode;
  211. /* virtual channel number */
  212. int virt;
  213. #ifdef CONFIG_PM
  214. unsigned int pm_saved_dac_rate, pm_saved_adc_rate;
  215. #endif
  216. struct dmabuf {
  217. /* wave sample stuff */
  218. unsigned int rate;
  219. unsigned char fmt, enable, trigger;
  220. /* hardware channel */
  221. struct ali_channel *read_channel;
  222. struct ali_channel *write_channel;
  223. struct ali_channel *codec_spdifout_channel;
  224. struct ali_channel *controller_spdifout_channel;
  225. /* OSS buffer management stuff */
  226. void *rawbuf;
  227. dma_addr_t dma_handle;
  228. unsigned buforder;
  229. unsigned numfrag;
  230. unsigned fragshift;
  231. /* our buffer acts like a circular ring */
  232. unsigned hwptr; /* where dma last started, updated by update_ptr */
  233. unsigned swptr; /* where driver last clear/filled, updated by read/write */
  234. int count; /* bytes to be consumed or been generated by dma machine */
  235. unsigned total_bytes; /* total bytes dmaed by hardware */
  236. unsigned error; /* number of over/underruns */
  237. wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
  238. /* redundant, but makes calculations easier */
  239. /* what the hardware uses */
  240. unsigned dmasize;
  241. unsigned fragsize;
  242. unsigned fragsamples;
  243. /* what we tell the user to expect */
  244. unsigned userfrags;
  245. unsigned userfragsize;
  246. /* OSS stuff */
  247. unsigned mapped:1;
  248. unsigned ready:1;
  249. unsigned update_flag;
  250. unsigned ossfragsize;
  251. unsigned ossmaxfrags;
  252. unsigned subdivision;
  253. } dmabuf;
  254. };
  255. struct ali_card {
  256. struct ali_channel channel[5];
  257. unsigned int magic;
  258. /* We keep ali5455 cards in a linked list */
  259. struct ali_card *next;
  260. /* The ali has a certain amount of cross channel interaction
  261. so we use a single per card lock */
  262. spinlock_t lock;
  263. spinlock_t ac97_lock;
  264. /* PCI device stuff */
  265. struct pci_dev *pci_dev;
  266. u16 pci_id;
  267. #ifdef CONFIG_PM
  268. u16 pm_suspended;
  269. int pm_saved_mixer_settings[SOUND_MIXER_NRDEVICES][NR_AC97];
  270. #endif
  271. /* soundcore stuff */
  272. int dev_audio;
  273. /* structures for abstraction of hardware facilities, codecs, banks and channels */
  274. struct ac97_codec *ac97_codec[NR_AC97];
  275. struct ali_state *states[NR_HW_CH];
  276. u16 ac97_features;
  277. u16 ac97_status;
  278. u16 channels;
  279. /* hardware resources */
  280. unsigned long iobase;
  281. u32 irq;
  282. /* Function support */
  283. struct ali_channel *(*alloc_pcm_channel) (struct ali_card *);
  284. struct ali_channel *(*alloc_rec_pcm_channel) (struct ali_card *);
  285. struct ali_channel *(*alloc_rec_mic_channel) (struct ali_card *);
  286. struct ali_channel *(*alloc_codec_spdifout_channel) (struct ali_card *);
  287. struct ali_channel *(*alloc_controller_spdifout_channel) (struct ali_card *);
  288. void (*free_pcm_channel) (struct ali_card *, int chan);
  289. /* We have a *very* long init time possibly, so use this to block */
  290. /* attempts to open our devices before we are ready (stops oops'es) */
  291. int initializing;
  292. };
  293. static struct ali_card *devs = NULL;
  294. static int ali_open_mixdev(struct inode *inode, struct file *file);
  295. static int ali_ioctl_mixdev(struct inode *inode, struct file *file,
  296. unsigned int cmd, unsigned long arg);
  297. static u16 ali_ac97_get(struct ac97_codec *dev, u8 reg);
  298. static void ali_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
  299. static struct ali_channel *ali_alloc_pcm_channel(struct ali_card *card)
  300. {
  301. if (card->channel[1].used == 1)
  302. return NULL;
  303. card->channel[1].used = 1;
  304. return &card->channel[1];
  305. }
  306. static struct ali_channel *ali_alloc_rec_pcm_channel(struct ali_card *card)
  307. {
  308. if (card->channel[0].used == 1)
  309. return NULL;
  310. card->channel[0].used = 1;
  311. return &card->channel[0];
  312. }
  313. static struct ali_channel *ali_alloc_rec_mic_channel(struct ali_card *card)
  314. {
  315. if (card->channel[2].used == 1)
  316. return NULL;
  317. card->channel[2].used = 1;
  318. return &card->channel[2];
  319. }
  320. static struct ali_channel *ali_alloc_codec_spdifout_channel(struct ali_card *card)
  321. {
  322. if (card->channel[3].used == 1)
  323. return NULL;
  324. card->channel[3].used = 1;
  325. return &card->channel[3];
  326. }
  327. static struct ali_channel *ali_alloc_controller_spdifout_channel(struct ali_card *card)
  328. {
  329. if (card->channel[4].used == 1)
  330. return NULL;
  331. card->channel[4].used = 1;
  332. return &card->channel[4];
  333. }
  334. static void ali_free_pcm_channel(struct ali_card *card, int channel)
  335. {
  336. card->channel[channel].used = 0;
  337. }
  338. //add support codec spdif out
  339. static int ali_valid_spdif_rate(struct ac97_codec *codec, int rate)
  340. {
  341. unsigned long id = 0L;
  342. id = (ali_ac97_get(codec, AC97_VENDOR_ID1) << 16);
  343. id |= ali_ac97_get(codec, AC97_VENDOR_ID2) & 0xffff;
  344. switch (id) {
  345. case 0x41445361: /* AD1886 */
  346. if (rate == 48000) {
  347. return 1;
  348. }
  349. break;
  350. case 0x414c4720: /* ALC650 */
  351. if (rate == 48000) {
  352. return 1;
  353. }
  354. break;
  355. default: /* all other codecs, until we know otherwiae */
  356. if (rate == 48000 || rate == 44100 || rate == 32000) {
  357. return 1;
  358. }
  359. break;
  360. }
  361. return (0);
  362. }
  363. /* ali_set_spdif_output
  364. *
  365. * Configure the S/PDIF output transmitter. When we turn on
  366. * S/PDIF, we turn off the analog output. This may not be
  367. * the right thing to do.
  368. *
  369. * Assumptions:
  370. * The DSP sample rate must already be set to a supported
  371. * S/PDIF rate (32kHz, 44.1kHz, or 48kHz) or we abort.
  372. */
  373. static void ali_set_spdif_output(struct ali_state *state, int slots,
  374. int rate)
  375. {
  376. int vol;
  377. int aud_reg;
  378. struct ac97_codec *codec = state->card->ac97_codec[0];
  379. if (!(state->card->ac97_features & 4)) {
  380. state->card->ac97_status &= ~SPDIF_ON;
  381. } else {
  382. if (slots == -1) { /* Turn off S/PDIF */
  383. aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
  384. ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
  385. /* If the volume wasn't muted before we turned on S/PDIF, unmute it */
  386. if (!(state->card->ac97_status & VOL_MUTED)) {
  387. aud_reg = ali_ac97_get(codec, AC97_MASTER_VOL_STEREO);
  388. ali_ac97_set(codec, AC97_MASTER_VOL_STEREO,
  389. (aud_reg & ~VOL_MUTED));
  390. }
  391. state->card->ac97_status &= ~(VOL_MUTED | SPDIF_ON);
  392. return;
  393. }
  394. vol = ali_ac97_get(codec, AC97_MASTER_VOL_STEREO);
  395. state->card->ac97_status = vol & VOL_MUTED;
  396. /* Set S/PDIF transmitter sample rate */
  397. aud_reg = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
  398. switch (rate) {
  399. case 32000:
  400. aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
  401. break;
  402. case 44100:
  403. aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
  404. break;
  405. case 48000:
  406. aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
  407. break;
  408. default:
  409. /* turn off S/PDIF */
  410. aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
  411. ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
  412. state->card->ac97_status &= ~SPDIF_ON;
  413. return;
  414. }
  415. ali_ac97_set(codec, AC97_SPDIF_CONTROL, aud_reg);
  416. aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
  417. aud_reg = (aud_reg & AC97_EA_SLOT_MASK) | slots | AC97_EA_SPDIF;
  418. ali_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
  419. aud_reg = ali_ac97_get(codec, AC97_POWER_CONTROL);
  420. aud_reg |= 0x0002;
  421. ali_ac97_set(codec, AC97_POWER_CONTROL, aud_reg);
  422. udelay(1);
  423. state->card->ac97_status |= SPDIF_ON;
  424. /* Check to make sure the configuration is valid */
  425. aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
  426. if (!(aud_reg & 0x0400)) {
  427. /* turn off S/PDIF */
  428. ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
  429. state->card->ac97_status &= ~SPDIF_ON;
  430. return;
  431. }
  432. if (codec_independent_spdif_locked > 0) {
  433. aud_reg = ali_ac97_get(codec, 0x6a);
  434. ali_ac97_set(codec, 0x6a, (aud_reg & 0xefff));
  435. }
  436. /* Mute the analog output */
  437. /* Should this only mute the PCM volume??? */
  438. }
  439. }
  440. /* ali_set_dac_channels
  441. *
  442. * Configure the codec's multi-channel DACs
  443. *
  444. * The logic is backwards. Setting the bit to 1 turns off the DAC.
  445. *
  446. * What about the ICH? We currently configure it using the
  447. * SNDCTL_DSP_CHANNELS ioctl. If we're turnning on the DAC,
  448. * does that imply that we want the ICH set to support
  449. * these channels?
  450. *
  451. * TODO:
  452. * vailidate that the codec really supports these DACs
  453. * before turning them on.
  454. */
  455. static void ali_set_dac_channels(struct ali_state *state, int channel)
  456. {
  457. int aud_reg;
  458. struct ac97_codec *codec = state->card->ac97_codec[0];
  459. aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
  460. aud_reg |= AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK;
  461. state->card->ac97_status &= ~(SURR_ON | CENTER_LFE_ON);
  462. switch (channel) {
  463. case 2: /* always enabled */
  464. break;
  465. case 4:
  466. aud_reg &= ~AC97_EA_PRJ;
  467. state->card->ac97_status |= SURR_ON;
  468. break;
  469. case 6:
  470. aud_reg &= ~(AC97_EA_PRJ | AC97_EA_PRI | AC97_EA_PRK);
  471. state->card->ac97_status |= SURR_ON | CENTER_LFE_ON;
  472. break;
  473. default:
  474. break;
  475. }
  476. ali_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
  477. }
  478. /* set playback sample rate */
  479. static unsigned int ali_set_dac_rate(struct ali_state *state,
  480. unsigned int rate)
  481. {
  482. struct dmabuf *dmabuf = &state->dmabuf;
  483. u32 new_rate;
  484. struct ac97_codec *codec = state->card->ac97_codec[0];
  485. if (!(state->card->ac97_features & 0x0001)) {
  486. dmabuf->rate = clocking;
  487. return clocking;
  488. }
  489. if (rate > 48000)
  490. rate = 48000;
  491. if (rate < 8000)
  492. rate = 8000;
  493. dmabuf->rate = rate;
  494. /*
  495. * Adjust for misclocked crap
  496. */
  497. rate = (rate * clocking) / 48000;
  498. if (strict_clocking && rate < 8000) {
  499. rate = 8000;
  500. dmabuf->rate = (rate * 48000) / clocking;
  501. }
  502. new_rate = ac97_set_dac_rate(codec, rate);
  503. if (new_rate != rate) {
  504. dmabuf->rate = (new_rate * 48000) / clocking;
  505. }
  506. rate = new_rate;
  507. return dmabuf->rate;
  508. }
  509. /* set recording sample rate */
  510. static unsigned int ali_set_adc_rate(struct ali_state *state,
  511. unsigned int rate)
  512. {
  513. struct dmabuf *dmabuf = &state->dmabuf;
  514. u32 new_rate;
  515. struct ac97_codec *codec = state->card->ac97_codec[0];
  516. if (!(state->card->ac97_features & 0x0001)) {
  517. dmabuf->rate = clocking;
  518. return clocking;
  519. }
  520. if (rate > 48000)
  521. rate = 48000;
  522. if (rate < 8000)
  523. rate = 8000;
  524. dmabuf->rate = rate;
  525. /*
  526. * Adjust for misclocked crap
  527. */
  528. rate = (rate * clocking) / 48000;
  529. if (strict_clocking && rate < 8000) {
  530. rate = 8000;
  531. dmabuf->rate = (rate * 48000) / clocking;
  532. }
  533. new_rate = ac97_set_adc_rate(codec, rate);
  534. if (new_rate != rate) {
  535. dmabuf->rate = (new_rate * 48000) / clocking;
  536. rate = new_rate;
  537. }
  538. return dmabuf->rate;
  539. }
  540. /* set codec independent spdifout sample rate */
  541. static unsigned int ali_set_codecspdifout_rate(struct ali_state *state,
  542. unsigned int rate)
  543. {
  544. struct dmabuf *dmabuf = &state->dmabuf;
  545. if (!(state->card->ac97_features & 0x0001)) {
  546. dmabuf->rate = clocking;
  547. return clocking;
  548. }
  549. if (rate > 48000)
  550. rate = 48000;
  551. if (rate < 8000)
  552. rate = 8000;
  553. dmabuf->rate = rate;
  554. return dmabuf->rate;
  555. }
  556. /* set controller independent spdif out function sample rate */
  557. static void ali_set_spdifout_rate(struct ali_state *state,
  558. unsigned int rate)
  559. {
  560. unsigned char ch_st_sel;
  561. unsigned short status_rate;
  562. switch (rate) {
  563. case 44100:
  564. status_rate = 0;
  565. break;
  566. case 32000:
  567. status_rate = 0x300;
  568. break;
  569. case 48000:
  570. default:
  571. status_rate = 0x200;
  572. break;
  573. }
  574. ch_st_sel = inb(state->card->iobase + ALI_SPDIFICS) & ALI_SPDIF_OUT_CH_STATUS; //select spdif_out
  575. ch_st_sel |= 0x80; //select right
  576. outb(ch_st_sel, (state->card->iobase + ALI_SPDIFICS));
  577. outb(status_rate | 0x20, (state->card->iobase + ALI_SPDIFCSR + 2));
  578. ch_st_sel &= (~0x80); //select left
  579. outb(ch_st_sel, (state->card->iobase + ALI_SPDIFICS));
  580. outw(status_rate | 0x10, (state->card->iobase + ALI_SPDIFCSR + 2));
  581. }
  582. /* get current playback/recording dma buffer pointer (byte offset from LBA),
  583. called with spinlock held! */
  584. static inline unsigned ali_get_dma_addr(struct ali_state *state, int rec)
  585. {
  586. struct dmabuf *dmabuf = &state->dmabuf;
  587. unsigned int civ, offset, port, port_picb;
  588. unsigned int data;
  589. if (!dmabuf->enable)
  590. return 0;
  591. if (rec == 1)
  592. port = state->card->iobase + dmabuf->read_channel->port;
  593. else if (rec == 2)
  594. port = state->card->iobase + dmabuf->codec_spdifout_channel->port;
  595. else if (rec == 3)
  596. port = state->card->iobase + dmabuf->controller_spdifout_channel->port;
  597. else
  598. port = state->card->iobase + dmabuf->write_channel->port;
  599. port_picb = port + OFF_PICB;
  600. do {
  601. civ = inb(port + OFF_CIV) & 31;
  602. offset = inw(port_picb);
  603. /* Must have a delay here! */
  604. if (offset == 0)
  605. udelay(1);
  606. /* Reread both registers and make sure that that total
  607. * offset from the first reading to the second is 0.
  608. * There is an issue with SiS hardware where it will count
  609. * picb down to 0, then update civ to the next value,
  610. * then set the new picb to fragsize bytes. We can catch
  611. * it between the civ update and the picb update, making
  612. * it look as though we are 1 fragsize ahead of where we
  613. * are. The next to we get the address though, it will
  614. * be back in thdelay is more than long enough
  615. * that we won't have to worry about the chip still being
  616. * out of sync with reality ;-)
  617. */
  618. } while (civ != (inb(port + OFF_CIV) & 31) || offset != inw(port_picb));
  619. data = ((civ + 1) * dmabuf->fragsize - (2 * offset)) % dmabuf->dmasize;
  620. if (inw(port_picb) == 0)
  621. data -= 2048;
  622. return data;
  623. }
  624. /* Stop recording (lock held) */
  625. static inline void __stop_adc(struct ali_state *state)
  626. {
  627. struct dmabuf *dmabuf = &state->dmabuf;
  628. struct ali_card *card = state->card;
  629. dmabuf->enable &= ~ADC_RUNNING;
  630. outl((1 << 18) | (1 << 16), card->iobase + ALI_DMACR);
  631. udelay(1);
  632. outb(0, card->iobase + PI_CR);
  633. while (inb(card->iobase + PI_CR) != 0);
  634. // now clear any latent interrupt bits (like the halt bit)
  635. outb(inb(card->iobase + PI_SR) | 0x001e, card->iobase + PI_SR);
  636. outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_PCMIN, card->iobase + ALI_INTERRUPTSR);
  637. }
  638. static void stop_adc(struct ali_state *state)
  639. {
  640. struct ali_card *card = state->card;
  641. unsigned long flags;
  642. spin_lock_irqsave(&card->lock, flags);
  643. __stop_adc(state);
  644. spin_unlock_irqrestore(&card->lock, flags);
  645. }
  646. static inline void __start_adc(struct ali_state *state)
  647. {
  648. struct dmabuf *dmabuf = &state->dmabuf;
  649. if (dmabuf->count < dmabuf->dmasize && dmabuf->ready
  650. && !dmabuf->enable && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  651. dmabuf->enable |= ADC_RUNNING;
  652. outb((1 << 4) | (1 << 2), state->card->iobase + PI_CR);
  653. if (state->card->channel[0].used == 1)
  654. outl(1, state->card->iobase + ALI_DMACR); // DMA CONTROL REGISTRER
  655. udelay(100);
  656. if (state->card->channel[2].used == 1)
  657. outl((1 << 2), state->card->iobase + ALI_DMACR); //DMA CONTROL REGISTER
  658. udelay(100);
  659. }
  660. }
  661. static void start_adc(struct ali_state *state)
  662. {
  663. struct ali_card *card = state->card;
  664. unsigned long flags;
  665. spin_lock_irqsave(&card->lock, flags);
  666. __start_adc(state);
  667. spin_unlock_irqrestore(&card->lock, flags);
  668. }
  669. /* stop playback (lock held) */
  670. static inline void __stop_dac(struct ali_state *state)
  671. {
  672. struct dmabuf *dmabuf = &state->dmabuf;
  673. struct ali_card *card = state->card;
  674. dmabuf->enable &= ~DAC_RUNNING;
  675. outl(0x00020000, card->iobase + 0x08);
  676. outb(0, card->iobase + PO_CR);
  677. while (inb(card->iobase + PO_CR) != 0)
  678. cpu_relax();
  679. outb(inb(card->iobase + PO_SR) | 0x001e, card->iobase + PO_SR);
  680. outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_PCMOUT, card->iobase + ALI_INTERRUPTSR);
  681. }
  682. static void stop_dac(struct ali_state *state)
  683. {
  684. struct ali_card *card = state->card;
  685. unsigned long flags;
  686. spin_lock_irqsave(&card->lock, flags);
  687. __stop_dac(state);
  688. spin_unlock_irqrestore(&card->lock, flags);
  689. }
  690. static inline void __start_dac(struct ali_state *state)
  691. {
  692. struct dmabuf *dmabuf = &state->dmabuf;
  693. if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
  694. (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  695. dmabuf->enable |= DAC_RUNNING;
  696. outb((1 << 4) | (1 << 2), state->card->iobase + PO_CR);
  697. outl((1 << 1), state->card->iobase + 0x08); //dma control register
  698. }
  699. }
  700. static void start_dac(struct ali_state *state)
  701. {
  702. struct ali_card *card = state->card;
  703. unsigned long flags;
  704. spin_lock_irqsave(&card->lock, flags);
  705. __start_dac(state);
  706. spin_unlock_irqrestore(&card->lock, flags);
  707. }
  708. /* stop codec and controller spdif out (lock held) */
  709. static inline void __stop_spdifout(struct ali_state *state)
  710. {
  711. struct dmabuf *dmabuf = &state->dmabuf;
  712. struct ali_card *card = state->card;
  713. if (codec_independent_spdif_locked > 0) {
  714. dmabuf->enable &= ~CODEC_SPDIFOUT_RUNNING;
  715. outl((1 << 19), card->iobase + 0x08);
  716. outb(0, card->iobase + CODECSPDIFOUT_CR);
  717. while (inb(card->iobase + CODECSPDIFOUT_CR) != 0)
  718. cpu_relax();
  719. outb(inb(card->iobase + CODECSPDIFOUT_SR) | 0x001e, card->iobase + CODECSPDIFOUT_SR);
  720. outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_CODECSPDIFOUT, card->iobase + ALI_INTERRUPTSR);
  721. } else {
  722. if (controller_independent_spdif_locked > 0) {
  723. dmabuf->enable &= ~CONTROLLER_SPDIFOUT_RUNNING;
  724. outl((1 << 23), card->iobase + 0x08);
  725. outb(0, card->iobase + CONTROLLERSPDIFOUT_CR);
  726. while (inb(card->iobase + CONTROLLERSPDIFOUT_CR) != 0)
  727. cpu_relax();
  728. outb(inb(card->iobase + CONTROLLERSPDIFOUT_SR) | 0x001e, card->iobase + CONTROLLERSPDIFOUT_SR);
  729. outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_SPDIFOUT, card->iobase + ALI_INTERRUPTSR);
  730. }
  731. }
  732. }
  733. static void stop_spdifout(struct ali_state *state)
  734. {
  735. struct ali_card *card = state->card;
  736. unsigned long flags;
  737. spin_lock_irqsave(&card->lock, flags);
  738. __stop_spdifout(state);
  739. spin_unlock_irqrestore(&card->lock, flags);
  740. }
  741. static inline void __start_spdifout(struct ali_state *state)
  742. {
  743. struct dmabuf *dmabuf = &state->dmabuf;
  744. if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
  745. (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
  746. if (codec_independent_spdif_locked > 0) {
  747. dmabuf->enable |= CODEC_SPDIFOUT_RUNNING;
  748. outb((1 << 4) | (1 << 2), state->card->iobase + CODECSPDIFOUT_CR);
  749. outl((1 << 3), state->card->iobase + 0x08); //dma control register
  750. } else {
  751. if (controller_independent_spdif_locked > 0) {
  752. dmabuf->enable |= CONTROLLER_SPDIFOUT_RUNNING;
  753. outb((1 << 4) | (1 << 2), state->card->iobase + CONTROLLERSPDIFOUT_CR);
  754. outl((1 << 7), state->card->iobase + 0x08); //dma control register
  755. }
  756. }
  757. }
  758. }
  759. static void start_spdifout(struct ali_state *state)
  760. {
  761. struct ali_card *card = state->card;
  762. unsigned long flags;
  763. spin_lock_irqsave(&card->lock, flags);
  764. __start_spdifout(state);
  765. spin_unlock_irqrestore(&card->lock, flags);
  766. }
  767. #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
  768. #define DMABUF_MINORDER 1
  769. /* allocate DMA buffer, playback , recording,spdif out buffer should be allocated separately */
  770. static int alloc_dmabuf(struct ali_state *state)
  771. {
  772. struct dmabuf *dmabuf = &state->dmabuf;
  773. void *rawbuf = NULL;
  774. int order, size;
  775. struct page *page, *pend;
  776. /* If we don't have any oss frag params, then use our default ones */
  777. if (dmabuf->ossmaxfrags == 0)
  778. dmabuf->ossmaxfrags = 4;
  779. if (dmabuf->ossfragsize == 0)
  780. dmabuf->ossfragsize = (PAGE_SIZE << DMABUF_DEFAULTORDER) / dmabuf->ossmaxfrags;
  781. size = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
  782. if (dmabuf->rawbuf && (PAGE_SIZE << dmabuf->buforder) == size)
  783. return 0;
  784. /* alloc enough to satisfy the oss params */
  785. for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
  786. if ((PAGE_SIZE << order) > size)
  787. continue;
  788. if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
  789. PAGE_SIZE << order,
  790. &dmabuf->dma_handle)))
  791. break;
  792. }
  793. if (!rawbuf)
  794. return -ENOMEM;
  795. dmabuf->ready = dmabuf->mapped = 0;
  796. dmabuf->rawbuf = rawbuf;
  797. dmabuf->buforder = order;
  798. /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
  799. pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
  800. for (page = virt_to_page(rawbuf); page <= pend; page++)
  801. SetPageReserved(page);
  802. return 0;
  803. }
  804. /* free DMA buffer */
  805. static void dealloc_dmabuf(struct ali_state *state)
  806. {
  807. struct dmabuf *dmabuf = &state->dmabuf;
  808. struct page *page, *pend;
  809. if (dmabuf->rawbuf) {
  810. /* undo marking the pages as reserved */
  811. pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
  812. for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
  813. ClearPageReserved(page);
  814. pci_free_consistent(state->card->pci_dev,
  815. PAGE_SIZE << dmabuf->buforder,
  816. dmabuf->rawbuf, dmabuf->dma_handle);
  817. }
  818. dmabuf->rawbuf = NULL;
  819. dmabuf->mapped = dmabuf->ready = 0;
  820. }
  821. static int prog_dmabuf(struct ali_state *state, unsigned rec)
  822. {
  823. struct dmabuf *dmabuf = &state->dmabuf;
  824. struct ali_channel *c = NULL;
  825. struct sg_item *sg;
  826. unsigned long flags;
  827. int ret;
  828. unsigned fragint;
  829. int i;
  830. spin_lock_irqsave(&state->card->lock, flags);
  831. if (dmabuf->enable & DAC_RUNNING)
  832. __stop_dac(state);
  833. if (dmabuf->enable & ADC_RUNNING)
  834. __stop_adc(state);
  835. if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  836. __stop_spdifout(state);
  837. if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  838. __stop_spdifout(state);
  839. dmabuf->total_bytes = 0;
  840. dmabuf->count = dmabuf->error = 0;
  841. dmabuf->swptr = dmabuf->hwptr = 0;
  842. spin_unlock_irqrestore(&state->card->lock, flags);
  843. /* allocate DMA buffer, let alloc_dmabuf determine if we are already
  844. * allocated well enough or if we should replace the current buffer
  845. * (assuming one is already allocated, if it isn't, then allocate it).
  846. */
  847. if ((ret = alloc_dmabuf(state)))
  848. return ret;
  849. /* FIXME: figure out all this OSS fragment stuff */
  850. /* I did, it now does what it should according to the OSS API. DL */
  851. /* We may not have realloced our dmabuf, but the fragment size to
  852. * fragment number ratio may have changed, so go ahead and reprogram
  853. * things
  854. */
  855. dmabuf->dmasize = PAGE_SIZE << dmabuf->buforder;
  856. dmabuf->numfrag = SG_LEN;
  857. dmabuf->fragsize = dmabuf->dmasize / dmabuf->numfrag;
  858. dmabuf->fragsamples = dmabuf->fragsize >> 1;
  859. dmabuf->userfragsize = dmabuf->ossfragsize;
  860. dmabuf->userfrags = dmabuf->dmasize / dmabuf->ossfragsize;
  861. memset(dmabuf->rawbuf, 0, dmabuf->dmasize);
  862. if (dmabuf->ossmaxfrags == 4) {
  863. fragint = 8;
  864. dmabuf->fragshift = 2;
  865. } else if (dmabuf->ossmaxfrags == 8) {
  866. fragint = 4;
  867. dmabuf->fragshift = 3;
  868. } else if (dmabuf->ossmaxfrags == 16) {
  869. fragint = 2;
  870. dmabuf->fragshift = 4;
  871. } else {
  872. fragint = 1;
  873. dmabuf->fragshift = 5;
  874. }
  875. /*
  876. * Now set up the ring
  877. */
  878. if (rec == 1)
  879. c = dmabuf->read_channel;
  880. else if (rec == 2)
  881. c = dmabuf->codec_spdifout_channel;
  882. else if (rec == 3)
  883. c = dmabuf->controller_spdifout_channel;
  884. else if (rec == 0)
  885. c = dmabuf->write_channel;
  886. if (c != NULL) {
  887. sg = &c->sg[0];
  888. /*
  889. * Load up 32 sg entries and take an interrupt at half
  890. * way (we might want more interrupts later..)
  891. */
  892. for (i = 0; i < dmabuf->numfrag; i++) {
  893. sg->busaddr =
  894. virt_to_bus(dmabuf->rawbuf +
  895. dmabuf->fragsize * i);
  896. // the card will always be doing 16bit stereo
  897. sg->control = dmabuf->fragsamples;
  898. sg->control |= CON_BUFPAD; //I modify
  899. // set us up to get IOC interrupts as often as needed to
  900. // satisfy numfrag requirements, no more
  901. if (((i + 1) % fragint) == 0) {
  902. sg->control |= CON_IOC;
  903. }
  904. sg++;
  905. }
  906. spin_lock_irqsave(&state->card->lock, flags);
  907. outb(2, state->card->iobase + c->port + OFF_CR); /* reset DMA machine */
  908. outl(virt_to_bus(&c->sg[0]), state->card->iobase + c->port + OFF_BDBAR);
  909. outb(0, state->card->iobase + c->port + OFF_CIV);
  910. outb(0, state->card->iobase + c->port + OFF_LVI);
  911. spin_unlock_irqrestore(&state->card->lock, flags);
  912. }
  913. /* set the ready flag for the dma buffer */
  914. dmabuf->ready = 1;
  915. return 0;
  916. }
  917. static void __ali_update_lvi(struct ali_state *state, int rec)
  918. {
  919. struct dmabuf *dmabuf = &state->dmabuf;
  920. int x, port;
  921. port = state->card->iobase;
  922. if (rec == 1)
  923. port += dmabuf->read_channel->port;
  924. else if (rec == 2)
  925. port += dmabuf->codec_spdifout_channel->port;
  926. else if (rec == 3)
  927. port += dmabuf->controller_spdifout_channel->port;
  928. else if (rec == 0)
  929. port += dmabuf->write_channel->port;
  930. /* if we are currently stopped, then our CIV is actually set to our
  931. * *last* sg segment and we are ready to wrap to the next. However,
  932. * if we set our LVI to the last sg segment, then it won't wrap to
  933. * the next sg segment, it won't even get a start. So, instead, when
  934. * we are stopped, we set both the LVI value and also we increment
  935. * the CIV value to the next sg segment to be played so that when
  936. * we call start_{dac,adc}, things will operate properly
  937. */
  938. if (!dmabuf->enable && dmabuf->ready) {
  939. if (rec && dmabuf->count < dmabuf->dmasize && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  940. outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
  941. __start_adc(state);
  942. while (! (inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
  943. cpu_relax();
  944. } else if (!rec && dmabuf->count && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  945. outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
  946. __start_dac(state);
  947. while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
  948. cpu_relax();
  949. } else if (rec && dmabuf->count && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
  950. if (codec_independent_spdif_locked > 0) {
  951. // outb((inb(port+OFF_CIV))&31, port+OFF_LVI);
  952. outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
  953. __start_spdifout(state);
  954. while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
  955. cpu_relax();
  956. } else {
  957. if (controller_independent_spdif_locked > 0) {
  958. outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
  959. __start_spdifout(state);
  960. while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
  961. cpu_relax();
  962. }
  963. }
  964. }
  965. }
  966. /* swptr - 1 is the tail of our transfer */
  967. x = (dmabuf->dmasize + dmabuf->swptr - 1) % dmabuf->dmasize;
  968. x /= dmabuf->fragsize;
  969. outb(x, port + OFF_LVI);
  970. }
  971. static void ali_update_lvi(struct ali_state *state, int rec)
  972. {
  973. struct dmabuf *dmabuf = &state->dmabuf;
  974. unsigned long flags;
  975. if (!dmabuf->ready)
  976. return;
  977. spin_lock_irqsave(&state->card->lock, flags);
  978. __ali_update_lvi(state, rec);
  979. spin_unlock_irqrestore(&state->card->lock, flags);
  980. }
  981. /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
  982. static void ali_update_ptr(struct ali_state *state)
  983. {
  984. struct dmabuf *dmabuf = &state->dmabuf;
  985. unsigned hwptr;
  986. int diff;
  987. /* error handling and process wake up for DAC */
  988. if (dmabuf->enable == ADC_RUNNING) {
  989. /* update hardware pointer */
  990. hwptr = ali_get_dma_addr(state, 1);
  991. diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
  992. dmabuf->hwptr = hwptr;
  993. dmabuf->total_bytes += diff;
  994. dmabuf->count += diff;
  995. if (dmabuf->count > dmabuf->dmasize) {
  996. /* buffer underrun or buffer overrun */
  997. /* this is normal for the end of a read */
  998. /* only give an error if we went past the */
  999. /* last valid sg entry */
  1000. if ((inb(state->card->iobase + PI_CIV) & 31) != (inb(state->card->iobase + PI_LVI) & 31)) {
  1001. printk(KERN_WARNING "ali_audio: DMA overrun on read\n");
  1002. dmabuf->error++;
  1003. }
  1004. }
  1005. if (dmabuf->count > dmabuf->userfragsize)
  1006. wake_up(&dmabuf->wait);
  1007. }
  1008. /* error handling and process wake up for DAC */
  1009. if (dmabuf->enable == DAC_RUNNING) {
  1010. /* update hardware pointer */
  1011. hwptr = ali_get_dma_addr(state, 0);
  1012. diff =
  1013. (dmabuf->dmasize + hwptr -
  1014. dmabuf->hwptr) % dmabuf->dmasize;
  1015. #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
  1016. printk("DAC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
  1017. #endif
  1018. dmabuf->hwptr = hwptr;
  1019. dmabuf->total_bytes += diff;
  1020. dmabuf->count -= diff;
  1021. if (dmabuf->count < 0) {
  1022. /* buffer underrun or buffer overrun */
  1023. /* this is normal for the end of a write */
  1024. /* only give an error if we went past the */
  1025. /* last valid sg entry */
  1026. if ((inb(state->card->iobase + PO_CIV) & 31) != (inb(state->card->iobase + PO_LVI) & 31)) {
  1027. printk(KERN_WARNING "ali_audio: DMA overrun on write\n");
  1028. printk(KERN_DEBUG "ali_audio: CIV %d, LVI %d, hwptr %x, count %d\n",
  1029. inb(state->card->iobase + PO_CIV) & 31,
  1030. inb(state->card->iobase + PO_LVI) & 31,
  1031. dmabuf->hwptr,
  1032. dmabuf->count);
  1033. dmabuf->error++;
  1034. }
  1035. }
  1036. if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
  1037. wake_up(&dmabuf->wait);
  1038. }
  1039. /* error handling and process wake up for CODEC SPDIF OUT */
  1040. if (dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
  1041. /* update hardware pointer */
  1042. hwptr = ali_get_dma_addr(state, 2);
  1043. diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
  1044. dmabuf->hwptr = hwptr;
  1045. dmabuf->total_bytes += diff;
  1046. dmabuf->count -= diff;
  1047. if (dmabuf->count < 0) {
  1048. /* buffer underrun or buffer overrun */
  1049. /* this is normal for the end of a write */
  1050. /* only give an error if we went past the */
  1051. /* last valid sg entry */
  1052. if ((inb(state->card->iobase + CODECSPDIFOUT_CIV) & 31) != (inb(state->card->iobase + CODECSPDIFOUT_LVI) & 31)) {
  1053. printk(KERN_WARNING "ali_audio: DMA overrun on write\n");
  1054. printk(KERN_DEBUG "ali_audio: CIV %d, LVI %d, hwptr %x, count %d\n",
  1055. inb(state->card->iobase + CODECSPDIFOUT_CIV) & 31,
  1056. inb(state->card->iobase + CODECSPDIFOUT_LVI) & 31,
  1057. dmabuf->hwptr, dmabuf->count);
  1058. dmabuf->error++;
  1059. }
  1060. }
  1061. if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
  1062. wake_up(&dmabuf->wait);
  1063. }
  1064. /* error handling and process wake up for CONTROLLER SPDIF OUT */
  1065. if (dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
  1066. /* update hardware pointer */
  1067. hwptr = ali_get_dma_addr(state, 3);
  1068. diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
  1069. dmabuf->hwptr = hwptr;
  1070. dmabuf->total_bytes += diff;
  1071. dmabuf->count -= diff;
  1072. if (dmabuf->count < 0) {
  1073. /* buffer underrun or buffer overrun */
  1074. /* this is normal for the end of a write */
  1075. /* only give an error if we went past the */
  1076. /* last valid sg entry */
  1077. if ((inb(state->card->iobase + CONTROLLERSPDIFOUT_CIV) & 31) != (inb(state->card->iobase + CONTROLLERSPDIFOUT_LVI) & 31)) {
  1078. printk(KERN_WARNING
  1079. "ali_audio: DMA overrun on write\n");
  1080. printk("ali_audio: CIV %d, LVI %d, hwptr %x, "
  1081. "count %d\n",
  1082. inb(state->card->iobase + CONTROLLERSPDIFOUT_CIV) & 31,
  1083. inb(state->card->iobase + CONTROLLERSPDIFOUT_LVI) & 31,
  1084. dmabuf->hwptr, dmabuf->count);
  1085. dmabuf->error++;
  1086. }
  1087. }
  1088. if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
  1089. wake_up(&dmabuf->wait);
  1090. }
  1091. }
  1092. static inline int ali_get_free_write_space(struct
  1093. ali_state
  1094. *state)
  1095. {
  1096. struct dmabuf *dmabuf = &state->dmabuf;
  1097. int free;
  1098. if (dmabuf->count < 0) {
  1099. dmabuf->count = 0;
  1100. dmabuf->swptr = dmabuf->hwptr;
  1101. }
  1102. free = dmabuf->dmasize - dmabuf->swptr;
  1103. if ((dmabuf->count + free) > dmabuf->dmasize){
  1104. free = dmabuf->dmasize - dmabuf->count;
  1105. }
  1106. return free;
  1107. }
  1108. static inline int ali_get_available_read_data(struct
  1109. ali_state
  1110. *state)
  1111. {
  1112. struct dmabuf *dmabuf = &state->dmabuf;
  1113. int avail;
  1114. ali_update_ptr(state);
  1115. // catch overruns during record
  1116. if (dmabuf->count > dmabuf->dmasize) {
  1117. dmabuf->count = dmabuf->dmasize;
  1118. dmabuf->swptr = dmabuf->hwptr;
  1119. }
  1120. avail = dmabuf->count;
  1121. avail -= (dmabuf->hwptr % dmabuf->fragsize);
  1122. if (avail < 0)
  1123. return (0);
  1124. return (avail);
  1125. }
  1126. static int drain_dac(struct ali_state *state, int signals_allowed)
  1127. {
  1128. DECLARE_WAITQUEUE(wait, current);
  1129. struct dmabuf *dmabuf = &state->dmabuf;
  1130. unsigned long flags;
  1131. unsigned long tmo;
  1132. int count;
  1133. if (!dmabuf->ready)
  1134. return 0;
  1135. if (dmabuf->mapped) {
  1136. stop_dac(state);
  1137. return 0;
  1138. }
  1139. add_wait_queue(&dmabuf->wait, &wait);
  1140. for (;;) {
  1141. spin_lock_irqsave(&state->card->lock, flags);
  1142. ali_update_ptr(state);
  1143. count = dmabuf->count;
  1144. spin_unlock_irqrestore(&state->card->lock, flags);
  1145. if (count <= 0)
  1146. break;
  1147. /*
  1148. * This will make sure that our LVI is correct, that our
  1149. * pointer is updated, and that the DAC is running. We
  1150. * have to force the setting of dmabuf->trigger to avoid
  1151. * any possible deadlocks.
  1152. */
  1153. if (!dmabuf->enable) {
  1154. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  1155. ali_update_lvi(state, 0);
  1156. }
  1157. if (signal_pending(current) && signals_allowed) {
  1158. break;
  1159. }
  1160. /* It seems that we have to set the current state to
  1161. * TASK_INTERRUPTIBLE every time to make the process
  1162. * really go to sleep. This also has to be *after* the
  1163. * update_ptr() call because update_ptr is likely to
  1164. * do a wake_up() which will unset this before we ever
  1165. * try to sleep, resuling in a tight loop in this code
  1166. * instead of actually sleeping and waiting for an
  1167. * interrupt to wake us up!
  1168. */
  1169. set_current_state(TASK_INTERRUPTIBLE);
  1170. /*
  1171. * set the timeout to significantly longer than it *should*
  1172. * take for the DAC to drain the DMA buffer
  1173. */
  1174. tmo = (count * HZ) / (dmabuf->rate);
  1175. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1176. printk(KERN_ERR "ali_audio: drain_dac, dma timeout?\n");
  1177. count = 0;
  1178. break;
  1179. }
  1180. }
  1181. set_current_state(TASK_RUNNING);
  1182. remove_wait_queue(&dmabuf->wait, &wait);
  1183. if (count > 0 && signal_pending(current) && signals_allowed)
  1184. return -ERESTARTSYS;
  1185. stop_dac(state);
  1186. return 0;
  1187. }
  1188. static int drain_spdifout(struct ali_state *state, int signals_allowed)
  1189. {
  1190. DECLARE_WAITQUEUE(wait, current);
  1191. struct dmabuf *dmabuf = &state->dmabuf;
  1192. unsigned long flags;
  1193. unsigned long tmo;
  1194. int count;
  1195. if (!dmabuf->ready)
  1196. return 0;
  1197. if (dmabuf->mapped) {
  1198. stop_spdifout(state);
  1199. return 0;
  1200. }
  1201. add_wait_queue(&dmabuf->wait, &wait);
  1202. for (;;) {
  1203. spin_lock_irqsave(&state->card->lock, flags);
  1204. ali_update_ptr(state);
  1205. count = dmabuf->count;
  1206. spin_unlock_irqrestore(&state->card->lock, flags);
  1207. if (count <= 0)
  1208. break;
  1209. /*
  1210. * This will make sure that our LVI is correct, that our
  1211. * pointer is updated, and that the DAC is running. We
  1212. * have to force the setting of dmabuf->trigger to avoid
  1213. * any possible deadlocks.
  1214. */
  1215. if (!dmabuf->enable) {
  1216. if (codec_independent_spdif_locked > 0) {
  1217. dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
  1218. ali_update_lvi(state, 2);
  1219. } else {
  1220. if (controller_independent_spdif_locked > 0) {
  1221. dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
  1222. ali_update_lvi(state, 3);
  1223. }
  1224. }
  1225. }
  1226. if (signal_pending(current) && signals_allowed) {
  1227. break;
  1228. }
  1229. /* It seems that we have to set the current state to
  1230. * TASK_INTERRUPTIBLE every time to make the process
  1231. * really go to sleep. This also has to be *after* the
  1232. * update_ptr() call because update_ptr is likely to
  1233. * do a wake_up() which will unset this before we ever
  1234. * try to sleep, resuling in a tight loop in this code
  1235. * instead of actually sleeping and waiting for an
  1236. * interrupt to wake us up!
  1237. */
  1238. set_current_state(TASK_INTERRUPTIBLE);
  1239. /*
  1240. * set the timeout to significantly longer than it *should*
  1241. * take for the DAC to drain the DMA buffer
  1242. */
  1243. tmo = (count * HZ) / (dmabuf->rate);
  1244. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1245. printk(KERN_ERR "ali_audio: drain_spdifout, dma timeout?\n");
  1246. count = 0;
  1247. break;
  1248. }
  1249. }
  1250. set_current_state(TASK_RUNNING);
  1251. remove_wait_queue(&dmabuf->wait, &wait);
  1252. if (count > 0 && signal_pending(current) && signals_allowed)
  1253. return -ERESTARTSYS;
  1254. stop_spdifout(state);
  1255. return 0;
  1256. }
  1257. static void ali_channel_interrupt(struct ali_card *card)
  1258. {
  1259. int i, count;
  1260. for (i = 0; i < NR_HW_CH; i++) {
  1261. struct ali_state *state = card->states[i];
  1262. struct ali_channel *c = NULL;
  1263. struct dmabuf *dmabuf;
  1264. unsigned long port = card->iobase;
  1265. u16 status;
  1266. if (!state)
  1267. continue;
  1268. if (!state->dmabuf.ready)
  1269. continue;
  1270. dmabuf = &state->dmabuf;
  1271. if (codec_independent_spdif_locked > 0) {
  1272. if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
  1273. c = dmabuf->codec_spdifout_channel;
  1274. }
  1275. } else {
  1276. if (controller_independent_spdif_locked > 0) {
  1277. if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1278. c = dmabuf->controller_spdifout_channel;
  1279. } else {
  1280. if (dmabuf->enable & DAC_RUNNING) {
  1281. c = dmabuf->write_channel;
  1282. } else if (dmabuf->enable & ADC_RUNNING) {
  1283. c = dmabuf->read_channel;
  1284. } else
  1285. continue;
  1286. }
  1287. }
  1288. port += c->port;
  1289. status = inw(port + OFF_SR);
  1290. if (status & DMA_INT_COMPLETE) {
  1291. /* only wake_up() waiters if this interrupt signals
  1292. * us being beyond a userfragsize of data open or
  1293. * available, and ali_update_ptr() does that for
  1294. * us
  1295. */
  1296. ali_update_ptr(state);
  1297. }
  1298. if (status & DMA_INT_LVI) {
  1299. ali_update_ptr(state);
  1300. wake_up(&dmabuf->wait);
  1301. if (dmabuf->enable & DAC_RUNNING)
  1302. count = dmabuf->count;
  1303. else if (dmabuf->enable & ADC_RUNNING)
  1304. count = dmabuf->dmasize - dmabuf->count;
  1305. else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  1306. count = dmabuf->count;
  1307. else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1308. count = dmabuf->count;
  1309. else count = 0;
  1310. if (count > 0) {
  1311. if (dmabuf->enable & DAC_RUNNING)
  1312. outl((1 << 1), state->card->iobase + ALI_DMACR);
  1313. else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  1314. outl((1 << 3), state->card->iobase + ALI_DMACR);
  1315. else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1316. outl((1 << 7), state->card->iobase + ALI_DMACR);
  1317. } else {
  1318. if (dmabuf->enable & DAC_RUNNING)
  1319. __stop_dac(state);
  1320. if (dmabuf->enable & ADC_RUNNING)
  1321. __stop_adc(state);
  1322. if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  1323. __stop_spdifout(state);
  1324. if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1325. __stop_spdifout(state);
  1326. dmabuf->enable = 0;
  1327. wake_up(&dmabuf->wait);
  1328. }
  1329. }
  1330. if (!(status & DMA_INT_DCH)) {
  1331. ali_update_ptr(state);
  1332. wake_up(&dmabuf->wait);
  1333. if (dmabuf->enable & DAC_RUNNING)
  1334. count = dmabuf->count;
  1335. else if (dmabuf->enable & ADC_RUNNING)
  1336. count = dmabuf->dmasize - dmabuf->count;
  1337. else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  1338. count = dmabuf->count;
  1339. else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1340. count = dmabuf->count;
  1341. else
  1342. count = 0;
  1343. if (count > 0) {
  1344. if (dmabuf->enable & DAC_RUNNING)
  1345. outl((1 << 1), state->card->iobase + ALI_DMACR);
  1346. else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  1347. outl((1 << 3), state->card->iobase + ALI_DMACR);
  1348. else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1349. outl((1 << 7), state->card->iobase + ALI_DMACR);
  1350. } else {
  1351. if (dmabuf->enable & DAC_RUNNING)
  1352. __stop_dac(state);
  1353. if (dmabuf->enable & ADC_RUNNING)
  1354. __stop_adc(state);
  1355. if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
  1356. __stop_spdifout(state);
  1357. if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
  1358. __stop_spdifout(state);
  1359. dmabuf->enable = 0;
  1360. wake_up(&dmabuf->wait);
  1361. }
  1362. }
  1363. outw(status & DMA_INT_MASK, port + OFF_SR);
  1364. }
  1365. }
  1366. static irqreturn_t ali_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1367. {
  1368. struct ali_card *card = (struct ali_card *) dev_id;
  1369. u32 status;
  1370. u16 status2;
  1371. spin_lock(&card->lock);
  1372. status = inl(card->iobase + ALI_INTERRUPTSR);
  1373. if (!(status & INT_MASK)) {
  1374. spin_unlock(&card->lock);
  1375. return IRQ_NONE; /* not for us */
  1376. }
  1377. if (codec_independent_spdif_locked > 0) {
  1378. if (globel == 0) {
  1379. globel += 1;
  1380. status2 = inw(card->iobase + 0x76);
  1381. outw(status2 | 0x000c, card->iobase + 0x76);
  1382. } else {
  1383. if (status & (INT_PCMOUT | INT_PCMIN | INT_MICIN | INT_SPDIFOUT | INT_CODECSPDIFOUT))
  1384. ali_channel_interrupt(card);
  1385. }
  1386. } else {
  1387. if (status & (INT_PCMOUT | INT_PCMIN | INT_MICIN | INT_SPDIFOUT | INT_CODECSPDIFOUT))
  1388. ali_channel_interrupt(card);
  1389. }
  1390. /* clear 'em */
  1391. outl(status & INT_MASK, card->iobase + ALI_INTERRUPTSR);
  1392. spin_unlock(&card->lock);
  1393. return IRQ_HANDLED;
  1394. }
  1395. /* in this loop, dmabuf.count signifies the amount of data that is
  1396. waiting to be copied to the user's buffer. It is filled by the dma
  1397. machine and drained by this loop. */
  1398. static ssize_t ali_read(struct file *file, char __user *buffer,
  1399. size_t count, loff_t * ppos)
  1400. {
  1401. struct ali_state *state = (struct ali_state *) file->private_data;
  1402. struct ali_card *card = state ? state->card : NULL;
  1403. struct dmabuf *dmabuf = &state->dmabuf;
  1404. ssize_t ret;
  1405. unsigned long flags;
  1406. unsigned int swptr;
  1407. int cnt;
  1408. DECLARE_WAITQUEUE(waita, current);
  1409. #ifdef DEBUG2
  1410. printk("ali_audio: ali_read called, count = %d\n", count);
  1411. #endif
  1412. if (dmabuf->mapped)
  1413. return -ENXIO;
  1414. if (dmabuf->enable & DAC_RUNNING)
  1415. return -ENODEV;
  1416. if (!dmabuf->read_channel) {
  1417. dmabuf->ready = 0;
  1418. dmabuf->read_channel = card->alloc_rec_pcm_channel(card);
  1419. if (!dmabuf->read_channel) {
  1420. return -EBUSY;
  1421. }
  1422. }
  1423. if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
  1424. return ret;
  1425. if (!access_ok(VERIFY_WRITE, buffer, count))
  1426. return -EFAULT;
  1427. ret = 0;
  1428. add_wait_queue(&dmabuf->wait, &waita);
  1429. while (count > 0) {
  1430. set_current_state(TASK_INTERRUPTIBLE);
  1431. spin_lock_irqsave(&card->lock, flags);
  1432. if (PM_SUSPENDED(card)) {
  1433. spin_unlock_irqrestore(&card->lock, flags);
  1434. schedule();
  1435. if (signal_pending(current)) {
  1436. if (!ret)
  1437. ret = -EAGAIN;
  1438. break;
  1439. }
  1440. continue;
  1441. }
  1442. swptr = dmabuf->swptr;
  1443. cnt = ali_get_available_read_data(state);
  1444. // this is to make the copy_to_user simpler below
  1445. if (cnt > (dmabuf->dmasize - swptr))
  1446. cnt = dmabuf->dmasize - swptr;
  1447. spin_unlock_irqrestore(&card->lock, flags);
  1448. if (cnt > count)
  1449. cnt = count;
  1450. /* Lop off the last two bits to force the code to always
  1451. * write in full samples. This keeps software that sets
  1452. * O_NONBLOCK but doesn't check the return value of the
  1453. * write call from getting things out of state where they
  1454. * think a full 4 byte sample was written when really only
  1455. * a portion was, resulting in odd sound and stereo
  1456. * hysteresis.
  1457. */
  1458. cnt &= ~0x3;
  1459. if (cnt <= 0) {
  1460. unsigned long tmo;
  1461. /*
  1462. * Don't let us deadlock. The ADC won't start if
  1463. * dmabuf->trigger isn't set. A call to SETTRIGGER
  1464. * could have turned it off after we set it to on
  1465. * previously.
  1466. */
  1467. dmabuf->trigger = PCM_ENABLE_INPUT;
  1468. /*
  1469. * This does three things. Updates LVI to be correct,
  1470. * makes sure the ADC is running, and updates the
  1471. * hwptr.
  1472. */
  1473. ali_update_lvi(state, 1);
  1474. if (file->f_flags & O_NONBLOCK) {
  1475. if (!ret)
  1476. ret = -EAGAIN;
  1477. goto done;
  1478. }
  1479. /* Set the timeout to how long it would take to fill
  1480. * two of our buffers. If we haven't been woke up
  1481. * by then, then we know something is wrong.
  1482. */
  1483. tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
  1484. /* There are two situations when sleep_on_timeout returns, one is when
  1485. the interrupt is serviced correctly and the process is waked up by
  1486. ISR ON TIME. Another is when timeout is expired, which means that
  1487. either interrupt is NOT serviced correctly (pending interrupt) or it
  1488. is TOO LATE for the process to be scheduled to run (scheduler latency)
  1489. which results in a (potential) buffer overrun. And worse, there is
  1490. NOTHING we can do to prevent it. */
  1491. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1492. printk(KERN_ERR
  1493. "ali_audio: recording schedule timeout, "
  1494. "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
  1495. dmabuf->dmasize, dmabuf->fragsize,
  1496. dmabuf->count, dmabuf->hwptr,
  1497. dmabuf->swptr);
  1498. /* a buffer overrun, we delay the recovery until next time the
  1499. while loop begin and we REALLY have space to record */
  1500. }
  1501. if (signal_pending(current)) {
  1502. ret = ret ? ret : -ERESTARTSYS;
  1503. goto done;
  1504. }
  1505. continue;
  1506. }
  1507. if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
  1508. if (!ret)
  1509. ret = -EFAULT;
  1510. goto done;
  1511. }
  1512. swptr = (swptr + cnt) % dmabuf->dmasize;
  1513. spin_lock_irqsave(&card->lock, flags);
  1514. if (PM_SUSPENDED(card)) {
  1515. spin_unlock_irqrestore(&card->lock, flags);
  1516. continue;
  1517. }
  1518. dmabuf->swptr = swptr;
  1519. dmabuf->count -= cnt;
  1520. spin_unlock_irqrestore(&card->lock, flags);
  1521. count -= cnt;
  1522. buffer += cnt;
  1523. ret += cnt;
  1524. }
  1525. done:
  1526. ali_update_lvi(state, 1);
  1527. set_current_state(TASK_RUNNING);
  1528. remove_wait_queue(&dmabuf->wait, &waita);
  1529. return ret;
  1530. }
  1531. /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
  1532. the soundcard. it is drained by the dma machine and filled by this loop. */
  1533. static ssize_t ali_write(struct file *file,
  1534. const char __user *buffer, size_t count, loff_t * ppos)
  1535. {
  1536. struct ali_state *state = (struct ali_state *) file->private_data;
  1537. struct ali_card *card = state ? state->card : NULL;
  1538. struct dmabuf *dmabuf = &state->dmabuf;
  1539. ssize_t ret;
  1540. unsigned long flags;
  1541. unsigned int swptr = 0;
  1542. int cnt, x;
  1543. DECLARE_WAITQUEUE(waita, current);
  1544. #ifdef DEBUG2
  1545. printk("ali_audio: ali_write called, count = %d\n", count);
  1546. #endif
  1547. if (dmabuf->mapped)
  1548. return -ENXIO;
  1549. if (dmabuf->enable & ADC_RUNNING)
  1550. return -ENODEV;
  1551. if (codec_independent_spdif_locked > 0) {
  1552. if (!dmabuf->codec_spdifout_channel) {
  1553. dmabuf->ready = 0;
  1554. dmabuf->codec_spdifout_channel = card->alloc_codec_spdifout_channel(card);
  1555. if (!dmabuf->codec_spdifout_channel)
  1556. return -EBUSY;
  1557. }
  1558. } else {
  1559. if (controller_independent_spdif_locked > 0) {
  1560. if (!dmabuf->controller_spdifout_channel) {
  1561. dmabuf->ready = 0;
  1562. dmabuf->controller_spdifout_channel = card->alloc_controller_spdifout_channel(card);
  1563. if (!dmabuf->controller_spdifout_channel)
  1564. return -EBUSY;
  1565. }
  1566. } else {
  1567. if (!dmabuf->write_channel) {
  1568. dmabuf->ready = 0;
  1569. dmabuf->write_channel =
  1570. card->alloc_pcm_channel(card);
  1571. if (!dmabuf->write_channel)
  1572. return -EBUSY;
  1573. }
  1574. }
  1575. }
  1576. if (codec_independent_spdif_locked > 0) {
  1577. if (!dmabuf->ready && (ret = prog_dmabuf(state, 2)))
  1578. return ret;
  1579. } else {
  1580. if (controller_independent_spdif_locked > 0) {
  1581. if (!dmabuf->ready && (ret = prog_dmabuf(state, 3)))
  1582. return ret;
  1583. } else {
  1584. if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
  1585. return ret;
  1586. }
  1587. }
  1588. if (!access_ok(VERIFY_READ, buffer, count))
  1589. return -EFAULT;
  1590. ret = 0;
  1591. add_wait_queue(&dmabuf->wait, &waita);
  1592. while (count > 0) {
  1593. set_current_state(TASK_INTERRUPTIBLE);
  1594. spin_lock_irqsave(&state->card->lock, flags);
  1595. if (PM_SUSPENDED(card)) {
  1596. spin_unlock_irqrestore(&card->lock, flags);
  1597. schedule();
  1598. if (signal_pending(current)) {
  1599. if (!ret)
  1600. ret = -EAGAIN;
  1601. break;
  1602. }
  1603. continue;
  1604. }
  1605. swptr = dmabuf->swptr;
  1606. cnt = ali_get_free_write_space(state);
  1607. /* Bound the maximum size to how much we can copy to the
  1608. * dma buffer before we hit the end. If we have more to
  1609. * copy then it will get done in a second pass of this
  1610. * loop starting from the beginning of the buffer.
  1611. */
  1612. if (cnt > (dmabuf->dmasize - swptr))
  1613. cnt = dmabuf->dmasize - swptr;
  1614. spin_unlock_irqrestore(&state->card->lock, flags);
  1615. #ifdef DEBUG2
  1616. printk(KERN_INFO
  1617. "ali_audio: ali_write: %d bytes available space\n",
  1618. cnt);
  1619. #endif
  1620. if (cnt > count)
  1621. cnt = count;
  1622. /* Lop off the last two bits to force the code to always
  1623. * write in full samples. This keeps software that sets
  1624. * O_NONBLOCK but doesn't check the return value of the
  1625. * write call from getting things out of state where they
  1626. * think a full 4 byte sample was written when really only
  1627. * a portion was, resulting in odd sound and stereo
  1628. * hysteresis.
  1629. */
  1630. cnt &= ~0x3;
  1631. if (cnt <= 0) {
  1632. unsigned long tmo;
  1633. // There is data waiting to be played
  1634. /*
  1635. * Force the trigger setting since we would
  1636. * deadlock with it set any other way
  1637. */
  1638. if (codec_independent_spdif_locked > 0) {
  1639. dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
  1640. ali_update_lvi(state, 2);
  1641. } else {
  1642. if (controller_independent_spdif_locked > 0) {
  1643. dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
  1644. ali_update_lvi(state, 3);
  1645. } else {
  1646. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  1647. ali_update_lvi(state, 0);
  1648. }
  1649. }
  1650. if (file->f_flags & O_NONBLOCK) {
  1651. if (!ret)
  1652. ret = -EAGAIN;
  1653. goto ret;
  1654. }
  1655. /* Not strictly correct but works */
  1656. tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
  1657. /* There are two situations when sleep_on_timeout returns, one is when
  1658. the interrupt is serviced correctly and the process is waked up by
  1659. ISR ON TIME. Another is when timeout is expired, which means that
  1660. either interrupt is NOT serviced correctly (pending interrupt) or it
  1661. is TOO LATE for the process to be scheduled to run (scheduler latency)
  1662. which results in a (potential) buffer underrun. And worse, there is
  1663. NOTHING we can do to prevent it. */
  1664. /* FIXME - do timeout handling here !! */
  1665. schedule_timeout(tmo >= 2 ? tmo : 2);
  1666. if (signal_pending(current)) {
  1667. if (!ret)
  1668. ret = -ERESTARTSYS;
  1669. goto ret;
  1670. }
  1671. continue;
  1672. }
  1673. if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
  1674. if (!ret)
  1675. ret = -EFAULT;
  1676. goto ret;
  1677. }
  1678. swptr = (swptr + cnt) % dmabuf->dmasize;
  1679. spin_lock_irqsave(&state->card->lock, flags);
  1680. if (PM_SUSPENDED(card)) {
  1681. spin_unlock_irqrestore(&card->lock, flags);
  1682. continue;
  1683. }
  1684. dmabuf->swptr = swptr;
  1685. dmabuf->count += cnt;
  1686. count -= cnt;
  1687. buffer += cnt;
  1688. ret += cnt;
  1689. spin_unlock_irqrestore(&state->card->lock, flags);
  1690. }
  1691. if (swptr % dmabuf->fragsize) {
  1692. x = dmabuf->fragsize - (swptr % dmabuf->fragsize);
  1693. memset(dmabuf->rawbuf + swptr, '\0', x);
  1694. }
  1695. ret:
  1696. if (codec_independent_spdif_locked > 0) {
  1697. ali_update_lvi(state, 2);
  1698. } else {
  1699. if (controller_independent_spdif_locked > 0) {
  1700. ali_update_lvi(state, 3);
  1701. } else {
  1702. ali_update_lvi(state, 0);
  1703. }
  1704. }
  1705. set_current_state(TASK_RUNNING);
  1706. remove_wait_queue(&dmabuf->wait, &waita);
  1707. return ret;
  1708. }
  1709. /* No kernel lock - we have our own spinlock */
  1710. static unsigned int ali_poll(struct file *file, struct poll_table_struct
  1711. *wait)
  1712. {
  1713. struct ali_state *state = (struct ali_state *) file->private_data;
  1714. struct dmabuf *dmabuf = &state->dmabuf;
  1715. unsigned long flags;
  1716. unsigned int mask = 0;
  1717. if (!dmabuf->ready)
  1718. return 0;
  1719. poll_wait(file, &dmabuf->wait, wait);
  1720. spin_lock_irqsave(&state->card->lock, flags);
  1721. ali_update_ptr(state);
  1722. if (file->f_mode & FMODE_READ && dmabuf->enable & ADC_RUNNING) {
  1723. if (dmabuf->count >= (signed) dmabuf->fragsize)
  1724. mask |= POLLIN | POLLRDNORM;
  1725. }
  1726. if (file->f_mode & FMODE_WRITE && (dmabuf->enable & (DAC_RUNNING|CODEC_SPDIFOUT_RUNNING|CONTROLLER_SPDIFOUT_RUNNING))) {
  1727. if ((signed) dmabuf->dmasize >= dmabuf->count + (signed) dmabuf->fragsize)
  1728. mask |= POLLOUT | POLLWRNORM;
  1729. }
  1730. spin_unlock_irqrestore(&state->card->lock, flags);
  1731. return mask;
  1732. }
  1733. static int ali_mmap(struct file *file, struct vm_area_struct *vma)
  1734. {
  1735. struct ali_state *state = (struct ali_state *) file->private_data;
  1736. struct dmabuf *dmabuf = &state->dmabuf;
  1737. int ret = -EINVAL;
  1738. unsigned long size;
  1739. lock_kernel();
  1740. if (vma->vm_flags & VM_WRITE) {
  1741. if (!dmabuf->write_channel && (dmabuf->write_channel = state->card->alloc_pcm_channel(state->card)) == NULL) {
  1742. ret = -EBUSY;
  1743. goto out;
  1744. }
  1745. }
  1746. if (vma->vm_flags & VM_READ) {
  1747. if (!dmabuf->read_channel && (dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card)) == NULL) {
  1748. ret = -EBUSY;
  1749. goto out;
  1750. }
  1751. }
  1752. if ((ret = prog_dmabuf(state, 0)) != 0)
  1753. goto out;
  1754. ret = -EINVAL;
  1755. if (vma->vm_pgoff != 0)
  1756. goto out;
  1757. size = vma->vm_end - vma->vm_start;
  1758. if (size > (PAGE_SIZE << dmabuf->buforder))
  1759. goto out;
  1760. ret = -EAGAIN;
  1761. if (remap_pfn_range(vma, vma->vm_start,
  1762. virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
  1763. size, vma->vm_page_prot))
  1764. goto out;
  1765. dmabuf->mapped = 1;
  1766. dmabuf->trigger = 0;
  1767. ret = 0;
  1768. out:
  1769. unlock_kernel();
  1770. return ret;
  1771. }
  1772. static int ali_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1773. {
  1774. struct ali_state *state = (struct ali_state *) file->private_data;
  1775. struct ali_channel *c = NULL;
  1776. struct dmabuf *dmabuf = &state->dmabuf;
  1777. unsigned long flags;
  1778. audio_buf_info abinfo;
  1779. count_info cinfo;
  1780. unsigned int i_scr;
  1781. int val = 0, ret;
  1782. struct ac97_codec *codec = state->card->ac97_codec[0];
  1783. void __user *argp = (void __user *)arg;
  1784. int __user *p = argp;
  1785. #ifdef DEBUG
  1786. printk("ali_audio: ali_ioctl, arg=0x%x, cmd=",
  1787. arg ? *p : 0);
  1788. #endif
  1789. switch (cmd) {
  1790. case OSS_GETVERSION:
  1791. #ifdef DEBUG
  1792. printk("OSS_GETVERSION\n");
  1793. #endif
  1794. return put_user(SOUND_VERSION, p);
  1795. case SNDCTL_DSP_RESET:
  1796. #ifdef DEBUG
  1797. printk("SNDCTL_DSP_RESET\n");
  1798. #endif
  1799. spin_lock_irqsave(&state->card->lock, flags);
  1800. if (dmabuf->enable == DAC_RUNNING) {
  1801. c = dmabuf->write_channel;
  1802. __stop_dac(state);
  1803. }
  1804. if (dmabuf->enable == ADC_RUNNING) {
  1805. c = dmabuf->read_channel;
  1806. __stop_adc(state);
  1807. }
  1808. if (dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
  1809. c = dmabuf->codec_spdifout_channel;
  1810. __stop_spdifout(state);
  1811. }
  1812. if (dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
  1813. c = dmabuf->controller_spdifout_channel;
  1814. __stop_spdifout(state);
  1815. }
  1816. if (c != NULL) {
  1817. outb(2, state->card->iobase + c->port + OFF_CR); /* reset DMA machine */
  1818. outl(virt_to_bus(&c->sg[0]),
  1819. state->card->iobase + c->port + OFF_BDBAR);
  1820. outb(0, state->card->iobase + c->port + OFF_CIV);
  1821. outb(0, state->card->iobase + c->port + OFF_LVI);
  1822. }
  1823. spin_unlock_irqrestore(&state->card->lock, flags);
  1824. synchronize_irq(state->card->pci_dev->irq);
  1825. dmabuf->ready = 0;
  1826. dmabuf->swptr = dmabuf->hwptr = 0;
  1827. dmabuf->count = dmabuf->total_bytes = 0;
  1828. return 0;
  1829. case SNDCTL_DSP_SYNC:
  1830. #ifdef DEBUG
  1831. printk("SNDCTL_DSP_SYNC\n");
  1832. #endif
  1833. if (codec_independent_spdif_locked > 0) {
  1834. if (dmabuf->enable != CODEC_SPDIFOUT_RUNNING
  1835. || file->f_flags & O_NONBLOCK)
  1836. return 0;
  1837. if ((val = drain_spdifout(state, 1)))
  1838. return val;
  1839. } else {
  1840. if (controller_independent_spdif_locked > 0) {
  1841. if (dmabuf->enable !=
  1842. CONTROLLER_SPDIFOUT_RUNNING
  1843. || file->f_flags & O_NONBLOCK)
  1844. return 0;
  1845. if ((val = drain_spdifout(state, 1)))
  1846. return val;
  1847. } else {
  1848. if (dmabuf->enable != DAC_RUNNING
  1849. || file->f_flags & O_NONBLOCK)
  1850. return 0;
  1851. if ((val = drain_dac(state, 1)))
  1852. return val;
  1853. }
  1854. }
  1855. dmabuf->total_bytes = 0;
  1856. return 0;
  1857. case SNDCTL_DSP_SPEED: /* set smaple rate */
  1858. #ifdef DEBUG
  1859. printk("SNDCTL_DSP_SPEED\n");
  1860. #endif
  1861. if (get_user(val, p))
  1862. return -EFAULT;
  1863. if (val >= 0) {
  1864. if (file->f_mode & FMODE_WRITE) {
  1865. if ((state->card->ac97_status & SPDIF_ON)) { /* S/PDIF Enabled */
  1866. /* RELTEK ALC650 only support 48000, need to check that */
  1867. if (ali_valid_spdif_rate(codec, val)) {
  1868. if (codec_independent_spdif_locked > 0) {
  1869. ali_set_spdif_output(state, -1, 0);
  1870. stop_spdifout(state);
  1871. dmabuf->ready = 0;
  1872. /* I add test codec independent spdif out */
  1873. spin_lock_irqsave(&state->card->lock, flags);
  1874. ali_set_codecspdifout_rate(state, val); // I modified
  1875. spin_unlock_irqrestore(&state->card->lock, flags);
  1876. /* Set S/PDIF transmitter rate. */
  1877. i_scr = inl(state->card->iobase + ALI_SCR);
  1878. if ((i_scr & 0x00300000) == 0x00100000) {
  1879. ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
  1880. } else {
  1881. if ((i_scr&0x00300000) == 0x00200000)
  1882. {
  1883. ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
  1884. } else {
  1885. if ((i_scr & 0x00300000) == 0x00300000) {
  1886. ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
  1887. } else {
  1888. ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
  1889. }
  1890. }
  1891. }
  1892. if (!(state->card->ac97_status & SPDIF_ON)) {
  1893. val = dmabuf->rate;
  1894. }
  1895. } else {
  1896. if (controller_independent_spdif_locked > 0)
  1897. {
  1898. stop_spdifout(state);
  1899. dmabuf->ready = 0;
  1900. spin_lock_irqsave(&state->card->lock, flags);
  1901. ali_set_spdifout_rate(state, controller_independent_spdif_locked);
  1902. spin_unlock_irqrestore(&state->card->lock, flags);
  1903. } else {
  1904. /* Set DAC rate */
  1905. ali_set_spdif_output(state, -1, 0);
  1906. stop_dac(state);
  1907. dmabuf->ready = 0;
  1908. spin_lock_irqsave(&state->card->lock, flags);
  1909. ali_set_dac_rate(state, val);
  1910. spin_unlock_irqrestore(&state->card->lock, flags);
  1911. /* Set S/PDIF transmitter rate. */
  1912. ali_set_spdif_output(state, AC97_EA_SPSA_3_4, val);
  1913. if (!(state->card->ac97_status & SPDIF_ON))
  1914. {
  1915. val = dmabuf->rate;
  1916. }
  1917. }
  1918. }
  1919. } else { /* Not a valid rate for S/PDIF, ignore it */
  1920. val = dmabuf->rate;
  1921. }
  1922. } else {
  1923. stop_dac(state);
  1924. dmabuf->ready = 0;
  1925. spin_lock_irqsave(&state->card->lock, flags);
  1926. ali_set_dac_rate(state, val);
  1927. spin_unlock_irqrestore(&state->card->lock, flags);
  1928. }
  1929. }
  1930. if (file->f_mode & FMODE_READ) {
  1931. stop_adc(state);
  1932. dmabuf->ready = 0;
  1933. spin_lock_irqsave(&state->card->lock, flags);
  1934. ali_set_adc_rate(state, val);
  1935. spin_unlock_irqrestore(&state->card->lock, flags);
  1936. }
  1937. }
  1938. return put_user(dmabuf->rate, p);
  1939. case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
  1940. #ifdef DEBUG
  1941. printk("SNDCTL_DSP_STEREO\n");
  1942. #endif
  1943. if (dmabuf->enable & DAC_RUNNING) {
  1944. stop_dac(state);
  1945. }
  1946. if (dmabuf->enable & ADC_RUNNING) {
  1947. stop_adc(state);
  1948. }
  1949. if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
  1950. stop_spdifout(state);
  1951. }
  1952. if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING) {
  1953. stop_spdifout(state);
  1954. }
  1955. return put_user(1, p);
  1956. case SNDCTL_DSP_GETBLKSIZE:
  1957. if (file->f_mode & FMODE_WRITE) {
  1958. if (codec_independent_spdif_locked > 0) {
  1959. if (!dmabuf->ready && (val = prog_dmabuf(state, 2)))
  1960. return val;
  1961. } else {
  1962. if (controller_independent_spdif_locked > 0) {
  1963. if (!dmabuf->ready && (val = prog_dmabuf(state, 3)))
  1964. return val;
  1965. } else {
  1966. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
  1967. return val;
  1968. }
  1969. }
  1970. }
  1971. if (file->f_mode & FMODE_READ) {
  1972. if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
  1973. return val;
  1974. }
  1975. #ifdef DEBUG
  1976. printk("SNDCTL_DSP_GETBLKSIZE %d\n", dmabuf->userfragsize);
  1977. #endif
  1978. return put_user(dmabuf->userfragsize, p);
  1979. case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format */
  1980. #ifdef DEBUG
  1981. printk("SNDCTL_DSP_GETFMTS\n");
  1982. #endif
  1983. return put_user(AFMT_S16_LE, p);
  1984. case SNDCTL_DSP_SETFMT: /* Select sample format */
  1985. #ifdef DEBUG
  1986. printk("SNDCTL_DSP_SETFMT\n");
  1987. #endif
  1988. return put_user(AFMT_S16_LE, p);
  1989. case SNDCTL_DSP_CHANNELS: // add support 4,6 channel
  1990. #ifdef DEBUG
  1991. printk("SNDCTL_DSP_CHANNELS\n");
  1992. #endif
  1993. if (get_user(val, p))
  1994. return -EFAULT;
  1995. if (val > 0) {
  1996. if (dmabuf->enable & DAC_RUNNING) {
  1997. stop_dac(state);
  1998. }
  1999. if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
  2000. stop_spdifout(state);
  2001. }
  2002. if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING) {
  2003. stop_spdifout(state);
  2004. }
  2005. if (dmabuf->enable & ADC_RUNNING) {
  2006. stop_adc(state);
  2007. }
  2008. } else {
  2009. return put_user(state->card->channels, p);
  2010. }
  2011. i_scr = inl(state->card->iobase + ALI_SCR);
  2012. /* Current # of channels enabled */
  2013. if (i_scr & 0x00000100)
  2014. ret = 4;
  2015. else if (i_scr & 0x00000200)
  2016. ret = 6;
  2017. else
  2018. ret = 2;
  2019. switch (val) {
  2020. case 2: /* 2 channels is always supported */
  2021. if (codec_independent_spdif_locked > 0) {
  2022. outl(((i_scr & 0xfffffcff) | 0x00100000), (state->card->iobase + ALI_SCR));
  2023. } else
  2024. outl((i_scr & 0xfffffcff), (state->card->iobase + ALI_SCR));
  2025. /* Do we need to change mixer settings???? */
  2026. break;
  2027. case 4: /* Supported on some chipsets, better check first */
  2028. if (codec_independent_spdif_locked > 0) {
  2029. outl(((i_scr & 0xfffffcff) | 0x00000100 | 0x00200000), (state->card->iobase + ALI_SCR));
  2030. } else
  2031. outl(((i_scr & 0xfffffcff) | 0x00000100), (state->card->iobase + ALI_SCR));
  2032. break;
  2033. case 6: /* Supported on some chipsets, better check first */
  2034. if (codec_independent_spdif_locked > 0) {
  2035. outl(((i_scr & 0xfffffcff) | 0x00000200 | 0x00008000 | 0x00300000), (state->card->iobase + ALI_SCR));
  2036. } else
  2037. outl(((i_scr & 0xfffffcff) | 0x00000200 | 0x00008000), (state->card->iobase + ALI_SCR));
  2038. break;
  2039. default: /* nothing else is ever supported by the chipset */
  2040. val = ret;
  2041. break;
  2042. }
  2043. return put_user(val, p);
  2044. case SNDCTL_DSP_POST: /* the user has sent all data and is notifying us */
  2045. /* we update the swptr to the end of the last sg segment then return */
  2046. #ifdef DEBUG
  2047. printk("SNDCTL_DSP_POST\n");
  2048. #endif
  2049. if (codec_independent_spdif_locked > 0) {
  2050. if (!dmabuf->ready || (dmabuf->enable != CODEC_SPDIFOUT_RUNNING))
  2051. return 0;
  2052. } else {
  2053. if (controller_independent_spdif_locked > 0) {
  2054. if (!dmabuf->ready || (dmabuf->enable != CONTROLLER_SPDIFOUT_RUNNING))
  2055. return 0;
  2056. } else {
  2057. if (!dmabuf->ready || (dmabuf->enable != DAC_RUNNING))
  2058. return 0;
  2059. }
  2060. }
  2061. if ((dmabuf->swptr % dmabuf->fragsize) != 0) {
  2062. val = dmabuf->fragsize - (dmabuf->swptr % dmabuf->fragsize);
  2063. dmabuf->swptr += val;
  2064. dmabuf->count += val;
  2065. }
  2066. return 0;
  2067. case SNDCTL_DSP_SUBDIVIDE:
  2068. if (dmabuf->subdivision)
  2069. return -EINVAL;
  2070. if (get_user(val, p))
  2071. return -EFAULT;
  2072. if (val != 1 && val != 2 && val != 4)
  2073. return -EINVAL;
  2074. #ifdef DEBUG
  2075. printk("SNDCTL_DSP_SUBDIVIDE %d\n", val);
  2076. #endif
  2077. dmabuf->subdivision = val;
  2078. dmabuf->ready = 0;
  2079. return 0;
  2080. case SNDCTL_DSP_SETFRAGMENT:
  2081. if (get_user(val, p))
  2082. return -EFAULT;
  2083. dmabuf->ossfragsize = 1 << (val & 0xffff);
  2084. dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
  2085. if (!dmabuf->ossfragsize || !dmabuf->ossmaxfrags)
  2086. return -EINVAL;
  2087. /*
  2088. * Bound the frag size into our allowed range of 256 - 4096
  2089. */
  2090. if (dmabuf->ossfragsize < 256)
  2091. dmabuf->ossfragsize = 256;
  2092. else if (dmabuf->ossfragsize > 4096)
  2093. dmabuf->ossfragsize = 4096;
  2094. /*
  2095. * The numfrags could be something reasonable, or it could
  2096. * be 0xffff meaning "Give me as much as possible". So,
  2097. * we check the numfrags * fragsize doesn't exceed our
  2098. * 64k buffer limit, nor is it less than our 8k minimum.
  2099. * If it fails either one of these checks, then adjust the
  2100. * number of fragments, not the size of them. It's OK if
  2101. * our number of fragments doesn't equal 32 or anything
  2102. * like our hardware based number now since we are using
  2103. * a different frag count for the hardware. Before we get
  2104. * into this though, bound the maxfrags to avoid overflow
  2105. * issues. A reasonable bound would be 64k / 256 since our
  2106. * maximum buffer size is 64k and our minimum frag size is
  2107. * 256. On the other end, our minimum buffer size is 8k and
  2108. * our maximum frag size is 4k, so the lower bound should
  2109. * be 2.
  2110. */
  2111. if (dmabuf->ossmaxfrags > 256)
  2112. dmabuf->ossmaxfrags = 256;
  2113. else if (dmabuf->ossmaxfrags < 2)
  2114. dmabuf->ossmaxfrags = 2;
  2115. val = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
  2116. while (val < 8192) {
  2117. val <<= 1;
  2118. dmabuf->ossmaxfrags <<= 1;
  2119. }
  2120. while (val > 65536) {
  2121. val >>= 1;
  2122. dmabuf->ossmaxfrags >>= 1;
  2123. }
  2124. dmabuf->ready = 0;
  2125. #ifdef DEBUG
  2126. printk("SNDCTL_DSP_SETFRAGMENT 0x%x, %d, %d\n", val,
  2127. dmabuf->ossfragsize, dmabuf->ossmaxfrags);
  2128. #endif
  2129. return 0;
  2130. case SNDCTL_DSP_GETOSPACE:
  2131. if (!(file->f_mode & FMODE_WRITE))
  2132. return -EINVAL;
  2133. if (codec_independent_spdif_locked > 0) {
  2134. if (!dmabuf->ready && (val = prog_dmabuf(state, 2)) != 0)
  2135. return val;
  2136. } else {
  2137. if (controller_independent_spdif_locked > 0) {
  2138. if (!dmabuf->ready && (val = prog_dmabuf(state, 3)) != 0)
  2139. return val;
  2140. } else {
  2141. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  2142. return val;
  2143. }
  2144. }
  2145. spin_lock_irqsave(&state->card->lock, flags);
  2146. ali_update_ptr(state);
  2147. abinfo.fragsize = dmabuf->userfragsize;
  2148. abinfo.fragstotal = dmabuf->userfrags;
  2149. if (dmabuf->mapped)
  2150. abinfo.bytes = dmabuf->dmasize;
  2151. else
  2152. abinfo.bytes = ali_get_free_write_space(state);
  2153. abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
  2154. spin_unlock_irqrestore(&state->card->lock, flags);
  2155. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2156. printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %d\n",
  2157. abinfo.bytes, abinfo.fragsize, abinfo.fragments,
  2158. abinfo.fragstotal);
  2159. #endif
  2160. return copy_to_user(argp, &abinfo,
  2161. sizeof(abinfo)) ? -EFAULT : 0;
  2162. case SNDCTL_DSP_GETOPTR:
  2163. if (!(file->f_mode & FMODE_WRITE))
  2164. return -EINVAL;
  2165. if (codec_independent_spdif_locked > 0) {
  2166. if (!dmabuf->ready && (val = prog_dmabuf(state, 2)) != 0)
  2167. return val;
  2168. } else {
  2169. if (controller_independent_spdif_locked > 0) {
  2170. if (!dmabuf->ready && (val = prog_dmabuf(state, 3)) != 0)
  2171. return val;
  2172. } else {
  2173. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  2174. return val;
  2175. }
  2176. }
  2177. spin_lock_irqsave(&state->card->lock, flags);
  2178. val = ali_get_free_write_space(state);
  2179. cinfo.bytes = dmabuf->total_bytes;
  2180. cinfo.ptr = dmabuf->hwptr;
  2181. cinfo.blocks = val / dmabuf->userfragsize;
  2182. if (codec_independent_spdif_locked > 0) {
  2183. if (dmabuf->mapped && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
  2184. dmabuf->count += val;
  2185. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  2186. __ali_update_lvi(state, 2);
  2187. }
  2188. } else {
  2189. if (controller_independent_spdif_locked > 0) {
  2190. if (dmabuf->mapped && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
  2191. dmabuf->count += val;
  2192. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  2193. __ali_update_lvi(state, 3);
  2194. }
  2195. } else {
  2196. if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  2197. dmabuf->count += val;
  2198. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  2199. __ali_update_lvi(state, 0);
  2200. }
  2201. }
  2202. }
  2203. spin_unlock_irqrestore(&state->card->lock, flags);
  2204. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2205. printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %d\n", cinfo.bytes,
  2206. cinfo.blocks, cinfo.ptr, dmabuf->count);
  2207. #endif
  2208. return copy_to_user(argp, &cinfo, sizeof(cinfo))? -EFAULT : 0;
  2209. case SNDCTL_DSP_GETISPACE:
  2210. if (!(file->f_mode & FMODE_READ))
  2211. return -EINVAL;
  2212. if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
  2213. return val;
  2214. spin_lock_irqsave(&state->card->lock, flags);
  2215. abinfo.bytes = ali_get_available_read_data(state);
  2216. abinfo.fragsize = dmabuf->userfragsize;
  2217. abinfo.fragstotal = dmabuf->userfrags;
  2218. abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
  2219. spin_unlock_irqrestore(&state->card->lock, flags);
  2220. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2221. printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %d\n",
  2222. abinfo.bytes, abinfo.fragsize, abinfo.fragments,
  2223. abinfo.fragstotal);
  2224. #endif
  2225. return copy_to_user(argp, &abinfo,
  2226. sizeof(abinfo)) ? -EFAULT : 0;
  2227. case SNDCTL_DSP_GETIPTR:
  2228. if (!(file->f_mode & FMODE_READ))
  2229. return -EINVAL;
  2230. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  2231. return val;
  2232. spin_lock_irqsave(&state->card->lock, flags);
  2233. val = ali_get_available_read_data(state);
  2234. cinfo.bytes = dmabuf->total_bytes;
  2235. cinfo.blocks = val / dmabuf->userfragsize;
  2236. cinfo.ptr = dmabuf->hwptr;
  2237. if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  2238. dmabuf->count -= val;
  2239. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  2240. __ali_update_lvi(state, 1);
  2241. }
  2242. spin_unlock_irqrestore(&state->card->lock, flags);
  2243. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2244. printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %d\n", cinfo.bytes,
  2245. cinfo.blocks, cinfo.ptr, dmabuf->count);
  2246. #endif
  2247. return copy_to_user(argp, &cinfo, sizeof(cinfo))? -EFAULT: 0;
  2248. case SNDCTL_DSP_NONBLOCK:
  2249. #ifdef DEBUG
  2250. printk("SNDCTL_DSP_NONBLOCK\n");
  2251. #endif
  2252. file->f_flags |= O_NONBLOCK;
  2253. return 0;
  2254. case SNDCTL_DSP_GETCAPS:
  2255. #ifdef DEBUG
  2256. printk("SNDCTL_DSP_GETCAPS\n");
  2257. #endif
  2258. return put_user(DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
  2259. DSP_CAP_MMAP | DSP_CAP_BIND, p);
  2260. case SNDCTL_DSP_GETTRIGGER:
  2261. val = 0;
  2262. #ifdef DEBUG
  2263. printk("SNDCTL_DSP_GETTRIGGER 0x%x\n", dmabuf->trigger);
  2264. #endif
  2265. return put_user(dmabuf->trigger, p);
  2266. case SNDCTL_DSP_SETTRIGGER:
  2267. if (get_user(val, p))
  2268. return -EFAULT;
  2269. #if defined(DEBUG) || defined(DEBUG_MMAP)
  2270. printk("SNDCTL_DSP_SETTRIGGER 0x%x\n", val);
  2271. #endif
  2272. if (!(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
  2273. stop_adc(state);
  2274. }
  2275. if (!(val & PCM_ENABLE_OUTPUT) && dmabuf->enable == DAC_RUNNING) {
  2276. stop_dac(state);
  2277. }
  2278. if (!(val & SPDIF_ENABLE_OUTPUT) && dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
  2279. stop_spdifout(state);
  2280. }
  2281. if (!(val & SPDIF_ENABLE_OUTPUT) && dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
  2282. stop_spdifout(state);
  2283. }
  2284. dmabuf->trigger = val;
  2285. if (val & PCM_ENABLE_OUTPUT && !(dmabuf->enable & DAC_RUNNING)) {
  2286. if (!dmabuf->write_channel) {
  2287. dmabuf->ready = 0;
  2288. dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
  2289. if (!dmabuf->write_channel)
  2290. return -EBUSY;
  2291. }
  2292. if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
  2293. return ret;
  2294. if (dmabuf->mapped) {
  2295. spin_lock_irqsave(&state->card->lock, flags);
  2296. ali_update_ptr(state);
  2297. dmabuf->count = 0;
  2298. dmabuf->swptr = dmabuf->hwptr;
  2299. dmabuf->count = ali_get_free_write_space(state);
  2300. dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
  2301. __ali_update_lvi(state, 0);
  2302. spin_unlock_irqrestore(&state->card->lock,
  2303. flags);
  2304. } else
  2305. start_dac(state);
  2306. }
  2307. if (val & SPDIF_ENABLE_OUTPUT && !(dmabuf->enable & CODEC_SPDIFOUT_RUNNING)) {
  2308. if (!dmabuf->codec_spdifout_channel) {
  2309. dmabuf->ready = 0;
  2310. dmabuf->codec_spdifout_channel = state->card->alloc_codec_spdifout_channel(state->card);
  2311. if (!dmabuf->codec_spdifout_channel)
  2312. return -EBUSY;
  2313. }
  2314. if (!dmabuf->ready && (ret = prog_dmabuf(state, 2)))
  2315. return ret;
  2316. if (dmabuf->mapped) {
  2317. spin_lock_irqsave(&state->card->lock, flags);
  2318. ali_update_ptr(state);
  2319. dmabuf->count = 0;
  2320. dmabuf->swptr = dmabuf->hwptr;
  2321. dmabuf->count = ali_get_free_write_space(state);
  2322. dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
  2323. __ali_update_lvi(state, 2);
  2324. spin_unlock_irqrestore(&state->card->lock,
  2325. flags);
  2326. } else
  2327. start_spdifout(state);
  2328. }
  2329. if (val & SPDIF_ENABLE_OUTPUT && !(dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)) {
  2330. if (!dmabuf->controller_spdifout_channel) {
  2331. dmabuf->ready = 0;
  2332. dmabuf->controller_spdifout_channel = state->card->alloc_controller_spdifout_channel(state->card);
  2333. if (!dmabuf->controller_spdifout_channel)
  2334. return -EBUSY;
  2335. }
  2336. if (!dmabuf->ready && (ret = prog_dmabuf(state, 3)))
  2337. return ret;
  2338. if (dmabuf->mapped) {
  2339. spin_lock_irqsave(&state->card->lock, flags);
  2340. ali_update_ptr(state);
  2341. dmabuf->count = 0;
  2342. dmabuf->swptr = dmabuf->hwptr;
  2343. dmabuf->count = ali_get_free_write_space(state);
  2344. dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
  2345. __ali_update_lvi(state, 3);
  2346. spin_unlock_irqrestore(&state->card->lock, flags);
  2347. } else
  2348. start_spdifout(state);
  2349. }
  2350. if (val & PCM_ENABLE_INPUT && !(dmabuf->enable & ADC_RUNNING)) {
  2351. if (!dmabuf->read_channel) {
  2352. dmabuf->ready = 0;
  2353. dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
  2354. if (!dmabuf->read_channel)
  2355. return -EBUSY;
  2356. }
  2357. if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
  2358. return ret;
  2359. if (dmabuf->mapped) {
  2360. spin_lock_irqsave(&state->card->lock,
  2361. flags);
  2362. ali_update_ptr(state);
  2363. dmabuf->swptr = dmabuf->hwptr;
  2364. dmabuf->count = 0;
  2365. spin_unlock_irqrestore(&state->card->lock, flags);
  2366. }
  2367. ali_update_lvi(state, 1);
  2368. start_adc(state);
  2369. }
  2370. return 0;
  2371. case SNDCTL_DSP_SETDUPLEX:
  2372. #ifdef DEBUG
  2373. printk("SNDCTL_DSP_SETDUPLEX\n");
  2374. #endif
  2375. return -EINVAL;
  2376. case SNDCTL_DSP_GETODELAY:
  2377. if (!(file->f_mode & FMODE_WRITE))
  2378. return -EINVAL;
  2379. spin_lock_irqsave(&state->card->lock, flags);
  2380. ali_update_ptr(state);
  2381. val = dmabuf->count;
  2382. spin_unlock_irqrestore(&state->card->lock, flags);
  2383. #ifdef DEBUG
  2384. printk("SNDCTL_DSP_GETODELAY %d\n", dmabuf->count);
  2385. #endif
  2386. return put_user(val, p);
  2387. case SOUND_PCM_READ_RATE:
  2388. #ifdef DEBUG
  2389. printk("SOUND_PCM_READ_RATE %d\n", dmabuf->rate);
  2390. #endif
  2391. return put_user(dmabuf->rate, p);
  2392. case SOUND_PCM_READ_CHANNELS:
  2393. #ifdef DEBUG
  2394. printk("SOUND_PCM_READ_CHANNELS\n");
  2395. #endif
  2396. return put_user(2, p);
  2397. case SOUND_PCM_READ_BITS:
  2398. #ifdef DEBUG
  2399. printk("SOUND_PCM_READ_BITS\n");
  2400. #endif
  2401. return put_user(AFMT_S16_LE, p);
  2402. case SNDCTL_DSP_SETSPDIF: /* Set S/PDIF Control register */
  2403. #ifdef DEBUG
  2404. printk("SNDCTL_DSP_SETSPDIF\n");
  2405. #endif
  2406. if (get_user(val, p))
  2407. return -EFAULT;
  2408. /* Check to make sure the codec supports S/PDIF transmitter */
  2409. if ((state->card->ac97_features & 4)) {
  2410. /* mask out the transmitter speed bits so the user can't set them */
  2411. val &= ~0x3000;
  2412. /* Add the current transmitter speed bits to the passed value */
  2413. ret = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
  2414. val |= (ret & 0x3000);
  2415. ali_ac97_set(codec, AC97_SPDIF_CONTROL, val);
  2416. if (ali_ac97_get(codec, AC97_SPDIF_CONTROL) != val) {
  2417. printk(KERN_ERR "ali_audio: Unable to set S/PDIF configuration to 0x%04x.\n", val);
  2418. return -EFAULT;
  2419. }
  2420. }
  2421. #ifdef DEBUG
  2422. else
  2423. printk(KERN_WARNING "ali_audio: S/PDIF transmitter not avalible.\n");
  2424. #endif
  2425. return put_user(val, p);
  2426. case SNDCTL_DSP_GETSPDIF: /* Get S/PDIF Control register */
  2427. #ifdef DEBUG
  2428. printk("SNDCTL_DSP_GETSPDIF\n");
  2429. #endif
  2430. if (get_user(val, p))
  2431. return -EFAULT;
  2432. /* Check to make sure the codec supports S/PDIF transmitter */
  2433. if (!(state->card->ac97_features & 4)) {
  2434. #ifdef DEBUG
  2435. printk(KERN_WARNING "ali_audio: S/PDIF transmitter not avalible.\n");
  2436. #endif
  2437. val = 0;
  2438. } else {
  2439. val = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
  2440. }
  2441. return put_user(val, p);
  2442. //end add support spdif out
  2443. //add support 4,6 channel
  2444. case SNDCTL_DSP_GETCHANNELMASK:
  2445. #ifdef DEBUG
  2446. printk("SNDCTL_DSP_GETCHANNELMASK\n");
  2447. #endif
  2448. if (get_user(val, p))
  2449. return -EFAULT;
  2450. /* Based on AC'97 DAC support, not ICH hardware */
  2451. val = DSP_BIND_FRONT;
  2452. if (state->card->ac97_features & 0x0004)
  2453. val |= DSP_BIND_SPDIF;
  2454. if (state->card->ac97_features & 0x0080)
  2455. val |= DSP_BIND_SURR;
  2456. if (state->card->ac97_features & 0x0140)
  2457. val |= DSP_BIND_CENTER_LFE;
  2458. return put_user(val, p);
  2459. case SNDCTL_DSP_BIND_CHANNEL:
  2460. #ifdef DEBUG
  2461. printk("SNDCTL_DSP_BIND_CHANNEL\n");
  2462. #endif
  2463. if (get_user(val, p))
  2464. return -EFAULT;
  2465. if (val == DSP_BIND_QUERY) {
  2466. val = DSP_BIND_FRONT; /* Always report this as being enabled */
  2467. if (state->card->ac97_status & SPDIF_ON)
  2468. val |= DSP_BIND_SPDIF;
  2469. else {
  2470. if (state->card->ac97_status & SURR_ON)
  2471. val |= DSP_BIND_SURR;
  2472. if (state->card->
  2473. ac97_status & CENTER_LFE_ON)
  2474. val |= DSP_BIND_CENTER_LFE;
  2475. }
  2476. } else { /* Not a query, set it */
  2477. if (!(file->f_mode & FMODE_WRITE))
  2478. return -EINVAL;
  2479. if (dmabuf->enable == DAC_RUNNING) {
  2480. stop_dac(state);
  2481. }
  2482. if (val & DSP_BIND_SPDIF) { /* Turn on SPDIF */
  2483. /* Ok, this should probably define what slots
  2484. * to use. For now, we'll only set it to the
  2485. * defaults:
  2486. *
  2487. * non multichannel codec maps to slots 3&4
  2488. * 2 channel codec maps to slots 7&8
  2489. * 4 channel codec maps to slots 6&9
  2490. * 6 channel codec maps to slots 10&11
  2491. *
  2492. * there should be some way for the app to
  2493. * select the slot assignment.
  2494. */
  2495. i_scr = inl(state->card->iobase + ALI_SCR);
  2496. if (codec_independent_spdif_locked > 0) {
  2497. if ((i_scr & 0x00300000) == 0x00100000) {
  2498. ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
  2499. } else {
  2500. if ((i_scr & 0x00300000) == 0x00200000) {
  2501. ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
  2502. } else {
  2503. if ((i_scr & 0x00300000) == 0x00300000) {
  2504. ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
  2505. }
  2506. }
  2507. }
  2508. } else { /* codec spdif out (pcm out share ) */
  2509. ali_set_spdif_output(state, AC97_EA_SPSA_3_4, dmabuf->rate); //I do not modify
  2510. }
  2511. if (!(state->card->ac97_status & SPDIF_ON))
  2512. val &= ~DSP_BIND_SPDIF;
  2513. } else {
  2514. int mask;
  2515. int channels;
  2516. /* Turn off S/PDIF if it was on */
  2517. if (state->card->ac97_status & SPDIF_ON)
  2518. ali_set_spdif_output(state, -1, 0);
  2519. mask =
  2520. val & (DSP_BIND_FRONT | DSP_BIND_SURR |
  2521. DSP_BIND_CENTER_LFE);
  2522. switch (mask) {
  2523. case DSP_BIND_FRONT:
  2524. channels = 2;
  2525. break;
  2526. case DSP_BIND_FRONT | DSP_BIND_SURR:
  2527. channels = 4;
  2528. break;
  2529. case DSP_BIND_FRONT | DSP_BIND_SURR | DSP_BIND_CENTER_LFE:
  2530. channels = 6;
  2531. break;
  2532. default:
  2533. val = DSP_BIND_FRONT;
  2534. channels = 2;
  2535. break;
  2536. }
  2537. ali_set_dac_channels(state, channels);
  2538. /* check that they really got turned on */
  2539. if (!state->card->ac97_status & SURR_ON)
  2540. val &= ~DSP_BIND_SURR;
  2541. if (!state->card->
  2542. ac97_status & CENTER_LFE_ON)
  2543. val &= ~DSP_BIND_CENTER_LFE;
  2544. }
  2545. }
  2546. return put_user(val, p);
  2547. case SNDCTL_DSP_MAPINBUF:
  2548. case SNDCTL_DSP_MAPOUTBUF:
  2549. case SNDCTL_DSP_SETSYNCRO:
  2550. case SOUND_PCM_WRITE_FILTER:
  2551. case SOUND_PCM_READ_FILTER:
  2552. return -EINVAL;
  2553. }
  2554. return -EINVAL;
  2555. }
  2556. static int ali_open(struct inode *inode, struct file *file)
  2557. {
  2558. int i = 0;
  2559. struct ali_card *card = devs;
  2560. struct ali_state *state = NULL;
  2561. struct dmabuf *dmabuf = NULL;
  2562. unsigned int i_scr;
  2563. /* find an available virtual channel (instance of /dev/dsp) */
  2564. while (card != NULL) {
  2565. /*
  2566. * If we are initializing and then fail, card could go
  2567. * away unuexpectedly while we are in the for() loop.
  2568. * So, check for card on each iteration before we check
  2569. * for card->initializing to avoid a possible oops.
  2570. * This usually only matters for times when the driver is
  2571. * autoloaded by kmod.
  2572. */
  2573. for (i = 0; i < 50 && card && card->initializing; i++) {
  2574. set_current_state(TASK_UNINTERRUPTIBLE);
  2575. schedule_timeout(HZ / 20);
  2576. }
  2577. for (i = 0; i < NR_HW_CH && card && !card->initializing; i++) {
  2578. if (card->states[i] == NULL) {
  2579. state = card->states[i] = (struct ali_state *) kmalloc(sizeof(struct ali_state), GFP_KERNEL);
  2580. if (state == NULL)
  2581. return -ENOMEM;
  2582. memset(state, 0, sizeof(struct ali_state));
  2583. dmabuf = &state->dmabuf;
  2584. goto found_virt;
  2585. }
  2586. }
  2587. card = card->next;
  2588. }
  2589. /* no more virtual channel avaiable */
  2590. if (!state)
  2591. return -ENODEV;
  2592. found_virt:
  2593. /* initialize the virtual channel */
  2594. state->virt = i;
  2595. state->card = card;
  2596. state->magic = ALI5455_STATE_MAGIC;
  2597. init_waitqueue_head(&dmabuf->wait);
  2598. init_MUTEX(&state->open_sem);
  2599. file->private_data = state;
  2600. dmabuf->trigger = 0;
  2601. /* allocate hardware channels */
  2602. if (file->f_mode & FMODE_READ) {
  2603. if ((dmabuf->read_channel =
  2604. card->alloc_rec_pcm_channel(card)) == NULL) {
  2605. kfree(card->states[i]);
  2606. card->states[i] = NULL;
  2607. return -EBUSY;
  2608. }
  2609. dmabuf->trigger |= PCM_ENABLE_INPUT;
  2610. ali_set_adc_rate(state, 8000);
  2611. }
  2612. if (file->f_mode & FMODE_WRITE) {
  2613. if (codec_independent_spdif_locked > 0) {
  2614. if ((dmabuf->codec_spdifout_channel = card->alloc_codec_spdifout_channel(card)) == NULL) {
  2615. kfree(card->states[i]);
  2616. card->states[i] = NULL;
  2617. return -EBUSY;
  2618. }
  2619. dmabuf->trigger |= SPDIF_ENABLE_OUTPUT;
  2620. ali_set_codecspdifout_rate(state, codec_independent_spdif_locked); //It must add
  2621. i_scr = inl(state->card->iobase + ALI_SCR);
  2622. if ((i_scr & 0x00300000) == 0x00100000) {
  2623. ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
  2624. } else {
  2625. if ((i_scr & 0x00300000) == 0x00200000) {
  2626. ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
  2627. } else {
  2628. if ((i_scr & 0x00300000) == 0x00300000) {
  2629. ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
  2630. } else {
  2631. ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
  2632. }
  2633. }
  2634. }
  2635. } else {
  2636. if (controller_independent_spdif_locked > 0) {
  2637. if ((dmabuf->controller_spdifout_channel = card->alloc_controller_spdifout_channel(card)) == NULL) {
  2638. kfree(card->states[i]);
  2639. card->states[i] = NULL;
  2640. return -EBUSY;
  2641. }
  2642. dmabuf->trigger |= SPDIF_ENABLE_OUTPUT;
  2643. ali_set_spdifout_rate(state, controller_independent_spdif_locked);
  2644. } else {
  2645. if ((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
  2646. kfree(card->states[i]);
  2647. card->states[i] = NULL;
  2648. return -EBUSY;
  2649. }
  2650. /* Initialize to 8kHz? What if we don't support 8kHz? */
  2651. /* Let's change this to check for S/PDIF stuff */
  2652. dmabuf->trigger |= PCM_ENABLE_OUTPUT;
  2653. if (codec_pcmout_share_spdif_locked) {
  2654. ali_set_dac_rate(state, codec_pcmout_share_spdif_locked);
  2655. ali_set_spdif_output(state, AC97_EA_SPSA_3_4, codec_pcmout_share_spdif_locked);
  2656. } else {
  2657. ali_set_dac_rate(state, 8000);
  2658. }
  2659. }
  2660. }
  2661. }
  2662. /* set default sample format. According to OSS Programmer's Guide /dev/dsp
  2663. should be default to unsigned 8-bits, mono, with sample rate 8kHz and
  2664. /dev/dspW will accept 16-bits sample, but we don't support those so we
  2665. set it immediately to stereo and 16bit, which is all we do support */
  2666. dmabuf->fmt |= ALI5455_FMT_16BIT | ALI5455_FMT_STEREO;
  2667. dmabuf->ossfragsize = 0;
  2668. dmabuf->ossmaxfrags = 0;
  2669. dmabuf->subdivision = 0;
  2670. state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
  2671. outl(0x00000000, card->iobase + ALI_INTERRUPTCR);
  2672. outl(0x00000000, card->iobase + ALI_INTERRUPTSR);
  2673. return nonseekable_open(inode, file);
  2674. }
  2675. static int ali_release(struct inode *inode, struct file *file)
  2676. {
  2677. struct ali_state *state = (struct ali_state *) file->private_data;
  2678. struct ali_card *card = state->card;
  2679. struct dmabuf *dmabuf = &state->dmabuf;
  2680. unsigned long flags;
  2681. lock_kernel();
  2682. /* stop DMA state machine and free DMA buffers/channels */
  2683. if (dmabuf->trigger & PCM_ENABLE_OUTPUT)
  2684. drain_dac(state, 0);
  2685. if (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)
  2686. drain_spdifout(state, 0);
  2687. if (dmabuf->trigger & PCM_ENABLE_INPUT)
  2688. stop_adc(state);
  2689. spin_lock_irqsave(&card->lock, flags);
  2690. dealloc_dmabuf(state);
  2691. if (file->f_mode & FMODE_WRITE) {
  2692. if (codec_independent_spdif_locked > 0) {
  2693. state->card->free_pcm_channel(state->card, dmabuf->codec_spdifout_channel->num);
  2694. } else {
  2695. if (controller_independent_spdif_locked > 0)
  2696. state->card->free_pcm_channel(state->card,
  2697. dmabuf->controller_spdifout_channel->num);
  2698. else state->card->free_pcm_channel(state->card,
  2699. dmabuf->write_channel->num);
  2700. }
  2701. }
  2702. if (file->f_mode & FMODE_READ)
  2703. state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
  2704. state->card->states[state->virt] = NULL;
  2705. kfree(state);
  2706. spin_unlock_irqrestore(&card->lock, flags);
  2707. unlock_kernel();
  2708. return 0;
  2709. }
  2710. static /*const */ struct file_operations ali_audio_fops = {
  2711. .owner = THIS_MODULE,
  2712. .llseek = no_llseek,
  2713. .read = ali_read,
  2714. .write = ali_write,
  2715. .poll = ali_poll,
  2716. .ioctl = ali_ioctl,
  2717. .mmap = ali_mmap,
  2718. .open = ali_open,
  2719. .release = ali_release,
  2720. };
  2721. /* Read AC97 codec registers */
  2722. static u16 ali_ac97_get(struct ac97_codec *dev, u8 reg)
  2723. {
  2724. struct ali_card *card = dev->private_data;
  2725. int count1 = 100;
  2726. char val;
  2727. unsigned short int data = 0, count, addr1, addr2 = 0;
  2728. spin_lock(&card->ac97_lock);
  2729. while (count1-- && (inl(card->iobase + ALI_CAS) & 0x80000000))
  2730. udelay(1);
  2731. addr1 = reg;
  2732. reg |= 0x0080;
  2733. for (count = 0; count < 0x7f; count++) {
  2734. val = inb(card->iobase + ALI_CSPSR);
  2735. if (val & 0x08)
  2736. break;
  2737. }
  2738. if (count == 0x7f)
  2739. {
  2740. spin_unlock(&card->ac97_lock);
  2741. return -1;
  2742. }
  2743. outw(reg, (card->iobase + ALI_CPR) + 2);
  2744. for (count = 0; count < 0x7f; count++) {
  2745. val = inb(card->iobase + ALI_CSPSR);
  2746. if (val & 0x02) {
  2747. data = inw(card->iobase + ALI_SPR);
  2748. addr2 = inw((card->iobase + ALI_SPR) + 2);
  2749. break;
  2750. }
  2751. }
  2752. spin_unlock(&card->ac97_lock);
  2753. if (count == 0x7f)
  2754. return -1;
  2755. if (addr2 != addr1)
  2756. return -1;
  2757. return ((u16) data);
  2758. }
  2759. /* write ac97 codec register */
  2760. static void ali_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
  2761. {
  2762. struct ali_card *card = dev->private_data;
  2763. int count1 = 100;
  2764. char val;
  2765. unsigned short int count;
  2766. spin_lock(&card->ac97_lock);
  2767. while (count1-- && (inl(card->iobase + ALI_CAS) & 0x80000000))
  2768. udelay(1);
  2769. for (count = 0; count < 0x7f; count++) {
  2770. val = inb(card->iobase + ALI_CSPSR);
  2771. if (val & 0x08)
  2772. break;
  2773. }
  2774. if (count == 0x7f) {
  2775. printk(KERN_WARNING "ali_ac97_set: AC97 codec register access timed out. \n");
  2776. spin_unlock(&card->ac97_lock);
  2777. return;
  2778. }
  2779. outw(data, (card->iobase + ALI_CPR));
  2780. outb(reg, (card->iobase + ALI_CPR) + 2);
  2781. for (count = 0; count < 0x7f; count++) {
  2782. val = inb(card->iobase + ALI_CSPSR);
  2783. if (val & 0x01)
  2784. break;
  2785. }
  2786. spin_unlock(&card->ac97_lock);
  2787. if (count == 0x7f)
  2788. printk(KERN_WARNING "ali_ac97_set: AC97 codec register access timed out. \n");
  2789. return;
  2790. }
  2791. /* OSS /dev/mixer file operation methods */
  2792. static int ali_open_mixdev(struct inode *inode, struct file *file)
  2793. {
  2794. int i;
  2795. int minor = iminor(inode);
  2796. struct ali_card *card = devs;
  2797. for (card = devs; card != NULL; card = card->next) {
  2798. /*
  2799. * If we are initializing and then fail, card could go
  2800. * away unuexpectedly while we are in the for() loop.
  2801. * So, check for card on each iteration before we check
  2802. * for card->initializing to avoid a possible oops.
  2803. * This usually only matters for times when the driver is
  2804. * autoloaded by kmod.
  2805. */
  2806. for (i = 0; i < 50 && card && card->initializing; i++) {
  2807. set_current_state(TASK_UNINTERRUPTIBLE);
  2808. schedule_timeout(HZ / 20);
  2809. }
  2810. for (i = 0; i < NR_AC97 && card && !card->initializing; i++)
  2811. if (card->ac97_codec[i] != NULL
  2812. && card->ac97_codec[i]->dev_mixer == minor) {
  2813. file->private_data = card->ac97_codec[i];
  2814. return nonseekable_open(inode, file);
  2815. }
  2816. }
  2817. return -ENODEV;
  2818. }
  2819. static int ali_ioctl_mixdev(struct inode *inode,
  2820. struct file *file,
  2821. unsigned int cmd, unsigned long arg)
  2822. {
  2823. struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
  2824. return codec->mixer_ioctl(codec, cmd, arg);
  2825. }
  2826. static /*const */ struct file_operations ali_mixer_fops = {
  2827. .owner = THIS_MODULE,
  2828. .llseek = no_llseek,
  2829. .ioctl = ali_ioctl_mixdev,
  2830. .open = ali_open_mixdev,
  2831. };
  2832. /* AC97 codec initialisation. These small functions exist so we don't
  2833. duplicate code between module init and apm resume */
  2834. static inline int ali_ac97_exists(struct ali_card *card, int ac97_number)
  2835. {
  2836. unsigned int i = 1;
  2837. u32 reg = inl(card->iobase + ALI_RTSR);
  2838. if (ac97_number) {
  2839. while (i < 100) {
  2840. reg = inl(card->iobase + ALI_RTSR);
  2841. if (reg & 0x40) {
  2842. break;
  2843. } else {
  2844. outl(reg | 0x00000040,
  2845. card->iobase + 0x34);
  2846. udelay(1);
  2847. }
  2848. i++;
  2849. }
  2850. } else {
  2851. while (i < 100) {
  2852. reg = inl(card->iobase + ALI_RTSR);
  2853. if (reg & 0x80) {
  2854. break;
  2855. } else {
  2856. outl(reg | 0x00000080,
  2857. card->iobase + 0x34);
  2858. udelay(1);
  2859. }
  2860. i++;
  2861. }
  2862. }
  2863. if (ac97_number)
  2864. return reg & 0x40;
  2865. else
  2866. return reg & 0x80;
  2867. }
  2868. static inline int ali_ac97_enable_variable_rate(struct ac97_codec *codec)
  2869. {
  2870. ali_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
  2871. ali_ac97_set(codec, AC97_EXTENDED_STATUS, ali_ac97_get(codec, AC97_EXTENDED_STATUS) | 0xE800);
  2872. return (ali_ac97_get(codec, AC97_EXTENDED_STATUS) & 1);
  2873. }
  2874. static int ali_ac97_probe_and_powerup(struct ali_card *card, struct ac97_codec *codec)
  2875. {
  2876. /* Returns 0 on failure */
  2877. int i;
  2878. u16 addr;
  2879. if (ac97_probe_codec(codec) == 0)
  2880. return 0;
  2881. /* ac97_probe_codec is success ,then begin to init codec */
  2882. ali_ac97_set(codec, AC97_RESET, 0xffff);
  2883. if (card->channel[0].used == 1) {
  2884. ali_ac97_set(codec, AC97_RECORD_SELECT, 0x0000);
  2885. ali_ac97_set(codec, AC97_LINEIN_VOL, 0x0808);
  2886. ali_ac97_set(codec, AC97_RECORD_GAIN, 0x0F0F);
  2887. }
  2888. if (card->channel[2].used == 1) //if MICin then init codec
  2889. {
  2890. ali_ac97_set(codec, AC97_RECORD_SELECT, 0x0000);
  2891. ali_ac97_set(codec, AC97_MIC_VOL, 0x8808);
  2892. ali_ac97_set(codec, AC97_RECORD_GAIN, 0x0F0F);
  2893. ali_ac97_set(codec, AC97_RECORD_GAIN_MIC, 0x0000);
  2894. }
  2895. ali_ac97_set(codec, AC97_MASTER_VOL_STEREO, 0x0000);
  2896. ali_ac97_set(codec, AC97_HEADPHONE_VOL, 0x0000);
  2897. ali_ac97_set(codec, AC97_PCMOUT_VOL, 0x0000);
  2898. ali_ac97_set(codec, AC97_CD_VOL, 0x0808);
  2899. ali_ac97_set(codec, AC97_VIDEO_VOL, 0x0808);
  2900. ali_ac97_set(codec, AC97_AUX_VOL, 0x0808);
  2901. ali_ac97_set(codec, AC97_PHONE_VOL, 0x8048);
  2902. ali_ac97_set(codec, AC97_PCBEEP_VOL, 0x0000);
  2903. ali_ac97_set(codec, AC97_GENERAL_PURPOSE, AC97_GP_MIX);
  2904. ali_ac97_set(codec, AC97_MASTER_VOL_MONO, 0x0000);
  2905. ali_ac97_set(codec, 0x38, 0x0000);
  2906. addr = ali_ac97_get(codec, 0x2a);
  2907. ali_ac97_set(codec, 0x2a, addr | 0x0001);
  2908. addr = ali_ac97_get(codec, 0x2a);
  2909. addr = ali_ac97_get(codec, 0x28);
  2910. ali_ac97_set(codec, 0x2c, 0xbb80);
  2911. addr = ali_ac97_get(codec, 0x2c);
  2912. /* power it all up */
  2913. ali_ac97_set(codec, AC97_POWER_CONTROL,
  2914. ali_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
  2915. /* wait for analog ready */
  2916. for (i = 10; i && ((ali_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf); i--) {
  2917. set_current_state(TASK_UNINTERRUPTIBLE);
  2918. schedule_timeout(HZ / 20);
  2919. }
  2920. /* FIXME !! */
  2921. i++;
  2922. return i;
  2923. }
  2924. /* I clone ali5455(2.4.7 ) not clone i810_audio(2.4.18) */
  2925. static int ali_reset_5455(struct ali_card *card)
  2926. {
  2927. outl(0x80000003, card->iobase + ALI_SCR);
  2928. outl(0x83838383, card->iobase + ALI_FIFOCR1);
  2929. outl(0x83838383, card->iobase + ALI_FIFOCR2);
  2930. if (controller_pcmout_share_spdif_locked > 0) {
  2931. outl((inl(card->iobase + ALI_SPDIFICS) | 0x00000001),
  2932. card->iobase + ALI_SPDIFICS);
  2933. outl(0x0408000a, card->iobase + ALI_INTERFACECR);
  2934. } else {
  2935. if (codec_independent_spdif_locked > 0) {
  2936. outl((inl(card->iobase + ALI_SCR) | 0x00100000), card->iobase + ALI_SCR); // now I select slot 7 & 8
  2937. outl(0x00200000, card->iobase + ALI_INTERFACECR); //enable codec independent spdifout
  2938. } else
  2939. outl(0x04080002, card->iobase + ALI_INTERFACECR);
  2940. }
  2941. outl(0x00000000, card->iobase + ALI_INTERRUPTCR);
  2942. outl(0x00000000, card->iobase + ALI_INTERRUPTSR);
  2943. if (controller_independent_spdif_locked > 0)
  2944. outl((inl(card->iobase + ALI_SPDIFICS) | 0x00000001),
  2945. card->iobase + ALI_SPDIFICS);
  2946. return 1;
  2947. }
  2948. static int ali_ac97_random_init_stuff(struct ali_card
  2949. *card)
  2950. {
  2951. u32 reg = inl(card->iobase + ALI_SCR);
  2952. int i = 0;
  2953. reg = inl(card->iobase + ALI_SCR);
  2954. if ((reg & 2) == 0) /* Cold required */
  2955. reg |= 2;
  2956. else
  2957. reg |= 1; /* Warm */
  2958. reg &= ~0x80000000; /* ACLink on */
  2959. outl(reg, card->iobase + ALI_SCR);
  2960. while (i < 10) {
  2961. if ((inl(card->iobase + 0x18) & (1 << 1)) == 0)
  2962. break;
  2963. current->state = TASK_UNINTERRUPTIBLE;
  2964. schedule_timeout(HZ / 20);
  2965. i++;
  2966. }
  2967. if (i == 10) {
  2968. printk(KERN_ERR "ali_audio: AC'97 reset failed.\n");
  2969. return 0;
  2970. }
  2971. set_current_state(TASK_UNINTERRUPTIBLE);
  2972. schedule_timeout(HZ / 2);
  2973. return 1;
  2974. }
  2975. /* AC97 codec initialisation. */
  2976. static int __devinit ali_ac97_init(struct ali_card *card)
  2977. {
  2978. int num_ac97 = 0;
  2979. int total_channels = 0;
  2980. struct ac97_codec *codec;
  2981. u16 eid;
  2982. if (!ali_ac97_random_init_stuff(card))
  2983. return 0;
  2984. /* Number of channels supported */
  2985. /* What about the codec? Just because the ICH supports */
  2986. /* multiple channels doesn't mean the codec does. */
  2987. /* we'll have to modify this in the codec section below */
  2988. /* to reflect what the codec has. */
  2989. /* ICH and ICH0 only support 2 channels so don't bother */
  2990. /* to check.... */
  2991. inl(card->iobase + ALI_CPR);
  2992. card->channels = 2;
  2993. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  2994. /* Assume codec isn't available until we go through the
  2995. * gauntlet below */
  2996. card->ac97_codec[num_ac97] = NULL;
  2997. /* The ICH programmer's reference says you should */
  2998. /* check the ready status before probing. So we chk */
  2999. /* What do we do if it's not ready? Wait and try */
  3000. /* again, or abort? */
  3001. if (!ali_ac97_exists(card, num_ac97)) {
  3002. if (num_ac97 == 0)
  3003. printk(KERN_ERR "ali_audio: Primary codec not ready.\n");
  3004. break;
  3005. }
  3006. if ((codec = ac97_alloc_codec()) == NULL)
  3007. return -ENOMEM;
  3008. /* initialize some basic codec information, other fields will be filled
  3009. in ac97_probe_codec */
  3010. codec->private_data = card;
  3011. codec->id = num_ac97;
  3012. codec->codec_read = ali_ac97_get;
  3013. codec->codec_write = ali_ac97_set;
  3014. if (!ali_ac97_probe_and_powerup(card, codec)) {
  3015. printk(KERN_ERR "ali_audio: timed out waiting for codec %d analog ready",
  3016. num_ac97);
  3017. kfree(codec);
  3018. break; /* it didn't work */
  3019. }
  3020. /* Store state information about S/PDIF transmitter */
  3021. card->ac97_status = 0;
  3022. /* Don't attempt to get eid until powerup is complete */
  3023. eid = ali_ac97_get(codec, AC97_EXTENDED_ID);
  3024. if (eid == 0xFFFF) {
  3025. printk(KERN_ERR "ali_audio: no codec attached ?\n");
  3026. kfree(codec);
  3027. break;
  3028. }
  3029. card->ac97_features = eid;
  3030. /* Now check the codec for useful features to make up for
  3031. the dumbness of the ali5455 hardware engine */
  3032. if (!(eid & 0x0001))
  3033. printk(KERN_WARNING
  3034. "ali_audio: only 48Khz playback available.\n");
  3035. else {
  3036. if (!ali_ac97_enable_variable_rate(codec)) {
  3037. printk(KERN_WARNING
  3038. "ali_audio: Codec refused to allow VRA, using 48Khz only.\n");
  3039. card->ac97_features &= ~1;
  3040. }
  3041. }
  3042. /* Determine how many channels the codec(s) support */
  3043. /* - The primary codec always supports 2 */
  3044. /* - If the codec supports AMAP, surround DACs will */
  3045. /* automaticlly get assigned to slots. */
  3046. /* * Check for surround DACs and increment if */
  3047. /* found. */
  3048. /* - Else check if the codec is revision 2.2 */
  3049. /* * If surround DACs exist, assign them to slots */
  3050. /* and increment channel count. */
  3051. /* All of this only applies to ICH2 and above. ICH */
  3052. /* and ICH0 only support 2 channels. ICH2 will only */
  3053. /* support multiple codecs in a "split audio" config. */
  3054. /* as described above. */
  3055. /* TODO: Remove all the debugging messages! */
  3056. if ((eid & 0xc000) == 0) /* primary codec */
  3057. total_channels += 2;
  3058. if ((codec->dev_mixer = register_sound_mixer(&ali_mixer_fops, -1)) < 0) {
  3059. printk(KERN_ERR "ali_audio: couldn't register mixer!\n");
  3060. kfree(codec);
  3061. break;
  3062. }
  3063. card->ac97_codec[num_ac97] = codec;
  3064. }
  3065. /* pick the minimum of channels supported by ICHx or codec(s) */
  3066. card->channels = (card->channels > total_channels) ? total_channels : card->channels;
  3067. return num_ac97;
  3068. }
  3069. static void __devinit ali_configure_clocking(void)
  3070. {
  3071. struct ali_card *card;
  3072. struct ali_state *state;
  3073. struct dmabuf *dmabuf;
  3074. unsigned int i, offset, new_offset;
  3075. unsigned long flags;
  3076. card = devs;
  3077. /* We could try to set the clocking for multiple cards, but can you even have
  3078. * more than one ali in a machine? Besides, clocking is global, so unless
  3079. * someone actually thinks more than one ali in a machine is possible and
  3080. * decides to rewrite that little bit, setting the rate for more than one card
  3081. * is a waste of time.
  3082. */
  3083. if (card != NULL) {
  3084. state = card->states[0] = (struct ali_state *)
  3085. kmalloc(sizeof(struct ali_state), GFP_KERNEL);
  3086. if (state == NULL)
  3087. return;
  3088. memset(state, 0, sizeof(struct ali_state));
  3089. dmabuf = &state->dmabuf;
  3090. dmabuf->write_channel = card->alloc_pcm_channel(card);
  3091. state->virt = 0;
  3092. state->card = card;
  3093. state->magic = ALI5455_STATE_MAGIC;
  3094. init_waitqueue_head(&dmabuf->wait);
  3095. init_MUTEX(&state->open_sem);
  3096. dmabuf->fmt = ALI5455_FMT_STEREO | ALI5455_FMT_16BIT;
  3097. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  3098. ali_set_dac_rate(state, 48000);
  3099. if (prog_dmabuf(state, 0) != 0)
  3100. goto config_out_nodmabuf;
  3101. if (dmabuf->dmasize < 16384)
  3102. goto config_out;
  3103. dmabuf->count = dmabuf->dmasize;
  3104. outb(31, card->iobase + dmabuf->write_channel->port + OFF_LVI);
  3105. local_irq_save(flags);
  3106. start_dac(state);
  3107. offset = ali_get_dma_addr(state, 0);
  3108. mdelay(50);
  3109. new_offset = ali_get_dma_addr(state, 0);
  3110. stop_dac(state);
  3111. outb(2, card->iobase + dmabuf->write_channel->port + OFF_CR);
  3112. local_irq_restore(flags);
  3113. i = new_offset - offset;
  3114. if (i == 0)
  3115. goto config_out;
  3116. i = i / 4 * 20;
  3117. if (i > 48500 || i < 47500) {
  3118. clocking = clocking * clocking / i;
  3119. }
  3120. config_out:
  3121. dealloc_dmabuf(state);
  3122. config_out_nodmabuf:
  3123. state->card->free_pcm_channel(state->card, state->dmabuf. write_channel->num);
  3124. kfree(state);
  3125. card->states[0] = NULL;
  3126. }
  3127. }
  3128. /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
  3129. until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
  3130. static int __devinit ali_probe(struct pci_dev *pci_dev,
  3131. const struct pci_device_id *pci_id)
  3132. {
  3133. struct ali_card *card;
  3134. if (pci_enable_device(pci_dev))
  3135. return -EIO;
  3136. if (pci_set_dma_mask(pci_dev, ALI5455_DMA_MASK)) {
  3137. printk(KERN_ERR "ali5455: architecture does not support"
  3138. " 32bit PCI busmaster DMA\n");
  3139. return -ENODEV;
  3140. }
  3141. if ((card = kmalloc(sizeof(struct ali_card), GFP_KERNEL)) == NULL) {
  3142. printk(KERN_ERR "ali_audio: out of memory\n");
  3143. return -ENOMEM;
  3144. }
  3145. memset(card, 0, sizeof(*card));
  3146. card->initializing = 1;
  3147. card->iobase = pci_resource_start(pci_dev, 0);
  3148. card->pci_dev = pci_dev;
  3149. card->pci_id = pci_id->device;
  3150. card->irq = pci_dev->irq;
  3151. card->next = devs;
  3152. card->magic = ALI5455_CARD_MAGIC;
  3153. #ifdef CONFIG_PM
  3154. card->pm_suspended = 0;
  3155. #endif
  3156. spin_lock_init(&card->lock);
  3157. spin_lock_init(&card->ac97_lock);
  3158. devs = card;
  3159. pci_set_master(pci_dev);
  3160. printk(KERN_INFO "ali: %s found at IO 0x%04lx, IRQ %d\n",
  3161. card_names[pci_id->driver_data], card->iobase, card->irq);
  3162. card->alloc_pcm_channel = ali_alloc_pcm_channel;
  3163. card->alloc_rec_pcm_channel = ali_alloc_rec_pcm_channel;
  3164. card->alloc_rec_mic_channel = ali_alloc_rec_mic_channel;
  3165. card->alloc_codec_spdifout_channel = ali_alloc_codec_spdifout_channel;
  3166. card->alloc_controller_spdifout_channel = ali_alloc_controller_spdifout_channel;
  3167. card->free_pcm_channel = ali_free_pcm_channel;
  3168. card->channel[0].offset = 0;
  3169. card->channel[0].port = 0x40;
  3170. card->channel[0].num = 0;
  3171. card->channel[1].offset = 0;
  3172. card->channel[1].port = 0x50;
  3173. card->channel[1].num = 1;
  3174. card->channel[2].offset = 0;
  3175. card->channel[2].port = 0x60;
  3176. card->channel[2].num = 2;
  3177. card->channel[3].offset = 0;
  3178. card->channel[3].port = 0x70;
  3179. card->channel[3].num = 3;
  3180. card->channel[4].offset = 0;
  3181. card->channel[4].port = 0xb0;
  3182. card->channel[4].num = 4;
  3183. /* claim our iospace and irq */
  3184. request_region(card->iobase, 256, card_names[pci_id->driver_data]);
  3185. if (request_irq(card->irq, &ali_interrupt, SA_SHIRQ,
  3186. card_names[pci_id->driver_data], card)) {
  3187. printk(KERN_ERR "ali_audio: unable to allocate irq %d\n",
  3188. card->irq);
  3189. release_region(card->iobase, 256);
  3190. kfree(card);
  3191. return -ENODEV;
  3192. }
  3193. if (ali_reset_5455(card) <= 0) {
  3194. unregister_sound_dsp(card->dev_audio);
  3195. release_region(card->iobase, 256);
  3196. free_irq(card->irq, card);
  3197. kfree(card);
  3198. return -ENODEV;
  3199. }
  3200. /* initialize AC97 codec and register /dev/mixer */
  3201. if (ali_ac97_init(card) < 0) {
  3202. release_region(card->iobase, 256);
  3203. free_irq(card->irq, card);
  3204. kfree(card);
  3205. return -ENODEV;
  3206. }
  3207. pci_set_drvdata(pci_dev, card);
  3208. if (clocking == 0) {
  3209. clocking = 48000;
  3210. ali_configure_clocking();
  3211. }
  3212. /* register /dev/dsp */
  3213. if ((card->dev_audio = register_sound_dsp(&ali_audio_fops, -1)) < 0) {
  3214. int i;
  3215. printk(KERN_ERR"ali_audio: couldn't register DSP device!\n");
  3216. release_region(card->iobase, 256);
  3217. free_irq(card->irq, card);
  3218. for (i = 0; i < NR_AC97; i++)
  3219. if (card->ac97_codec[i] != NULL) {
  3220. unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
  3221. kfree(card->ac97_codec[i]);
  3222. }
  3223. kfree(card);
  3224. return -ENODEV;
  3225. }
  3226. card->initializing = 0;
  3227. return 0;
  3228. }
  3229. static void __devexit ali_remove(struct pci_dev *pci_dev)
  3230. {
  3231. int i;
  3232. struct ali_card *card = pci_get_drvdata(pci_dev);
  3233. /* free hardware resources */
  3234. free_irq(card->irq, devs);
  3235. release_region(card->iobase, 256);
  3236. /* unregister audio devices */
  3237. for (i = 0; i < NR_AC97; i++)
  3238. if (card->ac97_codec[i] != NULL) {
  3239. unregister_sound_mixer(card->ac97_codec[i]->
  3240. dev_mixer);
  3241. ac97_release_codec(card->ac97_codec[i]);
  3242. card->ac97_codec[i] = NULL;
  3243. }
  3244. unregister_sound_dsp(card->dev_audio);
  3245. kfree(card);
  3246. }
  3247. #ifdef CONFIG_PM
  3248. static int ali_pm_suspend(struct pci_dev *dev, pm_message_t pm_state)
  3249. {
  3250. struct ali_card *card = pci_get_drvdata(dev);
  3251. struct ali_state *state;
  3252. unsigned long flags;
  3253. struct dmabuf *dmabuf;
  3254. int i, num_ac97;
  3255. if (!card)
  3256. return 0;
  3257. spin_lock_irqsave(&card->lock, flags);
  3258. card->pm_suspended = 1;
  3259. for (i = 0; i < NR_HW_CH; i++) {
  3260. state = card->states[i];
  3261. if (!state)
  3262. continue;
  3263. /* this happens only if there are open files */
  3264. dmabuf = &state->dmabuf;
  3265. if (dmabuf->enable & DAC_RUNNING ||
  3266. (dmabuf->count
  3267. && (dmabuf->trigger & PCM_ENABLE_OUTPUT))) {
  3268. state->pm_saved_dac_rate = dmabuf->rate;
  3269. stop_dac(state);
  3270. } else {
  3271. state->pm_saved_dac_rate = 0;
  3272. }
  3273. if (dmabuf->enable & ADC_RUNNING) {
  3274. state->pm_saved_adc_rate = dmabuf->rate;
  3275. stop_adc(state);
  3276. } else {
  3277. state->pm_saved_adc_rate = 0;
  3278. }
  3279. dmabuf->ready = 0;
  3280. dmabuf->swptr = dmabuf->hwptr = 0;
  3281. dmabuf->count = dmabuf->total_bytes = 0;
  3282. }
  3283. spin_unlock_irqrestore(&card->lock, flags);
  3284. /* save mixer settings */
  3285. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  3286. struct ac97_codec *codec = card->ac97_codec[num_ac97];
  3287. if (!codec)
  3288. continue;
  3289. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  3290. if ((supported_mixer(codec, i)) && (codec->read_mixer)) {
  3291. card->pm_saved_mixer_settings[i][num_ac97] = codec->read_mixer(codec, i);
  3292. }
  3293. }
  3294. }
  3295. pci_save_state(dev); /* XXX do we need this? */
  3296. pci_disable_device(dev); /* disable busmastering */
  3297. pci_set_power_state(dev, 3); /* Zzz. */
  3298. return 0;
  3299. }
  3300. static int ali_pm_resume(struct pci_dev *dev)
  3301. {
  3302. int num_ac97, i = 0;
  3303. struct ali_card *card = pci_get_drvdata(dev);
  3304. pci_enable_device(dev);
  3305. pci_restore_state(dev);
  3306. /* observation of a toshiba portege 3440ct suggests that the
  3307. hardware has to be more or less completely reinitialized from
  3308. scratch after an apm suspend. Works For Me. -dan */
  3309. ali_ac97_random_init_stuff(card);
  3310. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  3311. struct ac97_codec *codec = card->ac97_codec[num_ac97];
  3312. /* check they haven't stolen the hardware while we were
  3313. away */
  3314. if (!codec || !ali_ac97_exists(card, num_ac97)) {
  3315. if (num_ac97)
  3316. continue;
  3317. else
  3318. BUG();
  3319. }
  3320. if (!ali_ac97_probe_and_powerup(card, codec))
  3321. BUG();
  3322. if ((card->ac97_features & 0x0001)) {
  3323. /* at probe time we found we could do variable
  3324. rates, but APM suspend has made it forget
  3325. its magical powers */
  3326. if (!ali_ac97_enable_variable_rate(codec))
  3327. BUG();
  3328. }
  3329. /* we lost our mixer settings, so restore them */
  3330. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  3331. if (supported_mixer(codec, i)) {
  3332. int val = card->pm_saved_mixer_settings[i][num_ac97];
  3333. codec->mixer_state[i] = val;
  3334. codec->write_mixer(codec, i,
  3335. (val & 0xff),
  3336. ((val >> 8) & 0xff));
  3337. }
  3338. }
  3339. }
  3340. /* we need to restore the sample rate from whatever it was */
  3341. for (i = 0; i < NR_HW_CH; i++) {
  3342. struct ali_state *state = card->states[i];
  3343. if (state) {
  3344. if (state->pm_saved_adc_rate)
  3345. ali_set_adc_rate(state, state->pm_saved_adc_rate);
  3346. if (state->pm_saved_dac_rate)
  3347. ali_set_dac_rate(state, state->pm_saved_dac_rate);
  3348. }
  3349. }
  3350. card->pm_suspended = 0;
  3351. /* any processes that were reading/writing during the suspend
  3352. probably ended up here */
  3353. for (i = 0; i < NR_HW_CH; i++) {
  3354. struct ali_state *state = card->states[i];
  3355. if (state)
  3356. wake_up(&state->dmabuf.wait);
  3357. }
  3358. return 0;
  3359. }
  3360. #endif /* CONFIG_PM */
  3361. MODULE_AUTHOR("");
  3362. MODULE_DESCRIPTION("ALI 5455 audio support");
  3363. MODULE_LICENSE("GPL");
  3364. module_param(clocking, int, 0);
  3365. /* FIXME: bool? */
  3366. module_param(strict_clocking, uint, 0);
  3367. module_param(codec_pcmout_share_spdif_locked, uint, 0);
  3368. module_param(codec_independent_spdif_locked, uint, 0);
  3369. module_param(controller_pcmout_share_spdif_locked, uint, 0);
  3370. module_param(controller_independent_spdif_locked, uint, 0);
  3371. #define ALI5455_MODULE_NAME "ali5455"
  3372. static struct pci_driver ali_pci_driver = {
  3373. .name = ALI5455_MODULE_NAME,
  3374. .id_table = ali_pci_tbl,
  3375. .probe = ali_probe,
  3376. .remove = __devexit_p(ali_remove),
  3377. #ifdef CONFIG_PM
  3378. .suspend = ali_pm_suspend,
  3379. .resume = ali_pm_resume,
  3380. #endif /* CONFIG_PM */
  3381. };
  3382. static int __init ali_init_module(void)
  3383. {
  3384. printk(KERN_INFO "ALI 5455 + AC97 Audio, version "
  3385. DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
  3386. if (codec_independent_spdif_locked > 0) {
  3387. if (codec_independent_spdif_locked == 32000
  3388. || codec_independent_spdif_locked == 44100
  3389. || codec_independent_spdif_locked == 48000) {
  3390. printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", codec_independent_spdif_locked);
  3391. } else {
  3392. printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
  3393. codec_independent_spdif_locked = 0;
  3394. }
  3395. }
  3396. if (controller_independent_spdif_locked > 0) {
  3397. if (controller_independent_spdif_locked == 32000
  3398. || controller_independent_spdif_locked == 44100
  3399. || controller_independent_spdif_locked == 48000) {
  3400. printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", controller_independent_spdif_locked);
  3401. } else {
  3402. printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
  3403. controller_independent_spdif_locked = 0;
  3404. }
  3405. }
  3406. if (codec_pcmout_share_spdif_locked > 0) {
  3407. if (codec_pcmout_share_spdif_locked == 32000
  3408. || codec_pcmout_share_spdif_locked == 44100
  3409. || codec_pcmout_share_spdif_locked == 48000) {
  3410. printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", codec_pcmout_share_spdif_locked);
  3411. } else {
  3412. printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
  3413. codec_pcmout_share_spdif_locked = 0;
  3414. }
  3415. }
  3416. if (controller_pcmout_share_spdif_locked > 0) {
  3417. if (controller_pcmout_share_spdif_locked == 32000
  3418. || controller_pcmout_share_spdif_locked == 44100
  3419. || controller_pcmout_share_spdif_locked == 48000) {
  3420. printk(KERN_INFO "ali_audio: Enabling controller S/PDIF at sample rate %dHz.\n", controller_pcmout_share_spdif_locked);
  3421. } else {
  3422. printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
  3423. controller_pcmout_share_spdif_locked = 0;
  3424. }
  3425. }
  3426. return pci_register_driver(&ali_pci_driver);
  3427. }
  3428. static void __exit ali_cleanup_module(void)
  3429. {
  3430. pci_unregister_driver(&ali_pci_driver);
  3431. }
  3432. module_init(ali_init_module);
  3433. module_exit(ali_cleanup_module);
  3434. /*
  3435. Local Variables:
  3436. c-basic-offset: 8
  3437. End:
  3438. */