mixer_oss.c 39 KB

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