vserver 1.9.3
[linux-2.6.git] / sound / pci / ymfpci / ymfpci.c
1 /*
2  *  The driver for the Yamaha's DS1/DS1E cards
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
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/time.h>
26 #include <linux/moduleparam.h>
27 #include <sound/core.h>
28 #include <sound/ymfpci.h>
29 #include <sound/mpu401.h>
30 #include <sound/opl3.h>
31 #include <sound/initval.h>
32
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34 MODULE_DESCRIPTION("Yamaha DS-XG PCI");
35 MODULE_LICENSE("GPL");
36 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF724},"
37                 "{Yamaha,YMF724F},"
38                 "{Yamaha,YMF740},"
39                 "{Yamaha,YMF740C},"
40                 "{Yamaha,YMF744},"
41                 "{Yamaha,YMF754}}");
42
43 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
44 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
45 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
46 static long fm_port[SNDRV_CARDS];
47 static long mpu_port[SNDRV_CARDS];
48 #ifdef SUPPORT_JOYSTICK
49 static long joystick_port[SNDRV_CARDS];
50 #endif
51 static int rear_switch[SNDRV_CARDS];
52 static int boot_devs;
53
54 module_param_array(index, int, boot_devs, 0444);
55 MODULE_PARM_DESC(index, "Index value for the Yamaha DS-XG PCI soundcard.");
56 module_param_array(id, charp, boot_devs, 0444);
57 MODULE_PARM_DESC(id, "ID string for the Yamaha DS-XG PCI soundcard.");
58 module_param_array(enable, bool, boot_devs, 0444);
59 MODULE_PARM_DESC(enable, "Enable Yamaha DS-XG soundcard.");
60 module_param_array(mpu_port, long, boot_devs, 0444);
61 MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
62 module_param_array(fm_port, long, boot_devs, 0444);
63 MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
64 #ifdef SUPPORT_JOYSTICK
65 module_param_array(joystick_port, long, boot_devs, 0444);
66 MODULE_PARM_DESC(joystick_port, "Joystick port address");
67 #endif
68 module_param_array(rear_switch, bool, boot_devs, 0444);
69 MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
70
71 static struct pci_device_id snd_ymfpci_ids[] = {
72         { 0x1073, 0x0004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF724 */
73         { 0x1073, 0x000d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF724F */
74         { 0x1073, 0x000a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF740 */
75         { 0x1073, 0x000c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF740C */
76         { 0x1073, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF744 */
77         { 0x1073, 0x0012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF754 */
78         { 0, }
79 };
80
81 MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
82
83 static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
84                                            const struct pci_device_id *pci_id)
85 {
86         static int dev;
87         snd_card_t *card;
88         struct resource *fm_res = NULL;
89         struct resource *mpu_res = NULL;
90 #ifdef SUPPORT_JOYSTICK
91         struct resource *joystick_res = NULL;
92 #endif
93         ymfpci_t *chip;
94         opl3_t *opl3;
95         char *str;
96         int err;
97         u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
98
99         if (dev >= SNDRV_CARDS)
100                 return -ENODEV;
101         if (!enable[dev]) {
102                 dev++;
103                 return -ENOENT;
104         }
105
106         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
107         if (card == NULL)
108                 return -ENOMEM;
109
110         switch (pci_id->device) {
111         case 0x0004: str = "YMF724"; break;
112         case 0x000d: str = "YMF724F"; break;
113         case 0x000a: str = "YMF740"; break;
114         case 0x000c: str = "YMF740C"; break;
115         case 0x0010: str = "YMF744"; break;
116         case 0x0012: str = "YMF754"; break;
117         default: str = "???"; break;
118         }
119
120         legacy_ctrl = 0;
121         legacy_ctrl2 = 0x0800;  /* SBEN = 0, SMOD = 01, LAD = 0 */
122
123         if (pci_id->device >= 0x0010) { /* YMF 744/754 */
124                 if (fm_port[dev] == 1) {
125                         /* auto-detect */
126                         fm_port[dev] = pci_resource_start(pci, 1);
127                 }
128                 if (fm_port[dev] > 0 &&
129                     (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
130                         legacy_ctrl |= YMFPCI_LEGACY_FMEN;
131                         pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
132                 }
133                 if (mpu_port[dev] == 1) {
134                         /* auto-detect */
135                         mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
136                 }
137                 if (mpu_port[dev] > 0 &&
138                     (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
139                         legacy_ctrl |= YMFPCI_LEGACY_MEN;
140                         pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
141                 }
142 #ifdef SUPPORT_JOYSTICK
143                 if (joystick_port[dev] == 1) {
144                         /* auto-detect */
145                         joystick_port[dev] = pci_resource_start(pci, 2);
146                 }
147                 if (joystick_port[dev] > 0 &&
148                     (joystick_res = request_region(joystick_port[dev], 1, "YMFPCI gameport")) != NULL) {
149                         legacy_ctrl |= YMFPCI_LEGACY_JPEN;
150                         pci_write_config_word(pci, PCIR_DSXG_JOYBASE, joystick_port[dev]);
151                 }
152 #endif
153         } else {
154                 switch (fm_port[dev]) {
155                 case 0x388: legacy_ctrl2 |= 0; break;
156                 case 0x398: legacy_ctrl2 |= 1; break;
157                 case 0x3a0: legacy_ctrl2 |= 2; break;
158                 case 0x3a8: legacy_ctrl2 |= 3; break;
159                 default: fm_port[dev] = 0; break;
160                 }
161                 if (fm_port[dev] > 0 &&
162                     (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
163                         legacy_ctrl |= YMFPCI_LEGACY_FMEN;
164                 } else {
165                         legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
166                         fm_port[dev] = 0;
167                 }
168                 switch (mpu_port[dev]) {
169                 case 0x330: legacy_ctrl2 |= 0 << 4; break;
170                 case 0x300: legacy_ctrl2 |= 1 << 4; break;
171                 case 0x332: legacy_ctrl2 |= 2 << 4; break;
172                 case 0x334: legacy_ctrl2 |= 3 << 4; break;
173                 default: mpu_port[dev] = 0; break;
174                 }
175                 if (mpu_port[dev] > 0 &&
176                     (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
177                         legacy_ctrl |= YMFPCI_LEGACY_MEN;
178                 } else {
179                         legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
180                         mpu_port[dev] = 0;
181                 }
182 #ifdef SUPPORT_JOYSTICK
183                 if (joystick_port[dev] == 1) {
184                         /* auto-detect */
185                         long p;
186                         for (p = 0x201; p <= 0x205; p++) {
187                                 if (p == 0x203) continue;
188                                 if ((joystick_res = request_region(p, 1, "YMFPCI gameport")) != NULL)
189                                         break;
190                         }
191                         if (joystick_res)
192                                 joystick_port[dev] = p;
193                 }
194                 switch (joystick_port[dev]) {
195                 case 0x201: legacy_ctrl2 |= 0 << 6; break;
196                 case 0x202: legacy_ctrl2 |= 1 << 6; break;
197                 case 0x204: legacy_ctrl2 |= 2 << 6; break;
198                 case 0x205: legacy_ctrl2 |= 3 << 6; break;
199                 default: joystick_port[dev] = 0; break;
200                 }
201                 if (! joystick_res && joystick_port[dev] > 0)
202                         joystick_res = request_region(joystick_port[dev], 1, "YMFPCI gameport");
203                 if (joystick_res) {
204                         legacy_ctrl |= YMFPCI_LEGACY_JPEN;
205                 } else {
206                         legacy_ctrl2 &= ~YMFPCI_LEGACY2_JSIO;
207                         joystick_port[dev] = 0;
208                 }
209 #endif
210         }
211         if (mpu_res) {
212                 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
213                 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
214         }
215         pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
216         pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
217         pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
218         if ((err = snd_ymfpci_create(card, pci,
219                                      old_legacy_ctrl,
220                                      &chip)) < 0) {
221                 snd_card_free(card);
222                 if (mpu_res) {
223                         release_resource(mpu_res);
224                         kfree_nocheck(mpu_res);
225                 }
226                 if (fm_res) {
227                         release_resource(fm_res);
228                         kfree_nocheck(fm_res);
229                 }
230 #ifdef SUPPORT_JOYSTICK
231                 if (joystick_res) {
232                         release_resource(joystick_res);
233                         kfree_nocheck(joystick_res);
234                 }
235 #endif
236                 return err;
237         }
238         chip->fm_res = fm_res;
239         chip->mpu_res = mpu_res;
240 #ifdef SUPPORT_JOYSTICK
241         chip->joystick_res = joystick_res;
242 #endif
243         strcpy(card->driver, str);
244         sprintf(card->shortname, "Yamaha DS-XG (%s)", str);
245         sprintf(card->longname, "%s at 0x%lx, irq %i",
246                 card->shortname,
247                 chip->reg_area_phys,
248                 chip->irq);
249         if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
250                 snd_card_free(card);
251                 return err;
252         }
253         if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
254                 snd_card_free(card);
255                 return err;
256         }
257         if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
258                 snd_card_free(card);
259                 return err;
260         }
261         if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
262                 snd_card_free(card);
263                 return err;
264         }
265         if ((err = snd_ymfpci_mixer(chip, rear_switch[dev])) < 0) {
266                 snd_card_free(card);
267                 return err;
268         }
269         if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
270                 snd_card_free(card);
271                 return err;
272         }
273         if (chip->mpu_res) {
274                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
275                                                mpu_port[dev], 1,
276                                                pci->irq, 0, &chip->rawmidi)) < 0) {
277                         printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
278                         legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
279                         pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
280                 }
281         }
282         if (chip->fm_res) {
283                 if ((err = snd_opl3_create(card,
284                                            fm_port[dev],
285                                            fm_port[dev] + 2,
286                                            OPL3_HW_OPL3, 1, &opl3)) < 0) {
287                         printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
288                         legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
289                         pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
290                 } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
291                         snd_card_free(card);
292                         snd_printk("cannot create opl3 hwdep\n");
293                         return err;
294                 }
295         }
296 #ifdef SUPPORT_JOYSTICK
297         if (chip->joystick_res) {
298                 chip->gameport.io = joystick_port[dev];
299                 gameport_register_port(&chip->gameport);
300         }
301 #endif
302
303         if ((err = snd_card_register(card)) < 0) {
304                 snd_card_free(card);
305                 return err;
306         }
307         pci_set_drvdata(pci, card);
308         dev++;
309         return 0;
310 }
311
312 static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
313 {
314         snd_card_free(pci_get_drvdata(pci));
315         pci_set_drvdata(pci, NULL);
316 }
317
318 static struct pci_driver driver = {
319         .name = "Yamaha DS-XG PCI",
320         .id_table = snd_ymfpci_ids,
321         .probe = snd_card_ymfpci_probe,
322         .remove = __devexit_p(snd_card_ymfpci_remove),
323         SND_PCI_PM_CALLBACKS
324 };
325
326 static int __init alsa_card_ymfpci_init(void)
327 {
328         return pci_module_init(&driver);
329 }
330
331 static void __exit alsa_card_ymfpci_exit(void)
332 {
333         pci_unregister_driver(&driver);
334 }
335
336 module_init(alsa_card_ymfpci_init)
337 module_exit(alsa_card_ymfpci_exit)