sound_oss.c 7.0 KB

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