xref: /titanic_44/usr/src/uts/common/io/audio/ac97/ac97.c (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (C) 4Front Technologies 1996-2008.
23  *
24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/list.h>
30 #include <sys/sysmacros.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/audio/audio_driver.h>
34 #include <sys/audio/ac97.h>
35 #include <sys/note.h>
36 #include "ac97_impl.h"
37 
38 /*
39  * This is the initial value for many controls. This is
40  * a 75% level.
41  */
42 #define	INIT_VAL_MAIN	75
43 #define	INIT_VAL_ST	((75 << 8) | 75)
44 #define	INIT_VAL_MN	75
45 #define	INIT_IGAIN_ST	((50 << 8) | 50)
46 #define	INIT_IGAIN_MN	50
47 
48 /*
49  * In AC'97 v2.3, the registers are carved up as follows:
50  *
51  * Audio Base Registers: 	0x00 - 0x26
52  * Audio Extended Registers:	0x28 - 0x3A
53  * Modem Extended Registers:	0x3C - 0x58
54  * Vendor Reserved Registers:	0x5A - 0x5F
55  * Page Registers:		0x60 - 0x6F
56  * Vendor Reserved Registers:	0x70 - 0x7A
57  * Vendor ID Registers:		0x7C - 0x7F
58  *
59  * We only need to shadow the normal audio registers by default.
60  * TBD: Handling of codec-specific registers in vendor reserved space.
61  * We cannot necessarily meaningfully shadow them.
62  */
63 #define	LAST_SHADOW_REG	0x3A
64 #define	NUM_SHADOW	((LAST_SHADOW_REG / sizeof (uint16_t)) + 1)
65 #define	SHADOW(ac, reg)	((ac)->shadow[((reg) / sizeof (uint16_t))])
66 
67 /*
68  * Record source selection.
69  */
70 #define	INPUT_MIC		0
71 #define	INPUT_CD		1
72 #define	INPUT_VIDEO		2
73 #define	INPUT_AUXIN		3
74 #define	INPUT_LINEIN		4
75 #define	INPUT_STEREOMIX		5
76 #define	INPUT_MONOMIX		6
77 #define	INPUT_PHONE		7
78 
79 static const char *ac_insrcs[] = {
80 	AUDIO_PORT_MIC,
81 	AUDIO_PORT_CD,
82 	AUDIO_PORT_VIDEO,
83 	AUDIO_PORT_AUX1IN,
84 	AUDIO_PORT_LINEIN,
85 	AUDIO_PORT_STEREOMIX,
86 	AUDIO_PORT_MONOMIX,
87 	AUDIO_PORT_PHONE,
88 	NULL,
89 };
90 
91 /*
92  * Per audio device state structure
93  */
94 struct ac97 {
95 	dev_info_t	*dip;	/* DDI device instance */
96 	audio_dev_t	*d;
97 	void		*private;  /* drivers devc */
98 	ac97_rd_t	rd;	/* drivers port read routine */
99 	ac97_wr_t	wr;	/* drivers port write routine */
100 	char		name[128]; /* driver instance name */
101 	uint8_t		nchan;
102 
103 	uint16_t	shadow[NUM_SHADOW];
104 
105 	uint32_t	flags;
106 #define	AC97_FLAG_AMPLIFIER	(1 << 0)	/* ext. amp on by default */
107 #define	AC97_FLAG_MICBOOST	(1 << 1)	/* micboost on by default */
108 #define	AC97_FLAG_SPEAKER	(1 << 2)	/* mono out on by default */
109 
110 #define	AC97_FLAG_AUX_HP	(1 << 4)	/* possible uses for AUX_OUT */
111 #define	AC97_FLAG_AUX_4CH	(1 << 5)
112 #define	AC97_FLAG_AUX_LVL	(1 << 6)
113 #define	AC97_FLAG_SPEAKER_OK	(1 << 7)	/* expose mono out */
114 #define	AC97_FLAG_NO_HEADPHONE	(1 << 8)	/* do not expose headphone */
115 #define	AC97_FLAG_NO_CDROM	(1 << 9)	/* do not expose CDROM */
116 #define	AC97_FLAG_NO_PHONE	(1 << 10)	/* do not expose phone in */
117 #define	AC97_FLAG_NO_VIDEO	(1 << 11)	/* do not expose video in */
118 #define	AC97_FLAG_NO_AUXIN	(1 << 12)	/* do not expose aux in */
119 #define	AC97_FLAG_NO_AUXOUT	(1 << 13)	/* do not expose aux out */
120 #define	AC97_FLAG_NO_LINEIN	(1 << 14)	/* do not expose linein */
121 #define	AC97_FLAG_NO_MIC	(1 << 15)	/* do not expose mic */
122 
123 	uint32_t	vid;			/* Vendor ID for CODEC */
124 	uint16_t	caps;
125 
126 	void		(*codec_init)(ac97_t *);
127 	void		(*codec_reset)(ac97_t *);
128 
129 	list_t		ctrls;
130 
131 	uint64_t	inputs;
132 
133 };
134 
135 struct modlmisc ac97_modlmisc = {
136 	&mod_miscops,
137 	"Audio Codec '97 Support"
138 };
139 
140 struct modlinkage ac97_modlinkage = {
141 	MODREV_1,
142 	{ &ac97_modlmisc, NULL }
143 };
144 
145 int
146 _init(void)
147 {
148 	return (mod_install(&ac97_modlinkage));
149 }
150 
151 int
152 _fini(void)
153 {
154 	return (mod_install(&ac97_modlinkage));
155 }
156 
157 int
158 _info(struct modinfo *modinfop)
159 {
160 	return (mod_info(&ac97_modlinkage, modinfop));
161 }
162 
163 
164 #if 0
165 /*
166  * The following table, and the code to scale it, works in percentages.
167  * This may be convenient for humans, but it would be faster if the table
168  * entries were rescaled to 256.  (Division by 100 is painful.  Divison by
169  * 256 is trivial.)
170  */
171 static const char ac97_val_cvt[101] = {
172 	0, 0, 3, 7, 10, 13, 16, 19,
173 	21, 23, 26, 28, 30, 32, 34, 35,
174 	37, 39, 40, 42,	43, 45, 46, 47,
175 	49, 50, 51, 52, 53, 55, 56, 57,
176 	58, 59, 60, 61, 62, 63, 64, 65,
177 	65, 66, 67, 68, 69, 70, 70, 71,
178 	72, 73, 73, 74, 75, 75, 76, 77,
179 	77, 78, 79, 79, 80, 81, 81, 82,
180 	82, 83, 84, 84, 85, 85, 86, 86,
181 	87, 87, 88, 88, 89, 89, 90, 90,
182 	91, 91, 92, 92, 93, 93, 94, 94,
183 	95, 95, 96, 96, 96, 97, 97, 98,
184 	98, 98, 99, 99, 100
185 };
186 #endif
187 
188 /*
189  * This code has three main functions. All related to converting
190  * a standard controls value to hardware specific values. All
191  * Standard passed in values are 0-100 as in percent.
192  *
193  * First it takes a value passed in as volume or gain and
194  * converts to attenuation or gain correspondingly. Since this is
195  * what the hardware needs.
196  *
197  * Second it adjusts the value passed in to compensate for the none
198  * linear nature of human hearing, sound loudness, sensitivity. It
199  * converts the linear value to a logarithmic value. This gives users
200  * the perception that the controls are linear.
201  *
202  * Third it converts the value to the number of bits that a hardware
203  * register needs to be.
204  *
205  * On input the following are supplied:
206  * left           - The gain or volume in percent for left channel.
207  * right          - The gain or volume in percent for right channel.
208  * bits           - The number of bits the hardware needs. If this value
209  *                  is negetive then right and left are gain else they
210  *                  are volume.
211  *
212  * On return the following is returned:
213  *
214  * bit: 15             8 7             0
215  *     ----------------------------------
216  *     | left channel   | right channel |
217  *     ----------------------------------
218  *      ( each channel is "bits" wide )
219  */
220 uint16_t
221 ac_val_scale(int left, int right, int bits)
222 {
223 	ASSERT(left <= 100);
224 	ASSERT(right <= 100);
225 
226 	if (bits < 0) {		/* This is gain not ATTN */
227 		left = 100 - left;
228 		right = 100 - right;
229 		bits = -bits;
230 	}
231 
232 #if 0
233 	/*
234 	 * 4Front's code used a table to smooth the transitions
235 	 * somewhat.  Without this change, the volume levels adjusted
236 	 * near the top of the table seem to have less effect.  Its
237 	 * hard to notice a volume change from 100 to 95, without the
238 	 * val_cvt table, for example.  However, the scaling has an
239 	 * ugly side effect, which is at the default volumes (75%), we
240 	 * wind up having the level set too high for some
241 	 * codec/amplifier combinations.
242 	 *
243 	 * Legacy Sun code didn't have this table, and some
244 	 * qualitative testing shows that it isn't really necessary.
245 	 */
246 	left = 100 - ac97_val_cvt[left];
247 	right = 100 - ac97_val_cvt[right];
248 #else
249 	left = 100 - left;
250 	right = 100 - right;
251 #endif
252 	return (((left * ((1 << bits) - 1) / 100) << 8) |
253 	    (right * ((1 << bits) - 1) / 100));
254 }
255 
256 uint16_t
257 ac_mono_scale(int val, int bits)
258 {
259 	ASSERT(val <= 100);
260 
261 	if (bits < 0) {		/* This is gain not ATTN */
262 		bits = -bits;
263 	} else {
264 		val = 100 - val;	/* convert to attenuation */
265 	}
266 	return (val * ((1 << bits) - 1) / 100);
267 }
268 
269 audio_dev_t *
270 ac_get_dev(ac97_t *ac)
271 {
272 	return (ac->d);
273 }
274 
275 int
276 ac_get_prop(ac97_t *ac, char *prop, int defval)
277 {
278 	int	rv;
279 
280 	rv = ddi_prop_get_int(DDI_DEV_T_ANY, ac->dip, DDI_PROP_DONTPASS,
281 	    prop, defval);
282 	return (rv);
283 }
284 
285 /*
286  * This calls the Hardware drivers access write routine
287  * to write to a device register.
288  */
289 #define	WR(r, v)	(ac)->wr((ac)->private, (r), (v))
290 #define	RD(r)		(ac)->rd((ac)->private, (r))
291 
292 /*
293  * Probe routines for optional controls
294  *
295  * These routines each probe one aspect of hardware
296  * for controls presents.
297  * If the control is present these routines should
298  * return none zero.
299  */
300 
301 /*
302  * Is the named register implemented?  This routine saves and
303  * restores the original value, and relies on the fact that the
304  * registers (if implemented) will have at least one bit that acts
305  * as a mute (0x8000, 0x8080), so we can probe "silently".
306  *
307  * The probe logic is suggested by the AC'97 2.3 spec.  (Unimplemented
308  * registers are required to return zero to facilitate this sort of
309  * detection.)
310  */
311 static int
312 ac_probe_reg(ac97_t *ac, uint8_t reg)
313 {
314 	uint16_t	val;
315 	int		rv = 0;
316 
317 	/* get the original value */
318 	val = RD(reg);
319 	WR(reg, 0xffff);
320 	if (RD(reg) != 0) {
321 		rv = 1;
322 	}
323 	/* restore the original value */
324 	WR(reg, val);
325 	return (rv);
326 }
327 
328 /*
329  * Does this device have bass/treble controls?
330  */
331 static int
332 ac_probe_tone(ac97_t *ac)
333 {
334 	/* Bass/Treble contols  present */
335 	if (ac->caps & RR_BASS_TREBLE)
336 		return (1);
337 	else
338 		return (0);
339 }
340 
341 /*
342  * If there is a loudness switch?
343  */
344 static int
345 ac_probe_loud(ac97_t *ac)
346 {
347 	/* loudness contol present */
348 	if (ac->caps & RR_LOUDNESS_SUPPORT)
349 		return (1);
350 	else
351 		return (0);
352 }
353 
354 /*
355  * Does this device have a mono-mic input volume control?
356  */
357 static int
358 ac_probe_mmic(ac97_t *ac)
359 {
360 	/* mono mic present */
361 	if (ac->caps & RR_DEDICATED_MIC)
362 		return (1);
363 	else
364 		return (0);
365 }
366 
367 /*
368  * Does this device have a simulated stereo switch?
369  */
370 static int
371 ac_probe_stsim(ac97_t *ac)
372 {
373 	/* simulated stereocontol present */
374 	if (ac->caps & RR_PSEUDO_STEREO)
375 		return (1);
376 	else
377 		return (0);
378 }
379 
380 /*
381  * Does this device have a PC beeper input volume control?
382  */
383 static int
384 ac_probe_pcbeep(ac97_t *ac)
385 {
386 	return (ac_probe_reg(ac, AC97_PC_BEEP_REGISTER));
387 }
388 
389 /*
390  * Does this device have AUX output port volume control?
391  */
392 static int
393 ac_probe_rear(ac97_t *ac)
394 {
395 	if (ac->flags & AC97_FLAG_AUX_4CH)
396 		return (1);
397 	else
398 		return (0);
399 
400 }
401 
402 /*
403  * Does this device have a mic?
404  */
405 static int
406 ac_probe_mic(ac97_t *ac)
407 {
408 	if ((!(ac->flags & AC97_FLAG_NO_MIC)) &&
409 	    (ac_probe_reg(ac, AC97_MIC_VOLUME_REGISTER))) {
410 		ac->inputs |= (1U << INPUT_MIC);
411 		return (1);
412 	}
413 	return (0);
414 }
415 
416 /*
417  * If this device has an AUX output port is it used for headphones?
418  */
419 static int
420 ac_probe_headphone(ac97_t *ac)
421 {
422 	/* headphone control present */
423 	if ((ac->flags & AC97_FLAG_AUX_HP) &&
424 	    !(ac->flags & AC97_FLAG_NO_HEADPHONE)) {
425 		return (1);
426 	}
427 	return (0);
428 }
429 
430 /*
431  * Does this device have AUX output port volume control?
432  */
433 static int
434 ac_probe_auxout(ac97_t *ac)
435 {
436 	/* ALT PCM control present */
437 	if ((ac->flags & AC97_FLAG_AUX_LVL) &&
438 	    !(ac->flags & AC97_FLAG_NO_AUXOUT)) {
439 		return (1);
440 	}
441 	return (0);
442 }
443 
444 /*
445  * Does this device have an AUX input port volume control?
446  */
447 static int
448 ac_probe_auxin(ac97_t *ac)
449 {
450 	if ((!(ac->flags & AC97_FLAG_NO_AUXIN)) &&
451 	    (ac_probe_reg(ac, AC97_AUX_VOLUME_REGISTER))) {
452 		ac->inputs |= (1U << INPUT_AUXIN);
453 		return (1);
454 	}
455 	return (0);
456 }
457 
458 /*
459  * Does this device have a phone input port with a volume control?
460  */
461 static int
462 ac_probe_phone(ac97_t *ac)
463 {
464 	if ((!(ac->flags & AC97_FLAG_NO_PHONE)) &&
465 	    (ac_probe_reg(ac, AC97_PHONE_VOLUME_REGISTER))) {
466 		ac->inputs |= (1U << INPUT_PHONE);
467 		return (1);
468 	}
469 	return (0);
470 }
471 
472 /*
473  * Does this device have a mono output port with volume control?
474  */
475 static int
476 ac_probe_mono(ac97_t *ac)
477 {
478 	if (!(ac->flags & AC97_FLAG_SPEAKER_OK)) {
479 		return (0);
480 	}
481 	if (ac_probe_reg(ac, AC97_MONO_MASTER_VOLUME_REGISTER)) {
482 		return (1);
483 	}
484 	return (0);
485 }
486 
487 /*
488  * Does this device have a line input port with volume control?
489  */
490 static int
491 ac_probe_linein(ac97_t *ac)
492 {
493 	if ((!(ac->flags & AC97_FLAG_NO_LINEIN)) &&
494 	    (ac_probe_reg(ac, AC97_LINE_IN_VOLUME_REGISTER))) {
495 		ac->inputs |= (1U << INPUT_LINEIN);
496 		return (1);
497 	}
498 	return (0);
499 }
500 
501 /*
502  * Does this device have a cdrom input port with volume control?
503  */
504 static int
505 ac_probe_cdrom(ac97_t *ac)
506 {
507 	if ((!(ac->flags & AC97_FLAG_NO_CDROM)) &&
508 	    (ac_probe_reg(ac, AC97_CD_VOLUME_REGISTER))) {
509 		ac->inputs |= (1U << INPUT_CD);
510 		return (1);
511 	}
512 	return (0);
513 }
514 
515 /*
516  * Does this device have a video input port with volume control?
517  */
518 static int
519 ac_probe_video(ac97_t *ac)
520 {
521 	if ((!(ac->flags & AC97_FLAG_NO_VIDEO)) &&
522 	    (ac_probe_reg(ac, AC97_VIDEO_VOLUME_REGISTER))) {
523 		ac->inputs |= (1U << INPUT_VIDEO);
524 		return (1);
525 	}
526 	return (0);
527 }
528 
529 /*
530  * Does this device have a 3D sound enhancement?
531  */
532 static int
533 ac_probe_3d(ac97_t *ac)
534 {
535 	/* 3D control present */
536 	if (ac->caps & RR_3D_STEREO_ENHANCE_MASK)
537 		return (1);
538 	else
539 		return (0);
540 }
541 
542 static int
543 ac_probe_3d_impl(ac97_t *ac, uint16_t mask)
544 {
545 	int	rv = 0;
546 	uint16_t val;
547 
548 	if ((ac->caps & RR_3D_STEREO_ENHANCE_MASK) == 0)
549 		return (0);
550 
551 	/* get the original value */
552 	val = RD(AC97_THREE_D_CONTROL_REGISTER);
553 	WR(AC97_THREE_D_CONTROL_REGISTER, mask);
554 	if ((RD(AC97_THREE_D_CONTROL_REGISTER) & mask) != 0) {
555 		rv = 1;
556 	}
557 	/* restore the original value */
558 	WR(AC97_THREE_D_CONTROL_REGISTER, val);
559 	return (rv);
560 }
561 
562 static int
563 ac_probe_3d_depth(ac97_t *ac)
564 {
565 	return (ac_probe_3d_impl(ac, TDCR_DEPTH_MASK));
566 }
567 
568 static int
569 ac_probe_3d_center(ac97_t *ac)
570 {
571 	return (ac_probe_3d_impl(ac, TDCR_CENTER_MASK));
572 }
573 
574 /*
575  * Does this device have a center output port with volume control?
576  */
577 static int
578 ac_probe_center(ac97_t *ac)
579 {
580 	uint16_t val;
581 
582 	val = RD(AC97_EXTENDED_AUDIO_REGISTER);
583 
584 	/* center volume present */
585 	if (val & EAR_CDAC)
586 		return (1);
587 	else
588 		return (0);
589 }
590 
591 /*
592  * Does this device have a LFE (Sub-woofer) output port with
593  * a volume control?
594  */
595 static int
596 ac_probe_lfe(ac97_t *ac)
597 {
598 	uint16_t val;
599 
600 	val = RD(AC97_EXTENDED_AUDIO_REGISTER);
601 
602 	/* We have LFE control */
603 	if (val & EAR_LDAC)
604 		return (1);
605 	else
606 		return (0);
607 
608 }
609 
610 /*
611  * Are we a multichannel codec?
612  */
613 static int
614 ac_probe_front(ac97_t *ac)
615 {
616 	uint16_t val;
617 
618 	val = RD(AC97_EXTENDED_AUDIO_REGISTER);
619 
620 	/* Are any of the Surround, Center, or LFE dacs present? */
621 	if (val & (EAR_SDAC | EAR_CDAC | EAR_LDAC))
622 		return (1);
623 	else
624 		return (0);
625 }
626 
627 static int
628 ac_probe_lineout(ac97_t *ac)
629 {
630 	/* if not multichannel, then use "lineout" instead of "front" label */
631 	return (!ac_probe_front(ac));
632 }
633 
634 static const char *ac_mics[] = {
635 	AUDIO_PORT_MIC1,
636 	AUDIO_PORT_MIC2,
637 	NULL,
638 };
639 
640 static const char *ac_monos[] = {
641 	AUDIO_PORT_MONOMIX,
642 	AUDIO_PORT_MIC,
643 	NULL
644 };
645 
646 /*
647  * This calls the Hardware drivers access write routine
648  * to write to a device register.
649  */
650 void
651 ac_wr(ac97_t *ac, uint8_t reg, uint16_t val)
652 {
653 	if ((reg < LAST_SHADOW_REG) && (reg > 0)) {
654 		SHADOW(ac, reg) = val;
655 	}
656 
657 	ac->wr(ac->private, reg, val);
658 }
659 
660 /*
661  * This obtains the shadowed value of a register.  If the register is
662  * out of range, zero is returned.
663  *
664  * To read a hardware register, use the RD() macro above.
665  */
666 uint16_t
667 ac_rd(ac97_t *ac, uint8_t reg)
668 {
669 	if ((reg < LAST_SHADOW_REG) && (reg > 0)) {
670 		return (SHADOW(ac, reg));
671 	}
672 	return (ac->rd(ac->private, reg));
673 }
674 
675 /*
676  * This calls the hardware driver's access read/write routine
677  * to set bits in a device register.
678  */
679 void
680 ac_set(ac97_t *ac, uint8_t reg, uint16_t val)
681 {
682 	ac_wr(ac, reg, ac->rd(ac->private, reg) | val);
683 }
684 
685 /*
686  * This calls the hardware driver's access read/write routine
687  * to clear bits in a device register.
688  */
689 void
690 ac_clr(ac97_t *ac, uint8_t reg, uint16_t val)
691 {
692 	ac_wr(ac, reg, ac->rd(ac->private, reg) & ~val);
693 }
694 
695 /*
696  * Look for a control attached to this device based
697  * on its control number.
698  *
699  * If this control number is found the per controls state
700  * structure is returned.
701  */
702 ac97_ctrl_t *
703 ac97_control_find(ac97_t *ac, const char *name)
704 {
705 	ac97_ctrl_t *ctrl;
706 	list_t *l = &ac->ctrls;
707 
708 	/* Validate that ctrlnum is real and usable */
709 	for (ctrl = list_head(l); ctrl; ctrl = list_next(l, ctrl)) {
710 		if (strcmp(ctrl->actrl_name, name) == 0) {
711 			return (ctrl);
712 		}
713 	}
714 	return (NULL);
715 }
716 
717 /*
718  * This will update all the codec registers from the shadow table.
719  */
720 static void
721 ac_restore(ac97_t *ac)
722 {
723 	/*
724 	 * If we are restoring previous settings, just reload from the
725 	 * shadowed settings.
726 	 */
727 	for (int i = 2; i < LAST_SHADOW_REG; i += sizeof (uint16_t)) {
728 		ac->wr(ac->private, i, SHADOW(ac, i));
729 	}
730 }
731 
732 /*
733  * This will update all the hardware controls to the initial values at
734  * start of day.
735  */
736 static void
737 ac_init_values(ac97_t *ac)
738 {
739 	ac97_ctrl_t	*ctrl;
740 
741 	for (ctrl = list_head(&ac->ctrls); ctrl;
742 	    ctrl = list_next(&ac->ctrls, ctrl)) {
743 		ctrl->actrl_value = ctrl->actrl_initval;
744 		ctrl->actrl_write_fn(ctrl, ctrl->actrl_initval);
745 	}
746 }
747 
748 /*
749  * Select the input source for recording. This is the set routine
750  * for the control AUDIO_CONTROL_INPUTS.
751  */
752 static void
753 ac_insrc_set(ac97_ctrl_t *ctrl, uint64_t value)
754 {
755 	ac97_t		*ac = ctrl->actrl_ac97;
756 	uint16_t	set_val;
757 
758 	set_val = ddi_ffs(value & 0xffff);
759 	if ((set_val > 0) && (set_val <= 8)) {
760 		set_val--;
761 		ac_wr(ac, AC97_RECORD_SELECT_CTRL_REGISTER,
762 		    set_val | (set_val << 8));
763 	}
764 }
765 
766 static void
767 ac_gpr_toggle(ac97_ctrl_t *ctrl, int bit, uint64_t onoff)
768 {
769 	ac97_t			*ac = ctrl->actrl_ac97;
770 	uint16_t		v;
771 
772 	v = SHADOW(ac, AC97_GENERAL_PURPOSE_REGISTER);
773 	if (onoff) {
774 		v |= bit;
775 	} else {
776 		v &= ~bit;
777 	}
778 	ac_wr(ac, AC97_GENERAL_PURPOSE_REGISTER, v);
779 }
780 
781 static void
782 ac_3donoff_set(ac97_ctrl_t *ctrl, uint64_t value)
783 {
784 	ac_gpr_toggle(ctrl, GPR_3D_STEREO_ENHANCE, value);
785 }
786 
787 static void
788 ac_loudness_set(ac97_ctrl_t *ctrl, uint64_t value)
789 {
790 	ac_gpr_toggle(ctrl, GPR_BASS_BOOST, value);
791 }
792 
793 static void
794 ac_loopback_set(ac97_ctrl_t *ctrl, uint64_t value)
795 {
796 	ac_gpr_toggle(ctrl, GPR_LPBK, value);
797 }
798 
799 /*
800  * This will set simulated stereo control to on or off.
801  */
802 static void
803 ac_stsim_set(ac97_ctrl_t *ctrl, uint64_t value)
804 {
805 	ac_gpr_toggle(ctrl, GPR_ST, value);
806 }
807 
808 /*
809  * This will set mic select control to mic1=0 or mic2=1.
810  */
811 static void
812 ac_selmic_set(ac97_ctrl_t *ctrl, uint64_t value)
813 {
814 	ac_gpr_toggle(ctrl, GPR_MS_MIC2, value & 2);
815 }
816 
817 /*
818  * This will set mono source select control to mix=0 or mic=1.
819  */
820 static void
821 ac_monosrc_set(ac97_ctrl_t *ctrl, uint64_t value)
822 {
823 	ac_gpr_toggle(ctrl, GPR_MONO_MIC_IN, value & 2);
824 }
825 
826 static void
827 ac_stereo_set(ac97_ctrl_t *ctrl, uint64_t value, uint8_t reg)
828 {
829 	ac97_t			*ac = ctrl->actrl_ac97;
830 	uint8_t			left, right;
831 	uint16_t		mute;
832 
833 	left = (value >> 8) & 0xff;
834 	right = value & 0xff;
835 	mute = value ? 0 : ctrl->actrl_muteable;
836 
837 	ac_wr(ac, reg, ac_val_scale(left, right, ctrl->actrl_bits) | mute);
838 }
839 
840 static void
841 ac_mono_set(ac97_ctrl_t *ctrl, uint64_t value, uint8_t reg, int shift)
842 {
843 	ac97_t			*ac = ctrl->actrl_ac97;
844 	uint8_t			val;
845 	uint16_t		mute, v;
846 	uint16_t		mask;
847 
848 	val = value & 0xff;
849 	mute = val ? 0 : ctrl->actrl_muteable;
850 
851 	mask = ctrl->actrl_muteable |
852 	    (((1 << ABS(ctrl->actrl_bits)) - 1) << shift);
853 
854 	v = SHADOW(ac, reg);
855 	v &= ~mask;	/* clear all of our bits, preserve others */
856 
857 	/* now set the mute bit, and volume bits */
858 	v |= mute;
859 	v |= (ac_mono_scale(val, ctrl->actrl_bits) << shift);
860 
861 	ac_wr(ac, reg, v);
862 }
863 
864 static void
865 ac97_master_set(ac97_ctrl_t *ctrl, uint64_t value)
866 {
867 	value = value | (value << 8);
868 	ac_stereo_set(ctrl, value, AC97_PCM_OUT_VOLUME_REGISTER);
869 }
870 
871 static void
872 ac97_lineout_set(ac97_ctrl_t *ctrl, uint64_t value)
873 {
874 	ac_stereo_set(ctrl, value, AC97_MASTER_VOLUME_REGISTER);
875 }
876 
877 static void
878 ac97_surround_set(ac97_ctrl_t *ctrl, uint64_t value)
879 {
880 	ac_stereo_set(ctrl, value, AC97_EXTENDED_LRS_VOLUME_REGISTER);
881 }
882 
883 static void
884 ac97_aux1out_set(ac97_ctrl_t *ctrl, uint64_t value)
885 {
886 	ac_stereo_set(ctrl, value, AC97_HEADPHONE_VOLUME_REGISTER);
887 }
888 
889 static void
890 ac97_headphone_set(ac97_ctrl_t *ctrl, uint64_t value)
891 {
892 	ac_stereo_set(ctrl, value, AC97_HEADPHONE_VOLUME_REGISTER);
893 }
894 
895 static void
896 ac_cd_set(ac97_ctrl_t *ctrl, uint64_t value)
897 {
898 	ac_stereo_set(ctrl, value, AC97_CD_VOLUME_REGISTER);
899 }
900 
901 static void
902 ac_video_set(ac97_ctrl_t *ctrl, uint64_t value)
903 {
904 	ac_stereo_set(ctrl, value, AC97_VIDEO_VOLUME_REGISTER);
905 }
906 
907 static void
908 ac_auxin_set(ac97_ctrl_t *ctrl, uint64_t value)
909 {
910 	ac_stereo_set(ctrl, value, AC97_AUX_VOLUME_REGISTER);
911 }
912 
913 static void
914 ac_linein_set(ac97_ctrl_t *ctrl, uint64_t value)
915 {
916 	ac_stereo_set(ctrl, value, AC97_LINE_IN_VOLUME_REGISTER);
917 }
918 
919 /*
920  * This will set mono mic gain control.
921  */
922 static void
923 ac_monomic_set(ac97_ctrl_t *ctrl, uint64_t value)
924 {
925 	ac_mono_set(ctrl, value, AC97_RECORD_GAIN_MIC_REGISTER, 0);
926 }
927 
928 static void
929 ac_phone_set(ac97_ctrl_t *ctrl, uint64_t value)
930 {
931 	ac_mono_set(ctrl, value, AC97_PHONE_VOLUME_REGISTER, 0);
932 }
933 
934 static void
935 ac_mic_set(ac97_ctrl_t *ctrl, uint64_t value)
936 {
937 	ac_mono_set(ctrl, value, AC97_MIC_VOLUME_REGISTER, 0);
938 }
939 
940 static void
941 ac_speaker_set(ac97_ctrl_t *ctrl, uint64_t value)
942 {
943 	ac_mono_set(ctrl, value, AC97_MONO_MASTER_VOLUME_REGISTER, 0);
944 }
945 
946 static void
947 ac_pcbeep_set(ac97_ctrl_t *ctrl, uint64_t value)
948 {
949 	ac_mono_set(ctrl, value, AC97_PC_BEEP_REGISTER, 1);
950 }
951 
952 static void
953 ac_recgain_set(ac97_ctrl_t *ctrl, uint64_t value)
954 {
955 	ac_stereo_set(ctrl, value, AC97_RECORD_GAIN_REGISTER);
956 }
957 
958 static void
959 ac_center_set(ac97_ctrl_t *ctrl, uint64_t value)
960 {
961 	ac_mono_set(ctrl, value, AC97_EXTENDED_C_LFE_VOLUME_REGISTER, 0);
962 }
963 
964 static void
965 ac_lfe_set(ac97_ctrl_t *ctrl, uint64_t value)
966 {
967 	ac_mono_set(ctrl, value, AC97_EXTENDED_C_LFE_VOLUME_REGISTER, 8);
968 }
969 
970 static void
971 ac_bass_set(ac97_ctrl_t *ctrl, uint64_t value)
972 {
973 	ac_mono_set(ctrl, value, AC97_MASTER_TONE_CONTROL_REGISTER, 8);
974 }
975 
976 static void
977 ac_treble_set(ac97_ctrl_t *ctrl, uint64_t value)
978 {
979 	ac_mono_set(ctrl, value, AC97_MASTER_TONE_CONTROL_REGISTER, 0);
980 }
981 
982 static void
983 ac_3ddepth_set(ac97_ctrl_t *ctrl, uint64_t value)
984 {
985 	/*
986 	 * XXX: This is all wrong... 3D depth/center cannot necessarily
987 	 * be scaled, because the technology in use may vary.  We
988 	 * need more information about each of the options available
989 	 * to do the right thing.
990 	 */
991 	ac_mono_set(ctrl, value, AC97_THREE_D_CONTROL_REGISTER, 0);
992 }
993 
994 static void
995 ac_3dcent_set(ac97_ctrl_t *ctrl, uint64_t value)
996 {
997 	/*
998 	 * XXX: This is all wrong... 3D depth/center cannot necessarily
999 	 * be scaled, because the technology in use may vary.  We
1000 	 * need more information about each of the options available
1001 	 * to do the right thing.
1002 	 */
1003 	ac_mono_set(ctrl, value, AC97_THREE_D_CONTROL_REGISTER, 8);
1004 }
1005 
1006 static void
1007 ac97_micboost_set(ac97_ctrl_t *ctrl, uint64_t value)
1008 {
1009 	ac97_t		*ac = ctrl->actrl_ac97;
1010 	uint16_t	v;
1011 
1012 	v = SHADOW(ac, AC97_MIC_VOLUME_REGISTER);
1013 	if (value) {
1014 		v |= MICVR_20dB_BOOST;
1015 	} else {
1016 		v &= ~MICVR_20dB_BOOST;
1017 	}
1018 	ac_wr(ac, AC97_MIC_VOLUME_REGISTER, v);
1019 }
1020 
1021 /*
1022  * This will return the stored value for any control that has been set.
1023  * Note this does not return the actual hardware value from a port. But
1024  * instead returns the cached value from the last write to the hardware
1025  * port.
1026  *
1027  * arg            - This control structure for this control.
1028  * value          - This is a pointer to the location to put the
1029  *                  controls value.
1030  *
1031  * On success zero is returned.
1032  */
1033 int
1034 ac97_control_get(ac97_ctrl_t *ctrl, uint64_t *value)
1035 {
1036 	*value = ctrl->actrl_value;
1037 
1038 	return (0);
1039 }
1040 
1041 int
1042 ac97_control_set(ac97_ctrl_t *ctrl, uint64_t value)
1043 {
1044 	uint8_t			v1, v2;
1045 
1046 	/* a bit of quick checking */
1047 	switch (ctrl->actrl_type) {
1048 	case AUDIO_CTRL_TYPE_STEREO:
1049 		v1 = (value >> 8) & 0xff;
1050 		v2 = value & 0xff;
1051 		if ((v1 < ctrl->actrl_minval) || (v1 > ctrl->actrl_maxval) ||
1052 		    (v2 < ctrl->actrl_minval) || (v2 > ctrl->actrl_maxval) ||
1053 		    (value > 0xffff)) {
1054 			return (EINVAL);
1055 		}
1056 		break;
1057 
1058 	case AUDIO_CTRL_TYPE_ENUM:
1059 		if ((value & ~ctrl->actrl_minval) !=
1060 		    (ctrl->actrl_maxval & ~ctrl->actrl_minval)) {
1061 			return (EINVAL);
1062 		}
1063 		break;
1064 
1065 	case AUDIO_CTRL_TYPE_MONO:
1066 	case AUDIO_CTRL_TYPE_BOOLEAN:
1067 		if ((value < ctrl->actrl_minval) ||
1068 		    (value > ctrl->actrl_maxval)) {
1069 			return (EINVAL);
1070 		}
1071 		break;
1072 	}
1073 
1074 	ctrl->actrl_value = value;
1075 	ctrl->actrl_write_fn(ctrl, value);
1076 
1077 	return (0);
1078 }
1079 
1080 static int
1081 ac_get_value(void *arg, uint64_t *value)
1082 {
1083 	return (ac97_control_get(arg, value));
1084 }
1085 
1086 static int
1087 ac_set_value(void *arg, uint64_t value)
1088 {
1089 	return (ac97_control_set(arg, value));
1090 }
1091 
1092 /*
1093  * Reset the analog codec hardware
1094  *
1095  * Reset all analog AC97 hardware, input ADC's, output DAC's and MIXER.
1096  * Wait a resonable amount of time for hardware to become ready.
1097  */
1098 static void
1099 ac_analog_reset(ac97_t *ac)
1100 {
1101 	uint16_t	tmp;
1102 	int		wait = 1000; /* delay for up to 1s */
1103 
1104 	/* Clear stale data and resync register accesses */
1105 	tmp = RD(AC97_POWERDOWN_CTRL_STAT_REGISTER);
1106 
1107 	/* reset the codec */
1108 	WR(AC97_RESET_REGISTER, 0);
1109 	tmp = RD(AC97_RESET_REGISTER);
1110 
1111 	/* power up */
1112 	WR(AC97_POWERDOWN_CTRL_STAT_REGISTER, 0);
1113 
1114 	/* Wait for ADC/DAC/MIXER to become ready */
1115 	while (wait--) {
1116 		/* 1 msec delay */
1117 		drv_usecwait(1000);
1118 
1119 		/* If all ready - end delay */
1120 		tmp = RD(AC97_POWERDOWN_CTRL_STAT_REGISTER);
1121 		SHADOW(ac, AC97_POWERDOWN_CTRL_STAT_REGISTER) = tmp;
1122 		if ((tmp & PCSR_POWERD_UP) == PCSR_POWERD_UP) {
1123 			return;
1124 		}
1125 	}
1126 
1127 	audio_dev_warn(ac->d, "AC'97 analog power up timed out");
1128 }
1129 
1130 /*
1131  * This is the internal hardware reset routine.
1132  * It has no locking and we must be locked before it is
1133  * called!
1134  *
1135  * This will reset and re-initialize the device.
1136  * It has two modes of operation that affect how it handles
1137  * all controls.
1138  *
1139  * It re-initializes the device and reloads values with
1140  * last updated versions.
1141  */
1142 static void
1143 ac_hw_reset(ac97_t *ac)
1144 {
1145 	/*
1146 	 * Fully Power up the device
1147 	 */
1148 	if (ac->flags & AC97_FLAG_AMPLIFIER) {
1149 		/* power up - external amp powerd up */
1150 		ac_wr(ac, AC97_POWERDOWN_CTRL_STAT_REGISTER, 0);
1151 	} else {
1152 		/* power up - external amp powered down */
1153 		ac_wr(ac, AC97_POWERDOWN_CTRL_STAT_REGISTER, PCSR_EAPD);
1154 	}
1155 
1156 	ac_wr(ac, AC97_GENERAL_PURPOSE_REGISTER, 0);
1157 
1158 	switch (ac->vid) {
1159 	case AC97_CODEC_STAC9708:
1160 #if 0
1161 		/* non-inverted phase */
1162 		/* ac_rd(ac, AC97_VENDOR_REGISTER_11) & ~0x8); */
1163 #endif
1164 		WR(AC97_VENDOR_REGISTER_11, 8);
1165 		break;
1166 
1167 	case AC97_CODEC_AD1886:
1168 		/* jack sense */
1169 		WR(AC97_VENDOR_REGISTER_13,
1170 		    (RD(AC97_VENDOR_REGISTER_13) & ~0xEF) | 0x10);
1171 		break;
1172 
1173 	case AC97_CODEC_AD1888:
1174 		WR(AC97_VENDOR_REGISTER_15, 0xC420);
1175 #if 0
1176 		/* GED: This looks fishy to me, so I'm nuking it for now */
1177 		/* headphone/aux volume (?) */
1178 		ac_wr(ac, AC97_HEADPHONE_VOLUME_REGISTER,  0x0808);
1179 #endif
1180 		break;
1181 
1182 	case AC97_CODEC_AD1980:
1183 #if 0
1184 		/* set jacksense to mute line if headphone is plugged */
1185 		WR(AC97_VENDOR_REGISTER_13,
1186 		    (RD(AC97_VENDOR_REGISTER_13) & ~0xe00) | 0x400);
1187 #endif
1188 		WR(AC97_VENDOR_REGISTER_15, 0xC420);
1189 		break;
1190 
1191 	case AC97_CODEC_AD1985:
1192 		WR(AC97_VENDOR_REGISTER_15, 0xC420);
1193 		break;
1194 
1195 	case AC97_CODEC_WM9704:
1196 		/* enable I2S */
1197 		WR(AC97_VENDOR_REGISTER_01, RD(AC97_VENDOR_REGISTER_01) | 0x80);
1198 		break;
1199 
1200 	case AC97_CODEC_VT1612A:
1201 	case AC97_CODEC_VT1617A:
1202 	case AC97_CODEC_VT1616:
1203 		/* Turn on Center, Surround, and LFE DACs */
1204 		ac_clr(ac, AC97_EXTENDED_AUDIO_STAT_CTRL_REGISTER,
1205 		    EASCR_PRI | EASCR_PRJ | EASCR_PRK);
1206 		WR(AC97_VENDOR_REGISTER_01, 0x0230);
1207 		break;
1208 
1209 	case AC97_CODEC_YMF753:
1210 		/* set TX8 + 3AWE */
1211 		WR(AC97_VENDOR_REGISTER_07, RD(AC97_VENDOR_REGISTER_07) | 0x9);
1212 		break;
1213 
1214 	default:
1215 		break;
1216 	}
1217 
1218 	/* call codec specific reset hook */
1219 	if (ac->codec_reset != NULL) {
1220 		ac->codec_reset(ac);
1221 	}
1222 
1223 	/* Turn off variable sampling rate support */
1224 	ac_clr(ac, AC97_EXTENDED_AUDIO_STAT_CTRL_REGISTER, EASCR_VRA);
1225 }
1226 
1227 /*
1228  * This will reset and re-initialize the device.  It is still incumbent
1229  * on the caller (or the audio framework) to replay control settings!
1230  */
1231 void
1232 ac97_reset(ac97_t *ac)
1233 {
1234 	ac_analog_reset(ac);
1235 	ac_hw_reset(ac);
1236 	ac_restore(ac);
1237 }
1238 
1239 /*
1240  * Return the number of channels supported by this codec.
1241  */
1242 int
1243 ac97_num_channels(ac97_t *ac)
1244 {
1245 	return (ac->nchan);
1246 }
1247 
1248 /*
1249  * Register a control -- if it fails, it will generate a message to
1250  * syslog, but the driver muddles on.  (Failure to register a control
1251  * should never occur, but is generally benign if it happens.)
1252  */
1253 void
1254 ac97_control_register(ac97_ctrl_t *ctrl)
1255 {
1256 	ac97_t	*ac = ctrl->actrl_ac97;
1257 	ASSERT(ac->d != NULL);
1258 
1259 	ctrl->actrl_suppress = B_FALSE;
1260 
1261 	/* Register control with framework */
1262 	ctrl->actrl_ctrl = audio_dev_add_control(ac->d, &ctrl->actrl_desc,
1263 	    ac_get_value, ac_set_value, ctrl);
1264 	if (ctrl->actrl_ctrl == NULL) {
1265 		audio_dev_warn(ac->d, "AC97 %s alloc failed",
1266 		    ctrl->actrl_name);
1267 	}
1268 }
1269 
1270 void
1271 ac97_control_unregister(ac97_ctrl_t *ctrl)
1272 {
1273 	ctrl->actrl_suppress = B_TRUE;
1274 
1275 	if (ctrl->actrl_ctrl != NULL) {
1276 		audio_dev_del_control(ctrl->actrl_ctrl);
1277 		ctrl->actrl_ctrl = NULL;
1278 	}
1279 }
1280 
1281 const char *
1282 ac97_control_name(ac97_ctrl_t *ctrl)
1283 {
1284 	return (ctrl->actrl_name);
1285 }
1286 
1287 const audio_ctrl_desc_t *
1288 ac97_control_desc(ac97_ctrl_t *ctrl)
1289 {
1290 	return (&ctrl->actrl_desc);
1291 }
1292 
1293 void
1294 ac97_register_controls(ac97_t *ac)
1295 {
1296 	ac97_ctrl_t	*ctrl;
1297 
1298 	for (ctrl = list_head(&ac->ctrls); ctrl;
1299 	    ctrl = list_next(&ac->ctrls, ctrl)) {
1300 		if (ctrl->actrl_suppress)
1301 			continue;
1302 		ac97_control_register(ctrl);
1303 	}
1304 }
1305 
1306 void
1307 ac97_walk_controls(ac97_t *ac, ac97_ctrl_walk_t walker, void *arg)
1308 {
1309 	ac97_ctrl_t	*ctrl;
1310 
1311 	for (ctrl = list_head(&ac->ctrls); ctrl;
1312 	    ctrl = list_next(&ac->ctrls, ctrl)) {
1313 		if (!(*walker)(ctrl, arg)) {
1314 			break;
1315 		}
1316 	}
1317 }
1318 
1319 void
1320 ac_add_control(ac97_t *ac, ac97_ctrl_probe_t *cpt)
1321 {
1322 	ac97_ctrl_t		*ctrl;
1323 	boolean_t		is_new;
1324 
1325 	ASSERT(ac);
1326 	ASSERT(ac->d);
1327 
1328 	ctrl = ac97_control_find(ac, cpt->cp_name);
1329 	if (ctrl != NULL) {
1330 		is_new = B_FALSE;
1331 	} else {
1332 		ctrl = kmem_zalloc(sizeof (ac97_ctrl_t), KM_SLEEP);
1333 		is_new = B_TRUE;
1334 	}
1335 	ctrl->actrl_ac97 = ac;
1336 	ctrl->actrl_minval = cpt->cp_minval;
1337 	ctrl->actrl_maxval = cpt->cp_maxval;
1338 	ctrl->actrl_type = cpt->cp_type;
1339 	ctrl->actrl_name = cpt->cp_name;
1340 	ctrl->actrl_flags = cpt->cp_flags;
1341 	if (cpt->cp_enum) {
1342 		for (int e = 0; e < 64; e++) {
1343 			if (cpt->cp_enum[e] == NULL)
1344 				break;
1345 			ctrl->actrl_enum[e] = cpt->cp_enum[e];
1346 		}
1347 	}
1348 
1349 	/*
1350 	 * Warning for extended controls this field gets changed
1351 	 * by audio_dev_add_control() to be a unique value.
1352 	 */
1353 	ctrl->actrl_initval = cpt->cp_initval;
1354 	ctrl->actrl_muteable = cpt->cp_muteable;
1355 	ctrl->actrl_write_fn = cpt->cp_write_fn;
1356 	ctrl->actrl_bits = cpt->cp_bits;
1357 
1358 	/*
1359 	 * Not that it can not be referenced until it is in the
1360 	 * list. So again by adding to the list last we avoid the need
1361 	 * for locks.
1362 	 */
1363 	if (is_new)
1364 		list_insert_tail(&ac->ctrls, ctrl);
1365 }
1366 
1367 /*
1368  * De-Register and free up a control
1369  */
1370 void
1371 ac97_control_remove(ac97_ctrl_t *ctrl)
1372 {
1373 	ac97_t	*ac = ctrl->actrl_ac97;
1374 
1375 	list_remove(&ac->ctrls, ctrl);
1376 
1377 	if (ctrl->actrl_ctrl != NULL)
1378 		audio_dev_del_control(ctrl->actrl_ctrl);
1379 	kmem_free(ctrl, sizeof (ac97_ctrl_t));
1380 }
1381 
1382 /*
1383  * This is the master list of all controls known and handled by
1384  * the AC97 framework. This is the list used to probe, allocate
1385  * and configure controls. If a control is not in this list it
1386  * will not be handled. If a control is in this list but does not
1387  * have a probe routine then it will always be included. If a
1388  * control in list has a probe routine then it must return true
1389  * for that control to be included.
1390  */
1391 
1392 #define	MONCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_MONITOR)
1393 #define	PLAYCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_PLAY)
1394 #define	RECCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_REC)
1395 #define	T3DCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_3D)
1396 #define	TONECTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_TONE)
1397 #define	MAINVOL	(PLAYCTL | AUDIO_CTRL_FLAG_MAINVOL)
1398 #define	PCMVOL	(PLAYCTL | AUDIO_CTRL_FLAG_PCMVOL)
1399 #define	RECVOL	(RECCTL | AUDIO_CTRL_FLAG_RECVOL)
1400 #define	MONVOL	(MONCTL | AUDIO_CTRL_FLAG_MONVOL)
1401 
1402 ac97_ctrl_probe_t	ctrl_probe_tbl[] = {
1403 
1404 	/* Master PCM Volume */
1405 	{AUDIO_CTRL_ID_VOLUME, INIT_VAL_MAIN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1406 	PCMVOL, PCMOVR_MUTE, ac97_master_set, NULL, 5},
1407 
1408 	/* LINE out volume */
1409 	{AUDIO_CTRL_ID_LINEOUT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1410 	MAINVOL, 0x8080, ac97_lineout_set, ac_probe_lineout, 6},
1411 
1412 	/* Front volume */
1413 	{AUDIO_CTRL_ID_FRONT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1414 	MAINVOL, 0x8080, ac97_lineout_set, ac_probe_front, 6},
1415 
1416 	/* 4CH out volume (has one of three possible uses, first use) */
1417 	{AUDIO_CTRL_ID_SURROUND, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1418 	MAINVOL, 0x8080, ac97_surround_set, ac_probe_rear, 6},
1419 
1420 	/* ALT out volume (has one of three possible uses, second use) */
1421 	{AUDIO_CTRL_ID_HEADPHONE, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1422 	MAINVOL, 0x8080, ac97_headphone_set, ac_probe_headphone, 6},
1423 
1424 	/* ALT out volume (has one of three possible uses, third use) */
1425 	{AUDIO_CTRL_ID_AUX1OUT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1426 	MAINVOL, 0x8080, ac97_aux1out_set, ac_probe_auxout, 6},
1427 
1428 	/* center out volume */
1429 	{AUDIO_CTRL_ID_CENTER, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1430 	MAINVOL, EXLFEVR_CENTER_MUTE, ac_center_set, ac_probe_center, 6},
1431 
1432 	/* LFE out volume (sub-woofer) */
1433 	{AUDIO_CTRL_ID_LFE, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1434 	MAINVOL, EXLFEVR_LFE_MUTE, ac_lfe_set, ac_probe_lfe, 6},
1435 
1436 	/* MONO out volume */
1437 	{AUDIO_CTRL_ID_SPEAKER, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1438 	MAINVOL, MMVR_MUTE, ac_speaker_set, ac_probe_mono, 6},
1439 
1440 	/* Record in GAIN */
1441 	{AUDIO_CTRL_ID_RECGAIN, INIT_IGAIN_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1442 	RECVOL, RGR_MUTE, ac_recgain_set, NULL, -4},
1443 
1444 	/* MIC in volume */
1445 	{AUDIO_CTRL_ID_MIC, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1446 	MONVOL, MICVR_MUTE, ac_mic_set, ac_probe_mic, 5},
1447 
1448 	/* LINE in volume */
1449 	{AUDIO_CTRL_ID_LINEIN, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1450 	MONVOL, LIVR_MUTE, ac_linein_set, ac_probe_linein, 5},
1451 
1452 	/* CD in volume */
1453 	{AUDIO_CTRL_ID_CD, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1454 	MONVOL, CDVR_MUTE, ac_cd_set, ac_probe_cdrom, 5},
1455 
1456 	/* VIDEO in volume */
1457 	{AUDIO_CTRL_ID_VIDEO, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1458 	MONVOL, VIDVR_MUTE, ac_video_set, ac_probe_video, 5},
1459 
1460 	/* AUX in volume */
1461 	{AUDIO_CTRL_ID_AUX1IN, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1462 	MONVOL, AUXVR_MUTE, ac_auxin_set, ac_probe_auxin, 5},
1463 
1464 	/* PHONE in volume */
1465 	{AUDIO_CTRL_ID_PHONE, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1466 	MONVOL, PVR_MUTE, ac_phone_set, ac_probe_phone, 5},
1467 
1468 	/* PC BEEPER in volume (motherboard speaker pins) */
1469 	{AUDIO_CTRL_ID_BEEP, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1470 	AC97_RW, PCBR_MUTE, ac_pcbeep_set, ac_probe_pcbeep, 4},
1471 
1472 	/* BASS out level (note, zero is hardware bypass) */
1473 	{AUDIO_CTRL_ID_BASS, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1474 	TONECTL, 0, ac_bass_set, ac_probe_tone, 4},
1475 
1476 	/* TREBLE out level (note, zero is hardware bypass) */
1477 	{AUDIO_CTRL_ID_TREBLE, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1478 	TONECTL, 0, ac_treble_set, ac_probe_tone, 4},
1479 
1480 	/* Loudness on/off switch */
1481 	{AUDIO_CTRL_ID_LOUDNESS, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1482 	TONECTL, 0, ac_loudness_set, ac_probe_loud, 0},
1483 
1484 	/* 3D depth out level */
1485 	{AUDIO_CTRL_ID_3DDEPTH, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1486 	T3DCTL, 0, ac_3ddepth_set, ac_probe_3d_depth, 4},
1487 
1488 	/* 3D center out level */
1489 	{AUDIO_CTRL_ID_3DCENT, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1490 	T3DCTL, 0, ac_3dcent_set, ac_probe_3d_center, 4},
1491 
1492 	/* 3D enhance on/off switch */
1493 	{AUDIO_CTRL_ID_3DENHANCE, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1494 	T3DCTL, 0, ac_3donoff_set, ac_probe_3d, 0},
1495 
1496 	/* MIC BOOST switch */
1497 	{AUDIO_CTRL_ID_MICBOOST, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1498 	RECCTL, 0, ac97_micboost_set, ac_probe_mic, 0},
1499 
1500 	/* Loopback on/off switch */
1501 	{AUDIO_CTRL_ID_LOOPBACK, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1502 	AC97_RW, 0, ac_loopback_set, NULL, 0},
1503 
1504 	/*
1505 	 * The following selectors *must* come after the others, as they rely
1506 	 * on the probe results of other controls.
1507 	 */
1508 	/* record src select  (only one port at a time) */
1509 	{AUDIO_CTRL_ID_RECSRC, (1U << INPUT_MIC), 0, 0, AUDIO_CTRL_TYPE_ENUM,
1510 	RECCTL, 0, ac_insrc_set, NULL, 0, ac_insrcs},
1511 
1512 	/* Start of non-standard private controls */
1513 
1514 	/* Simulated stereo on/off switch */
1515 	{AUDIO_CTRL_ID_STEREOSIM, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1516 	AC97_RW, 0, ac_stsim_set, ac_probe_stsim, 0},
1517 
1518 	/* mono MIC GAIN */
1519 	{AUDIO_CTRL_ID_MICGAIN, INIT_IGAIN_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1520 	RECCTL, RGMR_MUTE, ac_monomic_set, ac_probe_mmic, -4},
1521 
1522 	/* MIC select switch 0=mic1 1=mic2 */
1523 	{AUDIO_CTRL_ID_MICSRC, 1, 3, 3, AUDIO_CTRL_TYPE_ENUM,
1524 	RECCTL, 0, ac_selmic_set, ac_probe_mic, 0, ac_mics},
1525 
1526 	/* MONO out src select 0=mix 1=mic */
1527 	{AUDIO_CTRL_ID_SPKSRC, 1, 3, 3, AUDIO_CTRL_TYPE_ENUM,
1528 	AC97_RW, 0, ac_monosrc_set, ac_probe_mono, 0, ac_monos},
1529 
1530 	{NULL}
1531 };
1532 
1533 /*
1534  * Probe all possible controls and register existing
1535  * ones and set initial values
1536  *
1537  * Returns zero on success
1538  */
1539 static void
1540 ac_probeinit_ctrls(ac97_t *ac, int vol_bits, int enh_bits)
1541 {
1542 	ac97_ctrl_probe_t	*cpt;
1543 	ac97_ctrl_probe_t	my_cpt;
1544 
1545 	ASSERT(ac);
1546 
1547 	/*
1548 	 * Set some ports which are always present.
1549 	 */
1550 	ac->inputs = (1U << INPUT_STEREOMIX) | (1U << INPUT_MONOMIX);
1551 	for (cpt = &ctrl_probe_tbl[0]; cpt->cp_name != NULL; cpt++) {
1552 		bcopy(cpt, &my_cpt, sizeof (my_cpt));
1553 
1554 		if (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_RECSRC) == 0) {
1555 			my_cpt.cp_minval |= ac->inputs;
1556 			my_cpt.cp_maxval |= ac->inputs;
1557 		}
1558 
1559 		if (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_MICBOOST) == 0) {
1560 			if (ac->flags & AC97_FLAG_MICBOOST)
1561 				my_cpt.cp_initval = 1;
1562 		}
1563 
1564 		if ((strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_FRONT) == 0) ||
1565 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_HEADPHONE) == 0) ||
1566 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_SURROUND) == 0) ||
1567 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_SPEAKER) == 0)) {
1568 			my_cpt.cp_bits = vol_bits;
1569 		}
1570 
1571 		if ((strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_3DDEPTH) == 0) ||
1572 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_3DCENT) == 0)) {
1573 			my_cpt.cp_bits = enh_bits;
1574 		}
1575 
1576 		if (!my_cpt.cp_probe || my_cpt.cp_probe(ac)) {
1577 			ac_add_control(ac, &my_cpt);
1578 		}
1579 	}
1580 
1581 	if (ac->codec_init != NULL) {
1582 		ac->codec_init(ac);
1583 	}
1584 }
1585 
1586 /*
1587  * Allocate an AC97 instance for use by a hardware driver.
1588  *
1589  * returns an allocated and initialize ac97 structure.
1590  */
1591 ac97_t *
1592 ac97_alloc(dev_info_t *dip, ac97_rd_t rd, ac97_wr_t wr, void *priv)
1593 {
1594 	ac97_t	*ac;
1595 
1596 	ac = kmem_zalloc(sizeof (ac97_t), KM_SLEEP);
1597 	ac->dip = dip;
1598 	ac->rd = rd;
1599 	ac->wr = wr;
1600 	ac->private = priv;
1601 
1602 	list_create(&ac->ctrls, sizeof (struct ac97_ctrl),
1603 	    offsetof(struct ac97_ctrl, actrl_linkage));
1604 
1605 #define	PROP_FLAG(prop, flag, def)				    \
1606 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, \
1607 	    (prop), (def))) {					    \
1608 		ac->flags |= (flag);				    \
1609 	} else {						    \
1610 		ac->flags &= ~(flag);				    \
1611 	}
1612 
1613 	/*
1614 	 * Engage the external amplifier by default, suppress with
1615 	 * a property of the form "ac97-amplifier=0".
1616 	 */
1617 	PROP_FLAG(AC97_PROP_AMPLIFIER, AC97_FLAG_AMPLIFIER, 1);
1618 
1619 	/*
1620 	 * We cannot necessarily know if the headphone jack is present
1621 	 * or not.  There's a technique to probe the codec for
1622 	 * headphone support, but many vendors seem to simply hang the
1623 	 * headphone jack on the line out circuit, and have some kind
1624 	 * of jack sense detection to enable or disable it by default.
1625 	 * None of this is visible in the AC'97 registers.
1626 	 *
1627 	 * We cannot do much about it, but what we can do is offer users
1628 	 * a way to suppress the option for a headphone port.  Users and
1629 	 * administrators can then set a flag in the driver.conf to suppress
1630 	 * the option from display.
1631 	 *
1632 	 * It turns out that this problem exists for other signals as
1633 	 * well.
1634 	 */
1635 	PROP_FLAG(AC97_PROP_NO_HEADPHONE, AC97_FLAG_NO_HEADPHONE, 0);
1636 	PROP_FLAG(AC97_PROP_NO_AUXOUT, AC97_FLAG_NO_AUXOUT, 0);
1637 	PROP_FLAG(AC97_PROP_NO_CDROM, AC97_FLAG_NO_CDROM, 0);
1638 	PROP_FLAG(AC97_PROP_NO_AUXIN, AC97_FLAG_NO_AUXIN, 0);
1639 	PROP_FLAG(AC97_PROP_NO_VIDEO, AC97_FLAG_NO_VIDEO, 0);
1640 	PROP_FLAG(AC97_PROP_NO_LINEIN, AC97_FLAG_NO_LINEIN, 0);
1641 	PROP_FLAG(AC97_PROP_NO_MIC, AC97_FLAG_NO_MIC, 0);
1642 
1643 	/*
1644 	 * Most SPARC systems use the AC97 monoaural output for the
1645 	 * built-in speaker.  On these systems, we want to expose and
1646 	 * enable the built-in speaker by default.
1647 	 *
1648 	 * On most x86 systems, the mono output is not connected to
1649 	 * anything -- the AC'97 spec makes it pretty clear that the
1650 	 * output was actually intended for use with speaker phones.
1651 	 * So on those systems, we really don't want to activate the
1652 	 * speaker -- we don't even want to expose it's presence
1653 	 * normally.
1654 	 *
1655 	 * However, there could be an exception to the rule here.  To
1656 	 * facilitate this, we allow for the presence of the property
1657 	 * to indicate that the speaker should be exposed.  Whether it
1658 	 * is enabled by default or not depends on the value of the
1659 	 * property.  (Generally on SPARC, we enable by default.  On
1660 	 * other systems we do not.)
1661 	 */
1662 #ifdef	__sparc
1663 	ac->flags |= AC97_FLAG_SPEAKER_OK;
1664 	PROP_FLAG(AC97_PROP_SPEAKER, AC97_FLAG_SPEAKER, 1);
1665 #else
1666 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1667 	    AC97_PROP_SPEAKER)) {
1668 		ac->flags |= AC97_FLAG_SPEAKER_OK;
1669 		PROP_FLAG(AC97_PROP_SPEAKER, AC97_FLAG_SPEAKER, 0);
1670 	}
1671 #endif
1672 
1673 	/*
1674 	 * Enable microphone boost (20dB normally) by default?
1675 	 */
1676 	PROP_FLAG(AC97_PROP_MICBOOST, AC97_FLAG_MICBOOST, 0);
1677 
1678 	return (ac);
1679 }
1680 /*
1681  * Allocate an AC97 instance for use by a hardware driver.
1682  *
1683  * returns an allocated and initialize ac97 structure.
1684  */
1685 ac97_t *
1686 ac97_allocate(audio_dev_t *adev, dev_info_t *dip, ac97_rd_t rd, ac97_wr_t wr,
1687     void *priv)
1688 {
1689 	ac97_t *ac;
1690 
1691 	ac = ac97_alloc(dip, rd, wr, priv);
1692 	if (ac != NULL) {
1693 		ac->d = adev;
1694 	}
1695 	return (ac);
1696 }
1697 
1698 /*
1699  * Free an AC97 instance.
1700  */
1701 void
1702 ac97_free(ac97_t *ac)
1703 {
1704 	ac97_ctrl_t *ctrl;
1705 
1706 	/* Clear out any controls that are still attached */
1707 	while ((ctrl = list_head(&ac->ctrls)) != NULL) {
1708 		ac97_control_remove(ctrl);
1709 	}
1710 
1711 	list_destroy(&ac->ctrls);
1712 	kmem_free(ac, sizeof (ac97_t));
1713 }
1714 
1715 static struct vendor {
1716 	unsigned	id;
1717 	const char	*name;
1718 } vendors[] = {
1719 	{ AC97_VENDOR_ADS,	"Analog Devices" },
1720 	{ AC97_VENDOR_AKM,	"Asahi Kasei" },
1721 	{ AC97_VENDOR_ALC,	"Realtek" },
1722 	{ AC97_VENDOR_ALG,	"Avance Logic" },
1723 	{ AC97_VENDOR_CMI,	"C-Media" },
1724 	{ AC97_VENDOR_CRY,	"Cirrus Logic" },
1725 	{ AC97_VENDOR_CXT,	"Conexant" },
1726 	{ AC97_VENDOR_EMC,	"eMicro" },
1727 	{ AC97_VENDOR_ESS,	"ESS Technology" },
1728 	{ AC97_VENDOR_EV,	"Ectiva" },
1729 	{ AC97_VENDOR_HRS,	"Intersil" },
1730 	{ AC97_VENDOR_ICE,	"ICEnsemble" },
1731 	{ AC97_VENDOR_ITE,	"ITE, Inc." },
1732 	{ AC97_VENDOR_NSC,	"National Semiconductor" },
1733 	{ AC97_VENDOR_PSC,	"Philips Semiconductor" },
1734 	{ AC97_VENDOR_SIL,	"Silicon Laboratories" },
1735 	{ AC97_VENDOR_ST,	"SigmaTel" },
1736 	{ AC97_VENDOR_TRA,	"TriTech", },
1737 	{ AC97_VENDOR_TXN,	"Texas Instruments", },
1738 	{ AC97_VENDOR_VIA,	"VIA Technologies" },
1739 	{ AC97_VENDOR_WML,	"Wolfson" },
1740 	{ AC97_VENDOR_YMH,	"Yamaha" },
1741 	{ 0, NULL },
1742 };
1743 
1744 static struct codec {
1745 	unsigned	id;
1746 	const char	*name;
1747 	int		enh_bits;
1748 	void		(*init)(ac97_t *ac);
1749 	void		(*reset)(ac97_t *ac);
1750 } codecs[] = {
1751 	{ AC97_CODEC_AK4540,	"AK4540" },
1752 	{ AC97_CODEC_STAC9700,	"STAC9700" },
1753 	{ AC97_CODEC_STAC9701,  "STAC9701" },
1754 	{ AC97_CODEC_STAC9701_2,	"STAC9701" },
1755 	{ AC97_CODEC_STAC9704,	"STAC9704" },
1756 	{ AC97_CODEC_STAC9705,	"STAC9705" },
1757 	{ AC97_CODEC_STAC9721,	"STAC9721" },
1758 	{ AC97_CODEC_STAC9708,	"STAC9708", 2 },
1759 	{ AC97_CODEC_STAC9744,	"STAC9744" },
1760 	{ AC97_CODEC_STAC9750,	"STAC9750", 3 },
1761 	{ AC97_CODEC_STAC9752,	"STAC9752", 3 },
1762 	{ AC97_CODEC_STAC9756,	"STAC9756", 3 },
1763 	{ AC97_CODEC_STAC9758,	"STAC9758", 3 },
1764 	{ AC97_CODEC_STAC9766,	"STAC9766", 3 },
1765 	{ AC97_CODEC_TR28028,	"TR28028" },
1766 	{ AC97_CODEC_TR28028_2,	"TR28028" },
1767 	{ AC97_CODEC_TR28023,	"TR28023" },
1768 	{ AC97_CODEC_TR28023_2,	"TR28023" },
1769 	{ AC97_CODEC_EM28028,	"EM28028" },
1770 	{ AC97_CODEC_CX20468,	"CX20468" },
1771 	{ AC97_CODEC_CX20468_2,	"CX20468" },
1772 	{ AC97_CODEC_CX20468_21,	"CX20468-21" },
1773 	{ AC97_CODEC_CS4297,	"CS4297" },
1774 	{ AC97_CODEC_CS4297A,	"CS4297A" },
1775 	{ AC97_CODEC_CS4294,	"CS4294" },
1776 	{ AC97_CODEC_CS4299,	"CS4299" },
1777 	{ AC97_CODEC_CS4202,	"CS4202" },
1778 	{ AC97_CODEC_CS4205,	"CS4205" },
1779 	{ AC97_CODEC_AD1819B,	"AD1819B" },
1780 	{ AC97_CODEC_AD1881,	"AD1881" },
1781 	{ AC97_CODEC_AD1881A,	"AD1881A" },
1782 	{ AC97_CODEC_AD1885,	"AD1885" },
1783 	{ AC97_CODEC_AD1886,	"AD1886" },
1784 	{ AC97_CODEC_AD1887,	"AD1887" },
1785 	{ AC97_CODEC_AD1888,	"AD1888" },
1786 	{ AC97_CODEC_AD1980,	"AD1980" },
1787 	{ AC97_CODEC_AD1981,	"AD1981" },	/* no data sheet */
1788 	{ AC97_CODEC_AD1981A,	"AD1981A", 0, ad1981a_init },
1789 	{ AC97_CODEC_AD1981B,	"AD1981B", 0, ad1981b_init },
1790 	{ AC97_CODEC_AD1985,	"AD1985" },
1791 	{ AC97_CODEC_WM9701A,	"WM9701A" },
1792 	{ AC97_CODEC_WM9703,	"WM9703" },
1793 	{ AC97_CODEC_WM9704,	"WM9704" },
1794 	{ AC97_CODEC_ES1921,	"ES1921" },
1795 	{ AC97_CODEC_ICE1232,	"ICE1232/VT1611A" },
1796 	{ AC97_CODEC_LM4550,	"LM4550" },
1797 	{ AC97_CODEC_VT1612A,	"VT1612A" },
1798 	{ AC97_CODEC_VT1616,	"VT1616" },
1799 	{ AC97_CODEC_VT1616A,	"VT1616A" },
1800 	{ AC97_CODEC_VT1617A,	"VT1617A" },
1801 	{ AC97_CODEC_VT1618,	"VT1618" },
1802 	{ AC97_CODEC_ALC100,	"ALC100", 2 },
1803 	{ AC97_CODEC_ALC200P,	"ALC200P", 2 },
1804 	{ AC97_CODEC_ALC202,	"ALC202", 2 },
1805 	{ AC97_CODEC_ALC203,	"ALC203", 2 },
1806 	{ AC97_CODEC_ALC250,	"ALC250", 2 },
1807 	{ AC97_CODEC_ALC250_2,	"ALC250", 2 },
1808 	{ AC97_CODEC_ALC650,	"ALC650", 2, alc650_init },
1809 	{ AC97_CODEC_ALC655,	"ALC655", 2, alc650_init },
1810 	{ AC97_CODEC_ALC658,	"ALC658", 2, alc650_init },
1811 	{ AC97_CODEC_ALC850,	"ALC850", 2, alc850_init },
1812 	{ AC97_CODEC_EV1938,	"EV1938" },
1813 	{ AC97_CODEC_CMI9738,	"CMI9738", 0, cmi9738_init },
1814 	{ AC97_CODEC_CMI9739,	"CMI9739", 0, cmi9739_init },
1815 	{ AC97_CODEC_CMI9780,	"CMI9780" },
1816 	{ AC97_CODEC_CMI9761,	"CMI9761A", 0, cmi9761_init },
1817 	{ AC97_CODEC_CMI9761_2,	"CMI9761B", 0, cmi9761_init },
1818 	{ AC97_CODEC_CMI9761_3,	"CMI9761A+", 0, cmi9761_init },
1819 	{ AC97_CODEC_YMF743,	"YMF743" },
1820 	{ AC97_CODEC_YMF753,	"YMF753" },
1821 	{ 0, NULL }
1822 };
1823 
1824 void
1825 ac97_probe_controls(ac97_t *ac)
1826 {
1827 	uint32_t		vid1, vid2;
1828 	uint16_t		ear;
1829 	const char		*name = NULL;
1830 	const char		*vendor = NULL;
1831 	int			enh_bits;
1832 	int			vol_bits;
1833 	uint32_t		flags;
1834 	char			nmbuf[128];
1835 	char			buf[128];
1836 
1837 	/* This is only valid when used with new style ac97_allocate(). */
1838 	ASSERT(ac->d);
1839 
1840 	ac_analog_reset(ac);
1841 
1842 	vid1 = RD(AC97_VENDOR_ID1_REGISTER);
1843 	vid2 = RD(AC97_VENDOR_ID2_REGISTER);
1844 
1845 	if (vid1 == 0xffff) {
1846 		audio_dev_warn(ac->d, "AC'97 codec unresponsive");
1847 		return;
1848 	}
1849 
1850 	ac->vid = (vid1 << 16) | vid2;
1851 
1852 	/*
1853 	 * Find out kind of codec we have and set any special case
1854 	 * settings needed.
1855 	 */
1856 	for (int i = 0; codecs[i].id; i++) {
1857 		if (ac->vid == codecs[i].id) {
1858 			name = codecs[i].name;
1859 			enh_bits = codecs[i].enh_bits;
1860 			ac->codec_init = codecs[i].init;
1861 			break;
1862 		}
1863 	}
1864 	for (int i = 0; vendors[i].id; i++) {
1865 		if ((ac->vid & 0xffffff00) == vendors[i].id) {
1866 			vendor = vendors[i].name;
1867 			break;
1868 		}
1869 	}
1870 	if (name == NULL) {
1871 		(void) snprintf(nmbuf, sizeof (nmbuf), "0x%04x%04x",
1872 		    vid1, vid2);
1873 		name = nmbuf;
1874 	}
1875 	if (vendor == NULL) {
1876 		vendor = "Unknown";
1877 	}
1878 
1879 	/*
1880 	 * Populate the initial shadow table.
1881 	 */
1882 	for (int i = 0; i < LAST_SHADOW_REG; i += sizeof (uint16_t)) {
1883 		SHADOW(ac, i) = RD(i);
1884 	}
1885 
1886 	ac->caps = RD(AC97_RESET_REGISTER);
1887 
1888 	enh_bits = 4;
1889 	vol_bits = 6;
1890 	flags = 0;
1891 
1892 	/* detect the bit width of the master volume controls */
1893 	WR(AC97_MASTER_VOLUME_REGISTER, 0x20);
1894 	if ((RD(AC97_MASTER_VOLUME_REGISTER) & 0x1f) == 0x1f) {
1895 		vol_bits = 5;
1896 	}
1897 
1898 	/*
1899 	 * AC'97 2.3 spec indicates three possible uses for AUX_OUT
1900 	 * (aka LNLVL_OUT aka HP_OUT).  We have to figure out which one
1901 	 * is in use.
1902 	 */
1903 	if (ac->caps & RR_HEADPHONE_SUPPORT) {
1904 		/* it looks like it is probably headphones */
1905 		if (ac_probe_reg(ac, AC97_HEADPHONE_VOLUME_REGISTER)) {
1906 			/* it is implemented */
1907 			ac->flags |= AC97_FLAG_AUX_HP;
1908 		}
1909 	}
1910 
1911 	/* Read EAR just once. */
1912 	ear = RD(AC97_EXTENDED_AUDIO_REGISTER);
1913 
1914 	/*
1915 	 * If not a headphone, is it 4CH_OUT (surround?)
1916 	 */
1917 	if ((!(ac->flags & AC97_FLAG_AUX_HP)) && (ear & EAR_SDAC)) {
1918 		if (ac_probe_reg(ac, AC97_EXTENDED_LRS_VOLUME_REGISTER)) {
1919 			ac->flags |= AC97_FLAG_AUX_4CH;
1920 		}
1921 	}
1922 
1923 	/*
1924 	 * If neither, then maybe its an auxiliary line level output?
1925 	 */
1926 	if (!(ac->flags & (AC97_FLAG_AUX_HP | AC97_FLAG_AUX_4CH))) {
1927 		if (ac_probe_reg(ac, AC97_HEADPHONE_VOLUME_REGISTER)) {
1928 			ac->flags |= AC97_FLAG_AUX_LVL;
1929 		}
1930 	}
1931 
1932 	/*
1933 	 * How many channels?
1934 	 */
1935 	ac->nchan = 2;
1936 	if (ear & EAR_SDAC) {
1937 		ac->nchan += 2;
1938 	}
1939 	if (ear & EAR_CDAC) {
1940 		ac->nchan++;
1941 	}
1942 	if (ear & EAR_LDAC) {
1943 		ac->nchan++;
1944 	}
1945 
1946 	ac->flags |= flags;
1947 	(void) snprintf(ac->name, sizeof (ac->name), "%s %s", vendor, name);
1948 
1949 	(void) snprintf(buf, sizeof (buf), "AC'97 codec: %s", ac->name);
1950 	audio_dev_add_info(ac->d, buf);
1951 
1952 	cmn_err(CE_CONT,
1953 	    "?%s#%d: AC'97 codec id %s (%x, %d channels, caps %x)\n",
1954 	    ddi_driver_name(ac->dip), ddi_get_instance(ac->dip),
1955 	    ac->name, ac->vid, ac->nchan, ac->caps);
1956 
1957 	/*
1958 	 * Probe and register all known controls with framework
1959 	 */
1960 	ac_probeinit_ctrls(ac, vol_bits, enh_bits);
1961 
1962 	ac_hw_reset(ac);
1963 	ac_init_values(ac);
1964 }
1965 
1966 /*
1967  * Init the actual hardware related to a previously allocated instance
1968  * of an AC97 device.  This is a legacy function and should not be
1969  * used in new code.
1970  *
1971  * Return zero on success.
1972  */
1973 int
1974 ac97_init(ac97_t *ac, struct audio_dev *d)
1975 {
1976 	/* Make sure we aren't using this with new style ac97_allocate(). */
1977 	ASSERT(ac->d == NULL);
1978 
1979 	/* Save audio framework instance structure */
1980 	ac->d = d;
1981 
1982 	ac97_probe_controls(ac);
1983 	ac97_register_controls(ac);
1984 
1985 	return (0);
1986 }
1987