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