emupcm.c 56 KB

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