sound.c 12 KB

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