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