sound_oss.c 7.1 KB

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