emupcm.c 56 KB

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