sound.c 12 KB

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