adlib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * AdLib FM card driver.
  3. */
  4. #include <sound/driver.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <sound/core.h>
  9. #include <sound/initval.h>
  10. #include <sound/opl3.h>
  11. #define CRD_NAME "AdLib FM"
  12. #define DRV_NAME "snd_adlib"
  13. MODULE_DESCRIPTION(CRD_NAME);
  14. MODULE_AUTHOR("Rene Herman");
  15. MODULE_LICENSE("GPL");
  16. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  17. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  18. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
  19. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  20. module_param_array(index, int, NULL, 0444);
  21. MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
  22. module_param_array(id, charp, NULL, 0444);
  23. MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
  24. module_param_array(enable, bool, NULL, 0444);
  25. MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
  26. module_param_array(port, long, NULL, 0444);
  27. MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
  28. static struct platform_device *devices[SNDRV_CARDS];
  29. static void snd_adlib_free(struct snd_card *card)
  30. {
  31. release_and_free_resource(card->private_data);
  32. }
  33. static int __devinit snd_adlib_probe(struct platform_device *device)
  34. {
  35. struct snd_card *card;
  36. struct snd_opl3 *opl3;
  37. int error, i = device->id;
  38. if (port[i] == SNDRV_AUTO_PORT) {
  39. snd_printk(KERN_ERR DRV_NAME ": please specify port\n");
  40. error = -EINVAL;
  41. goto out0;
  42. }
  43. card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
  44. if (!card) {
  45. snd_printk(KERN_ERR DRV_NAME ": could not create card\n");
  46. error = -EINVAL;
  47. goto out0;
  48. }
  49. card->private_data = request_region(port[i], 4, CRD_NAME);
  50. if (!card->private_data) {
  51. snd_printk(KERN_ERR DRV_NAME ": could not grab ports\n");
  52. error = -EBUSY;
  53. goto out1;
  54. }
  55. card->private_free = snd_adlib_free;
  56. error = snd_opl3_create(card, port[i], port[i] + 2, OPL3_HW_AUTO, 1, &opl3);
  57. if (error < 0) {
  58. snd_printk(KERN_ERR DRV_NAME ": could not create OPL\n");
  59. goto out1;
  60. }
  61. error = snd_opl3_hwdep_new(opl3, 0, 0, NULL);
  62. if (error < 0) {
  63. snd_printk(KERN_ERR DRV_NAME ": could not create FM\n");
  64. goto out1;
  65. }
  66. strcpy(card->driver, DRV_NAME);
  67. strcpy(card->shortname, CRD_NAME);
  68. sprintf(card->longname, CRD_NAME " at %#lx", port[i]);
  69. snd_card_set_dev(card, &device->dev);
  70. error = snd_card_register(card);
  71. if (error < 0) {
  72. snd_printk(KERN_ERR DRV_NAME ": could not register card\n");
  73. goto out1;
  74. }
  75. platform_set_drvdata(device, card);
  76. return 0;
  77. out1: snd_card_free(card);
  78. out0: return error;
  79. }
  80. static int __devexit snd_adlib_remove(struct platform_device *device)
  81. {
  82. snd_card_free(platform_get_drvdata(device));
  83. platform_set_drvdata(device, NULL);
  84. return 0;
  85. }
  86. static struct platform_driver snd_adlib_driver = {
  87. .probe = snd_adlib_probe,
  88. .remove = __devexit_p(snd_adlib_remove),
  89. .driver = {
  90. .name = DRV_NAME
  91. }
  92. };
  93. static int __init alsa_card_adlib_init(void)
  94. {
  95. int i, cards;
  96. if (platform_driver_register(&snd_adlib_driver) < 0) {
  97. snd_printk(KERN_ERR DRV_NAME ": could not register driver\n");
  98. return -ENODEV;
  99. }
  100. for (cards = 0, i = 0; i < SNDRV_CARDS; i++) {
  101. struct platform_device *device;
  102. if (!enable[i])
  103. continue;
  104. device = platform_device_register_simple(DRV_NAME, i, NULL, 0);
  105. if (IS_ERR(device))
  106. continue;
  107. if (!platform_get_drvdata(device)) {
  108. platform_device_unregister(device);
  109. continue;
  110. }
  111. devices[i] = device;
  112. cards++;
  113. }
  114. if (!cards) {
  115. #ifdef MODULE
  116. printk(KERN_ERR CRD_NAME " soundcard not found or device busy\n");
  117. #endif
  118. platform_driver_unregister(&snd_adlib_driver);
  119. return -ENODEV;
  120. }
  121. return 0;
  122. }
  123. static void __exit alsa_card_adlib_exit(void)
  124. {
  125. int i;
  126. for (i = 0; i < SNDRV_CARDS; i++)
  127. platform_device_unregister(devices[i]);
  128. platform_driver_unregister(&snd_adlib_driver);
  129. }
  130. module_init(alsa_card_adlib_init);
  131. module_exit(alsa_card_adlib_exit);