sound.c 12 KB

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