emupcm.c 57 KB

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