emupcm.c 57 KB

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