sound_oss.c 7.5 KB

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