mixer_oss.c 40 KB

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