emumixer.c 29 KB

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