emupcm.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Creative Labs, Inc.
  4. * Routines for control of EMU10K1 chips / PCM routines
  5. * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
  6. *
  7. * BUGS:
  8. * --
  9. *
  10. * TODO:
  11. * --
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <linux/pci.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/time.h>
  32. #include <linux/init.h>
  33. #include <sound/core.h>
  34. #include <sound/emu10k1.h>
  35. static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
  36. struct snd_emu10k1_voice *voice)
  37. {
  38. struct snd_emu10k1_pcm *epcm;
  39. if ((epcm = voice->epcm) == NULL)
  40. return;
  41. if (epcm->substream == NULL)
  42. return;
  43. #if 0
  44. dev_dbg(emu->card->dev,
  45. "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
  46. epcm->substream->runtime->hw->pointer(emu, epcm->substream),
  47. snd_pcm_lib_period_bytes(epcm->substream),
  48. snd_pcm_lib_buffer_bytes(epcm->substream));
  49. #endif
  50. snd_pcm_period_elapsed(epcm->substream);
  51. }
  52. static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
  53. unsigned int status)
  54. {
  55. #if 0
  56. if (status & IPR_ADCBUFHALFFULL) {
  57. if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  58. return;
  59. }
  60. #endif
  61. snd_pcm_period_elapsed(emu->pcm_capture_substream);
  62. }
  63. static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
  64. unsigned int status)
  65. {
  66. #if 0
  67. if (status & IPR_MICBUFHALFFULL) {
  68. if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  69. return;
  70. }
  71. #endif
  72. snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
  73. }
  74. static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
  75. unsigned int status)
  76. {
  77. #if 0
  78. if (status & IPR_EFXBUFHALFFULL) {
  79. if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  80. return;
  81. }
  82. #endif
  83. snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
  84. }
  85. static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
  86. {
  87. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  88. struct snd_pcm_runtime *runtime = substream->runtime;
  89. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  90. unsigned int ptr;
  91. if (!epcm->running)
  92. return 0;
  93. ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
  94. ptr += runtime->buffer_size;
  95. ptr -= epcm->ccca_start_addr;
  96. ptr %= runtime->buffer_size;
  97. return ptr;
  98. }
  99. static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
  100. {
  101. int err, i;
  102. if (epcm->voices[1] != NULL && voices < 2) {
  103. snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
  104. epcm->voices[1] = NULL;
  105. }
  106. for (i = 0; i < voices; i++) {
  107. if (epcm->voices[i] == NULL)
  108. break;
  109. }
  110. if (i == voices)
  111. return 0; /* already allocated */
  112. for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
  113. if (epcm->voices[i]) {
  114. snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
  115. epcm->voices[i] = NULL;
  116. }
  117. }
  118. err = snd_emu10k1_voice_alloc(epcm->emu,
  119. epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
  120. voices,
  121. &epcm->voices[0]);
  122. if (err < 0)
  123. return err;
  124. epcm->voices[0]->epcm = epcm;
  125. if (voices > 1) {
  126. for (i = 1; i < voices; i++) {
  127. epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
  128. epcm->voices[i]->epcm = epcm;
  129. }
  130. }
  131. if (epcm->extra == NULL) {
  132. err = snd_emu10k1_voice_alloc(epcm->emu,
  133. epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
  134. 1,
  135. &epcm->extra);
  136. if (err < 0) {
  137. /*
  138. dev_dbg(emu->card->dev, "pcm_channel_alloc: "
  139. "failed extra: voices=%d, frame=%d\n",
  140. voices, frame);
  141. */
  142. for (i = 0; i < voices; i++) {
  143. snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
  144. epcm->voices[i] = NULL;
  145. }
  146. return err;
  147. }
  148. epcm->extra->epcm = epcm;
  149. epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
  150. }
  151. return 0;
  152. }
  153. static unsigned int capture_period_sizes[31] = {
  154. 384, 448, 512, 640,
  155. 384*2, 448*2, 512*2, 640*2,
  156. 384*4, 448*4, 512*4, 640*4,
  157. 384*8, 448*8, 512*8, 640*8,
  158. 384*16, 448*16, 512*16, 640*16,
  159. 384*32, 448*32, 512*32, 640*32,
  160. 384*64, 448*64, 512*64, 640*64,
  161. 384*128,448*128,512*128
  162. };
  163. static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
  164. .count = 31,
  165. .list = capture_period_sizes,
  166. .mask = 0
  167. };
  168. static unsigned int capture_rates[8] = {
  169. 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
  170. };
  171. static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
  172. .count = 8,
  173. .list = capture_rates,
  174. .mask = 0
  175. };
  176. static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
  177. {
  178. switch (rate) {
  179. case 8000: return ADCCR_SAMPLERATE_8;
  180. case 11025: return ADCCR_SAMPLERATE_11;
  181. case 16000: return ADCCR_SAMPLERATE_16;
  182. case 22050: return ADCCR_SAMPLERATE_22;
  183. case 24000: return ADCCR_SAMPLERATE_24;
  184. case 32000: return ADCCR_SAMPLERATE_32;
  185. case 44100: return ADCCR_SAMPLERATE_44;
  186. case 48000: return ADCCR_SAMPLERATE_48;
  187. default:
  188. snd_BUG();
  189. return ADCCR_SAMPLERATE_8;
  190. }
  191. }
  192. static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
  193. {
  194. switch (rate) {
  195. case 8000: return A_ADCCR_SAMPLERATE_8;
  196. case 11025: return A_ADCCR_SAMPLERATE_11;
  197. case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
  198. case 16000: return ADCCR_SAMPLERATE_16;
  199. case 22050: return ADCCR_SAMPLERATE_22;
  200. case 24000: return ADCCR_SAMPLERATE_24;
  201. case 32000: return ADCCR_SAMPLERATE_32;
  202. case 44100: return ADCCR_SAMPLERATE_44;
  203. case 48000: return ADCCR_SAMPLERATE_48;
  204. default:
  205. snd_BUG();
  206. return A_ADCCR_SAMPLERATE_8;
  207. }
  208. }
  209. static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
  210. {
  211. unsigned int pitch_target;
  212. pitch_target = (rate << 8) / 375;
  213. pitch_target = (pitch_target >> 1) + (pitch_target & 1);
  214. return pitch_target;
  215. }
  216. #define PITCH_48000 0x00004000
  217. #define PITCH_96000 0x00008000
  218. #define PITCH_85000 0x00007155
  219. #define PITCH_80726 0x00006ba2
  220. #define PITCH_67882 0x00005a82
  221. #define PITCH_57081 0x00004c1c
  222. static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
  223. {
  224. if (pitch_target == PITCH_48000)
  225. return CCCA_INTERPROM_0;
  226. else if (pitch_target < PITCH_48000)
  227. return CCCA_INTERPROM_1;
  228. else if (pitch_target >= PITCH_96000)
  229. return CCCA_INTERPROM_0;
  230. else if (pitch_target >= PITCH_85000)
  231. return CCCA_INTERPROM_6;
  232. else if (pitch_target >= PITCH_80726)
  233. return CCCA_INTERPROM_5;
  234. else if (pitch_target >= PITCH_67882)
  235. return CCCA_INTERPROM_4;
  236. else if (pitch_target >= PITCH_57081)
  237. return CCCA_INTERPROM_3;
  238. else
  239. return CCCA_INTERPROM_2;
  240. }
  241. /*
  242. * calculate cache invalidate size
  243. *
  244. * stereo: channel is stereo
  245. * w_16: using 16bit samples
  246. *
  247. * returns: cache invalidate size in samples
  248. */
  249. static inline int emu10k1_ccis(int stereo, int w_16)
  250. {
  251. if (w_16) {
  252. return stereo ? 24 : 26;
  253. } else {
  254. return stereo ? 24*2 : 26*2;
  255. }
  256. }
  257. static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
  258. int master, int extra,
  259. struct snd_emu10k1_voice *evoice,
  260. unsigned int start_addr,
  261. unsigned int end_addr,
  262. struct snd_emu10k1_pcm_mixer *mix)
  263. {
  264. struct snd_pcm_substream *substream = evoice->epcm->substream;
  265. struct snd_pcm_runtime *runtime = substream->runtime;
  266. unsigned int silent_page, tmp;
  267. int voice, stereo, w_16;
  268. unsigned char attn, send_amount[8];
  269. unsigned char send_routing[8];
  270. unsigned long flags;
  271. unsigned int pitch_target;
  272. unsigned int ccis;
  273. voice = evoice->number;
  274. stereo = runtime->channels == 2;
  275. w_16 = snd_pcm_format_width(runtime->format) == 16;
  276. if (!extra && stereo) {
  277. start_addr >>= 1;
  278. end_addr >>= 1;
  279. }
  280. if (w_16) {
  281. start_addr >>= 1;
  282. end_addr >>= 1;
  283. }
  284. spin_lock_irqsave(&emu->reg_lock, flags);
  285. /* volume parameters */
  286. if (extra) {
  287. attn = 0;
  288. memset(send_routing, 0, sizeof(send_routing));
  289. send_routing[0] = 0;
  290. send_routing[1] = 1;
  291. send_routing[2] = 2;
  292. send_routing[3] = 3;
  293. memset(send_amount, 0, sizeof(send_amount));
  294. } else {
  295. /* mono, left, right (master voice = left) */
  296. tmp = stereo ? (master ? 1 : 2) : 0;
  297. memcpy(send_routing, &mix->send_routing[tmp][0], 8);
  298. memcpy(send_amount, &mix->send_volume[tmp][0], 8);
  299. }
  300. ccis = emu10k1_ccis(stereo, w_16);
  301. if (master) {
  302. evoice->epcm->ccca_start_addr = start_addr + ccis;
  303. if (extra) {
  304. start_addr += ccis;
  305. end_addr += ccis + emu->delay_pcm_irq;
  306. }
  307. if (stereo && !extra) {
  308. snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
  309. snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
  310. } else {
  311. snd_emu10k1_ptr_write(emu, CPF, voice, 0);
  312. }
  313. }
  314. /* setup routing */
  315. if (emu->audigy) {
  316. snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
  317. snd_emu10k1_compose_audigy_fxrt1(send_routing));
  318. snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
  319. snd_emu10k1_compose_audigy_fxrt2(send_routing));
  320. snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
  321. ((unsigned int)send_amount[4] << 24) |
  322. ((unsigned int)send_amount[5] << 16) |
  323. ((unsigned int)send_amount[6] << 8) |
  324. (unsigned int)send_amount[7]);
  325. } else
  326. snd_emu10k1_ptr_write(emu, FXRT, voice,
  327. snd_emu10k1_compose_send_routing(send_routing));
  328. /* Stop CA */
  329. /* Assumption that PT is already 0 so no harm overwriting */
  330. snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
  331. snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
  332. snd_emu10k1_ptr_write(emu, PSST, voice,
  333. (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
  334. (send_amount[2] << 24));
  335. if (emu->card_capabilities->emu_model)
  336. pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
  337. else
  338. pitch_target = emu10k1_calc_pitch_target(runtime->rate);
  339. if (extra)
  340. snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
  341. emu10k1_select_interprom(pitch_target) |
  342. (w_16 ? 0 : CCCA_8BITSELECT));
  343. else
  344. snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
  345. emu10k1_select_interprom(pitch_target) |
  346. (w_16 ? 0 : CCCA_8BITSELECT));
  347. /* Clear filter delay memory */
  348. snd_emu10k1_ptr_write(emu, Z1, voice, 0);
  349. snd_emu10k1_ptr_write(emu, Z2, voice, 0);
  350. /* invalidate maps */
  351. silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
  352. snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
  353. snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
  354. /* modulation envelope */
  355. snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
  356. snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
  357. snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
  358. snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
  359. snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
  360. snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
  361. snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
  362. snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
  363. snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
  364. snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
  365. /* volume envelope */
  366. snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
  367. snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
  368. /* filter envelope */
  369. snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
  370. /* pitch envelope */
  371. snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
  372. spin_unlock_irqrestore(&emu->reg_lock, flags);
  373. }
  374. static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
  375. struct snd_pcm_hw_params *hw_params)
  376. {
  377. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  378. struct snd_pcm_runtime *runtime = substream->runtime;
  379. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  380. int err;
  381. if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
  382. return err;
  383. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  384. return err;
  385. if (err > 0) { /* change */
  386. int mapped;
  387. if (epcm->memblk != NULL)
  388. snd_emu10k1_free_pages(emu, epcm->memblk);
  389. epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
  390. epcm->start_addr = 0;
  391. if (! epcm->memblk)
  392. return -ENOMEM;
  393. mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
  394. if (mapped < 0)
  395. return -ENOMEM;
  396. epcm->start_addr = mapped << PAGE_SHIFT;
  397. }
  398. return 0;
  399. }
  400. static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
  401. {
  402. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  403. struct snd_pcm_runtime *runtime = substream->runtime;
  404. struct snd_emu10k1_pcm *epcm;
  405. if (runtime->private_data == NULL)
  406. return 0;
  407. epcm = runtime->private_data;
  408. if (epcm->extra) {
  409. snd_emu10k1_voice_free(epcm->emu, epcm->extra);
  410. epcm->extra = NULL;
  411. }
  412. if (epcm->voices[1]) {
  413. snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
  414. epcm->voices[1] = NULL;
  415. }
  416. if (epcm->voices[0]) {
  417. snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
  418. epcm->voices[0] = NULL;
  419. }
  420. if (epcm->memblk) {
  421. snd_emu10k1_free_pages(emu, epcm->memblk);
  422. epcm->memblk = NULL;
  423. epcm->start_addr = 0;
  424. }
  425. snd_pcm_lib_free_pages(substream);
  426. return 0;
  427. }
  428. static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
  429. {
  430. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  431. struct snd_pcm_runtime *runtime = substream->runtime;
  432. struct snd_emu10k1_pcm *epcm;
  433. int i;
  434. if (runtime->private_data == NULL)
  435. return 0;
  436. epcm = runtime->private_data;
  437. if (epcm->extra) {
  438. snd_emu10k1_voice_free(epcm->emu, epcm->extra);
  439. epcm->extra = NULL;
  440. }
  441. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  442. if (epcm->voices[i]) {
  443. snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
  444. epcm->voices[i] = NULL;
  445. }
  446. }
  447. if (epcm->memblk) {
  448. snd_emu10k1_free_pages(emu, epcm->memblk);
  449. epcm->memblk = NULL;
  450. epcm->start_addr = 0;
  451. }
  452. snd_pcm_lib_free_pages(substream);
  453. return 0;
  454. }
  455. static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
  456. {
  457. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  458. struct snd_pcm_runtime *runtime = substream->runtime;
  459. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  460. unsigned int start_addr, end_addr;
  461. start_addr = epcm->start_addr;
  462. end_addr = snd_pcm_lib_period_bytes(substream);
  463. if (runtime->channels == 2) {
  464. start_addr >>= 1;
  465. end_addr >>= 1;
  466. }
  467. end_addr += start_addr;
  468. snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
  469. start_addr, end_addr, NULL);
  470. start_addr = epcm->start_addr;
  471. end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
  472. snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
  473. start_addr, end_addr,
  474. &emu->pcm_mixer[substream->number]);
  475. if (epcm->voices[1])
  476. snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
  477. start_addr, end_addr,
  478. &emu->pcm_mixer[substream->number]);
  479. return 0;
  480. }
  481. static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
  482. {
  483. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  484. struct snd_pcm_runtime *runtime = substream->runtime;
  485. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  486. unsigned int start_addr, end_addr;
  487. unsigned int channel_size;
  488. int i;
  489. start_addr = epcm->start_addr;
  490. end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
  491. /*
  492. * the kX driver leaves some space between voices
  493. */
  494. channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
  495. snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
  496. start_addr, start_addr + (channel_size / 2), NULL);
  497. /* only difference with the master voice is we use it for the pointer */
  498. snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
  499. start_addr, start_addr + channel_size,
  500. &emu->efx_pcm_mixer[0]);
  501. start_addr += channel_size;
  502. for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
  503. snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
  504. start_addr, start_addr + channel_size,
  505. &emu->efx_pcm_mixer[i]);
  506. start_addr += channel_size;
  507. }
  508. return 0;
  509. }
  510. static struct snd_pcm_hardware snd_emu10k1_efx_playback =
  511. {
  512. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
  513. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  514. SNDRV_PCM_INFO_RESUME |
  515. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  516. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  517. .rates = SNDRV_PCM_RATE_48000,
  518. .rate_min = 48000,
  519. .rate_max = 48000,
  520. .channels_min = NUM_EFX_PLAYBACK,
  521. .channels_max = NUM_EFX_PLAYBACK,
  522. .buffer_bytes_max = (64*1024),
  523. .period_bytes_min = 64,
  524. .period_bytes_max = (64*1024),
  525. .periods_min = 2,
  526. .periods_max = 2,
  527. .fifo_size = 0,
  528. };
  529. static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
  530. struct snd_pcm_hw_params *hw_params)
  531. {
  532. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  533. }
  534. static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
  535. {
  536. return snd_pcm_lib_free_pages(substream);
  537. }
  538. static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
  539. {
  540. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  541. struct snd_pcm_runtime *runtime = substream->runtime;
  542. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  543. int idx;
  544. /* zeroing the buffer size will stop capture */
  545. snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
  546. switch (epcm->type) {
  547. case CAPTURE_AC97ADC:
  548. snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
  549. break;
  550. case CAPTURE_EFX:
  551. if (emu->audigy) {
  552. snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
  553. snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
  554. } else
  555. snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
  556. break;
  557. default:
  558. break;
  559. }
  560. snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
  561. epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
  562. epcm->capture_bs_val = 0;
  563. for (idx = 0; idx < 31; idx++) {
  564. if (capture_period_sizes[idx] == epcm->capture_bufsize) {
  565. epcm->capture_bs_val = idx + 1;
  566. break;
  567. }
  568. }
  569. if (epcm->capture_bs_val == 0) {
  570. snd_BUG();
  571. epcm->capture_bs_val++;
  572. }
  573. if (epcm->type == CAPTURE_AC97ADC) {
  574. epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
  575. if (runtime->channels > 1)
  576. epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
  577. epcm->capture_cr_val |= emu->audigy ?
  578. snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
  579. snd_emu10k1_capture_rate_reg(runtime->rate);
  580. }
  581. return 0;
  582. }
  583. static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
  584. {
  585. struct snd_pcm_runtime *runtime;
  586. unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
  587. if (evoice == NULL)
  588. return;
  589. runtime = evoice->epcm->substream->runtime;
  590. voice = evoice->number;
  591. stereo = (!extra && runtime->channels == 2);
  592. sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
  593. ccis = emu10k1_ccis(stereo, sample == 0);
  594. /* set cs to 2 * number of cache registers beside the invalidated */
  595. cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
  596. if (cs > 16) cs = 16;
  597. for (i = 0; i < cs; i++) {
  598. snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
  599. if (stereo) {
  600. snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
  601. }
  602. }
  603. /* reset cache */
  604. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
  605. snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
  606. if (stereo) {
  607. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
  608. snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
  609. }
  610. /* fill cache */
  611. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
  612. if (stereo) {
  613. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
  614. }
  615. }
  616. static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
  617. int master, int extra,
  618. struct snd_emu10k1_pcm_mixer *mix)
  619. {
  620. struct snd_pcm_substream *substream;
  621. struct snd_pcm_runtime *runtime;
  622. unsigned int attn, vattn;
  623. unsigned int voice, tmp;
  624. if (evoice == NULL) /* skip second voice for mono */
  625. return;
  626. substream = evoice->epcm->substream;
  627. runtime = substream->runtime;
  628. voice = evoice->number;
  629. attn = extra ? 0 : 0x00ff;
  630. tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
  631. vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
  632. snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
  633. snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
  634. snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
  635. snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
  636. snd_emu10k1_voice_clear_loop_stop(emu, voice);
  637. }
  638. static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
  639. {
  640. struct snd_pcm_substream *substream;
  641. struct snd_pcm_runtime *runtime;
  642. unsigned int voice, pitch, pitch_target;
  643. if (evoice == NULL) /* skip second voice for mono */
  644. return;
  645. substream = evoice->epcm->substream;
  646. runtime = substream->runtime;
  647. voice = evoice->number;
  648. pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
  649. if (emu->card_capabilities->emu_model)
  650. pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
  651. else
  652. pitch_target = emu10k1_calc_pitch_target(runtime->rate);
  653. snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
  654. if (master || evoice->epcm->type == PLAYBACK_EFX)
  655. snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
  656. snd_emu10k1_ptr_write(emu, IP, voice, pitch);
  657. if (extra)
  658. snd_emu10k1_voice_intr_enable(emu, voice);
  659. }
  660. static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
  661. {
  662. unsigned int voice;
  663. if (evoice == NULL)
  664. return;
  665. voice = evoice->number;
  666. snd_emu10k1_voice_intr_disable(emu, voice);
  667. snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
  668. snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
  669. snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
  670. snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
  671. snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
  672. snd_emu10k1_ptr_write(emu, IP, voice, 0);
  673. }
  674. static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
  675. struct snd_emu10k1_pcm *epcm,
  676. struct snd_pcm_substream *substream,
  677. struct snd_pcm_runtime *runtime)
  678. {
  679. unsigned int ptr, period_pos;
  680. /* try to sychronize the current position for the interrupt
  681. source voice */
  682. period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
  683. period_pos %= runtime->period_size;
  684. ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
  685. ptr &= ~0x00ffffff;
  686. ptr |= epcm->ccca_start_addr + period_pos;
  687. snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
  688. }
  689. static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
  690. int cmd)
  691. {
  692. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  693. struct snd_pcm_runtime *runtime = substream->runtime;
  694. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  695. struct snd_emu10k1_pcm_mixer *mix;
  696. int result = 0;
  697. /*
  698. dev_dbg(emu->card->dev,
  699. "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
  700. (int)emu, cmd, substream->ops->pointer(substream))
  701. */
  702. spin_lock(&emu->reg_lock);
  703. switch (cmd) {
  704. case SNDRV_PCM_TRIGGER_START:
  705. snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
  706. snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
  707. /* follow thru */
  708. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  709. case SNDRV_PCM_TRIGGER_RESUME:
  710. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
  711. snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
  712. mix = &emu->pcm_mixer[substream->number];
  713. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
  714. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
  715. snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
  716. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
  717. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
  718. snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
  719. epcm->running = 1;
  720. break;
  721. case SNDRV_PCM_TRIGGER_STOP:
  722. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  723. case SNDRV_PCM_TRIGGER_SUSPEND:
  724. epcm->running = 0;
  725. snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
  726. snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
  727. snd_emu10k1_playback_stop_voice(emu, epcm->extra);
  728. break;
  729. default:
  730. result = -EINVAL;
  731. break;
  732. }
  733. spin_unlock(&emu->reg_lock);
  734. return result;
  735. }
  736. static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
  737. int cmd)
  738. {
  739. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  740. struct snd_pcm_runtime *runtime = substream->runtime;
  741. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  742. int result = 0;
  743. spin_lock(&emu->reg_lock);
  744. switch (cmd) {
  745. case SNDRV_PCM_TRIGGER_START:
  746. case SNDRV_PCM_TRIGGER_RESUME:
  747. /* hmm this should cause full and half full interrupt to be raised? */
  748. outl(epcm->capture_ipr, emu->port + IPR);
  749. snd_emu10k1_intr_enable(emu, epcm->capture_inte);
  750. /*
  751. dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
  752. epcm->adccr, epcm->adcbs);
  753. */
  754. switch (epcm->type) {
  755. case CAPTURE_AC97ADC:
  756. snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
  757. break;
  758. case CAPTURE_EFX:
  759. if (emu->audigy) {
  760. snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
  761. snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
  762. dev_dbg(emu->card->dev,
  763. "cr_val=0x%x, cr_val2=0x%x\n",
  764. epcm->capture_cr_val,
  765. epcm->capture_cr_val2);
  766. } else
  767. snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
  768. break;
  769. default:
  770. break;
  771. }
  772. snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
  773. epcm->running = 1;
  774. epcm->first_ptr = 1;
  775. break;
  776. case SNDRV_PCM_TRIGGER_STOP:
  777. case SNDRV_PCM_TRIGGER_SUSPEND:
  778. epcm->running = 0;
  779. snd_emu10k1_intr_disable(emu, epcm->capture_inte);
  780. outl(epcm->capture_ipr, emu->port + IPR);
  781. snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
  782. switch (epcm->type) {
  783. case CAPTURE_AC97ADC:
  784. snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
  785. break;
  786. case CAPTURE_EFX:
  787. if (emu->audigy) {
  788. snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
  789. snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
  790. } else
  791. snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
  792. break;
  793. default:
  794. break;
  795. }
  796. break;
  797. default:
  798. result = -EINVAL;
  799. }
  800. spin_unlock(&emu->reg_lock);
  801. return result;
  802. }
  803. static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
  804. {
  805. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  806. struct snd_pcm_runtime *runtime = substream->runtime;
  807. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  808. unsigned int ptr;
  809. if (!epcm->running)
  810. return 0;
  811. ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
  812. #if 0 /* Perex's code */
  813. ptr += runtime->buffer_size;
  814. ptr -= epcm->ccca_start_addr;
  815. ptr %= runtime->buffer_size;
  816. #else /* EMU10K1 Open Source code from Creative */
  817. if (ptr < epcm->ccca_start_addr)
  818. ptr += runtime->buffer_size - epcm->ccca_start_addr;
  819. else {
  820. ptr -= epcm->ccca_start_addr;
  821. if (ptr >= runtime->buffer_size)
  822. ptr -= runtime->buffer_size;
  823. }
  824. #endif
  825. /*
  826. dev_dbg(emu->card->dev,
  827. "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
  828. (long)ptr, (long)runtime->buffer_size,
  829. (long)runtime->period_size);
  830. */
  831. return ptr;
  832. }
  833. static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
  834. int cmd)
  835. {
  836. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  837. struct snd_pcm_runtime *runtime = substream->runtime;
  838. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  839. int i;
  840. int result = 0;
  841. spin_lock(&emu->reg_lock);
  842. switch (cmd) {
  843. case SNDRV_PCM_TRIGGER_START:
  844. /* prepare voices */
  845. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  846. snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
  847. }
  848. snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
  849. /* follow thru */
  850. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  851. case SNDRV_PCM_TRIGGER_RESUME:
  852. snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
  853. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
  854. &emu->efx_pcm_mixer[0]);
  855. for (i = 1; i < NUM_EFX_PLAYBACK; i++)
  856. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
  857. &emu->efx_pcm_mixer[i]);
  858. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
  859. snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
  860. for (i = 1; i < NUM_EFX_PLAYBACK; i++)
  861. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
  862. epcm->running = 1;
  863. break;
  864. case SNDRV_PCM_TRIGGER_SUSPEND:
  865. case SNDRV_PCM_TRIGGER_STOP:
  866. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  867. epcm->running = 0;
  868. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  869. snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
  870. }
  871. snd_emu10k1_playback_stop_voice(emu, epcm->extra);
  872. break;
  873. default:
  874. result = -EINVAL;
  875. break;
  876. }
  877. spin_unlock(&emu->reg_lock);
  878. return result;
  879. }
  880. static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
  881. {
  882. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  883. struct snd_pcm_runtime *runtime = substream->runtime;
  884. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  885. unsigned int ptr;
  886. if (!epcm->running)
  887. return 0;
  888. if (epcm->first_ptr) {
  889. udelay(50); /* hack, it takes awhile until capture is started */
  890. epcm->first_ptr = 0;
  891. }
  892. ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
  893. return bytes_to_frames(runtime, ptr);
  894. }
  895. /*
  896. * Playback support device description
  897. */
  898. static struct snd_pcm_hardware snd_emu10k1_playback =
  899. {
  900. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  901. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  902. SNDRV_PCM_INFO_RESUME |
  903. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  904. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  905. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
  906. .rate_min = 4000,
  907. .rate_max = 96000,
  908. .channels_min = 1,
  909. .channels_max = 2,
  910. .buffer_bytes_max = (128*1024),
  911. .period_bytes_min = 64,
  912. .period_bytes_max = (128*1024),
  913. .periods_min = 1,
  914. .periods_max = 1024,
  915. .fifo_size = 0,
  916. };
  917. /*
  918. * Capture support device description
  919. */
  920. static struct snd_pcm_hardware snd_emu10k1_capture =
  921. {
  922. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  923. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  924. SNDRV_PCM_INFO_RESUME |
  925. SNDRV_PCM_INFO_MMAP_VALID),
  926. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  927. .rates = SNDRV_PCM_RATE_8000_48000,
  928. .rate_min = 8000,
  929. .rate_max = 48000,
  930. .channels_min = 1,
  931. .channels_max = 2,
  932. .buffer_bytes_max = (64*1024),
  933. .period_bytes_min = 384,
  934. .period_bytes_max = (64*1024),
  935. .periods_min = 2,
  936. .periods_max = 2,
  937. .fifo_size = 0,
  938. };
  939. static struct snd_pcm_hardware snd_emu10k1_capture_efx =
  940. {
  941. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  942. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  943. SNDRV_PCM_INFO_RESUME |
  944. SNDRV_PCM_INFO_MMAP_VALID),
  945. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  946. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  947. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  948. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
  949. .rate_min = 44100,
  950. .rate_max = 192000,
  951. .channels_min = 8,
  952. .channels_max = 8,
  953. .buffer_bytes_max = (64*1024),
  954. .period_bytes_min = 384,
  955. .period_bytes_max = (64*1024),
  956. .periods_min = 2,
  957. .periods_max = 2,
  958. .fifo_size = 0,
  959. };
  960. /*
  961. *
  962. */
  963. static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
  964. {
  965. struct snd_ctl_elem_id id;
  966. if (! kctl)
  967. return;
  968. if (activate)
  969. kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  970. else
  971. kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  972. snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
  973. SNDRV_CTL_EVENT_MASK_INFO,
  974. snd_ctl_build_ioff(&id, kctl, idx));
  975. }
  976. static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
  977. {
  978. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
  979. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
  980. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
  981. }
  982. static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
  983. {
  984. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
  985. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
  986. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
  987. }
  988. static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
  989. {
  990. kfree(runtime->private_data);
  991. }
  992. static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
  993. {
  994. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  995. struct snd_emu10k1_pcm_mixer *mix;
  996. int i;
  997. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  998. mix = &emu->efx_pcm_mixer[i];
  999. mix->epcm = NULL;
  1000. snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
  1001. }
  1002. return 0;
  1003. }
  1004. static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
  1005. {
  1006. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1007. struct snd_emu10k1_pcm *epcm;
  1008. struct snd_emu10k1_pcm_mixer *mix;
  1009. struct snd_pcm_runtime *runtime = substream->runtime;
  1010. int i;
  1011. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1012. if (epcm == NULL)
  1013. return -ENOMEM;
  1014. epcm->emu = emu;
  1015. epcm->type = PLAYBACK_EFX;
  1016. epcm->substream = substream;
  1017. emu->pcm_playback_efx_substream = substream;
  1018. runtime->private_data = epcm;
  1019. runtime->private_free = snd_emu10k1_pcm_free_substream;
  1020. runtime->hw = snd_emu10k1_efx_playback;
  1021. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  1022. mix = &emu->efx_pcm_mixer[i];
  1023. mix->send_routing[0][0] = i;
  1024. memset(&mix->send_volume, 0, sizeof(mix->send_volume));
  1025. mix->send_volume[0][0] = 255;
  1026. mix->attn[0] = 0xffff;
  1027. mix->epcm = epcm;
  1028. snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
  1029. }
  1030. return 0;
  1031. }
  1032. static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
  1033. {
  1034. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1035. struct snd_emu10k1_pcm *epcm;
  1036. struct snd_emu10k1_pcm_mixer *mix;
  1037. struct snd_pcm_runtime *runtime = substream->runtime;
  1038. int i, err, sample_rate;
  1039. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1040. if (epcm == NULL)
  1041. return -ENOMEM;
  1042. epcm->emu = emu;
  1043. epcm->type = PLAYBACK_EMUVOICE;
  1044. epcm->substream = substream;
  1045. runtime->private_data = epcm;
  1046. runtime->private_free = snd_emu10k1_pcm_free_substream;
  1047. runtime->hw = snd_emu10k1_playback;
  1048. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
  1049. kfree(epcm);
  1050. return err;
  1051. }
  1052. if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
  1053. kfree(epcm);
  1054. return err;
  1055. }
  1056. if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
  1057. sample_rate = 44100;
  1058. else
  1059. sample_rate = 48000;
  1060. err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
  1061. if (err < 0) {
  1062. kfree(epcm);
  1063. return err;
  1064. }
  1065. mix = &emu->pcm_mixer[substream->number];
  1066. for (i = 0; i < 4; i++)
  1067. mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
  1068. memset(&mix->send_volume, 0, sizeof(mix->send_volume));
  1069. mix->send_volume[0][0] = mix->send_volume[0][1] =
  1070. mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
  1071. mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
  1072. mix->epcm = epcm;
  1073. snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
  1074. return 0;
  1075. }
  1076. static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
  1077. {
  1078. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1079. struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
  1080. mix->epcm = NULL;
  1081. snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
  1082. return 0;
  1083. }
  1084. static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
  1085. {
  1086. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1087. struct snd_pcm_runtime *runtime = substream->runtime;
  1088. struct snd_emu10k1_pcm *epcm;
  1089. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1090. if (epcm == NULL)
  1091. return -ENOMEM;
  1092. epcm->emu = emu;
  1093. epcm->type = CAPTURE_AC97ADC;
  1094. epcm->substream = substream;
  1095. epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
  1096. epcm->capture_inte = INTE_ADCBUFENABLE;
  1097. epcm->capture_ba_reg = ADCBA;
  1098. epcm->capture_bs_reg = ADCBS;
  1099. epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
  1100. runtime->private_data = epcm;
  1101. runtime->private_free = snd_emu10k1_pcm_free_substream;
  1102. runtime->hw = snd_emu10k1_capture;
  1103. emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
  1104. emu->pcm_capture_substream = substream;
  1105. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
  1106. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
  1107. return 0;
  1108. }
  1109. static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
  1110. {
  1111. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1112. emu->capture_interrupt = NULL;
  1113. emu->pcm_capture_substream = NULL;
  1114. return 0;
  1115. }
  1116. static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
  1117. {
  1118. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1119. struct snd_emu10k1_pcm *epcm;
  1120. struct snd_pcm_runtime *runtime = substream->runtime;
  1121. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1122. if (epcm == NULL)
  1123. return -ENOMEM;
  1124. epcm->emu = emu;
  1125. epcm->type = CAPTURE_AC97MIC;
  1126. epcm->substream = substream;
  1127. epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
  1128. epcm->capture_inte = INTE_MICBUFENABLE;
  1129. epcm->capture_ba_reg = MICBA;
  1130. epcm->capture_bs_reg = MICBS;
  1131. epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
  1132. substream->runtime->private_data = epcm;
  1133. substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
  1134. runtime->hw = snd_emu10k1_capture;
  1135. runtime->hw.rates = SNDRV_PCM_RATE_8000;
  1136. runtime->hw.rate_min = runtime->hw.rate_max = 8000;
  1137. runtime->hw.channels_min = 1;
  1138. emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
  1139. emu->pcm_capture_mic_substream = substream;
  1140. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
  1141. return 0;
  1142. }
  1143. static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
  1144. {
  1145. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1146. emu->capture_interrupt = NULL;
  1147. emu->pcm_capture_mic_substream = NULL;
  1148. return 0;
  1149. }
  1150. static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
  1151. {
  1152. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1153. struct snd_emu10k1_pcm *epcm;
  1154. struct snd_pcm_runtime *runtime = substream->runtime;
  1155. int nefx = emu->audigy ? 64 : 32;
  1156. int idx;
  1157. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1158. if (epcm == NULL)
  1159. return -ENOMEM;
  1160. epcm->emu = emu;
  1161. epcm->type = CAPTURE_EFX;
  1162. epcm->substream = substream;
  1163. epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
  1164. epcm->capture_inte = INTE_EFXBUFENABLE;
  1165. epcm->capture_ba_reg = FXBA;
  1166. epcm->capture_bs_reg = FXBS;
  1167. epcm->capture_idx_reg = FXIDX;
  1168. substream->runtime->private_data = epcm;
  1169. substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
  1170. runtime->hw = snd_emu10k1_capture_efx;
  1171. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  1172. runtime->hw.rate_min = runtime->hw.rate_max = 48000;
  1173. spin_lock_irq(&emu->reg_lock);
  1174. if (emu->card_capabilities->emu_model) {
  1175. /* Nb. of channels has been increased to 16 */
  1176. /* TODO
  1177. * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
  1178. * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  1179. * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  1180. * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
  1181. * rate_min = 44100,
  1182. * rate_max = 192000,
  1183. * channels_min = 16,
  1184. * channels_max = 16,
  1185. * Need to add mixer control to fix sample rate
  1186. *
  1187. * There are 32 mono channels of 16bits each.
  1188. * 24bit Audio uses 2x channels over 16bit
  1189. * 96kHz uses 2x channels over 48kHz
  1190. * 192kHz uses 4x channels over 48kHz
  1191. * So, for 48kHz 24bit, one has 16 channels
  1192. * for 96kHz 24bit, one has 8 channels
  1193. * for 192kHz 24bit, one has 4 channels
  1194. *
  1195. */
  1196. #if 1
  1197. switch (emu->emu1010.internal_clock) {
  1198. case 0:
  1199. /* For 44.1kHz */
  1200. runtime->hw.rates = SNDRV_PCM_RATE_44100;
  1201. runtime->hw.rate_min = runtime->hw.rate_max = 44100;
  1202. runtime->hw.channels_min =
  1203. runtime->hw.channels_max = 16;
  1204. break;
  1205. case 1:
  1206. /* For 48kHz */
  1207. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  1208. runtime->hw.rate_min = runtime->hw.rate_max = 48000;
  1209. runtime->hw.channels_min =
  1210. runtime->hw.channels_max = 16;
  1211. break;
  1212. }
  1213. #endif
  1214. #if 0
  1215. /* For 96kHz */
  1216. runtime->hw.rates = SNDRV_PCM_RATE_96000;
  1217. runtime->hw.rate_min = runtime->hw.rate_max = 96000;
  1218. runtime->hw.channels_min = runtime->hw.channels_max = 4;
  1219. #endif
  1220. #if 0
  1221. /* For 192kHz */
  1222. runtime->hw.rates = SNDRV_PCM_RATE_192000;
  1223. runtime->hw.rate_min = runtime->hw.rate_max = 192000;
  1224. runtime->hw.channels_min = runtime->hw.channels_max = 2;
  1225. #endif
  1226. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
  1227. /* efx_voices_mask[0] is expected to be zero
  1228. * efx_voices_mask[1] is expected to have 32bits set
  1229. */
  1230. } else {
  1231. runtime->hw.channels_min = runtime->hw.channels_max = 0;
  1232. for (idx = 0; idx < nefx; idx++) {
  1233. if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
  1234. runtime->hw.channels_min++;
  1235. runtime->hw.channels_max++;
  1236. }
  1237. }
  1238. }
  1239. epcm->capture_cr_val = emu->efx_voices_mask[0];
  1240. epcm->capture_cr_val2 = emu->efx_voices_mask[1];
  1241. spin_unlock_irq(&emu->reg_lock);
  1242. emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
  1243. emu->pcm_capture_efx_substream = substream;
  1244. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
  1245. return 0;
  1246. }
  1247. static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
  1248. {
  1249. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1250. emu->capture_interrupt = NULL;
  1251. emu->pcm_capture_efx_substream = NULL;
  1252. return 0;
  1253. }
  1254. static struct snd_pcm_ops snd_emu10k1_playback_ops = {
  1255. .open = snd_emu10k1_playback_open,
  1256. .close = snd_emu10k1_playback_close,
  1257. .ioctl = snd_pcm_lib_ioctl,
  1258. .hw_params = snd_emu10k1_playback_hw_params,
  1259. .hw_free = snd_emu10k1_playback_hw_free,
  1260. .prepare = snd_emu10k1_playback_prepare,
  1261. .trigger = snd_emu10k1_playback_trigger,
  1262. .pointer = snd_emu10k1_playback_pointer,
  1263. .page = snd_pcm_sgbuf_ops_page,
  1264. };
  1265. static struct snd_pcm_ops snd_emu10k1_capture_ops = {
  1266. .open = snd_emu10k1_capture_open,
  1267. .close = snd_emu10k1_capture_close,
  1268. .ioctl = snd_pcm_lib_ioctl,
  1269. .hw_params = snd_emu10k1_capture_hw_params,
  1270. .hw_free = snd_emu10k1_capture_hw_free,
  1271. .prepare = snd_emu10k1_capture_prepare,
  1272. .trigger = snd_emu10k1_capture_trigger,
  1273. .pointer = snd_emu10k1_capture_pointer,
  1274. };
  1275. /* EFX playback */
  1276. static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
  1277. .open = snd_emu10k1_efx_playback_open,
  1278. .close = snd_emu10k1_efx_playback_close,
  1279. .ioctl = snd_pcm_lib_ioctl,
  1280. .hw_params = snd_emu10k1_playback_hw_params,
  1281. .hw_free = snd_emu10k1_efx_playback_hw_free,
  1282. .prepare = snd_emu10k1_efx_playback_prepare,
  1283. .trigger = snd_emu10k1_efx_playback_trigger,
  1284. .pointer = snd_emu10k1_efx_playback_pointer,
  1285. .page = snd_pcm_sgbuf_ops_page,
  1286. };
  1287. int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device, struct snd_pcm **rpcm)
  1288. {
  1289. struct snd_pcm *pcm;
  1290. struct snd_pcm_substream *substream;
  1291. int err;
  1292. if (rpcm)
  1293. *rpcm = NULL;
  1294. if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
  1295. return err;
  1296. pcm->private_data = emu;
  1297. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
  1298. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
  1299. pcm->info_flags = 0;
  1300. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1301. strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
  1302. emu->pcm = pcm;
  1303. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1304. if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
  1305. return err;
  1306. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
  1307. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
  1308. if (rpcm)
  1309. *rpcm = pcm;
  1310. return 0;
  1311. }
  1312. int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device,
  1313. struct snd_pcm **rpcm)
  1314. {
  1315. struct snd_pcm *pcm;
  1316. struct snd_pcm_substream *substream;
  1317. int err;
  1318. if (rpcm)
  1319. *rpcm = NULL;
  1320. if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
  1321. return err;
  1322. pcm->private_data = emu;
  1323. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
  1324. pcm->info_flags = 0;
  1325. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1326. strcpy(pcm->name, "Multichannel Playback");
  1327. emu->pcm_multi = pcm;
  1328. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1329. if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
  1330. return err;
  1331. if (rpcm)
  1332. *rpcm = pcm;
  1333. return 0;
  1334. }
  1335. static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
  1336. .open = snd_emu10k1_capture_mic_open,
  1337. .close = snd_emu10k1_capture_mic_close,
  1338. .ioctl = snd_pcm_lib_ioctl,
  1339. .hw_params = snd_emu10k1_capture_hw_params,
  1340. .hw_free = snd_emu10k1_capture_hw_free,
  1341. .prepare = snd_emu10k1_capture_prepare,
  1342. .trigger = snd_emu10k1_capture_trigger,
  1343. .pointer = snd_emu10k1_capture_pointer,
  1344. };
  1345. int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device,
  1346. struct snd_pcm **rpcm)
  1347. {
  1348. struct snd_pcm *pcm;
  1349. int err;
  1350. if (rpcm)
  1351. *rpcm = NULL;
  1352. if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
  1353. return err;
  1354. pcm->private_data = emu;
  1355. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
  1356. pcm->info_flags = 0;
  1357. strcpy(pcm->name, "Mic Capture");
  1358. emu->pcm_mic = pcm;
  1359. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
  1360. if (rpcm)
  1361. *rpcm = pcm;
  1362. return 0;
  1363. }
  1364. static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1365. {
  1366. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  1367. int nefx = emu->audigy ? 64 : 32;
  1368. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1369. uinfo->count = nefx;
  1370. uinfo->value.integer.min = 0;
  1371. uinfo->value.integer.max = 1;
  1372. return 0;
  1373. }
  1374. static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1375. {
  1376. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  1377. int nefx = emu->audigy ? 64 : 32;
  1378. int idx;
  1379. spin_lock_irq(&emu->reg_lock);
  1380. for (idx = 0; idx < nefx; idx++)
  1381. ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
  1382. spin_unlock_irq(&emu->reg_lock);
  1383. return 0;
  1384. }
  1385. static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1386. {
  1387. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  1388. unsigned int nval[2], bits;
  1389. int nefx = emu->audigy ? 64 : 32;
  1390. int nefxb = emu->audigy ? 7 : 6;
  1391. int change, idx;
  1392. nval[0] = nval[1] = 0;
  1393. for (idx = 0, bits = 0; idx < nefx; idx++)
  1394. if (ucontrol->value.integer.value[idx]) {
  1395. nval[idx / 32] |= 1 << (idx % 32);
  1396. bits++;
  1397. }
  1398. for (idx = 0; idx < nefxb; idx++)
  1399. if (1 << idx == bits)
  1400. break;
  1401. if (idx >= nefxb)
  1402. return -EINVAL;
  1403. spin_lock_irq(&emu->reg_lock);
  1404. change = (nval[0] != emu->efx_voices_mask[0]) ||
  1405. (nval[1] != emu->efx_voices_mask[1]);
  1406. emu->efx_voices_mask[0] = nval[0];
  1407. emu->efx_voices_mask[1] = nval[1];
  1408. spin_unlock_irq(&emu->reg_lock);
  1409. return change;
  1410. }
  1411. static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
  1412. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1413. .name = "Captured FX8010 Outputs",
  1414. .info = snd_emu10k1_pcm_efx_voices_mask_info,
  1415. .get = snd_emu10k1_pcm_efx_voices_mask_get,
  1416. .put = snd_emu10k1_pcm_efx_voices_mask_put
  1417. };
  1418. static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
  1419. .open = snd_emu10k1_capture_efx_open,
  1420. .close = snd_emu10k1_capture_efx_close,
  1421. .ioctl = snd_pcm_lib_ioctl,
  1422. .hw_params = snd_emu10k1_capture_hw_params,
  1423. .hw_free = snd_emu10k1_capture_hw_free,
  1424. .prepare = snd_emu10k1_capture_prepare,
  1425. .trigger = snd_emu10k1_capture_trigger,
  1426. .pointer = snd_emu10k1_capture_pointer,
  1427. };
  1428. /* EFX playback */
  1429. #define INITIAL_TRAM_SHIFT 14
  1430. #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
  1431. static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
  1432. {
  1433. struct snd_pcm_substream *substream = private_data;
  1434. snd_pcm_period_elapsed(substream);
  1435. }
  1436. static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
  1437. unsigned short *dst_right,
  1438. unsigned short *src,
  1439. unsigned int count,
  1440. unsigned int tram_shift)
  1441. {
  1442. /*
  1443. dev_dbg(emu->card->dev,
  1444. "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
  1445. "src = 0x%p, count = 0x%x\n",
  1446. dst_left, dst_right, src, count);
  1447. */
  1448. if ((tram_shift & 1) == 0) {
  1449. while (count--) {
  1450. *dst_left-- = *src++;
  1451. *dst_right-- = *src++;
  1452. }
  1453. } else {
  1454. while (count--) {
  1455. *dst_right-- = *src++;
  1456. *dst_left-- = *src++;
  1457. }
  1458. }
  1459. }
  1460. static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
  1461. struct snd_pcm_indirect *rec, size_t bytes)
  1462. {
  1463. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1464. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1465. unsigned int tram_size = pcm->buffer_size;
  1466. unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
  1467. unsigned int frames = bytes >> 2, count;
  1468. unsigned int tram_pos = pcm->tram_pos;
  1469. unsigned int tram_shift = pcm->tram_shift;
  1470. while (frames > tram_pos) {
  1471. count = tram_pos + 1;
  1472. snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
  1473. (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
  1474. src, count, tram_shift);
  1475. src += count * 2;
  1476. frames -= count;
  1477. tram_pos = (tram_size / 2) - 1;
  1478. tram_shift++;
  1479. }
  1480. snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
  1481. (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
  1482. src, frames, tram_shift);
  1483. tram_pos -= frames;
  1484. pcm->tram_pos = tram_pos;
  1485. pcm->tram_shift = tram_shift;
  1486. }
  1487. static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
  1488. {
  1489. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1490. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1491. snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
  1492. return 0;
  1493. }
  1494. static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
  1495. struct snd_pcm_hw_params *hw_params)
  1496. {
  1497. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  1498. }
  1499. static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
  1500. {
  1501. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1502. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1503. unsigned int i;
  1504. for (i = 0; i < pcm->channels; i++)
  1505. snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
  1506. snd_pcm_lib_free_pages(substream);
  1507. return 0;
  1508. }
  1509. static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
  1510. {
  1511. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1512. struct snd_pcm_runtime *runtime = substream->runtime;
  1513. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1514. unsigned int i;
  1515. /*
  1516. dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
  1517. "buffer_size = 0x%x (0x%x)\n",
  1518. emu->fx8010.etram_pages, runtime->dma_area,
  1519. runtime->buffer_size, runtime->buffer_size << 2);
  1520. */
  1521. memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
  1522. pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
  1523. pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  1524. pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
  1525. pcm->tram_shift = 0;
  1526. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
  1527. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
  1528. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
  1529. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
  1530. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
  1531. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
  1532. for (i = 0; i < pcm->channels; i++)
  1533. snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
  1534. return 0;
  1535. }
  1536. static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  1537. {
  1538. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1539. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1540. int result = 0;
  1541. spin_lock(&emu->reg_lock);
  1542. switch (cmd) {
  1543. case SNDRV_PCM_TRIGGER_START:
  1544. /* follow thru */
  1545. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1546. case SNDRV_PCM_TRIGGER_RESUME:
  1547. #ifdef EMU10K1_SET_AC3_IEC958
  1548. {
  1549. int i;
  1550. for (i = 0; i < 3; i++) {
  1551. unsigned int bits;
  1552. bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1553. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
  1554. 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
  1555. snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
  1556. }
  1557. }
  1558. #endif
  1559. result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
  1560. if (result < 0)
  1561. goto __err;
  1562. snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
  1563. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
  1564. break;
  1565. case SNDRV_PCM_TRIGGER_STOP:
  1566. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1567. case SNDRV_PCM_TRIGGER_SUSPEND:
  1568. snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
  1569. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
  1570. pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
  1571. pcm->tram_shift = 0;
  1572. break;
  1573. default:
  1574. result = -EINVAL;
  1575. break;
  1576. }
  1577. __err:
  1578. spin_unlock(&emu->reg_lock);
  1579. return result;
  1580. }
  1581. static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
  1582. {
  1583. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1584. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1585. size_t ptr; /* byte pointer */
  1586. if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
  1587. return 0;
  1588. ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
  1589. return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
  1590. }
  1591. static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
  1592. {
  1593. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1594. SNDRV_PCM_INFO_RESUME |
  1595. /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
  1596. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1597. .rates = SNDRV_PCM_RATE_48000,
  1598. .rate_min = 48000,
  1599. .rate_max = 48000,
  1600. .channels_min = 1,
  1601. .channels_max = 1,
  1602. .buffer_bytes_max = (128*1024),
  1603. .period_bytes_min = 1024,
  1604. .period_bytes_max = (128*1024),
  1605. .periods_min = 2,
  1606. .periods_max = 1024,
  1607. .fifo_size = 0,
  1608. };
  1609. static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
  1610. {
  1611. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1612. struct snd_pcm_runtime *runtime = substream->runtime;
  1613. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1614. runtime->hw = snd_emu10k1_fx8010_playback;
  1615. runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
  1616. runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
  1617. spin_lock_irq(&emu->reg_lock);
  1618. if (pcm->valid == 0) {
  1619. spin_unlock_irq(&emu->reg_lock);
  1620. return -ENODEV;
  1621. }
  1622. pcm->opened = 1;
  1623. spin_unlock_irq(&emu->reg_lock);
  1624. return 0;
  1625. }
  1626. static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
  1627. {
  1628. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1629. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1630. spin_lock_irq(&emu->reg_lock);
  1631. pcm->opened = 0;
  1632. spin_unlock_irq(&emu->reg_lock);
  1633. return 0;
  1634. }
  1635. static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
  1636. .open = snd_emu10k1_fx8010_playback_open,
  1637. .close = snd_emu10k1_fx8010_playback_close,
  1638. .ioctl = snd_pcm_lib_ioctl,
  1639. .hw_params = snd_emu10k1_fx8010_playback_hw_params,
  1640. .hw_free = snd_emu10k1_fx8010_playback_hw_free,
  1641. .prepare = snd_emu10k1_fx8010_playback_prepare,
  1642. .trigger = snd_emu10k1_fx8010_playback_trigger,
  1643. .pointer = snd_emu10k1_fx8010_playback_pointer,
  1644. .ack = snd_emu10k1_fx8010_playback_transfer,
  1645. };
  1646. int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device,
  1647. struct snd_pcm **rpcm)
  1648. {
  1649. struct snd_pcm *pcm;
  1650. struct snd_kcontrol *kctl;
  1651. int err;
  1652. if (rpcm)
  1653. *rpcm = NULL;
  1654. if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
  1655. return err;
  1656. pcm->private_data = emu;
  1657. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
  1658. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
  1659. pcm->info_flags = 0;
  1660. strcpy(pcm->name, "Multichannel Capture/PT Playback");
  1661. emu->pcm_efx = pcm;
  1662. if (rpcm)
  1663. *rpcm = pcm;
  1664. /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
  1665. * to these
  1666. */
  1667. /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
  1668. if (emu->audigy) {
  1669. emu->efx_voices_mask[0] = 0;
  1670. if (emu->card_capabilities->emu_model)
  1671. /* Pavel Hofman - 32 voices will be used for
  1672. * capture (write mode) -
  1673. * each bit = corresponding voice
  1674. */
  1675. emu->efx_voices_mask[1] = 0xffffffff;
  1676. else
  1677. emu->efx_voices_mask[1] = 0xffff;
  1678. } else {
  1679. emu->efx_voices_mask[0] = 0xffff0000;
  1680. emu->efx_voices_mask[1] = 0;
  1681. }
  1682. /* For emu1010, the control has to set 32 upper bits (voices)
  1683. * out of the 64 bits (voices) to true for the 16-channels capture
  1684. * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
  1685. * is already defined but the snd_emu10k1_pcm_efx_voices_mask
  1686. * control can override this register's value.
  1687. */
  1688. kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
  1689. if (!kctl)
  1690. return -ENOMEM;
  1691. kctl->id.device = device;
  1692. snd_ctl_add(emu->card, kctl);
  1693. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
  1694. return 0;
  1695. }