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