ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <sound/core.h>
27 #include <sound/ymfpci.h>
28 #include <sound/mpu401.h>
29 #include <sound/opl3.h>
30 #define SNDRV_GET_ID
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
54 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
55 MODULE_PARM_DESC(index, "Index value for the Yamaha DS-XG PCI soundcard.");
56 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
57 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
58 MODULE_PARM_DESC(id, "ID string for the Yamaha DS-XG PCI soundcard.");
59 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
60 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
61 MODULE_PARM_DESC(enable, "Enable Yamaha DS-XG soundcard.");
62 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
63 MODULE_PARM(mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
64 MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
65 MODULE_PARM_SYNTAX(mpu_port, SNDRV_ENABLED);
66 MODULE_PARM(fm_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
67 MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
68 MODULE_PARM_SYNTAX(fm_port, SNDRV_ENABLED);
69 #ifdef SUPPORT_JOYSTICK
70 MODULE_PARM(joystick_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
71 MODULE_PARM_DESC(joystick_port, "Joystick port address");
72 MODULE_PARM_SYNTAX(joystick_port, SNDRV_ENABLED);
73 #endif
74 MODULE_PARM(rear_switch, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
75 MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
76 MODULE_PARM_SYNTAX(rear_switch, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
77
78 static struct pci_device_id snd_ymfpci_ids[] = {
79         { 0x1073, 0x0004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF724 */
80         { 0x1073, 0x000d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF724F */
81         { 0x1073, 0x000a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF740 */
82         { 0x1073, 0x000c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF740C */
83         { 0x1073, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF744 */
84         { 0x1073, 0x0012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* YMF754 */
85         { 0, }
86 };
87
88 MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
89
90 static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
91                                            const struct pci_device_id *pci_id)
92 {
93         static int dev;
94         snd_card_t *card;
95         struct resource *fm_res = NULL;
96         struct resource *mpu_res = NULL;
97 #ifdef SUPPORT_JOYSTICK
98         struct resource *joystick_res = NULL;
99 #endif
100         ymfpci_t *chip;
101         opl3_t *opl3;
102         char *str;
103         int err;
104         u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
105
106         if (dev >= SNDRV_CARDS)
107                 return -ENODEV;
108         if (!enable[dev]) {
109                 dev++;
110                 return -ENOENT;
111         }
112
113         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
114         if (card == NULL)
115                 return -ENOMEM;
116
117         switch (pci_id->device) {
118         case 0x0004: str = "YMF724"; break;
119         case 0x000d: str = "YMF724F"; break;
120         case 0x000a: str = "YMF740"; break;
121         case 0x000c: str = "YMF740C"; break;
122         case 0x0010: str = "YMF744"; break;
123         case 0x0012: str = "YMF754"; break;
124         default: str = "???"; break;
125         }
126
127         legacy_ctrl = 0;
128         legacy_ctrl2 = 0x0800;  /* SBEN = 0, SMOD = 01, LAD = 0 */
129
130         if (pci_id->device >= 0x0010) { /* YMF 744/754 */
131                 if (fm_port[dev] == 1) {
132                         /* auto-detect */
133                         fm_port[dev] = pci_resource_start(pci, 1);
134                 }
135                 if (fm_port[dev] > 0 &&
136                     (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
137                         legacy_ctrl |= YMFPCI_LEGACY_FMEN;
138                         pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
139                 }
140                 if (mpu_port[dev] == 1) {
141                         /* auto-detect */
142                         mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
143                 }
144                 if (mpu_port[dev] > 0 &&
145                     (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
146                         legacy_ctrl |= YMFPCI_LEGACY_MEN;
147                         pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
148                 }
149 #ifdef SUPPORT_JOYSTICK
150                 if (joystick_port[dev] == 1) {
151                         /* auto-detect */
152                         joystick_port[dev] = pci_resource_start(pci, 2);
153                 }
154                 if (joystick_port[dev] > 0 &&
155                     (joystick_res = request_region(joystick_port[dev], 1, "YMFPCI gameport")) != NULL) {
156                         legacy_ctrl |= YMFPCI_LEGACY_JPEN;
157                         pci_write_config_word(pci, PCIR_DSXG_JOYBASE, joystick_port[dev]);
158                 }
159 #endif
160         } else {
161                 switch (fm_port[dev]) {
162                 case 0x388: legacy_ctrl2 |= 0; break;
163                 case 0x398: legacy_ctrl2 |= 1; break;
164                 case 0x3a0: legacy_ctrl2 |= 2; break;
165                 case 0x3a8: legacy_ctrl2 |= 3; break;
166                 default: fm_port[dev] = 0; break;
167                 }
168                 if (fm_port[dev] > 0 &&
169                     (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
170                         legacy_ctrl |= YMFPCI_LEGACY_FMEN;
171                 } else {
172                         legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
173                         fm_port[dev] = 0;
174                 }
175                 switch (mpu_port[dev]) {
176                 case 0x330: legacy_ctrl2 |= 0 << 4; break;
177                 case 0x300: legacy_ctrl2 |= 1 << 4; break;
178                 case 0x332: legacy_ctrl2 |= 2 << 4; break;
179                 case 0x334: legacy_ctrl2 |= 3 << 4; break;
180                 default: mpu_port[dev] = 0; break;
181                 }
182                 if (mpu_port[dev] > 0 &&
183                     (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
184                         legacy_ctrl |= YMFPCI_LEGACY_MEN;
185                 } else {
186                         legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
187                         mpu_port[dev] = 0;
188                 }
189 #ifdef SUPPORT_JOYSTICK
190                 if (joystick_port[dev] == 1) {
191                         /* auto-detect */
192                         long p;
193                         for (p = 0x201; p <= 0x205; p++) {
194                                 if (p == 0x203) continue;
195                                 if ((joystick_res = request_region(p, 1, "YMFPCI gameport")) != NULL)
196                                         break;
197                         }
198                         if (joystick_res)
199                                 joystick_port[dev] = p;
200                 }
201                 switch (joystick_port[dev]) {
202                 case 0x201: legacy_ctrl2 |= 0 << 6; break;
203                 case 0x202: legacy_ctrl2 |= 1 << 6; break;
204                 case 0x204: legacy_ctrl2 |= 2 << 6; break;
205                 case 0x205: legacy_ctrl2 |= 3 << 6; break;
206                 default: joystick_port[dev] = 0; break;
207                 }
208                 if (! joystick_res && joystick_port[dev] > 0)
209                         joystick_res = request_region(joystick_port[dev], 1, "YMFPCI gameport");
210                 if (joystick_res) {
211                         legacy_ctrl |= YMFPCI_LEGACY_JPEN;
212                 } else {
213                         legacy_ctrl2 &= ~YMFPCI_LEGACY2_JSIO;
214                         joystick_port[dev] = 0;
215                 }
216 #endif
217         }
218         if (mpu_res) {
219                 legacy_ctrl |= YMFPCI_LEGACY_MIEN;
220                 legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
221         }
222         pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
223         pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
224         pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
225         if ((err = snd_ymfpci_create(card, pci,
226                                      old_legacy_ctrl,
227                                      &chip)) < 0) {
228                 snd_card_free(card);
229                 if (mpu_res) {
230                         release_resource(mpu_res);
231                         kfree_nocheck(mpu_res);
232                 }
233                 if (fm_res) {
234                         release_resource(fm_res);
235                         kfree_nocheck(fm_res);
236                 }
237 #ifdef SUPPORT_JOYSTICK
238                 if (joystick_res) {
239                         release_resource(joystick_res);
240                         kfree_nocheck(joystick_res);
241                 }
242 #endif
243                 return err;
244         }
245         chip->fm_res = fm_res;
246         chip->mpu_res = mpu_res;
247 #ifdef SUPPORT_JOYSTICK
248         chip->joystick_res = joystick_res;
249 #endif
250         strcpy(card->driver, str);
251         sprintf(card->shortname, "Yamaha DS-XG (%s)", str);
252         sprintf(card->longname, "%s at 0x%lx, irq %i",
253                 card->shortname,
254                 chip->reg_area_phys,
255                 chip->irq);
256         if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
257                 snd_card_free(card);
258                 return err;
259         }
260         if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
261                 snd_card_free(card);
262                 return err;
263         }
264         if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
265                 snd_card_free(card);
266                 return err;
267         }
268         if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
269                 snd_card_free(card);
270                 return err;
271         }
272         if ((err = snd_ymfpci_mixer(chip, rear_switch[dev])) < 0) {
273                 snd_card_free(card);
274                 return err;
275         }
276         if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
277                 snd_card_free(card);
278                 return err;
279         }
280         if (chip->mpu_res) {
281                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
282                                                mpu_port[dev], 1,
283                                                pci->irq, 0, &chip->rawmidi)) < 0) {
284                         printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
285                         legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
286                         pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
287                 }
288         }
289         if (chip->fm_res) {
290                 if ((err = snd_opl3_create(card,
291                                            fm_port[dev],
292                                            fm_port[dev] + 2,
293                                            OPL3_HW_OPL3, 1, &opl3)) < 0) {
294                         printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
295                         legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
296                         pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
297                 } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
298                         snd_card_free(card);
299                         snd_printk("cannot create opl3 hwdep\n");
300                         return err;
301                 }
302         }
303 #ifdef SUPPORT_JOYSTICK
304         if (chip->joystick_res) {
305                 chip->gameport.io = joystick_port[dev];
306                 gameport_register_port(&chip->gameport);
307         }
308 #endif
309
310         if ((err = snd_card_register(card)) < 0) {
311                 snd_card_free(card);
312                 return err;
313         }
314         pci_set_drvdata(pci, chip);
315         dev++;
316         return 0;
317 }
318
319 #ifdef CONFIG_PM
320 static int snd_card_ymfpci_suspend(struct pci_dev *pci, u32 state)
321 {
322         ymfpci_t *chip = snd_magic_cast(ymfpci_t, pci_get_drvdata(pci), return -ENXIO);
323         snd_ymfpci_suspend(chip);
324         return 0;
325 }
326 static int snd_card_ymfpci_resume(struct pci_dev *pci)
327 {
328         ymfpci_t *chip = snd_magic_cast(ymfpci_t, pci_get_drvdata(pci), return -ENXIO);
329         snd_ymfpci_resume(chip);
330         return 0;
331 }
332 #endif
333
334 static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
335 {
336         ymfpci_t *chip = snd_magic_cast(ymfpci_t, pci_get_drvdata(pci), return);
337         if (chip)
338                 snd_card_free(chip->card);
339         pci_set_drvdata(pci, NULL);
340 }
341
342 static struct pci_driver driver = {
343         .name = "Yamaha DS-XG PCI",
344         .id_table = snd_ymfpci_ids,
345         .probe = snd_card_ymfpci_probe,
346         .remove = __devexit_p(snd_card_ymfpci_remove),
347 #ifdef CONFIG_PM
348         .suspend = snd_card_ymfpci_suspend,
349         .resume = snd_card_ymfpci_resume,
350 #endif  
351 };
352
353 static int __init alsa_card_ymfpci_init(void)
354 {
355         int err;
356
357         if ((err = pci_module_init(&driver)) < 0) {
358 #ifdef MODULE
359                 printk(KERN_ERR "Yamaha DS-XG PCI soundcard not found or device busy\n");
360 #endif
361                 return err;
362         }
363         return 0;
364 }
365
366 static void __exit alsa_card_ymfpci_exit(void)
367 {
368         pci_unregister_driver(&driver);
369 }
370
371 module_init(alsa_card_ymfpci_init)
372 module_exit(alsa_card_ymfpci_exit)
373
374 #ifndef MODULE
375
376 /* format is: snd-ymfpci=enable,index,id,
377                          fm_port,mpu_port */
378
379 static int __init alsa_card_ymfpci_setup(char *str)
380 {
381         static unsigned __initdata nr_dev = 0;
382
383         if (nr_dev >= SNDRV_CARDS)
384                 return 0;
385         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
386                get_option(&str,&index[nr_dev]) == 2 &&
387                get_id(&str,&id[nr_dev]) == 2 &&
388                get_option_long(&str,&fm_port[nr_dev]) == 2 &&
389                get_option_long(&str,&mpu_port[nr_dev]) == 2);
390         nr_dev++;
391         return 1;
392 }
393
394 __setup("snd-ymfpci=", alsa_card_ymfpci_setup);
395
396 #endif /* ifndef MODULE */