sound.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/device.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #include <sound/minors.h>
  29. #include <sound/info.h>
  30. #include <sound/version.h>
  31. #include <sound/control.h>
  32. #include <sound/initval.h>
  33. #include <linux/kmod.h>
  34. #include <linux/devfs_fs_kernel.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 snd_minor *snd_minors[SNDRV_OS_MINORS];
  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. /**
  96. * snd_lookup_minor_data - get user data of a registered device
  97. * @minor: the minor number
  98. * @type: device type (SNDRV_DEVICE_TYPE_XXX)
  99. *
  100. * Checks that a minor device with the specified type is registered, and returns
  101. * its user data pointer.
  102. */
  103. void *snd_lookup_minor_data(unsigned int minor, int type)
  104. {
  105. struct snd_minor *mreg;
  106. void *private_data;
  107. if (minor > ARRAY_SIZE(snd_minors))
  108. return NULL;
  109. down(&sound_mutex);
  110. mreg = snd_minors[minor];
  111. if (mreg && mreg->type == type)
  112. private_data = mreg->private_data;
  113. else
  114. private_data = NULL;
  115. up(&sound_mutex);
  116. return private_data;
  117. }
  118. static int snd_open(struct inode *inode, struct file *file)
  119. {
  120. unsigned int minor = iminor(inode);
  121. struct snd_minor *mptr = NULL;
  122. struct file_operations *old_fops;
  123. int err = 0;
  124. if (minor > ARRAY_SIZE(snd_minors))
  125. return -ENODEV;
  126. mptr = snd_minors[minor];
  127. if (mptr == NULL) {
  128. #ifdef CONFIG_KMOD
  129. int dev = SNDRV_MINOR_DEVICE(minor);
  130. if (dev == SNDRV_MINOR_CONTROL) {
  131. /* /dev/aloadC? */
  132. int card = SNDRV_MINOR_CARD(minor);
  133. if (snd_cards[card] == NULL)
  134. snd_request_card(card);
  135. } else if (dev == SNDRV_MINOR_GLOBAL) {
  136. /* /dev/aloadSEQ */
  137. snd_request_other(minor);
  138. }
  139. #ifndef CONFIG_SND_DYNAMIC_MINORS
  140. /* /dev/snd/{controlC?,seq} */
  141. mptr = snd_minors[minor];
  142. if (mptr == NULL)
  143. #endif
  144. #endif
  145. return -ENODEV;
  146. }
  147. old_fops = file->f_op;
  148. file->f_op = fops_get(mptr->f_ops);
  149. if (file->f_op->open)
  150. err = file->f_op->open(inode, file);
  151. if (err) {
  152. fops_put(file->f_op);
  153. file->f_op = fops_get(old_fops);
  154. }
  155. fops_put(old_fops);
  156. return err;
  157. }
  158. static struct file_operations snd_fops =
  159. {
  160. .owner = THIS_MODULE,
  161. .open = snd_open
  162. };
  163. #ifdef CONFIG_SND_DYNAMIC_MINORS
  164. static int snd_find_free_minor(void)
  165. {
  166. int minor;
  167. for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
  168. /* skip minors still used statically for autoloading devices */
  169. if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL ||
  170. minor == SNDRV_MINOR_SEQUENCER)
  171. continue;
  172. if (!snd_minors[minor])
  173. return minor;
  174. }
  175. return -EBUSY;
  176. }
  177. #else
  178. static int snd_kernel_minor(int type, struct snd_card *card, int dev)
  179. {
  180. int minor;
  181. switch (type) {
  182. case SNDRV_DEVICE_TYPE_SEQUENCER:
  183. case SNDRV_DEVICE_TYPE_TIMER:
  184. minor = type;
  185. break;
  186. case SNDRV_DEVICE_TYPE_CONTROL:
  187. snd_assert(card != NULL, return -EINVAL);
  188. minor = SNDRV_MINOR(card->number, type);
  189. break;
  190. case SNDRV_DEVICE_TYPE_HWDEP:
  191. case SNDRV_DEVICE_TYPE_RAWMIDI:
  192. case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
  193. case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
  194. snd_assert(card != NULL, return -EINVAL);
  195. minor = SNDRV_MINOR(card->number, type + dev);
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
  201. return minor;
  202. }
  203. #endif
  204. /**
  205. * snd_register_device - Register the ALSA device file for the card
  206. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  207. * @card: the card instance
  208. * @dev: the device index
  209. * @f_ops: the file operations
  210. * @private_data: user pointer for f_ops->open()
  211. * @name: the device file name
  212. *
  213. * Registers an ALSA device file for the given card.
  214. * The operators have to be set in reg parameter.
  215. *
  216. * Retrurns zero if successful, or a negative error code on failure.
  217. */
  218. int snd_register_device(int type, struct snd_card *card, int dev,
  219. struct file_operations *f_ops, void *private_data,
  220. const char *name)
  221. {
  222. int minor;
  223. struct snd_minor *preg;
  224. struct device *device = NULL;
  225. snd_assert(name, return -EINVAL);
  226. preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
  227. if (preg == NULL)
  228. return -ENOMEM;
  229. preg->type = type;
  230. preg->card = card ? card->number : -1;
  231. preg->device = dev;
  232. preg->f_ops = f_ops;
  233. preg->private_data = private_data;
  234. strcpy(preg->name, name);
  235. down(&sound_mutex);
  236. #ifdef CONFIG_SND_DYNAMIC_MINORS
  237. minor = snd_find_free_minor();
  238. #else
  239. minor = snd_kernel_minor(type, card, dev);
  240. if (minor >= 0 && snd_minors[minor])
  241. minor = -EBUSY;
  242. #endif
  243. if (minor < 0) {
  244. up(&sound_mutex);
  245. kfree(preg);
  246. return minor;
  247. }
  248. snd_minors[minor] = preg;
  249. if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
  250. devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
  251. if (card)
  252. device = card->dev;
  253. class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
  254. up(&sound_mutex);
  255. return 0;
  256. }
  257. /**
  258. * snd_unregister_device - unregister the device on the given card
  259. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  260. * @card: the card instance
  261. * @dev: the device index
  262. *
  263. * Unregisters the device file already registered via
  264. * snd_register_device().
  265. *
  266. * Returns zero if sucecessful, or a negative error code on failure
  267. */
  268. int snd_unregister_device(int type, struct snd_card *card, int dev)
  269. {
  270. int cardnum, minor;
  271. struct snd_minor *mptr;
  272. cardnum = card ? card->number : -1;
  273. down(&sound_mutex);
  274. for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
  275. if ((mptr = snd_minors[minor]) != NULL &&
  276. mptr->type == type &&
  277. mptr->card == cardnum &&
  278. mptr->device == dev)
  279. break;
  280. if (minor == ARRAY_SIZE(snd_minors)) {
  281. up(&sound_mutex);
  282. return -EINVAL;
  283. }
  284. if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
  285. mptr->card >= cards_limit) /* created in sound.c */
  286. devfs_remove("snd/%s", mptr->name);
  287. class_device_destroy(sound_class, MKDEV(major, minor));
  288. snd_minors[minor] = NULL;
  289. up(&sound_mutex);
  290. kfree(mptr);
  291. return 0;
  292. }
  293. #ifdef CONFIG_PROC_FS
  294. /*
  295. * INFO PART
  296. */
  297. static struct snd_info_entry *snd_minor_info_entry = NULL;
  298. static const char *snd_device_type_name(int type)
  299. {
  300. switch (type) {
  301. case SNDRV_DEVICE_TYPE_CONTROL:
  302. return "control";
  303. case SNDRV_DEVICE_TYPE_HWDEP:
  304. return "hardware dependent";
  305. case SNDRV_DEVICE_TYPE_RAWMIDI:
  306. return "raw midi";
  307. case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
  308. return "digital audio playback";
  309. case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
  310. return "digital audio capture";
  311. case SNDRV_DEVICE_TYPE_SEQUENCER:
  312. return "sequencer";
  313. case SNDRV_DEVICE_TYPE_TIMER:
  314. return "timer";
  315. default:
  316. return "?";
  317. }
  318. }
  319. static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  320. {
  321. int minor;
  322. struct snd_minor *mptr;
  323. down(&sound_mutex);
  324. for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
  325. if (!(mptr = snd_minors[minor]))
  326. continue;
  327. if (mptr->card >= 0) {
  328. if (mptr->device >= 0)
  329. snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n",
  330. minor, mptr->card, mptr->device,
  331. snd_device_type_name(mptr->type));
  332. else
  333. snd_iprintf(buffer, "%3i: [%2i] : %s\n",
  334. minor, mptr->card,
  335. snd_device_type_name(mptr->type));
  336. } else
  337. snd_iprintf(buffer, "%3i: : %s\n", minor,
  338. snd_device_type_name(mptr->type));
  339. }
  340. up(&sound_mutex);
  341. }
  342. int __init snd_minor_info_init(void)
  343. {
  344. struct snd_info_entry *entry;
  345. entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
  346. if (entry) {
  347. entry->c.text.read_size = PAGE_SIZE;
  348. entry->c.text.read = snd_minor_info_read;
  349. if (snd_info_register(entry) < 0) {
  350. snd_info_free_entry(entry);
  351. entry = NULL;
  352. }
  353. }
  354. snd_minor_info_entry = entry;
  355. return 0;
  356. }
  357. int __exit snd_minor_info_done(void)
  358. {
  359. if (snd_minor_info_entry)
  360. snd_info_unregister(snd_minor_info_entry);
  361. return 0;
  362. }
  363. #endif /* CONFIG_PROC_FS */
  364. /*
  365. * INIT PART
  366. */
  367. static int __init alsa_sound_init(void)
  368. {
  369. short controlnum;
  370. snd_major = major;
  371. snd_ecards_limit = cards_limit;
  372. devfs_mk_dir("snd");
  373. if (register_chrdev(major, "alsa", &snd_fops)) {
  374. snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
  375. devfs_remove("snd");
  376. return -EIO;
  377. }
  378. if (snd_info_init() < 0) {
  379. unregister_chrdev(major, "alsa");
  380. devfs_remove("snd");
  381. return -ENOMEM;
  382. }
  383. snd_info_minor_register();
  384. for (controlnum = 0; controlnum < cards_limit; controlnum++)
  385. devfs_mk_cdev(MKDEV(major, controlnum<<5), S_IFCHR | device_mode, "snd/controlC%d", controlnum);
  386. #ifndef MODULE
  387. printk(KERN_INFO "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n");
  388. #endif
  389. return 0;
  390. }
  391. static void __exit alsa_sound_exit(void)
  392. {
  393. short controlnum;
  394. for (controlnum = 0; controlnum < cards_limit; controlnum++)
  395. devfs_remove("snd/controlC%d", controlnum);
  396. snd_info_minor_unregister();
  397. snd_info_done();
  398. if (unregister_chrdev(major, "alsa") != 0)
  399. snd_printk(KERN_ERR "unable to unregister major device number %d\n", major);
  400. devfs_remove("snd");
  401. }
  402. module_init(alsa_sound_init)
  403. module_exit(alsa_sound_exit)
  404. /* sound.c */
  405. EXPORT_SYMBOL(snd_major);
  406. EXPORT_SYMBOL(snd_ecards_limit);
  407. #if defined(CONFIG_KMOD)
  408. EXPORT_SYMBOL(snd_request_card);
  409. #endif
  410. EXPORT_SYMBOL(snd_register_device);
  411. EXPORT_SYMBOL(snd_unregister_device);
  412. EXPORT_SYMBOL(snd_lookup_minor_data);
  413. #if defined(CONFIG_SND_OSSEMUL)
  414. EXPORT_SYMBOL(snd_register_oss_device);
  415. EXPORT_SYMBOL(snd_unregister_oss_device);
  416. EXPORT_SYMBOL(snd_lookup_oss_minor_data);
  417. #endif
  418. /* memory.c */
  419. EXPORT_SYMBOL(copy_to_user_fromio);
  420. EXPORT_SYMBOL(copy_from_user_toio);
  421. /* init.c */
  422. EXPORT_SYMBOL(snd_cards);
  423. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  424. EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
  425. #endif
  426. EXPORT_SYMBOL(snd_card_new);
  427. EXPORT_SYMBOL(snd_card_disconnect);
  428. EXPORT_SYMBOL(snd_card_free);
  429. EXPORT_SYMBOL(snd_card_free_in_thread);
  430. EXPORT_SYMBOL(snd_card_register);
  431. EXPORT_SYMBOL(snd_component_add);
  432. EXPORT_SYMBOL(snd_card_file_add);
  433. EXPORT_SYMBOL(snd_card_file_remove);
  434. #ifdef CONFIG_PM
  435. EXPORT_SYMBOL(snd_power_wait);
  436. #endif
  437. /* device.c */
  438. EXPORT_SYMBOL(snd_device_new);
  439. EXPORT_SYMBOL(snd_device_register);
  440. EXPORT_SYMBOL(snd_device_free);
  441. /* isadma.c */
  442. #ifdef CONFIG_ISA_DMA_API
  443. EXPORT_SYMBOL(snd_dma_program);
  444. EXPORT_SYMBOL(snd_dma_disable);
  445. EXPORT_SYMBOL(snd_dma_pointer);
  446. #endif
  447. /* info.c */
  448. #ifdef CONFIG_PROC_FS
  449. EXPORT_SYMBOL(snd_seq_root);
  450. EXPORT_SYMBOL(snd_iprintf);
  451. EXPORT_SYMBOL(snd_info_get_line);
  452. EXPORT_SYMBOL(snd_info_get_str);
  453. EXPORT_SYMBOL(snd_info_create_module_entry);
  454. EXPORT_SYMBOL(snd_info_create_card_entry);
  455. EXPORT_SYMBOL(snd_info_free_entry);
  456. EXPORT_SYMBOL(snd_info_register);
  457. EXPORT_SYMBOL(snd_info_unregister);
  458. EXPORT_SYMBOL(snd_card_proc_new);
  459. #endif
  460. /* info_oss.c */
  461. #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
  462. EXPORT_SYMBOL(snd_oss_info_register);
  463. #endif
  464. /* control.c */
  465. EXPORT_SYMBOL(snd_ctl_new);
  466. EXPORT_SYMBOL(snd_ctl_new1);
  467. EXPORT_SYMBOL(snd_ctl_free_one);
  468. EXPORT_SYMBOL(snd_ctl_add);
  469. EXPORT_SYMBOL(snd_ctl_remove);
  470. EXPORT_SYMBOL(snd_ctl_remove_id);
  471. EXPORT_SYMBOL(snd_ctl_rename_id);
  472. EXPORT_SYMBOL(snd_ctl_find_numid);
  473. EXPORT_SYMBOL(snd_ctl_find_id);
  474. EXPORT_SYMBOL(snd_ctl_notify);
  475. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  476. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  477. #ifdef CONFIG_COMPAT
  478. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  479. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  480. #endif
  481. EXPORT_SYMBOL(snd_ctl_elem_read);
  482. EXPORT_SYMBOL(snd_ctl_elem_write);
  483. /* misc.c */
  484. EXPORT_SYMBOL(release_and_free_resource);
  485. #ifdef CONFIG_SND_VERBOSE_PRINTK
  486. EXPORT_SYMBOL(snd_verbose_printk);
  487. #endif
  488. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
  489. EXPORT_SYMBOL(snd_verbose_printd);
  490. #endif