mixer_oss.c 41 KB

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