sound.c 11 KB

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