/kern_2.6.32/sound/core/oss/mixer_oss.c

http://omnia2droid.googlecode.com/ · C · 1401 lines · 1242 code · 111 blank · 48 comment · 324 complexity · 828db44bf871b02c759c454e746b8132 MD5 · raw file

  1. /*
  2. * OSS emulation layer for the mixer interface
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/time.h>
  24. #include <linux/string.h>
  25. #include <sound/core.h>
  26. #include <sound/minors.h>
  27. #include <sound/control.h>
  28. #include <sound/info.h>
  29. #include <sound/mixer_oss.h>
  30. #include <linux/soundcard.h>
  31. #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  33. MODULE_DESCRIPTION("Mixer OSS emulation for ALSA.");
  34. MODULE_LICENSE("GPL");
  35. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MIXER);
  36. static int snd_mixer_oss_open(struct inode *inode, struct file *file)
  37. {
  38. struct snd_card *card;
  39. struct snd_mixer_oss_file *fmixer;
  40. int err;
  41. card = snd_lookup_oss_minor_data(iminor(inode),
  42. SNDRV_OSS_DEVICE_TYPE_MIXER);
  43. if (card == NULL)
  44. return -ENODEV;
  45. if (card->mixer_oss == NULL)
  46. return -ENODEV;
  47. err = snd_card_file_add(card, file);
  48. if (err < 0)
  49. return err;
  50. fmixer = kzalloc(sizeof(*fmixer), GFP_KERNEL);
  51. if (fmixer == NULL) {
  52. snd_card_file_remove(card, file);
  53. return -ENOMEM;
  54. }
  55. fmixer->card = card;
  56. fmixer->mixer = card->mixer_oss;
  57. file->private_data = fmixer;
  58. if (!try_module_get(card->module)) {
  59. kfree(fmixer);
  60. snd_card_file_remove(card, file);
  61. return -EFAULT;
  62. }
  63. return 0;
  64. }
  65. static int snd_mixer_oss_release(struct inode *inode, struct file *file)
  66. {
  67. struct snd_mixer_oss_file *fmixer;
  68. if (file->private_data) {
  69. fmixer = (struct snd_mixer_oss_file *) file->private_data;
  70. module_put(fmixer->card->module);
  71. snd_card_file_remove(fmixer->card, file);
  72. kfree(fmixer);
  73. }
  74. return 0;
  75. }
  76. static int snd_mixer_oss_info(struct snd_mixer_oss_file *fmixer,
  77. mixer_info __user *_info)
  78. {
  79. struct snd_card *card = fmixer->card;
  80. struct snd_mixer_oss *mixer = fmixer->mixer;
  81. struct mixer_info info;
  82. memset(&info, 0, sizeof(info));
  83. strlcpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id));
  84. strlcpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name));
  85. info.modify_counter = card->mixer_oss_change_count;
  86. if (copy_to_user(_info, &info, sizeof(info)))
  87. return -EFAULT;
  88. return 0;
  89. }
  90. static int snd_mixer_oss_info_obsolete(struct snd_mixer_oss_file *fmixer,
  91. _old_mixer_info __user *_info)
  92. {
  93. struct snd_card *card = fmixer->card;
  94. struct snd_mixer_oss *mixer = fmixer->mixer;
  95. _old_mixer_info info;
  96. memset(&info, 0, sizeof(info));
  97. strlcpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id));
  98. strlcpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name));
  99. if (copy_to_user(_info, &info, sizeof(info)))
  100. return -EFAULT;
  101. return 0;
  102. }
  103. static int snd_mixer_oss_caps(struct snd_mixer_oss_file *fmixer)
  104. {
  105. struct snd_mixer_oss *mixer = fmixer->mixer;
  106. int result = 0;
  107. if (mixer == NULL)
  108. return -EIO;
  109. if (mixer->get_recsrc && mixer->put_recsrc)
  110. result |= SOUND_CAP_EXCL_INPUT;
  111. return result;
  112. }
  113. static int snd_mixer_oss_devmask(struct snd_mixer_oss_file *fmixer)
  114. {
  115. struct snd_mixer_oss *mixer = fmixer->mixer;
  116. struct snd_mixer_oss_slot *pslot;
  117. int result = 0, chn;
  118. if (mixer == NULL)
  119. return -EIO;
  120. for (chn = 0; chn < 31; chn++) {
  121. pslot = &mixer->slots[chn];
  122. if (pslot->put_volume || pslot->put_recsrc)
  123. result |= 1 << chn;
  124. }
  125. return result;
  126. }
  127. static int snd_mixer_oss_stereodevs(struct snd_mixer_oss_file *fmixer)
  128. {
  129. struct snd_mixer_oss *mixer = fmixer->mixer;
  130. struct snd_mixer_oss_slot *pslot;
  131. int result = 0, chn;
  132. if (mixer == NULL)
  133. return -EIO;
  134. for (chn = 0; chn < 31; chn++) {
  135. pslot = &mixer->slots[chn];
  136. if (pslot->put_volume && pslot->stereo)
  137. result |= 1 << chn;
  138. }
  139. return result;
  140. }
  141. static int snd_mixer_oss_recmask(struct snd_mixer_oss_file *fmixer)
  142. {
  143. struct snd_mixer_oss *mixer = fmixer->mixer;
  144. int result = 0;
  145. if (mixer == NULL)
  146. return -EIO;
  147. if (mixer->put_recsrc && mixer->get_recsrc) { /* exclusive */
  148. result = mixer->mask_recsrc;
  149. } else {
  150. struct snd_mixer_oss_slot *pslot;
  151. int chn;
  152. for (chn = 0; chn < 31; chn++) {
  153. pslot = &mixer->slots[chn];
  154. if (pslot->put_recsrc)
  155. result |= 1 << chn;
  156. }
  157. }
  158. return result;
  159. }
  160. static int snd_mixer_oss_get_recsrc(struct snd_mixer_oss_file *fmixer)
  161. {
  162. struct snd_mixer_oss *mixer = fmixer->mixer;
  163. int result = 0;
  164. if (mixer == NULL)
  165. return -EIO;
  166. if (mixer->put_recsrc && mixer->get_recsrc) { /* exclusive */
  167. int err;
  168. if ((err = mixer->get_recsrc(fmixer, &result)) < 0)
  169. return err;
  170. result = 1 << result;
  171. } else {
  172. struct snd_mixer_oss_slot *pslot;
  173. int chn;
  174. for (chn = 0; chn < 31; chn++) {
  175. pslot = &mixer->slots[chn];
  176. if (pslot->get_recsrc) {
  177. int active = 0;
  178. pslot->get_recsrc(fmixer, pslot, &active);
  179. if (active)
  180. result |= 1 << chn;
  181. }
  182. }
  183. }
  184. return mixer->oss_recsrc = result;
  185. }
  186. static int snd_mixer_oss_set_recsrc(struct snd_mixer_oss_file *fmixer, int recsrc)
  187. {
  188. struct snd_mixer_oss *mixer = fmixer->mixer;
  189. struct snd_mixer_oss_slot *pslot;
  190. int chn, active;
  191. int result = 0;
  192. if (mixer == NULL)
  193. return -EIO;
  194. if (mixer->get_recsrc && mixer->put_recsrc) { /* exclusive input */
  195. if (recsrc & ~mixer->oss_recsrc)
  196. recsrc &= ~mixer->oss_recsrc;
  197. mixer->put_recsrc(fmixer, ffz(~recsrc));
  198. mixer->get_recsrc(fmixer, &result);
  199. result = 1 << result;
  200. }
  201. for (chn = 0; chn < 31; chn++) {
  202. pslot = &mixer->slots[chn];
  203. if (pslot->put_recsrc) {
  204. active = (recsrc & (1 << chn)) ? 1 : 0;
  205. pslot->put_recsrc(fmixer, pslot, active);
  206. }
  207. }
  208. if (! result) {
  209. for (chn = 0; chn < 31; chn++) {
  210. pslot = &mixer->slots[chn];
  211. if (pslot->get_recsrc) {
  212. active = 0;
  213. pslot->get_recsrc(fmixer, pslot, &active);
  214. if (active)
  215. result |= 1 << chn;
  216. }
  217. }
  218. }
  219. return result;
  220. }
  221. static int snd_mixer_oss_get_volume(struct snd_mixer_oss_file *fmixer, int slot)
  222. {
  223. struct snd_mixer_oss *mixer = fmixer->mixer;
  224. struct snd_mixer_oss_slot *pslot;
  225. int result = 0, left, right;
  226. if (mixer == NULL || slot > 30)
  227. return -EIO;
  228. pslot = &mixer->slots[slot];
  229. left = pslot->volume[0];
  230. right = pslot->volume[1];
  231. if (pslot->get_volume)
  232. result = pslot->get_volume(fmixer, pslot, &left, &right);
  233. if (!pslot->stereo)
  234. right = left;
  235. if (snd_BUG_ON(left < 0 || left > 100))
  236. return -EIO;
  237. if (snd_BUG_ON(right < 0 || right > 100))
  238. return -EIO;
  239. if (result >= 0) {
  240. pslot->volume[0] = left;
  241. pslot->volume[1] = right;
  242. result = (left & 0xff) | ((right & 0xff) << 8);
  243. }
  244. return result;
  245. }
  246. static int snd_mixer_oss_set_volume(struct snd_mixer_oss_file *fmixer,
  247. int slot, int volume)
  248. {
  249. struct snd_mixer_oss *mixer = fmixer->mixer;
  250. struct snd_mixer_oss_slot *pslot;
  251. int result = 0, left = volume & 0xff, right = (volume >> 8) & 0xff;
  252. if (mixer == NULL || slot > 30)
  253. return -EIO;
  254. pslot = &mixer->slots[slot];
  255. if (left > 100)
  256. left = 100;
  257. if (right > 100)
  258. right = 100;
  259. if (!pslot->stereo)
  260. right = left;
  261. if (pslot->put_volume)
  262. result = pslot->put_volume(fmixer, pslot, left, right);
  263. if (result < 0)
  264. return result;
  265. pslot->volume[0] = left;
  266. pslot->volume[1] = right;
  267. return (left & 0xff) | ((right & 0xff) << 8);
  268. }
  269. static int snd_mixer_oss_ioctl1(struct snd_mixer_oss_file *fmixer, unsigned int cmd, unsigned long arg)
  270. {
  271. void __user *argp = (void __user *)arg;
  272. int __user *p = argp;
  273. int tmp;
  274. if (snd_BUG_ON(!fmixer))
  275. return -ENXIO;
  276. if (((cmd >> 8) & 0xff) == 'M') {
  277. switch (cmd) {
  278. case SOUND_MIXER_INFO:
  279. return snd_mixer_oss_info(fmixer, argp);
  280. case SOUND_OLD_MIXER_INFO:
  281. return snd_mixer_oss_info_obsolete(fmixer, argp);
  282. case SOUND_MIXER_WRITE_RECSRC:
  283. if (get_user(tmp, p))
  284. return -EFAULT;
  285. tmp = snd_mixer_oss_set_recsrc(fmixer, tmp);
  286. if (tmp < 0)
  287. return tmp;
  288. return put_user(tmp, p);
  289. case OSS_GETVERSION:
  290. return put_user(SNDRV_OSS_VERSION, p);
  291. case OSS_ALSAEMULVER:
  292. return put_user(1, p);
  293. case SOUND_MIXER_READ_DEVMASK:
  294. tmp = snd_mixer_oss_devmask(fmixer);
  295. if (tmp < 0)
  296. return tmp;
  297. return put_user(tmp, p);
  298. case SOUND_MIXER_READ_STEREODEVS:
  299. tmp = snd_mixer_oss_stereodevs(fmixer);
  300. if (tmp < 0)
  301. return tmp;
  302. return put_user(tmp, p);
  303. case SOUND_MIXER_READ_RECMASK:
  304. tmp = snd_mixer_oss_recmask(fmixer);
  305. if (tmp < 0)
  306. return tmp;
  307. return put_user(tmp, p);
  308. case SOUND_MIXER_READ_CAPS:
  309. tmp = snd_mixer_oss_caps(fmixer);
  310. if (tmp < 0)
  311. return tmp;
  312. return put_user(tmp, p);
  313. case SOUND_MIXER_READ_RECSRC:
  314. tmp = snd_mixer_oss_get_recsrc(fmixer);
  315. if (tmp < 0)
  316. return tmp;
  317. return put_user(tmp, p);
  318. }
  319. }
  320. if (cmd & SIOC_IN) {
  321. if (get_user(tmp, p))
  322. return -EFAULT;
  323. tmp = snd_mixer_oss_set_volume(fmixer, cmd & 0xff, tmp);
  324. if (tmp < 0)
  325. return tmp;
  326. return put_user(tmp, p);
  327. } else if (cmd & SIOC_OUT) {
  328. tmp = snd_mixer_oss_get_volume(fmixer, cmd & 0xff);
  329. if (tmp < 0)
  330. return tmp;
  331. return put_user(tmp, p);
  332. }
  333. return -ENXIO;
  334. }
  335. static long snd_mixer_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  336. {
  337. return snd_mixer_oss_ioctl1((struct snd_mixer_oss_file *) file->private_data, cmd, arg);
  338. }
  339. int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg)
  340. {
  341. struct snd_mixer_oss_file fmixer;
  342. if (snd_BUG_ON(!card))
  343. return -ENXIO;
  344. if (card->mixer_oss == NULL)
  345. return -ENXIO;
  346. memset(&fmixer, 0, sizeof(fmixer));
  347. fmixer.card = card;
  348. fmixer.mixer = card->mixer_oss;
  349. return snd_mixer_oss_ioctl1(&fmixer, cmd, arg);
  350. }
  351. #ifdef CONFIG_COMPAT
  352. /* all compatible */
  353. #define snd_mixer_oss_ioctl_compat snd_mixer_oss_ioctl
  354. #else
  355. #define snd_mixer_oss_ioctl_compat NULL
  356. #endif
  357. /*
  358. * REGISTRATION PART
  359. */
  360. static const struct file_operations snd_mixer_oss_f_ops =
  361. {
  362. .owner = THIS_MODULE,
  363. .open = snd_mixer_oss_open,
  364. .release = snd_mixer_oss_release,
  365. .unlocked_ioctl = snd_mixer_oss_ioctl,
  366. .compat_ioctl = snd_mixer_oss_ioctl_compat,
  367. };
  368. /*
  369. * utilities
  370. */
  371. static long snd_mixer_oss_conv(long val, long omin, long omax, long nmin, long nmax)
  372. {
  373. long orange = omax - omin, nrange = nmax - nmin;
  374. if (orange == 0)
  375. return 0;
  376. return ((nrange * (val - omin)) + (orange / 2)) / orange + nmin;
  377. }
  378. /* convert from alsa native to oss values (0-100) */
  379. static long snd_mixer_oss_conv1(long val, long min, long max, int *old)
  380. {
  381. if (val == snd_mixer_oss_conv(*old, 0, 100, min, max))
  382. return *old;
  383. return snd_mixer_oss_conv(val, min, max, 0, 100);
  384. }
  385. /* convert from oss to alsa native values */
  386. static long snd_mixer_oss_conv2(long val, long min, long max)
  387. {
  388. return snd_mixer_oss_conv(val, 0, 100, min, max);
  389. }
  390. #if 0
  391. static void snd_mixer_oss_recsrce_set(struct snd_card *card, int slot)
  392. {
  393. struct snd_mixer_oss *mixer = card->mixer_oss;
  394. if (mixer)
  395. mixer->mask_recsrc |= 1 << slot;
  396. }
  397. static int snd_mixer_oss_recsrce_get(struct snd_card *card, int slot)
  398. {
  399. struct snd_mixer_oss *mixer = card->mixer_oss;
  400. if (mixer && (mixer->mask_recsrc & (1 << slot)))
  401. return 1;
  402. return 0;
  403. }
  404. #endif
  405. #define SNDRV_MIXER_OSS_SIGNATURE 0x65999250
  406. #define SNDRV_MIXER_OSS_ITEM_GLOBAL 0
  407. #define SNDRV_MIXER_OSS_ITEM_GSWITCH 1
  408. #define SNDRV_MIXER_OSS_ITEM_GROUTE 2
  409. #define SNDRV_MIXER_OSS_ITEM_GVOLUME 3
  410. #define SNDRV_MIXER_OSS_ITEM_PSWITCH 4
  411. #define SNDRV_MIXER_OSS_ITEM_PROUTE 5
  412. #define SNDRV_MIXER_OSS_ITEM_PVOLUME 6
  413. #define SNDRV_MIXER_OSS_ITEM_CSWITCH 7
  414. #define SNDRV_MIXER_OSS_ITEM_CROUTE 8
  415. #define SNDRV_MIXER_OSS_ITEM_CVOLUME 9
  416. #define SNDRV_MIXER_OSS_ITEM_CAPTURE 10
  417. #define SNDRV_MIXER_OSS_ITEM_COUNT 11
  418. #define SNDRV_MIXER_OSS_PRESENT_GLOBAL (1<<0)
  419. #define SNDRV_MIXER_OSS_PRESENT_GSWITCH (1<<1)
  420. #define SNDRV_MIXER_OSS_PRESENT_GROUTE (1<<2)
  421. #define SNDRV_MIXER_OSS_PRESENT_GVOLUME (1<<3)
  422. #define SNDRV_MIXER_OSS_PRESENT_PSWITCH (1<<4)
  423. #define SNDRV_MIXER_OSS_PRESENT_PROUTE (1<<5)
  424. #define SNDRV_MIXER_OSS_PRESENT_PVOLUME (1<<6)
  425. #define SNDRV_MIXER_OSS_PRESENT_CSWITCH (1<<7)
  426. #define SNDRV_MIXER_OSS_PRESENT_CROUTE (1<<8)
  427. #define SNDRV_MIXER_OSS_PRESENT_CVOLUME (1<<9)
  428. #define SNDRV_MIXER_OSS_PRESENT_CAPTURE (1<<10)
  429. struct slot {
  430. unsigned int signature;
  431. unsigned int present;
  432. unsigned int channels;
  433. unsigned int numid[SNDRV_MIXER_OSS_ITEM_COUNT];
  434. unsigned int capture_item;
  435. struct snd_mixer_oss_assign_table *assigned;
  436. unsigned int allocated: 1;
  437. };
  438. #define ID_UNKNOWN ((unsigned int)-1)
  439. static struct snd_kcontrol *snd_mixer_oss_test_id(struct snd_mixer_oss *mixer, const char *name, int index)
  440. {
  441. struct snd_card *card = mixer->card;
  442. struct snd_ctl_elem_id id;
  443. memset(&id, 0, sizeof(id));
  444. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  445. strcpy(id.name, name);
  446. id.index = index;
  447. return snd_ctl_find_id(card, &id);
  448. }
  449. static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,
  450. struct snd_mixer_oss_slot *pslot,
  451. unsigned int numid,
  452. int *left, int *right)
  453. {
  454. struct snd_ctl_elem_info *uinfo;
  455. struct snd_ctl_elem_value *uctl;
  456. struct snd_kcontrol *kctl;
  457. struct snd_card *card = fmixer->card;
  458. if (numid == ID_UNKNOWN)
  459. return;
  460. down_read(&card->controls_rwsem);
  461. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
  462. up_read(&card->controls_rwsem);
  463. return;
  464. }
  465. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  466. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  467. if (uinfo == NULL || uctl == NULL)
  468. goto __unalloc;
  469. if (kctl->info(kctl, uinfo))
  470. goto __unalloc;
  471. if (kctl->get(kctl, uctl))
  472. goto __unalloc;
  473. if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN &&
  474. uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1)
  475. goto __unalloc;
  476. *left = snd_mixer_oss_conv1(uctl->value.integer.value[0], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[0]);
  477. if (uinfo->count > 1)
  478. *right = snd_mixer_oss_conv1(uctl->value.integer.value[1], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[1]);
  479. __unalloc:
  480. up_read(&card->controls_rwsem);
  481. kfree(uctl);
  482. kfree(uinfo);
  483. }
  484. static void snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file *fmixer,
  485. struct snd_mixer_oss_slot *pslot,
  486. unsigned int numid,
  487. int *left, int *right,
  488. int route)
  489. {
  490. struct snd_ctl_elem_info *uinfo;
  491. struct snd_ctl_elem_value *uctl;
  492. struct snd_kcontrol *kctl;
  493. struct snd_card *card = fmixer->card;
  494. if (numid == ID_UNKNOWN)
  495. return;
  496. down_read(&card->controls_rwsem);
  497. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
  498. up_read(&card->controls_rwsem);
  499. return;
  500. }
  501. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  502. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  503. if (uinfo == NULL || uctl == NULL)
  504. goto __unalloc;
  505. if (kctl->info(kctl, uinfo))
  506. goto __unalloc;
  507. if (kctl->get(kctl, uctl))
  508. goto __unalloc;
  509. if (!uctl->value.integer.value[0]) {
  510. *left = 0;
  511. if (uinfo->count == 1)
  512. *right = 0;
  513. }
  514. if (uinfo->count > 1 && !uctl->value.integer.value[route ? 3 : 1])
  515. *right = 0;
  516. __unalloc:
  517. up_read(&card->controls_rwsem);
  518. kfree(uctl);
  519. kfree(uinfo);
  520. }
  521. static int snd_mixer_oss_get_volume1(struct snd_mixer_oss_file *fmixer,
  522. struct snd_mixer_oss_slot *pslot,
  523. int *left, int *right)
  524. {
  525. struct slot *slot = (struct slot *)pslot->private_data;
  526. *left = *right = 100;
  527. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) {
  528. snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right);
  529. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) {
  530. snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right);
  531. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) {
  532. snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right);
  533. }
  534. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
  535. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
  536. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
  537. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
  538. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
  539. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
  540. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
  541. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
  542. }
  543. return 0;
  544. }
  545. static void snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file *fmixer,
  546. struct snd_mixer_oss_slot *pslot,
  547. unsigned int numid,
  548. int left, int right)
  549. {
  550. struct snd_ctl_elem_info *uinfo;
  551. struct snd_ctl_elem_value *uctl;
  552. struct snd_kcontrol *kctl;
  553. struct snd_card *card = fmixer->card;
  554. int res;
  555. if (numid == ID_UNKNOWN)
  556. return;
  557. down_read(&card->controls_rwsem);
  558. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL)
  559. return;
  560. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  561. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  562. if (uinfo == NULL || uctl == NULL)
  563. goto __unalloc;
  564. if (kctl->info(kctl, uinfo))
  565. goto __unalloc;
  566. if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN &&
  567. uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1)
  568. goto __unalloc;
  569. uctl->value.integer.value[0] = snd_mixer_oss_conv2(left, uinfo->value.integer.min, uinfo->value.integer.max);
  570. if (uinfo->count > 1)
  571. uctl->value.integer.value[1] = snd_mixer_oss_conv2(right, uinfo->value.integer.min, uinfo->value.integer.max);
  572. if ((res = kctl->put(kctl, uctl)) < 0)
  573. goto __unalloc;
  574. if (res > 0)
  575. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  576. __unalloc:
  577. up_read(&card->controls_rwsem);
  578. kfree(uctl);
  579. kfree(uinfo);
  580. }
  581. static void snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file *fmixer,
  582. struct snd_mixer_oss_slot *pslot,
  583. unsigned int numid,
  584. int left, int right,
  585. int route)
  586. {
  587. struct snd_ctl_elem_info *uinfo;
  588. struct snd_ctl_elem_value *uctl;
  589. struct snd_kcontrol *kctl;
  590. struct snd_card *card = fmixer->card;
  591. int res;
  592. if (numid == ID_UNKNOWN)
  593. return;
  594. down_read(&card->controls_rwsem);
  595. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
  596. up_read(&fmixer->card->controls_rwsem);
  597. return;
  598. }
  599. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  600. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  601. if (uinfo == NULL || uctl == NULL)
  602. goto __unalloc;
  603. if (kctl->info(kctl, uinfo))
  604. goto __unalloc;
  605. if (uinfo->count > 1) {
  606. uctl->value.integer.value[0] = left > 0 ? 1 : 0;
  607. uctl->value.integer.value[route ? 3 : 1] = right > 0 ? 1 : 0;
  608. if (route) {
  609. uctl->value.integer.value[1] =
  610. uctl->value.integer.value[2] = 0;
  611. }
  612. } else {
  613. uctl->value.integer.value[0] = (left > 0 || right > 0) ? 1 : 0;
  614. }
  615. if ((res = kctl->put(kctl, uctl)) < 0)
  616. goto __unalloc;
  617. if (res > 0)
  618. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  619. __unalloc:
  620. up_read(&card->controls_rwsem);
  621. kfree(uctl);
  622. kfree(uinfo);
  623. }
  624. static int snd_mixer_oss_put_volume1(struct snd_mixer_oss_file *fmixer,
  625. struct snd_mixer_oss_slot *pslot,
  626. int left, int right)
  627. {
  628. struct slot *slot = (struct slot *)pslot->private_data;
  629. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) {
  630. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right);
  631. if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME)
  632. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right);
  633. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME) {
  634. snd_mixer_oss_put_volume1_vol(fmixer, pslot,
  635. slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right);
  636. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) {
  637. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right);
  638. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) {
  639. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right);
  640. }
  641. if (left || right) {
  642. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH)
  643. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
  644. if (slot->present & SNDRV_MIXER_OSS_PRESENT_CSWITCH)
  645. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], left, right, 0);
  646. if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH)
  647. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
  648. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE)
  649. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
  650. if (slot->present & SNDRV_MIXER_OSS_PRESENT_CROUTE)
  651. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], left, right, 1);
  652. if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE)
  653. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
  654. } else {
  655. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
  656. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
  657. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) {
  658. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], left, right, 0);
  659. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
  660. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
  661. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
  662. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
  663. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CROUTE) {
  664. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], left, right, 1);
  665. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
  666. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
  667. }
  668. }
  669. return 0;
  670. }
  671. static int snd_mixer_oss_get_recsrc1_sw(struct snd_mixer_oss_file *fmixer,
  672. struct snd_mixer_oss_slot *pslot,
  673. int *active)
  674. {
  675. struct slot *slot = (struct slot *)pslot->private_data;
  676. int left, right;
  677. left = right = 1;
  678. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], &left, &right, 0);
  679. *active = (left || right) ? 1 : 0;
  680. return 0;
  681. }
  682. static int snd_mixer_oss_get_recsrc1_route(struct snd_mixer_oss_file *fmixer,
  683. struct snd_mixer_oss_slot *pslot,
  684. int *active)
  685. {
  686. struct slot *slot = (struct slot *)pslot->private_data;
  687. int left, right;
  688. left = right = 1;
  689. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], &left, &right, 1);
  690. *active = (left || right) ? 1 : 0;
  691. return 0;
  692. }
  693. static int snd_mixer_oss_put_recsrc1_sw(struct snd_mixer_oss_file *fmixer,
  694. struct snd_mixer_oss_slot *pslot,
  695. int active)
  696. {
  697. struct slot *slot = (struct slot *)pslot->private_data;
  698. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], active, active, 0);
  699. return 0;
  700. }
  701. static int snd_mixer_oss_put_recsrc1_route(struct snd_mixer_oss_file *fmixer,
  702. struct snd_mixer_oss_slot *pslot,
  703. int active)
  704. {
  705. struct slot *slot = (struct slot *)pslot->private_data;
  706. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], active, active, 1);
  707. return 0;
  708. }
  709. static int snd_mixer_oss_get_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int *active_index)
  710. {
  711. struct snd_card *card = fmixer->card;
  712. struct snd_mixer_oss *mixer = fmixer->mixer;
  713. struct snd_kcontrol *kctl;
  714. struct snd_mixer_oss_slot *pslot;
  715. struct slot *slot;
  716. struct snd_ctl_elem_info *uinfo;
  717. struct snd_ctl_elem_value *uctl;
  718. int err, idx;
  719. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  720. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  721. if (uinfo == NULL || uctl == NULL) {
  722. err = -ENOMEM;
  723. goto __unlock;
  724. }
  725. down_read(&card->controls_rwsem);
  726. kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
  727. if (! kctl) {
  728. err = -ENOENT;
  729. goto __unlock;
  730. }
  731. if ((err = kctl->info(kctl, uinfo)) < 0)
  732. goto __unlock;
  733. if ((err = kctl->get(kctl, uctl)) < 0)
  734. goto __unlock;
  735. for (idx = 0; idx < 32; idx++) {
  736. if (!(mixer->mask_recsrc & (1 << idx)))
  737. continue;
  738. pslot = &mixer->slots[idx];
  739. slot = (struct slot *)pslot->private_data;
  740. if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
  741. continue;
  742. if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
  743. continue;
  744. if (slot->capture_item == uctl->value.enumerated.item[0]) {
  745. *active_index = idx;
  746. break;
  747. }
  748. }
  749. err = 0;
  750. __unlock:
  751. up_read(&card->controls_rwsem);
  752. kfree(uctl);
  753. kfree(uinfo);
  754. return err;
  755. }
  756. static int snd_mixer_oss_put_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int active_index)
  757. {
  758. struct snd_card *card = fmixer->card;
  759. struct snd_mixer_oss *mixer = fmixer->mixer;
  760. struct snd_kcontrol *kctl;
  761. struct snd_mixer_oss_slot *pslot;
  762. struct slot *slot = NULL;
  763. struct snd_ctl_elem_info *uinfo;
  764. struct snd_ctl_elem_value *uctl;
  765. int err;
  766. unsigned int idx;
  767. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  768. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  769. if (uinfo == NULL || uctl == NULL) {
  770. err = -ENOMEM;
  771. goto __unlock;
  772. }
  773. down_read(&card->controls_rwsem);
  774. kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
  775. if (! kctl) {
  776. err = -ENOENT;
  777. goto __unlock;
  778. }
  779. if ((err = kctl->info(kctl, uinfo)) < 0)
  780. goto __unlock;
  781. for (idx = 0; idx < 32; idx++) {
  782. if (!(mixer->mask_recsrc & (1 << idx)))
  783. continue;
  784. pslot = &mixer->slots[idx];
  785. slot = (struct slot *)pslot->private_data;
  786. if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
  787. continue;
  788. if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
  789. continue;
  790. if (idx == active_index)
  791. break;
  792. slot = NULL;
  793. }
  794. if (! slot)
  795. goto __unlock;
  796. for (idx = 0; idx < uinfo->count; idx++)
  797. uctl->value.enumerated.item[idx] = slot->capture_item;
  798. err = kctl->put(kctl, uctl);
  799. if (err > 0)
  800. snd_ctl_notify(fmixer->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  801. err = 0;
  802. __unlock:
  803. up_read(&card->controls_rwsem);
  804. kfree(uctl);
  805. kfree(uinfo);
  806. return err;
  807. }
  808. struct snd_mixer_oss_assign_table {
  809. int oss_id;
  810. const char *name;
  811. int index;
  812. };
  813. static int snd_mixer_oss_build_test(struct snd_mixer_oss *mixer, struct slot *slot, const char *name, int index, int item)
  814. {
  815. struct snd_ctl_elem_info *info;
  816. struct snd_kcontrol *kcontrol;
  817. struct snd_card *card = mixer->card;
  818. int err;
  819. down_read(&card->controls_rwsem);
  820. kcontrol = snd_mixer_oss_test_id(mixer, name, index);
  821. if (kcontrol == NULL) {
  822. up_read(&card->controls_rwsem);
  823. return 0;
  824. }
  825. info = kmalloc(sizeof(*info), GFP_KERNEL);
  826. if (! info) {
  827. up_read(&card->controls_rwsem);
  828. return -ENOMEM;
  829. }
  830. if ((err = kcontrol->info(kcontrol, info)) < 0) {
  831. up_read(&card->controls_rwsem);
  832. kfree(info);
  833. return err;
  834. }
  835. slot->numid[item] = kcontrol->id.numid;
  836. up_read(&card->controls_rwsem);
  837. if (info->count > slot->channels)
  838. slot->channels = info->count;
  839. slot->present |= 1 << item;
  840. kfree(info);
  841. return 0;
  842. }
  843. static void snd_mixer_oss_slot_free(struct snd_mixer_oss_slot *chn)
  844. {
  845. struct slot *p = (struct slot *)chn->private_data;
  846. if (p) {
  847. if (p->allocated && p->assigned) {
  848. kfree(p->assigned->name);
  849. kfree(p->assigned);
  850. }
  851. kfree(p);
  852. }
  853. }
  854. static void mixer_slot_clear(struct snd_mixer_oss_slot *rslot)
  855. {
  856. int idx = rslot->number; /* remember this */
  857. if (rslot->private_free)
  858. rslot->private_free(rslot);
  859. memset(rslot, 0, sizeof(*rslot));
  860. rslot->number = idx;
  861. }
  862. /* In a separate function to keep gcc 3.2 happy - do NOT merge this in
  863. snd_mixer_oss_build_input! */
  864. static int snd_mixer_oss_build_test_all(struct snd_mixer_oss *mixer,
  865. struct snd_mixer_oss_assign_table *ptr,
  866. struct slot *slot)
  867. {
  868. char str[64];
  869. int err;
  870. err = snd_mixer_oss_build_test(mixer, slot, ptr->name, ptr->index,
  871. SNDRV_MIXER_OSS_ITEM_GLOBAL);
  872. if (err)
  873. return err;
  874. sprintf(str, "%s Switch", ptr->name);
  875. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  876. SNDRV_MIXER_OSS_ITEM_GSWITCH);
  877. if (err)
  878. return err;
  879. sprintf(str, "%s Route", ptr->name);
  880. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  881. SNDRV_MIXER_OSS_ITEM_GROUTE);
  882. if (err)
  883. return err;
  884. sprintf(str, "%s Volume", ptr->name);
  885. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  886. SNDRV_MIXER_OSS_ITEM_GVOLUME);
  887. if (err)
  888. return err;
  889. sprintf(str, "%s Playback Switch", ptr->name);
  890. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  891. SNDRV_MIXER_OSS_ITEM_PSWITCH);
  892. if (err)
  893. return err;
  894. sprintf(str, "%s Playback Route", ptr->name);
  895. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  896. SNDRV_MIXER_OSS_ITEM_PROUTE);
  897. if (err)
  898. return err;
  899. sprintf(str, "%s Playback Volume", ptr->name);
  900. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  901. SNDRV_MIXER_OSS_ITEM_PVOLUME);
  902. if (err)
  903. return err;
  904. sprintf(str, "%s Capture Switch", ptr->name);
  905. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  906. SNDRV_MIXER_OSS_ITEM_CSWITCH);
  907. if (err)
  908. return err;
  909. sprintf(str, "%s Capture Route", ptr->name);
  910. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  911. SNDRV_MIXER_OSS_ITEM_CROUTE);
  912. if (err)
  913. return err;
  914. sprintf(str, "%s Capture Volume", ptr->name);
  915. err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index,
  916. SNDRV_MIXER_OSS_ITEM_CVOLUME);
  917. if (err)
  918. return err;
  919. return 0;
  920. }
  921. /*
  922. * build an OSS mixer element.
  923. * ptr_allocated means the entry is dynamically allocated (change via proc file).
  924. * when replace_old = 1, the old entry is replaced with the new one.
  925. */
  926. static int snd_mixer_oss_build_input(struct snd_mixer_oss *mixer, struct snd_mixer_oss_assign_table *ptr, int ptr_allocated, int replace_old)
  927. {
  928. struct slot slot;
  929. struct slot *pslot;
  930. struct snd_kcontrol *kctl;
  931. struct snd_mixer_oss_slot *rslot;
  932. char str[64];
  933. /* check if already assigned */
  934. if (mixer->slots[ptr->oss_id].get_volume && ! replace_old)
  935. return 0;
  936. memset(&slot, 0, sizeof(slot));
  937. memset(slot.numid, 0xff, sizeof(slot.numid)); /* ID_UNKNOWN */
  938. if (snd_mixer_oss_build_test_all(mixer, ptr, &slot))
  939. return 0;
  940. down_read(&mixer->card->controls_rwsem);
  941. if (ptr->index == 0 && (kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0)) != NULL) {
  942. struct snd_ctl_elem_info *uinfo;
  943. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  944. if (! uinfo) {
  945. up_read(&mixer->card->controls_rwsem);
  946. return -ENOMEM;
  947. }
  948. if (kctl->info(kctl, uinfo)) {
  949. up_read(&mixer->card->controls_rwsem);
  950. return 0;
  951. }
  952. strcpy(str, ptr->name);
  953. if (!strcmp(str, "Master"))
  954. strcpy(str, "Mix");
  955. if (!strcmp(str, "Master Mono"))
  956. strcpy(str, "Mix Mono");
  957. slot.capture_item = 0;
  958. if (!strcmp(uinfo->value.enumerated.name, str)) {
  959. slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
  960. } else {
  961. for (slot.capture_item = 1; slot.capture_item < uinfo->value.enumerated.items; slot.capture_item++) {
  962. uinfo->value.enumerated.item = slot.capture_item;
  963. if (kctl->info(kctl, uinfo)) {
  964. up_read(&mixer->card->controls_rwsem);
  965. return 0;
  966. }
  967. if (!strcmp(uinfo->value.enumerated.name, str)) {
  968. slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
  969. break;
  970. }
  971. }
  972. }
  973. kfree(uinfo);
  974. }
  975. up_read(&mixer->card->controls_rwsem);
  976. if (slot.present != 0) {
  977. pslot = kmalloc(sizeof(slot), GFP_KERNEL);
  978. if (! pslot)
  979. return -ENOMEM;
  980. *pslot = slot;
  981. pslot->signature = SNDRV_MIXER_OSS_SIGNATURE;
  982. pslot->assigned = ptr;
  983. pslot->allocated = ptr_allocated;
  984. rslot = &mixer->slots[ptr->oss_id];
  985. mixer_slot_clear(rslot);
  986. rslot->stereo = slot.channels > 1 ? 1 : 0;
  987. rslot->get_volume = snd_mixer_oss_get_volume1;
  988. rslot->put_volume = snd_mixer_oss_put_volume1;
  989. /* note: ES18xx have both Capture Source and XX Capture Volume !!! */
  990. if (slot.present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) {
  991. rslot->get_recsrc = snd_mixer_oss_get_recsrc1_sw;
  992. rslot->put_recsrc = snd_mixer_oss_put_recsrc1_sw;
  993. } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CROUTE) {
  994. rslot->get_recsrc = snd_mixer_oss_get_recsrc1_route;
  995. rslot->put_recsrc = snd_mixer_oss_put_recsrc1_route;
  996. } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CAPTURE) {
  997. mixer->mask_recsrc |= 1 << ptr->oss_id;
  998. }
  999. rslot->private_data = pslot;
  1000. rslot->private_free = snd_mixer_oss_slot_free;
  1001. return 1;
  1002. }
  1003. return 0;
  1004. }
  1005. #ifdef CONFIG_PROC_FS
  1006. /*
  1007. */
  1008. #define MIXER_VOL(name) [SOUND_MIXER_##name] = #name
  1009. static char *oss_mixer_names[SNDRV_OSS_MAX_MIXERS] = {
  1010. MIXER_VOL(VOLUME),
  1011. MIXER_VOL(BASS),
  1012. MIXER_VOL(TREBLE),
  1013. MIXER_VOL(SYNTH),
  1014. MIXER_VOL(PCM),
  1015. MIXER_VOL(SPEAKER),
  1016. MIXER_VOL(LINE),
  1017. MIXER_VOL(MIC),
  1018. MIXER_VOL(CD),
  1019. MIXER_VOL(IMIX),
  1020. MIXER_VOL(ALTPCM),
  1021. MIXER_VOL(RECLEV),
  1022. MIXER_VOL(IGAIN),
  1023. MIXER_VOL(OGAIN),
  1024. MIXER_VOL(LINE1),
  1025. MIXER_VOL(LINE2),
  1026. MIXER_VOL(LINE3),
  1027. MIXER_VOL(DIGITAL1),
  1028. MIXER_VOL(DIGITAL2),
  1029. MIXER_VOL(DIGITAL3),
  1030. MIXER_VOL(PHONEIN),
  1031. MIXER_VOL(PHONEOUT),
  1032. MIXER_VOL(VIDEO),
  1033. MIXER_VOL(RADIO),
  1034. MIXER_VOL(MONITOR),
  1035. };
  1036. /*
  1037. * /proc interface
  1038. */
  1039. static void snd_mixer_oss_proc_read(struct snd_info_entry *entry,
  1040. struct snd_info_buffer *buffer)
  1041. {
  1042. struct snd_mixer_oss *mixer = entry->private_data;
  1043. int i;
  1044. mutex_lock(&mixer->reg_mutex);
  1045. for (i = 0; i < SNDRV_OSS_MAX_MIXERS; i++) {
  1046. struct slot *p;
  1047. if (! oss_mixer_names[i])
  1048. continue;
  1049. p = (struct slot *)mixer->slots[i].private_data;
  1050. snd_iprintf(buffer, "%s ", oss_mixer_names[i]);
  1051. if (p && p->assigned)
  1052. snd_iprintf(buffer, "\"%s\" %d\n",
  1053. p->assigned->name,
  1054. p->assigned->index);
  1055. else
  1056. snd_iprintf(buffer, "\"\" 0\n");
  1057. }
  1058. mutex_unlock(&mixer->reg_mutex);
  1059. }
  1060. static void snd_mixer_oss_proc_write(struct snd_info_entry *entry,
  1061. struct snd_info_buffer *buffer)
  1062. {
  1063. struct snd_mixer_oss *mixer = entry->private_data;
  1064. char line[128], str[32], idxstr[16];
  1065. const char *cptr;
  1066. int ch, idx;
  1067. struct snd_mixer_oss_assign_table *tbl;
  1068. struct slot *slot;
  1069. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  1070. cptr = snd_info_get_str(str, line, sizeof(str));
  1071. for (ch = 0; ch < SNDRV_OSS_MAX_MIXERS; ch++)
  1072. if (oss_mixer_names[ch] && strcmp(oss_mixer_names[ch], str) == 0)
  1073. break;
  1074. if (ch >= SNDRV_OSS_MAX_MIXERS) {
  1075. snd_printk(KERN_ERR "mixer_oss: invalid OSS volume '%s'\n", str);
  1076. continue;
  1077. }
  1078. cptr = snd_info_get_str(str, cptr, sizeof(str));
  1079. if (! *str) {
  1080. /* remove the entry */
  1081. mutex_lock(&mixer->reg_mutex);
  1082. mixer_slot_clear(&mixer->slots[ch]);
  1083. mutex_unlock(&mixer->reg_mutex);
  1084. continue;
  1085. }
  1086. snd_info_get_str(idxstr, cptr, sizeof(idxstr));
  1087. idx = simple_strtoul(idxstr, NULL, 10);
  1088. if (idx >= 0x4000) { /* too big */
  1089. snd_printk(KERN_ERR "mixer_oss: invalid index %d\n", idx);
  1090. continue;
  1091. }
  1092. mutex_lock(&mixer->reg_mutex);
  1093. slot = (struct slot *)mixer->slots[ch].private_data;
  1094. if (slot && slot->assigned &&
  1095. slot->assigned->index == idx && ! strcmp(slot->assigned->name, str))
  1096. /* not changed */
  1097. goto __unlock;
  1098. tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
  1099. if (! tbl) {
  1100. snd_printk(KERN_ERR "mixer_oss: no memory\n");
  1101. goto __unlock;
  1102. }
  1103. tbl->oss_id = ch;
  1104. tbl->name = kstrdup(str, GFP_KERNEL);
  1105. if (! tbl->name) {
  1106. kfree(tbl);
  1107. goto __unlock;
  1108. }
  1109. tbl->index = idx;
  1110. if (snd_mixer_oss_build_input(mixer, tbl, 1, 1) <= 0) {
  1111. kfree(tbl->name);
  1112. kfree(tbl);
  1113. }
  1114. __unlock:
  1115. mutex_unlock(&mixer->reg_mutex);
  1116. }
  1117. }
  1118. static void snd_mixer_oss_proc_init(struct snd_mixer_oss *mixer)
  1119. {
  1120. struct snd_info_entry *entry;
  1121. entry = snd_info_create_card_entry(mixer->card, "oss_mixer",
  1122. mixer->card->proc_root);
  1123. if (! entry)
  1124. return;
  1125. entry->content = SNDRV_INFO_CONTENT_TEXT;
  1126. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  1127. entry->c.text.read = snd_mixer_oss_proc_read;
  1128. entry->c.text.write = snd_mixer_oss_proc_write;
  1129. entry->private_data = mixer;
  1130. if (snd_info_register(entry) < 0) {
  1131. snd_info_free_entry(entry);
  1132. entry = NULL;
  1133. }
  1134. mixer->proc_entry = entry;
  1135. }
  1136. static void snd_mixer_oss_proc_done(struct snd_mixer_oss *mixer)
  1137. {
  1138. snd_info_free_entry(mixer->proc_entry);
  1139. mixer->proc_entry = NULL;
  1140. }
  1141. #else /* !CONFIG_PROC_FS */
  1142. #define snd_mixer_oss_proc_init(mix)
  1143. #define snd_mixer_oss_proc_done(mix)
  1144. #endif /* CONFIG_PROC_FS */
  1145. static void snd_mixer_oss_build(struct snd_mixer_oss *mixer)
  1146. {
  1147. static struct snd_mixer_oss_assign_table table[] = {
  1148. { SOUND_MIXER_VOLUME, "Master", 0 },
  1149. { SOUND_MIXER_VOLUME, "Front", 0 }, /* fallback */
  1150. { SOUND_MIXER_BASS, "Tone Control - Bass", 0 },
  1151. { SOUND_MIXER_TREBLE, "Tone Control - Treble", 0 },
  1152. { SOUND_MIXER_SYNTH, "Synth", 0 },
  1153. { SOUND_MIXER_SYNTH, "FM", 0 }, /* fallback */
  1154. { SOUND_MIXER_SYNTH, "Music", 0 }, /* fallback */
  1155. { SOUND_MIXER_PCM, "PCM", 0 },
  1156. { SOUND_MIXER_SPEAKER, "PC Speaker", 0 },
  1157. { SOUND_MIXER_LINE, "Line", 0 },
  1158. { SOUND_MIXER_MIC, "Mic", 0 },
  1159. { SOUND_MIXER_CD, "CD", 0 },
  1160. { SOUND_MIXER_IMIX, "Monitor Mix", 0 },
  1161. { SOUND_MIXER_ALTPCM, "PCM", 1 },
  1162. { SOUND_MIXER_ALTPCM, "Headphone", 0 }, /* fallback */
  1163. { SOUND_MIXER_ALTPCM, "Wave", 0 }, /* fallback */
  1164. { SOUND_MIXER_RECLEV, "-- nothing --", 0 },
  1165. { SOUND_MIXER_IGAIN, "Capture", 0 },
  1166. { SOUND_MIXER_OGAIN, "Playback", 0 },
  1167. { SOUND_MIXER_LINE1, "Aux", 0 },
  1168. { SOUND_MIXER_LINE2, "Aux", 1 },
  1169. { SOUND_MIXER_LINE3, "Aux", 2 },
  1170. { SOUND_MIXER_DIGITAL1, "Digital", 0 },
  1171. { SOUND_MIXER_DIGITAL1, "IEC958", 0 }, /* fallback */
  1172. { SOUND_MIXER_DIGITAL1, "IEC958 Optical", 0 }, /* fallback */
  1173. { SOUND_MIXER_DIGITAL1, "IEC958 Coaxial", 0 }, /* fallback */
  1174. { SOUND_MIXER_DIGITAL2, "Digital", 1 },
  1175. { SOUND_MIXER_DIGITAL3, "Digital", 2 },
  1176. { SOUND_MIXER_PHONEIN, "Phone", 0 },
  1177. { SOUND_MIXER_PHONEOUT, "Master Mono", 0 },
  1178. { SOUND_MIXER_PHONEOUT, "Speaker", 0 }, /*fallback*/
  1179. { SOUND_MIXER_PHONEOUT, "Mono", 0 }, /*fallback*/
  1180. { SOUND_MIXER_PHONEOUT, "Phone", 0 }, /* fallback */
  1181. { SOUND_MIXER_VIDEO, "Video", 0 },
  1182. { SOUND_MIXER_RADIO, "Radio", 0 },
  1183. { SOUND_MIXER_MONITOR, "Monitor", 0 }
  1184. };
  1185. unsigned int idx;
  1186. for (idx = 0; idx < ARRAY_SIZE(table); idx++)
  1187. snd_mixer_oss_build_input(mixer, &table[idx], 0, 0);
  1188. if (mixer->mask_recsrc) {
  1189. mixer->get_recsrc = snd_mixer_oss_get_recsrc2;
  1190. mixer->put_recsrc = snd_mixer_oss_put_recsrc2;
  1191. }
  1192. }
  1193. /*
  1194. *
  1195. */
  1196. static int snd_mixer_oss_free1(void *private)
  1197. {
  1198. struct snd_mixer_oss *mixer = private;
  1199. struct snd_card *card;
  1200. int idx;
  1201. if (!mixer)
  1202. return 0;
  1203. card = mixer->card;
  1204. if (snd_BUG_ON(mixer != card->mixer_oss))
  1205. return -ENXIO;
  1206. card->mixer_oss = NULL;
  1207. for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++) {
  1208. struct snd_mixer_oss_slot *chn = &mixer->slots[idx];
  1209. if (chn->private_free)
  1210. chn->private_free(chn);
  1211. }
  1212. kfree(mixer);
  1213. return 0;
  1214. }
  1215. static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd)
  1216. {
  1217. struct snd_mixer_oss *mixer;
  1218. if (cmd == SND_MIXER_OSS_NOTIFY_REGISTER) {
  1219. char name[128];
  1220. int idx, err;
  1221. mixer = kcalloc(2, sizeof(*mixer), GFP_KERNEL);
  1222. if (mixer == NULL)
  1223. return -ENOMEM;
  1224. mutex_init(&mixer->reg_mutex);
  1225. sprintf(name, "mixer%i%i", card->number, 0);
  1226. if ((err = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER,
  1227. card, 0,
  1228. &snd_mixer_oss_f_ops, card,
  1229. name)) < 0) {
  1230. snd_printk(KERN_ERR "unable to register OSS mixer device %i:%i\n",
  1231. card->number, 0);
  1232. kfree(mixer);
  1233. return err;
  1234. }
  1235. mixer->oss_dev_alloc = 1;
  1236. mixer->card = card;
  1237. if (*card->mixername)
  1238. strlcpy(mixer->name, card->mixername, sizeof(mixer->name));
  1239. else
  1240. strlcpy(mixer->name, name, sizeof(mixer->name));
  1241. #ifdef SNDRV_OSS_INFO_DEV_MIXERS
  1242. snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIXERS,
  1243. card->number,
  1244. mixer->name);
  1245. #endif
  1246. for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++)
  1247. mixer->slots[idx].number = idx;
  1248. card->mixer_oss = mixer;
  1249. snd_mixer_oss_build(mixer);
  1250. snd_mixer_oss_proc_init(mixer);
  1251. } else {
  1252. mixer = card->mixer_oss;
  1253. if (mixer == NULL)
  1254. return 0;
  1255. if (mixer->oss_dev_alloc) {
  1256. #ifdef SNDRV_OSS_INFO_DEV_MIXERS
  1257. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
  1258. #endif
  1259. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
  1260. mixer->oss_dev_alloc = 0;
  1261. }
  1262. if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
  1263. return 0;
  1264. snd_mixer_oss_proc_done(mixer);
  1265. return snd_mixer_oss_free1(mixer);
  1266. }
  1267. return 0;
  1268. }
  1269. static int __init alsa_mixer_oss_init(void)
  1270. {
  1271. int idx;
  1272. snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler;
  1273. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  1274. if (snd_cards[idx])
  1275. snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_REGISTER);
  1276. }
  1277. return 0;
  1278. }
  1279. static void __exit alsa_mixer_oss_exit(void)
  1280. {
  1281. int idx;
  1282. snd_mixer_oss_notify_callback = NULL;
  1283. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  1284. if (snd_cards[idx])
  1285. snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_FREE);
  1286. }
  1287. }
  1288. module_init(alsa_mixer_oss_init)
  1289. module_exit(alsa_mixer_oss_exit)
  1290. EXPORT_SYMBOL(snd_mixer_oss_ioctl_card);