sound.c 13 KB

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