sound_oss.c 7.2 KB

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