mixer_oss.c 41 KB

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