sound.c 12 KB

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