emupcm.c 56 KB

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