sound.c 11 KB

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