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