Merge branch 'upstream'
[linux-2.6] / sound / oss / opl3sa2.c
1 /*
2  * sound/opl3sa2.c
3  *
4  * A low level driver for Yamaha OPL3-SA2 and SA3 cards.
5  * NOTE: All traces of the name OPL3-SAx have now (December 2000) been
6  *       removed from the driver code, as an email exchange with Yamaha
7  *       provided the information that the YMF-719 is indeed just a
8  *       re-badged 715.
9  *
10  * Copyright 1998-2001 Scott Murray <scott@spiteful.org>
11  *
12  * Originally based on the CS4232 driver (in cs4232.c) by Hannu Savolainen
13  * and others.  Now incorporates code/ideas from pss.c, also by Hannu
14  * Savolainen.  Both of those files are distributed with the following
15  * license:
16  *
17  * "Copyright (C) by Hannu Savolainen 1993-1997
18  *
19  *  OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
20  *  Version 2 (June 1991). See the "COPYING" file distributed with this software
21  *  for more info."
22  *
23  * As such, in accordance with the above license, this file, opl3sa2.c, is
24  * distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2 (June 1991).
25  * See the "COPYING" file distributed with this software for more information.
26  *
27  * Change History
28  * --------------
29  * Scott Murray            Original driver (Jun 14, 1998)
30  * Paul J.Y. Lahaie        Changed probing / attach code order
31  * Scott Murray            Added mixer support (Dec 03, 1998)
32  * Scott Murray            Changed detection code to be more forgiving,
33  *                         added force option as last resort,
34  *                         fixed ioctl return values. (Dec 30, 1998)
35  * Scott Murray            Simpler detection code should work all the time now
36  *                         (with thanks to Ben Hutchings for the heuristic),
37  *                         removed now unnecessary force option. (Jan 5, 1999)
38  * Christoph Hellwig       Adapted to module_init/module_exit (Mar 4, 2000)
39  * Scott Murray            Reworked SA2 versus SA3 mixer code, updated chipset
40  *                         version detection code (again!). (Dec 5, 2000)
41  * Scott Murray            Adjusted master volume mixer scaling. (Dec 6, 2000)
42  * Scott Murray            Based on a patch by Joel Yliluoma (aka Bisqwit),
43  *                         integrated wide mixer and adjusted mic, bass, treble
44  *                         scaling. (Dec 6, 2000)
45  * Scott Murray            Based on a patch by Peter Englmaier, integrated
46  *                         ymode and loopback options. (Dec 6, 2000)
47  * Scott Murray            Inspired by a patch by Peter Englmaier, and based on
48  *                         what ALSA does, added initialization code for the
49  *                         default DMA and IRQ settings. (Dec 6, 2000)
50  * Scott Murray            Added some more checks to the card detection code,
51  *                         based on what ALSA does. (Dec 12, 2000)
52  * Scott Murray            Inspired by similar patches from John Fremlin,
53  *                         Jim Radford, Mike Rolig, and Ingmar Steen, added 2.4
54  *                         ISA PnP API support, mainly based on bits from
55  *                         sb_card.c and awe_wave.c. (Dec 12, 2000)
56  * Scott Murray            Some small cleanups to the init code output.
57  *                         (Jan 7, 2001)
58  * Zwane Mwaikambo         Added PM support. (Dec 4 2001)
59  *
60  * Adam Belay              Converted driver to new PnP Layer (Oct 12, 2002)
61  * Zwane Mwaikambo         Code, data structure cleanups. (Feb 15 2002)
62  * Zwane Mwaikambo         Free resources during auxiliary device probe
63  *                         failures (Apr 29 2002)
64  *   
65  */
66
67 #include <linux/config.h>
68 #include <linux/pnp.h>
69 #include <linux/init.h>
70 #include <linux/module.h>
71 #include <linux/delay.h>
72 #include <linux/pm.h>
73 #include <linux/pm_legacy.h>
74 #include "sound_config.h"
75
76 #include "ad1848.h"
77 #include "mpu401.h"
78
79 #define OPL3SA2_MODULE_NAME     "opl3sa2"
80 #define PFX                     OPL3SA2_MODULE_NAME ": "
81
82 /* Useful control port indexes: */
83 #define OPL3SA2_PM           0x01
84 #define OPL3SA2_SYS_CTRL     0x02
85 #define OPL3SA2_IRQ_CONFIG   0x03
86 #define OPL3SA2_DMA_CONFIG   0x06
87 #define OPL3SA2_MASTER_LEFT  0x07
88 #define OPL3SA2_MASTER_RIGHT 0x08
89 #define OPL3SA2_MIC          0x09
90 #define OPL3SA2_MISC         0x0A
91
92 #define OPL3SA3_WIDE         0x14
93 #define OPL3SA3_BASS         0x15
94 #define OPL3SA3_TREBLE       0x16
95
96 /* Useful constants: */
97 #define DEFAULT_VOLUME 50
98 #define DEFAULT_MIC    50
99 #define DEFAULT_TIMBRE 0
100
101 /* Power saving modes */
102 #define OPL3SA2_PM_MODE0        0x00
103 #define OPL3SA2_PM_MODE1        0x04    /* PSV */
104 #define OPL3SA2_PM_MODE2        0x05    /* PSV | PDX */
105 #define OPL3SA2_PM_MODE3        0x27    /* ADOWN | PSV | PDN | PDX */
106
107
108 /* For checking against what the card returns: */
109 #define VERSION_UNKNOWN 0
110 #define VERSION_YMF711  1
111 #define VERSION_YMF715  2
112 #define VERSION_YMF715B 3
113 #define VERSION_YMF715E 4
114 /* also assuming that anything > 4 but <= 7 is a 715E */
115
116 /* Chipset type constants for use below */
117 #define CHIPSET_UNKNOWN -1
118 #define CHIPSET_OPL3SA2 0
119 #define CHIPSET_OPL3SA3 1
120 static const char *CHIPSET_TABLE[] = {"OPL3-SA2", "OPL3-SA3"};
121
122 #ifdef CONFIG_PNP
123 #define OPL3SA2_CARDS_MAX 4
124 #else
125 #define OPL3SA2_CARDS_MAX 1
126 #endif
127
128 /* This should be pretty obvious */
129 static int opl3sa2_cards_num;
130
131 typedef struct {
132         /* device resources */
133         unsigned short cfg_port;
134         struct address_info cfg;
135         struct address_info cfg_mss;
136         struct address_info cfg_mpu;
137 #ifdef CONFIG_PNP
138         /* PnP Stuff */
139         struct pnp_dev* pdev;
140         int activated;                  /* Whether said devices have been activated */
141 #endif
142 #ifdef CONFIG_PM_LEGACY
143         unsigned int    in_suspend;
144         struct pm_dev   *pmdev;
145 #endif
146         unsigned int    card;
147         int             chipset;        /* What's my version(s)? */
148         char            *chipset_name;
149
150         /* mixer data */
151         int             mixer;
152         unsigned int    volume_l;
153         unsigned int    volume_r;
154         unsigned int    mic;
155         unsigned int    bass_l;
156         unsigned int    bass_r;
157         unsigned int    treble_l;
158         unsigned int    treble_r;
159         unsigned int    wide_l;
160         unsigned int    wide_r;
161 } opl3sa2_state_t;
162 static opl3sa2_state_t opl3sa2_state[OPL3SA2_CARDS_MAX];
163
164         
165
166 /* Our parameters */
167 static int __initdata io        = -1;
168 static int __initdata mss_io    = -1;
169 static int __initdata mpu_io    = -1;
170 static int __initdata irq       = -1;
171 static int __initdata dma       = -1;
172 static int __initdata dma2      = -1;
173 static int __initdata ymode     = -1;
174 static int __initdata loopback  = -1;
175
176 #ifdef CONFIG_PNP
177 /* PnP specific parameters */
178 static int __initdata isapnp = 1;
179 static int __initdata multiple = 1;
180
181 /* Whether said devices have been activated */
182 static int opl3sa2_activated[OPL3SA2_CARDS_MAX];
183 #else
184 static int __initdata isapnp; /* = 0 */
185 static int __initdata multiple; /* = 0 */
186 #endif
187
188 MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver).");
189 MODULE_AUTHOR("Scott Murray <scott@spiteful.org>");
190 MODULE_LICENSE("GPL");
191
192
193 module_param(io, int, 0);
194 MODULE_PARM_DESC(io, "Set I/O base of OPL3-SA2 or SA3 card (usually 0x370.  Address must be even and must be from 0x100 to 0xFFE)");
195
196 module_param(mss_io, int, 0);
197 MODULE_PARM_DESC(mss_io, "Set MSS (audio) I/O base (0x530, 0xE80, or other. Address must end in 0 or 4 and must be from 0x530 to 0xF48)");
198
199 module_param(mpu_io, int, 0);
200 MODULE_PARM_DESC(mpu_io, "Set MIDI I/O base (0x330 or other. Address must be even and must be from 0x300 to 0x334)");
201
202 module_param(irq, int, 0);
203 MODULE_PARM_DESC(irq, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)");
204
205 module_param(dma, int, 0);
206 MODULE_PARM_DESC(dma, "Set MSS (audio) first DMA channel (0, 1, 3)");
207
208 module_param(dma2, int, 0);
209 MODULE_PARM_DESC(dma2, "Set MSS (audio) second DMA channel (0, 1, 3)");
210
211 module_param(ymode, int, 0);
212 MODULE_PARM_DESC(ymode, "Set Yamaha 3D enhancement mode (0 = Desktop/Normal, 1 = Notebook PC (1), 2 = Notebook PC (2), 3 = Hi-Fi)");
213
214 module_param(loopback, int, 0);
215 MODULE_PARM_DESC(loopback, "Set A/D input source. Useful for echo cancellation (0 = Mic Rch (default), 1 = Mono output loopback)");
216
217 #ifdef CONFIG_PNP
218 module_param(isapnp, bool, 0);
219 MODULE_PARM_DESC(isapnp, "When set to 0, ISA PnP support will be disabled");
220
221 module_param(multiple, bool, 0);
222 MODULE_PARM_DESC(multiple, "When set to 0, will not search for multiple cards");
223 #endif
224
225
226 /*
227  * Standard read and write functions
228 */
229
230 static inline void opl3sa2_write(unsigned short port,
231                                  unsigned char  index,
232                                  unsigned char  data)
233 {
234         outb_p(index, port);
235         outb(data, port + 1);
236 }
237
238
239 static inline void opl3sa2_read(unsigned short port,
240                                 unsigned char  index,
241                                 unsigned char* data)
242 {
243         outb_p(index, port);
244         *data = inb(port + 1);
245 }
246
247
248 /*
249  * All of the mixer functions...
250  */
251
252 static void opl3sa2_set_volume(opl3sa2_state_t* devc, int left, int right)
253 {
254         static unsigned char scale[101] = {
255                 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
256                 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0c,
257                 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b,
258                 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09,
259                 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x08,
260                 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
261                 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
262                 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
263                 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01,
264                 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
265                 0x00
266         };
267         unsigned char vol;
268
269         vol = scale[left];
270
271         /* If level is zero, turn on mute */
272         if(!left)
273                 vol |= 0x80;
274
275         opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
276
277         vol = scale[right];
278
279         /* If level is zero, turn on mute */
280         if(!right)
281                 vol |= 0x80;
282
283         opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol);
284 }
285
286
287 static void opl3sa2_set_mic(opl3sa2_state_t* devc, int level)
288 {
289         unsigned char vol = 0x1F;
290
291         if((level >= 0) && (level <= 100))
292                 vol = 0x1F - (unsigned char) (32 * level / 101);
293
294         /* If level is zero, turn on mute */
295         if(!level)
296                 vol |= 0x80;
297
298         opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
299 }
300
301
302 static void opl3sa3_set_bass(opl3sa2_state_t* devc, int left, int right)
303 {
304         unsigned char bass;
305
306         bass = left ? ((unsigned char) (8 * left / 101)) : 0; 
307         bass |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
308
309         opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass);
310 }
311
312
313 static void opl3sa3_set_treble(opl3sa2_state_t* devc, int left, int right)
314 {       
315         unsigned char treble;
316
317         treble = left ? ((unsigned char) (8 * left / 101)) : 0; 
318         treble |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
319
320         opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble);
321 }
322
323
324
325
326 static void opl3sa2_mixer_reset(opl3sa2_state_t* devc)
327 {
328         if (devc) {
329                 opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME);
330                 devc->volume_l = devc->volume_r = DEFAULT_VOLUME;
331
332                 opl3sa2_set_mic(devc, DEFAULT_MIC);
333                 devc->mic = DEFAULT_MIC;
334
335                 if (devc->chipset == CHIPSET_OPL3SA3) {
336                         opl3sa3_set_bass(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
337                         devc->bass_l = devc->bass_r = DEFAULT_TIMBRE;
338                         opl3sa3_set_treble(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
339                         devc->treble_l = devc->treble_r = DEFAULT_TIMBRE;
340                 }
341         }
342 }
343
344 /* Currently only used for power management */
345 #ifdef CONFIG_PM_LEGACY
346 static void opl3sa2_mixer_restore(opl3sa2_state_t* devc)
347 {
348         if (devc) {
349                 opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
350                 opl3sa2_set_mic(devc, devc->mic);
351
352                 if (devc->chipset == CHIPSET_OPL3SA3) {
353                         opl3sa3_set_bass(devc, devc->bass_l, devc->bass_r);
354                         opl3sa3_set_treble(devc, devc->treble_l, devc->treble_r);
355                 }
356         }
357 }
358 #endif /* CONFIG_PM_LEGACY */
359
360 static inline void arg_to_vol_mono(unsigned int vol, int* value)
361 {
362         int left;
363         
364         left = vol & 0x00ff;
365         if (left > 100)
366                 left = 100;
367         *value = left;
368 }
369
370
371 static inline void arg_to_vol_stereo(unsigned int vol, int* aleft, int* aright)
372 {
373         arg_to_vol_mono(vol, aleft);
374         arg_to_vol_mono(vol >> 8, aright);
375 }
376
377
378 static inline int ret_vol_mono(int vol)
379 {
380         return ((vol << 8) | vol);
381 }
382
383
384 static inline int ret_vol_stereo(int left, int right)
385 {
386         return ((right << 8) | left);
387 }
388
389
390 static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
391 {
392         int retval, value, cmdf = cmd & 0xff;
393         int __user *p = (int __user *)arg;
394
395         opl3sa2_state_t* devc = &opl3sa2_state[dev];
396         
397         switch (cmdf) {
398                 case SOUND_MIXER_VOLUME:
399                 case SOUND_MIXER_MIC:
400                 case SOUND_MIXER_DEVMASK:
401                 case SOUND_MIXER_STEREODEVS: 
402                 case SOUND_MIXER_RECMASK:
403                 case SOUND_MIXER_RECSRC:
404                 case SOUND_MIXER_CAPS: 
405                         break;
406
407                 default:
408                         return -EINVAL;
409         }
410         
411         if (((cmd >> 8) & 0xff) != 'M')
412                 return -EINVAL;
413                 
414         retval = 0;
415         if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
416                 switch (cmdf) {
417                         case SOUND_MIXER_VOLUME:
418                                 retval = get_user(value, (unsigned __user *) arg);
419                                 if (retval)
420                                         break;
421                                 arg_to_vol_stereo(value, &devc->volume_l, &devc->volume_r);
422                                 opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
423                                 value = ret_vol_stereo(devc->volume_l, devc->volume_r);
424                                 retval = put_user(value, p);
425                                 break;
426                   
427                         case SOUND_MIXER_MIC:
428                                 retval = get_user(value, (unsigned __user *) arg);
429                                 if (retval)
430                                         break;
431                                 arg_to_vol_mono(value, &devc->mic);
432                                 opl3sa2_set_mic(devc, devc->mic);
433                                 value = ret_vol_mono(devc->mic);
434                                 retval = put_user(value, p);
435                                 break;
436
437                         default:
438                                 retval = -EINVAL;
439                 }
440         }
441         else {
442                 /*
443                  * Return parameters
444                  */
445                 switch (cmdf) {
446                         case SOUND_MIXER_DEVMASK:
447                                 retval = put_user(SOUND_MASK_VOLUME | SOUND_MASK_MIC, p);
448                                 break;
449                   
450                         case SOUND_MIXER_STEREODEVS:
451                                 retval = put_user(SOUND_MASK_VOLUME, p);
452                                 break;
453                   
454                         case SOUND_MIXER_RECMASK:
455                                 /* No recording devices */
456                                 retval = put_user(0, p);
457                                 break;
458
459                         case SOUND_MIXER_CAPS:
460                                 retval = put_user(SOUND_CAP_EXCL_INPUT, p);
461                                 break;
462
463                         case SOUND_MIXER_RECSRC:
464                                 /* No recording source */
465                                 retval = put_user(0, p);
466                                 break;
467
468                         case SOUND_MIXER_VOLUME:
469                                 value = ret_vol_stereo(devc->volume_l, devc->volume_r);
470                                 retval = put_user(value, p);
471                                 break;
472                           
473                         case SOUND_MIXER_MIC:
474                                 value = ret_vol_mono(devc->mic);
475                                 put_user(value, p);
476                                 break;
477
478                         default:
479                                 retval = -EINVAL;
480                 }
481         }
482         return retval;
483 }
484 /* opl3sa2_mixer_ioctl end */
485
486
487 static int opl3sa3_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
488 {
489         int value, retval, cmdf = cmd & 0xff;
490
491         opl3sa2_state_t* devc = &opl3sa2_state[dev];
492
493         switch (cmdf) {
494         case SOUND_MIXER_BASS:
495                 value = ret_vol_stereo(devc->bass_l, devc->bass_r);
496                 retval = put_user(value, (int __user *) arg);
497                 break;
498                 
499         case SOUND_MIXER_TREBLE:
500                 value = ret_vol_stereo(devc->treble_l, devc->treble_r);
501                 retval = put_user(value, (int __user *) arg);
502                 break;
503
504         case SOUND_MIXER_DIGITAL1:
505                 value = ret_vol_stereo(devc->wide_l, devc->wide_r);
506                 retval = put_user(value, (int __user *) arg);
507                 break;
508
509         default:
510                 retval = -EINVAL;
511         }
512         return retval;
513 }
514 /* opl3sa3_mixer_ioctl end */
515
516
517 static struct mixer_operations opl3sa2_mixer_operations =
518 {
519         .owner  = THIS_MODULE,
520         .id     = "OPL3-SA2",
521         .name   = "Yamaha OPL3-SA2",
522         .ioctl  = opl3sa2_mixer_ioctl
523 };
524
525 static struct mixer_operations opl3sa3_mixer_operations =
526 {
527         .owner  = THIS_MODULE,
528         .id     = "OPL3-SA3",
529         .name   = "Yamaha OPL3-SA3",
530         .ioctl  = opl3sa3_mixer_ioctl
531 };
532
533 /* End of mixer-related stuff */
534
535
536 /*
537  * Component probe, attach, unload functions
538  */
539
540 static inline void __exit unload_opl3sa2_mpu(struct address_info *hw_config)
541 {
542         unload_mpu401(hw_config);
543 }
544
545
546 static void __init attach_opl3sa2_mss(struct address_info* hw_config, struct resource *ports)
547 {
548         int initial_mixers;
549
550         initial_mixers = num_mixers;
551         attach_ms_sound(hw_config, ports, THIS_MODULE); /* Slot 0 */
552         if (hw_config->slots[0] != -1) {
553                 /* Did the MSS driver install? */
554                 if(num_mixers == (initial_mixers + 1)) {
555                         /* The MSS mixer is installed, reroute mixers appropiately */
556                         AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
557                         AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
558                         AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
559                 }
560                 else {
561                         printk(KERN_ERR PFX "MSS mixer not installed?\n");
562                 }
563         }
564 }
565
566
567 static inline void __exit unload_opl3sa2_mss(struct address_info* hw_config)
568 {
569         unload_ms_sound(hw_config);
570 }
571
572
573 static int __init probe_opl3sa2(struct address_info* hw_config, int card)
574 {
575         unsigned char misc;
576         unsigned char tmp;
577         unsigned char version;
578
579         /*
580          * Try and allocate our I/O port range.
581          */
582         if (!request_region(hw_config->io_base, 2, OPL3SA2_MODULE_NAME)) {
583                 printk(KERN_ERR PFX "Control I/O port %#x not free\n",
584                        hw_config->io_base);
585                 goto out_nodev;
586         }
587
588         /*
589          * Check if writing to the read-only version bits of the miscellaneous
590          * register succeeds or not (it should not).
591          */
592         opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
593         opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc ^ 0x07);
594         opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &tmp);
595         if(tmp != misc) {
596                 printk(KERN_ERR PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
597                        hw_config->io_base);
598                 goto out_region;
599         }
600
601         /*
602          * Check if the MIC register is accessible.
603          */
604         opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
605         opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, 0x8a);
606         opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
607         if((tmp & 0x9f) != 0x8a) {
608                 printk(KERN_ERR
609                        PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
610                        hw_config->io_base);
611                 goto out_region;
612         }
613         opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, tmp);
614
615         /*
616          * Determine chipset type (SA2 or SA3)
617          *
618          * This is done by looking at the chipset version in the lower 3 bits
619          * of the miscellaneous register.
620          */
621         version = misc & 0x07;
622         printk(KERN_DEBUG PFX "Chipset version = %#x\n", version);
623         switch (version) {
624                 case 0:
625                         opl3sa2_state[card].chipset = CHIPSET_UNKNOWN;
626                         printk(KERN_ERR
627                                PFX "Unknown Yamaha audio controller version\n");
628                         break;
629
630                 case VERSION_YMF711:
631                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA2;
632                         printk(KERN_INFO PFX "Found OPL3-SA2 (YMF711)\n");
633                         break;
634
635                 case VERSION_YMF715:
636                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
637                         printk(KERN_INFO
638                                PFX "Found OPL3-SA3 (YMF715 or YMF719)\n");
639                         break;
640
641                 case VERSION_YMF715B:
642                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
643                         printk(KERN_INFO
644                                PFX "Found OPL3-SA3 (YMF715B or YMF719B)\n");
645                         break;
646
647                 case VERSION_YMF715E:
648                 default:
649                         opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
650                         printk(KERN_INFO
651                                PFX "Found OPL3-SA3 (YMF715E or YMF719E)\n");
652                         break;
653         }
654
655         if (opl3sa2_state[card].chipset != CHIPSET_UNKNOWN) {
656                 /* Generate a pretty name */
657                 opl3sa2_state[card].chipset_name = (char *)CHIPSET_TABLE[opl3sa2_state[card].chipset];
658                 return 0;
659         }
660
661 out_region:
662         release_region(hw_config->io_base, 2);
663 out_nodev:
664         return -ENODEV;
665 }
666
667
668 static void __init attach_opl3sa2(struct address_info* hw_config, int card)
669 {
670         /* Initialize IRQ configuration to IRQ-B: -, IRQ-A: WSS+MPU+OPL3 */
671         opl3sa2_write(hw_config->io_base, OPL3SA2_IRQ_CONFIG, 0x0d);
672
673         /* Initialize DMA configuration */
674         if(hw_config->dma2 == hw_config->dma) {
675                 /* Want DMA configuration DMA-B: -, DMA-A: WSS-P+WSS-R */
676                 opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x03);
677         }
678         else {
679                 /* Want DMA configuration DMA-B: WSS-R, DMA-A: WSS-P */
680                 opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x21);
681         }
682 }
683
684
685 static void __init attach_opl3sa2_mixer(struct address_info *hw_config, int card)
686 {
687         struct mixer_operations* mixer_operations;
688         opl3sa2_state_t* devc = &opl3sa2_state[card];
689
690         /* Install master mixer */
691         if (devc->chipset == CHIPSET_OPL3SA3) {
692                 mixer_operations = &opl3sa3_mixer_operations;
693         }
694         else {
695                 mixer_operations = &opl3sa2_mixer_operations;
696         }
697
698         devc->cfg_port = hw_config->io_base;
699         devc->mixer = sound_install_mixer(MIXER_DRIVER_VERSION,
700                                           mixer_operations->name,
701                                           mixer_operations,
702                                           sizeof(struct mixer_operations),
703                                           devc);
704         if(devc->mixer < 0) {
705                 printk(KERN_ERR PFX "Could not install %s master mixer\n",
706                          mixer_operations->name);
707         }
708         else {
709                         opl3sa2_mixer_reset(devc);
710
711         }
712 }
713
714
715 static void opl3sa2_clear_slots(struct address_info* hw_config)
716 {
717         int i;
718
719         for(i = 0; i < 6; i++) {
720                 hw_config->slots[i] = -1;
721         }
722 }
723
724
725 static void __init opl3sa2_set_ymode(struct address_info* hw_config, int ymode)
726 {
727         /*
728          * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
729          * it's supported.
730          *
731          * 0: Desktop (aka normal)   5-12 cm speakers
732          * 1: Notebook PC mode 1     3 cm speakers
733          * 2: Notebook PC mode 2     1.5 cm speakers
734          * 3: Hi-fi                  16-38 cm speakers
735          */
736         if(ymode >= 0 && ymode <= 3) {
737                 unsigned char sys_ctrl;
738
739                 opl3sa2_read(hw_config->io_base, OPL3SA2_SYS_CTRL, &sys_ctrl);
740                 sys_ctrl = (sys_ctrl & 0xcf) | ((ymode & 3) << 4);
741                 opl3sa2_write(hw_config->io_base, OPL3SA2_SYS_CTRL, sys_ctrl);
742         }
743         else {
744                 printk(KERN_ERR PFX "not setting ymode, it must be one of 0,1,2,3\n");
745         }
746 }
747
748
749 static void __init opl3sa2_set_loopback(struct address_info* hw_config, int loopback)
750 {
751         if(loopback >= 0 && loopback <= 1) {
752                 unsigned char misc;
753
754                 opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
755                 misc = (misc & 0xef) | ((loopback & 1) << 4);
756                 opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc);
757         }
758         else {
759                 printk(KERN_ERR PFX "not setting loopback, it must be either 0 or 1\n");
760         }
761 }
762
763
764 static void __exit unload_opl3sa2(struct address_info* hw_config, int card)
765 {
766         /* Release control ports */
767         release_region(hw_config->io_base, 2);
768
769         /* Unload mixer */
770         if(opl3sa2_state[card].mixer >= 0)
771                 sound_unload_mixerdev(opl3sa2_state[card].mixer);
772
773 }
774
775 #ifdef CONFIG_PNP
776 static struct pnp_device_id pnp_opl3sa2_list[] = {
777         {.id = "YMH0021", .driver_data = 0},
778         {.id = ""}
779 };
780
781 MODULE_DEVICE_TABLE(pnp, pnp_opl3sa2_list);
782
783 static int opl3sa2_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
784 {
785         int card = opl3sa2_cards_num;
786
787         /* we don't actually want to return an error as the user may have specified
788          * no multiple card search
789          */
790
791         if (opl3sa2_cards_num == OPL3SA2_CARDS_MAX)
792                 return 0;
793         opl3sa2_activated[card] = 1;
794
795         /* Our own config: */
796         opl3sa2_state[card].cfg.io_base = pnp_port_start(dev, 4);
797         opl3sa2_state[card].cfg.irq     = pnp_irq(dev, 0);
798         opl3sa2_state[card].cfg.dma     = pnp_dma(dev, 0);
799         opl3sa2_state[card].cfg.dma2    = pnp_dma(dev, 1);
800
801         /* The MSS config: */
802         opl3sa2_state[card].cfg_mss.io_base      = pnp_port_start(dev, 1);
803         opl3sa2_state[card].cfg_mss.irq          = pnp_irq(dev, 0);
804         opl3sa2_state[card].cfg_mss.dma          = pnp_dma(dev, 0);
805         opl3sa2_state[card].cfg_mss.dma2         = pnp_dma(dev, 1);
806         opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
807
808         opl3sa2_state[card].cfg_mpu.io_base       = pnp_port_start(dev, 3);
809         opl3sa2_state[card].cfg_mpu.irq           = pnp_irq(dev, 0);
810         opl3sa2_state[card].cfg_mpu.dma           = -1;
811         opl3sa2_state[card].cfg_mpu.dma2          = -1;
812         opl3sa2_state[card].cfg_mpu.always_detect = 1; /* It's there, so use shared IRQs */
813
814         /* Call me paranoid: */
815         opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
816         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
817         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
818
819         opl3sa2_state[card].pdev = dev;
820         opl3sa2_cards_num++;
821
822         return 0;
823 }
824
825 static struct pnp_driver opl3sa2_driver = {
826         .name           = "opl3sa2",
827         .id_table       = pnp_opl3sa2_list,
828         .probe          = opl3sa2_pnp_probe,
829 };
830
831 #endif /* CONFIG_PNP */
832
833 /* End of component functions */
834
835 #ifdef CONFIG_PM_LEGACY
836
837 static DEFINE_SPINLOCK(opl3sa2_lock);
838
839 /* Power Management support functions */
840 static int opl3sa2_suspend(struct pm_dev *pdev, unsigned int pm_mode)
841 {
842         unsigned long flags;
843         opl3sa2_state_t *p;
844
845         if (!pdev)
846                 return -EINVAL;
847
848         spin_lock_irqsave(&opl3sa2_lock,flags);
849
850         p = (opl3sa2_state_t *) pdev->data;
851         switch (pm_mode) {
852         case 1:
853                 pm_mode = OPL3SA2_PM_MODE1;
854                 break;
855         case 2:
856                 pm_mode = OPL3SA2_PM_MODE2;
857                 break;
858         case 3:
859                 pm_mode = OPL3SA2_PM_MODE3;
860                 break;
861         default:
862                 /* we don't know howto handle this... */
863                 spin_unlock_irqrestore(&opl3sa2_lock, flags);
864                 return -EBUSY;
865         }
866
867         p->in_suspend = 1;
868
869         /* its supposed to automute before suspending, so we won't bother */
870         opl3sa2_write(p->cfg_port, OPL3SA2_PM, pm_mode);
871         /* wait a while for the clock oscillator to stabilise */
872         mdelay(10);
873
874         spin_unlock_irqrestore(&opl3sa2_lock,flags);
875         return 0;
876 }
877
878 static int opl3sa2_resume(struct pm_dev *pdev)
879 {
880         unsigned long flags;
881         opl3sa2_state_t *p;
882
883         if (!pdev)
884                 return -EINVAL;
885
886         p = (opl3sa2_state_t *) pdev->data;
887         spin_lock_irqsave(&opl3sa2_lock,flags);
888
889         /* I don't think this is necessary */
890         opl3sa2_write(p->cfg_port, OPL3SA2_PM, OPL3SA2_PM_MODE0);
891         opl3sa2_mixer_restore(p);
892         p->in_suspend = 0;
893
894         spin_unlock_irqrestore(&opl3sa2_lock,flags);
895         return 0;
896 }
897
898 static int opl3sa2_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
899 {
900         unsigned long mode = (unsigned  long)data;
901
902         switch (rqst) {
903                 case PM_SUSPEND:
904                         return opl3sa2_suspend(pdev, mode);
905
906                 case PM_RESUME:
907                         return opl3sa2_resume(pdev);
908         }
909         return 0;
910 }
911 #endif /* CONFIG_PM_LEGACY */
912
913 /*
914  * Install OPL3-SA2 based card(s).
915  *
916  * Need to have ad1848 and mpu401 loaded ready.
917  */
918 static int __init init_opl3sa2(void)
919 {
920         int card, max;
921
922         /* Sanitize isapnp and multiple settings */
923         isapnp = isapnp != 0 ? 1 : 0;
924         multiple = multiple != 0 ? 1 : 0;
925
926         max = (multiple && isapnp) ? OPL3SA2_CARDS_MAX : 1;
927
928 #ifdef CONFIG_PNP
929         if (isapnp){
930                 pnp_register_driver(&opl3sa2_driver);
931                 if(!opl3sa2_cards_num){
932                         printk(KERN_INFO PFX "No PnP cards found\n");
933                         isapnp = 0;
934                 }
935                 max = opl3sa2_cards_num;
936         }
937 #endif
938
939         for (card = 0; card < max; card++) {
940                 /* If a user wants an I/O then assume they meant it */
941                 struct resource *ports;
942                 int base;
943                 
944                 if (!isapnp) {
945                         if (io == -1 || irq == -1 || dma == -1 ||
946                             dma2 == -1 || mss_io == -1) {
947                                 printk(KERN_ERR
948                                        PFX "io, mss_io, irq, dma, and dma2 must be set\n");
949                                 return -EINVAL;
950                         }
951                         opl3sa2_cards_num++;
952
953                         /*
954                          * Our own config:
955                          * (NOTE: IRQ and DMA aren't used, so they're set to
956                          *  give pretty output from conf_printf. :)
957                          */
958                         opl3sa2_state[card].cfg.io_base = io;
959                         opl3sa2_state[card].cfg.irq     = irq;
960                         opl3sa2_state[card].cfg.dma     = dma;
961                         opl3sa2_state[card].cfg.dma2    = dma2;
962         
963                         /* The MSS config: */
964                         opl3sa2_state[card].cfg_mss.io_base      = mss_io;
965                         opl3sa2_state[card].cfg_mss.irq          = irq;
966                         opl3sa2_state[card].cfg_mss.dma          = dma;
967                         opl3sa2_state[card].cfg_mss.dma2         = dma2;
968                         opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
969
970                         opl3sa2_state[card].cfg_mpu.io_base       = mpu_io;
971                         opl3sa2_state[card].cfg_mpu.irq           = irq;
972                         opl3sa2_state[card].cfg_mpu.dma           = -1;
973                         opl3sa2_state[card].cfg_mpu.always_detect = 1; /* Use shared IRQs */
974
975                         /* Call me paranoid: */
976                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
977                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
978                         opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
979                 }
980
981                 /* FIXME: leak */
982                 if (probe_opl3sa2(&opl3sa2_state[card].cfg, card))
983                         return -ENODEV;
984
985                 base = opl3sa2_state[card].cfg_mss.io_base;
986
987                 if (!request_region(base, 4, "WSS config"))
988                         goto failed;
989
990                 ports = request_region(base + 4, 4, "ad1848");
991                 if (!ports)
992                         goto failed2;
993
994                 if (!probe_ms_sound(&opl3sa2_state[card].cfg_mss, ports)) {
995                         /*
996                          * If one or more cards are already registered, don't
997                          * return an error but print a warning.  Note, this
998                          * should never really happen unless the hardware or
999                          * ISA PnP screwed up.
1000                          */
1001                         release_region(base + 4, 4);
1002                 failed2:
1003                         release_region(base, 4);
1004                 failed:
1005                         release_region(opl3sa2_state[card].cfg.io_base, 2);
1006
1007                         if (opl3sa2_cards_num) {
1008                                 printk(KERN_WARNING
1009                                        PFX "There was a problem probing one "
1010                                        " of the ISA PNP cards, continuing\n");
1011                                 opl3sa2_cards_num--;
1012                                 continue;
1013                         } else
1014                                 return -ENODEV;
1015                 }
1016
1017                 attach_opl3sa2(&opl3sa2_state[card].cfg, card);
1018                 conf_printf(opl3sa2_state[card].chipset_name, &opl3sa2_state[card].cfg);
1019                 attach_opl3sa2_mixer(&opl3sa2_state[card].cfg, card);
1020                 attach_opl3sa2_mss(&opl3sa2_state[card].cfg_mss, ports);
1021
1022                 /* ewww =) */
1023                 opl3sa2_state[card].card = card;
1024 #ifdef CONFIG_PM_LEGACY
1025                 /* register our power management capabilities */
1026                 opl3sa2_state[card].pmdev = pm_register(PM_ISA_DEV, card, opl3sa2_pm_callback);
1027                 if (opl3sa2_state[card].pmdev)
1028                         opl3sa2_state[card].pmdev->data = &opl3sa2_state[card];
1029 #endif /* CONFIG_PM_LEGACY */
1030
1031                 /*
1032                  * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
1033                  * it's supported.
1034                  */
1035                 if (ymode != -1) {
1036                         if (opl3sa2_state[card].chipset == CHIPSET_OPL3SA2) {
1037                                 printk(KERN_ERR
1038                                        PFX "ymode not supported on OPL3-SA2\n");
1039                         }
1040                         else {
1041                                 opl3sa2_set_ymode(&opl3sa2_state[card].cfg, ymode);
1042                         }
1043                 }
1044
1045
1046                 /* Set A/D input to Mono loopback if asked to. */
1047                 if (loopback != -1) {
1048                         opl3sa2_set_loopback(&opl3sa2_state[card].cfg, loopback);
1049                 }
1050                 
1051                 /* Attach MPU if we've been asked to do so, failure isn't fatal */
1052                 if (opl3sa2_state[card].cfg_mpu.io_base != -1) {
1053                         int base = opl3sa2_state[card].cfg_mpu.io_base;
1054                         struct resource *ports;
1055                         ports = request_region(base, 2, "mpu401");
1056                         if (!ports)
1057                                 goto out;
1058                         if (!probe_mpu401(&opl3sa2_state[card].cfg_mpu, ports)) {
1059                                 release_region(base, 2);
1060                                 goto out;
1061                         }
1062                         if (attach_mpu401(&opl3sa2_state[card].cfg_mpu, THIS_MODULE)) {
1063                                 printk(KERN_ERR PFX "failed to attach MPU401\n");
1064                                 opl3sa2_state[card].cfg_mpu.slots[1] = -1;
1065                         }
1066                 }
1067         }
1068
1069 out:
1070         if (isapnp) {
1071                 printk(KERN_NOTICE PFX "%d PnP card(s) found.\n", opl3sa2_cards_num);
1072         }
1073
1074         return 0;
1075 }
1076
1077
1078 /*
1079  * Uninstall OPL3-SA2 based card(s).
1080  */
1081 static void __exit cleanup_opl3sa2(void)
1082 {
1083         int card;
1084
1085         for(card = 0; card < opl3sa2_cards_num; card++) {
1086 #ifdef CONFIG_PM_LEGACY
1087                 if (opl3sa2_state[card].pmdev)
1088                         pm_unregister(opl3sa2_state[card].pmdev);
1089 #endif
1090                 if (opl3sa2_state[card].cfg_mpu.slots[1] != -1) {
1091                         unload_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu);
1092                 }
1093                 unload_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
1094                 unload_opl3sa2(&opl3sa2_state[card].cfg, card);
1095 #ifdef CONFIG_PNP
1096                 pnp_unregister_driver(&opl3sa2_driver);
1097 #endif
1098         }
1099 }
1100
1101 module_init(init_opl3sa2);
1102 module_exit(cleanup_opl3sa2);
1103
1104 #ifndef MODULE
1105 static int __init setup_opl3sa2(char *str)
1106 {
1107         /* io, irq, dma, dma2,... */
1108 #ifdef CONFIG_PNP
1109         int ints[11];
1110 #else
1111         int ints[9];
1112 #endif
1113         str = get_options(str, ARRAY_SIZE(ints), ints);
1114         
1115         io       = ints[1];
1116         irq      = ints[2];
1117         dma      = ints[3];
1118         dma2     = ints[4];
1119         mss_io   = ints[5];
1120         mpu_io   = ints[6];
1121         ymode    = ints[7];
1122         loopback = ints[8];
1123 #ifdef CONFIG_PNP
1124         isapnp   = ints[9];
1125         multiple = ints[10];
1126 #endif
1127         return 1;
1128 }
1129
1130 __setup("opl3sa2=", setup_opl3sa2);
1131 #endif