sound.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/time.h>
  25. #include <linux/device.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #include <sound/minors.h>
  29. #include <sound/info.h>
  30. #include <sound/version.h>
  31. #include <sound/control.h>
  32. #include <sound/initval.h>
  33. #include <linux/kmod.h>
  34. #include <linux/devfs_fs_kernel.h>
  35. #include <linux/mutex.h>
  36. #define SNDRV_OS_MINORS 256
  37. static int major = CONFIG_SND_MAJOR;
  38. int snd_major;
  39. EXPORT_SYMBOL(snd_major);
  40. static int cards_limit = 1;
  41. static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO;
  42. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  43. MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
  44. MODULE_LICENSE("GPL");
  45. module_param(major, int, 0444);
  46. MODULE_PARM_DESC(major, "Major # for sound driver.");
  47. module_param(cards_limit, int, 0444);
  48. MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
  49. #ifdef CONFIG_DEVFS_FS
  50. module_param(device_mode, int, 0444);
  51. MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs.");
  52. #endif
  53. MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
  54. /* this one holds the actual max. card number currently available.
  55. * as default, it's identical with cards_limit option. when more
  56. * modules are loaded manually, this limit number increases, too.
  57. */
  58. int snd_ecards_limit;
  59. EXPORT_SYMBOL(snd_ecards_limit);
  60. static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
  61. static DEFINE_MUTEX(sound_mutex);
  62. extern struct class *sound_class;
  63. #ifdef CONFIG_KMOD
  64. /**
  65. * snd_request_card - try to load the card module
  66. * @card: the card number
  67. *
  68. * Tries to load the module "snd-card-X" for the given card number
  69. * via KMOD. Returns immediately if already loaded.
  70. */
  71. void snd_request_card(int card)
  72. {
  73. if (! current->fs->root)
  74. return;
  75. if (snd_card_locked(card))
  76. return;
  77. if (card < 0 || card >= cards_limit)
  78. return;
  79. request_module("snd-card-%i", card);
  80. }
  81. EXPORT_SYMBOL(snd_request_card);
  82. static void snd_request_other(int minor)
  83. {
  84. char *str;
  85. if (! current->fs->root)
  86. return;
  87. switch (minor) {
  88. case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break;
  89. case SNDRV_MINOR_TIMER: str = "snd-timer"; break;
  90. default: return;
  91. }
  92. request_module(str);
  93. }
  94. #endif /* request_module support */
  95. /**
  96. * snd_lookup_minor_data - get user data of a registered device
  97. * @minor: the minor number
  98. * @type: device type (SNDRV_DEVICE_TYPE_XXX)
  99. *
  100. * Checks that a minor device with the specified type is registered, and returns
  101. * its user data pointer.
  102. */
  103. void *snd_lookup_minor_data(unsigned int minor, int type)
  104. {
  105. struct snd_minor *mreg;
  106. void *private_data;
  107. if (minor >= ARRAY_SIZE(snd_minors))
  108. return NULL;
  109. mutex_lock(&sound_mutex);
  110. mreg = snd_minors[minor];
  111. if (mreg && mreg->type == type)
  112. private_data = mreg->private_data;
  113. else
  114. private_data = NULL;
  115. mutex_unlock(&sound_mutex);
  116. return private_data;
  117. }
  118. EXPORT_SYMBOL(snd_lookup_minor_data);
  119. static int snd_open(struct inode *inode, struct file *file)
  120. {
  121. unsigned int minor = iminor(inode);
  122. struct snd_minor *mptr = NULL;
  123. const struct file_operations *old_fops;
  124. int err = 0;
  125. if (minor >= ARRAY_SIZE(snd_minors))
  126. return -ENODEV;
  127. mptr = snd_minors[minor];
  128. if (mptr == NULL) {
  129. #ifdef CONFIG_KMOD
  130. int dev = SNDRV_MINOR_DEVICE(minor);
  131. if (dev == SNDRV_MINOR_CONTROL) {
  132. /* /dev/aloadC? */
  133. int card = SNDRV_MINOR_CARD(minor);
  134. if (snd_cards[card] == NULL)
  135. snd_request_card(card);
  136. } else if (dev == SNDRV_MINOR_GLOBAL) {
  137. /* /dev/aloadSEQ */
  138. snd_request_other(minor);
  139. }
  140. #ifndef CONFIG_SND_DYNAMIC_MINORS
  141. /* /dev/snd/{controlC?,seq} */
  142. mptr = snd_minors[minor];
  143. if (mptr == NULL)
  144. #endif
  145. #endif
  146. return -ENODEV;
  147. }
  148. old_fops = file->f_op;
  149. file->f_op = fops_get(mptr->f_ops);
  150. if (file->f_op->open)
  151. err = file->f_op->open(inode, file);
  152. if (err) {
  153. fops_put(file->f_op);
  154. file->f_op = fops_get(old_fops);
  155. }
  156. fops_put(old_fops);
  157. return err;
  158. }
  159. static struct file_operations snd_fops =
  160. {
  161. .owner = THIS_MODULE,
  162. .open = snd_open
  163. };
  164. #ifdef CONFIG_SND_DYNAMIC_MINORS
  165. static int snd_find_free_minor(void)
  166. {
  167. int minor;
  168. for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
  169. /* skip minors still used statically for autoloading devices */
  170. if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL ||
  171. minor == SNDRV_MINOR_SEQUENCER)
  172. continue;
  173. if (!snd_minors[minor])
  174. return minor;
  175. }
  176. return -EBUSY;
  177. }
  178. #else
  179. static int snd_kernel_minor(int type, struct snd_card *card, int dev)
  180. {
  181. int minor;
  182. switch (type) {
  183. case SNDRV_DEVICE_TYPE_SEQUENCER:
  184. case SNDRV_DEVICE_TYPE_TIMER:
  185. minor = type;
  186. break;
  187. case SNDRV_DEVICE_TYPE_CONTROL:
  188. snd_assert(card != NULL, return -EINVAL);
  189. minor = SNDRV_MINOR(card->number, type);
  190. break;
  191. case SNDRV_DEVICE_TYPE_HWDEP:
  192. case SNDRV_DEVICE_TYPE_RAWMIDI:
  193. case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
  194. case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
  195. snd_assert(card != NULL, return -EINVAL);
  196. minor = SNDRV_MINOR(card->number, type + dev);
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
  202. return minor;
  203. }
  204. #endif
  205. /**
  206. * snd_register_device - Register the ALSA device file for the card
  207. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  208. * @card: the card instance
  209. * @dev: the device index
  210. * @f_ops: the file operations
  211. * @private_data: user pointer for f_ops->open()
  212. * @name: the device file name
  213. *
  214. * Registers an ALSA device file for the given card.
  215. * The operators have to be set in reg parameter.
  216. *
  217. * Retrurns zero if successful, or a negative error code on failure.
  218. */
  219. int snd_register_device(int type, struct snd_card *card, int dev,
  220. const struct file_operations *f_ops, void *private_data,
  221. const char *name)
  222. {
  223. int minor;
  224. struct snd_minor *preg;
  225. struct device *device = NULL;
  226. snd_assert(name, return -EINVAL);
  227. preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
  228. if (preg == NULL)
  229. return -ENOMEM;
  230. preg->type = type;
  231. preg->card = card ? card->number : -1;
  232. preg->device = dev;
  233. preg->f_ops = f_ops;
  234. preg->private_data = private_data;
  235. strcpy(preg->name, name);
  236. mutex_lock(&sound_mutex);
  237. #ifdef CONFIG_SND_DYNAMIC_MINORS
  238. minor = snd_find_free_minor();
  239. #else
  240. minor = snd_kernel_minor(type, card, dev);
  241. if (minor >= 0 && snd_minors[minor])
  242. minor = -EBUSY;
  243. #endif
  244. if (minor < 0) {
  245. mutex_unlock(&sound_mutex);
  246. kfree(preg);
  247. return minor;
  248. }
  249. snd_minors[minor] = preg;
  250. if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
  251. devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
  252. if (card)
  253. device = card->dev;
  254. class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
  255. mutex_unlock(&sound_mutex);
  256. return 0;
  257. }
  258. EXPORT_SYMBOL(snd_register_device);
  259. /**
  260. * snd_unregister_device - unregister the device on the given card
  261. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  262. * @card: the card instance
  263. * @dev: the device index
  264. *
  265. * Unregisters the device file already registered via
  266. * snd_register_device().
  267. *
  268. * Returns zero if sucecessful, or a negative error code on failure
  269. */
  270. int snd_unregister_device(int type, struct snd_card *card, int dev)
  271. {
  272. int cardnum, minor;
  273. struct snd_minor *mptr;
  274. cardnum = card ? card->number : -1;
  275. mutex_lock(&sound_mutex);
  276. for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
  277. if ((mptr = snd_minors[minor]) != NULL &&
  278. mptr->type == type &&
  279. mptr->card == cardnum &&
  280. mptr->device == dev)
  281. break;
  282. if (minor == ARRAY_SIZE(snd_minors)) {
  283. mutex_unlock(&sound_mutex);
  284. return -EINVAL;
  285. }
  286. if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
  287. mptr->card >= cards_limit) /* created in sound.c */
  288. devfs_remove("snd/%s", mptr->name);
  289. class_device_destroy(sound_class, MKDEV(major, minor));
  290. snd_minors[minor] = NULL;
  291. mutex_unlock(&sound_mutex);
  292. kfree(mptr);
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(snd_unregister_device);
  296. #ifdef CONFIG_PROC_FS
  297. /*
  298. * INFO PART
  299. */
  300. static struct snd_info_entry *snd_minor_info_entry;
  301. static const char *snd_device_type_name(int type)
  302. {
  303. switch (type) {
  304. case SNDRV_DEVICE_TYPE_CONTROL:
  305. return "control";
  306. case SNDRV_DEVICE_TYPE_HWDEP:
  307. return "hardware dependent";
  308. case SNDRV_DEVICE_TYPE_RAWMIDI:
  309. return "raw midi";
  310. case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
  311. return "digital audio playback";
  312. case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
  313. return "digital audio capture";
  314. case SNDRV_DEVICE_TYPE_SEQUENCER:
  315. return "sequencer";
  316. case SNDRV_DEVICE_TYPE_TIMER:
  317. return "timer";
  318. default:
  319. return "?";
  320. }
  321. }
  322. static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  323. {
  324. int minor;
  325. struct snd_minor *mptr;
  326. mutex_lock(&sound_mutex);
  327. for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
  328. if (!(mptr = snd_minors[minor]))
  329. continue;
  330. if (mptr->card >= 0) {
  331. if (mptr->device >= 0)
  332. snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n",
  333. minor, mptr->card, mptr->device,
  334. snd_device_type_name(mptr->type));
  335. else
  336. snd_iprintf(buffer, "%3i: [%2i] : %s\n",
  337. minor, mptr->card,
  338. snd_device_type_name(mptr->type));
  339. } else
  340. snd_iprintf(buffer, "%3i: : %s\n", minor,
  341. snd_device_type_name(mptr->type));
  342. }
  343. mutex_unlock(&sound_mutex);
  344. }
  345. int __init snd_minor_info_init(void)
  346. {
  347. struct snd_info_entry *entry;
  348. entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
  349. if (entry) {
  350. entry->c.text.read = snd_minor_info_read;
  351. if (snd_info_register(entry) < 0) {
  352. snd_info_free_entry(entry);
  353. entry = NULL;
  354. }
  355. }
  356. snd_minor_info_entry = entry;
  357. return 0;
  358. }
  359. int __exit snd_minor_info_done(void)
  360. {
  361. if (snd_minor_info_entry)
  362. snd_info_unregister(snd_minor_info_entry);
  363. return 0;
  364. }
  365. #endif /* CONFIG_PROC_FS */
  366. /*
  367. * INIT PART
  368. */
  369. static int __init alsa_sound_init(void)
  370. {
  371. short controlnum;
  372. snd_major = major;
  373. snd_ecards_limit = cards_limit;
  374. devfs_mk_dir("snd");
  375. if (register_chrdev(major, "alsa", &snd_fops)) {
  376. snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
  377. devfs_remove("snd");
  378. return -EIO;
  379. }
  380. if (snd_info_init() < 0) {
  381. unregister_chrdev(major, "alsa");
  382. devfs_remove("snd");
  383. return -ENOMEM;
  384. }
  385. snd_info_minor_register();
  386. for (controlnum = 0; controlnum < cards_limit; controlnum++)
  387. devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
  388. #ifndef MODULE
  389. printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
  390. #endif
  391. return 0;
  392. }
  393. static void __exit alsa_sound_exit(void)
  394. {
  395. short controlnum;
  396. for (controlnum = 0; controlnum < cards_limit; controlnum++)
  397. devfs_remove("snd/controlC%d", controlnum);
  398. snd_info_minor_unregister();
  399. snd_info_done();
  400. if (unregister_chrdev(major, "alsa") != 0)
  401. snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
  402. devfs_remove("snd");
  403. }
  404. module_init(alsa_sound_init)
  405. module_exit(alsa_sound_exit)