sound.c 11 KB

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