pas2_mixer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * sound/oss/pas2_mixer.c
  3. *
  4. * Mixer routines for the Pro Audio Spectrum cards.
  5. */
  6. /*
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. */
  13. /*
  14. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  15. * Bartlomiej Zolnierkiewicz : added __init to pas_init_mixer()
  16. */
  17. #include <linux/init.h>
  18. #include "sound_config.h"
  19. #include "pas2.h"
  20. extern int pas_translate_code;
  21. extern char pas_model;
  22. extern int *pas_osp;
  23. extern int pas_audiodev;
  24. static int rec_devices = (SOUND_MASK_MIC); /* Default recording source */
  25. static int mode_control;
  26. #define POSSIBLE_RECORDING_DEVICES (SOUND_MASK_SYNTH | SOUND_MASK_SPEAKER | SOUND_MASK_LINE | SOUND_MASK_MIC | \
  27. SOUND_MASK_CD | SOUND_MASK_ALTPCM)
  28. #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_SPEAKER | SOUND_MASK_LINE | SOUND_MASK_MIC | \
  29. SOUND_MASK_CD | SOUND_MASK_ALTPCM | SOUND_MASK_IMIX | \
  30. SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_RECLEV)
  31. static int *levels;
  32. static int default_levels[32] =
  33. {
  34. 0x3232, /* Master Volume */
  35. 0x3232, /* Bass */
  36. 0x3232, /* Treble */
  37. 0x5050, /* FM */
  38. 0x4b4b, /* PCM */
  39. 0x3232, /* PC Speaker */
  40. 0x4b4b, /* Ext Line */
  41. 0x4b4b, /* Mic */
  42. 0x4b4b, /* CD */
  43. 0x6464, /* Recording monitor */
  44. 0x4b4b, /* SB PCM */
  45. 0x6464 /* Recording level */
  46. };
  47. void
  48. mix_write(unsigned char data, int ioaddr)
  49. {
  50. /*
  51. * The Revision D cards have a problem with their MVA508 interface. The
  52. * kludge-o-rama fix is to make a 16-bit quantity with identical LSB and
  53. * MSBs out of the output byte and to do a 16-bit out to the mixer port -
  54. * 1. We need to do this because it isn't timing problem but chip access
  55. * sequence problem.
  56. */
  57. if (pas_model == 4)
  58. {
  59. outw(data | (data << 8), (ioaddr + pas_translate_code) - 1);
  60. outb((0x80), 0);
  61. } else
  62. pas_write(data, ioaddr);
  63. }
  64. static int
  65. mixer_output(int right_vol, int left_vol, int div, int bits,
  66. int mixer) /* Input or output mixer */
  67. {
  68. int left = left_vol * div / 100;
  69. int right = right_vol * div / 100;
  70. if (bits & 0x10)
  71. {
  72. left |= mixer;
  73. right |= mixer;
  74. }
  75. if (bits == 0x03 || bits == 0x04)
  76. {
  77. mix_write(0x80 | bits, 0x078B);
  78. mix_write(left, 0x078B);
  79. right_vol = left_vol;
  80. } else
  81. {
  82. mix_write(0x80 | 0x20 | bits, 0x078B);
  83. mix_write(left, 0x078B);
  84. mix_write(0x80 | 0x40 | bits, 0x078B);
  85. mix_write(right, 0x078B);
  86. }
  87. return (left_vol | (right_vol << 8));
  88. }
  89. static void
  90. set_mode(int new_mode)
  91. {
  92. mix_write(0x80 | 0x05, 0x078B);
  93. mix_write(new_mode, 0x078B);
  94. mode_control = new_mode;
  95. }
  96. static int
  97. pas_mixer_set(int whichDev, unsigned int level)
  98. {
  99. int left, right, devmask, changed, i, mixer = 0;
  100. left = level & 0x7f;
  101. right = (level & 0x7f00) >> 8;
  102. if (whichDev < SOUND_MIXER_NRDEVICES) {
  103. if ((1 << whichDev) & rec_devices)
  104. mixer = 0x20;
  105. else
  106. mixer = 0x00;
  107. }
  108. switch (whichDev)
  109. {
  110. case SOUND_MIXER_VOLUME: /* Master volume (0-63) */
  111. levels[whichDev] = mixer_output(right, left, 63, 0x01, 0);
  112. break;
  113. /*
  114. * Note! Bass and Treble are mono devices. Will use just the left
  115. * channel.
  116. */
  117. case SOUND_MIXER_BASS: /* Bass (0-12) */
  118. levels[whichDev] = mixer_output(right, left, 12, 0x03, 0);
  119. break;
  120. case SOUND_MIXER_TREBLE: /* Treble (0-12) */
  121. levels[whichDev] = mixer_output(right, left, 12, 0x04, 0);
  122. break;
  123. case SOUND_MIXER_SYNTH: /* Internal synthesizer (0-31) */
  124. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x00, mixer);
  125. break;
  126. case SOUND_MIXER_PCM: /* PAS PCM (0-31) */
  127. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x05, mixer);
  128. break;
  129. case SOUND_MIXER_ALTPCM: /* SB PCM (0-31) */
  130. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x07, mixer);
  131. break;
  132. case SOUND_MIXER_SPEAKER: /* PC speaker (0-31) */
  133. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x06, mixer);
  134. break;
  135. case SOUND_MIXER_LINE: /* External line (0-31) */
  136. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x02, mixer);
  137. break;
  138. case SOUND_MIXER_CD: /* CD (0-31) */
  139. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x03, mixer);
  140. break;
  141. case SOUND_MIXER_MIC: /* External microphone (0-31) */
  142. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x04, mixer);
  143. break;
  144. case SOUND_MIXER_IMIX: /* Recording monitor (0-31) (Output mixer only) */
  145. levels[whichDev] = mixer_output(right, left, 31, 0x10 | 0x01,
  146. 0x00);
  147. break;
  148. case SOUND_MIXER_RECLEV: /* Recording level (0-15) */
  149. levels[whichDev] = mixer_output(right, left, 15, 0x02, 0);
  150. break;
  151. case SOUND_MIXER_RECSRC:
  152. devmask = level & POSSIBLE_RECORDING_DEVICES;
  153. changed = devmask ^ rec_devices;
  154. rec_devices = devmask;
  155. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  156. if (changed & (1 << i))
  157. {
  158. pas_mixer_set(i, levels[i]);
  159. }
  160. return rec_devices;
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. return (levels[whichDev]);
  166. }
  167. /*****/
  168. static void
  169. pas_mixer_reset(void)
  170. {
  171. int foo;
  172. for (foo = 0; foo < SOUND_MIXER_NRDEVICES; foo++)
  173. pas_mixer_set(foo, levels[foo]);
  174. set_mode(0x04 | 0x01);
  175. }
  176. static int pas_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  177. {
  178. int level,v ;
  179. int __user *p = (int __user *)arg;
  180. if (cmd == SOUND_MIXER_PRIVATE1) { /* Set loudness bit */
  181. if (get_user(level, p))
  182. return -EFAULT;
  183. if (level == -1) /* Return current settings */
  184. level = (mode_control & 0x04);
  185. else {
  186. mode_control &= ~0x04;
  187. if (level)
  188. mode_control |= 0x04;
  189. set_mode(mode_control);
  190. }
  191. level = !!level;
  192. return put_user(level, p);
  193. }
  194. if (cmd == SOUND_MIXER_PRIVATE2) { /* Set enhance bit */
  195. if (get_user(level, p))
  196. return -EFAULT;
  197. if (level == -1) { /* Return current settings */
  198. if (!(mode_control & 0x03))
  199. level = 0;
  200. else
  201. level = ((mode_control & 0x03) + 1) * 20;
  202. } else {
  203. int i = 0;
  204. level &= 0x7f;
  205. if (level)
  206. i = (level / 20) - 1;
  207. mode_control &= ~0x03;
  208. mode_control |= i & 0x03;
  209. set_mode(mode_control);
  210. if (i)
  211. i = (i + 1) * 20;
  212. level = i;
  213. }
  214. return put_user(level, p);
  215. }
  216. if (cmd == SOUND_MIXER_PRIVATE3) { /* Set mute bit */
  217. if (get_user(level, p))
  218. return -EFAULT;
  219. if (level == -1) /* Return current settings */
  220. level = !(pas_read(0x0B8A) & 0x20);
  221. else {
  222. if (level)
  223. pas_write(pas_read(0x0B8A) & (~0x20), 0x0B8A);
  224. else
  225. pas_write(pas_read(0x0B8A) | 0x20, 0x0B8A);
  226. level = !(pas_read(0x0B8A) & 0x20);
  227. }
  228. return put_user(level, p);
  229. }
  230. if (((cmd >> 8) & 0xff) == 'M') {
  231. if (get_user(v, p))
  232. return -EFAULT;
  233. if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
  234. v = pas_mixer_set(cmd & 0xff, v);
  235. } else {
  236. switch (cmd & 0xff) {
  237. case SOUND_MIXER_RECSRC:
  238. v = rec_devices;
  239. break;
  240. case SOUND_MIXER_STEREODEVS:
  241. v = SUPPORTED_MIXER_DEVICES & ~(SOUND_MASK_BASS | SOUND_MASK_TREBLE);
  242. break;
  243. case SOUND_MIXER_DEVMASK:
  244. v = SUPPORTED_MIXER_DEVICES;
  245. break;
  246. case SOUND_MIXER_RECMASK:
  247. v = POSSIBLE_RECORDING_DEVICES & SUPPORTED_MIXER_DEVICES;
  248. break;
  249. case SOUND_MIXER_CAPS:
  250. v = 0; /* No special capabilities */
  251. break;
  252. default:
  253. v = levels[cmd & 0xff];
  254. break;
  255. }
  256. }
  257. return put_user(v, p);
  258. }
  259. return -EINVAL;
  260. }
  261. static struct mixer_operations pas_mixer_operations =
  262. {
  263. .owner = THIS_MODULE,
  264. .id = "PAS16",
  265. .name = "Pro Audio Spectrum 16",
  266. .ioctl = pas_mixer_ioctl
  267. };
  268. int __init
  269. pas_init_mixer(void)
  270. {
  271. int d;
  272. levels = load_mixer_volumes("PAS16_1", default_levels, 1);
  273. pas_mixer_reset();
  274. if ((d = sound_alloc_mixerdev()) != -1)
  275. {
  276. audio_devs[pas_audiodev]->mixer_dev = d;
  277. mixer_devs[d] = &pas_mixer_operations;
  278. }
  279. return 1;
  280. }