mixer_oss.c 39 KB

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