emupcm.c 56 KB

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