sound.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/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/devfs_fs_kernel.h>
  34. #define SNDRV_OS_MINORS 256
  35. static int major = CONFIG_SND_MAJOR;
  36. int snd_major;
  37. static int cards_limit = 1;
  38. static int device_mode = S_IFCHR | S_IRUGO | S_IWUGO;
  39. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  40. MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
  41. MODULE_LICENSE("GPL");
  42. module_param(major, int, 0444);
  43. MODULE_PARM_DESC(major, "Major # for sound driver.");
  44. module_param(cards_limit, int, 0444);
  45. MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
  46. #ifdef CONFIG_DEVFS_FS
  47. module_param(device_mode, int, 0444);
  48. MODULE_PARM_DESC(device_mode, "Device file permission mask for devfs.");
  49. #endif
  50. MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
  51. /* this one holds the actual max. card number currently available.
  52. * as default, it's identical with cards_limit option. when more
  53. * modules are loaded manually, this limit number increases, too.
  54. */
  55. int snd_ecards_limit;
  56. static struct list_head snd_minors_hash[SNDRV_CARDS];
  57. static DECLARE_MUTEX(sound_mutex);
  58. extern struct class *sound_class;
  59. #ifdef CONFIG_KMOD
  60. /**
  61. * snd_request_card - try to load the card module
  62. * @card: the card number
  63. *
  64. * Tries to load the module "snd-card-X" for the given card number
  65. * via KMOD. Returns immediately if already loaded.
  66. */
  67. void snd_request_card(int card)
  68. {
  69. int locked;
  70. if (! current->fs->root)
  71. return;
  72. read_lock(&snd_card_rwlock);
  73. locked = snd_cards_lock & (1 << card);
  74. read_unlock(&snd_card_rwlock);
  75. if (locked)
  76. return;
  77. if (card < 0 || card >= cards_limit)
  78. return;
  79. request_module("snd-card-%i", card);
  80. }
  81. static void snd_request_other(int minor)
  82. {
  83. char *str;
  84. if (! current->fs->root)
  85. return;
  86. switch (minor) {
  87. case SNDRV_MINOR_SEQUENCER: str = "snd-seq"; break;
  88. case SNDRV_MINOR_TIMER: str = "snd-timer"; break;
  89. default: return;
  90. }
  91. request_module(str);
  92. }
  93. #endif /* request_module support */
  94. static struct snd_minor *snd_minor_search(int minor)
  95. {
  96. struct list_head *list;
  97. struct snd_minor *mptr;
  98. list_for_each(list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]) {
  99. mptr = list_entry(list, struct snd_minor, list);
  100. if (mptr->number == minor)
  101. return mptr;
  102. }
  103. return NULL;
  104. }
  105. static int snd_open(struct inode *inode, struct file *file)
  106. {
  107. int minor = iminor(inode);
  108. int card = SNDRV_MINOR_CARD(minor);
  109. int dev = SNDRV_MINOR_DEVICE(minor);
  110. struct snd_minor *mptr = NULL;
  111. struct file_operations *old_fops;
  112. int err = 0;
  113. if (dev != SNDRV_MINOR_GLOBAL) {
  114. if (snd_cards[card] == NULL) {
  115. #ifdef CONFIG_KMOD
  116. snd_request_card(card);
  117. if (snd_cards[card] == NULL)
  118. #endif
  119. return -ENODEV;
  120. }
  121. } else {
  122. #ifdef CONFIG_KMOD
  123. if ((mptr = snd_minor_search(minor)) == NULL)
  124. snd_request_other(minor);
  125. #endif
  126. }
  127. if (mptr == NULL && (mptr = snd_minor_search(minor)) == NULL)
  128. return -ENODEV;
  129. old_fops = file->f_op;
  130. file->f_op = fops_get(mptr->f_ops);
  131. if (file->f_op->open)
  132. err = file->f_op->open(inode, file);
  133. if (err) {
  134. fops_put(file->f_op);
  135. file->f_op = fops_get(old_fops);
  136. }
  137. fops_put(old_fops);
  138. return err;
  139. }
  140. static struct file_operations snd_fops =
  141. {
  142. .owner = THIS_MODULE,
  143. .open = snd_open
  144. };
  145. static int snd_kernel_minor(int type, struct snd_card *card, int dev)
  146. {
  147. int minor;
  148. switch (type) {
  149. case SNDRV_DEVICE_TYPE_SEQUENCER:
  150. case SNDRV_DEVICE_TYPE_TIMER:
  151. minor = type;
  152. break;
  153. case SNDRV_DEVICE_TYPE_CONTROL:
  154. snd_assert(card != NULL, return -EINVAL);
  155. minor = SNDRV_MINOR(card->number, type);
  156. break;
  157. case SNDRV_DEVICE_TYPE_HWDEP:
  158. case SNDRV_DEVICE_TYPE_RAWMIDI:
  159. case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
  160. case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
  161. snd_assert(card != NULL, return -EINVAL);
  162. minor = SNDRV_MINOR(card->number, type + dev);
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
  168. return minor;
  169. }
  170. /**
  171. * snd_register_device - Register the ALSA device file for the card
  172. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  173. * @card: the card instance
  174. * @dev: the device index
  175. * @f_ops: the file operations
  176. * @name: the device file name
  177. *
  178. * Registers an ALSA device file for the given card.
  179. * The operators have to be set in reg parameter.
  180. *
  181. * Retrurns zero if successful, or a negative error code on failure.
  182. */
  183. int snd_register_device(int type, struct snd_card *card, int dev,
  184. struct file_operations *f_ops, const char *name)
  185. {
  186. int minor = snd_kernel_minor(type, card, dev);
  187. struct snd_minor *preg;
  188. struct device *device = NULL;
  189. if (minor < 0)
  190. return minor;
  191. snd_assert(name, return -EINVAL);
  192. preg = kzalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
  193. if (preg == NULL)
  194. return -ENOMEM;
  195. preg->number = minor;
  196. preg->type = type;
  197. preg->device = dev;
  198. preg->f_ops = f_ops;
  199. strcpy(preg->name, name);
  200. down(&sound_mutex);
  201. if (snd_minor_search(minor)) {
  202. up(&sound_mutex);
  203. kfree(preg);
  204. return -EBUSY;
  205. }
  206. list_add_tail(&preg->list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]);
  207. if (strncmp(name, "controlC", 8) || card->number >= cards_limit)
  208. devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
  209. if (card)
  210. device = card->dev;
  211. class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
  212. up(&sound_mutex);
  213. return 0;
  214. }
  215. /**
  216. * snd_unregister_device - unregister the device on the given card
  217. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  218. * @card: the card instance
  219. * @dev: the device index
  220. *
  221. * Unregisters the device file already registered via
  222. * snd_register_device().
  223. *
  224. * Returns zero if sucecessful, or a negative error code on failure
  225. */
  226. int snd_unregister_device(int type, struct snd_card *card, int dev)
  227. {
  228. int minor = snd_kernel_minor(type, card, dev);
  229. struct snd_minor *mptr;
  230. if (minor < 0)
  231. return minor;
  232. down(&sound_mutex);
  233. if ((mptr = snd_minor_search(minor)) == NULL) {
  234. up(&sound_mutex);
  235. return -EINVAL;
  236. }
  237. if (strncmp(mptr->name, "controlC", 8) || card->number >= cards_limit) /* created in sound.c */
  238. devfs_remove("snd/%s", mptr->name);
  239. class_device_destroy(sound_class, MKDEV(major, minor));
  240. list_del(&mptr->list);
  241. up(&sound_mutex);
  242. kfree(mptr);
  243. return 0;
  244. }
  245. /*
  246. * INFO PART
  247. */
  248. static struct snd_info_entry *snd_minor_info_entry = NULL;
  249. static const char *snd_device_type_name(int type)
  250. {
  251. switch (type) {
  252. case SNDRV_DEVICE_TYPE_CONTROL:
  253. return "control";
  254. case SNDRV_DEVICE_TYPE_HWDEP:
  255. return "hardware dependent";
  256. case SNDRV_DEVICE_TYPE_RAWMIDI:
  257. return "raw midi";
  258. case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
  259. return "digital audio playback";
  260. case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
  261. return "digital audio capture";
  262. case SNDRV_DEVICE_TYPE_SEQUENCER:
  263. return "sequencer";
  264. case SNDRV_DEVICE_TYPE_TIMER:
  265. return "timer";
  266. default:
  267. return "?";
  268. }
  269. }
  270. static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  271. {
  272. int card, device;
  273. struct list_head *list;
  274. struct snd_minor *mptr;
  275. down(&sound_mutex);
  276. for (card = 0; card < SNDRV_CARDS; card++) {
  277. list_for_each(list, &snd_minors_hash[card]) {
  278. mptr = list_entry(list, struct snd_minor, list);
  279. if (SNDRV_MINOR_DEVICE(mptr->number) != SNDRV_MINOR_GLOBAL) {
  280. if ((device = mptr->device) >= 0)
  281. snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, device, snd_device_type_name(mptr->type));
  282. else
  283. snd_iprintf(buffer, "%3i: [%i] : %s\n", mptr->number, card, snd_device_type_name(mptr->type));
  284. } else {
  285. snd_iprintf(buffer, "%3i: : %s\n", mptr->number, snd_device_type_name(mptr->type));
  286. }
  287. }
  288. }
  289. up(&sound_mutex);
  290. }
  291. int __init snd_minor_info_init(void)
  292. {
  293. struct snd_info_entry *entry;
  294. entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
  295. if (entry) {
  296. entry->c.text.read_size = PAGE_SIZE;
  297. entry->c.text.read = snd_minor_info_read;
  298. if (snd_info_register(entry) < 0) {
  299. snd_info_free_entry(entry);
  300. entry = NULL;
  301. }
  302. }
  303. snd_minor_info_entry = entry;
  304. return 0;
  305. }
  306. int __exit snd_minor_info_done(void)
  307. {
  308. if (snd_minor_info_entry)
  309. snd_info_unregister(snd_minor_info_entry);
  310. return 0;
  311. }
  312. /*
  313. * INIT PART
  314. */
  315. static int __init alsa_sound_init(void)
  316. {
  317. short controlnum;
  318. int err;
  319. int card;
  320. snd_major = major;
  321. snd_ecards_limit = cards_limit;
  322. for (card = 0; card < SNDRV_CARDS; card++)
  323. INIT_LIST_HEAD(&snd_minors_hash[card]);
  324. if ((err = snd_oss_init_module()) < 0)
  325. return err;
  326. devfs_mk_dir("snd");
  327. if (register_chrdev(major, "alsa", &snd_fops)) {
  328. snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
  329. devfs_remove("snd");
  330. return -EIO;
  331. }
  332. if (snd_info_init() < 0) {
  333. unregister_chrdev(major, "alsa");
  334. devfs_remove("snd");
  335. return -ENOMEM;
  336. }
  337. snd_info_minor_register();
  338. for (controlnum = 0; controlnum < cards_limit; controlnum++)
  339. devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
  340. #ifndef MODULE
  341. printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
  342. #endif
  343. return 0;
  344. }
  345. static void __exit alsa_sound_exit(void)
  346. {
  347. short controlnum;
  348. for (controlnum = 0; controlnum < cards_limit; controlnum++)
  349. devfs_remove("snd/controlC%d", controlnum);
  350. snd_info_minor_unregister();
  351. snd_info_done();
  352. if (unregister_chrdev(major, "alsa") != 0)
  353. snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
  354. devfs_remove("snd");
  355. }
  356. module_init(alsa_sound_init)
  357. module_exit(alsa_sound_exit)
  358. /* sound.c */
  359. EXPORT_SYMBOL(snd_major);
  360. EXPORT_SYMBOL(snd_ecards_limit);
  361. #if defined(CONFIG_KMOD)
  362. EXPORT_SYMBOL(snd_request_card);
  363. #endif
  364. EXPORT_SYMBOL(snd_register_device);
  365. EXPORT_SYMBOL(snd_unregister_device);
  366. #if defined(CONFIG_SND_OSSEMUL)
  367. EXPORT_SYMBOL(snd_register_oss_device);
  368. EXPORT_SYMBOL(snd_unregister_oss_device);
  369. #endif
  370. /* memory.c */
  371. EXPORT_SYMBOL(copy_to_user_fromio);
  372. EXPORT_SYMBOL(copy_from_user_toio);
  373. /* init.c */
  374. EXPORT_SYMBOL(snd_cards);
  375. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  376. EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
  377. #endif
  378. EXPORT_SYMBOL(snd_card_new);
  379. EXPORT_SYMBOL(snd_card_disconnect);
  380. EXPORT_SYMBOL(snd_card_free);
  381. EXPORT_SYMBOL(snd_card_free_in_thread);
  382. EXPORT_SYMBOL(snd_card_register);
  383. EXPORT_SYMBOL(snd_component_add);
  384. EXPORT_SYMBOL(snd_card_file_add);
  385. EXPORT_SYMBOL(snd_card_file_remove);
  386. #ifdef CONFIG_PM
  387. EXPORT_SYMBOL(snd_power_wait);
  388. #endif
  389. /* device.c */
  390. EXPORT_SYMBOL(snd_device_new);
  391. EXPORT_SYMBOL(snd_device_register);
  392. EXPORT_SYMBOL(snd_device_free);
  393. /* isadma.c */
  394. #ifdef CONFIG_ISA_DMA_API
  395. EXPORT_SYMBOL(snd_dma_program);
  396. EXPORT_SYMBOL(snd_dma_disable);
  397. EXPORT_SYMBOL(snd_dma_pointer);
  398. #endif
  399. /* info.c */
  400. #ifdef CONFIG_PROC_FS
  401. EXPORT_SYMBOL(snd_seq_root);
  402. EXPORT_SYMBOL(snd_iprintf);
  403. EXPORT_SYMBOL(snd_info_get_line);
  404. EXPORT_SYMBOL(snd_info_get_str);
  405. EXPORT_SYMBOL(snd_info_create_module_entry);
  406. EXPORT_SYMBOL(snd_info_create_card_entry);
  407. EXPORT_SYMBOL(snd_info_free_entry);
  408. EXPORT_SYMBOL(snd_info_register);
  409. EXPORT_SYMBOL(snd_info_unregister);
  410. EXPORT_SYMBOL(snd_card_proc_new);
  411. #endif
  412. /* info_oss.c */
  413. #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
  414. EXPORT_SYMBOL(snd_oss_info_register);
  415. #endif
  416. /* control.c */
  417. EXPORT_SYMBOL(snd_ctl_new);
  418. EXPORT_SYMBOL(snd_ctl_new1);
  419. EXPORT_SYMBOL(snd_ctl_free_one);
  420. EXPORT_SYMBOL(snd_ctl_add);
  421. EXPORT_SYMBOL(snd_ctl_remove);
  422. EXPORT_SYMBOL(snd_ctl_remove_id);
  423. EXPORT_SYMBOL(snd_ctl_rename_id);
  424. EXPORT_SYMBOL(snd_ctl_find_numid);
  425. EXPORT_SYMBOL(snd_ctl_find_id);
  426. EXPORT_SYMBOL(snd_ctl_notify);
  427. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  428. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  429. #ifdef CONFIG_COMPAT
  430. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  431. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  432. #endif
  433. EXPORT_SYMBOL(snd_ctl_elem_read);
  434. EXPORT_SYMBOL(snd_ctl_elem_write);
  435. /* misc.c */
  436. EXPORT_SYMBOL(release_and_free_resource);
  437. #ifdef CONFIG_SND_VERBOSE_PRINTK
  438. EXPORT_SYMBOL(snd_verbose_printk);
  439. #endif
  440. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
  441. EXPORT_SYMBOL(snd_verbose_printd);
  442. #endif