sound.c 11 KB

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