sound.c 11 KB

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