emupcm.c 53 KB

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