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