pas2_mixer.c 8.2 KB

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