es1968.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  1. /*
  2. * Driver for ESS Maestro 1/2/2E Sound Card (started 21.8.99)
  3. * Copyright (c) by Matze Braun <MatzeBraun@gmx.de>.
  4. * Takashi Iwai <tiwai@suse.de>
  5. *
  6. * Most of the driver code comes from Zach Brown(zab@redhat.com)
  7. * Alan Cox OSS Driver
  8. * Rewritted from card-es1938.c source.
  9. *
  10. * TODO:
  11. * Perhaps Synth
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. *
  28. * Notes from Zach Brown about the driver code
  29. *
  30. * Hardware Description
  31. *
  32. * A working Maestro setup contains the Maestro chip wired to a
  33. * codec or 2. In the Maestro we have the APUs, the ASSP, and the
  34. * Wavecache. The APUs can be though of as virtual audio routing
  35. * channels. They can take data from a number of sources and perform
  36. * basic encodings of the data. The wavecache is a storehouse for
  37. * PCM data. Typically it deals with PCI and interracts with the
  38. * APUs. The ASSP is a wacky DSP like device that ESS is loth
  39. * to release docs on. Thankfully it isn't required on the Maestro
  40. * until you start doing insane things like FM emulation and surround
  41. * encoding. The codecs are almost always AC-97 compliant codecs,
  42. * but it appears that early Maestros may have had PT101 (an ESS
  43. * part?) wired to them. The only real difference in the Maestro
  44. * families is external goop like docking capability, memory for
  45. * the ASSP, and initialization differences.
  46. *
  47. * Driver Operation
  48. *
  49. * We only drive the APU/Wavecache as typical DACs and drive the
  50. * mixers in the codecs. There are 64 APUs. We assign 6 to each
  51. * /dev/dsp? device. 2 channels for output, and 4 channels for
  52. * input.
  53. *
  54. * Each APU can do a number of things, but we only really use
  55. * 3 basic functions. For playback we use them to convert PCM
  56. * data fetched over PCI by the wavecahche into analog data that
  57. * is handed to the codec. One APU for mono, and a pair for stereo.
  58. * When in stereo, the combination of smarts in the APU and Wavecache
  59. * decide which wavecache gets the left or right channel.
  60. *
  61. * For record we still use the old overly mono system. For each in
  62. * coming channel the data comes in from the codec, through a 'input'
  63. * APU, through another rate converter APU, and then into memory via
  64. * the wavecache and PCI. If its stereo, we mash it back into LRLR in
  65. * software. The pass between the 2 APUs is supposedly what requires us
  66. * to have a 512 byte buffer sitting around in wavecache/memory.
  67. *
  68. * The wavecache makes our life even more fun. First off, it can
  69. * only address the first 28 bits of PCI address space, making it
  70. * useless on quite a few architectures. Secondly, its insane.
  71. * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
  72. * But that doesn't really work. You can only use 1 region. So all our
  73. * allocations have to be in 4meg of each other. Booo. Hiss.
  74. * So we have a module parameter, dsps_order, that is the order of
  75. * the number of dsps to provide. All their buffer space is allocated
  76. * on open time. The sonicvibes OSS routines we inherited really want
  77. * power of 2 buffers, so we have all those next to each other, then
  78. * 512 byte regions for the recording wavecaches. This ends up
  79. * wasting quite a bit of memory. The only fixes I can see would be
  80. * getting a kernel allocator that could work in zones, or figuring out
  81. * just how to coerce the WP into doing what we want.
  82. *
  83. * The indirection of the various registers means we have to spinlock
  84. * nearly all register accesses. We have the main register indirection
  85. * like the wave cache, maestro registers, etc. Then we have beasts
  86. * like the APU interface that is indirect registers gotten at through
  87. * the main maestro indirection. Ouch. We spinlock around the actual
  88. * ports on a per card basis. This means spinlock activity at each IO
  89. * operation, but the only IO operation clusters are in non critical
  90. * paths and it makes the code far easier to follow. Interrupts are
  91. * blocked while holding the locks because the int handler has to
  92. * get at some of them :(. The mixer interface doesn't, however.
  93. * We also have an OSS state lock that is thrown around in a few
  94. * places.
  95. */
  96. #include <asm/io.h>
  97. #include <linux/delay.h>
  98. #include <linux/interrupt.h>
  99. #include <linux/init.h>
  100. #include <linux/pci.h>
  101. #include <linux/dma-mapping.h>
  102. #include <linux/slab.h>
  103. #include <linux/gameport.h>
  104. #include <linux/moduleparam.h>
  105. #include <linux/mutex.h>
  106. #include <linux/input.h>
  107. #include <sound/core.h>
  108. #include <sound/pcm.h>
  109. #include <sound/mpu401.h>
  110. #include <sound/ac97_codec.h>
  111. #include <sound/initval.h>
  112. #ifdef CONFIG_SND_ES1968_RADIO
  113. #include <sound/tea575x-tuner.h>
  114. #endif
  115. #define CARD_NAME "ESS Maestro1/2"
  116. #define DRIVER_NAME "ES1968"
  117. MODULE_DESCRIPTION("ESS Maestro");
  118. MODULE_LICENSE("GPL");
  119. MODULE_SUPPORTED_DEVICE("{{ESS,Maestro 2e},"
  120. "{ESS,Maestro 2},"
  121. "{ESS,Maestro 1},"
  122. "{TerraTec,DMX}}");
  123. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  124. #define SUPPORT_JOYSTICK 1
  125. #endif
  126. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */
  127. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  128. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  129. static int total_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1024 };
  130. static int pcm_substreams_p[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4 };
  131. static int pcm_substreams_c[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1 };
  132. static int clock[SNDRV_CARDS];
  133. static int use_pm[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  134. static int enable_mpu[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  135. #ifdef SUPPORT_JOYSTICK
  136. static int joystick[SNDRV_CARDS];
  137. #endif
  138. module_param_array(index, int, NULL, 0444);
  139. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  140. module_param_array(id, charp, NULL, 0444);
  141. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  142. module_param_array(enable, bool, NULL, 0444);
  143. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  144. module_param_array(total_bufsize, int, NULL, 0444);
  145. MODULE_PARM_DESC(total_bufsize, "Total buffer size in kB.");
  146. module_param_array(pcm_substreams_p, int, NULL, 0444);
  147. MODULE_PARM_DESC(pcm_substreams_p, "PCM Playback substreams for " CARD_NAME " soundcard.");
  148. module_param_array(pcm_substreams_c, int, NULL, 0444);
  149. MODULE_PARM_DESC(pcm_substreams_c, "PCM Capture substreams for " CARD_NAME " soundcard.");
  150. module_param_array(clock, int, NULL, 0444);
  151. MODULE_PARM_DESC(clock, "Clock on " CARD_NAME " soundcard. (0 = auto-detect)");
  152. module_param_array(use_pm, int, NULL, 0444);
  153. MODULE_PARM_DESC(use_pm, "Toggle power-management. (0 = off, 1 = on, 2 = auto)");
  154. module_param_array(enable_mpu, int, NULL, 0444);
  155. MODULE_PARM_DESC(enable_mpu, "Enable MPU401. (0 = off, 1 = on, 2 = auto)");
  156. #ifdef SUPPORT_JOYSTICK
  157. module_param_array(joystick, bool, NULL, 0444);
  158. MODULE_PARM_DESC(joystick, "Enable joystick.");
  159. #endif
  160. #define NR_APUS 64
  161. #define NR_APU_REGS 16
  162. /* NEC Versas ? */
  163. #define NEC_VERSA_SUBID1 0x80581033
  164. #define NEC_VERSA_SUBID2 0x803c1033
  165. /* Mode Flags */
  166. #define ESS_FMT_STEREO 0x01
  167. #define ESS_FMT_16BIT 0x02
  168. #define DAC_RUNNING 1
  169. #define ADC_RUNNING 2
  170. /* Values for the ESM_LEGACY_AUDIO_CONTROL */
  171. #define ESS_DISABLE_AUDIO 0x8000
  172. #define ESS_ENABLE_SERIAL_IRQ 0x4000
  173. #define IO_ADRESS_ALIAS 0x0020
  174. #define MPU401_IRQ_ENABLE 0x0010
  175. #define MPU401_IO_ENABLE 0x0008
  176. #define GAME_IO_ENABLE 0x0004
  177. #define FM_IO_ENABLE 0x0002
  178. #define SB_IO_ENABLE 0x0001
  179. /* Values for the ESM_CONFIG_A */
  180. #define PIC_SNOOP1 0x4000
  181. #define PIC_SNOOP2 0x2000
  182. #define SAFEGUARD 0x0800
  183. #define DMA_CLEAR 0x0700
  184. #define DMA_DDMA 0x0000
  185. #define DMA_TDMA 0x0100
  186. #define DMA_PCPCI 0x0200
  187. #define POST_WRITE 0x0080
  188. #define PCI_TIMING 0x0040
  189. #define SWAP_LR 0x0020
  190. #define SUBTR_DECODE 0x0002
  191. /* Values for the ESM_CONFIG_B */
  192. #define SPDIF_CONFB 0x0100
  193. #define HWV_CONFB 0x0080
  194. #define DEBOUNCE 0x0040
  195. #define GPIO_CONFB 0x0020
  196. #define CHI_CONFB 0x0010
  197. #define IDMA_CONFB 0x0008 /*undoc */
  198. #define MIDI_FIX 0x0004 /*undoc */
  199. #define IRQ_TO_ISA 0x0001 /*undoc */
  200. /* Values for Ring Bus Control B */
  201. #define RINGB_2CODEC_ID_MASK 0x0003
  202. #define RINGB_DIS_VALIDATION 0x0008
  203. #define RINGB_EN_SPDIF 0x0010
  204. #define RINGB_EN_2CODEC 0x0020
  205. #define RINGB_SING_BIT_DUAL 0x0040
  206. /* ****Port Addresses**** */
  207. /* Write & Read */
  208. #define ESM_INDEX 0x02
  209. #define ESM_DATA 0x00
  210. /* AC97 + RingBus */
  211. #define ESM_AC97_INDEX 0x30
  212. #define ESM_AC97_DATA 0x32
  213. #define ESM_RING_BUS_DEST 0x34
  214. #define ESM_RING_BUS_CONTR_A 0x36
  215. #define ESM_RING_BUS_CONTR_B 0x38
  216. #define ESM_RING_BUS_SDO 0x3A
  217. /* WaveCache*/
  218. #define WC_INDEX 0x10
  219. #define WC_DATA 0x12
  220. #define WC_CONTROL 0x14
  221. /* ASSP*/
  222. #define ASSP_INDEX 0x80
  223. #define ASSP_MEMORY 0x82
  224. #define ASSP_DATA 0x84
  225. #define ASSP_CONTROL_A 0xA2
  226. #define ASSP_CONTROL_B 0xA4
  227. #define ASSP_CONTROL_C 0xA6
  228. #define ASSP_HOSTW_INDEX 0xA8
  229. #define ASSP_HOSTW_DATA 0xAA
  230. #define ASSP_HOSTW_IRQ 0xAC
  231. /* Midi */
  232. #define ESM_MPU401_PORT 0x98
  233. /* Others */
  234. #define ESM_PORT_HOST_IRQ 0x18
  235. #define IDR0_DATA_PORT 0x00
  236. #define IDR1_CRAM_POINTER 0x01
  237. #define IDR2_CRAM_DATA 0x02
  238. #define IDR3_WAVE_DATA 0x03
  239. #define IDR4_WAVE_PTR_LOW 0x04
  240. #define IDR5_WAVE_PTR_HI 0x05
  241. #define IDR6_TIMER_CTRL 0x06
  242. #define IDR7_WAVE_ROMRAM 0x07
  243. #define WRITEABLE_MAP 0xEFFFFF
  244. #define READABLE_MAP 0x64003F
  245. /* PCI Register */
  246. #define ESM_LEGACY_AUDIO_CONTROL 0x40
  247. #define ESM_ACPI_COMMAND 0x54
  248. #define ESM_CONFIG_A 0x50
  249. #define ESM_CONFIG_B 0x52
  250. #define ESM_DDMA 0x60
  251. /* Bob Bits */
  252. #define ESM_BOB_ENABLE 0x0001
  253. #define ESM_BOB_START 0x0001
  254. /* Host IRQ Control Bits */
  255. #define ESM_RESET_MAESTRO 0x8000
  256. #define ESM_RESET_DIRECTSOUND 0x4000
  257. #define ESM_HIRQ_ClkRun 0x0100
  258. #define ESM_HIRQ_HW_VOLUME 0x0040
  259. #define ESM_HIRQ_HARPO 0x0030 /* What's that? */
  260. #define ESM_HIRQ_ASSP 0x0010
  261. #define ESM_HIRQ_DSIE 0x0004
  262. #define ESM_HIRQ_MPU401 0x0002
  263. #define ESM_HIRQ_SB 0x0001
  264. /* Host IRQ Status Bits */
  265. #define ESM_MPU401_IRQ 0x02
  266. #define ESM_SB_IRQ 0x01
  267. #define ESM_SOUND_IRQ 0x04
  268. #define ESM_ASSP_IRQ 0x10
  269. #define ESM_HWVOL_IRQ 0x40
  270. #define ESS_SYSCLK 50000000
  271. #define ESM_BOB_FREQ 200
  272. #define ESM_BOB_FREQ_MAX 800
  273. #define ESM_FREQ_ESM1 (49152000L / 1024L) /* default rate 48000 */
  274. #define ESM_FREQ_ESM2 (50000000L / 1024L)
  275. /* APU Modes: reg 0x00, bit 4-7 */
  276. #define ESM_APU_MODE_SHIFT 4
  277. #define ESM_APU_MODE_MASK (0xf << 4)
  278. #define ESM_APU_OFF 0x00
  279. #define ESM_APU_16BITLINEAR 0x01 /* 16-Bit Linear Sample Player */
  280. #define ESM_APU_16BITSTEREO 0x02 /* 16-Bit Stereo Sample Player */
  281. #define ESM_APU_8BITLINEAR 0x03 /* 8-Bit Linear Sample Player */
  282. #define ESM_APU_8BITSTEREO 0x04 /* 8-Bit Stereo Sample Player */
  283. #define ESM_APU_8BITDIFF 0x05 /* 8-Bit Differential Sample Playrer */
  284. #define ESM_APU_DIGITALDELAY 0x06 /* Digital Delay Line */
  285. #define ESM_APU_DUALTAP 0x07 /* Dual Tap Reader */
  286. #define ESM_APU_CORRELATOR 0x08 /* Correlator */
  287. #define ESM_APU_INPUTMIXER 0x09 /* Input Mixer */
  288. #define ESM_APU_WAVETABLE 0x0A /* Wave Table Mode */
  289. #define ESM_APU_SRCONVERTOR 0x0B /* Sample Rate Convertor */
  290. #define ESM_APU_16BITPINGPONG 0x0C /* 16-Bit Ping-Pong Sample Player */
  291. #define ESM_APU_RESERVED1 0x0D /* Reserved 1 */
  292. #define ESM_APU_RESERVED2 0x0E /* Reserved 2 */
  293. #define ESM_APU_RESERVED3 0x0F /* Reserved 3 */
  294. /* reg 0x00 */
  295. #define ESM_APU_FILTER_Q_SHIFT 0
  296. #define ESM_APU_FILTER_Q_MASK (3 << 0)
  297. /* APU Filtey Q Control */
  298. #define ESM_APU_FILTER_LESSQ 0x00
  299. #define ESM_APU_FILTER_MOREQ 0x03
  300. #define ESM_APU_FILTER_TYPE_SHIFT 2
  301. #define ESM_APU_FILTER_TYPE_MASK (3 << 2)
  302. #define ESM_APU_ENV_TYPE_SHIFT 8
  303. #define ESM_APU_ENV_TYPE_MASK (3 << 8)
  304. #define ESM_APU_ENV_STATE_SHIFT 10
  305. #define ESM_APU_ENV_STATE_MASK (3 << 10)
  306. #define ESM_APU_END_CURVE (1 << 12)
  307. #define ESM_APU_INT_ON_LOOP (1 << 13)
  308. #define ESM_APU_DMA_ENABLE (1 << 14)
  309. /* reg 0x02 */
  310. #define ESM_APU_SUBMIX_GROUP_SHIRT 0
  311. #define ESM_APU_SUBMIX_GROUP_MASK (7 << 0)
  312. #define ESM_APU_SUBMIX_MODE (1 << 3)
  313. #define ESM_APU_6dB (1 << 4)
  314. #define ESM_APU_DUAL_EFFECT (1 << 5)
  315. #define ESM_APU_EFFECT_CHANNELS_SHIFT 6
  316. #define ESM_APU_EFFECT_CHANNELS_MASK (3 << 6)
  317. /* reg 0x03 */
  318. #define ESM_APU_STEP_SIZE_MASK 0x0fff
  319. /* reg 0x04 */
  320. #define ESM_APU_PHASE_SHIFT 0
  321. #define ESM_APU_PHASE_MASK (0xff << 0)
  322. #define ESM_APU_WAVE64K_PAGE_SHIFT 8 /* most 8bit of wave start offset */
  323. #define ESM_APU_WAVE64K_PAGE_MASK (0xff << 8)
  324. /* reg 0x05 - wave start offset */
  325. /* reg 0x06 - wave end offset */
  326. /* reg 0x07 - wave loop length */
  327. /* reg 0x08 */
  328. #define ESM_APU_EFFECT_GAIN_SHIFT 0
  329. #define ESM_APU_EFFECT_GAIN_MASK (0xff << 0)
  330. #define ESM_APU_TREMOLO_DEPTH_SHIFT 8
  331. #define ESM_APU_TREMOLO_DEPTH_MASK (0xf << 8)
  332. #define ESM_APU_TREMOLO_RATE_SHIFT 12
  333. #define ESM_APU_TREMOLO_RATE_MASK (0xf << 12)
  334. /* reg 0x09 */
  335. /* bit 0-7 amplitude dest? */
  336. #define ESM_APU_AMPLITUDE_NOW_SHIFT 8
  337. #define ESM_APU_AMPLITUDE_NOW_MASK (0xff << 8)
  338. /* reg 0x0a */
  339. #define ESM_APU_POLAR_PAN_SHIFT 0
  340. #define ESM_APU_POLAR_PAN_MASK (0x3f << 0)
  341. /* Polar Pan Control */
  342. #define ESM_APU_PAN_CENTER_CIRCLE 0x00
  343. #define ESM_APU_PAN_MIDDLE_RADIUS 0x01
  344. #define ESM_APU_PAN_OUTSIDE_RADIUS 0x02
  345. #define ESM_APU_FILTER_TUNING_SHIFT 8
  346. #define ESM_APU_FILTER_TUNING_MASK (0xff << 8)
  347. /* reg 0x0b */
  348. #define ESM_APU_DATA_SRC_A_SHIFT 0
  349. #define ESM_APU_DATA_SRC_A_MASK (0x7f << 0)
  350. #define ESM_APU_INV_POL_A (1 << 7)
  351. #define ESM_APU_DATA_SRC_B_SHIFT 8
  352. #define ESM_APU_DATA_SRC_B_MASK (0x7f << 8)
  353. #define ESM_APU_INV_POL_B (1 << 15)
  354. #define ESM_APU_VIBRATO_RATE_SHIFT 0
  355. #define ESM_APU_VIBRATO_RATE_MASK (0xf << 0)
  356. #define ESM_APU_VIBRATO_DEPTH_SHIFT 4
  357. #define ESM_APU_VIBRATO_DEPTH_MASK (0xf << 4)
  358. #define ESM_APU_VIBRATO_PHASE_SHIFT 8
  359. #define ESM_APU_VIBRATO_PHASE_MASK (0xff << 8)
  360. /* reg 0x0c */
  361. #define ESM_APU_RADIUS_SELECT (1 << 6)
  362. /* APU Filter Control */
  363. #define ESM_APU_FILTER_2POLE_LOPASS 0x00
  364. #define ESM_APU_FILTER_2POLE_BANDPASS 0x01
  365. #define ESM_APU_FILTER_2POLE_HIPASS 0x02
  366. #define ESM_APU_FILTER_1POLE_LOPASS 0x03
  367. #define ESM_APU_FILTER_1POLE_HIPASS 0x04
  368. #define ESM_APU_FILTER_OFF 0x05
  369. /* APU ATFP Type */
  370. #define ESM_APU_ATFP_AMPLITUDE 0x00
  371. #define ESM_APU_ATFP_TREMELO 0x01
  372. #define ESM_APU_ATFP_FILTER 0x02
  373. #define ESM_APU_ATFP_PAN 0x03
  374. /* APU ATFP Flags */
  375. #define ESM_APU_ATFP_FLG_OFF 0x00
  376. #define ESM_APU_ATFP_FLG_WAIT 0x01
  377. #define ESM_APU_ATFP_FLG_DONE 0x02
  378. #define ESM_APU_ATFP_FLG_INPROCESS 0x03
  379. /* capture mixing buffer size */
  380. #define ESM_MEM_ALIGN 0x1000
  381. #define ESM_MIXBUF_SIZE 0x400
  382. #define ESM_MODE_PLAY 0
  383. #define ESM_MODE_CAPTURE 1
  384. /* APU use in the driver */
  385. enum snd_enum_apu_type {
  386. ESM_APU_PCM_PLAY,
  387. ESM_APU_PCM_CAPTURE,
  388. ESM_APU_PCM_RATECONV,
  389. ESM_APU_FREE
  390. };
  391. /* chip type */
  392. enum {
  393. TYPE_MAESTRO, TYPE_MAESTRO2, TYPE_MAESTRO2E
  394. };
  395. /* DMA Hack! */
  396. struct esm_memory {
  397. struct snd_dma_buffer buf;
  398. int empty; /* status */
  399. struct list_head list;
  400. };
  401. /* Playback Channel */
  402. struct esschan {
  403. int running;
  404. u8 apu[4];
  405. u8 apu_mode[4];
  406. /* playback/capture pcm buffer */
  407. struct esm_memory *memory;
  408. /* capture mixer buffer */
  409. struct esm_memory *mixbuf;
  410. unsigned int hwptr; /* current hw pointer in bytes */
  411. unsigned int count; /* sample counter in bytes */
  412. unsigned int dma_size; /* total buffer size in bytes */
  413. unsigned int frag_size; /* period size in bytes */
  414. unsigned int wav_shift;
  415. u16 base[4]; /* offset for ptr */
  416. /* stereo/16bit flag */
  417. unsigned char fmt;
  418. int mode; /* playback / capture */
  419. int bob_freq; /* required timer frequency */
  420. struct snd_pcm_substream *substream;
  421. /* linked list */
  422. struct list_head list;
  423. #ifdef CONFIG_PM
  424. u16 wc_map[4];
  425. #endif
  426. };
  427. struct es1968 {
  428. /* Module Config */
  429. int total_bufsize; /* in bytes */
  430. int playback_streams, capture_streams;
  431. unsigned int clock; /* clock */
  432. /* for clock measurement */
  433. unsigned int in_measurement: 1;
  434. unsigned int measure_apu;
  435. unsigned int measure_lastpos;
  436. unsigned int measure_count;
  437. /* buffer */
  438. struct snd_dma_buffer dma;
  439. /* Resources... */
  440. int irq;
  441. unsigned long io_port;
  442. int type;
  443. struct pci_dev *pci;
  444. struct snd_card *card;
  445. struct snd_pcm *pcm;
  446. int do_pm; /* power-management enabled */
  447. /* DMA memory block */
  448. struct list_head buf_list;
  449. /* ALSA Stuff */
  450. struct snd_ac97 *ac97;
  451. struct snd_rawmidi *rmidi;
  452. spinlock_t reg_lock;
  453. unsigned int in_suspend;
  454. /* Maestro Stuff */
  455. u16 maestro_map[32];
  456. int bobclient; /* active timer instancs */
  457. int bob_freq; /* timer frequency */
  458. struct mutex memory_mutex; /* memory lock */
  459. /* APU states */
  460. unsigned char apu[NR_APUS];
  461. /* active substreams */
  462. struct list_head substream_list;
  463. spinlock_t substream_lock;
  464. #ifdef CONFIG_PM
  465. u16 apu_map[NR_APUS][NR_APU_REGS];
  466. #endif
  467. #ifdef SUPPORT_JOYSTICK
  468. struct gameport *gameport;
  469. #endif
  470. #ifdef CONFIG_SND_ES1968_INPUT
  471. struct input_dev *input_dev;
  472. char phys[64]; /* physical device path */
  473. #else
  474. struct snd_kcontrol *master_switch; /* for h/w volume control */
  475. struct snd_kcontrol *master_volume;
  476. spinlock_t ac97_lock;
  477. struct tasklet_struct hwvol_tq;
  478. #endif
  479. #ifdef CONFIG_SND_ES1968_RADIO
  480. struct snd_tea575x tea;
  481. #endif
  482. };
  483. static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id);
  484. static DEFINE_PCI_DEVICE_TABLE(snd_es1968_ids) = {
  485. /* Maestro 1 */
  486. { 0x1285, 0x0100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO },
  487. /* Maestro 2 */
  488. { 0x125d, 0x1968, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2 },
  489. /* Maestro 2E */
  490. { 0x125d, 0x1978, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2E },
  491. { 0, }
  492. };
  493. MODULE_DEVICE_TABLE(pci, snd_es1968_ids);
  494. /* *********************
  495. * Low Level Funcs! *
  496. *********************/
  497. /* no spinlock */
  498. static void __maestro_write(struct es1968 *chip, u16 reg, u16 data)
  499. {
  500. outw(reg, chip->io_port + ESM_INDEX);
  501. outw(data, chip->io_port + ESM_DATA);
  502. chip->maestro_map[reg] = data;
  503. }
  504. static inline void maestro_write(struct es1968 *chip, u16 reg, u16 data)
  505. {
  506. unsigned long flags;
  507. spin_lock_irqsave(&chip->reg_lock, flags);
  508. __maestro_write(chip, reg, data);
  509. spin_unlock_irqrestore(&chip->reg_lock, flags);
  510. }
  511. /* no spinlock */
  512. static u16 __maestro_read(struct es1968 *chip, u16 reg)
  513. {
  514. if (READABLE_MAP & (1 << reg)) {
  515. outw(reg, chip->io_port + ESM_INDEX);
  516. chip->maestro_map[reg] = inw(chip->io_port + ESM_DATA);
  517. }
  518. return chip->maestro_map[reg];
  519. }
  520. static inline u16 maestro_read(struct es1968 *chip, u16 reg)
  521. {
  522. unsigned long flags;
  523. u16 result;
  524. spin_lock_irqsave(&chip->reg_lock, flags);
  525. result = __maestro_read(chip, reg);
  526. spin_unlock_irqrestore(&chip->reg_lock, flags);
  527. return result;
  528. }
  529. /* Wait for the codec bus to be free */
  530. static int snd_es1968_ac97_wait(struct es1968 *chip)
  531. {
  532. int timeout = 100000;
  533. while (timeout-- > 0) {
  534. if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
  535. return 0;
  536. cond_resched();
  537. }
  538. snd_printd("es1968: ac97 timeout\n");
  539. return 1; /* timeout */
  540. }
  541. static int snd_es1968_ac97_wait_poll(struct es1968 *chip)
  542. {
  543. int timeout = 100000;
  544. while (timeout-- > 0) {
  545. if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
  546. return 0;
  547. }
  548. snd_printd("es1968: ac97 timeout\n");
  549. return 1; /* timeout */
  550. }
  551. static void snd_es1968_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
  552. {
  553. struct es1968 *chip = ac97->private_data;
  554. #ifndef CONFIG_SND_ES1968_INPUT
  555. unsigned long flags;
  556. #endif
  557. snd_es1968_ac97_wait(chip);
  558. /* Write the bus */
  559. #ifndef CONFIG_SND_ES1968_INPUT
  560. spin_lock_irqsave(&chip->ac97_lock, flags);
  561. #endif
  562. outw(val, chip->io_port + ESM_AC97_DATA);
  563. /*msleep(1);*/
  564. outb(reg, chip->io_port + ESM_AC97_INDEX);
  565. /*msleep(1);*/
  566. #ifndef CONFIG_SND_ES1968_INPUT
  567. spin_unlock_irqrestore(&chip->ac97_lock, flags);
  568. #endif
  569. }
  570. static unsigned short snd_es1968_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  571. {
  572. u16 data = 0;
  573. struct es1968 *chip = ac97->private_data;
  574. #ifndef CONFIG_SND_ES1968_INPUT
  575. unsigned long flags;
  576. #endif
  577. snd_es1968_ac97_wait(chip);
  578. #ifndef CONFIG_SND_ES1968_INPUT
  579. spin_lock_irqsave(&chip->ac97_lock, flags);
  580. #endif
  581. outb(reg | 0x80, chip->io_port + ESM_AC97_INDEX);
  582. /*msleep(1);*/
  583. if (!snd_es1968_ac97_wait_poll(chip)) {
  584. data = inw(chip->io_port + ESM_AC97_DATA);
  585. /*msleep(1);*/
  586. }
  587. #ifndef CONFIG_SND_ES1968_INPUT
  588. spin_unlock_irqrestore(&chip->ac97_lock, flags);
  589. #endif
  590. return data;
  591. }
  592. /* no spinlock */
  593. static void apu_index_set(struct es1968 *chip, u16 index)
  594. {
  595. int i;
  596. __maestro_write(chip, IDR1_CRAM_POINTER, index);
  597. for (i = 0; i < 1000; i++)
  598. if (__maestro_read(chip, IDR1_CRAM_POINTER) == index)
  599. return;
  600. snd_printd("es1968: APU register select failed. (Timeout)\n");
  601. }
  602. /* no spinlock */
  603. static void apu_data_set(struct es1968 *chip, u16 data)
  604. {
  605. int i;
  606. for (i = 0; i < 1000; i++) {
  607. if (__maestro_read(chip, IDR0_DATA_PORT) == data)
  608. return;
  609. __maestro_write(chip, IDR0_DATA_PORT, data);
  610. }
  611. snd_printd("es1968: APU register set probably failed (Timeout)!\n");
  612. }
  613. /* no spinlock */
  614. static void __apu_set_register(struct es1968 *chip, u16 channel, u8 reg, u16 data)
  615. {
  616. if (snd_BUG_ON(channel >= NR_APUS))
  617. return;
  618. #ifdef CONFIG_PM
  619. chip->apu_map[channel][reg] = data;
  620. #endif
  621. reg |= (channel << 4);
  622. apu_index_set(chip, reg);
  623. apu_data_set(chip, data);
  624. }
  625. static void apu_set_register(struct es1968 *chip, u16 channel, u8 reg, u16 data)
  626. {
  627. unsigned long flags;
  628. spin_lock_irqsave(&chip->reg_lock, flags);
  629. __apu_set_register(chip, channel, reg, data);
  630. spin_unlock_irqrestore(&chip->reg_lock, flags);
  631. }
  632. static u16 __apu_get_register(struct es1968 *chip, u16 channel, u8 reg)
  633. {
  634. if (snd_BUG_ON(channel >= NR_APUS))
  635. return 0;
  636. reg |= (channel << 4);
  637. apu_index_set(chip, reg);
  638. return __maestro_read(chip, IDR0_DATA_PORT);
  639. }
  640. static u16 apu_get_register(struct es1968 *chip, u16 channel, u8 reg)
  641. {
  642. unsigned long flags;
  643. u16 v;
  644. spin_lock_irqsave(&chip->reg_lock, flags);
  645. v = __apu_get_register(chip, channel, reg);
  646. spin_unlock_irqrestore(&chip->reg_lock, flags);
  647. return v;
  648. }
  649. #if 0 /* ASSP is not supported */
  650. static void assp_set_register(struct es1968 *chip, u32 reg, u32 value)
  651. {
  652. unsigned long flags;
  653. spin_lock_irqsave(&chip->reg_lock, flags);
  654. outl(reg, chip->io_port + ASSP_INDEX);
  655. outl(value, chip->io_port + ASSP_DATA);
  656. spin_unlock_irqrestore(&chip->reg_lock, flags);
  657. }
  658. static u32 assp_get_register(struct es1968 *chip, u32 reg)
  659. {
  660. unsigned long flags;
  661. u32 value;
  662. spin_lock_irqsave(&chip->reg_lock, flags);
  663. outl(reg, chip->io_port + ASSP_INDEX);
  664. value = inl(chip->io_port + ASSP_DATA);
  665. spin_unlock_irqrestore(&chip->reg_lock, flags);
  666. return value;
  667. }
  668. #endif
  669. static void wave_set_register(struct es1968 *chip, u16 reg, u16 value)
  670. {
  671. unsigned long flags;
  672. spin_lock_irqsave(&chip->reg_lock, flags);
  673. outw(reg, chip->io_port + WC_INDEX);
  674. outw(value, chip->io_port + WC_DATA);
  675. spin_unlock_irqrestore(&chip->reg_lock, flags);
  676. }
  677. static u16 wave_get_register(struct es1968 *chip, u16 reg)
  678. {
  679. unsigned long flags;
  680. u16 value;
  681. spin_lock_irqsave(&chip->reg_lock, flags);
  682. outw(reg, chip->io_port + WC_INDEX);
  683. value = inw(chip->io_port + WC_DATA);
  684. spin_unlock_irqrestore(&chip->reg_lock, flags);
  685. return value;
  686. }
  687. /* *******************
  688. * Bob the Timer! *
  689. *******************/
  690. static void snd_es1968_bob_stop(struct es1968 *chip)
  691. {
  692. u16 reg;
  693. reg = __maestro_read(chip, 0x11);
  694. reg &= ~ESM_BOB_ENABLE;
  695. __maestro_write(chip, 0x11, reg);
  696. reg = __maestro_read(chip, 0x17);
  697. reg &= ~ESM_BOB_START;
  698. __maestro_write(chip, 0x17, reg);
  699. }
  700. static void snd_es1968_bob_start(struct es1968 *chip)
  701. {
  702. int prescale;
  703. int divide;
  704. /* compute ideal interrupt frequency for buffer size & play rate */
  705. /* first, find best prescaler value to match freq */
  706. for (prescale = 5; prescale < 12; prescale++)
  707. if (chip->bob_freq > (ESS_SYSCLK >> (prescale + 9)))
  708. break;
  709. /* next, back off prescaler whilst getting divider into optimum range */
  710. divide = 1;
  711. while ((prescale > 5) && (divide < 32)) {
  712. prescale--;
  713. divide <<= 1;
  714. }
  715. divide >>= 1;
  716. /* now fine-tune the divider for best match */
  717. for (; divide < 31; divide++)
  718. if (chip->bob_freq >
  719. ((ESS_SYSCLK >> (prescale + 9)) / (divide + 1))) break;
  720. /* divide = 0 is illegal, but don't let prescale = 4! */
  721. if (divide == 0) {
  722. divide++;
  723. if (prescale > 5)
  724. prescale--;
  725. } else if (divide > 1)
  726. divide--;
  727. __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide); /* set reg */
  728. /* Now set IDR 11/17 */
  729. __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
  730. __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
  731. }
  732. /* call with substream spinlock */
  733. static void snd_es1968_bob_inc(struct es1968 *chip, int freq)
  734. {
  735. chip->bobclient++;
  736. if (chip->bobclient == 1) {
  737. chip->bob_freq = freq;
  738. snd_es1968_bob_start(chip);
  739. } else if (chip->bob_freq < freq) {
  740. snd_es1968_bob_stop(chip);
  741. chip->bob_freq = freq;
  742. snd_es1968_bob_start(chip);
  743. }
  744. }
  745. /* call with substream spinlock */
  746. static void snd_es1968_bob_dec(struct es1968 *chip)
  747. {
  748. chip->bobclient--;
  749. if (chip->bobclient <= 0)
  750. snd_es1968_bob_stop(chip);
  751. else if (chip->bob_freq > ESM_BOB_FREQ) {
  752. /* check reduction of timer frequency */
  753. int max_freq = ESM_BOB_FREQ;
  754. struct esschan *es;
  755. list_for_each_entry(es, &chip->substream_list, list) {
  756. if (max_freq < es->bob_freq)
  757. max_freq = es->bob_freq;
  758. }
  759. if (max_freq != chip->bob_freq) {
  760. snd_es1968_bob_stop(chip);
  761. chip->bob_freq = max_freq;
  762. snd_es1968_bob_start(chip);
  763. }
  764. }
  765. }
  766. static int
  767. snd_es1968_calc_bob_rate(struct es1968 *chip, struct esschan *es,
  768. struct snd_pcm_runtime *runtime)
  769. {
  770. /* we acquire 4 interrupts per period for precise control.. */
  771. int freq = runtime->rate * 4;
  772. if (es->fmt & ESS_FMT_STEREO)
  773. freq <<= 1;
  774. if (es->fmt & ESS_FMT_16BIT)
  775. freq <<= 1;
  776. freq /= es->frag_size;
  777. if (freq < ESM_BOB_FREQ)
  778. freq = ESM_BOB_FREQ;
  779. else if (freq > ESM_BOB_FREQ_MAX)
  780. freq = ESM_BOB_FREQ_MAX;
  781. return freq;
  782. }
  783. /*************
  784. * PCM Part *
  785. *************/
  786. static u32 snd_es1968_compute_rate(struct es1968 *chip, u32 freq)
  787. {
  788. u32 rate = (freq << 16) / chip->clock;
  789. #if 0 /* XXX: do we need this? */
  790. if (rate > 0x10000)
  791. rate = 0x10000;
  792. #endif
  793. return rate;
  794. }
  795. /* get current pointer */
  796. static inline unsigned int
  797. snd_es1968_get_dma_ptr(struct es1968 *chip, struct esschan *es)
  798. {
  799. unsigned int offset;
  800. offset = apu_get_register(chip, es->apu[0], 5);
  801. offset -= es->base[0];
  802. return (offset & 0xFFFE); /* hardware is in words */
  803. }
  804. static void snd_es1968_apu_set_freq(struct es1968 *chip, int apu, int freq)
  805. {
  806. apu_set_register(chip, apu, 2,
  807. (apu_get_register(chip, apu, 2) & 0x00FF) |
  808. ((freq & 0xff) << 8) | 0x10);
  809. apu_set_register(chip, apu, 3, freq >> 8);
  810. }
  811. /* spin lock held */
  812. static inline void snd_es1968_trigger_apu(struct es1968 *esm, int apu, int mode)
  813. {
  814. /* set the APU mode */
  815. __apu_set_register(esm, apu, 0,
  816. (__apu_get_register(esm, apu, 0) & 0xff0f) |
  817. (mode << 4));
  818. }
  819. static void snd_es1968_pcm_start(struct es1968 *chip, struct esschan *es)
  820. {
  821. spin_lock(&chip->reg_lock);
  822. __apu_set_register(chip, es->apu[0], 5, es->base[0]);
  823. snd_es1968_trigger_apu(chip, es->apu[0], es->apu_mode[0]);
  824. if (es->mode == ESM_MODE_CAPTURE) {
  825. __apu_set_register(chip, es->apu[2], 5, es->base[2]);
  826. snd_es1968_trigger_apu(chip, es->apu[2], es->apu_mode[2]);
  827. }
  828. if (es->fmt & ESS_FMT_STEREO) {
  829. __apu_set_register(chip, es->apu[1], 5, es->base[1]);
  830. snd_es1968_trigger_apu(chip, es->apu[1], es->apu_mode[1]);
  831. if (es->mode == ESM_MODE_CAPTURE) {
  832. __apu_set_register(chip, es->apu[3], 5, es->base[3]);
  833. snd_es1968_trigger_apu(chip, es->apu[3], es->apu_mode[3]);
  834. }
  835. }
  836. spin_unlock(&chip->reg_lock);
  837. }
  838. static void snd_es1968_pcm_stop(struct es1968 *chip, struct esschan *es)
  839. {
  840. spin_lock(&chip->reg_lock);
  841. snd_es1968_trigger_apu(chip, es->apu[0], 0);
  842. snd_es1968_trigger_apu(chip, es->apu[1], 0);
  843. if (es->mode == ESM_MODE_CAPTURE) {
  844. snd_es1968_trigger_apu(chip, es->apu[2], 0);
  845. snd_es1968_trigger_apu(chip, es->apu[3], 0);
  846. }
  847. spin_unlock(&chip->reg_lock);
  848. }
  849. /* set the wavecache control reg */
  850. static void snd_es1968_program_wavecache(struct es1968 *chip, struct esschan *es,
  851. int channel, u32 addr, int capture)
  852. {
  853. u32 tmpval = (addr - 0x10) & 0xFFF8;
  854. if (! capture) {
  855. if (!(es->fmt & ESS_FMT_16BIT))
  856. tmpval |= 4; /* 8bit */
  857. if (es->fmt & ESS_FMT_STEREO)
  858. tmpval |= 2; /* stereo */
  859. }
  860. /* set the wavecache control reg */
  861. wave_set_register(chip, es->apu[channel] << 3, tmpval);
  862. #ifdef CONFIG_PM
  863. es->wc_map[channel] = tmpval;
  864. #endif
  865. }
  866. static void snd_es1968_playback_setup(struct es1968 *chip, struct esschan *es,
  867. struct snd_pcm_runtime *runtime)
  868. {
  869. u32 pa;
  870. int high_apu = 0;
  871. int channel, apu;
  872. int i, size;
  873. unsigned long flags;
  874. u32 freq;
  875. size = es->dma_size >> es->wav_shift;
  876. if (es->fmt & ESS_FMT_STEREO)
  877. high_apu++;
  878. for (channel = 0; channel <= high_apu; channel++) {
  879. apu = es->apu[channel];
  880. snd_es1968_program_wavecache(chip, es, channel, es->memory->buf.addr, 0);
  881. /* Offset to PCMBAR */
  882. pa = es->memory->buf.addr;
  883. pa -= chip->dma.addr;
  884. pa >>= 1; /* words */
  885. pa |= 0x00400000; /* System RAM (Bit 22) */
  886. if (es->fmt & ESS_FMT_STEREO) {
  887. /* Enable stereo */
  888. if (channel)
  889. pa |= 0x00800000; /* (Bit 23) */
  890. if (es->fmt & ESS_FMT_16BIT)
  891. pa >>= 1;
  892. }
  893. /* base offset of dma calcs when reading the pointer
  894. on this left one */
  895. es->base[channel] = pa & 0xFFFF;
  896. for (i = 0; i < 16; i++)
  897. apu_set_register(chip, apu, i, 0x0000);
  898. /* Load the buffer into the wave engine */
  899. apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
  900. apu_set_register(chip, apu, 5, pa & 0xFFFF);
  901. apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
  902. /* setting loop == sample len */
  903. apu_set_register(chip, apu, 7, size);
  904. /* clear effects/env.. */
  905. apu_set_register(chip, apu, 8, 0x0000);
  906. /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
  907. apu_set_register(chip, apu, 9, 0xD000);
  908. /* clear routing stuff */
  909. apu_set_register(chip, apu, 11, 0x0000);
  910. /* dma on, no envelopes, filter to all 1s) */
  911. apu_set_register(chip, apu, 0, 0x400F);
  912. if (es->fmt & ESS_FMT_16BIT)
  913. es->apu_mode[channel] = ESM_APU_16BITLINEAR;
  914. else
  915. es->apu_mode[channel] = ESM_APU_8BITLINEAR;
  916. if (es->fmt & ESS_FMT_STEREO) {
  917. /* set panning: left or right */
  918. /* Check: different panning. On my Canyon 3D Chipset the
  919. Channels are swapped. I don't know, about the output
  920. to the SPDif Link. Perhaps you have to change this
  921. and not the APU Regs 4-5. */
  922. apu_set_register(chip, apu, 10,
  923. 0x8F00 | (channel ? 0 : 0x10));
  924. es->apu_mode[channel] += 1; /* stereo */
  925. } else
  926. apu_set_register(chip, apu, 10, 0x8F08);
  927. }
  928. spin_lock_irqsave(&chip->reg_lock, flags);
  929. /* clear WP interrupts */
  930. outw(1, chip->io_port + 0x04);
  931. /* enable WP ints */
  932. outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
  933. spin_unlock_irqrestore(&chip->reg_lock, flags);
  934. freq = runtime->rate;
  935. /* set frequency */
  936. if (freq > 48000)
  937. freq = 48000;
  938. if (freq < 4000)
  939. freq = 4000;
  940. /* hmmm.. */
  941. if (!(es->fmt & ESS_FMT_16BIT) && !(es->fmt & ESS_FMT_STEREO))
  942. freq >>= 1;
  943. freq = snd_es1968_compute_rate(chip, freq);
  944. /* Load the frequency, turn on 6dB */
  945. snd_es1968_apu_set_freq(chip, es->apu[0], freq);
  946. snd_es1968_apu_set_freq(chip, es->apu[1], freq);
  947. }
  948. static void init_capture_apu(struct es1968 *chip, struct esschan *es, int channel,
  949. unsigned int pa, unsigned int bsize,
  950. int mode, int route)
  951. {
  952. int i, apu = es->apu[channel];
  953. es->apu_mode[channel] = mode;
  954. /* set the wavecache control reg */
  955. snd_es1968_program_wavecache(chip, es, channel, pa, 1);
  956. /* Offset to PCMBAR */
  957. pa -= chip->dma.addr;
  958. pa >>= 1; /* words */
  959. /* base offset of dma calcs when reading the pointer
  960. on this left one */
  961. es->base[channel] = pa & 0xFFFF;
  962. pa |= 0x00400000; /* bit 22 -> System RAM */
  963. /* Begin loading the APU */
  964. for (i = 0; i < 16; i++)
  965. apu_set_register(chip, apu, i, 0x0000);
  966. /* need to enable subgroups.. and we should probably
  967. have different groups for different /dev/dsps.. */
  968. apu_set_register(chip, apu, 2, 0x8);
  969. /* Load the buffer into the wave engine */
  970. apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
  971. apu_set_register(chip, apu, 5, pa & 0xFFFF);
  972. apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
  973. apu_set_register(chip, apu, 7, bsize);
  974. /* clear effects/env.. */
  975. apu_set_register(chip, apu, 8, 0x00F0);
  976. /* amplitude now? sure. why not. */
  977. apu_set_register(chip, apu, 9, 0x0000);
  978. /* set filter tune, radius, polar pan */
  979. apu_set_register(chip, apu, 10, 0x8F08);
  980. /* route input */
  981. apu_set_register(chip, apu, 11, route);
  982. /* dma on, no envelopes, filter to all 1s) */
  983. apu_set_register(chip, apu, 0, 0x400F);
  984. }
  985. static void snd_es1968_capture_setup(struct es1968 *chip, struct esschan *es,
  986. struct snd_pcm_runtime *runtime)
  987. {
  988. int size;
  989. u32 freq;
  990. unsigned long flags;
  991. size = es->dma_size >> es->wav_shift;
  992. /* APU assignments:
  993. 0 = mono/left SRC
  994. 1 = right SRC
  995. 2 = mono/left Input Mixer
  996. 3 = right Input Mixer
  997. */
  998. /* data seems to flow from the codec, through an apu into
  999. the 'mixbuf' bit of page, then through the SRC apu
  1000. and out to the real 'buffer'. ok. sure. */
  1001. /* input mixer (left/mono) */
  1002. /* parallel in crap, see maestro reg 0xC [8-11] */
  1003. init_capture_apu(chip, es, 2,
  1004. es->mixbuf->buf.addr, ESM_MIXBUF_SIZE/4, /* in words */
  1005. ESM_APU_INPUTMIXER, 0x14);
  1006. /* SRC (left/mono); get input from inputing apu */
  1007. init_capture_apu(chip, es, 0, es->memory->buf.addr, size,
  1008. ESM_APU_SRCONVERTOR, es->apu[2]);
  1009. if (es->fmt & ESS_FMT_STEREO) {
  1010. /* input mixer (right) */
  1011. init_capture_apu(chip, es, 3,
  1012. es->mixbuf->buf.addr + ESM_MIXBUF_SIZE/2,
  1013. ESM_MIXBUF_SIZE/4, /* in words */
  1014. ESM_APU_INPUTMIXER, 0x15);
  1015. /* SRC (right) */
  1016. init_capture_apu(chip, es, 1,
  1017. es->memory->buf.addr + size*2, size,
  1018. ESM_APU_SRCONVERTOR, es->apu[3]);
  1019. }
  1020. freq = runtime->rate;
  1021. /* Sample Rate conversion APUs don't like 0x10000 for their rate */
  1022. if (freq > 47999)
  1023. freq = 47999;
  1024. if (freq < 4000)
  1025. freq = 4000;
  1026. freq = snd_es1968_compute_rate(chip, freq);
  1027. /* Load the frequency, turn on 6dB */
  1028. snd_es1968_apu_set_freq(chip, es->apu[0], freq);
  1029. snd_es1968_apu_set_freq(chip, es->apu[1], freq);
  1030. /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
  1031. freq = 0x10000;
  1032. snd_es1968_apu_set_freq(chip, es->apu[2], freq);
  1033. snd_es1968_apu_set_freq(chip, es->apu[3], freq);
  1034. spin_lock_irqsave(&chip->reg_lock, flags);
  1035. /* clear WP interrupts */
  1036. outw(1, chip->io_port + 0x04);
  1037. /* enable WP ints */
  1038. outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
  1039. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1040. }
  1041. /*******************
  1042. * ALSA Interface *
  1043. *******************/
  1044. static int snd_es1968_pcm_prepare(struct snd_pcm_substream *substream)
  1045. {
  1046. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1047. struct snd_pcm_runtime *runtime = substream->runtime;
  1048. struct esschan *es = runtime->private_data;
  1049. es->dma_size = snd_pcm_lib_buffer_bytes(substream);
  1050. es->frag_size = snd_pcm_lib_period_bytes(substream);
  1051. es->wav_shift = 1; /* maestro handles always 16bit */
  1052. es->fmt = 0;
  1053. if (snd_pcm_format_width(runtime->format) == 16)
  1054. es->fmt |= ESS_FMT_16BIT;
  1055. if (runtime->channels > 1) {
  1056. es->fmt |= ESS_FMT_STEREO;
  1057. if (es->fmt & ESS_FMT_16BIT) /* 8bit is already word shifted */
  1058. es->wav_shift++;
  1059. }
  1060. es->bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
  1061. switch (es->mode) {
  1062. case ESM_MODE_PLAY:
  1063. snd_es1968_playback_setup(chip, es, runtime);
  1064. break;
  1065. case ESM_MODE_CAPTURE:
  1066. snd_es1968_capture_setup(chip, es, runtime);
  1067. break;
  1068. }
  1069. return 0;
  1070. }
  1071. static int snd_es1968_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  1072. {
  1073. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1074. struct esschan *es = substream->runtime->private_data;
  1075. spin_lock(&chip->substream_lock);
  1076. switch (cmd) {
  1077. case SNDRV_PCM_TRIGGER_START:
  1078. case SNDRV_PCM_TRIGGER_RESUME:
  1079. if (es->running)
  1080. break;
  1081. snd_es1968_bob_inc(chip, es->bob_freq);
  1082. es->count = 0;
  1083. es->hwptr = 0;
  1084. snd_es1968_pcm_start(chip, es);
  1085. es->running = 1;
  1086. break;
  1087. case SNDRV_PCM_TRIGGER_STOP:
  1088. case SNDRV_PCM_TRIGGER_SUSPEND:
  1089. if (! es->running)
  1090. break;
  1091. snd_es1968_pcm_stop(chip, es);
  1092. es->running = 0;
  1093. snd_es1968_bob_dec(chip);
  1094. break;
  1095. }
  1096. spin_unlock(&chip->substream_lock);
  1097. return 0;
  1098. }
  1099. static snd_pcm_uframes_t snd_es1968_pcm_pointer(struct snd_pcm_substream *substream)
  1100. {
  1101. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1102. struct esschan *es = substream->runtime->private_data;
  1103. unsigned int ptr;
  1104. ptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
  1105. return bytes_to_frames(substream->runtime, ptr % es->dma_size);
  1106. }
  1107. static struct snd_pcm_hardware snd_es1968_playback = {
  1108. .info = (SNDRV_PCM_INFO_MMAP |
  1109. SNDRV_PCM_INFO_MMAP_VALID |
  1110. SNDRV_PCM_INFO_INTERLEAVED |
  1111. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1112. /*SNDRV_PCM_INFO_PAUSE |*/
  1113. SNDRV_PCM_INFO_RESUME),
  1114. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1115. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1116. .rate_min = 4000,
  1117. .rate_max = 48000,
  1118. .channels_min = 1,
  1119. .channels_max = 2,
  1120. .buffer_bytes_max = 65536,
  1121. .period_bytes_min = 256,
  1122. .period_bytes_max = 65536,
  1123. .periods_min = 1,
  1124. .periods_max = 1024,
  1125. .fifo_size = 0,
  1126. };
  1127. static struct snd_pcm_hardware snd_es1968_capture = {
  1128. .info = (SNDRV_PCM_INFO_NONINTERLEAVED |
  1129. SNDRV_PCM_INFO_MMAP |
  1130. SNDRV_PCM_INFO_MMAP_VALID |
  1131. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1132. /*SNDRV_PCM_INFO_PAUSE |*/
  1133. SNDRV_PCM_INFO_RESUME),
  1134. .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE,
  1135. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1136. .rate_min = 4000,
  1137. .rate_max = 48000,
  1138. .channels_min = 1,
  1139. .channels_max = 2,
  1140. .buffer_bytes_max = 65536,
  1141. .period_bytes_min = 256,
  1142. .period_bytes_max = 65536,
  1143. .periods_min = 1,
  1144. .periods_max = 1024,
  1145. .fifo_size = 0,
  1146. };
  1147. /* *************************
  1148. * DMA memory management *
  1149. *************************/
  1150. /* Because the Maestro can only take addresses relative to the PCM base address
  1151. register :( */
  1152. static int calc_available_memory_size(struct es1968 *chip)
  1153. {
  1154. int max_size = 0;
  1155. struct esm_memory *buf;
  1156. mutex_lock(&chip->memory_mutex);
  1157. list_for_each_entry(buf, &chip->buf_list, list) {
  1158. if (buf->empty && buf->buf.bytes > max_size)
  1159. max_size = buf->buf.bytes;
  1160. }
  1161. mutex_unlock(&chip->memory_mutex);
  1162. if (max_size >= 128*1024)
  1163. max_size = 127*1024;
  1164. return max_size;
  1165. }
  1166. /* allocate a new memory chunk with the specified size */
  1167. static struct esm_memory *snd_es1968_new_memory(struct es1968 *chip, int size)
  1168. {
  1169. struct esm_memory *buf;
  1170. size = ALIGN(size, ESM_MEM_ALIGN);
  1171. mutex_lock(&chip->memory_mutex);
  1172. list_for_each_entry(buf, &chip->buf_list, list) {
  1173. if (buf->empty && buf->buf.bytes >= size)
  1174. goto __found;
  1175. }
  1176. mutex_unlock(&chip->memory_mutex);
  1177. return NULL;
  1178. __found:
  1179. if (buf->buf.bytes > size) {
  1180. struct esm_memory *chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
  1181. if (chunk == NULL) {
  1182. mutex_unlock(&chip->memory_mutex);
  1183. return NULL;
  1184. }
  1185. chunk->buf = buf->buf;
  1186. chunk->buf.bytes -= size;
  1187. chunk->buf.area += size;
  1188. chunk->buf.addr += size;
  1189. chunk->empty = 1;
  1190. buf->buf.bytes = size;
  1191. list_add(&chunk->list, &buf->list);
  1192. }
  1193. buf->empty = 0;
  1194. mutex_unlock(&chip->memory_mutex);
  1195. return buf;
  1196. }
  1197. /* free a memory chunk */
  1198. static void snd_es1968_free_memory(struct es1968 *chip, struct esm_memory *buf)
  1199. {
  1200. struct esm_memory *chunk;
  1201. mutex_lock(&chip->memory_mutex);
  1202. buf->empty = 1;
  1203. if (buf->list.prev != &chip->buf_list) {
  1204. chunk = list_entry(buf->list.prev, struct esm_memory, list);
  1205. if (chunk->empty) {
  1206. chunk->buf.bytes += buf->buf.bytes;
  1207. list_del(&buf->list);
  1208. kfree(buf);
  1209. buf = chunk;
  1210. }
  1211. }
  1212. if (buf->list.next != &chip->buf_list) {
  1213. chunk = list_entry(buf->list.next, struct esm_memory, list);
  1214. if (chunk->empty) {
  1215. buf->buf.bytes += chunk->buf.bytes;
  1216. list_del(&chunk->list);
  1217. kfree(chunk);
  1218. }
  1219. }
  1220. mutex_unlock(&chip->memory_mutex);
  1221. }
  1222. static void snd_es1968_free_dmabuf(struct es1968 *chip)
  1223. {
  1224. struct list_head *p;
  1225. if (! chip->dma.area)
  1226. return;
  1227. snd_dma_reserve_buf(&chip->dma, snd_dma_pci_buf_id(chip->pci));
  1228. while ((p = chip->buf_list.next) != &chip->buf_list) {
  1229. struct esm_memory *chunk = list_entry(p, struct esm_memory, list);
  1230. list_del(p);
  1231. kfree(chunk);
  1232. }
  1233. }
  1234. static int __devinit
  1235. snd_es1968_init_dmabuf(struct es1968 *chip)
  1236. {
  1237. int err;
  1238. struct esm_memory *chunk;
  1239. chip->dma.dev.type = SNDRV_DMA_TYPE_DEV;
  1240. chip->dma.dev.dev = snd_dma_pci_data(chip->pci);
  1241. if (! snd_dma_get_reserved_buf(&chip->dma, snd_dma_pci_buf_id(chip->pci))) {
  1242. err = snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
  1243. snd_dma_pci_data(chip->pci),
  1244. chip->total_bufsize, &chip->dma);
  1245. if (err < 0 || ! chip->dma.area) {
  1246. snd_printk(KERN_ERR "es1968: can't allocate dma pages for size %d\n",
  1247. chip->total_bufsize);
  1248. return -ENOMEM;
  1249. }
  1250. if ((chip->dma.addr + chip->dma.bytes - 1) & ~((1 << 28) - 1)) {
  1251. snd_dma_free_pages(&chip->dma);
  1252. snd_printk(KERN_ERR "es1968: DMA buffer beyond 256MB.\n");
  1253. return -ENOMEM;
  1254. }
  1255. }
  1256. INIT_LIST_HEAD(&chip->buf_list);
  1257. /* allocate an empty chunk */
  1258. chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
  1259. if (chunk == NULL) {
  1260. snd_es1968_free_dmabuf(chip);
  1261. return -ENOMEM;
  1262. }
  1263. memset(chip->dma.area, 0, ESM_MEM_ALIGN);
  1264. chunk->buf = chip->dma;
  1265. chunk->buf.area += ESM_MEM_ALIGN;
  1266. chunk->buf.addr += ESM_MEM_ALIGN;
  1267. chunk->buf.bytes -= ESM_MEM_ALIGN;
  1268. chunk->empty = 1;
  1269. list_add(&chunk->list, &chip->buf_list);
  1270. return 0;
  1271. }
  1272. /* setup the dma_areas */
  1273. /* buffer is extracted from the pre-allocated memory chunk */
  1274. static int snd_es1968_hw_params(struct snd_pcm_substream *substream,
  1275. struct snd_pcm_hw_params *hw_params)
  1276. {
  1277. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1278. struct snd_pcm_runtime *runtime = substream->runtime;
  1279. struct esschan *chan = runtime->private_data;
  1280. int size = params_buffer_bytes(hw_params);
  1281. if (chan->memory) {
  1282. if (chan->memory->buf.bytes >= size) {
  1283. runtime->dma_bytes = size;
  1284. return 0;
  1285. }
  1286. snd_es1968_free_memory(chip, chan->memory);
  1287. }
  1288. chan->memory = snd_es1968_new_memory(chip, size);
  1289. if (chan->memory == NULL) {
  1290. // snd_printd("cannot allocate dma buffer: size = %d\n", size);
  1291. return -ENOMEM;
  1292. }
  1293. snd_pcm_set_runtime_buffer(substream, &chan->memory->buf);
  1294. return 1; /* area was changed */
  1295. }
  1296. /* remove dma areas if allocated */
  1297. static int snd_es1968_hw_free(struct snd_pcm_substream *substream)
  1298. {
  1299. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1300. struct snd_pcm_runtime *runtime = substream->runtime;
  1301. struct esschan *chan;
  1302. if (runtime->private_data == NULL)
  1303. return 0;
  1304. chan = runtime->private_data;
  1305. if (chan->memory) {
  1306. snd_es1968_free_memory(chip, chan->memory);
  1307. chan->memory = NULL;
  1308. }
  1309. return 0;
  1310. }
  1311. /*
  1312. * allocate APU pair
  1313. */
  1314. static int snd_es1968_alloc_apu_pair(struct es1968 *chip, int type)
  1315. {
  1316. int apu;
  1317. for (apu = 0; apu < NR_APUS; apu += 2) {
  1318. if (chip->apu[apu] == ESM_APU_FREE &&
  1319. chip->apu[apu + 1] == ESM_APU_FREE) {
  1320. chip->apu[apu] = chip->apu[apu + 1] = type;
  1321. return apu;
  1322. }
  1323. }
  1324. return -EBUSY;
  1325. }
  1326. /*
  1327. * release APU pair
  1328. */
  1329. static void snd_es1968_free_apu_pair(struct es1968 *chip, int apu)
  1330. {
  1331. chip->apu[apu] = chip->apu[apu + 1] = ESM_APU_FREE;
  1332. }
  1333. /******************
  1334. * PCM open/close *
  1335. ******************/
  1336. static int snd_es1968_playback_open(struct snd_pcm_substream *substream)
  1337. {
  1338. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1339. struct snd_pcm_runtime *runtime = substream->runtime;
  1340. struct esschan *es;
  1341. int apu1;
  1342. /* search 2 APUs */
  1343. apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
  1344. if (apu1 < 0)
  1345. return apu1;
  1346. es = kzalloc(sizeof(*es), GFP_KERNEL);
  1347. if (!es) {
  1348. snd_es1968_free_apu_pair(chip, apu1);
  1349. return -ENOMEM;
  1350. }
  1351. es->apu[0] = apu1;
  1352. es->apu[1] = apu1 + 1;
  1353. es->apu_mode[0] = 0;
  1354. es->apu_mode[1] = 0;
  1355. es->running = 0;
  1356. es->substream = substream;
  1357. es->mode = ESM_MODE_PLAY;
  1358. runtime->private_data = es;
  1359. runtime->hw = snd_es1968_playback;
  1360. runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
  1361. calc_available_memory_size(chip);
  1362. spin_lock_irq(&chip->substream_lock);
  1363. list_add(&es->list, &chip->substream_list);
  1364. spin_unlock_irq(&chip->substream_lock);
  1365. return 0;
  1366. }
  1367. static int snd_es1968_capture_open(struct snd_pcm_substream *substream)
  1368. {
  1369. struct snd_pcm_runtime *runtime = substream->runtime;
  1370. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1371. struct esschan *es;
  1372. int apu1, apu2;
  1373. apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_CAPTURE);
  1374. if (apu1 < 0)
  1375. return apu1;
  1376. apu2 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_RATECONV);
  1377. if (apu2 < 0) {
  1378. snd_es1968_free_apu_pair(chip, apu1);
  1379. return apu2;
  1380. }
  1381. es = kzalloc(sizeof(*es), GFP_KERNEL);
  1382. if (!es) {
  1383. snd_es1968_free_apu_pair(chip, apu1);
  1384. snd_es1968_free_apu_pair(chip, apu2);
  1385. return -ENOMEM;
  1386. }
  1387. es->apu[0] = apu1;
  1388. es->apu[1] = apu1 + 1;
  1389. es->apu[2] = apu2;
  1390. es->apu[3] = apu2 + 1;
  1391. es->apu_mode[0] = 0;
  1392. es->apu_mode[1] = 0;
  1393. es->apu_mode[2] = 0;
  1394. es->apu_mode[3] = 0;
  1395. es->running = 0;
  1396. es->substream = substream;
  1397. es->mode = ESM_MODE_CAPTURE;
  1398. /* get mixbuffer */
  1399. if ((es->mixbuf = snd_es1968_new_memory(chip, ESM_MIXBUF_SIZE)) == NULL) {
  1400. snd_es1968_free_apu_pair(chip, apu1);
  1401. snd_es1968_free_apu_pair(chip, apu2);
  1402. kfree(es);
  1403. return -ENOMEM;
  1404. }
  1405. memset(es->mixbuf->buf.area, 0, ESM_MIXBUF_SIZE);
  1406. runtime->private_data = es;
  1407. runtime->hw = snd_es1968_capture;
  1408. runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
  1409. calc_available_memory_size(chip) - 1024; /* keep MIXBUF size */
  1410. snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  1411. spin_lock_irq(&chip->substream_lock);
  1412. list_add(&es->list, &chip->substream_list);
  1413. spin_unlock_irq(&chip->substream_lock);
  1414. return 0;
  1415. }
  1416. static int snd_es1968_playback_close(struct snd_pcm_substream *substream)
  1417. {
  1418. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1419. struct esschan *es;
  1420. if (substream->runtime->private_data == NULL)
  1421. return 0;
  1422. es = substream->runtime->private_data;
  1423. spin_lock_irq(&chip->substream_lock);
  1424. list_del(&es->list);
  1425. spin_unlock_irq(&chip->substream_lock);
  1426. snd_es1968_free_apu_pair(chip, es->apu[0]);
  1427. kfree(es);
  1428. return 0;
  1429. }
  1430. static int snd_es1968_capture_close(struct snd_pcm_substream *substream)
  1431. {
  1432. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1433. struct esschan *es;
  1434. if (substream->runtime->private_data == NULL)
  1435. return 0;
  1436. es = substream->runtime->private_data;
  1437. spin_lock_irq(&chip->substream_lock);
  1438. list_del(&es->list);
  1439. spin_unlock_irq(&chip->substream_lock);
  1440. snd_es1968_free_memory(chip, es->mixbuf);
  1441. snd_es1968_free_apu_pair(chip, es->apu[0]);
  1442. snd_es1968_free_apu_pair(chip, es->apu[2]);
  1443. kfree(es);
  1444. return 0;
  1445. }
  1446. static struct snd_pcm_ops snd_es1968_playback_ops = {
  1447. .open = snd_es1968_playback_open,
  1448. .close = snd_es1968_playback_close,
  1449. .ioctl = snd_pcm_lib_ioctl,
  1450. .hw_params = snd_es1968_hw_params,
  1451. .hw_free = snd_es1968_hw_free,
  1452. .prepare = snd_es1968_pcm_prepare,
  1453. .trigger = snd_es1968_pcm_trigger,
  1454. .pointer = snd_es1968_pcm_pointer,
  1455. };
  1456. static struct snd_pcm_ops snd_es1968_capture_ops = {
  1457. .open = snd_es1968_capture_open,
  1458. .close = snd_es1968_capture_close,
  1459. .ioctl = snd_pcm_lib_ioctl,
  1460. .hw_params = snd_es1968_hw_params,
  1461. .hw_free = snd_es1968_hw_free,
  1462. .prepare = snd_es1968_pcm_prepare,
  1463. .trigger = snd_es1968_pcm_trigger,
  1464. .pointer = snd_es1968_pcm_pointer,
  1465. };
  1466. /*
  1467. * measure clock
  1468. */
  1469. #define CLOCK_MEASURE_BUFSIZE 16768 /* enough large for a single shot */
  1470. static void __devinit es1968_measure_clock(struct es1968 *chip)
  1471. {
  1472. int i, apu;
  1473. unsigned int pa, offset, t;
  1474. struct esm_memory *memory;
  1475. struct timeval start_time, stop_time;
  1476. if (chip->clock == 0)
  1477. chip->clock = 48000; /* default clock value */
  1478. /* search 2 APUs (although one apu is enough) */
  1479. if ((apu = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY)) < 0) {
  1480. snd_printk(KERN_ERR "Hmm, cannot find empty APU pair!?\n");
  1481. return;
  1482. }
  1483. if ((memory = snd_es1968_new_memory(chip, CLOCK_MEASURE_BUFSIZE)) == NULL) {
  1484. snd_printk(KERN_ERR "cannot allocate dma buffer - using default clock %d\n", chip->clock);
  1485. snd_es1968_free_apu_pair(chip, apu);
  1486. return;
  1487. }
  1488. memset(memory->buf.area, 0, CLOCK_MEASURE_BUFSIZE);
  1489. wave_set_register(chip, apu << 3, (memory->buf.addr - 0x10) & 0xfff8);
  1490. pa = (unsigned int)((memory->buf.addr - chip->dma.addr) >> 1);
  1491. pa |= 0x00400000; /* System RAM (Bit 22) */
  1492. /* initialize apu */
  1493. for (i = 0; i < 16; i++)
  1494. apu_set_register(chip, apu, i, 0x0000);
  1495. apu_set_register(chip, apu, 0, 0x400f);
  1496. apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
  1497. apu_set_register(chip, apu, 5, pa & 0xffff);
  1498. apu_set_register(chip, apu, 6, (pa + CLOCK_MEASURE_BUFSIZE/2) & 0xffff);
  1499. apu_set_register(chip, apu, 7, CLOCK_MEASURE_BUFSIZE/2);
  1500. apu_set_register(chip, apu, 8, 0x0000);
  1501. apu_set_register(chip, apu, 9, 0xD000);
  1502. apu_set_register(chip, apu, 10, 0x8F08);
  1503. apu_set_register(chip, apu, 11, 0x0000);
  1504. spin_lock_irq(&chip->reg_lock);
  1505. outw(1, chip->io_port + 0x04); /* clear WP interrupts */
  1506. outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ); /* enable WP ints */
  1507. spin_unlock_irq(&chip->reg_lock);
  1508. snd_es1968_apu_set_freq(chip, apu, ((unsigned int)48000 << 16) / chip->clock); /* 48000 Hz */
  1509. chip->in_measurement = 1;
  1510. chip->measure_apu = apu;
  1511. spin_lock_irq(&chip->reg_lock);
  1512. snd_es1968_bob_inc(chip, ESM_BOB_FREQ);
  1513. __apu_set_register(chip, apu, 5, pa & 0xffff);
  1514. snd_es1968_trigger_apu(chip, apu, ESM_APU_16BITLINEAR);
  1515. do_gettimeofday(&start_time);
  1516. spin_unlock_irq(&chip->reg_lock);
  1517. msleep(50);
  1518. spin_lock_irq(&chip->reg_lock);
  1519. offset = __apu_get_register(chip, apu, 5);
  1520. do_gettimeofday(&stop_time);
  1521. snd_es1968_trigger_apu(chip, apu, 0); /* stop */
  1522. snd_es1968_bob_dec(chip);
  1523. chip->in_measurement = 0;
  1524. spin_unlock_irq(&chip->reg_lock);
  1525. /* check the current position */
  1526. offset -= (pa & 0xffff);
  1527. offset &= 0xfffe;
  1528. offset += chip->measure_count * (CLOCK_MEASURE_BUFSIZE/2);
  1529. t = stop_time.tv_sec - start_time.tv_sec;
  1530. t *= 1000000;
  1531. if (stop_time.tv_usec < start_time.tv_usec)
  1532. t -= start_time.tv_usec - stop_time.tv_usec;
  1533. else
  1534. t += stop_time.tv_usec - start_time.tv_usec;
  1535. if (t == 0) {
  1536. snd_printk(KERN_ERR "?? calculation error..\n");
  1537. } else {
  1538. offset *= 1000;
  1539. offset = (offset / t) * 1000 + ((offset % t) * 1000) / t;
  1540. if (offset < 47500 || offset > 48500) {
  1541. if (offset >= 40000 && offset <= 50000)
  1542. chip->clock = (chip->clock * offset) / 48000;
  1543. }
  1544. printk(KERN_INFO "es1968: clocking to %d\n", chip->clock);
  1545. }
  1546. snd_es1968_free_memory(chip, memory);
  1547. snd_es1968_free_apu_pair(chip, apu);
  1548. }
  1549. /*
  1550. */
  1551. static void snd_es1968_pcm_free(struct snd_pcm *pcm)
  1552. {
  1553. struct es1968 *esm = pcm->private_data;
  1554. snd_es1968_free_dmabuf(esm);
  1555. esm->pcm = NULL;
  1556. }
  1557. static int __devinit
  1558. snd_es1968_pcm(struct es1968 *chip, int device)
  1559. {
  1560. struct snd_pcm *pcm;
  1561. int err;
  1562. /* get DMA buffer */
  1563. if ((err = snd_es1968_init_dmabuf(chip)) < 0)
  1564. return err;
  1565. /* set PCMBAR */
  1566. wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
  1567. wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
  1568. wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
  1569. wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
  1570. if ((err = snd_pcm_new(chip->card, "ESS Maestro", device,
  1571. chip->playback_streams,
  1572. chip->capture_streams, &pcm)) < 0)
  1573. return err;
  1574. pcm->private_data = chip;
  1575. pcm->private_free = snd_es1968_pcm_free;
  1576. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1968_playback_ops);
  1577. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1968_capture_ops);
  1578. pcm->info_flags = 0;
  1579. strcpy(pcm->name, "ESS Maestro");
  1580. chip->pcm = pcm;
  1581. return 0;
  1582. }
  1583. /*
  1584. * suppress jitter on some maestros when playing stereo
  1585. */
  1586. static void snd_es1968_suppress_jitter(struct es1968 *chip, struct esschan *es)
  1587. {
  1588. unsigned int cp1;
  1589. unsigned int cp2;
  1590. unsigned int diff;
  1591. cp1 = __apu_get_register(chip, 0, 5);
  1592. cp2 = __apu_get_register(chip, 1, 5);
  1593. diff = (cp1 > cp2 ? cp1 - cp2 : cp2 - cp1);
  1594. if (diff > 1)
  1595. __maestro_write(chip, IDR0_DATA_PORT, cp1);
  1596. }
  1597. /*
  1598. * update pointer
  1599. */
  1600. static void snd_es1968_update_pcm(struct es1968 *chip, struct esschan *es)
  1601. {
  1602. unsigned int hwptr;
  1603. unsigned int diff;
  1604. struct snd_pcm_substream *subs = es->substream;
  1605. if (subs == NULL || !es->running)
  1606. return;
  1607. hwptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
  1608. hwptr %= es->dma_size;
  1609. diff = (es->dma_size + hwptr - es->hwptr) % es->dma_size;
  1610. es->hwptr = hwptr;
  1611. es->count += diff;
  1612. if (es->count > es->frag_size) {
  1613. spin_unlock(&chip->substream_lock);
  1614. snd_pcm_period_elapsed(subs);
  1615. spin_lock(&chip->substream_lock);
  1616. es->count %= es->frag_size;
  1617. }
  1618. }
  1619. /* The hardware volume works by incrementing / decrementing 2 counters
  1620. (without wrap around) in response to volume button presses and then
  1621. generating an interrupt. The pair of counters is stored in bits 1-3 and 5-7
  1622. of a byte wide register. The meaning of bits 0 and 4 is unknown. */
  1623. static void es1968_update_hw_volume(unsigned long private_data)
  1624. {
  1625. struct es1968 *chip = (struct es1968 *) private_data;
  1626. int x, val;
  1627. #ifndef CONFIG_SND_ES1968_INPUT
  1628. unsigned long flags;
  1629. #endif
  1630. /* Figure out which volume control button was pushed,
  1631. based on differences from the default register
  1632. values. */
  1633. x = inb(chip->io_port + 0x1c) & 0xee;
  1634. /* Reset the volume control registers. */
  1635. outb(0x88, chip->io_port + 0x1c);
  1636. outb(0x88, chip->io_port + 0x1d);
  1637. outb(0x88, chip->io_port + 0x1e);
  1638. outb(0x88, chip->io_port + 0x1f);
  1639. if (chip->in_suspend)
  1640. return;
  1641. #ifndef CONFIG_SND_ES1968_INPUT
  1642. if (! chip->master_switch || ! chip->master_volume)
  1643. return;
  1644. /* FIXME: we can't call snd_ac97_* functions since here is in tasklet. */
  1645. spin_lock_irqsave(&chip->ac97_lock, flags);
  1646. val = chip->ac97->regs[AC97_MASTER];
  1647. switch (x) {
  1648. case 0x88:
  1649. /* mute */
  1650. val ^= 0x8000;
  1651. chip->ac97->regs[AC97_MASTER] = val;
  1652. outw(val, chip->io_port + ESM_AC97_DATA);
  1653. outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
  1654. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1655. &chip->master_switch->id);
  1656. break;
  1657. case 0xaa:
  1658. /* volume up */
  1659. if ((val & 0x7f) > 0)
  1660. val--;
  1661. if ((val & 0x7f00) > 0)
  1662. val -= 0x0100;
  1663. chip->ac97->regs[AC97_MASTER] = val;
  1664. outw(val, chip->io_port + ESM_AC97_DATA);
  1665. outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
  1666. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1667. &chip->master_volume->id);
  1668. break;
  1669. case 0x66:
  1670. /* volume down */
  1671. if ((val & 0x7f) < 0x1f)
  1672. val++;
  1673. if ((val & 0x7f00) < 0x1f00)
  1674. val += 0x0100;
  1675. chip->ac97->regs[AC97_MASTER] = val;
  1676. outw(val, chip->io_port + ESM_AC97_DATA);
  1677. outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
  1678. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1679. &chip->master_volume->id);
  1680. break;
  1681. }
  1682. spin_unlock_irqrestore(&chip->ac97_lock, flags);
  1683. #else
  1684. if (!chip->input_dev)
  1685. return;
  1686. val = 0;
  1687. switch (x) {
  1688. case 0x88:
  1689. /* The counters have not changed, yet we've received a HV
  1690. interrupt. According to tests run by various people this
  1691. happens when pressing the mute button. */
  1692. val = KEY_MUTE;
  1693. break;
  1694. case 0xaa:
  1695. /* counters increased by 1 -> volume up */
  1696. val = KEY_VOLUMEUP;
  1697. break;
  1698. case 0x66:
  1699. /* counters decreased by 1 -> volume down */
  1700. val = KEY_VOLUMEDOWN;
  1701. break;
  1702. }
  1703. if (val) {
  1704. input_report_key(chip->input_dev, val, 1);
  1705. input_sync(chip->input_dev);
  1706. input_report_key(chip->input_dev, val, 0);
  1707. input_sync(chip->input_dev);
  1708. }
  1709. #endif
  1710. }
  1711. /*
  1712. * interrupt handler
  1713. */
  1714. static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id)
  1715. {
  1716. struct es1968 *chip = dev_id;
  1717. u32 event;
  1718. if (!(event = inb(chip->io_port + 0x1A)))
  1719. return IRQ_NONE;
  1720. outw(inw(chip->io_port + 4) & 1, chip->io_port + 4);
  1721. if (event & ESM_HWVOL_IRQ)
  1722. #ifdef CONFIG_SND_ES1968_INPUT
  1723. es1968_update_hw_volume((unsigned long)chip);
  1724. #else
  1725. tasklet_schedule(&chip->hwvol_tq); /* we'll do this later */
  1726. #endif
  1727. /* else ack 'em all, i imagine */
  1728. outb(0xFF, chip->io_port + 0x1A);
  1729. if ((event & ESM_MPU401_IRQ) && chip->rmidi) {
  1730. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  1731. }
  1732. if (event & ESM_SOUND_IRQ) {
  1733. struct esschan *es;
  1734. spin_lock(&chip->substream_lock);
  1735. list_for_each_entry(es, &chip->substream_list, list) {
  1736. if (es->running) {
  1737. snd_es1968_update_pcm(chip, es);
  1738. if (es->fmt & ESS_FMT_STEREO)
  1739. snd_es1968_suppress_jitter(chip, es);
  1740. }
  1741. }
  1742. spin_unlock(&chip->substream_lock);
  1743. if (chip->in_measurement) {
  1744. unsigned int curp = __apu_get_register(chip, chip->measure_apu, 5);
  1745. if (curp < chip->measure_lastpos)
  1746. chip->measure_count++;
  1747. chip->measure_lastpos = curp;
  1748. }
  1749. }
  1750. return IRQ_HANDLED;
  1751. }
  1752. /*
  1753. * Mixer stuff
  1754. */
  1755. static int __devinit
  1756. snd_es1968_mixer(struct es1968 *chip)
  1757. {
  1758. struct snd_ac97_bus *pbus;
  1759. struct snd_ac97_template ac97;
  1760. #ifndef CONFIG_SND_ES1968_INPUT
  1761. struct snd_ctl_elem_id elem_id;
  1762. #endif
  1763. int err;
  1764. static struct snd_ac97_bus_ops ops = {
  1765. .write = snd_es1968_ac97_write,
  1766. .read = snd_es1968_ac97_read,
  1767. };
  1768. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  1769. return err;
  1770. pbus->no_vra = 1; /* ES1968 doesn't need VRA */
  1771. memset(&ac97, 0, sizeof(ac97));
  1772. ac97.private_data = chip;
  1773. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
  1774. return err;
  1775. #ifndef CONFIG_SND_ES1968_INPUT
  1776. /* attach master switch / volumes for h/w volume control */
  1777. memset(&elem_id, 0, sizeof(elem_id));
  1778. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1779. strcpy(elem_id.name, "Master Playback Switch");
  1780. chip->master_switch = snd_ctl_find_id(chip->card, &elem_id);
  1781. memset(&elem_id, 0, sizeof(elem_id));
  1782. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1783. strcpy(elem_id.name, "Master Playback Volume");
  1784. chip->master_volume = snd_ctl_find_id(chip->card, &elem_id);
  1785. #endif
  1786. return 0;
  1787. }
  1788. /*
  1789. * reset ac97 codec
  1790. */
  1791. static void snd_es1968_ac97_reset(struct es1968 *chip)
  1792. {
  1793. unsigned long ioaddr = chip->io_port;
  1794. unsigned short save_ringbus_a;
  1795. unsigned short save_68;
  1796. unsigned short w;
  1797. unsigned int vend;
  1798. /* save configuration */
  1799. save_ringbus_a = inw(ioaddr + 0x36);
  1800. //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38); /* clear second codec id? */
  1801. /* set command/status address i/o to 1st codec */
  1802. outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
  1803. outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
  1804. /* disable ac link */
  1805. outw(0x0000, ioaddr + 0x36);
  1806. save_68 = inw(ioaddr + 0x68);
  1807. pci_read_config_word(chip->pci, 0x58, &w); /* something magical with gpio and bus arb. */
  1808. pci_read_config_dword(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  1809. if (w & 1)
  1810. save_68 |= 0x10;
  1811. outw(0xfffe, ioaddr + 0x64); /* unmask gpio 0 */
  1812. outw(0x0001, ioaddr + 0x68); /* gpio write */
  1813. outw(0x0000, ioaddr + 0x60); /* write 0 to gpio 0 */
  1814. udelay(20);
  1815. outw(0x0001, ioaddr + 0x60); /* write 1 to gpio 1 */
  1816. msleep(20);
  1817. outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
  1818. outw((inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
  1819. outw((inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
  1820. outw((inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
  1821. /* now the second codec */
  1822. /* disable ac link */
  1823. outw(0x0000, ioaddr + 0x36);
  1824. outw(0xfff7, ioaddr + 0x64); /* unmask gpio 3 */
  1825. save_68 = inw(ioaddr + 0x68);
  1826. outw(0x0009, ioaddr + 0x68); /* gpio write 0 & 3 ?? */
  1827. outw(0x0001, ioaddr + 0x60); /* write 1 to gpio */
  1828. udelay(20);
  1829. outw(0x0009, ioaddr + 0x60); /* write 9 to gpio */
  1830. msleep(500);
  1831. //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
  1832. outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
  1833. outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
  1834. #if 0 /* the loop here needs to be much better if we want it.. */
  1835. snd_printk(KERN_INFO "trying software reset\n");
  1836. /* try and do a software reset */
  1837. outb(0x80 | 0x7c, ioaddr + 0x30);
  1838. for (w = 0;; w++) {
  1839. if ((inw(ioaddr + 0x30) & 1) == 0) {
  1840. if (inb(ioaddr + 0x32) != 0)
  1841. break;
  1842. outb(0x80 | 0x7d, ioaddr + 0x30);
  1843. if (((inw(ioaddr + 0x30) & 1) == 0)
  1844. && (inb(ioaddr + 0x32) != 0))
  1845. break;
  1846. outb(0x80 | 0x7f, ioaddr + 0x30);
  1847. if (((inw(ioaddr + 0x30) & 1) == 0)
  1848. && (inb(ioaddr + 0x32) != 0))
  1849. break;
  1850. }
  1851. if (w > 10000) {
  1852. outb(inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
  1853. msleep(500); /* oh my.. */
  1854. outb(inb(ioaddr + 0x37) & ~0x08,
  1855. ioaddr + 0x37);
  1856. udelay(1);
  1857. outw(0x80, ioaddr + 0x30);
  1858. for (w = 0; w < 10000; w++) {
  1859. if ((inw(ioaddr + 0x30) & 1) == 0)
  1860. break;
  1861. }
  1862. }
  1863. }
  1864. #endif
  1865. if (vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
  1866. /* turn on external amp? */
  1867. outw(0xf9ff, ioaddr + 0x64);
  1868. outw(inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
  1869. outw(0x0209, ioaddr + 0x60);
  1870. }
  1871. /* restore.. */
  1872. outw(save_ringbus_a, ioaddr + 0x36);
  1873. /* Turn on the 978 docking chip.
  1874. First frob the "master output enable" bit,
  1875. then set most of the playback volume control registers to max. */
  1876. outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
  1877. outb(0xff, ioaddr+0xc3);
  1878. outb(0xff, ioaddr+0xc4);
  1879. outb(0xff, ioaddr+0xc6);
  1880. outb(0xff, ioaddr+0xc8);
  1881. outb(0x3f, ioaddr+0xcf);
  1882. outb(0x3f, ioaddr+0xd0);
  1883. }
  1884. static void snd_es1968_reset(struct es1968 *chip)
  1885. {
  1886. /* Reset */
  1887. outw(ESM_RESET_MAESTRO | ESM_RESET_DIRECTSOUND,
  1888. chip->io_port + ESM_PORT_HOST_IRQ);
  1889. udelay(10);
  1890. outw(0x0000, chip->io_port + ESM_PORT_HOST_IRQ);
  1891. udelay(10);
  1892. }
  1893. /*
  1894. * initialize maestro chip
  1895. */
  1896. static void snd_es1968_chip_init(struct es1968 *chip)
  1897. {
  1898. struct pci_dev *pci = chip->pci;
  1899. int i;
  1900. unsigned long iobase = chip->io_port;
  1901. u16 w;
  1902. u32 n;
  1903. /* We used to muck around with pci config space that
  1904. * we had no business messing with. We don't know enough
  1905. * about the machine to know which DMA mode is appropriate,
  1906. * etc. We were guessing wrong on some machines and making
  1907. * them unhappy. We now trust in the BIOS to do things right,
  1908. * which almost certainly means a new host of problems will
  1909. * arise with broken BIOS implementations. screw 'em.
  1910. * We're already intolerant of machines that don't assign
  1911. * IRQs.
  1912. */
  1913. /* Config Reg A */
  1914. pci_read_config_word(pci, ESM_CONFIG_A, &w);
  1915. w &= ~DMA_CLEAR; /* Clear DMA bits */
  1916. w &= ~(PIC_SNOOP1 | PIC_SNOOP2); /* Clear Pic Snoop Mode Bits */
  1917. w &= ~SAFEGUARD; /* Safeguard off */
  1918. w |= POST_WRITE; /* Posted write */
  1919. w |= PCI_TIMING; /* PCI timing on */
  1920. /* XXX huh? claims to be reserved.. */
  1921. w &= ~SWAP_LR; /* swap left/right
  1922. seems to only have effect on SB
  1923. Emulation */
  1924. w &= ~SUBTR_DECODE; /* Subtractive decode off */
  1925. pci_write_config_word(pci, ESM_CONFIG_A, w);
  1926. /* Config Reg B */
  1927. pci_read_config_word(pci, ESM_CONFIG_B, &w);
  1928. w &= ~(1 << 15); /* Turn off internal clock multiplier */
  1929. /* XXX how do we know which to use? */
  1930. w &= ~(1 << 14); /* External clock */
  1931. w &= ~SPDIF_CONFB; /* disable S/PDIF output */
  1932. w |= HWV_CONFB; /* HWV on */
  1933. w |= DEBOUNCE; /* Debounce off: easier to push the HW buttons */
  1934. w &= ~GPIO_CONFB; /* GPIO 4:5 */
  1935. w |= CHI_CONFB; /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
  1936. w &= ~IDMA_CONFB; /* IDMA off (undocumented) */
  1937. w &= ~MIDI_FIX; /* MIDI fix off (undoc) */
  1938. w &= ~(1 << 1); /* reserved, always write 0 */
  1939. w &= ~IRQ_TO_ISA; /* IRQ to ISA off (undoc) */
  1940. pci_write_config_word(pci, ESM_CONFIG_B, w);
  1941. /* DDMA off */
  1942. pci_read_config_word(pci, ESM_DDMA, &w);
  1943. w &= ~(1 << 0);
  1944. pci_write_config_word(pci, ESM_DDMA, w);
  1945. /*
  1946. * Legacy mode
  1947. */
  1948. pci_read_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, &w);
  1949. w |= ESS_DISABLE_AUDIO; /* Disable Legacy Audio */
  1950. w &= ~ESS_ENABLE_SERIAL_IRQ; /* Disable SIRQ */
  1951. w &= ~(0x1f); /* disable mpu irq/io, game port, fm, SB */
  1952. pci_write_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, w);
  1953. /* Set up 978 docking control chip. */
  1954. pci_read_config_word(pci, 0x58, &w);
  1955. w|=1<<2; /* Enable 978. */
  1956. w|=1<<3; /* Turn on 978 hardware volume control. */
  1957. w&=~(1<<11); /* Turn on 978 mixer volume control. */
  1958. pci_write_config_word(pci, 0x58, w);
  1959. /* Sound Reset */
  1960. snd_es1968_reset(chip);
  1961. /*
  1962. * Ring Bus Setup
  1963. */
  1964. /* setup usual 0x34 stuff.. 0x36 may be chip specific */
  1965. outw(0xC090, iobase + ESM_RING_BUS_DEST); /* direct sound, stereo */
  1966. udelay(20);
  1967. outw(0x3000, iobase + ESM_RING_BUS_CONTR_A); /* enable ringbus/serial */
  1968. udelay(20);
  1969. /*
  1970. * Reset the CODEC
  1971. */
  1972. snd_es1968_ac97_reset(chip);
  1973. /* Ring Bus Control B */
  1974. n = inl(iobase + ESM_RING_BUS_CONTR_B);
  1975. n &= ~RINGB_EN_SPDIF; /* SPDIF off */
  1976. //w |= RINGB_EN_2CODEC; /* enable 2nd codec */
  1977. outl(n, iobase + ESM_RING_BUS_CONTR_B);
  1978. /* Set hardware volume control registers to midpoints.
  1979. We can tell which button was pushed based on how they change. */
  1980. outb(0x88, iobase+0x1c);
  1981. outb(0x88, iobase+0x1d);
  1982. outb(0x88, iobase+0x1e);
  1983. outb(0x88, iobase+0x1f);
  1984. /* it appears some maestros (dell 7500) only work if these are set,
  1985. regardless of wether we use the assp or not. */
  1986. outb(0, iobase + ASSP_CONTROL_B);
  1987. outb(3, iobase + ASSP_CONTROL_A); /* M: Reserved bits... */
  1988. outb(0, iobase + ASSP_CONTROL_C); /* M: Disable ASSP, ASSP IRQ's and FM Port */
  1989. /*
  1990. * set up wavecache
  1991. */
  1992. for (i = 0; i < 16; i++) {
  1993. /* Write 0 into the buffer area 0x1E0->1EF */
  1994. outw(0x01E0 + i, iobase + WC_INDEX);
  1995. outw(0x0000, iobase + WC_DATA);
  1996. /* The 1.10 test program seem to write 0 into the buffer area
  1997. * 0x1D0-0x1DF too.*/
  1998. outw(0x01D0 + i, iobase + WC_INDEX);
  1999. outw(0x0000, iobase + WC_DATA);
  2000. }
  2001. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  2002. (wave_get_register(chip, IDR7_WAVE_ROMRAM) & 0xFF00));
  2003. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  2004. wave_get_register(chip, IDR7_WAVE_ROMRAM) | 0x100);
  2005. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  2006. wave_get_register(chip, IDR7_WAVE_ROMRAM) & ~0x200);
  2007. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  2008. wave_get_register(chip, IDR7_WAVE_ROMRAM) | ~0x400);
  2009. maestro_write(chip, IDR2_CRAM_DATA, 0x0000);
  2010. /* Now back to the DirectSound stuff */
  2011. /* audio serial configuration.. ? */
  2012. maestro_write(chip, 0x08, 0xB004);
  2013. maestro_write(chip, 0x09, 0x001B);
  2014. maestro_write(chip, 0x0A, 0x8000);
  2015. maestro_write(chip, 0x0B, 0x3F37);
  2016. maestro_write(chip, 0x0C, 0x0098);
  2017. /* parallel in, has something to do with recording :) */
  2018. maestro_write(chip, 0x0C,
  2019. (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
  2020. /* parallel out */
  2021. maestro_write(chip, 0x0C,
  2022. (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
  2023. maestro_write(chip, 0x0D, 0x7632);
  2024. /* Wave cache control on - test off, sg off,
  2025. enable, enable extra chans 1Mb */
  2026. w = inw(iobase + WC_CONTROL);
  2027. w &= ~0xFA00; /* Seems to be reserved? I don't know */
  2028. w |= 0xA000; /* reserved... I don't know */
  2029. w &= ~0x0200; /* Channels 56,57,58,59 as Extra Play,Rec Channel enable
  2030. Seems to crash the Computer if enabled... */
  2031. w |= 0x0100; /* Wave Cache Operation Enabled */
  2032. w |= 0x0080; /* Channels 60/61 as Placback/Record enabled */
  2033. w &= ~0x0060; /* Clear Wavtable Size */
  2034. w |= 0x0020; /* Wavetable Size : 1MB */
  2035. /* Bit 4 is reserved */
  2036. w &= ~0x000C; /* DMA Stuff? I don't understand what the datasheet means */
  2037. /* Bit 1 is reserved */
  2038. w &= ~0x0001; /* Test Mode off */
  2039. outw(w, iobase + WC_CONTROL);
  2040. /* Now clear the APU control ram */
  2041. for (i = 0; i < NR_APUS; i++) {
  2042. for (w = 0; w < NR_APU_REGS; w++)
  2043. apu_set_register(chip, i, w, 0);
  2044. }
  2045. }
  2046. /* Enable IRQ's */
  2047. static void snd_es1968_start_irq(struct es1968 *chip)
  2048. {
  2049. unsigned short w;
  2050. w = ESM_HIRQ_DSIE | ESM_HIRQ_HW_VOLUME;
  2051. if (chip->rmidi)
  2052. w |= ESM_HIRQ_MPU401;
  2053. outb(w, chip->io_port + 0x1A);
  2054. outw(w, chip->io_port + ESM_PORT_HOST_IRQ);
  2055. }
  2056. #ifdef CONFIG_PM
  2057. /*
  2058. * PM support
  2059. */
  2060. static int es1968_suspend(struct pci_dev *pci, pm_message_t state)
  2061. {
  2062. struct snd_card *card = pci_get_drvdata(pci);
  2063. struct es1968 *chip = card->private_data;
  2064. if (! chip->do_pm)
  2065. return 0;
  2066. chip->in_suspend = 1;
  2067. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2068. snd_pcm_suspend_all(chip->pcm);
  2069. snd_ac97_suspend(chip->ac97);
  2070. snd_es1968_bob_stop(chip);
  2071. pci_disable_device(pci);
  2072. pci_save_state(pci);
  2073. pci_set_power_state(pci, pci_choose_state(pci, state));
  2074. return 0;
  2075. }
  2076. static int es1968_resume(struct pci_dev *pci)
  2077. {
  2078. struct snd_card *card = pci_get_drvdata(pci);
  2079. struct es1968 *chip = card->private_data;
  2080. struct esschan *es;
  2081. if (! chip->do_pm)
  2082. return 0;
  2083. /* restore all our config */
  2084. pci_set_power_state(pci, PCI_D0);
  2085. pci_restore_state(pci);
  2086. if (pci_enable_device(pci) < 0) {
  2087. printk(KERN_ERR "es1968: pci_enable_device failed, "
  2088. "disabling device\n");
  2089. snd_card_disconnect(card);
  2090. return -EIO;
  2091. }
  2092. pci_set_master(pci);
  2093. snd_es1968_chip_init(chip);
  2094. /* need to restore the base pointers.. */
  2095. if (chip->dma.addr) {
  2096. /* set PCMBAR */
  2097. wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
  2098. }
  2099. snd_es1968_start_irq(chip);
  2100. /* restore ac97 state */
  2101. snd_ac97_resume(chip->ac97);
  2102. list_for_each_entry(es, &chip->substream_list, list) {
  2103. switch (es->mode) {
  2104. case ESM_MODE_PLAY:
  2105. snd_es1968_playback_setup(chip, es, es->substream->runtime);
  2106. break;
  2107. case ESM_MODE_CAPTURE:
  2108. snd_es1968_capture_setup(chip, es, es->substream->runtime);
  2109. break;
  2110. }
  2111. }
  2112. /* start timer again */
  2113. if (chip->bobclient)
  2114. snd_es1968_bob_start(chip);
  2115. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2116. chip->in_suspend = 0;
  2117. return 0;
  2118. }
  2119. #endif /* CONFIG_PM */
  2120. #ifdef SUPPORT_JOYSTICK
  2121. #define JOYSTICK_ADDR 0x200
  2122. static int __devinit snd_es1968_create_gameport(struct es1968 *chip, int dev)
  2123. {
  2124. struct gameport *gp;
  2125. struct resource *r;
  2126. u16 val;
  2127. if (!joystick[dev])
  2128. return -ENODEV;
  2129. r = request_region(JOYSTICK_ADDR, 8, "ES1968 gameport");
  2130. if (!r)
  2131. return -EBUSY;
  2132. chip->gameport = gp = gameport_allocate_port();
  2133. if (!gp) {
  2134. printk(KERN_ERR "es1968: cannot allocate memory for gameport\n");
  2135. release_and_free_resource(r);
  2136. return -ENOMEM;
  2137. }
  2138. pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &val);
  2139. pci_write_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, val | 0x04);
  2140. gameport_set_name(gp, "ES1968 Gameport");
  2141. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2142. gameport_set_dev_parent(gp, &chip->pci->dev);
  2143. gp->io = JOYSTICK_ADDR;
  2144. gameport_set_port_data(gp, r);
  2145. gameport_register_port(gp);
  2146. return 0;
  2147. }
  2148. static void snd_es1968_free_gameport(struct es1968 *chip)
  2149. {
  2150. if (chip->gameport) {
  2151. struct resource *r = gameport_get_port_data(chip->gameport);
  2152. gameport_unregister_port(chip->gameport);
  2153. chip->gameport = NULL;
  2154. release_and_free_resource(r);
  2155. }
  2156. }
  2157. #else
  2158. static inline int snd_es1968_create_gameport(struct es1968 *chip, int dev) { return -ENOSYS; }
  2159. static inline void snd_es1968_free_gameport(struct es1968 *chip) { }
  2160. #endif
  2161. #ifdef CONFIG_SND_ES1968_INPUT
  2162. static int __devinit snd_es1968_input_register(struct es1968 *chip)
  2163. {
  2164. struct input_dev *input_dev;
  2165. int err;
  2166. input_dev = input_allocate_device();
  2167. if (!input_dev)
  2168. return -ENOMEM;
  2169. snprintf(chip->phys, sizeof(chip->phys), "pci-%s/input0",
  2170. pci_name(chip->pci));
  2171. input_dev->name = chip->card->driver;
  2172. input_dev->phys = chip->phys;
  2173. input_dev->id.bustype = BUS_PCI;
  2174. input_dev->id.vendor = chip->pci->vendor;
  2175. input_dev->id.product = chip->pci->device;
  2176. input_dev->dev.parent = &chip->pci->dev;
  2177. __set_bit(EV_KEY, input_dev->evbit);
  2178. __set_bit(KEY_MUTE, input_dev->keybit);
  2179. __set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
  2180. __set_bit(KEY_VOLUMEUP, input_dev->keybit);
  2181. err = input_register_device(input_dev);
  2182. if (err) {
  2183. input_free_device(input_dev);
  2184. return err;
  2185. }
  2186. chip->input_dev = input_dev;
  2187. return 0;
  2188. }
  2189. #endif /* CONFIG_SND_ES1968_INPUT */
  2190. #ifdef CONFIG_SND_ES1968_RADIO
  2191. #define GPIO_DATA 0x60
  2192. #define IO_MASK 4 /* mask register offset from GPIO_DATA
  2193. bits 1=unmask write to given bit */
  2194. #define IO_DIR 8 /* direction register offset from GPIO_DATA
  2195. bits 0/1=read/write direction */
  2196. /* mask bits for GPIO lines */
  2197. #define STR_DATA 0x0040 /* GPIO6 */
  2198. #define STR_CLK 0x0080 /* GPIO7 */
  2199. #define STR_WREN 0x0100 /* GPIO8 */
  2200. #define STR_MOST 0x0200 /* GPIO9 */
  2201. static void snd_es1968_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
  2202. {
  2203. struct es1968 *chip = tea->private_data;
  2204. unsigned long io = chip->io_port + GPIO_DATA;
  2205. u16 val = 0;
  2206. val |= (pins & TEA575X_DATA) ? STR_DATA : 0;
  2207. val |= (pins & TEA575X_CLK) ? STR_CLK : 0;
  2208. val |= (pins & TEA575X_WREN) ? STR_WREN : 0;
  2209. outw(val, io);
  2210. }
  2211. static u8 snd_es1968_tea575x_get_pins(struct snd_tea575x *tea)
  2212. {
  2213. struct es1968 *chip = tea->private_data;
  2214. unsigned long io = chip->io_port + GPIO_DATA;
  2215. u16 val = inw(io);
  2216. return (val & STR_DATA) ? TEA575X_DATA : 0 |
  2217. (val & STR_MOST) ? TEA575X_MOST : 0;
  2218. }
  2219. static void snd_es1968_tea575x_set_direction(struct snd_tea575x *tea, bool output)
  2220. {
  2221. struct es1968 *chip = tea->private_data;
  2222. unsigned long io = chip->io_port + GPIO_DATA;
  2223. u16 odir = inw(io + IO_DIR);
  2224. if (output) {
  2225. outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
  2226. outw(odir | STR_DATA | STR_CLK | STR_WREN, io + IO_DIR);
  2227. } else {
  2228. outw(~(STR_CLK | STR_WREN | STR_DATA | STR_MOST), io + IO_MASK);
  2229. outw((odir & ~(STR_DATA | STR_MOST)) | STR_CLK | STR_WREN, io + IO_DIR);
  2230. }
  2231. }
  2232. static struct snd_tea575x_ops snd_es1968_tea_ops = {
  2233. .set_pins = snd_es1968_tea575x_set_pins,
  2234. .get_pins = snd_es1968_tea575x_get_pins,
  2235. .set_direction = snd_es1968_tea575x_set_direction,
  2236. };
  2237. #endif
  2238. static int snd_es1968_free(struct es1968 *chip)
  2239. {
  2240. #ifdef CONFIG_SND_ES1968_INPUT
  2241. if (chip->input_dev)
  2242. input_unregister_device(chip->input_dev);
  2243. #endif
  2244. if (chip->io_port) {
  2245. if (chip->irq >= 0)
  2246. synchronize_irq(chip->irq);
  2247. outw(1, chip->io_port + 0x04); /* clear WP interrupts */
  2248. outw(0, chip->io_port + ESM_PORT_HOST_IRQ); /* disable IRQ */
  2249. }
  2250. #ifdef CONFIG_SND_ES1968_RADIO
  2251. snd_tea575x_exit(&chip->tea);
  2252. #endif
  2253. if (chip->irq >= 0)
  2254. free_irq(chip->irq, chip);
  2255. snd_es1968_free_gameport(chip);
  2256. pci_release_regions(chip->pci);
  2257. pci_disable_device(chip->pci);
  2258. kfree(chip);
  2259. return 0;
  2260. }
  2261. static int snd_es1968_dev_free(struct snd_device *device)
  2262. {
  2263. struct es1968 *chip = device->device_data;
  2264. return snd_es1968_free(chip);
  2265. }
  2266. struct ess_device_list {
  2267. unsigned short type; /* chip type */
  2268. unsigned short vendor; /* subsystem vendor id */
  2269. };
  2270. static struct ess_device_list pm_whitelist[] __devinitdata = {
  2271. { TYPE_MAESTRO2E, 0x0e11 }, /* Compaq Armada */
  2272. { TYPE_MAESTRO2E, 0x1028 },
  2273. { TYPE_MAESTRO2E, 0x103c },
  2274. { TYPE_MAESTRO2E, 0x1179 },
  2275. { TYPE_MAESTRO2E, 0x14c0 }, /* HP omnibook 4150 */
  2276. { TYPE_MAESTRO2E, 0x1558 },
  2277. };
  2278. static struct ess_device_list mpu_blacklist[] __devinitdata = {
  2279. { TYPE_MAESTRO2, 0x125d },
  2280. };
  2281. static int __devinit snd_es1968_create(struct snd_card *card,
  2282. struct pci_dev *pci,
  2283. int total_bufsize,
  2284. int play_streams,
  2285. int capt_streams,
  2286. int chip_type,
  2287. int do_pm,
  2288. struct es1968 **chip_ret)
  2289. {
  2290. static struct snd_device_ops ops = {
  2291. .dev_free = snd_es1968_dev_free,
  2292. };
  2293. struct es1968 *chip;
  2294. int i, err;
  2295. *chip_ret = NULL;
  2296. /* enable PCI device */
  2297. if ((err = pci_enable_device(pci)) < 0)
  2298. return err;
  2299. /* check, if we can restrict PCI DMA transfers to 28 bits */
  2300. if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
  2301. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
  2302. snd_printk(KERN_ERR "architecture does not support 28bit PCI busmaster DMA\n");
  2303. pci_disable_device(pci);
  2304. return -ENXIO;
  2305. }
  2306. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  2307. if (! chip) {
  2308. pci_disable_device(pci);
  2309. return -ENOMEM;
  2310. }
  2311. /* Set Vars */
  2312. chip->type = chip_type;
  2313. spin_lock_init(&chip->reg_lock);
  2314. spin_lock_init(&chip->substream_lock);
  2315. INIT_LIST_HEAD(&chip->buf_list);
  2316. INIT_LIST_HEAD(&chip->substream_list);
  2317. mutex_init(&chip->memory_mutex);
  2318. #ifndef CONFIG_SND_ES1968_INPUT
  2319. spin_lock_init(&chip->ac97_lock);
  2320. tasklet_init(&chip->hwvol_tq, es1968_update_hw_volume, (unsigned long)chip);
  2321. #endif
  2322. chip->card = card;
  2323. chip->pci = pci;
  2324. chip->irq = -1;
  2325. chip->total_bufsize = total_bufsize; /* in bytes */
  2326. chip->playback_streams = play_streams;
  2327. chip->capture_streams = capt_streams;
  2328. if ((err = pci_request_regions(pci, "ESS Maestro")) < 0) {
  2329. kfree(chip);
  2330. pci_disable_device(pci);
  2331. return err;
  2332. }
  2333. chip->io_port = pci_resource_start(pci, 0);
  2334. if (request_irq(pci->irq, snd_es1968_interrupt, IRQF_SHARED,
  2335. "ESS Maestro", chip)) {
  2336. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2337. snd_es1968_free(chip);
  2338. return -EBUSY;
  2339. }
  2340. chip->irq = pci->irq;
  2341. /* Clear Maestro_map */
  2342. for (i = 0; i < 32; i++)
  2343. chip->maestro_map[i] = 0;
  2344. /* Clear Apu Map */
  2345. for (i = 0; i < NR_APUS; i++)
  2346. chip->apu[i] = ESM_APU_FREE;
  2347. /* just to be sure */
  2348. pci_set_master(pci);
  2349. if (do_pm > 1) {
  2350. /* disable power-management if not on the whitelist */
  2351. unsigned short vend;
  2352. pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  2353. for (i = 0; i < (int)ARRAY_SIZE(pm_whitelist); i++) {
  2354. if (chip->type == pm_whitelist[i].type &&
  2355. vend == pm_whitelist[i].vendor) {
  2356. do_pm = 1;
  2357. break;
  2358. }
  2359. }
  2360. if (do_pm > 1) {
  2361. /* not matched; disabling pm */
  2362. printk(KERN_INFO "es1968: not attempting power management.\n");
  2363. do_pm = 0;
  2364. }
  2365. }
  2366. chip->do_pm = do_pm;
  2367. snd_es1968_chip_init(chip);
  2368. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  2369. snd_es1968_free(chip);
  2370. return err;
  2371. }
  2372. snd_card_set_dev(card, &pci->dev);
  2373. #ifdef CONFIG_SND_ES1968_RADIO
  2374. chip->tea.private_data = chip;
  2375. chip->tea.ops = &snd_es1968_tea_ops;
  2376. strlcpy(chip->tea.card, "SF64-PCE2", sizeof(chip->tea.card));
  2377. sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
  2378. if (!snd_tea575x_init(&chip->tea))
  2379. printk(KERN_INFO "es1968: detected TEA575x radio\n");
  2380. #endif
  2381. *chip_ret = chip;
  2382. return 0;
  2383. }
  2384. /*
  2385. */
  2386. static int __devinit snd_es1968_probe(struct pci_dev *pci,
  2387. const struct pci_device_id *pci_id)
  2388. {
  2389. static int dev;
  2390. struct snd_card *card;
  2391. struct es1968 *chip;
  2392. unsigned int i;
  2393. int err;
  2394. if (dev >= SNDRV_CARDS)
  2395. return -ENODEV;
  2396. if (!enable[dev]) {
  2397. dev++;
  2398. return -ENOENT;
  2399. }
  2400. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  2401. if (err < 0)
  2402. return err;
  2403. if (total_bufsize[dev] < 128)
  2404. total_bufsize[dev] = 128;
  2405. if (total_bufsize[dev] > 4096)
  2406. total_bufsize[dev] = 4096;
  2407. if ((err = snd_es1968_create(card, pci,
  2408. total_bufsize[dev] * 1024, /* in bytes */
  2409. pcm_substreams_p[dev],
  2410. pcm_substreams_c[dev],
  2411. pci_id->driver_data,
  2412. use_pm[dev],
  2413. &chip)) < 0) {
  2414. snd_card_free(card);
  2415. return err;
  2416. }
  2417. card->private_data = chip;
  2418. switch (chip->type) {
  2419. case TYPE_MAESTRO2E:
  2420. strcpy(card->driver, "ES1978");
  2421. strcpy(card->shortname, "ESS ES1978 (Maestro 2E)");
  2422. break;
  2423. case TYPE_MAESTRO2:
  2424. strcpy(card->driver, "ES1968");
  2425. strcpy(card->shortname, "ESS ES1968 (Maestro 2)");
  2426. break;
  2427. case TYPE_MAESTRO:
  2428. strcpy(card->driver, "ESM1");
  2429. strcpy(card->shortname, "ESS Maestro 1");
  2430. break;
  2431. }
  2432. if ((err = snd_es1968_pcm(chip, 0)) < 0) {
  2433. snd_card_free(card);
  2434. return err;
  2435. }
  2436. if ((err = snd_es1968_mixer(chip)) < 0) {
  2437. snd_card_free(card);
  2438. return err;
  2439. }
  2440. if (enable_mpu[dev] == 2) {
  2441. /* check the black list */
  2442. unsigned short vend;
  2443. pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  2444. for (i = 0; i < ARRAY_SIZE(mpu_blacklist); i++) {
  2445. if (chip->type == mpu_blacklist[i].type &&
  2446. vend == mpu_blacklist[i].vendor) {
  2447. enable_mpu[dev] = 0;
  2448. break;
  2449. }
  2450. }
  2451. }
  2452. if (enable_mpu[dev]) {
  2453. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  2454. chip->io_port + ESM_MPU401_PORT,
  2455. MPU401_INFO_INTEGRATED,
  2456. chip->irq, 0, &chip->rmidi)) < 0) {
  2457. printk(KERN_WARNING "es1968: skipping MPU-401 MIDI support..\n");
  2458. }
  2459. }
  2460. snd_es1968_create_gameport(chip, dev);
  2461. #ifdef CONFIG_SND_ES1968_INPUT
  2462. err = snd_es1968_input_register(chip);
  2463. if (err)
  2464. snd_printk(KERN_WARNING "Input device registration "
  2465. "failed with error %i", err);
  2466. #endif
  2467. snd_es1968_start_irq(chip);
  2468. chip->clock = clock[dev];
  2469. if (! chip->clock)
  2470. es1968_measure_clock(chip);
  2471. sprintf(card->longname, "%s at 0x%lx, irq %i",
  2472. card->shortname, chip->io_port, chip->irq);
  2473. if ((err = snd_card_register(card)) < 0) {
  2474. snd_card_free(card);
  2475. return err;
  2476. }
  2477. pci_set_drvdata(pci, card);
  2478. dev++;
  2479. return 0;
  2480. }
  2481. static void __devexit snd_es1968_remove(struct pci_dev *pci)
  2482. {
  2483. snd_card_free(pci_get_drvdata(pci));
  2484. pci_set_drvdata(pci, NULL);
  2485. }
  2486. static struct pci_driver driver = {
  2487. .name = "ES1968 (ESS Maestro)",
  2488. .id_table = snd_es1968_ids,
  2489. .probe = snd_es1968_probe,
  2490. .remove = __devexit_p(snd_es1968_remove),
  2491. #ifdef CONFIG_PM
  2492. .suspend = es1968_suspend,
  2493. .resume = es1968_resume,
  2494. #endif
  2495. };
  2496. static int __init alsa_card_es1968_init(void)
  2497. {
  2498. return pci_register_driver(&driver);
  2499. }
  2500. static void __exit alsa_card_es1968_exit(void)
  2501. {
  2502. pci_unregister_driver(&driver);
  2503. }
  2504. module_init(alsa_card_es1968_init)
  2505. module_exit(alsa_card_es1968_exit)