emupcm.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  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 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 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 unsigned int capture_rates[8] = {
  169. 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
  170. };
  171. static 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 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 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 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 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 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 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 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 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 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 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. snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
  1478. return 0;
  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 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 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. }