mixer_oss.c 39 KB

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