mixer_oss.c 41 KB

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