mixer_oss.c 41 KB

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