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