xref: /illumos-gate/usr/src/uts/common/io/audio/ac97/ac97.c (revision 93a18d6d401e844455263f926578e9d2aa6b47ec)
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_AD1886:
1221 		/* jack sense */
1222 		WR(AC97_VENDOR_REGISTER_13,
1223 		    (RD(AC97_VENDOR_REGISTER_13) & ~0xEF) | 0x10);
1224 		break;
1225 
1226 	case AC97_CODEC_AD1888:
1227 		WR(AC97_VENDOR_REGISTER_15, 0xC420);
1228 #if 0
1229 		/* GED: This looks fishy to me, so I'm nuking it for now */
1230 		/* headphone/aux volume (?) */
1231 		ac_wr(ac, AC97_HEADPHONE_VOLUME_REGISTER,  0x0808);
1232 #endif
1233 		break;
1234 
1235 	case AC97_CODEC_AD1980:
1236 #if 0
1237 		/* set jacksense to mute line if headphone is plugged */
1238 		WR(AC97_VENDOR_REGISTER_13,
1239 		    (RD(AC97_VENDOR_REGISTER_13) & ~0xe00) | 0x400);
1240 #endif
1241 		WR(AC97_VENDOR_REGISTER_15, 0xC420);
1242 		break;
1243 
1244 	case AC97_CODEC_AD1985:
1245 		WR(AC97_VENDOR_REGISTER_15, 0xC420);
1246 		break;
1247 
1248 	case AC97_CODEC_WM9704:
1249 		/* enable I2S */
1250 		WR(AC97_VENDOR_REGISTER_01, RD(AC97_VENDOR_REGISTER_01) | 0x80);
1251 		break;
1252 
1253 	case AC97_CODEC_VT1612A:
1254 	case AC97_CODEC_VT1617A:
1255 	case AC97_CODEC_VT1616:
1256 		/* Turn on Center, Surround, and LFE DACs */
1257 		ac_clr(ac, AC97_EXTENDED_AUDIO_STAT_CTRL_REGISTER,
1258 		    EASCR_PRI | EASCR_PRJ | EASCR_PRK);
1259 		WR(AC97_VENDOR_REGISTER_01, 0x0230);
1260 		break;
1261 
1262 	case AC97_CODEC_YMF753:
1263 		/* set TX8 + 3AWE */
1264 		WR(AC97_VENDOR_REGISTER_07, RD(AC97_VENDOR_REGISTER_07) | 0x9);
1265 		break;
1266 
1267 	default:
1268 		break;
1269 	}
1270 
1271 	/* call codec specific reset hook */
1272 	if (ac->codec_reset != NULL) {
1273 		ac->codec_reset(ac);
1274 	}
1275 
1276 	/* Turn off variable sampling rate support */
1277 	ac_clr(ac, AC97_EXTENDED_AUDIO_STAT_CTRL_REGISTER, EASCR_VRA);
1278 }
1279 
1280 /*
1281  * This will reset and re-initialize the device.
1282  * It has two modes of operation that affect how it handles
1283  * all controls.
1284  *
1285  * It re-initializes the device and then can either reset
1286  * all controls back to their initial values or it can
1287  * re-load all controls with their last updated values.
1288  *
1289  * initval         - If this is none zero then all controls will
1290  *                   be restored to their initial values.
1291  */
1292 void
1293 ac97_reset(ac97_t *ac)
1294 {
1295 	/* If we are about to suspend so no point in going on */
1296 	mutex_enter(&ac->ac_lock);
1297 	if (ac->suspended) {
1298 		mutex_exit(&ac->ac_lock);
1299 		return;
1300 	}
1301 	ac_analog_reset(ac);
1302 	ac_hw_reset(ac);
1303 	ac_restore(ac);
1304 
1305 	mutex_exit(&ac->ac_lock);
1306 }
1307 
1308 /*
1309  * Given the need to resume the hardware this reloads the base hardware
1310  * and then takes the stored values for each control and sends them
1311  * to the hardware again.
1312  */
1313 void
1314 ac97_resume(ac97_t *ac)
1315 {
1316 
1317 	/*
1318 	 * This should only be called when already suspended.
1319 	 * this takes us out of suspend state after it brings the
1320 	 * controls back to life.
1321 	 */
1322 	ASSERT(ac->suspended);
1323 	mutex_enter(&ac->ac_lock);
1324 	ac->resumer = ddi_get_kt_did();
1325 
1326 	/* We simply call reset since the operation is the same */
1327 	ac_analog_reset(ac);
1328 	ac_hw_reset(ac);
1329 	ac_restore(ac);
1330 
1331 	ac->resumer = 0;
1332 	ac->suspended = B_FALSE;
1333 	mutex_exit(&ac->ac_lock);
1334 }
1335 
1336 /*
1337  * Return the number of channels supported by this codec.
1338  */
1339 int
1340 ac97_num_channels(ac97_t *ac)
1341 {
1342 	return (ac->nchan);
1343 }
1344 
1345 /*
1346  * Register a control -- if it fails, it will generate a message to
1347  * syslog, but the driver muddles on.  (Failure to register a control
1348  * should never occur, but is generally benign if it happens.)
1349  */
1350 void
1351 ac97_control_register(ac97_ctrl_t *ctrl)
1352 {
1353 	ac97_t	*ac = ctrl->actrl_ac97;
1354 	ASSERT(ac->d != NULL);
1355 
1356 	ctrl->actrl_suppress = B_FALSE;
1357 
1358 	/* Register control with framework */
1359 	ctrl->actrl_ctrl = audio_dev_add_control(ac->d, &ctrl->actrl_desc,
1360 	    ac_get_value, ac_set_value, ctrl);
1361 	if (ctrl->actrl_ctrl == NULL) {
1362 		audio_dev_warn(ac->d, "AC97 %s alloc failed",
1363 		    ctrl->actrl_name);
1364 	}
1365 }
1366 
1367 void
1368 ac97_control_unregister(ac97_ctrl_t *ctrl)
1369 {
1370 	ctrl->actrl_suppress = B_TRUE;
1371 
1372 	if (ctrl->actrl_ctrl != NULL) {
1373 		audio_dev_del_control(ctrl->actrl_ctrl);
1374 		ctrl->actrl_ctrl = NULL;
1375 	}
1376 }
1377 
1378 const char *
1379 ac97_control_name(ac97_ctrl_t *ctrl)
1380 {
1381 	return (ctrl->actrl_name);
1382 }
1383 
1384 const audio_ctrl_desc_t *
1385 ac97_control_desc(ac97_ctrl_t *ctrl)
1386 {
1387 	return (&ctrl->actrl_desc);
1388 }
1389 
1390 void
1391 ac97_register_controls(ac97_t *ac)
1392 {
1393 	ac97_ctrl_t	*ctrl;
1394 
1395 	for (ctrl = list_head(&ac->ctrls); ctrl;
1396 	    ctrl = list_next(&ac->ctrls, ctrl)) {
1397 		if (ctrl->actrl_suppress)
1398 			continue;
1399 		ac97_control_register(ctrl);
1400 	}
1401 }
1402 
1403 void
1404 ac97_walk_controls(ac97_t *ac, ac97_ctrl_walk_t walker, void *arg)
1405 {
1406 	ac97_ctrl_t	*ctrl;
1407 
1408 	for (ctrl = list_head(&ac->ctrls); ctrl;
1409 	    ctrl = list_next(&ac->ctrls, ctrl)) {
1410 		if (!(*walker)(ctrl, arg)) {
1411 			break;
1412 		}
1413 	}
1414 }
1415 
1416 void
1417 ac_add_control(ac97_t *ac, ac97_ctrl_probe_t *cpt)
1418 {
1419 	ac97_ctrl_t		*ctrl;
1420 	boolean_t		is_new;
1421 
1422 	ASSERT(ac);
1423 	ASSERT(ac->d);
1424 
1425 	ctrl = ac97_control_find(ac, cpt->cp_name);
1426 	if (ctrl != NULL) {
1427 		is_new = B_FALSE;
1428 	} else {
1429 		ctrl = kmem_zalloc(sizeof (ac97_ctrl_t), KM_SLEEP);
1430 		is_new = B_TRUE;
1431 	}
1432 	ctrl->actrl_ac97 = ac;
1433 	ctrl->actrl_minval = cpt->cp_minval;
1434 	ctrl->actrl_maxval = cpt->cp_maxval;
1435 	ctrl->actrl_type = cpt->cp_type;
1436 	ctrl->actrl_name = cpt->cp_name;
1437 	ctrl->actrl_flags = cpt->cp_flags;
1438 	if (cpt->cp_enum) {
1439 		for (int e = 0; e < 64; e++) {
1440 			if (cpt->cp_enum[e] == NULL)
1441 				break;
1442 			ctrl->actrl_enum[e] = cpt->cp_enum[e];
1443 		}
1444 	}
1445 
1446 	/*
1447 	 * Warning for extended controls this field gets changed
1448 	 * by audio_dev_add_control() to be a unique value.
1449 	 */
1450 	ctrl->actrl_initval = cpt->cp_initval;
1451 	ctrl->actrl_muteable = cpt->cp_muteable;
1452 	ctrl->actrl_write_fn = cpt->cp_write_fn;
1453 	ctrl->actrl_bits = cpt->cp_bits;
1454 
1455 	/*
1456 	 * Not that it can not be referenced until it is in the
1457 	 * list. So again by adding to the list last we avoid the need
1458 	 * for locks.
1459 	 */
1460 	if (is_new)
1461 		list_insert_tail(&ac->ctrls, ctrl);
1462 }
1463 
1464 /*
1465  * De-Register and free up a control
1466  */
1467 void
1468 ac97_control_remove(ac97_ctrl_t *ctrl)
1469 {
1470 	ac97_t	*ac = ctrl->actrl_ac97;
1471 
1472 	list_remove(&ac->ctrls, ctrl);
1473 
1474 	if (ctrl->actrl_ctrl != NULL)
1475 		audio_dev_del_control(ctrl->actrl_ctrl);
1476 	kmem_free(ctrl, sizeof (ac97_ctrl_t));
1477 }
1478 
1479 /*
1480  * This is the master list of all controls known and handled by
1481  * the AC97 framework. This is the list used to probe, allocate
1482  * and configure controls. If a control is not in this list it
1483  * will not be handled. If a control is in this list but does not
1484  * have a probe routine then it will always be included. If a
1485  * control in list has a probe routine then it must return true
1486  * for that control to be included.
1487  */
1488 
1489 #define	MONCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_MONITOR)
1490 #define	PLAYCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_PLAY)
1491 #define	RECCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_REC)
1492 #define	T3DCTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_3D)
1493 #define	TONECTL	(AC97_FLAGS | AUDIO_CTRL_FLAG_TONE)
1494 #define	MAINVOL	(PLAYCTL | AUDIO_CTRL_FLAG_MAINVOL)
1495 #define	PCMVOL	(PLAYCTL | AUDIO_CTRL_FLAG_PCMVOL)
1496 #define	RECVOL	(RECCTL | AUDIO_CTRL_FLAG_RECVOL)
1497 #define	MONVOL	(MONCTL | AUDIO_CTRL_FLAG_MONVOL)
1498 
1499 ac97_ctrl_probe_t	ctrl_probe_tbl[] = {
1500 
1501 	/* Master PCM Volume */
1502 	{AUDIO_CTRL_ID_VOLUME, INIT_VAL_MAIN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1503 	PCMVOL, PCMOVR_MUTE, ac97_master_set, NULL, 5},
1504 
1505 	/* LINE out volume */
1506 	{AUDIO_CTRL_ID_LINEOUT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1507 	MAINVOL, 0x8080, ac97_lineout_set, ac_probe_lineout, 6},
1508 
1509 	/* Front volume */
1510 	{AUDIO_CTRL_ID_FRONT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1511 	MAINVOL, 0x8080, ac97_lineout_set, ac_probe_front, 6},
1512 
1513 	/* 4CH out volume (has one of three possible uses, first use) */
1514 	{AUDIO_CTRL_ID_SURROUND, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1515 	MAINVOL, 0x8080, ac97_surround_set, ac_probe_rear, 6},
1516 
1517 	/* ALT out volume (has one of three possible uses, second use) */
1518 	{AUDIO_CTRL_ID_HEADPHONE, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1519 	MAINVOL, 0x8080, ac97_headphone_set, ac_probe_headphone, 6},
1520 
1521 	/* ALT out volume (has one of three possible uses, third use) */
1522 	{AUDIO_CTRL_ID_AUX1OUT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1523 	MAINVOL, 0x8080, ac97_aux1out_set, ac_probe_auxout, 6},
1524 
1525 	/* center out volume */
1526 	{AUDIO_CTRL_ID_CENTER, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1527 	MAINVOL, EXLFEVR_CENTER_MUTE, ac_center_set, ac_probe_center, 6},
1528 
1529 	/* LFE out volume (sub-woofer) */
1530 	{AUDIO_CTRL_ID_LFE, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1531 	MAINVOL, EXLFEVR_LFE_MUTE, ac_lfe_set, ac_probe_lfe, 6},
1532 
1533 	/* MONO out volume */
1534 	{AUDIO_CTRL_ID_SPEAKER, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1535 	MAINVOL, MMVR_MUTE, ac_speaker_set, ac_probe_mono, 6},
1536 
1537 	/* Record in GAIN */
1538 	{AUDIO_CTRL_ID_RECGAIN, INIT_IGAIN_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1539 	RECVOL, RGR_MUTE, ac_recgain_set, NULL, -4},
1540 
1541 	/* MIC in volume */
1542 	{AUDIO_CTRL_ID_MIC, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1543 	MONVOL, MICVR_MUTE, ac_mic_set, ac_probe_mic, 5},
1544 
1545 	/* LINE in volume */
1546 	{AUDIO_CTRL_ID_LINEIN, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1547 	MONVOL, LIVR_MUTE, ac_linein_set, ac_probe_linein, 5},
1548 
1549 	/* CD in volume */
1550 	{AUDIO_CTRL_ID_CD, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1551 	MONVOL, CDVR_MUTE, ac_cd_set, ac_probe_cdrom, 5},
1552 
1553 	/* VIDEO in volume */
1554 	{AUDIO_CTRL_ID_VIDEO, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1555 	MONVOL, VIDVR_MUTE, ac_video_set, ac_probe_video, 5},
1556 
1557 	/* AUX in volume */
1558 	{AUDIO_CTRL_ID_AUX1IN, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1559 	MONVOL, AUXVR_MUTE, ac_auxin_set, ac_probe_auxin, 5},
1560 
1561 	/* PHONE in volume */
1562 	{AUDIO_CTRL_ID_PHONE, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1563 	MONVOL, PVR_MUTE, ac_phone_set, ac_probe_phone, 5},
1564 
1565 	/* PC BEEPER in volume (motherboard speaker pins) */
1566 	{AUDIO_CTRL_ID_BEEP, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1567 	AC97_RW, PCBR_MUTE, ac_pcbeep_set, ac_probe_pcbeep, 4},
1568 
1569 	/* BASS out level (note, zero is hardware bypass) */
1570 	{AUDIO_CTRL_ID_BASS, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1571 	TONECTL, 0, ac_bass_set, ac_probe_tone, 4},
1572 
1573 	/* TREBLE out level (note, zero is hardware bypass) */
1574 	{AUDIO_CTRL_ID_TREBLE, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1575 	TONECTL, 0, ac_treble_set, ac_probe_tone, 4},
1576 
1577 	/* Loudness on/off switch */
1578 	{AUDIO_CTRL_ID_LOUDNESS, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1579 	TONECTL, 0, ac_loudness_set, ac_probe_loud, 0},
1580 
1581 	/* 3D depth out level */
1582 	{AUDIO_CTRL_ID_3DDEPTH, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1583 	T3DCTL, 0, ac_3ddepth_set, ac_probe_3d_depth, 4},
1584 
1585 	/* 3D center out level */
1586 	{AUDIO_CTRL_ID_3DCENT, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1587 	T3DCTL, 0, ac_3dcent_set, ac_probe_3d_center, 4},
1588 
1589 	/* 3D enhance on/off switch */
1590 	{AUDIO_CTRL_ID_3DENHANCE, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1591 	T3DCTL, 0, ac_3donoff_set, ac_probe_3d, 0},
1592 
1593 	/* MIC BOOST switch */
1594 	{AUDIO_CTRL_ID_MICBOOST, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1595 	RECCTL, 0, ac97_micboost_set, ac_probe_mic, 0},
1596 
1597 	/* Loopback on/off switch */
1598 	{AUDIO_CTRL_ID_LOOPBACK, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1599 	AC97_RW, 0, ac_loopback_set, NULL, 0},
1600 
1601 	/*
1602 	 * The following selectors *must* come after the others, as they rely
1603 	 * on the probe results of other controls.
1604 	 */
1605 	/* record src select  (only one port at a time) */
1606 	{AUDIO_CTRL_ID_RECSRC, (1U << INPUT_MIC), 0, 0, AUDIO_CTRL_TYPE_ENUM,
1607 	RECCTL, 0, ac_insrc_set, NULL, 0, ac_insrcs},
1608 
1609 	/* Start of non-standard private controls */
1610 
1611 	/* Simulated stereo on/off switch */
1612 	{AUDIO_CTRL_ID_STEREOSIM, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1613 	AC97_RW, 0, ac_stsim_set, ac_probe_stsim, 0},
1614 
1615 	/* mono MIC GAIN */
1616 	{AUDIO_CTRL_ID_MICGAIN, INIT_IGAIN_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1617 	RECCTL, RGMR_MUTE, ac_monomic_set, ac_probe_mmic, -4},
1618 
1619 	/* MIC select switch 0=mic1 1=mic2 */
1620 	{AUDIO_CTRL_ID_MICSRC, 1, 3, 3, AUDIO_CTRL_TYPE_ENUM,
1621 	RECCTL, 0, ac_selmic_set, ac_probe_mic, 0, ac_mics},
1622 
1623 	/* MONO out src select 0=mix 1=mic */
1624 	{AUDIO_CTRL_ID_SPKSRC, 1, 3, 3, AUDIO_CTRL_TYPE_ENUM,
1625 	AC97_RW, 0, ac_monosrc_set, ac_probe_mono, 0, ac_monos},
1626 
1627 	{NULL}
1628 };
1629 
1630 /*
1631  * Probe all possible controls and register existing
1632  * ones and set initial values
1633  *
1634  * Returns zero on success
1635  */
1636 static void
1637 ac_probeinit_ctrls(ac97_t *ac, int vol_bits, int enh_bits)
1638 {
1639 	ac97_ctrl_probe_t	*cpt;
1640 	ac97_ctrl_probe_t	my_cpt;
1641 
1642 	ASSERT(ac);
1643 
1644 	/*
1645 	 * Set some ports which are always present.
1646 	 */
1647 	ac->inputs = (1U << INPUT_STEREOMIX) | (1U << INPUT_MONOMIX);
1648 	for (cpt = &ctrl_probe_tbl[0]; cpt->cp_name != NULL; cpt++) {
1649 		bcopy(cpt, &my_cpt, sizeof (my_cpt));
1650 
1651 		if (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_RECSRC) == 0) {
1652 			my_cpt.cp_minval |= ac->inputs;
1653 			my_cpt.cp_maxval |= ac->inputs;
1654 		}
1655 
1656 		if (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_MICBOOST) == 0) {
1657 			if (ac->flags & AC97_FLAG_MICBOOST)
1658 				my_cpt.cp_initval = 1;
1659 		}
1660 
1661 		if ((strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_FRONT) == 0) ||
1662 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_HEADPHONE) == 0) ||
1663 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_SURROUND) == 0) ||
1664 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_SPEAKER) == 0)) {
1665 			my_cpt.cp_bits = vol_bits;
1666 		}
1667 
1668 		if ((strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_3DDEPTH) == 0) ||
1669 		    (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_3DCENT) == 0)) {
1670 			my_cpt.cp_bits = enh_bits;
1671 		}
1672 
1673 		if (!my_cpt.cp_probe || my_cpt.cp_probe(ac)) {
1674 			ac_add_control(ac, &my_cpt);
1675 		}
1676 	}
1677 
1678 	if (ac->codec_init != NULL) {
1679 		ac->codec_init(ac);
1680 	}
1681 }
1682 
1683 /*
1684  * Allocate an AC97 instance for use by a hardware driver.
1685  *
1686  * returns an allocated and initialize ac97 structure.
1687  */
1688 ac97_t *
1689 ac97_alloc(dev_info_t *dip, ac97_rd_t rd, ac97_wr_t wr, void *priv)
1690 {
1691 	ac97_t	*ac;
1692 
1693 	ac = kmem_zalloc(sizeof (ac97_t), KM_SLEEP);
1694 	ac->dip = dip;
1695 	ac->rd = rd;
1696 	ac->wr = wr;
1697 	ac->private = priv;
1698 
1699 	list_create(&ac->ctrls, sizeof (struct ac97_ctrl),
1700 	    offsetof(struct ac97_ctrl, actrl_linkage));
1701 
1702 	mutex_init(&ac->ac_lock, NULL, MUTEX_DRIVER, NULL);
1703 
1704 #define	PROP_FLAG(prop, flag, def)				    \
1705 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, \
1706 	    (prop), (def))) {					    \
1707 		ac->flags |= (flag);				    \
1708 	} else {						    \
1709 		ac->flags &= ~(flag);				    \
1710 	}
1711 
1712 	/*
1713 	 * Engage the external amplifier by default, suppress with
1714 	 * a property of the form "ac97-amplifier=0".
1715 	 */
1716 	PROP_FLAG(AC97_PROP_AMPLIFIER, AC97_FLAG_AMPLIFIER, 1);
1717 
1718 	/*
1719 	 * We cannot necessarily know if the headphone jack is present
1720 	 * or not.  There's a technique to probe the codec for
1721 	 * headphone support, but many vendors seem to simply hang the
1722 	 * headphone jack on the line out circuit, and have some kind
1723 	 * of jack sense detection to enable or disable it by default.
1724 	 * None of this is visible in the AC'97 registers.
1725 	 *
1726 	 * We cannot do much about it, but what we can do is offer users
1727 	 * a way to suppress the option for a headphone port.  Users and
1728 	 * administrators can then set a flag in the driver.conf to suppress
1729 	 * the option from display.
1730 	 *
1731 	 * It turns out that this problem exists for other signals as
1732 	 * well.
1733 	 */
1734 	PROP_FLAG(AC97_PROP_NO_HEADPHONE, AC97_FLAG_NO_HEADPHONE, 0);
1735 	PROP_FLAG(AC97_PROP_NO_AUXOUT, AC97_FLAG_NO_AUXOUT, 0);
1736 	PROP_FLAG(AC97_PROP_NO_CDROM, AC97_FLAG_NO_CDROM, 0);
1737 	PROP_FLAG(AC97_PROP_NO_AUXIN, AC97_FLAG_NO_AUXIN, 0);
1738 	PROP_FLAG(AC97_PROP_NO_VIDEO, AC97_FLAG_NO_VIDEO, 0);
1739 	PROP_FLAG(AC97_PROP_NO_LINEIN, AC97_FLAG_NO_LINEIN, 0);
1740 	PROP_FLAG(AC97_PROP_NO_MIC, AC97_FLAG_NO_MIC, 0);
1741 
1742 	/*
1743 	 * Most SPARC systems use the AC97 monoaural output for the
1744 	 * built-in speaker.  On these systems, we want to expose and
1745 	 * enable the built-in speaker by default.
1746 	 *
1747 	 * On most x86 systems, the mono output is not connected to
1748 	 * anything -- the AC'97 spec makes it pretty clear that the
1749 	 * output was actually intended for use with speaker phones.
1750 	 * So on those systems, we really don't want to activate the
1751 	 * speaker -- we don't even want to expose it's presence
1752 	 * normally.
1753 	 *
1754 	 * However, there could be an exception to the rule here.  To
1755 	 * facilitate this, we allow for the presence of the property
1756 	 * to indicate that the speaker should be exposed.  Whether it
1757 	 * is enabled by default or not depends on the value of the
1758 	 * property.  (Generally on SPARC, we enable by default.  On
1759 	 * other systems we do not.)
1760 	 */
1761 #ifdef	__sparc
1762 	ac->flags |= AC97_FLAG_SPEAKER_OK;
1763 	PROP_FLAG(AC97_PROP_SPEAKER, AC97_FLAG_SPEAKER, 1);
1764 #else
1765 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1766 	    AC97_PROP_SPEAKER)) {
1767 		ac->flags |= AC97_FLAG_SPEAKER_OK;
1768 		PROP_FLAG(AC97_PROP_SPEAKER, AC97_FLAG_SPEAKER, 0);
1769 	}
1770 #endif
1771 
1772 	/*
1773 	 * Enable microphone boost (20dB normally) by default?
1774 	 */
1775 	PROP_FLAG(AC97_PROP_MICBOOST, AC97_FLAG_MICBOOST, 0);
1776 
1777 	return (ac);
1778 }
1779 /*
1780  * Allocate an AC97 instance for use by a hardware driver.
1781  *
1782  * returns an allocated and initialize ac97 structure.
1783  */
1784 ac97_t *
1785 ac97_allocate(audio_dev_t *adev, dev_info_t *dip, ac97_rd_t rd, ac97_wr_t wr,
1786     void *priv)
1787 {
1788 	ac97_t *ac;
1789 
1790 	ac = ac97_alloc(dip, rd, wr, priv);
1791 	if (ac != NULL) {
1792 		ac->d = adev;
1793 	}
1794 	return (ac);
1795 }
1796 
1797 /*
1798  * Free an AC97 instance.
1799  */
1800 void
1801 ac97_free(ac97_t *ac)
1802 {
1803 	ac97_ctrl_t *ctrl;
1804 
1805 	/* Clear out any controls that are still attached */
1806 	while ((ctrl = list_head(&ac->ctrls)) != NULL) {
1807 		ac97_control_remove(ctrl);
1808 	}
1809 
1810 	list_destroy(&ac->ctrls);
1811 	mutex_destroy(&ac->ac_lock);
1812 	kmem_free(ac, sizeof (ac97_t));
1813 }
1814 
1815 static struct vendor {
1816 	unsigned	id;
1817 	const char	*name;
1818 } vendors[] = {
1819 	{ AC97_VENDOR_ADS,	"Analog Devices" },
1820 	{ AC97_VENDOR_AKM,	"Asahi Kasei" },
1821 	{ AC97_VENDOR_ALC,	"Realtek" },
1822 	{ AC97_VENDOR_ALG,	"Avance Logic" },
1823 	{ AC97_VENDOR_CMI,	"C-Media" },
1824 	{ AC97_VENDOR_CRY,	"Cirrus Logic" },
1825 	{ AC97_VENDOR_CXT,	"Conexant" },
1826 	{ AC97_VENDOR_EMC,	"eMicro" },
1827 	{ AC97_VENDOR_ESS,	"ESS Technology" },
1828 	{ AC97_VENDOR_EV,	"Ectiva" },
1829 	{ AC97_VENDOR_HRS,	"Intersil" },
1830 	{ AC97_VENDOR_ICE,	"ICEnsemble" },
1831 	{ AC97_VENDOR_ITE,	"ITE, Inc." },
1832 	{ AC97_VENDOR_NSC,	"National Semiconductor" },
1833 	{ AC97_VENDOR_PSC,	"Philips Semiconductor" },
1834 	{ AC97_VENDOR_SIL,	"Silicon Laboratories" },
1835 	{ AC97_VENDOR_ST,	"SigmaTel" },
1836 	{ AC97_VENDOR_TRA,	"TriTech", },
1837 	{ AC97_VENDOR_TXN,	"Texas Instruments", },
1838 	{ AC97_VENDOR_VIA,	"VIA Technologies" },
1839 	{ AC97_VENDOR_WML,	"Wolfson" },
1840 	{ AC97_VENDOR_YMH,	"Yamaha" },
1841 	{ 0, NULL },
1842 };
1843 
1844 static struct codec {
1845 	unsigned	id;
1846 	const char	*name;
1847 	int		enh_bits;
1848 	void		(*init)(ac97_t *ac);
1849 	void		(*reset)(ac97_t *ac);
1850 } codecs[] = {
1851 	{ AC97_CODEC_AK4540,	"AK4540" },
1852 	{ AC97_CODEC_STAC9700,	"STAC9700" },
1853 	{ AC97_CODEC_STAC9701,  "STAC9701" },
1854 	{ AC97_CODEC_STAC9701_2,	"STAC9701" },
1855 	{ AC97_CODEC_STAC9704,	"STAC9704" },
1856 	{ AC97_CODEC_STAC9705,	"STAC9705" },
1857 	{ AC97_CODEC_STAC9721,	"STAC9721" },
1858 	{ AC97_CODEC_STAC9708,	"STAC9708", 2 },
1859 	{ AC97_CODEC_STAC9744,	"STAC9744" },
1860 	{ AC97_CODEC_STAC9750,	"STAC9750", 3 },
1861 	{ AC97_CODEC_STAC9752,	"STAC9752", 3 },
1862 	{ AC97_CODEC_STAC9756,	"STAC9756", 3 },
1863 	{ AC97_CODEC_STAC9758,	"STAC9758", 3 },
1864 	{ AC97_CODEC_STAC9766,	"STAC9766", 3 },
1865 	{ AC97_CODEC_TR28028,	"TR28028" },
1866 	{ AC97_CODEC_TR28028_2,	"TR28028" },
1867 	{ AC97_CODEC_TR28023,	"TR28023" },
1868 	{ AC97_CODEC_TR28023_2,	"TR28023" },
1869 	{ AC97_CODEC_EM28028,	"EM28028" },
1870 	{ AC97_CODEC_CX20468,	"CX20468" },
1871 	{ AC97_CODEC_CX20468_2,	"CX20468" },
1872 	{ AC97_CODEC_CX20468_21,	"CX20468-21" },
1873 	{ AC97_CODEC_CS4297,	"CS4297" },
1874 	{ AC97_CODEC_CS4297A,	"CS4297A" },
1875 	{ AC97_CODEC_CS4294,	"CS4294" },
1876 	{ AC97_CODEC_CS4299,	"CS4299" },
1877 	{ AC97_CODEC_CS4202,	"CS4202" },
1878 	{ AC97_CODEC_CS4205,	"CS4205" },
1879 	{ AC97_CODEC_AD1819B,	"AD1819B" },
1880 	{ AC97_CODEC_AD1881,	"AD1881" },
1881 	{ AC97_CODEC_AD1881A,	"AD1881A" },
1882 	{ AC97_CODEC_AD1885,	"AD1885" },
1883 	{ AC97_CODEC_AD1886,	"AD1886" },
1884 	{ AC97_CODEC_AD1887,	"AD1887" },
1885 	{ AC97_CODEC_AD1888,	"AD1888" },
1886 	{ AC97_CODEC_AD1980,	"AD1980" },
1887 	{ AC97_CODEC_AD1981,	"AD1981" },	/* no data sheet */
1888 	{ AC97_CODEC_AD1981A,	"AD1981A", 0, ad1981a_init },
1889 	{ AC97_CODEC_AD1981B,	"AD1981B", 0, ad1981b_init },
1890 	{ AC97_CODEC_AD1985,	"AD1985" },
1891 	{ AC97_CODEC_WM9701A,	"WM9701A" },
1892 	{ AC97_CODEC_WM9703,	"WM9703" },
1893 	{ AC97_CODEC_WM9704,	"WM9704" },
1894 	{ AC97_CODEC_ES1921,	"ES1921" },
1895 	{ AC97_CODEC_ICE1232,	"ICE1232/VT1611A" },
1896 	{ AC97_CODEC_LM4550,	"LM4550" },
1897 	{ AC97_CODEC_VT1612A,	"VT1612A" },
1898 	{ AC97_CODEC_VT1616,	"VT1616" },
1899 	{ AC97_CODEC_VT1616A,	"VT1616A" },
1900 	{ AC97_CODEC_VT1617A,	"VT1617A" },
1901 	{ AC97_CODEC_VT1618,	"VT1618" },
1902 	{ AC97_CODEC_ALC100,	"ALC100", 2 },
1903 	{ AC97_CODEC_ALC200P,	"ALC200P", 2 },
1904 	{ AC97_CODEC_ALC202,	"ALC202", 2 },
1905 	{ AC97_CODEC_ALC203,	"ALC203", 2 },
1906 	{ AC97_CODEC_ALC250,	"ALC250", 2 },
1907 	{ AC97_CODEC_ALC250_2,	"ALC250", 2 },
1908 	{ AC97_CODEC_ALC650,	"ALC650", 2, alc650_init },
1909 	{ AC97_CODEC_ALC655,	"ALC655", 2, alc650_init },
1910 	{ AC97_CODEC_ALC658,	"ALC658", 2, alc650_init },
1911 	{ AC97_CODEC_ALC850,	"ALC850", 2, alc850_init },
1912 	{ AC97_CODEC_EV1938,	"EV1938" },
1913 	{ AC97_CODEC_CMI9738,	"CMI9738", 0, cmi9738_init },
1914 	{ AC97_CODEC_CMI9739,	"CMI9739", 0, cmi9739_init },
1915 	{ AC97_CODEC_CMI9780,	"CMI9780" },
1916 	{ AC97_CODEC_CMI9761,	"CMI9761A", 0, cmi9761_init },
1917 	{ AC97_CODEC_CMI9761_2,	"CMI9761B", 0, cmi9761_init },
1918 	{ AC97_CODEC_CMI9761_3,	"CMI9761A+", 0, cmi9761_init },
1919 	{ AC97_CODEC_YMF743,	"YMF743" },
1920 	{ AC97_CODEC_YMF753,	"YMF753" },
1921 	{ 0, NULL }
1922 };
1923 
1924 void
1925 ac97_probe_controls(ac97_t *ac)
1926 {
1927 	uint32_t		vid1, vid2;
1928 	uint16_t		ear;
1929 	const char		*name = NULL;
1930 	const char		*vendor = NULL;
1931 	int			enh_bits;
1932 	int			vol_bits;
1933 	uint32_t		flags;
1934 	char			nmbuf[128];
1935 	char			buf[128];
1936 
1937 	/* This is only valid when used with new style ac97_allocate(). */
1938 	ASSERT(ac->d);
1939 
1940 	ac_analog_reset(ac);
1941 
1942 	vid1 = RD(AC97_VENDOR_ID1_REGISTER);
1943 	vid2 = RD(AC97_VENDOR_ID2_REGISTER);
1944 
1945 	if (vid1 == 0xffff) {
1946 		audio_dev_warn(ac->d, "AC'97 codec unresponsive");
1947 		return;
1948 	}
1949 
1950 	ac->vid = (vid1 << 16) | vid2;
1951 
1952 	/*
1953 	 * Find out kind of codec we have and set any special case
1954 	 * settings needed.
1955 	 */
1956 	for (int i = 0; codecs[i].id; i++) {
1957 		if (ac->vid == codecs[i].id) {
1958 			name = codecs[i].name;
1959 			enh_bits = codecs[i].enh_bits;
1960 			ac->codec_init = codecs[i].init;
1961 			break;
1962 		}
1963 	}
1964 	for (int i = 0; vendors[i].id; i++) {
1965 		if ((ac->vid & 0xffffff00) == vendors[i].id) {
1966 			vendor = vendors[i].name;
1967 			break;
1968 		}
1969 	}
1970 	if (name == NULL) {
1971 		(void) snprintf(nmbuf, sizeof (nmbuf), "0x%04x%04x",
1972 		    vid1, vid2);
1973 		name = nmbuf;
1974 	}
1975 	if (vendor == NULL) {
1976 		vendor = "Unknown";
1977 	}
1978 
1979 	/*
1980 	 * Populate the initial shadow table.
1981 	 */
1982 	for (int i = 0; i < LAST_SHADOW_REG; i += sizeof (uint16_t)) {
1983 		SHADOW(ac, i) = RD(i);
1984 	}
1985 
1986 	ac->caps = RD(AC97_RESET_REGISTER);
1987 
1988 	enh_bits = 4;
1989 	vol_bits = 6;
1990 	flags = 0;
1991 
1992 	/* detect the bit width of the master volume controls */
1993 	WR(AC97_MASTER_VOLUME_REGISTER, 0x20);
1994 	if ((RD(AC97_MASTER_VOLUME_REGISTER) & 0x1f) == 0x1f) {
1995 		vol_bits = 5;
1996 	}
1997 
1998 	/*
1999 	 * AC'97 2.3 spec indicates three possible uses for AUX_OUT
2000 	 * (aka LNLVL_OUT aka HP_OUT).  We have to figure out which one
2001 	 * is in use.
2002 	 */
2003 	if (ac->caps & RR_HEADPHONE_SUPPORT) {
2004 		/* it looks like it is probably headphones */
2005 		if (ac_probe_reg(ac, AC97_HEADPHONE_VOLUME_REGISTER)) {
2006 			/* it is implemented */
2007 			ac->flags |= AC97_FLAG_AUX_HP;
2008 		}
2009 	}
2010 
2011 	/* Read EAR just once. */
2012 	ear = RD(AC97_EXTENDED_AUDIO_REGISTER);
2013 
2014 	/*
2015 	 * If not a headphone, is it 4CH_OUT (surround?)
2016 	 */
2017 	if ((!(ac->flags & AC97_FLAG_AUX_HP)) && (ear & EAR_SDAC)) {
2018 		if (ac_probe_reg(ac, AC97_EXTENDED_LRS_VOLUME_REGISTER)) {
2019 			ac->flags |= AC97_FLAG_AUX_4CH;
2020 		}
2021 	}
2022 
2023 	/*
2024 	 * If neither, then maybe its an auxiliary line level output?
2025 	 */
2026 	if (!(ac->flags & (AC97_FLAG_AUX_HP | AC97_FLAG_AUX_4CH))) {
2027 		if (ac_probe_reg(ac, AC97_HEADPHONE_VOLUME_REGISTER)) {
2028 			ac->flags |= AC97_FLAG_AUX_LVL;
2029 		}
2030 	}
2031 
2032 	/*
2033 	 * How many channels?
2034 	 */
2035 	ac->nchan = 2;
2036 	if (ear & EAR_SDAC) {
2037 		ac->nchan += 2;
2038 	}
2039 	if (ear & EAR_CDAC) {
2040 		ac->nchan++;
2041 	}
2042 	if (ear & EAR_LDAC) {
2043 		ac->nchan++;
2044 	}
2045 
2046 	ac->flags |= flags;
2047 	(void) snprintf(ac->name, sizeof (ac->name), "%s %s", vendor, name);
2048 
2049 	(void) snprintf(buf, sizeof (buf), "AC'97 codec: %s", ac->name);
2050 	audio_dev_add_info(ac->d, buf);
2051 
2052 	cmn_err(CE_CONT,
2053 	    "?%s#%d: AC'97 codec id %s (%x, %d channels, caps %x)\n",
2054 	    ddi_driver_name(ac->dip), ddi_get_instance(ac->dip),
2055 	    ac->name, ac->vid, ac->nchan, ac->caps);
2056 
2057 	/*
2058 	 * Probe and register all known controls with framework
2059 	 */
2060 	ac_probeinit_ctrls(ac, vol_bits, enh_bits);
2061 
2062 	ac_hw_reset(ac);
2063 	ac_init_values(ac);
2064 }
2065 
2066 /*
2067  * Init the actual hardware related to a previously allocated instance
2068  * of an AC97 device.  This is a legacy function and should not be
2069  * used in new code.
2070  *
2071  * Return zero on success.
2072  */
2073 int
2074 ac97_init(ac97_t *ac, struct audio_dev *d)
2075 {
2076 	/* Make sure we aren't using this with new style ac97_allocate(). */
2077 	ASSERT(ac->d == NULL);
2078 
2079 	/* Save audio framework instance structure */
2080 	ac->d = d;
2081 
2082 	ac97_probe_controls(ac);
2083 	ac97_register_controls(ac);
2084 
2085 	return (0);
2086 }
2087