mixer_oss.c 41 KB

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