emumixer.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
  3. * Takashi Iwai <tiwai@suse.de>
  4. * Creative Labs, Inc.
  5. * Routines for control of EMU10K1 chips / mixer routines
  6. * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
  7. *
  8. * BUGS:
  9. * --
  10. *
  11. * TODO:
  12. * --
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <sound/driver.h>
  30. #include <linux/time.h>
  31. #include <linux/init.h>
  32. #include <sound/core.h>
  33. #include <sound/emu10k1.h>
  34. #define AC97_ID_STAC9758 0x83847658
  35. static int snd_emu10k1_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  36. {
  37. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  38. uinfo->count = 1;
  39. return 0;
  40. }
  41. static int snd_emu10k1_spdif_get(snd_kcontrol_t * kcontrol,
  42. snd_ctl_elem_value_t * ucontrol)
  43. {
  44. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  45. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  46. unsigned long flags;
  47. spin_lock_irqsave(&emu->reg_lock, flags);
  48. ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
  49. ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
  50. ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
  51. ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
  52. spin_unlock_irqrestore(&emu->reg_lock, flags);
  53. return 0;
  54. }
  55. static int snd_emu10k1_spdif_get_mask(snd_kcontrol_t * kcontrol,
  56. snd_ctl_elem_value_t * ucontrol)
  57. {
  58. ucontrol->value.iec958.status[0] = 0xff;
  59. ucontrol->value.iec958.status[1] = 0xff;
  60. ucontrol->value.iec958.status[2] = 0xff;
  61. ucontrol->value.iec958.status[3] = 0xff;
  62. return 0;
  63. }
  64. static int snd_audigy_spdif_output_rate_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  65. {
  66. static char *texts[] = {"44100", "48000", "96000"};
  67. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  68. uinfo->count = 1;
  69. uinfo->value.enumerated.items = 3;
  70. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  71. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  72. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  73. return 0;
  74. }
  75. static int snd_audigy_spdif_output_rate_get(snd_kcontrol_t * kcontrol,
  76. snd_ctl_elem_value_t * ucontrol)
  77. {
  78. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  79. unsigned int tmp;
  80. unsigned long flags;
  81. spin_lock_irqsave(&emu->reg_lock, flags);
  82. tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
  83. switch (tmp & A_SPDIF_RATE_MASK) {
  84. case A_SPDIF_44100:
  85. ucontrol->value.enumerated.item[0] = 0;
  86. break;
  87. case A_SPDIF_48000:
  88. ucontrol->value.enumerated.item[0] = 1;
  89. break;
  90. case A_SPDIF_96000:
  91. ucontrol->value.enumerated.item[0] = 2;
  92. break;
  93. default:
  94. ucontrol->value.enumerated.item[0] = 1;
  95. }
  96. spin_unlock_irqrestore(&emu->reg_lock, flags);
  97. return 0;
  98. }
  99. static int snd_audigy_spdif_output_rate_put(snd_kcontrol_t * kcontrol,
  100. snd_ctl_elem_value_t * ucontrol)
  101. {
  102. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  103. int change;
  104. unsigned int reg, val, tmp;
  105. unsigned long flags;
  106. switch(ucontrol->value.enumerated.item[0]) {
  107. case 0:
  108. val = A_SPDIF_44100;
  109. break;
  110. case 1:
  111. val = A_SPDIF_48000;
  112. break;
  113. case 2:
  114. val = A_SPDIF_96000;
  115. break;
  116. default:
  117. val = A_SPDIF_48000;
  118. break;
  119. }
  120. spin_lock_irqsave(&emu->reg_lock, flags);
  121. reg = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
  122. tmp = reg & ~A_SPDIF_RATE_MASK;
  123. tmp |= val;
  124. if ((change = (tmp != reg)))
  125. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
  126. spin_unlock_irqrestore(&emu->reg_lock, flags);
  127. return change;
  128. }
  129. static snd_kcontrol_new_t snd_audigy_spdif_output_rate =
  130. {
  131. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  132. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  133. .name = "Audigy SPDIF Output Sample Rate",
  134. .count = 1,
  135. .info = snd_audigy_spdif_output_rate_info,
  136. .get = snd_audigy_spdif_output_rate_get,
  137. .put = snd_audigy_spdif_output_rate_put
  138. };
  139. static int snd_emu10k1_spdif_put(snd_kcontrol_t * kcontrol,
  140. snd_ctl_elem_value_t * ucontrol)
  141. {
  142. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  143. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  144. int change;
  145. unsigned int val;
  146. unsigned long flags;
  147. val = (ucontrol->value.iec958.status[0] << 0) |
  148. (ucontrol->value.iec958.status[1] << 8) |
  149. (ucontrol->value.iec958.status[2] << 16) |
  150. (ucontrol->value.iec958.status[3] << 24);
  151. spin_lock_irqsave(&emu->reg_lock, flags);
  152. change = val != emu->spdif_bits[idx];
  153. if (change) {
  154. snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
  155. emu->spdif_bits[idx] = val;
  156. }
  157. spin_unlock_irqrestore(&emu->reg_lock, flags);
  158. return change;
  159. }
  160. static snd_kcontrol_new_t snd_emu10k1_spdif_mask_control =
  161. {
  162. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  163. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  164. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  165. .count = 4,
  166. .info = snd_emu10k1_spdif_info,
  167. .get = snd_emu10k1_spdif_get_mask
  168. };
  169. static snd_kcontrol_new_t snd_emu10k1_spdif_control =
  170. {
  171. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  172. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  173. .count = 4,
  174. .info = snd_emu10k1_spdif_info,
  175. .get = snd_emu10k1_spdif_get,
  176. .put = snd_emu10k1_spdif_put
  177. };
  178. static void update_emu10k1_fxrt(emu10k1_t *emu, int voice, unsigned char *route)
  179. {
  180. if (emu->audigy) {
  181. snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
  182. snd_emu10k1_compose_audigy_fxrt1(route));
  183. snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
  184. snd_emu10k1_compose_audigy_fxrt2(route));
  185. } else {
  186. snd_emu10k1_ptr_write(emu, FXRT, voice,
  187. snd_emu10k1_compose_send_routing(route));
  188. }
  189. }
  190. static void update_emu10k1_send_volume(emu10k1_t *emu, int voice, unsigned char *volume)
  191. {
  192. snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
  193. snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
  194. snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
  195. snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
  196. if (emu->audigy) {
  197. unsigned int val = ((unsigned int)volume[4] << 24) |
  198. ((unsigned int)volume[5] << 16) |
  199. ((unsigned int)volume[6] << 8) |
  200. (unsigned int)volume[7];
  201. snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, val);
  202. }
  203. }
  204. /* PCM stream controls */
  205. static int snd_emu10k1_send_routing_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  206. {
  207. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  208. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  209. uinfo->count = emu->audigy ? 3*8 : 3*4;
  210. uinfo->value.integer.min = 0;
  211. uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
  212. return 0;
  213. }
  214. static int snd_emu10k1_send_routing_get(snd_kcontrol_t * kcontrol,
  215. snd_ctl_elem_value_t * ucontrol)
  216. {
  217. unsigned long flags;
  218. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  219. emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  220. int voice, idx;
  221. int num_efx = emu->audigy ? 8 : 4;
  222. int mask = emu->audigy ? 0x3f : 0x0f;
  223. spin_lock_irqsave(&emu->reg_lock, flags);
  224. for (voice = 0; voice < 3; voice++)
  225. for (idx = 0; idx < num_efx; idx++)
  226. ucontrol->value.integer.value[(voice * num_efx) + idx] =
  227. mix->send_routing[voice][idx] & mask;
  228. spin_unlock_irqrestore(&emu->reg_lock, flags);
  229. return 0;
  230. }
  231. static int snd_emu10k1_send_routing_put(snd_kcontrol_t * kcontrol,
  232. snd_ctl_elem_value_t * ucontrol)
  233. {
  234. unsigned long flags;
  235. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  236. emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  237. int change = 0, voice, idx, val;
  238. int num_efx = emu->audigy ? 8 : 4;
  239. int mask = emu->audigy ? 0x3f : 0x0f;
  240. spin_lock_irqsave(&emu->reg_lock, flags);
  241. for (voice = 0; voice < 3; voice++)
  242. for (idx = 0; idx < num_efx; idx++) {
  243. val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
  244. if (mix->send_routing[voice][idx] != val) {
  245. mix->send_routing[voice][idx] = val;
  246. change = 1;
  247. }
  248. }
  249. if (change && mix->epcm) {
  250. if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
  251. update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
  252. &mix->send_routing[1][0]);
  253. update_emu10k1_fxrt(emu, mix->epcm->voices[1]->number,
  254. &mix->send_routing[2][0]);
  255. } else if (mix->epcm->voices[0]) {
  256. update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
  257. &mix->send_routing[0][0]);
  258. }
  259. }
  260. spin_unlock_irqrestore(&emu->reg_lock, flags);
  261. return change;
  262. }
  263. static snd_kcontrol_new_t snd_emu10k1_send_routing_control =
  264. {
  265. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  266. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  267. .name = "EMU10K1 PCM Send Routing",
  268. .count = 32,
  269. .info = snd_emu10k1_send_routing_info,
  270. .get = snd_emu10k1_send_routing_get,
  271. .put = snd_emu10k1_send_routing_put
  272. };
  273. static int snd_emu10k1_send_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  274. {
  275. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  276. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  277. uinfo->count = emu->audigy ? 3*8 : 3*4;
  278. uinfo->value.integer.min = 0;
  279. uinfo->value.integer.max = 255;
  280. return 0;
  281. }
  282. static int snd_emu10k1_send_volume_get(snd_kcontrol_t * kcontrol,
  283. snd_ctl_elem_value_t * ucontrol)
  284. {
  285. unsigned long flags;
  286. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  287. emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  288. int idx;
  289. int num_efx = emu->audigy ? 8 : 4;
  290. spin_lock_irqsave(&emu->reg_lock, flags);
  291. for (idx = 0; idx < 3*num_efx; idx++)
  292. ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
  293. spin_unlock_irqrestore(&emu->reg_lock, flags);
  294. return 0;
  295. }
  296. static int snd_emu10k1_send_volume_put(snd_kcontrol_t * kcontrol,
  297. snd_ctl_elem_value_t * ucontrol)
  298. {
  299. unsigned long flags;
  300. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  301. emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  302. int change = 0, idx, val;
  303. int num_efx = emu->audigy ? 8 : 4;
  304. spin_lock_irqsave(&emu->reg_lock, flags);
  305. for (idx = 0; idx < 3*num_efx; idx++) {
  306. val = ucontrol->value.integer.value[idx] & 255;
  307. if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
  308. mix->send_volume[idx/num_efx][idx%num_efx] = val;
  309. change = 1;
  310. }
  311. }
  312. if (change && mix->epcm) {
  313. if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
  314. update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
  315. &mix->send_volume[1][0]);
  316. update_emu10k1_send_volume(emu, mix->epcm->voices[1]->number,
  317. &mix->send_volume[2][0]);
  318. } else if (mix->epcm->voices[0]) {
  319. update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
  320. &mix->send_volume[0][0]);
  321. }
  322. }
  323. spin_unlock_irqrestore(&emu->reg_lock, flags);
  324. return change;
  325. }
  326. static snd_kcontrol_new_t snd_emu10k1_send_volume_control =
  327. {
  328. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  329. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  330. .name = "EMU10K1 PCM Send Volume",
  331. .count = 32,
  332. .info = snd_emu10k1_send_volume_info,
  333. .get = snd_emu10k1_send_volume_get,
  334. .put = snd_emu10k1_send_volume_put
  335. };
  336. static int snd_emu10k1_attn_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  337. {
  338. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  339. uinfo->count = 3;
  340. uinfo->value.integer.min = 0;
  341. uinfo->value.integer.max = 0xffff;
  342. return 0;
  343. }
  344. static int snd_emu10k1_attn_get(snd_kcontrol_t * kcontrol,
  345. snd_ctl_elem_value_t * ucontrol)
  346. {
  347. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  348. emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  349. unsigned long flags;
  350. int idx;
  351. spin_lock_irqsave(&emu->reg_lock, flags);
  352. for (idx = 0; idx < 3; idx++)
  353. ucontrol->value.integer.value[idx] = mix->attn[idx];
  354. spin_unlock_irqrestore(&emu->reg_lock, flags);
  355. return 0;
  356. }
  357. static int snd_emu10k1_attn_put(snd_kcontrol_t * kcontrol,
  358. snd_ctl_elem_value_t * ucontrol)
  359. {
  360. unsigned long flags;
  361. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  362. emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  363. int change = 0, idx, val;
  364. spin_lock_irqsave(&emu->reg_lock, flags);
  365. for (idx = 0; idx < 3; idx++) {
  366. val = ucontrol->value.integer.value[idx] & 0xffff;
  367. if (mix->attn[idx] != val) {
  368. mix->attn[idx] = val;
  369. change = 1;
  370. }
  371. }
  372. if (change && mix->epcm) {
  373. if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
  374. snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
  375. snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[1]->number, mix->attn[2]);
  376. } else if (mix->epcm->voices[0]) {
  377. snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
  378. }
  379. }
  380. spin_unlock_irqrestore(&emu->reg_lock, flags);
  381. return change;
  382. }
  383. static snd_kcontrol_new_t snd_emu10k1_attn_control =
  384. {
  385. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  386. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  387. .name = "EMU10K1 PCM Volume",
  388. .count = 32,
  389. .info = snd_emu10k1_attn_info,
  390. .get = snd_emu10k1_attn_get,
  391. .put = snd_emu10k1_attn_put
  392. };
  393. /* Mutichannel PCM stream controls */
  394. static int snd_emu10k1_efx_send_routing_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  395. {
  396. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  397. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  398. uinfo->count = emu->audigy ? 8 : 4;
  399. uinfo->value.integer.min = 0;
  400. uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
  401. return 0;
  402. }
  403. static int snd_emu10k1_efx_send_routing_get(snd_kcontrol_t * kcontrol,
  404. snd_ctl_elem_value_t * ucontrol)
  405. {
  406. unsigned long flags;
  407. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  408. emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  409. int idx;
  410. int num_efx = emu->audigy ? 8 : 4;
  411. int mask = emu->audigy ? 0x3f : 0x0f;
  412. spin_lock_irqsave(&emu->reg_lock, flags);
  413. for (idx = 0; idx < num_efx; idx++)
  414. ucontrol->value.integer.value[idx] =
  415. mix->send_routing[0][idx] & mask;
  416. spin_unlock_irqrestore(&emu->reg_lock, flags);
  417. return 0;
  418. }
  419. static int snd_emu10k1_efx_send_routing_put(snd_kcontrol_t * kcontrol,
  420. snd_ctl_elem_value_t * ucontrol)
  421. {
  422. unsigned long flags;
  423. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  424. int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  425. emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[ch];
  426. int change = 0, idx, val;
  427. int num_efx = emu->audigy ? 8 : 4;
  428. int mask = emu->audigy ? 0x3f : 0x0f;
  429. spin_lock_irqsave(&emu->reg_lock, flags);
  430. for (idx = 0; idx < num_efx; idx++) {
  431. val = ucontrol->value.integer.value[idx] & mask;
  432. if (mix->send_routing[0][idx] != val) {
  433. mix->send_routing[0][idx] = val;
  434. change = 1;
  435. }
  436. }
  437. if (change && mix->epcm) {
  438. if (mix->epcm->voices[ch]) {
  439. update_emu10k1_fxrt(emu, mix->epcm->voices[ch]->number,
  440. &mix->send_routing[0][0]);
  441. }
  442. }
  443. spin_unlock_irqrestore(&emu->reg_lock, flags);
  444. return change;
  445. }
  446. static snd_kcontrol_new_t snd_emu10k1_efx_send_routing_control =
  447. {
  448. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  449. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  450. .name = "Multichannel PCM Send Routing",
  451. .count = 16,
  452. .info = snd_emu10k1_efx_send_routing_info,
  453. .get = snd_emu10k1_efx_send_routing_get,
  454. .put = snd_emu10k1_efx_send_routing_put
  455. };
  456. static int snd_emu10k1_efx_send_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  457. {
  458. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  459. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  460. uinfo->count = emu->audigy ? 8 : 4;
  461. uinfo->value.integer.min = 0;
  462. uinfo->value.integer.max = 255;
  463. return 0;
  464. }
  465. static int snd_emu10k1_efx_send_volume_get(snd_kcontrol_t * kcontrol,
  466. snd_ctl_elem_value_t * ucontrol)
  467. {
  468. unsigned long flags;
  469. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  470. emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  471. int idx;
  472. int num_efx = emu->audigy ? 8 : 4;
  473. spin_lock_irqsave(&emu->reg_lock, flags);
  474. for (idx = 0; idx < num_efx; idx++)
  475. ucontrol->value.integer.value[idx] = mix->send_volume[0][idx];
  476. spin_unlock_irqrestore(&emu->reg_lock, flags);
  477. return 0;
  478. }
  479. static int snd_emu10k1_efx_send_volume_put(snd_kcontrol_t * kcontrol,
  480. snd_ctl_elem_value_t * ucontrol)
  481. {
  482. unsigned long flags;
  483. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  484. int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  485. emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[ch];
  486. int change = 0, idx, val;
  487. int num_efx = emu->audigy ? 8 : 4;
  488. spin_lock_irqsave(&emu->reg_lock, flags);
  489. for (idx = 0; idx < num_efx; idx++) {
  490. val = ucontrol->value.integer.value[idx] & 255;
  491. if (mix->send_volume[0][idx] != val) {
  492. mix->send_volume[0][idx] = val;
  493. change = 1;
  494. }
  495. }
  496. if (change && mix->epcm) {
  497. if (mix->epcm->voices[ch]) {
  498. update_emu10k1_send_volume(emu, mix->epcm->voices[ch]->number,
  499. &mix->send_volume[0][0]);
  500. }
  501. }
  502. spin_unlock_irqrestore(&emu->reg_lock, flags);
  503. return change;
  504. }
  505. static snd_kcontrol_new_t snd_emu10k1_efx_send_volume_control =
  506. {
  507. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  508. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  509. .name = "Multichannel PCM Send Volume",
  510. .count = 16,
  511. .info = snd_emu10k1_efx_send_volume_info,
  512. .get = snd_emu10k1_efx_send_volume_get,
  513. .put = snd_emu10k1_efx_send_volume_put
  514. };
  515. static int snd_emu10k1_efx_attn_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  516. {
  517. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  518. uinfo->count = 1;
  519. uinfo->value.integer.min = 0;
  520. uinfo->value.integer.max = 0xffff;
  521. return 0;
  522. }
  523. static int snd_emu10k1_efx_attn_get(snd_kcontrol_t * kcontrol,
  524. snd_ctl_elem_value_t * ucontrol)
  525. {
  526. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  527. emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
  528. unsigned long flags;
  529. spin_lock_irqsave(&emu->reg_lock, flags);
  530. ucontrol->value.integer.value[0] = mix->attn[0];
  531. spin_unlock_irqrestore(&emu->reg_lock, flags);
  532. return 0;
  533. }
  534. static int snd_emu10k1_efx_attn_put(snd_kcontrol_t * kcontrol,
  535. snd_ctl_elem_value_t * ucontrol)
  536. {
  537. unsigned long flags;
  538. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  539. int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  540. emu10k1_pcm_mixer_t *mix = &emu->efx_pcm_mixer[ch];
  541. int change = 0, val;
  542. spin_lock_irqsave(&emu->reg_lock, flags);
  543. val = ucontrol->value.integer.value[0] & 0xffff;
  544. if (mix->attn[0] != val) {
  545. mix->attn[0] = val;
  546. change = 1;
  547. }
  548. if (change && mix->epcm) {
  549. if (mix->epcm->voices[ch]) {
  550. snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[ch]->number, mix->attn[0]);
  551. }
  552. }
  553. spin_unlock_irqrestore(&emu->reg_lock, flags);
  554. return change;
  555. }
  556. static snd_kcontrol_new_t snd_emu10k1_efx_attn_control =
  557. {
  558. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  559. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  560. .name = "Multichannel PCM Volume",
  561. .count = 16,
  562. .info = snd_emu10k1_efx_attn_info,
  563. .get = snd_emu10k1_efx_attn_get,
  564. .put = snd_emu10k1_efx_attn_put
  565. };
  566. static int snd_emu10k1_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  567. {
  568. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  569. uinfo->count = 1;
  570. uinfo->value.integer.min = 0;
  571. uinfo->value.integer.max = 1;
  572. return 0;
  573. }
  574. static int snd_emu10k1_shared_spdif_get(snd_kcontrol_t * kcontrol,
  575. snd_ctl_elem_value_t * ucontrol)
  576. {
  577. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  578. if (emu->audigy)
  579. ucontrol->value.integer.value[0] = inl(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
  580. else
  581. ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
  582. return 0;
  583. }
  584. static int snd_emu10k1_shared_spdif_put(snd_kcontrol_t * kcontrol,
  585. snd_ctl_elem_value_t * ucontrol)
  586. {
  587. unsigned long flags;
  588. emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
  589. unsigned int reg, val;
  590. int change = 0;
  591. spin_lock_irqsave(&emu->reg_lock, flags);
  592. if (emu->audigy) {
  593. reg = inl(emu->port + A_IOCFG);
  594. val = ucontrol->value.integer.value[0] ? A_IOCFG_GPOUT0 : 0;
  595. change = (reg & A_IOCFG_GPOUT0) != val;
  596. if (change) {
  597. reg &= ~A_IOCFG_GPOUT0;
  598. reg |= val;
  599. outl(reg | val, emu->port + A_IOCFG);
  600. }
  601. }
  602. reg = inl(emu->port + HCFG);
  603. val = ucontrol->value.integer.value[0] ? HCFG_GPOUT0 : 0;
  604. change |= (reg & HCFG_GPOUT0) != val;
  605. if (change) {
  606. reg &= ~HCFG_GPOUT0;
  607. reg |= val;
  608. outl(reg | val, emu->port + HCFG);
  609. }
  610. spin_unlock_irqrestore(&emu->reg_lock, flags);
  611. return change;
  612. }
  613. static snd_kcontrol_new_t snd_emu10k1_shared_spdif __devinitdata =
  614. {
  615. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  616. .name = "SB Live Analog/Digital Output Jack",
  617. .info = snd_emu10k1_shared_spdif_info,
  618. .get = snd_emu10k1_shared_spdif_get,
  619. .put = snd_emu10k1_shared_spdif_put
  620. };
  621. static snd_kcontrol_new_t snd_audigy_shared_spdif __devinitdata =
  622. {
  623. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  624. .name = "Audigy Analog/Digital Output Jack",
  625. .info = snd_emu10k1_shared_spdif_info,
  626. .get = snd_emu10k1_shared_spdif_get,
  627. .put = snd_emu10k1_shared_spdif_put
  628. };
  629. /*
  630. */
  631. static void snd_emu10k1_mixer_free_ac97(ac97_t *ac97)
  632. {
  633. emu10k1_t *emu = ac97->private_data;
  634. emu->ac97 = NULL;
  635. }
  636. /*
  637. */
  638. static int remove_ctl(snd_card_t *card, const char *name)
  639. {
  640. snd_ctl_elem_id_t id;
  641. memset(&id, 0, sizeof(id));
  642. strcpy(id.name, name);
  643. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  644. return snd_ctl_remove_id(card, &id);
  645. }
  646. static snd_kcontrol_t *ctl_find(snd_card_t *card, const char *name)
  647. {
  648. snd_ctl_elem_id_t sid;
  649. memset(&sid, 0, sizeof(sid));
  650. strcpy(sid.name, name);
  651. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  652. return snd_ctl_find_id(card, &sid);
  653. }
  654. static int rename_ctl(snd_card_t *card, const char *src, const char *dst)
  655. {
  656. snd_kcontrol_t *kctl = ctl_find(card, src);
  657. if (kctl) {
  658. strcpy(kctl->id.name, dst);
  659. return 0;
  660. }
  661. return -ENOENT;
  662. }
  663. int __devinit snd_emu10k1_mixer(emu10k1_t *emu)
  664. {
  665. int err, pcm;
  666. snd_kcontrol_t *kctl;
  667. snd_card_t *card = emu->card;
  668. char **c;
  669. static char *emu10k1_remove_ctls[] = {
  670. /* no AC97 mono, surround, center/lfe */
  671. "Master Mono Playback Switch",
  672. "Master Mono Playback Volume",
  673. "PCM Out Path & Mute",
  674. "Mono Output Select",
  675. "Surround Playback Switch",
  676. "Surround Playback Volume",
  677. "Center Playback Switch",
  678. "Center Playback Volume",
  679. "LFE Playback Switch",
  680. "LFE Playback Volume",
  681. NULL
  682. };
  683. static char *emu10k1_rename_ctls[] = {
  684. "Surround Digital Playback Volume", "Surround Playback Volume",
  685. "Center Digital Playback Volume", "Center Playback Volume",
  686. "LFE Digital Playback Volume", "LFE Playback Volume",
  687. NULL
  688. };
  689. static char *audigy_remove_ctls[] = {
  690. /* Master/PCM controls on ac97 of Audigy has no effect */
  691. "PCM Playback Switch",
  692. "PCM Playback Volume",
  693. "Master Mono Playback Switch",
  694. "Master Mono Playback Volume",
  695. "Master Playback Switch",
  696. "Master Playback Volume",
  697. "PCM Out Path & Mute",
  698. "Mono Output Select",
  699. /* remove unused AC97 capture controls */
  700. "Capture Source",
  701. "Capture Switch",
  702. "Capture Volume",
  703. "Mic Select",
  704. "Video Playback Switch",
  705. "Video Playback Volume",
  706. "Mic Playback Switch",
  707. "Mic Playback Volume",
  708. NULL
  709. };
  710. static char *audigy_rename_ctls[] = {
  711. /* use conventional names */
  712. "Wave Playback Volume", "PCM Playback Volume",
  713. /* "Wave Capture Volume", "PCM Capture Volume", */
  714. "Wave Master Playback Volume", "Master Playback Volume",
  715. "AMic Playback Volume", "Mic Playback Volume",
  716. NULL
  717. };
  718. if (!emu->no_ac97) {
  719. ac97_bus_t *pbus;
  720. ac97_template_t ac97;
  721. static ac97_bus_ops_t ops = {
  722. .write = snd_emu10k1_ac97_write,
  723. .read = snd_emu10k1_ac97_read,
  724. };
  725. if ((err = snd_ac97_bus(emu->card, 0, &ops, NULL, &pbus)) < 0)
  726. return err;
  727. pbus->no_vra = 1; /* we don't need VRA */
  728. memset(&ac97, 0, sizeof(ac97));
  729. ac97.private_data = emu;
  730. ac97.private_free = snd_emu10k1_mixer_free_ac97;
  731. ac97.scaps = AC97_SCAP_NO_SPDIF;
  732. if ((err = snd_ac97_mixer(pbus, &ac97, &emu->ac97)) < 0)
  733. return err;
  734. if (emu->audigy) {
  735. /* set master volume to 0 dB */
  736. snd_ac97_write(emu->ac97, AC97_MASTER, 0x0000);
  737. /* set capture source to mic */
  738. snd_ac97_write(emu->ac97, AC97_REC_SEL, 0x0000);
  739. c = audigy_remove_ctls;
  740. } else {
  741. /*
  742. * Credits for cards based on STAC9758:
  743. * James Courtier-Dutton <James@superbug.demon.co.uk>
  744. * Voluspa <voluspa@comhem.se>
  745. */
  746. if (emu->ac97->id == AC97_ID_STAC9758) {
  747. emu->rear_ac97 = 1;
  748. snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
  749. }
  750. /* remove unused AC97 controls */
  751. snd_ac97_write(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
  752. snd_ac97_write(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
  753. c = emu10k1_remove_ctls;
  754. }
  755. for (; *c; c++)
  756. remove_ctl(card, *c);
  757. } else {
  758. if (emu->APS)
  759. strcpy(emu->card->mixername, "EMU APS");
  760. else if (emu->audigy)
  761. strcpy(emu->card->mixername, "SB Audigy");
  762. else
  763. strcpy(emu->card->mixername, "Emu10k1");
  764. }
  765. if (emu->audigy)
  766. c = audigy_rename_ctls;
  767. else
  768. c = emu10k1_rename_ctls;
  769. for (; *c; c += 2)
  770. rename_ctl(card, c[0], c[1]);
  771. if ((kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu)) == NULL)
  772. return -ENOMEM;
  773. if ((err = snd_ctl_add(card, kctl)))
  774. return err;
  775. if ((kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu)) == NULL)
  776. return -ENOMEM;
  777. if ((err = snd_ctl_add(card, kctl)))
  778. return err;
  779. if ((kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu)) == NULL)
  780. return -ENOMEM;
  781. if ((err = snd_ctl_add(card, kctl)))
  782. return err;
  783. if ((kctl = emu->ctl_efx_send_routing = snd_ctl_new1(&snd_emu10k1_efx_send_routing_control, emu)) == NULL)
  784. return -ENOMEM;
  785. if ((err = snd_ctl_add(card, kctl)))
  786. return err;
  787. if ((kctl = emu->ctl_efx_send_volume = snd_ctl_new1(&snd_emu10k1_efx_send_volume_control, emu)) == NULL)
  788. return -ENOMEM;
  789. if ((err = snd_ctl_add(card, kctl)))
  790. return err;
  791. if ((kctl = emu->ctl_efx_attn = snd_ctl_new1(&snd_emu10k1_efx_attn_control, emu)) == NULL)
  792. return -ENOMEM;
  793. if ((err = snd_ctl_add(card, kctl)))
  794. return err;
  795. /* initialize the routing and volume table for each pcm playback stream */
  796. for (pcm = 0; pcm < 32; pcm++) {
  797. emu10k1_pcm_mixer_t *mix;
  798. int v;
  799. mix = &emu->pcm_mixer[pcm];
  800. mix->epcm = NULL;
  801. for (v = 0; v < 4; v++)
  802. mix->send_routing[0][v] =
  803. mix->send_routing[1][v] =
  804. mix->send_routing[2][v] = v;
  805. memset(&mix->send_volume, 0, sizeof(mix->send_volume));
  806. mix->send_volume[0][0] = mix->send_volume[0][1] =
  807. mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
  808. mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
  809. }
  810. /* initialize the routing and volume table for the multichannel playback stream */
  811. for (pcm = 0; pcm < NUM_EFX_PLAYBACK; pcm++) {
  812. emu10k1_pcm_mixer_t *mix;
  813. int v;
  814. mix = &emu->efx_pcm_mixer[pcm];
  815. mix->epcm = NULL;
  816. mix->send_routing[0][0] = pcm;
  817. mix->send_routing[0][1] = (pcm == 0) ? 1 : 0;
  818. for (v = 0; v < 2; v++)
  819. mix->send_routing[0][2+v] = 13+v;
  820. if (emu->audigy)
  821. for (v = 0; v < 4; v++)
  822. mix->send_routing[0][4+v] = 60+v;
  823. memset(&mix->send_volume, 0, sizeof(mix->send_volume));
  824. mix->send_volume[0][0] = 255;
  825. mix->attn[0] = 0xffff;
  826. }
  827. if (! emu->APS) { /* FIXME: APS has these controls? */
  828. /* sb live! and audigy */
  829. if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu)) == NULL)
  830. return -ENOMEM;
  831. if ((err = snd_ctl_add(card, kctl)))
  832. return err;
  833. if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu)) == NULL)
  834. return -ENOMEM;
  835. if ((err = snd_ctl_add(card, kctl)))
  836. return err;
  837. }
  838. if (emu->audigy) {
  839. if ((kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu)) == NULL)
  840. return -ENOMEM;
  841. if ((err = snd_ctl_add(card, kctl)))
  842. return err;
  843. if ((kctl = snd_ctl_new1(&snd_audigy_spdif_output_rate, emu)) == NULL)
  844. return -ENOMEM;
  845. if ((err = snd_ctl_add(card, kctl)))
  846. return err;
  847. } else if (! emu->APS) {
  848. /* sb live! */
  849. if ((kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu)) == NULL)
  850. return -ENOMEM;
  851. if ((err = snd_ctl_add(card, kctl)))
  852. return err;
  853. }
  854. if (emu->audigy && emu->revision == 4) { /* P16V */
  855. if ((err = snd_p16v_mixer(emu)))
  856. return err;
  857. }
  858. return 0;
  859. }