es1968.c 78 KB

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