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