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