sound_oss.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Advanced Linux Sound Architecture
  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. #ifdef CONFIG_SND_OSSEMUL
  23. #if !defined(CONFIG_SOUND) && !(defined(MODULE) && defined(CONFIG_SOUND_MODULE))
  24. #error "Enable the OSS soundcore multiplexer (CONFIG_SOUND) in the kernel."
  25. #endif
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/time.h>
  29. #include <sound/core.h>
  30. #include <sound/minors.h>
  31. #include <sound/info.h>
  32. #include <linux/sound.h>
  33. #define SNDRV_OS_MINORS 256
  34. static struct list_head snd_oss_minors_hash[SNDRV_CARDS];
  35. static DECLARE_MUTEX(sound_oss_mutex);
  36. static struct snd_minor *snd_oss_minor_search(int minor)
  37. {
  38. struct list_head *list;
  39. struct snd_minor *mptr;
  40. list_for_each(list, &snd_oss_minors_hash[SNDRV_MINOR_OSS_CARD(minor)]) {
  41. mptr = list_entry(list, struct snd_minor, list);
  42. if (mptr->number == minor)
  43. return mptr;
  44. }
  45. return NULL;
  46. }
  47. static int snd_oss_kernel_minor(int type, struct snd_card *card, int dev)
  48. {
  49. int minor;
  50. switch (type) {
  51. case SNDRV_OSS_DEVICE_TYPE_MIXER:
  52. snd_assert(card != NULL && dev <= 1, return -EINVAL);
  53. minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIXER1 : SNDRV_MINOR_OSS_MIXER));
  54. break;
  55. case SNDRV_OSS_DEVICE_TYPE_SEQUENCER:
  56. minor = SNDRV_MINOR_OSS_SEQUENCER;
  57. break;
  58. case SNDRV_OSS_DEVICE_TYPE_MUSIC:
  59. minor = SNDRV_MINOR_OSS_MUSIC;
  60. break;
  61. case SNDRV_OSS_DEVICE_TYPE_PCM:
  62. snd_assert(card != NULL && dev <= 1, return -EINVAL);
  63. minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_PCM1 : SNDRV_MINOR_OSS_PCM));
  64. break;
  65. case SNDRV_OSS_DEVICE_TYPE_MIDI:
  66. snd_assert(card != NULL && dev <= 1, return -EINVAL);
  67. minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIDI1 : SNDRV_MINOR_OSS_MIDI));
  68. break;
  69. case SNDRV_OSS_DEVICE_TYPE_DMFM:
  70. minor = SNDRV_MINOR_OSS(card->number, SNDRV_MINOR_OSS_DMFM);
  71. break;
  72. case SNDRV_OSS_DEVICE_TYPE_SNDSTAT:
  73. minor = SNDRV_MINOR_OSS_SNDSTAT;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
  79. return minor;
  80. }
  81. int snd_register_oss_device(int type, struct snd_card *card, int dev,
  82. struct snd_minor * reg, const char *name)
  83. {
  84. int minor = snd_oss_kernel_minor(type, card, dev);
  85. int minor_unit;
  86. struct snd_minor *preg;
  87. int cidx = SNDRV_MINOR_OSS_CARD(minor);
  88. int track2 = -1;
  89. int register1 = -1, register2 = -1;
  90. struct device *carddev = NULL;
  91. if (minor < 0)
  92. return minor;
  93. preg = (struct snd_minor *)kmalloc(sizeof(struct snd_minor), GFP_KERNEL);
  94. if (preg == NULL)
  95. return -ENOMEM;
  96. *preg = *reg;
  97. preg->number = minor;
  98. preg->device = dev;
  99. down(&sound_oss_mutex);
  100. list_add_tail(&preg->list, &snd_oss_minors_hash[cidx]);
  101. minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
  102. switch (minor_unit) {
  103. case SNDRV_MINOR_OSS_PCM:
  104. track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
  105. break;
  106. case SNDRV_MINOR_OSS_MIDI:
  107. track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
  108. break;
  109. case SNDRV_MINOR_OSS_MIDI1:
  110. track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
  111. break;
  112. }
  113. if (card)
  114. carddev = card->dev;
  115. register1 = register_sound_special_device(reg->f_ops, minor, carddev);
  116. if (register1 != minor)
  117. goto __end;
  118. if (track2 >= 0) {
  119. register2 = register_sound_special_device(reg->f_ops, track2, carddev);
  120. if (register2 != track2)
  121. goto __end;
  122. }
  123. up(&sound_oss_mutex);
  124. return 0;
  125. __end:
  126. if (register2 >= 0)
  127. unregister_sound_special(register2);
  128. if (register1 >= 0)
  129. unregister_sound_special(register1);
  130. list_del(&preg->list);
  131. up(&sound_oss_mutex);
  132. kfree(preg);
  133. return -EBUSY;
  134. }
  135. int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
  136. {
  137. int minor = snd_oss_kernel_minor(type, card, dev);
  138. int cidx = SNDRV_MINOR_OSS_CARD(minor);
  139. int track2 = -1;
  140. struct snd_minor *mptr;
  141. if (minor < 0)
  142. return minor;
  143. down(&sound_oss_mutex);
  144. mptr = snd_oss_minor_search(minor);
  145. if (mptr == NULL) {
  146. up(&sound_oss_mutex);
  147. return -ENOENT;
  148. }
  149. unregister_sound_special(minor);
  150. switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
  151. case SNDRV_MINOR_OSS_PCM:
  152. track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
  153. break;
  154. case SNDRV_MINOR_OSS_MIDI:
  155. track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
  156. break;
  157. case SNDRV_MINOR_OSS_MIDI1:
  158. track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
  159. break;
  160. }
  161. if (track2 >= 0)
  162. unregister_sound_special(track2);
  163. list_del(&mptr->list);
  164. up(&sound_oss_mutex);
  165. kfree(mptr);
  166. return 0;
  167. }
  168. /*
  169. * INFO PART
  170. */
  171. #ifdef CONFIG_PROC_FS
  172. static struct snd_info_entry *snd_minor_info_oss_entry = NULL;
  173. static void snd_minor_info_oss_read(struct snd_info_entry *entry,
  174. struct snd_info_buffer *buffer)
  175. {
  176. int card, dev;
  177. struct list_head *list;
  178. struct snd_minor *mptr;
  179. down(&sound_oss_mutex);
  180. for (card = 0; card < SNDRV_CARDS; card++) {
  181. list_for_each(list, &snd_oss_minors_hash[card]) {
  182. mptr = list_entry(list, struct snd_minor, list);
  183. dev = SNDRV_MINOR_OSS_DEVICE(mptr->number);
  184. if (dev != SNDRV_MINOR_OSS_SNDSTAT &&
  185. dev != SNDRV_MINOR_OSS_SEQUENCER &&
  186. dev != SNDRV_MINOR_OSS_MUSIC)
  187. snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, dev, mptr->comment);
  188. else
  189. snd_iprintf(buffer, "%3i: : %s\n", mptr->number, mptr->comment);
  190. }
  191. }
  192. up(&sound_oss_mutex);
  193. }
  194. #endif /* CONFIG_PROC_FS */
  195. int __init snd_minor_info_oss_init(void)
  196. {
  197. #ifdef CONFIG_PROC_FS
  198. struct snd_info_entry *entry;
  199. entry = snd_info_create_module_entry(THIS_MODULE, "devices", snd_oss_root);
  200. if (entry) {
  201. entry->c.text.read_size = PAGE_SIZE;
  202. entry->c.text.read = snd_minor_info_oss_read;
  203. if (snd_info_register(entry) < 0) {
  204. snd_info_free_entry(entry);
  205. entry = NULL;
  206. }
  207. }
  208. snd_minor_info_oss_entry = entry;
  209. #endif
  210. return 0;
  211. }
  212. int __exit snd_minor_info_oss_done(void)
  213. {
  214. #ifdef CONFIG_PROC_FS
  215. if (snd_minor_info_oss_entry)
  216. snd_info_unregister(snd_minor_info_oss_entry);
  217. #endif
  218. return 0;
  219. }
  220. int __init snd_oss_init_module(void)
  221. {
  222. int card;
  223. for (card = 0; card < SNDRV_CARDS; card++)
  224. INIT_LIST_HEAD(&snd_oss_minors_hash[card]);
  225. return 0;
  226. }
  227. #endif /* CONFIG_SND_OSSEMUL */