sound.c 14 KB

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