i810_audio.c 102 KB

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