mixer_oss.c 41 KB

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