sb_mixer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * sound/oss/sb_mixer.c
  3. *
  4. * The low level mixer driver for the Sound Blaster compatible 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. * Rolf Fokkens (Dec 20 1998) : Moved ESS stuff into sb_ess.[ch]
  16. * Stanislav Voronyi <stas@esc.kharkov.com> : Support for AWE 3DSE device (Jun 7 1999)
  17. */
  18. #include <linux/slab.h>
  19. #include "sound_config.h"
  20. #define __SB_MIXER_C__
  21. #include "sb.h"
  22. #include "sb_mixer.h"
  23. #include "sb_ess.h"
  24. #define SBPRO_RECORDING_DEVICES (SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD)
  25. /* Same as SB Pro, unless I find otherwise */
  26. #define SGNXPRO_RECORDING_DEVICES SBPRO_RECORDING_DEVICES
  27. #define SBPRO_MIXER_DEVICES (SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE | SOUND_MASK_MIC | \
  28. SOUND_MASK_CD | SOUND_MASK_VOLUME)
  29. /* SG NX Pro has treble and bass settings on the mixer. The 'speaker'
  30. * channel is the COVOX/DisneySoundSource emulation volume control
  31. * on the mixer. It does NOT control speaker volume. Should have own
  32. * mask eventually?
  33. */
  34. #define SGNXPRO_MIXER_DEVICES (SBPRO_MIXER_DEVICES|SOUND_MASK_BASS| \
  35. SOUND_MASK_TREBLE|SOUND_MASK_SPEAKER )
  36. #define SB16_RECORDING_DEVICES (SOUND_MASK_SYNTH | SOUND_MASK_LINE | SOUND_MASK_MIC | \
  37. SOUND_MASK_CD)
  38. #define SB16_OUTFILTER_DEVICES (SOUND_MASK_LINE | SOUND_MASK_MIC | \
  39. SOUND_MASK_CD)
  40. #define SB16_MIXER_DEVICES (SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_SPEAKER | SOUND_MASK_LINE | SOUND_MASK_MIC | \
  41. SOUND_MASK_CD | \
  42. SOUND_MASK_IGAIN | SOUND_MASK_OGAIN | \
  43. SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE | \
  44. SOUND_MASK_IMIX)
  45. /* These are the only devices that are working at the moment. Others could
  46. * be added once they are identified and a method is found to control them.
  47. */
  48. #define ALS007_MIXER_DEVICES (SOUND_MASK_SYNTH | SOUND_MASK_LINE | \
  49. SOUND_MASK_PCM | SOUND_MASK_MIC | \
  50. SOUND_MASK_CD | \
  51. SOUND_MASK_VOLUME)
  52. static mixer_tab sbpro_mix = {
  53. MIX_ENT(SOUND_MIXER_VOLUME, 0x22, 7, 4, 0x22, 3, 4),
  54. MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
  55. MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
  56. MIX_ENT(SOUND_MIXER_SYNTH, 0x26, 7, 4, 0x26, 3, 4),
  57. MIX_ENT(SOUND_MIXER_PCM, 0x04, 7, 4, 0x04, 3, 4),
  58. MIX_ENT(SOUND_MIXER_SPEAKER, 0x00, 0, 0, 0x00, 0, 0),
  59. MIX_ENT(SOUND_MIXER_LINE, 0x2e, 7, 4, 0x2e, 3, 4),
  60. MIX_ENT(SOUND_MIXER_MIC, 0x0a, 2, 3, 0x00, 0, 0),
  61. MIX_ENT(SOUND_MIXER_CD, 0x28, 7, 4, 0x28, 3, 4),
  62. MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
  63. MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
  64. MIX_ENT(SOUND_MIXER_RECLEV, 0x00, 0, 0, 0x00, 0, 0)
  65. };
  66. static mixer_tab sb16_mix = {
  67. MIX_ENT(SOUND_MIXER_VOLUME, 0x30, 7, 5, 0x31, 7, 5),
  68. MIX_ENT(SOUND_MIXER_BASS, 0x46, 7, 4, 0x47, 7, 4),
  69. MIX_ENT(SOUND_MIXER_TREBLE, 0x44, 7, 4, 0x45, 7, 4),
  70. MIX_ENT(SOUND_MIXER_SYNTH, 0x34, 7, 5, 0x35, 7, 5),
  71. MIX_ENT(SOUND_MIXER_PCM, 0x32, 7, 5, 0x33, 7, 5),
  72. MIX_ENT(SOUND_MIXER_SPEAKER, 0x3b, 7, 2, 0x00, 0, 0),
  73. MIX_ENT(SOUND_MIXER_LINE, 0x38, 7, 5, 0x39, 7, 5),
  74. MIX_ENT(SOUND_MIXER_MIC, 0x3a, 7, 5, 0x00, 0, 0),
  75. MIX_ENT(SOUND_MIXER_CD, 0x36, 7, 5, 0x37, 7, 5),
  76. MIX_ENT(SOUND_MIXER_IMIX, 0x3c, 0, 1, 0x00, 0, 0),
  77. MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
  78. MIX_ENT(SOUND_MIXER_RECLEV, 0x3f, 7, 2, 0x40, 7, 2), /* Obsolete. Use IGAIN */
  79. MIX_ENT(SOUND_MIXER_IGAIN, 0x3f, 7, 2, 0x40, 7, 2),
  80. MIX_ENT(SOUND_MIXER_OGAIN, 0x41, 7, 2, 0x42, 7, 2)
  81. };
  82. static mixer_tab als007_mix =
  83. {
  84. MIX_ENT(SOUND_MIXER_VOLUME, 0x62, 7, 4, 0x62, 3, 4),
  85. MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
  86. MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
  87. MIX_ENT(SOUND_MIXER_SYNTH, 0x66, 7, 4, 0x66, 3, 4),
  88. MIX_ENT(SOUND_MIXER_PCM, 0x64, 7, 4, 0x64, 3, 4),
  89. MIX_ENT(SOUND_MIXER_SPEAKER, 0x00, 0, 0, 0x00, 0, 0),
  90. MIX_ENT(SOUND_MIXER_LINE, 0x6e, 7, 4, 0x6e, 3, 4),
  91. MIX_ENT(SOUND_MIXER_MIC, 0x6a, 2, 3, 0x00, 0, 0),
  92. MIX_ENT(SOUND_MIXER_CD, 0x68, 7, 4, 0x68, 3, 4),
  93. MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
  94. MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
  95. MIX_ENT(SOUND_MIXER_RECLEV, 0x00, 0, 0, 0x00, 0, 0), /* Obsolete. Use IGAIN */
  96. MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
  97. MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0)
  98. };
  99. /* SM_GAMES Master volume is lower and PCM & FM volumes
  100. higher than with SB Pro. This improves the
  101. sound quality */
  102. static int smg_default_levels[32] =
  103. {
  104. 0x2020, /* Master Volume */
  105. 0x4b4b, /* Bass */
  106. 0x4b4b, /* Treble */
  107. 0x6464, /* FM */
  108. 0x6464, /* PCM */
  109. 0x4b4b, /* PC Speaker */
  110. 0x4b4b, /* Ext Line */
  111. 0x0000, /* Mic */
  112. 0x4b4b, /* CD */
  113. 0x4b4b, /* Recording monitor */
  114. 0x4b4b, /* SB PCM */
  115. 0x4b4b, /* Recording level */
  116. 0x4b4b, /* Input gain */
  117. 0x4b4b, /* Output gain */
  118. 0x4040, /* Line1 */
  119. 0x4040, /* Line2 */
  120. 0x1515 /* Line3 */
  121. };
  122. static int sb_default_levels[32] =
  123. {
  124. 0x5a5a, /* Master Volume */
  125. 0x4b4b, /* Bass */
  126. 0x4b4b, /* Treble */
  127. 0x4b4b, /* FM */
  128. 0x4b4b, /* PCM */
  129. 0x4b4b, /* PC Speaker */
  130. 0x4b4b, /* Ext Line */
  131. 0x1010, /* Mic */
  132. 0x4b4b, /* CD */
  133. 0x0000, /* Recording monitor */
  134. 0x4b4b, /* SB PCM */
  135. 0x4b4b, /* Recording level */
  136. 0x4b4b, /* Input gain */
  137. 0x4b4b, /* Output gain */
  138. 0x4040, /* Line1 */
  139. 0x4040, /* Line2 */
  140. 0x1515 /* Line3 */
  141. };
  142. static unsigned char sb16_recmasks_L[SOUND_MIXER_NRDEVICES] =
  143. {
  144. 0x00, /* SOUND_MIXER_VOLUME */
  145. 0x00, /* SOUND_MIXER_BASS */
  146. 0x00, /* SOUND_MIXER_TREBLE */
  147. 0x40, /* SOUND_MIXER_SYNTH */
  148. 0x00, /* SOUND_MIXER_PCM */
  149. 0x00, /* SOUND_MIXER_SPEAKER */
  150. 0x10, /* SOUND_MIXER_LINE */
  151. 0x01, /* SOUND_MIXER_MIC */
  152. 0x04, /* SOUND_MIXER_CD */
  153. 0x00, /* SOUND_MIXER_IMIX */
  154. 0x00, /* SOUND_MIXER_ALTPCM */
  155. 0x00, /* SOUND_MIXER_RECLEV */
  156. 0x00, /* SOUND_MIXER_IGAIN */
  157. 0x00 /* SOUND_MIXER_OGAIN */
  158. };
  159. static unsigned char sb16_recmasks_R[SOUND_MIXER_NRDEVICES] =
  160. {
  161. 0x00, /* SOUND_MIXER_VOLUME */
  162. 0x00, /* SOUND_MIXER_BASS */
  163. 0x00, /* SOUND_MIXER_TREBLE */
  164. 0x20, /* SOUND_MIXER_SYNTH */
  165. 0x00, /* SOUND_MIXER_PCM */
  166. 0x00, /* SOUND_MIXER_SPEAKER */
  167. 0x08, /* SOUND_MIXER_LINE */
  168. 0x01, /* SOUND_MIXER_MIC */
  169. 0x02, /* SOUND_MIXER_CD */
  170. 0x00, /* SOUND_MIXER_IMIX */
  171. 0x00, /* SOUND_MIXER_ALTPCM */
  172. 0x00, /* SOUND_MIXER_RECLEV */
  173. 0x00, /* SOUND_MIXER_IGAIN */
  174. 0x00 /* SOUND_MIXER_OGAIN */
  175. };
  176. static char smw_mix_regs[] = /* Left mixer registers */
  177. {
  178. 0x0b, /* SOUND_MIXER_VOLUME */
  179. 0x0d, /* SOUND_MIXER_BASS */
  180. 0x0d, /* SOUND_MIXER_TREBLE */
  181. 0x05, /* SOUND_MIXER_SYNTH */
  182. 0x09, /* SOUND_MIXER_PCM */
  183. 0x00, /* SOUND_MIXER_SPEAKER */
  184. 0x03, /* SOUND_MIXER_LINE */
  185. 0x01, /* SOUND_MIXER_MIC */
  186. 0x07, /* SOUND_MIXER_CD */
  187. 0x00, /* SOUND_MIXER_IMIX */
  188. 0x00, /* SOUND_MIXER_ALTPCM */
  189. 0x00, /* SOUND_MIXER_RECLEV */
  190. 0x00, /* SOUND_MIXER_IGAIN */
  191. 0x00, /* SOUND_MIXER_OGAIN */
  192. 0x00, /* SOUND_MIXER_LINE1 */
  193. 0x00, /* SOUND_MIXER_LINE2 */
  194. 0x00 /* SOUND_MIXER_LINE3 */
  195. };
  196. static int sbmixnum = 1;
  197. static void sb_mixer_reset(sb_devc * devc);
  198. void sb_mixer_set_stereo(sb_devc * devc, int mode)
  199. {
  200. sb_chgmixer(devc, OUT_FILTER, STEREO_DAC, (mode ? STEREO_DAC : MONO_DAC));
  201. }
  202. static int detect_mixer(sb_devc * devc)
  203. {
  204. /* Just trust the mixer is there */
  205. return 1;
  206. }
  207. static void oss_change_bits(sb_devc *devc, unsigned char *regval, int dev, int chn, int newval)
  208. {
  209. unsigned char mask;
  210. int shift;
  211. mask = (1 << (*devc->iomap)[dev][chn].nbits) - 1;
  212. newval = (int) ((newval * mask) + 50) / 100; /* Scale */
  213. shift = (*devc->iomap)[dev][chn].bitoffs - (*devc->iomap)[dev][LEFT_CHN].nbits + 1;
  214. *regval &= ~(mask << shift); /* Mask out previous value */
  215. *regval |= (newval & mask) << shift; /* Set the new value */
  216. }
  217. static int sb_mixer_get(sb_devc * devc, int dev)
  218. {
  219. if (!((1 << dev) & devc->supported_devices))
  220. return -EINVAL;
  221. return devc->levels[dev];
  222. }
  223. void smw_mixer_init(sb_devc * devc)
  224. {
  225. int i;
  226. sb_setmixer(devc, 0x00, 0x18); /* Mute unused (Telephone) line */
  227. sb_setmixer(devc, 0x10, 0x38); /* Config register 2 */
  228. devc->supported_devices = 0;
  229. for (i = 0; i < sizeof(smw_mix_regs); i++)
  230. if (smw_mix_regs[i] != 0)
  231. devc->supported_devices |= (1 << i);
  232. devc->supported_rec_devices = devc->supported_devices &
  233. ~(SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_PCM | SOUND_MASK_VOLUME);
  234. sb_mixer_reset(devc);
  235. }
  236. int sb_common_mixer_set(sb_devc * devc, int dev, int left, int right)
  237. {
  238. int regoffs;
  239. unsigned char val;
  240. if ((dev < 0) || (dev >= devc->iomap_sz))
  241. return -EINVAL;
  242. regoffs = (*devc->iomap)[dev][LEFT_CHN].regno;
  243. if (regoffs == 0)
  244. return -EINVAL;
  245. val = sb_getmixer(devc, regoffs);
  246. oss_change_bits(devc, &val, dev, LEFT_CHN, left);
  247. if ((*devc->iomap)[dev][RIGHT_CHN].regno != regoffs) /*
  248. * Change register
  249. */
  250. {
  251. sb_setmixer(devc, regoffs, val); /*
  252. * Save the old one
  253. */
  254. regoffs = (*devc->iomap)[dev][RIGHT_CHN].regno;
  255. if (regoffs == 0)
  256. return left | (left << 8); /*
  257. * Just left channel present
  258. */
  259. val = sb_getmixer(devc, regoffs); /*
  260. * Read the new one
  261. */
  262. }
  263. oss_change_bits(devc, &val, dev, RIGHT_CHN, right);
  264. sb_setmixer(devc, regoffs, val);
  265. return left | (right << 8);
  266. }
  267. static int smw_mixer_set(sb_devc * devc, int dev, int left, int right)
  268. {
  269. int reg, val;
  270. switch (dev)
  271. {
  272. case SOUND_MIXER_VOLUME:
  273. sb_setmixer(devc, 0x0b, 96 - (96 * left / 100)); /* 96=mute, 0=max */
  274. sb_setmixer(devc, 0x0c, 96 - (96 * right / 100));
  275. break;
  276. case SOUND_MIXER_BASS:
  277. case SOUND_MIXER_TREBLE:
  278. devc->levels[dev] = left | (right << 8);
  279. /* Set left bass and treble values */
  280. val = ((devc->levels[SOUND_MIXER_TREBLE] & 0xff) * 16 / (unsigned) 100) << 4;
  281. val |= ((devc->levels[SOUND_MIXER_BASS] & 0xff) * 16 / (unsigned) 100) & 0x0f;
  282. sb_setmixer(devc, 0x0d, val);
  283. /* Set right bass and treble values */
  284. val = (((devc->levels[SOUND_MIXER_TREBLE] >> 8) & 0xff) * 16 / (unsigned) 100) << 4;
  285. val |= (((devc->levels[SOUND_MIXER_BASS] >> 8) & 0xff) * 16 / (unsigned) 100) & 0x0f;
  286. sb_setmixer(devc, 0x0e, val);
  287. break;
  288. default:
  289. /* bounds check */
  290. if (dev < 0 || dev >= ARRAY_SIZE(smw_mix_regs))
  291. return -EINVAL;
  292. reg = smw_mix_regs[dev];
  293. if (reg == 0)
  294. return -EINVAL;
  295. sb_setmixer(devc, reg, (24 - (24 * left / 100)) | 0x20); /* 24=mute, 0=max */
  296. sb_setmixer(devc, reg + 1, (24 - (24 * right / 100)) | 0x40);
  297. }
  298. devc->levels[dev] = left | (right << 8);
  299. return left | (right << 8);
  300. }
  301. static int sb_mixer_set(sb_devc * devc, int dev, int value)
  302. {
  303. int left = value & 0x000000ff;
  304. int right = (value & 0x0000ff00) >> 8;
  305. int retval;
  306. if (left > 100)
  307. left = 100;
  308. if (right > 100)
  309. right = 100;
  310. if ((dev < 0) || (dev > 31))
  311. return -EINVAL;
  312. if (!(devc->supported_devices & (1 << dev))) /*
  313. * Not supported
  314. */
  315. return -EINVAL;
  316. /* Differentiate depending on the chipsets */
  317. switch (devc->model) {
  318. case MDL_SMW:
  319. retval = smw_mixer_set(devc, dev, left, right);
  320. break;
  321. case MDL_ESS:
  322. retval = ess_mixer_set(devc, dev, left, right);
  323. break;
  324. default:
  325. retval = sb_common_mixer_set(devc, dev, left, right);
  326. }
  327. if (retval >= 0) devc->levels[dev] = retval;
  328. return retval;
  329. }
  330. /*
  331. * set_recsrc doesn't apply to ES188x
  332. */
  333. static void set_recsrc(sb_devc * devc, int src)
  334. {
  335. sb_setmixer(devc, RECORD_SRC, (sb_getmixer(devc, RECORD_SRC) & ~7) | (src & 0x7));
  336. }
  337. static int set_recmask(sb_devc * devc, int mask)
  338. {
  339. int devmask, i;
  340. unsigned char regimageL, regimageR;
  341. devmask = mask & devc->supported_rec_devices;
  342. switch (devc->model)
  343. {
  344. case MDL_SBPRO:
  345. case MDL_ESS:
  346. case MDL_JAZZ:
  347. case MDL_SMW:
  348. if (devc->model == MDL_ESS && ess_set_recmask (devc, &devmask)) {
  349. break;
  350. }
  351. if (devmask != SOUND_MASK_MIC &&
  352. devmask != SOUND_MASK_LINE &&
  353. devmask != SOUND_MASK_CD)
  354. {
  355. /*
  356. * More than one device selected. Drop the
  357. * previous selection
  358. */
  359. devmask &= ~devc->recmask;
  360. }
  361. if (devmask != SOUND_MASK_MIC &&
  362. devmask != SOUND_MASK_LINE &&
  363. devmask != SOUND_MASK_CD)
  364. {
  365. /*
  366. * More than one device selected. Default to
  367. * mic
  368. */
  369. devmask = SOUND_MASK_MIC;
  370. }
  371. if (devmask ^ devc->recmask) /*
  372. * Input source changed
  373. */
  374. {
  375. switch (devmask)
  376. {
  377. case SOUND_MASK_MIC:
  378. set_recsrc(devc, SRC__MIC);
  379. break;
  380. case SOUND_MASK_LINE:
  381. set_recsrc(devc, SRC__LINE);
  382. break;
  383. case SOUND_MASK_CD:
  384. set_recsrc(devc, SRC__CD);
  385. break;
  386. default:
  387. set_recsrc(devc, SRC__MIC);
  388. }
  389. }
  390. break;
  391. case MDL_SB16:
  392. if (!devmask)
  393. devmask = SOUND_MASK_MIC;
  394. if (devc->submodel == SUBMDL_ALS007)
  395. {
  396. switch (devmask)
  397. {
  398. case SOUND_MASK_LINE:
  399. sb_setmixer(devc, ALS007_RECORD_SRC, ALS007_LINE);
  400. break;
  401. case SOUND_MASK_CD:
  402. sb_setmixer(devc, ALS007_RECORD_SRC, ALS007_CD);
  403. break;
  404. case SOUND_MASK_SYNTH:
  405. sb_setmixer(devc, ALS007_RECORD_SRC, ALS007_SYNTH);
  406. break;
  407. default: /* Also takes care of SOUND_MASK_MIC case */
  408. sb_setmixer(devc, ALS007_RECORD_SRC, ALS007_MIC);
  409. break;
  410. }
  411. }
  412. else
  413. {
  414. regimageL = regimageR = 0;
  415. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  416. {
  417. if ((1 << i) & devmask)
  418. {
  419. regimageL |= sb16_recmasks_L[i];
  420. regimageR |= sb16_recmasks_R[i];
  421. }
  422. sb_setmixer (devc, SB16_IMASK_L, regimageL);
  423. sb_setmixer (devc, SB16_IMASK_R, regimageR);
  424. }
  425. }
  426. break;
  427. }
  428. devc->recmask = devmask;
  429. return devc->recmask;
  430. }
  431. static int set_outmask(sb_devc * devc, int mask)
  432. {
  433. int devmask, i;
  434. unsigned char regimage;
  435. devmask = mask & devc->supported_out_devices;
  436. switch (devc->model)
  437. {
  438. case MDL_SB16:
  439. if (devc->submodel == SUBMDL_ALS007)
  440. break;
  441. else
  442. {
  443. regimage = 0;
  444. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  445. {
  446. if ((1 << i) & devmask)
  447. {
  448. regimage |= (sb16_recmasks_L[i] | sb16_recmasks_R[i]);
  449. }
  450. sb_setmixer (devc, SB16_OMASK, regimage);
  451. }
  452. }
  453. break;
  454. default:
  455. break;
  456. }
  457. devc->outmask = devmask;
  458. return devc->outmask;
  459. }
  460. static int sb_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  461. {
  462. sb_devc *devc = mixer_devs[dev]->devc;
  463. int val, ret;
  464. int __user *p = arg;
  465. /*
  466. * Use ioctl(fd, SOUND_MIXER_AGC, &mode) to turn AGC off (0) or on (1).
  467. * Use ioctl(fd, SOUND_MIXER_3DSE, &mode) to turn 3DSE off (0) or on (1)
  468. * or mode==2 put 3DSE state to mode.
  469. */
  470. if (devc->model == MDL_SB16) {
  471. if (cmd == SOUND_MIXER_AGC)
  472. {
  473. if (get_user(val, p))
  474. return -EFAULT;
  475. sb_setmixer(devc, 0x43, (~val) & 0x01);
  476. return 0;
  477. }
  478. if (cmd == SOUND_MIXER_3DSE)
  479. {
  480. /* I put here 15, but I don't know the exact version.
  481. At least my 4.13 havn't 3DSE, 4.16 has it. */
  482. if (devc->minor < 15)
  483. return -EINVAL;
  484. if (get_user(val, p))
  485. return -EFAULT;
  486. if (val == 0 || val == 1)
  487. sb_chgmixer(devc, AWE_3DSE, 0x01, val);
  488. else if (val == 2)
  489. {
  490. ret = sb_getmixer(devc, AWE_3DSE)&0x01;
  491. return put_user(ret, p);
  492. }
  493. else
  494. return -EINVAL;
  495. return 0;
  496. }
  497. }
  498. if (((cmd >> 8) & 0xff) == 'M')
  499. {
  500. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  501. {
  502. if (get_user(val, p))
  503. return -EFAULT;
  504. switch (cmd & 0xff)
  505. {
  506. case SOUND_MIXER_RECSRC:
  507. ret = set_recmask(devc, val);
  508. break;
  509. case SOUND_MIXER_OUTSRC:
  510. ret = set_outmask(devc, val);
  511. break;
  512. default:
  513. ret = sb_mixer_set(devc, cmd & 0xff, val);
  514. }
  515. }
  516. else switch (cmd & 0xff)
  517. {
  518. case SOUND_MIXER_RECSRC:
  519. ret = devc->recmask;
  520. break;
  521. case SOUND_MIXER_OUTSRC:
  522. ret = devc->outmask;
  523. break;
  524. case SOUND_MIXER_DEVMASK:
  525. ret = devc->supported_devices;
  526. break;
  527. case SOUND_MIXER_STEREODEVS:
  528. ret = devc->supported_devices;
  529. /* The ESS seems to have stereo mic controls */
  530. if (devc->model == MDL_ESS)
  531. ret &= ~(SOUND_MASK_SPEAKER|SOUND_MASK_IMIX);
  532. else if (devc->model != MDL_JAZZ && devc->model != MDL_SMW)
  533. ret &= ~(SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
  534. break;
  535. case SOUND_MIXER_RECMASK:
  536. ret = devc->supported_rec_devices;
  537. break;
  538. case SOUND_MIXER_OUTMASK:
  539. ret = devc->supported_out_devices;
  540. break;
  541. case SOUND_MIXER_CAPS:
  542. ret = devc->mixer_caps;
  543. break;
  544. default:
  545. ret = sb_mixer_get(devc, cmd & 0xff);
  546. break;
  547. }
  548. return put_user(ret, p);
  549. } else
  550. return -EINVAL;
  551. }
  552. static struct mixer_operations sb_mixer_operations =
  553. {
  554. .owner = THIS_MODULE,
  555. .id = "SB",
  556. .name = "Sound Blaster",
  557. .ioctl = sb_mixer_ioctl
  558. };
  559. static struct mixer_operations als007_mixer_operations =
  560. {
  561. .owner = THIS_MODULE,
  562. .id = "ALS007",
  563. .name = "Avance ALS-007",
  564. .ioctl = sb_mixer_ioctl
  565. };
  566. static void sb_mixer_reset(sb_devc * devc)
  567. {
  568. char name[32];
  569. int i;
  570. sprintf(name, "SB_%d", devc->sbmixnum);
  571. if (devc->sbmo.sm_games)
  572. devc->levels = load_mixer_volumes(name, smg_default_levels, 1);
  573. else
  574. devc->levels = load_mixer_volumes(name, sb_default_levels, 1);
  575. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  576. sb_mixer_set(devc, i, devc->levels[i]);
  577. if (devc->model != MDL_ESS || !ess_mixer_reset (devc)) {
  578. set_recmask(devc, SOUND_MASK_MIC);
  579. }
  580. }
  581. int sb_mixer_init(sb_devc * devc, struct module *owner)
  582. {
  583. int mixer_type = 0;
  584. int m;
  585. devc->sbmixnum = sbmixnum++;
  586. devc->levels = NULL;
  587. sb_setmixer(devc, 0x00, 0); /* Reset mixer */
  588. if (!(mixer_type = detect_mixer(devc)))
  589. return 0; /* No mixer. Why? */
  590. switch (devc->model)
  591. {
  592. case MDL_ESSPCI:
  593. case MDL_YMPCI:
  594. case MDL_SBPRO:
  595. case MDL_AZTECH:
  596. case MDL_JAZZ:
  597. devc->mixer_caps = SOUND_CAP_EXCL_INPUT;
  598. devc->supported_devices = SBPRO_MIXER_DEVICES;
  599. devc->supported_rec_devices = SBPRO_RECORDING_DEVICES;
  600. devc->iomap = &sbpro_mix;
  601. devc->iomap_sz = ARRAY_SIZE(sbpro_mix);
  602. break;
  603. case MDL_ESS:
  604. ess_mixer_init (devc);
  605. break;
  606. case MDL_SMW:
  607. devc->mixer_caps = SOUND_CAP_EXCL_INPUT;
  608. devc->supported_devices = 0;
  609. devc->supported_rec_devices = 0;
  610. devc->iomap = &sbpro_mix;
  611. devc->iomap_sz = ARRAY_SIZE(sbpro_mix);
  612. smw_mixer_init(devc);
  613. break;
  614. case MDL_SB16:
  615. devc->mixer_caps = 0;
  616. devc->supported_rec_devices = SB16_RECORDING_DEVICES;
  617. devc->supported_out_devices = SB16_OUTFILTER_DEVICES;
  618. if (devc->submodel != SUBMDL_ALS007)
  619. {
  620. devc->supported_devices = SB16_MIXER_DEVICES;
  621. devc->iomap = &sb16_mix;
  622. devc->iomap_sz = ARRAY_SIZE(sb16_mix);
  623. }
  624. else
  625. {
  626. devc->supported_devices = ALS007_MIXER_DEVICES;
  627. devc->iomap = &als007_mix;
  628. devc->iomap_sz = ARRAY_SIZE(als007_mix);
  629. }
  630. break;
  631. default:
  632. printk(KERN_WARNING "sb_mixer: Unsupported mixer type %d\n", devc->model);
  633. return 0;
  634. }
  635. m = sound_alloc_mixerdev();
  636. if (m == -1)
  637. return 0;
  638. mixer_devs[m] = kmalloc(sizeof(struct mixer_operations), GFP_KERNEL);
  639. if (mixer_devs[m] == NULL)
  640. {
  641. printk(KERN_ERR "sb_mixer: Can't allocate memory\n");
  642. sound_unload_mixerdev(m);
  643. return 0;
  644. }
  645. if (devc->submodel != SUBMDL_ALS007)
  646. memcpy ((char *) mixer_devs[m], (char *) &sb_mixer_operations, sizeof (struct mixer_operations));
  647. else
  648. memcpy ((char *) mixer_devs[m], (char *) &als007_mixer_operations, sizeof (struct mixer_operations));
  649. mixer_devs[m]->devc = devc;
  650. if (owner)
  651. mixer_devs[m]->owner = owner;
  652. devc->my_mixerdev = m;
  653. sb_mixer_reset(devc);
  654. return 1;
  655. }
  656. void sb_mixer_unload(sb_devc *devc)
  657. {
  658. if (devc->my_mixerdev == -1)
  659. return;
  660. kfree(mixer_devs[devc->my_mixerdev]);
  661. sound_unload_mixerdev(devc->my_mixerdev);
  662. sbmixnum--;
  663. }