mixer_oss.c 41 KB

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