mixer_oss.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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/smp_lock.h>
  24. #include <linux/slab.h>
  25. #include <linux/time.h>
  26. #include <linux/string.h>
  27. #include <sound/core.h>
  28. #include <sound/minors.h>
  29. #include <sound/control.h>
  30. #include <sound/info.h>
  31. #include <sound/mixer_oss.h>
  32. #include <linux/soundcard.h>
  33. #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  35. MODULE_DESCRIPTION("Mixer OSS emulation for ALSA.");
  36. MODULE_LICENSE("GPL");
  37. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MIXER);
  38. static int snd_mixer_oss_open(struct inode *inode, struct file *file)
  39. {
  40. int cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
  41. snd_card_t *card;
  42. snd_mixer_oss_file_t *fmixer;
  43. int err;
  44. if ((card = snd_cards[cardnum]) == 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. snd_mixer_oss_file_t *fmixer;
  69. if (file->private_data) {
  70. fmixer = (snd_mixer_oss_file_t *) 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(snd_mixer_oss_file_t *fmixer,
  78. mixer_info __user *_info)
  79. {
  80. snd_card_t *card = fmixer->card;
  81. snd_mixer_oss_t *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(snd_mixer_oss_file_t *fmixer,
  92. _old_mixer_info __user *_info)
  93. {
  94. snd_card_t *card = fmixer->card;
  95. snd_mixer_oss_t *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(snd_mixer_oss_file_t *fmixer)
  105. {
  106. snd_mixer_oss_t *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(snd_mixer_oss_file_t *fmixer)
  115. {
  116. snd_mixer_oss_t *mixer = fmixer->mixer;
  117. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *fmixer)
  129. {
  130. snd_mixer_oss_t *mixer = fmixer->mixer;
  131. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *fmixer)
  143. {
  144. snd_mixer_oss_t *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. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *fmixer)
  162. {
  163. snd_mixer_oss_t *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. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *fmixer, int recsrc)
  188. {
  189. snd_mixer_oss_t *mixer = fmixer->mixer;
  190. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *fmixer, int slot)
  223. {
  224. snd_mixer_oss_t *mixer = fmixer->mixer;
  225. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *fmixer,
  246. int slot, int volume)
  247. {
  248. snd_mixer_oss_t *mixer = fmixer->mixer;
  249. snd_mixer_oss_slot_t *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(snd_mixer_oss_file_t *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((snd_mixer_oss_file_t *) file->private_data, cmd, arg);
  336. }
  337. int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg)
  338. {
  339. snd_mixer_oss_file_t 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 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. static snd_minor_t snd_mixer_oss_reg =
  366. {
  367. .comment = "mixer",
  368. .f_ops = &snd_mixer_oss_f_ops,
  369. };
  370. /*
  371. * utilities
  372. */
  373. static long snd_mixer_oss_conv(long val, long omin, long omax, long nmin, long nmax)
  374. {
  375. long orange = omax - omin, nrange = nmax - nmin;
  376. if (orange == 0)
  377. return 0;
  378. return ((nrange * (val - omin)) + (orange / 2)) / orange + nmin;
  379. }
  380. /* convert from alsa native to oss values (0-100) */
  381. static long snd_mixer_oss_conv1(long val, long min, long max, int *old)
  382. {
  383. if (val == snd_mixer_oss_conv(*old, 0, 100, min, max))
  384. return *old;
  385. return snd_mixer_oss_conv(val, min, max, 0, 100);
  386. }
  387. /* convert from oss to alsa native values */
  388. static long snd_mixer_oss_conv2(long val, long min, long max)
  389. {
  390. return snd_mixer_oss_conv(val, 0, 100, min, max);
  391. }
  392. #if 0
  393. static void snd_mixer_oss_recsrce_set(snd_card_t *card, int slot)
  394. {
  395. snd_mixer_oss_t *mixer = card->mixer_oss;
  396. if (mixer)
  397. mixer->mask_recsrc |= 1 << slot;
  398. }
  399. static int snd_mixer_oss_recsrce_get(snd_card_t *card, int slot)
  400. {
  401. snd_mixer_oss_t *mixer = card->mixer_oss;
  402. if (mixer && (mixer->mask_recsrc & (1 << slot)))
  403. return 1;
  404. return 0;
  405. }
  406. #endif
  407. #define SNDRV_MIXER_OSS_SIGNATURE 0x65999250
  408. #define SNDRV_MIXER_OSS_ITEM_GLOBAL 0
  409. #define SNDRV_MIXER_OSS_ITEM_GSWITCH 1
  410. #define SNDRV_MIXER_OSS_ITEM_GROUTE 2
  411. #define SNDRV_MIXER_OSS_ITEM_GVOLUME 3
  412. #define SNDRV_MIXER_OSS_ITEM_PSWITCH 4
  413. #define SNDRV_MIXER_OSS_ITEM_PROUTE 5
  414. #define SNDRV_MIXER_OSS_ITEM_PVOLUME 6
  415. #define SNDRV_MIXER_OSS_ITEM_CSWITCH 7
  416. #define SNDRV_MIXER_OSS_ITEM_CROUTE 8
  417. #define SNDRV_MIXER_OSS_ITEM_CVOLUME 9
  418. #define SNDRV_MIXER_OSS_ITEM_CAPTURE 10
  419. #define SNDRV_MIXER_OSS_ITEM_COUNT 11
  420. #define SNDRV_MIXER_OSS_PRESENT_GLOBAL (1<<0)
  421. #define SNDRV_MIXER_OSS_PRESENT_GSWITCH (1<<1)
  422. #define SNDRV_MIXER_OSS_PRESENT_GROUTE (1<<2)
  423. #define SNDRV_MIXER_OSS_PRESENT_GVOLUME (1<<3)
  424. #define SNDRV_MIXER_OSS_PRESENT_PSWITCH (1<<4)
  425. #define SNDRV_MIXER_OSS_PRESENT_PROUTE (1<<5)
  426. #define SNDRV_MIXER_OSS_PRESENT_PVOLUME (1<<6)
  427. #define SNDRV_MIXER_OSS_PRESENT_CSWITCH (1<<7)
  428. #define SNDRV_MIXER_OSS_PRESENT_CROUTE (1<<8)
  429. #define SNDRV_MIXER_OSS_PRESENT_CVOLUME (1<<9)
  430. #define SNDRV_MIXER_OSS_PRESENT_CAPTURE (1<<10)
  431. struct slot {
  432. unsigned int signature;
  433. unsigned int present;
  434. unsigned int channels;
  435. unsigned int numid[SNDRV_MIXER_OSS_ITEM_COUNT];
  436. unsigned int capture_item;
  437. struct snd_mixer_oss_assign_table *assigned;
  438. unsigned int allocated: 1;
  439. };
  440. #define ID_UNKNOWN ((unsigned int)-1)
  441. static snd_kcontrol_t *snd_mixer_oss_test_id(snd_mixer_oss_t *mixer, const char *name, int index)
  442. {
  443. snd_card_t * card = mixer->card;
  444. snd_ctl_elem_id_t id;
  445. memset(&id, 0, sizeof(id));
  446. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  447. strcpy(id.name, name);
  448. id.index = index;
  449. return snd_ctl_find_id(card, &id);
  450. }
  451. static void snd_mixer_oss_get_volume1_vol(snd_mixer_oss_file_t *fmixer,
  452. snd_mixer_oss_slot_t *pslot,
  453. unsigned int numid,
  454. int *left, int *right)
  455. {
  456. snd_ctl_elem_info_t *uinfo;
  457. snd_ctl_elem_value_t *uctl;
  458. snd_kcontrol_t *kctl;
  459. snd_card_t *card = fmixer->card;
  460. if (numid == ID_UNKNOWN)
  461. return;
  462. down_read(&card->controls_rwsem);
  463. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
  464. up_read(&card->controls_rwsem);
  465. return;
  466. }
  467. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  468. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  469. if (uinfo == NULL || uctl == NULL)
  470. goto __unalloc;
  471. if (kctl->info(kctl, uinfo))
  472. goto __unalloc;
  473. if (kctl->get(kctl, uctl))
  474. goto __unalloc;
  475. if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN &&
  476. uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1)
  477. goto __unalloc;
  478. *left = snd_mixer_oss_conv1(uctl->value.integer.value[0], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[0]);
  479. if (uinfo->count > 1)
  480. *right = snd_mixer_oss_conv1(uctl->value.integer.value[1], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[1]);
  481. __unalloc:
  482. up_read(&card->controls_rwsem);
  483. kfree(uctl);
  484. kfree(uinfo);
  485. }
  486. static void snd_mixer_oss_get_volume1_sw(snd_mixer_oss_file_t *fmixer,
  487. snd_mixer_oss_slot_t *pslot,
  488. unsigned int numid,
  489. int *left, int *right,
  490. int route)
  491. {
  492. snd_ctl_elem_info_t *uinfo;
  493. snd_ctl_elem_value_t *uctl;
  494. snd_kcontrol_t *kctl;
  495. snd_card_t *card = fmixer->card;
  496. if (numid == ID_UNKNOWN)
  497. return;
  498. down_read(&card->controls_rwsem);
  499. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
  500. up_read(&card->controls_rwsem);
  501. return;
  502. }
  503. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  504. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  505. if (uinfo == NULL || uctl == NULL)
  506. goto __unalloc;
  507. if (kctl->info(kctl, uinfo))
  508. goto __unalloc;
  509. if (kctl->get(kctl, uctl))
  510. goto __unalloc;
  511. if (!uctl->value.integer.value[0]) {
  512. *left = 0;
  513. if (uinfo->count == 1)
  514. *right = 0;
  515. }
  516. if (uinfo->count > 1 && !uctl->value.integer.value[route ? 3 : 1])
  517. *right = 0;
  518. __unalloc:
  519. up_read(&card->controls_rwsem);
  520. kfree(uctl);
  521. kfree(uinfo);
  522. }
  523. static int snd_mixer_oss_get_volume1(snd_mixer_oss_file_t *fmixer,
  524. snd_mixer_oss_slot_t *pslot,
  525. int *left, int *right)
  526. {
  527. struct slot *slot = (struct slot *)pslot->private_data;
  528. *left = *right = 100;
  529. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) {
  530. snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right);
  531. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) {
  532. snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right);
  533. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) {
  534. snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right);
  535. }
  536. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
  537. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
  538. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
  539. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
  540. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
  541. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
  542. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
  543. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
  544. }
  545. return 0;
  546. }
  547. static void snd_mixer_oss_put_volume1_vol(snd_mixer_oss_file_t *fmixer,
  548. snd_mixer_oss_slot_t *pslot,
  549. unsigned int numid,
  550. int left, int right)
  551. {
  552. snd_ctl_elem_info_t *uinfo;
  553. snd_ctl_elem_value_t *uctl;
  554. snd_kcontrol_t *kctl;
  555. snd_card_t *card = fmixer->card;
  556. int res;
  557. if (numid == ID_UNKNOWN)
  558. return;
  559. down_read(&card->controls_rwsem);
  560. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL)
  561. return;
  562. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  563. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  564. if (uinfo == NULL || uctl == NULL)
  565. goto __unalloc;
  566. if (kctl->info(kctl, uinfo))
  567. goto __unalloc;
  568. if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN &&
  569. uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1)
  570. goto __unalloc;
  571. uctl->value.integer.value[0] = snd_mixer_oss_conv2(left, uinfo->value.integer.min, uinfo->value.integer.max);
  572. if (uinfo->count > 1)
  573. uctl->value.integer.value[1] = snd_mixer_oss_conv2(right, uinfo->value.integer.min, uinfo->value.integer.max);
  574. if ((res = kctl->put(kctl, uctl)) < 0)
  575. goto __unalloc;
  576. if (res > 0)
  577. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  578. __unalloc:
  579. up_read(&card->controls_rwsem);
  580. kfree(uctl);
  581. kfree(uinfo);
  582. }
  583. static void snd_mixer_oss_put_volume1_sw(snd_mixer_oss_file_t *fmixer,
  584. snd_mixer_oss_slot_t *pslot,
  585. unsigned int numid,
  586. int left, int right,
  587. int route)
  588. {
  589. snd_ctl_elem_info_t *uinfo;
  590. snd_ctl_elem_value_t *uctl;
  591. snd_kcontrol_t *kctl;
  592. snd_card_t *card = fmixer->card;
  593. int res;
  594. if (numid == ID_UNKNOWN)
  595. return;
  596. down_read(&card->controls_rwsem);
  597. if ((kctl = snd_ctl_find_numid(card, numid)) == NULL) {
  598. up_read(&fmixer->card->controls_rwsem);
  599. return;
  600. }
  601. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  602. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  603. if (uinfo == NULL || uctl == NULL)
  604. goto __unalloc;
  605. if (kctl->info(kctl, uinfo))
  606. goto __unalloc;
  607. if (uinfo->count > 1) {
  608. uctl->value.integer.value[0] = left > 0 ? 1 : 0;
  609. uctl->value.integer.value[route ? 3 : 1] = right > 0 ? 1 : 0;
  610. if (route) {
  611. uctl->value.integer.value[1] =
  612. uctl->value.integer.value[2] = 0;
  613. }
  614. } else {
  615. uctl->value.integer.value[0] = (left > 0 || right > 0) ? 1 : 0;
  616. }
  617. if ((res = kctl->put(kctl, uctl)) < 0)
  618. goto __unalloc;
  619. if (res > 0)
  620. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  621. __unalloc:
  622. up_read(&card->controls_rwsem);
  623. kfree(uctl);
  624. kfree(uinfo);
  625. }
  626. static int snd_mixer_oss_put_volume1(snd_mixer_oss_file_t *fmixer,
  627. snd_mixer_oss_slot_t *pslot,
  628. int left, int right)
  629. {
  630. struct slot *slot = (struct slot *)pslot->private_data;
  631. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) {
  632. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right);
  633. if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME)
  634. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right);
  635. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) {
  636. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right);
  637. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) {
  638. snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right);
  639. }
  640. if (left || right) {
  641. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH)
  642. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
  643. if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH)
  644. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
  645. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE)
  646. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
  647. if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE)
  648. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
  649. } else {
  650. if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) {
  651. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0);
  652. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) {
  653. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0);
  654. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) {
  655. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1);
  656. } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) {
  657. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1);
  658. }
  659. }
  660. return 0;
  661. }
  662. static int snd_mixer_oss_get_recsrc1_sw(snd_mixer_oss_file_t *fmixer,
  663. snd_mixer_oss_slot_t *pslot,
  664. int *active)
  665. {
  666. struct slot *slot = (struct slot *)pslot->private_data;
  667. int left, right;
  668. left = right = 1;
  669. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], &left, &right, 0);
  670. *active = (left || right) ? 1 : 0;
  671. return 0;
  672. }
  673. static int snd_mixer_oss_get_recsrc1_route(snd_mixer_oss_file_t *fmixer,
  674. snd_mixer_oss_slot_t *pslot,
  675. int *active)
  676. {
  677. struct slot *slot = (struct slot *)pslot->private_data;
  678. int left, right;
  679. left = right = 1;
  680. snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], &left, &right, 1);
  681. *active = (left || right) ? 1 : 0;
  682. return 0;
  683. }
  684. static int snd_mixer_oss_put_recsrc1_sw(snd_mixer_oss_file_t *fmixer,
  685. snd_mixer_oss_slot_t *pslot,
  686. int active)
  687. {
  688. struct slot *slot = (struct slot *)pslot->private_data;
  689. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], active, active, 0);
  690. return 0;
  691. }
  692. static int snd_mixer_oss_put_recsrc1_route(snd_mixer_oss_file_t *fmixer,
  693. snd_mixer_oss_slot_t *pslot,
  694. int active)
  695. {
  696. struct slot *slot = (struct slot *)pslot->private_data;
  697. snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], active, active, 1);
  698. return 0;
  699. }
  700. static int snd_mixer_oss_get_recsrc2(snd_mixer_oss_file_t *fmixer, unsigned int *active_index)
  701. {
  702. snd_card_t *card = fmixer->card;
  703. snd_mixer_oss_t *mixer = fmixer->mixer;
  704. snd_kcontrol_t *kctl;
  705. snd_mixer_oss_slot_t *pslot;
  706. struct slot *slot;
  707. snd_ctl_elem_info_t *uinfo;
  708. snd_ctl_elem_value_t *uctl;
  709. int err, idx;
  710. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  711. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  712. if (uinfo == NULL || uctl == NULL) {
  713. err = -ENOMEM;
  714. goto __unlock;
  715. }
  716. down_read(&card->controls_rwsem);
  717. kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
  718. if (! kctl) {
  719. err = -ENOENT;
  720. goto __unlock;
  721. }
  722. if ((err = kctl->info(kctl, uinfo)) < 0)
  723. goto __unlock;
  724. if ((err = kctl->get(kctl, uctl)) < 0)
  725. goto __unlock;
  726. for (idx = 0; idx < 32; idx++) {
  727. if (!(mixer->mask_recsrc & (1 << idx)))
  728. continue;
  729. pslot = &mixer->slots[idx];
  730. slot = (struct slot *)pslot->private_data;
  731. if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
  732. continue;
  733. if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
  734. continue;
  735. if (slot->capture_item == uctl->value.enumerated.item[0]) {
  736. *active_index = idx;
  737. break;
  738. }
  739. }
  740. err = 0;
  741. __unlock:
  742. up_read(&card->controls_rwsem);
  743. kfree(uctl);
  744. kfree(uinfo);
  745. return err;
  746. }
  747. static int snd_mixer_oss_put_recsrc2(snd_mixer_oss_file_t *fmixer, unsigned int active_index)
  748. {
  749. snd_card_t *card = fmixer->card;
  750. snd_mixer_oss_t *mixer = fmixer->mixer;
  751. snd_kcontrol_t *kctl;
  752. snd_mixer_oss_slot_t *pslot;
  753. struct slot *slot = NULL;
  754. snd_ctl_elem_info_t *uinfo;
  755. snd_ctl_elem_value_t *uctl;
  756. int err;
  757. unsigned int idx;
  758. uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL);
  759. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  760. if (uinfo == NULL || uctl == NULL) {
  761. err = -ENOMEM;
  762. goto __unlock;
  763. }
  764. down_read(&card->controls_rwsem);
  765. kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0);
  766. if (! kctl) {
  767. err = -ENOENT;
  768. goto __unlock;
  769. }
  770. if ((err = kctl->info(kctl, uinfo)) < 0)
  771. goto __unlock;
  772. for (idx = 0; idx < 32; idx++) {
  773. if (!(mixer->mask_recsrc & (1 << idx)))
  774. continue;
  775. pslot = &mixer->slots[idx];
  776. slot = (struct slot *)pslot->private_data;
  777. if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE)
  778. continue;
  779. if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE))
  780. continue;
  781. if (idx == active_index)
  782. break;
  783. slot = NULL;
  784. }
  785. if (! slot)
  786. goto __unlock;
  787. for (idx = 0; idx < uinfo->count; idx++)
  788. uctl->value.enumerated.item[idx] = slot->capture_item;
  789. err = kctl->put(kctl, uctl);
  790. if (err > 0)
  791. snd_ctl_notify(fmixer->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  792. err = 0;
  793. __unlock:
  794. up_read(&card->controls_rwsem);
  795. kfree(uctl);
  796. kfree(uinfo);
  797. return err;
  798. }
  799. struct snd_mixer_oss_assign_table {
  800. int oss_id;
  801. const char *name;
  802. int index;
  803. };
  804. static int snd_mixer_oss_build_test(snd_mixer_oss_t *mixer, struct slot *slot, const char *name, int index, int item)
  805. {
  806. snd_ctl_elem_info_t *info;
  807. snd_kcontrol_t *kcontrol;
  808. snd_card_t *card = mixer->card;
  809. int err;
  810. down_read(&card->controls_rwsem);
  811. kcontrol = snd_mixer_oss_test_id(mixer, name, index);
  812. if (kcontrol == NULL) {
  813. up_read(&card->controls_rwsem);
  814. return 0;
  815. }
  816. info = kmalloc(sizeof(*info), GFP_KERNEL);
  817. if (! info) {
  818. up_read(&card->controls_rwsem);
  819. return -ENOMEM;
  820. }
  821. if ((err = kcontrol->info(kcontrol, info)) < 0) {
  822. up_read(&card->controls_rwsem);
  823. kfree(info);
  824. return err;
  825. }
  826. slot->numid[item] = kcontrol->id.numid;
  827. up_read(&card->controls_rwsem);
  828. if (info->count > slot->channels)
  829. slot->channels = info->count;
  830. slot->present |= 1 << item;
  831. kfree(info);
  832. return 0;
  833. }
  834. static void snd_mixer_oss_slot_free(snd_mixer_oss_slot_t *chn)
  835. {
  836. struct slot *p = (struct slot *)chn->private_data;
  837. if (p) {
  838. if (p->allocated && p->assigned) {
  839. kfree(p->assigned->name);
  840. kfree(p->assigned);
  841. }
  842. kfree(p);
  843. }
  844. }
  845. static void mixer_slot_clear(snd_mixer_oss_slot_t *rslot)
  846. {
  847. int idx = rslot->number; /* remember this */
  848. if (rslot->private_free)
  849. rslot->private_free(rslot);
  850. memset(rslot, 0, sizeof(*rslot));
  851. rslot->number = idx;
  852. }
  853. /*
  854. * build an OSS mixer element.
  855. * ptr_allocated means the entry is dynamically allocated (change via proc file).
  856. * when replace_old = 1, the old entry is replaced with the new one.
  857. */
  858. static int snd_mixer_oss_build_input(snd_mixer_oss_t *mixer, struct snd_mixer_oss_assign_table *ptr, int ptr_allocated, int replace_old)
  859. {
  860. struct slot slot;
  861. struct slot *pslot;
  862. snd_kcontrol_t *kctl;
  863. snd_mixer_oss_slot_t *rslot;
  864. char str[64];
  865. /* check if already assigned */
  866. if (mixer->slots[ptr->oss_id].get_volume && ! replace_old)
  867. return 0;
  868. memset(&slot, 0, sizeof(slot));
  869. memset(slot.numid, 0xff, sizeof(slot.numid)); /* ID_UNKNOWN */
  870. if (snd_mixer_oss_build_test(mixer, &slot, ptr->name, ptr->index,
  871. SNDRV_MIXER_OSS_ITEM_GLOBAL))
  872. return 0;
  873. sprintf(str, "%s Switch", ptr->name);
  874. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  875. SNDRV_MIXER_OSS_ITEM_GSWITCH))
  876. return 0;
  877. sprintf(str, "%s Route", ptr->name);
  878. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  879. SNDRV_MIXER_OSS_ITEM_GROUTE))
  880. return 0;
  881. sprintf(str, "%s Volume", ptr->name);
  882. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  883. SNDRV_MIXER_OSS_ITEM_GVOLUME))
  884. return 0;
  885. sprintf(str, "%s Playback Switch", ptr->name);
  886. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  887. SNDRV_MIXER_OSS_ITEM_PSWITCH))
  888. return 0;
  889. sprintf(str, "%s Playback Route", ptr->name);
  890. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  891. SNDRV_MIXER_OSS_ITEM_PROUTE))
  892. return 0;
  893. sprintf(str, "%s Playback Volume", ptr->name);
  894. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  895. SNDRV_MIXER_OSS_ITEM_PVOLUME))
  896. return 0;
  897. sprintf(str, "%s Capture Switch", ptr->name);
  898. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  899. SNDRV_MIXER_OSS_ITEM_CSWITCH))
  900. return 0;
  901. sprintf(str, "%s Capture Route", ptr->name);
  902. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  903. SNDRV_MIXER_OSS_ITEM_CROUTE))
  904. return 0;
  905. sprintf(str, "%s Capture Volume", ptr->name);
  906. if (snd_mixer_oss_build_test(mixer, &slot, str, ptr->index,
  907. SNDRV_MIXER_OSS_ITEM_CVOLUME))
  908. return 0;
  909. down_read(&mixer->card->controls_rwsem);
  910. if (ptr->index == 0 && (kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0)) != NULL) {
  911. snd_ctl_elem_info_t *uinfo;
  912. uinfo = kmalloc(sizeof(*uinfo), GFP_KERNEL);
  913. if (! uinfo) {
  914. up_read(&mixer->card->controls_rwsem);
  915. return -ENOMEM;
  916. }
  917. memset(uinfo, 0, sizeof(*uinfo));
  918. if (kctl->info(kctl, uinfo)) {
  919. up_read(&mixer->card->controls_rwsem);
  920. return 0;
  921. }
  922. strcpy(str, ptr->name);
  923. if (!strcmp(str, "Master"))
  924. strcpy(str, "Mix");
  925. if (!strcmp(str, "Master Mono"))
  926. strcpy(str, "Mix Mono");
  927. slot.capture_item = 0;
  928. if (!strcmp(uinfo->value.enumerated.name, str)) {
  929. slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
  930. } else {
  931. for (slot.capture_item = 1; slot.capture_item < uinfo->value.enumerated.items; slot.capture_item++) {
  932. uinfo->value.enumerated.item = slot.capture_item;
  933. if (kctl->info(kctl, uinfo)) {
  934. up_read(&mixer->card->controls_rwsem);
  935. return 0;
  936. }
  937. if (!strcmp(uinfo->value.enumerated.name, str)) {
  938. slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
  939. break;
  940. }
  941. }
  942. }
  943. kfree(uinfo);
  944. }
  945. up_read(&mixer->card->controls_rwsem);
  946. if (slot.present != 0) {
  947. pslot = (struct slot *)kmalloc(sizeof(slot), GFP_KERNEL);
  948. if (! pslot)
  949. return -ENOMEM;
  950. *pslot = slot;
  951. pslot->signature = SNDRV_MIXER_OSS_SIGNATURE;
  952. pslot->assigned = ptr;
  953. pslot->allocated = ptr_allocated;
  954. rslot = &mixer->slots[ptr->oss_id];
  955. mixer_slot_clear(rslot);
  956. rslot->stereo = slot.channels > 1 ? 1 : 0;
  957. rslot->get_volume = snd_mixer_oss_get_volume1;
  958. rslot->put_volume = snd_mixer_oss_put_volume1;
  959. /* note: ES18xx have both Capture Source and XX Capture Volume !!! */
  960. if (slot.present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) {
  961. rslot->get_recsrc = snd_mixer_oss_get_recsrc1_sw;
  962. rslot->put_recsrc = snd_mixer_oss_put_recsrc1_sw;
  963. } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CROUTE) {
  964. rslot->get_recsrc = snd_mixer_oss_get_recsrc1_route;
  965. rslot->put_recsrc = snd_mixer_oss_put_recsrc1_route;
  966. } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CAPTURE) {
  967. mixer->mask_recsrc |= 1 << ptr->oss_id;
  968. }
  969. rslot->private_data = pslot;
  970. rslot->private_free = snd_mixer_oss_slot_free;
  971. return 1;
  972. }
  973. return 0;
  974. }
  975. /*
  976. */
  977. #define MIXER_VOL(name) [SOUND_MIXER_##name] = #name
  978. static char *oss_mixer_names[SNDRV_OSS_MAX_MIXERS] = {
  979. MIXER_VOL(VOLUME),
  980. MIXER_VOL(BASS),
  981. MIXER_VOL(TREBLE),
  982. MIXER_VOL(SYNTH),
  983. MIXER_VOL(PCM),
  984. MIXER_VOL(SPEAKER),
  985. MIXER_VOL(LINE),
  986. MIXER_VOL(MIC),
  987. MIXER_VOL(CD),
  988. MIXER_VOL(IMIX),
  989. MIXER_VOL(ALTPCM),
  990. MIXER_VOL(RECLEV),
  991. MIXER_VOL(IGAIN),
  992. MIXER_VOL(OGAIN),
  993. MIXER_VOL(LINE1),
  994. MIXER_VOL(LINE2),
  995. MIXER_VOL(LINE3),
  996. MIXER_VOL(DIGITAL1),
  997. MIXER_VOL(DIGITAL2),
  998. MIXER_VOL(DIGITAL3),
  999. MIXER_VOL(PHONEIN),
  1000. MIXER_VOL(PHONEOUT),
  1001. MIXER_VOL(VIDEO),
  1002. MIXER_VOL(RADIO),
  1003. MIXER_VOL(MONITOR),
  1004. };
  1005. /*
  1006. * /proc interface
  1007. */
  1008. static void snd_mixer_oss_proc_read(snd_info_entry_t *entry,
  1009. snd_info_buffer_t * buffer)
  1010. {
  1011. snd_mixer_oss_t *mixer = entry->private_data;
  1012. int i;
  1013. down(&mixer->reg_mutex);
  1014. for (i = 0; i < SNDRV_OSS_MAX_MIXERS; i++) {
  1015. struct slot *p;
  1016. if (! oss_mixer_names[i])
  1017. continue;
  1018. p = (struct slot *)mixer->slots[i].private_data;
  1019. snd_iprintf(buffer, "%s ", oss_mixer_names[i]);
  1020. if (p && p->assigned)
  1021. snd_iprintf(buffer, "\"%s\" %d\n",
  1022. p->assigned->name,
  1023. p->assigned->index);
  1024. else
  1025. snd_iprintf(buffer, "\"\" 0\n");
  1026. }
  1027. up(&mixer->reg_mutex);
  1028. }
  1029. static void snd_mixer_oss_proc_write(snd_info_entry_t *entry,
  1030. snd_info_buffer_t * buffer)
  1031. {
  1032. snd_mixer_oss_t *mixer = entry->private_data;
  1033. char line[128], str[32], idxstr[16], *cptr;
  1034. int ch, idx;
  1035. struct snd_mixer_oss_assign_table *tbl;
  1036. struct slot *slot;
  1037. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  1038. cptr = snd_info_get_str(str, line, sizeof(str));
  1039. for (ch = 0; ch < SNDRV_OSS_MAX_MIXERS; ch++)
  1040. if (oss_mixer_names[ch] && strcmp(oss_mixer_names[ch], str) == 0)
  1041. break;
  1042. if (ch >= SNDRV_OSS_MAX_MIXERS) {
  1043. snd_printk(KERN_ERR "mixer_oss: invalid OSS volume '%s'\n", str);
  1044. continue;
  1045. }
  1046. cptr = snd_info_get_str(str, cptr, sizeof(str));
  1047. if (! *str) {
  1048. /* remove the entry */
  1049. down(&mixer->reg_mutex);
  1050. mixer_slot_clear(&mixer->slots[ch]);
  1051. up(&mixer->reg_mutex);
  1052. continue;
  1053. }
  1054. snd_info_get_str(idxstr, cptr, sizeof(idxstr));
  1055. idx = simple_strtoul(idxstr, NULL, 10);
  1056. if (idx >= 0x4000) { /* too big */
  1057. snd_printk(KERN_ERR "mixer_oss: invalid index %d\n", idx);
  1058. continue;
  1059. }
  1060. down(&mixer->reg_mutex);
  1061. slot = (struct slot *)mixer->slots[ch].private_data;
  1062. if (slot && slot->assigned &&
  1063. slot->assigned->index == idx && ! strcmp(slot->assigned->name, str))
  1064. /* not changed */
  1065. goto __unlock;
  1066. tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
  1067. if (! tbl) {
  1068. snd_printk(KERN_ERR "mixer_oss: no memory\n");
  1069. goto __unlock;
  1070. }
  1071. tbl->oss_id = ch;
  1072. tbl->name = kstrdup(str, GFP_KERNEL);
  1073. if (! tbl->name) {
  1074. kfree(tbl);
  1075. goto __unlock;
  1076. }
  1077. tbl->index = idx;
  1078. if (snd_mixer_oss_build_input(mixer, tbl, 1, 1) <= 0) {
  1079. kfree(tbl->name);
  1080. kfree(tbl);
  1081. }
  1082. __unlock:
  1083. up(&mixer->reg_mutex);
  1084. }
  1085. }
  1086. static void snd_mixer_oss_proc_init(snd_mixer_oss_t *mixer)
  1087. {
  1088. snd_info_entry_t *entry;
  1089. entry = snd_info_create_card_entry(mixer->card, "oss_mixer",
  1090. mixer->card->proc_root);
  1091. if (! entry)
  1092. return;
  1093. entry->content = SNDRV_INFO_CONTENT_TEXT;
  1094. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  1095. entry->c.text.read_size = 8192;
  1096. entry->c.text.read = snd_mixer_oss_proc_read;
  1097. entry->c.text.write_size = 8192;
  1098. entry->c.text.write = snd_mixer_oss_proc_write;
  1099. entry->private_data = mixer;
  1100. if (snd_info_register(entry) < 0) {
  1101. snd_info_free_entry(entry);
  1102. entry = NULL;
  1103. }
  1104. mixer->proc_entry = entry;
  1105. }
  1106. static void snd_mixer_oss_proc_done(snd_mixer_oss_t *mixer)
  1107. {
  1108. if (mixer->proc_entry) {
  1109. snd_info_unregister(mixer->proc_entry);
  1110. mixer->proc_entry = NULL;
  1111. }
  1112. }
  1113. static void snd_mixer_oss_build(snd_mixer_oss_t *mixer)
  1114. {
  1115. static struct snd_mixer_oss_assign_table table[] = {
  1116. { SOUND_MIXER_VOLUME, "Master", 0 },
  1117. { SOUND_MIXER_VOLUME, "Front", 0 }, /* fallback */
  1118. { SOUND_MIXER_BASS, "Tone Control - Bass", 0 },
  1119. { SOUND_MIXER_TREBLE, "Tone Control - Treble", 0 },
  1120. { SOUND_MIXER_SYNTH, "Synth", 0 },
  1121. { SOUND_MIXER_SYNTH, "FM", 0 }, /* fallback */
  1122. { SOUND_MIXER_SYNTH, "Music", 0 }, /* fallback */
  1123. { SOUND_MIXER_PCM, "PCM", 0 },
  1124. { SOUND_MIXER_SPEAKER, "PC Speaker", 0 },
  1125. { SOUND_MIXER_LINE, "Line", 0 },
  1126. { SOUND_MIXER_MIC, "Mic", 0 },
  1127. { SOUND_MIXER_CD, "CD", 0 },
  1128. { SOUND_MIXER_IMIX, "Monitor Mix", 0 },
  1129. { SOUND_MIXER_ALTPCM, "PCM", 1 },
  1130. { SOUND_MIXER_ALTPCM, "Headphone", 0 }, /* fallback */
  1131. { SOUND_MIXER_ALTPCM, "Wave", 0 }, /* fallback */
  1132. { SOUND_MIXER_RECLEV, "-- nothing --", 0 },
  1133. { SOUND_MIXER_IGAIN, "Capture", 0 },
  1134. { SOUND_MIXER_OGAIN, "Playback", 0 },
  1135. { SOUND_MIXER_LINE1, "Aux", 0 },
  1136. { SOUND_MIXER_LINE2, "Aux", 1 },
  1137. { SOUND_MIXER_LINE3, "Aux", 2 },
  1138. { SOUND_MIXER_DIGITAL1, "Digital", 0 },
  1139. { SOUND_MIXER_DIGITAL1, "IEC958", 0 }, /* fallback */
  1140. { SOUND_MIXER_DIGITAL1, "IEC958 Optical", 0 }, /* fallback */
  1141. { SOUND_MIXER_DIGITAL1, "IEC958 Coaxial", 0 }, /* fallback */
  1142. { SOUND_MIXER_DIGITAL2, "Digital", 1 },
  1143. { SOUND_MIXER_DIGITAL3, "Digital", 2 },
  1144. { SOUND_MIXER_PHONEIN, "Phone", 0 },
  1145. { SOUND_MIXER_PHONEOUT, "Master Mono", 0 },
  1146. { SOUND_MIXER_PHONEOUT, "Phone", 0 }, /* fallback */
  1147. { SOUND_MIXER_VIDEO, "Video", 0 },
  1148. { SOUND_MIXER_RADIO, "Radio", 0 },
  1149. { SOUND_MIXER_MONITOR, "Monitor", 0 }
  1150. };
  1151. unsigned int idx;
  1152. for (idx = 0; idx < ARRAY_SIZE(table); idx++)
  1153. snd_mixer_oss_build_input(mixer, &table[idx], 0, 0);
  1154. if (mixer->mask_recsrc) {
  1155. mixer->get_recsrc = snd_mixer_oss_get_recsrc2;
  1156. mixer->put_recsrc = snd_mixer_oss_put_recsrc2;
  1157. }
  1158. }
  1159. /*
  1160. *
  1161. */
  1162. static int snd_mixer_oss_free1(void *private)
  1163. {
  1164. snd_mixer_oss_t *mixer = private;
  1165. snd_card_t * card;
  1166. int idx;
  1167. snd_assert(mixer != NULL, return -ENXIO);
  1168. card = mixer->card;
  1169. snd_assert(mixer == card->mixer_oss, return -ENXIO);
  1170. card->mixer_oss = NULL;
  1171. for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++) {
  1172. snd_mixer_oss_slot_t *chn = &mixer->slots[idx];
  1173. if (chn->private_free)
  1174. chn->private_free(chn);
  1175. }
  1176. kfree(mixer);
  1177. return 0;
  1178. }
  1179. static int snd_mixer_oss_notify_handler(snd_card_t * card, int cmd)
  1180. {
  1181. snd_mixer_oss_t *mixer;
  1182. if (cmd == SND_MIXER_OSS_NOTIFY_REGISTER) {
  1183. char name[128];
  1184. int idx, err;
  1185. mixer = kcalloc(2, sizeof(*mixer), GFP_KERNEL);
  1186. if (mixer == NULL)
  1187. return -ENOMEM;
  1188. init_MUTEX(&mixer->reg_mutex);
  1189. sprintf(name, "mixer%i%i", card->number, 0);
  1190. if ((err = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER,
  1191. card, 0,
  1192. &snd_mixer_oss_reg,
  1193. name)) < 0) {
  1194. snd_printk(KERN_ERR "unable to register OSS mixer device %i:%i\n",
  1195. card->number, 0);
  1196. kfree(mixer);
  1197. return err;
  1198. }
  1199. mixer->oss_dev_alloc = 1;
  1200. mixer->card = card;
  1201. if (*card->mixername)
  1202. strlcpy(mixer->name, card->mixername, sizeof(mixer->name));
  1203. else
  1204. strlcpy(mixer->name, name, sizeof(mixer->name));
  1205. #ifdef SNDRV_OSS_INFO_DEV_MIXERS
  1206. snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIXERS,
  1207. card->number,
  1208. mixer->name);
  1209. #endif
  1210. for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++)
  1211. mixer->slots[idx].number = idx;
  1212. card->mixer_oss = mixer;
  1213. snd_mixer_oss_build(mixer);
  1214. snd_mixer_oss_proc_init(mixer);
  1215. } else if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT) {
  1216. mixer = card->mixer_oss;
  1217. if (mixer == NULL || !mixer->oss_dev_alloc)
  1218. return 0;
  1219. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
  1220. mixer->oss_dev_alloc = 0;
  1221. } else { /* free */
  1222. mixer = card->mixer_oss;
  1223. if (mixer == NULL)
  1224. return 0;
  1225. #ifdef SNDRV_OSS_INFO_DEV_MIXERS
  1226. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
  1227. #endif
  1228. if (mixer->oss_dev_alloc)
  1229. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
  1230. snd_mixer_oss_proc_done(mixer);
  1231. return snd_mixer_oss_free1(mixer);
  1232. }
  1233. return 0;
  1234. }
  1235. static int __init alsa_mixer_oss_init(void)
  1236. {
  1237. int idx;
  1238. snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler;
  1239. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  1240. if (snd_cards[idx])
  1241. snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_REGISTER);
  1242. }
  1243. return 0;
  1244. }
  1245. static void __exit alsa_mixer_oss_exit(void)
  1246. {
  1247. int idx;
  1248. snd_mixer_oss_notify_callback = NULL;
  1249. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  1250. if (snd_cards[idx])
  1251. snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_FREE);
  1252. }
  1253. }
  1254. module_init(alsa_mixer_oss_init)
  1255. module_exit(alsa_mixer_oss_exit)
  1256. EXPORT_SYMBOL(snd_mixer_oss_ioctl_card);