emupcm.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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_RESUME |
  506. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  507. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  508. .rates = SNDRV_PCM_RATE_48000,
  509. .rate_min = 48000,
  510. .rate_max = 48000,
  511. .channels_min = NUM_EFX_PLAYBACK,
  512. .channels_max = NUM_EFX_PLAYBACK,
  513. .buffer_bytes_max = (64*1024),
  514. .period_bytes_min = 64,
  515. .period_bytes_max = (64*1024),
  516. .periods_min = 2,
  517. .periods_max = 2,
  518. .fifo_size = 0,
  519. };
  520. static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
  521. struct snd_pcm_hw_params *hw_params)
  522. {
  523. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  524. }
  525. static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
  526. {
  527. return snd_pcm_lib_free_pages(substream);
  528. }
  529. static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
  530. {
  531. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  532. struct snd_pcm_runtime *runtime = substream->runtime;
  533. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  534. int idx;
  535. /* zeroing the buffer size will stop capture */
  536. snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
  537. switch (epcm->type) {
  538. case CAPTURE_AC97ADC:
  539. snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
  540. break;
  541. case CAPTURE_EFX:
  542. if (emu->audigy) {
  543. snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
  544. snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
  545. } else
  546. snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
  547. break;
  548. default:
  549. break;
  550. }
  551. snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
  552. epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
  553. epcm->capture_bs_val = 0;
  554. for (idx = 0; idx < 31; idx++) {
  555. if (capture_period_sizes[idx] == epcm->capture_bufsize) {
  556. epcm->capture_bs_val = idx + 1;
  557. break;
  558. }
  559. }
  560. if (epcm->capture_bs_val == 0) {
  561. snd_BUG();
  562. epcm->capture_bs_val++;
  563. }
  564. if (epcm->type == CAPTURE_AC97ADC) {
  565. epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
  566. if (runtime->channels > 1)
  567. epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
  568. epcm->capture_cr_val |= emu->audigy ?
  569. snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
  570. snd_emu10k1_capture_rate_reg(runtime->rate);
  571. }
  572. return 0;
  573. }
  574. static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
  575. {
  576. struct snd_pcm_runtime *runtime;
  577. unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
  578. if (evoice == NULL)
  579. return;
  580. runtime = evoice->epcm->substream->runtime;
  581. voice = evoice->number;
  582. stereo = (!extra && runtime->channels == 2);
  583. sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
  584. ccis = emu10k1_ccis(stereo, sample == 0);
  585. // set cs to 2 * number of cache registers beside the invalidated
  586. cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
  587. if (cs > 16) cs = 16;
  588. for (i = 0; i < cs; i++) {
  589. snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
  590. if (stereo) {
  591. snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
  592. }
  593. }
  594. // reset cache
  595. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
  596. snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
  597. if (stereo) {
  598. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
  599. snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
  600. }
  601. // fill cache
  602. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
  603. if (stereo) {
  604. snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
  605. }
  606. }
  607. static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
  608. int master, int extra,
  609. struct snd_emu10k1_pcm_mixer *mix)
  610. {
  611. struct snd_pcm_substream *substream;
  612. struct snd_pcm_runtime *runtime;
  613. unsigned int attn, vattn;
  614. unsigned int voice, tmp;
  615. if (evoice == NULL) /* skip second voice for mono */
  616. return;
  617. substream = evoice->epcm->substream;
  618. runtime = substream->runtime;
  619. voice = evoice->number;
  620. attn = extra ? 0 : 0x00ff;
  621. tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
  622. vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
  623. snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
  624. snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
  625. snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
  626. snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
  627. snd_emu10k1_voice_clear_loop_stop(emu, voice);
  628. }
  629. static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
  630. {
  631. struct snd_pcm_substream *substream;
  632. struct snd_pcm_runtime *runtime;
  633. unsigned int voice, pitch, pitch_target;
  634. if (evoice == NULL) /* skip second voice for mono */
  635. return;
  636. substream = evoice->epcm->substream;
  637. runtime = substream->runtime;
  638. voice = evoice->number;
  639. pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
  640. pitch_target = emu10k1_calc_pitch_target(runtime->rate);
  641. snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
  642. if (master || evoice->epcm->type == PLAYBACK_EFX)
  643. snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
  644. snd_emu10k1_ptr_write(emu, IP, voice, pitch);
  645. if (extra)
  646. snd_emu10k1_voice_intr_enable(emu, voice);
  647. }
  648. static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
  649. {
  650. unsigned int voice;
  651. if (evoice == NULL)
  652. return;
  653. voice = evoice->number;
  654. snd_emu10k1_voice_intr_disable(emu, voice);
  655. snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
  656. snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
  657. snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
  658. snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
  659. snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
  660. snd_emu10k1_ptr_write(emu, IP, voice, 0);
  661. }
  662. static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
  663. int cmd)
  664. {
  665. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  666. struct snd_pcm_runtime *runtime = substream->runtime;
  667. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  668. struct snd_emu10k1_pcm_mixer *mix;
  669. int result = 0;
  670. // printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream));
  671. spin_lock(&emu->reg_lock);
  672. switch (cmd) {
  673. case SNDRV_PCM_TRIGGER_START:
  674. snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
  675. snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
  676. /* follow thru */
  677. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  678. case SNDRV_PCM_TRIGGER_RESUME:
  679. mix = &emu->pcm_mixer[substream->number];
  680. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
  681. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
  682. snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
  683. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
  684. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
  685. snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
  686. epcm->running = 1;
  687. break;
  688. case SNDRV_PCM_TRIGGER_STOP:
  689. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  690. case SNDRV_PCM_TRIGGER_SUSPEND:
  691. epcm->running = 0;
  692. snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
  693. snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
  694. snd_emu10k1_playback_stop_voice(emu, epcm->extra);
  695. break;
  696. default:
  697. result = -EINVAL;
  698. break;
  699. }
  700. spin_unlock(&emu->reg_lock);
  701. return result;
  702. }
  703. static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
  704. int cmd)
  705. {
  706. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  707. struct snd_pcm_runtime *runtime = substream->runtime;
  708. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  709. int result = 0;
  710. spin_lock(&emu->reg_lock);
  711. switch (cmd) {
  712. case SNDRV_PCM_TRIGGER_START:
  713. case SNDRV_PCM_TRIGGER_RESUME:
  714. // hmm this should cause full and half full interrupt to be raised?
  715. outl(epcm->capture_ipr, emu->port + IPR);
  716. snd_emu10k1_intr_enable(emu, epcm->capture_inte);
  717. // printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs);
  718. switch (epcm->type) {
  719. case CAPTURE_AC97ADC:
  720. snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
  721. break;
  722. case CAPTURE_EFX:
  723. if (emu->audigy) {
  724. snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
  725. snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
  726. } else
  727. snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
  728. break;
  729. default:
  730. break;
  731. }
  732. snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
  733. epcm->running = 1;
  734. epcm->first_ptr = 1;
  735. break;
  736. case SNDRV_PCM_TRIGGER_STOP:
  737. case SNDRV_PCM_TRIGGER_SUSPEND:
  738. epcm->running = 0;
  739. snd_emu10k1_intr_disable(emu, epcm->capture_inte);
  740. outl(epcm->capture_ipr, emu->port + IPR);
  741. snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
  742. switch (epcm->type) {
  743. case CAPTURE_AC97ADC:
  744. snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
  745. break;
  746. case CAPTURE_EFX:
  747. if (emu->audigy) {
  748. snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
  749. snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
  750. } else
  751. snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
  752. break;
  753. default:
  754. break;
  755. }
  756. break;
  757. default:
  758. result = -EINVAL;
  759. }
  760. spin_unlock(&emu->reg_lock);
  761. return result;
  762. }
  763. static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
  764. {
  765. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  766. struct snd_pcm_runtime *runtime = substream->runtime;
  767. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  768. unsigned int ptr;
  769. if (!epcm->running)
  770. return 0;
  771. ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
  772. #if 0 /* Perex's code */
  773. ptr += runtime->buffer_size;
  774. ptr -= epcm->ccca_start_addr;
  775. ptr %= runtime->buffer_size;
  776. #else /* EMU10K1 Open Source code from Creative */
  777. if (ptr < epcm->ccca_start_addr)
  778. ptr += runtime->buffer_size - epcm->ccca_start_addr;
  779. else {
  780. ptr -= epcm->ccca_start_addr;
  781. if (ptr >= runtime->buffer_size)
  782. ptr -= runtime->buffer_size;
  783. }
  784. #endif
  785. // printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size);
  786. return ptr;
  787. }
  788. static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
  789. int cmd)
  790. {
  791. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  792. struct snd_pcm_runtime *runtime = substream->runtime;
  793. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  794. int i;
  795. int result = 0;
  796. spin_lock(&emu->reg_lock);
  797. switch (cmd) {
  798. case SNDRV_PCM_TRIGGER_START:
  799. // prepare voices
  800. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  801. snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
  802. }
  803. snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
  804. /* follow thru */
  805. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  806. case SNDRV_PCM_TRIGGER_RESUME:
  807. snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
  808. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
  809. &emu->efx_pcm_mixer[0]);
  810. for (i = 1; i < NUM_EFX_PLAYBACK; i++)
  811. snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
  812. &emu->efx_pcm_mixer[i]);
  813. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
  814. snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
  815. for (i = 1; i < NUM_EFX_PLAYBACK; i++)
  816. snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
  817. epcm->running = 1;
  818. break;
  819. case SNDRV_PCM_TRIGGER_SUSPEND:
  820. case SNDRV_PCM_TRIGGER_STOP:
  821. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  822. epcm->running = 0;
  823. for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
  824. snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
  825. }
  826. snd_emu10k1_playback_stop_voice(emu, epcm->extra);
  827. break;
  828. default:
  829. result = -EINVAL;
  830. break;
  831. }
  832. spin_unlock(&emu->reg_lock);
  833. return result;
  834. }
  835. static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
  836. {
  837. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  838. struct snd_pcm_runtime *runtime = substream->runtime;
  839. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  840. unsigned int ptr;
  841. if (!epcm->running)
  842. return 0;
  843. if (epcm->first_ptr) {
  844. udelay(50); // hack, it takes awhile until capture is started
  845. epcm->first_ptr = 0;
  846. }
  847. ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
  848. return bytes_to_frames(runtime, ptr);
  849. }
  850. /*
  851. * Playback support device description
  852. */
  853. static struct snd_pcm_hardware snd_emu10k1_playback =
  854. {
  855. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  856. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  857. SNDRV_PCM_INFO_RESUME |
  858. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  859. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  860. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
  861. .rate_min = 4000,
  862. .rate_max = 96000,
  863. .channels_min = 1,
  864. .channels_max = 2,
  865. .buffer_bytes_max = (128*1024),
  866. .period_bytes_min = 64,
  867. .period_bytes_max = (128*1024),
  868. .periods_min = 1,
  869. .periods_max = 1024,
  870. .fifo_size = 0,
  871. };
  872. /*
  873. * Capture support device description
  874. */
  875. static struct snd_pcm_hardware snd_emu10k1_capture =
  876. {
  877. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  878. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  879. SNDRV_PCM_INFO_RESUME |
  880. SNDRV_PCM_INFO_MMAP_VALID),
  881. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  882. .rates = SNDRV_PCM_RATE_8000_48000,
  883. .rate_min = 8000,
  884. .rate_max = 48000,
  885. .channels_min = 1,
  886. .channels_max = 2,
  887. .buffer_bytes_max = (64*1024),
  888. .period_bytes_min = 384,
  889. .period_bytes_max = (64*1024),
  890. .periods_min = 2,
  891. .periods_max = 2,
  892. .fifo_size = 0,
  893. };
  894. /*
  895. *
  896. */
  897. static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
  898. {
  899. struct snd_ctl_elem_id id;
  900. if (! kctl)
  901. return;
  902. if (activate)
  903. kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  904. else
  905. kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  906. snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
  907. SNDRV_CTL_EVENT_MASK_INFO,
  908. snd_ctl_build_ioff(&id, kctl, idx));
  909. }
  910. static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
  911. {
  912. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
  913. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
  914. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
  915. }
  916. static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
  917. {
  918. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
  919. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
  920. snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
  921. }
  922. static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
  923. {
  924. kfree(runtime->private_data);
  925. }
  926. static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
  927. {
  928. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  929. struct snd_emu10k1_pcm_mixer *mix;
  930. int i;
  931. for (i=0; i < NUM_EFX_PLAYBACK; i++) {
  932. mix = &emu->efx_pcm_mixer[i];
  933. mix->epcm = NULL;
  934. snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
  935. }
  936. return 0;
  937. }
  938. static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
  939. {
  940. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  941. struct snd_emu10k1_pcm *epcm;
  942. struct snd_emu10k1_pcm_mixer *mix;
  943. struct snd_pcm_runtime *runtime = substream->runtime;
  944. int i;
  945. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  946. if (epcm == NULL)
  947. return -ENOMEM;
  948. epcm->emu = emu;
  949. epcm->type = PLAYBACK_EFX;
  950. epcm->substream = substream;
  951. emu->pcm_playback_efx_substream = substream;
  952. runtime->private_data = epcm;
  953. runtime->private_free = snd_emu10k1_pcm_free_substream;
  954. runtime->hw = snd_emu10k1_efx_playback;
  955. for (i=0; i < NUM_EFX_PLAYBACK; i++) {
  956. mix = &emu->efx_pcm_mixer[i];
  957. mix->send_routing[0][0] = i;
  958. memset(&mix->send_volume, 0, sizeof(mix->send_volume));
  959. mix->send_volume[0][0] = 255;
  960. mix->attn[0] = 0xffff;
  961. mix->epcm = epcm;
  962. snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
  963. }
  964. return 0;
  965. }
  966. static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
  967. {
  968. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  969. struct snd_emu10k1_pcm *epcm;
  970. struct snd_emu10k1_pcm_mixer *mix;
  971. struct snd_pcm_runtime *runtime = substream->runtime;
  972. int i, err;
  973. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  974. if (epcm == NULL)
  975. return -ENOMEM;
  976. epcm->emu = emu;
  977. epcm->type = PLAYBACK_EMUVOICE;
  978. epcm->substream = substream;
  979. runtime->private_data = epcm;
  980. runtime->private_free = snd_emu10k1_pcm_free_substream;
  981. runtime->hw = snd_emu10k1_playback;
  982. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
  983. kfree(epcm);
  984. return err;
  985. }
  986. if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
  987. kfree(epcm);
  988. return err;
  989. }
  990. mix = &emu->pcm_mixer[substream->number];
  991. for (i = 0; i < 4; i++)
  992. mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
  993. memset(&mix->send_volume, 0, sizeof(mix->send_volume));
  994. mix->send_volume[0][0] = mix->send_volume[0][1] =
  995. mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
  996. mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
  997. mix->epcm = epcm;
  998. snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
  999. return 0;
  1000. }
  1001. static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
  1002. {
  1003. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1004. struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
  1005. mix->epcm = NULL;
  1006. snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
  1007. return 0;
  1008. }
  1009. static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
  1010. {
  1011. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1012. struct snd_pcm_runtime *runtime = substream->runtime;
  1013. struct snd_emu10k1_pcm *epcm;
  1014. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1015. if (epcm == NULL)
  1016. return -ENOMEM;
  1017. epcm->emu = emu;
  1018. epcm->type = CAPTURE_AC97ADC;
  1019. epcm->substream = substream;
  1020. epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
  1021. epcm->capture_inte = INTE_ADCBUFENABLE;
  1022. epcm->capture_ba_reg = ADCBA;
  1023. epcm->capture_bs_reg = ADCBS;
  1024. epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
  1025. runtime->private_data = epcm;
  1026. runtime->private_free = snd_emu10k1_pcm_free_substream;
  1027. runtime->hw = snd_emu10k1_capture;
  1028. emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
  1029. emu->pcm_capture_substream = substream;
  1030. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
  1031. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
  1032. return 0;
  1033. }
  1034. static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
  1035. {
  1036. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1037. emu->capture_interrupt = NULL;
  1038. emu->pcm_capture_substream = NULL;
  1039. return 0;
  1040. }
  1041. static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
  1042. {
  1043. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1044. struct snd_emu10k1_pcm *epcm;
  1045. struct snd_pcm_runtime *runtime = substream->runtime;
  1046. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1047. if (epcm == NULL)
  1048. return -ENOMEM;
  1049. epcm->emu = emu;
  1050. epcm->type = CAPTURE_AC97MIC;
  1051. epcm->substream = substream;
  1052. epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
  1053. epcm->capture_inte = INTE_MICBUFENABLE;
  1054. epcm->capture_ba_reg = MICBA;
  1055. epcm->capture_bs_reg = MICBS;
  1056. epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
  1057. substream->runtime->private_data = epcm;
  1058. substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
  1059. runtime->hw = snd_emu10k1_capture;
  1060. runtime->hw.rates = SNDRV_PCM_RATE_8000;
  1061. runtime->hw.rate_min = runtime->hw.rate_max = 8000;
  1062. runtime->hw.channels_min = 1;
  1063. emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
  1064. emu->pcm_capture_mic_substream = substream;
  1065. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
  1066. return 0;
  1067. }
  1068. static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
  1069. {
  1070. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1071. emu->capture_interrupt = NULL;
  1072. emu->pcm_capture_mic_substream = NULL;
  1073. return 0;
  1074. }
  1075. static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
  1076. {
  1077. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1078. struct snd_emu10k1_pcm *epcm;
  1079. struct snd_pcm_runtime *runtime = substream->runtime;
  1080. int nefx = emu->audigy ? 64 : 32;
  1081. int idx;
  1082. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  1083. if (epcm == NULL)
  1084. return -ENOMEM;
  1085. epcm->emu = emu;
  1086. epcm->type = CAPTURE_EFX;
  1087. epcm->substream = substream;
  1088. epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
  1089. epcm->capture_inte = INTE_EFXBUFENABLE;
  1090. epcm->capture_ba_reg = FXBA;
  1091. epcm->capture_bs_reg = FXBS;
  1092. epcm->capture_idx_reg = FXIDX;
  1093. substream->runtime->private_data = epcm;
  1094. substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
  1095. runtime->hw = snd_emu10k1_capture;
  1096. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  1097. runtime->hw.rate_min = runtime->hw.rate_max = 48000;
  1098. spin_lock_irq(&emu->reg_lock);
  1099. runtime->hw.channels_min = runtime->hw.channels_max = 0;
  1100. for (idx = 0; idx < nefx; idx++) {
  1101. if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
  1102. runtime->hw.channels_min++;
  1103. runtime->hw.channels_max++;
  1104. }
  1105. }
  1106. epcm->capture_cr_val = emu->efx_voices_mask[0];
  1107. epcm->capture_cr_val2 = emu->efx_voices_mask[1];
  1108. spin_unlock_irq(&emu->reg_lock);
  1109. emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
  1110. emu->pcm_capture_efx_substream = substream;
  1111. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
  1112. return 0;
  1113. }
  1114. static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
  1115. {
  1116. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1117. emu->capture_interrupt = NULL;
  1118. emu->pcm_capture_efx_substream = NULL;
  1119. return 0;
  1120. }
  1121. static struct snd_pcm_ops snd_emu10k1_playback_ops = {
  1122. .open = snd_emu10k1_playback_open,
  1123. .close = snd_emu10k1_playback_close,
  1124. .ioctl = snd_pcm_lib_ioctl,
  1125. .hw_params = snd_emu10k1_playback_hw_params,
  1126. .hw_free = snd_emu10k1_playback_hw_free,
  1127. .prepare = snd_emu10k1_playback_prepare,
  1128. .trigger = snd_emu10k1_playback_trigger,
  1129. .pointer = snd_emu10k1_playback_pointer,
  1130. .page = snd_pcm_sgbuf_ops_page,
  1131. };
  1132. static struct snd_pcm_ops snd_emu10k1_capture_ops = {
  1133. .open = snd_emu10k1_capture_open,
  1134. .close = snd_emu10k1_capture_close,
  1135. .ioctl = snd_pcm_lib_ioctl,
  1136. .hw_params = snd_emu10k1_capture_hw_params,
  1137. .hw_free = snd_emu10k1_capture_hw_free,
  1138. .prepare = snd_emu10k1_capture_prepare,
  1139. .trigger = snd_emu10k1_capture_trigger,
  1140. .pointer = snd_emu10k1_capture_pointer,
  1141. };
  1142. /* EFX playback */
  1143. static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
  1144. .open = snd_emu10k1_efx_playback_open,
  1145. .close = snd_emu10k1_efx_playback_close,
  1146. .ioctl = snd_pcm_lib_ioctl,
  1147. .hw_params = snd_emu10k1_playback_hw_params,
  1148. .hw_free = snd_emu10k1_efx_playback_hw_free,
  1149. .prepare = snd_emu10k1_efx_playback_prepare,
  1150. .trigger = snd_emu10k1_efx_playback_trigger,
  1151. .pointer = snd_emu10k1_efx_playback_pointer,
  1152. .page = snd_pcm_sgbuf_ops_page,
  1153. };
  1154. int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
  1155. {
  1156. struct snd_pcm *pcm;
  1157. struct snd_pcm_substream *substream;
  1158. int err;
  1159. if (rpcm)
  1160. *rpcm = NULL;
  1161. if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
  1162. return err;
  1163. pcm->private_data = emu;
  1164. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
  1165. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
  1166. pcm->info_flags = 0;
  1167. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1168. strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
  1169. emu->pcm = pcm;
  1170. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1171. if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
  1172. return err;
  1173. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
  1174. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
  1175. if (rpcm)
  1176. *rpcm = pcm;
  1177. return 0;
  1178. }
  1179. int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
  1180. {
  1181. struct snd_pcm *pcm;
  1182. struct snd_pcm_substream *substream;
  1183. int err;
  1184. if (rpcm)
  1185. *rpcm = NULL;
  1186. if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
  1187. return err;
  1188. pcm->private_data = emu;
  1189. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
  1190. pcm->info_flags = 0;
  1191. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1192. strcpy(pcm->name, "Multichannel Playback");
  1193. emu->pcm_multi = pcm;
  1194. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1195. if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
  1196. return err;
  1197. if (rpcm)
  1198. *rpcm = pcm;
  1199. return 0;
  1200. }
  1201. static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
  1202. .open = snd_emu10k1_capture_mic_open,
  1203. .close = snd_emu10k1_capture_mic_close,
  1204. .ioctl = snd_pcm_lib_ioctl,
  1205. .hw_params = snd_emu10k1_capture_hw_params,
  1206. .hw_free = snd_emu10k1_capture_hw_free,
  1207. .prepare = snd_emu10k1_capture_prepare,
  1208. .trigger = snd_emu10k1_capture_trigger,
  1209. .pointer = snd_emu10k1_capture_pointer,
  1210. };
  1211. int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
  1212. {
  1213. struct snd_pcm *pcm;
  1214. int err;
  1215. if (rpcm)
  1216. *rpcm = NULL;
  1217. if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
  1218. return err;
  1219. pcm->private_data = emu;
  1220. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
  1221. pcm->info_flags = 0;
  1222. strcpy(pcm->name, "Mic Capture");
  1223. emu->pcm_mic = pcm;
  1224. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
  1225. if (rpcm)
  1226. *rpcm = pcm;
  1227. return 0;
  1228. }
  1229. static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1230. {
  1231. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  1232. int nefx = emu->audigy ? 64 : 32;
  1233. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1234. uinfo->count = nefx;
  1235. uinfo->value.integer.min = 0;
  1236. uinfo->value.integer.max = 1;
  1237. return 0;
  1238. }
  1239. static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1240. {
  1241. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  1242. int nefx = emu->audigy ? 64 : 32;
  1243. int idx;
  1244. spin_lock_irq(&emu->reg_lock);
  1245. for (idx = 0; idx < nefx; idx++)
  1246. ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
  1247. spin_unlock_irq(&emu->reg_lock);
  1248. return 0;
  1249. }
  1250. static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1251. {
  1252. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  1253. unsigned int nval[2], bits;
  1254. int nefx = emu->audigy ? 64 : 32;
  1255. int nefxb = emu->audigy ? 7 : 6;
  1256. int change, idx;
  1257. nval[0] = nval[1] = 0;
  1258. for (idx = 0, bits = 0; idx < nefx; idx++)
  1259. if (ucontrol->value.integer.value[idx]) {
  1260. nval[idx / 32] |= 1 << (idx % 32);
  1261. bits++;
  1262. }
  1263. for (idx = 0; idx < nefxb; idx++)
  1264. if (1 << idx == bits)
  1265. break;
  1266. if (idx >= nefxb)
  1267. return -EINVAL;
  1268. spin_lock_irq(&emu->reg_lock);
  1269. change = (nval[0] != emu->efx_voices_mask[0]) ||
  1270. (nval[1] != emu->efx_voices_mask[1]);
  1271. emu->efx_voices_mask[0] = nval[0];
  1272. emu->efx_voices_mask[1] = nval[1];
  1273. spin_unlock_irq(&emu->reg_lock);
  1274. return change;
  1275. }
  1276. static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
  1277. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1278. .name = "Captured FX8010 Outputs",
  1279. .info = snd_emu10k1_pcm_efx_voices_mask_info,
  1280. .get = snd_emu10k1_pcm_efx_voices_mask_get,
  1281. .put = snd_emu10k1_pcm_efx_voices_mask_put
  1282. };
  1283. static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
  1284. .open = snd_emu10k1_capture_efx_open,
  1285. .close = snd_emu10k1_capture_efx_close,
  1286. .ioctl = snd_pcm_lib_ioctl,
  1287. .hw_params = snd_emu10k1_capture_hw_params,
  1288. .hw_free = snd_emu10k1_capture_hw_free,
  1289. .prepare = snd_emu10k1_capture_prepare,
  1290. .trigger = snd_emu10k1_capture_trigger,
  1291. .pointer = snd_emu10k1_capture_pointer,
  1292. };
  1293. /* EFX playback */
  1294. #define INITIAL_TRAM_SHIFT 14
  1295. #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
  1296. static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
  1297. {
  1298. struct snd_pcm_substream *substream = private_data;
  1299. snd_pcm_period_elapsed(substream);
  1300. }
  1301. static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
  1302. unsigned short *dst_right,
  1303. unsigned short *src,
  1304. unsigned int count,
  1305. unsigned int tram_shift)
  1306. {
  1307. // printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count);
  1308. if ((tram_shift & 1) == 0) {
  1309. while (count--) {
  1310. *dst_left-- = *src++;
  1311. *dst_right-- = *src++;
  1312. }
  1313. } else {
  1314. while (count--) {
  1315. *dst_right-- = *src++;
  1316. *dst_left-- = *src++;
  1317. }
  1318. }
  1319. }
  1320. static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
  1321. struct snd_pcm_indirect *rec, size_t bytes)
  1322. {
  1323. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1324. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1325. unsigned int tram_size = pcm->buffer_size;
  1326. unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
  1327. unsigned int frames = bytes >> 2, count;
  1328. unsigned int tram_pos = pcm->tram_pos;
  1329. unsigned int tram_shift = pcm->tram_shift;
  1330. while (frames > tram_pos) {
  1331. count = tram_pos + 1;
  1332. snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
  1333. (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
  1334. src, count, tram_shift);
  1335. src += count * 2;
  1336. frames -= count;
  1337. tram_pos = (tram_size / 2) - 1;
  1338. tram_shift++;
  1339. }
  1340. snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
  1341. (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
  1342. src, frames, tram_shift);
  1343. tram_pos -= frames;
  1344. pcm->tram_pos = tram_pos;
  1345. pcm->tram_shift = tram_shift;
  1346. }
  1347. static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
  1348. {
  1349. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1350. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1351. snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
  1352. return 0;
  1353. }
  1354. static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
  1355. struct snd_pcm_hw_params *hw_params)
  1356. {
  1357. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  1358. }
  1359. static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
  1360. {
  1361. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1362. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1363. unsigned int i;
  1364. for (i = 0; i < pcm->channels; i++)
  1365. snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
  1366. snd_pcm_lib_free_pages(substream);
  1367. return 0;
  1368. }
  1369. static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
  1370. {
  1371. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1372. struct snd_pcm_runtime *runtime = substream->runtime;
  1373. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1374. unsigned int i;
  1375. // 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);
  1376. memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
  1377. pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
  1378. pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  1379. pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
  1380. pcm->tram_shift = 0;
  1381. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
  1382. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
  1383. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
  1384. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
  1385. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
  1386. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
  1387. for (i = 0; i < pcm->channels; i++)
  1388. snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
  1389. return 0;
  1390. }
  1391. static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  1392. {
  1393. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1394. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1395. int result = 0;
  1396. spin_lock(&emu->reg_lock);
  1397. switch (cmd) {
  1398. case SNDRV_PCM_TRIGGER_START:
  1399. /* follow thru */
  1400. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1401. case SNDRV_PCM_TRIGGER_RESUME:
  1402. #ifdef EMU10K1_SET_AC3_IEC958
  1403. {
  1404. int i;
  1405. for (i = 0; i < 3; i++) {
  1406. unsigned int bits;
  1407. bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1408. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
  1409. 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
  1410. snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
  1411. }
  1412. }
  1413. #endif
  1414. result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
  1415. if (result < 0)
  1416. goto __err;
  1417. snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
  1418. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
  1419. break;
  1420. case SNDRV_PCM_TRIGGER_STOP:
  1421. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1422. case SNDRV_PCM_TRIGGER_SUSPEND:
  1423. snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
  1424. snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
  1425. pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
  1426. pcm->tram_shift = 0;
  1427. break;
  1428. default:
  1429. result = -EINVAL;
  1430. break;
  1431. }
  1432. __err:
  1433. spin_unlock(&emu->reg_lock);
  1434. return result;
  1435. }
  1436. static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
  1437. {
  1438. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1439. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1440. size_t ptr; /* byte pointer */
  1441. if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
  1442. return 0;
  1443. ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
  1444. return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
  1445. }
  1446. static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
  1447. {
  1448. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1449. SNDRV_PCM_INFO_RESUME |
  1450. /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
  1451. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1452. .rates = SNDRV_PCM_RATE_48000,
  1453. .rate_min = 48000,
  1454. .rate_max = 48000,
  1455. .channels_min = 1,
  1456. .channels_max = 1,
  1457. .buffer_bytes_max = (128*1024),
  1458. .period_bytes_min = 1024,
  1459. .period_bytes_max = (128*1024),
  1460. .periods_min = 1,
  1461. .periods_max = 1024,
  1462. .fifo_size = 0,
  1463. };
  1464. static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
  1465. {
  1466. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1467. struct snd_pcm_runtime *runtime = substream->runtime;
  1468. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1469. runtime->hw = snd_emu10k1_fx8010_playback;
  1470. runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
  1471. runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
  1472. spin_lock_irq(&emu->reg_lock);
  1473. if (pcm->valid == 0) {
  1474. spin_unlock_irq(&emu->reg_lock);
  1475. return -ENODEV;
  1476. }
  1477. pcm->opened = 1;
  1478. spin_unlock_irq(&emu->reg_lock);
  1479. return 0;
  1480. }
  1481. static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
  1482. {
  1483. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  1484. struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
  1485. spin_lock_irq(&emu->reg_lock);
  1486. pcm->opened = 0;
  1487. spin_unlock_irq(&emu->reg_lock);
  1488. return 0;
  1489. }
  1490. static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
  1491. .open = snd_emu10k1_fx8010_playback_open,
  1492. .close = snd_emu10k1_fx8010_playback_close,
  1493. .ioctl = snd_pcm_lib_ioctl,
  1494. .hw_params = snd_emu10k1_fx8010_playback_hw_params,
  1495. .hw_free = snd_emu10k1_fx8010_playback_hw_free,
  1496. .prepare = snd_emu10k1_fx8010_playback_prepare,
  1497. .trigger = snd_emu10k1_fx8010_playback_trigger,
  1498. .pointer = snd_emu10k1_fx8010_playback_pointer,
  1499. .ack = snd_emu10k1_fx8010_playback_transfer,
  1500. };
  1501. int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
  1502. {
  1503. struct snd_pcm *pcm;
  1504. struct snd_kcontrol *kctl;
  1505. int err;
  1506. if (rpcm)
  1507. *rpcm = NULL;
  1508. if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
  1509. return err;
  1510. pcm->private_data = emu;
  1511. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
  1512. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
  1513. pcm->info_flags = 0;
  1514. strcpy(pcm->name, "Multichannel Capture/PT Playback");
  1515. emu->pcm_efx = pcm;
  1516. if (rpcm)
  1517. *rpcm = pcm;
  1518. /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
  1519. * to these
  1520. */
  1521. /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
  1522. if (emu->audigy) {
  1523. emu->efx_voices_mask[0] = 0;
  1524. emu->efx_voices_mask[1] = 0xffff;
  1525. } else {
  1526. emu->efx_voices_mask[0] = 0xffff0000;
  1527. emu->efx_voices_mask[1] = 0;
  1528. }
  1529. kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
  1530. if (!kctl)
  1531. return -ENOMEM;
  1532. kctl->id.device = device;
  1533. snd_ctl_add(emu->card, kctl);
  1534. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
  1535. return 0;
  1536. }