sound.c 12 KB

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