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