emupcm.c 58 KB

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