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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * AUDIO CONTROL Driver: usb_ac is a streams multiplexor that sits
30  * on top of usb_as and hid and is responsible for
31  * (1) providing the entry points to audio mixer framework, (2) passing
32  * streams messages to and from usb_as and hid and (3) processing
33  * control messages that it can handle.
34  *
35  * 1. Mixer entry points are: usb_ac_setup(), usb_ac_teardown(),
36  *	usb_ac_set_config(), usb_ac_set_format(), usb_ac_start_play(),
37  *	usb_ac_pause_play(), usb_ac_stop_play, usb_ac_start_record(),
38  *	usb_ac_stop_record().
39  * 2. usb_ac is a streams driver that passes streams messages down to
40  *	usb_as that selects the correct alternate with passed format
41  *	parameters, sets sample frequency, starts play/record, stops
42  *	play/record, pause play/record, open/close isoc pipe.
43  * 3. usb_ac handles the set_config command through the default pipe
44  *	of sound control interface of the audio device in a synchronous
45  *	manner.
46  *
47  * Serialization: usb_ac being a streams driver and having the requirement
48  * of making non-blockings calls (USBA or streams or mixer) needs to drop
49  * mutexes over such calls. But at the same time, a competing thread
50  * can't be allowed to interfere with (1) pipe, (2) streams state.
51  * So we need some kind of serialization among the asynchronous
52  * threads that can run in the driver. The serialization is mostly
53  * needed to avoid races among open/close/events/power entry points
54  * etc. Once a routine takes control, it checks if the resource (pipe or
55  * stream or dev state) is still accessible. If so, it proceeds with
56  * its job and until it completes, no other thread requiring the same
57  * resource can run.
58  *
59  * PM model in usb_ac: Raise power during attach. If a device is not at full
60  * power, raise power in the entry points. After the command is over,
61  * pm_idle_component() is called. The power is lowered in detach().
62  *
63  * locking: Warlock is not aware of the automatic locking mechanisms for
64  * streams drivers.
65  */
66 #include <sys/usb/usba/usbai_version.h>
67 #include <sys/usb/usba.h>
68 #include <sys/stropts.h>
69 #include <sys/sunndi.h>
70 #include <sys/ndi_impldefs.h>
71 #include <sys/strsubr.h>
72 
73 #include <sys/audio.h>
74 #include <sys/audiovar.h>
75 #include <sys/audio/audio_support.h>
76 #include <sys/audio/audio_src.h>
77 #include <sys/mixer.h>
78 #include <sys/audio/audio_mixer.h>
79 
80 #include <sys/usb/clients/audio/usb_audio.h>
81 #include <sys/usb/clients/audio/usb_mixer.h>
82 #include <sys/usb/clients/audio/usb_ac/usb_ac.h>
83 
84 /* debug support */
85 uint_t	usb_ac_errlevel 	= USB_LOG_L4;
86 uint_t	usb_ac_errmask		= (uint_t)-1;
87 uint_t	usb_ac_instance_debug	= (uint_t)-1;
88 
89 #ifdef DEBUG
90 /*
91  * tunable timeout for usb_as response, allow at least 10 secs for control
92  * cmd to timeout
93  */
94 int	usb_ac_wait_timeout = 10000000;
95 #endif
96 
97 /*
98  * table for converting term types of input and output terminals
99  * to SADA port types (pretty rough mapping)
100  */
101 static struct {
102 	ushort_t	term_type;
103 	ushort_t	port_type;
104 } usb_ac_term_type_map[] = {
105 { USB_AUDIO_TERM_TYPE_STREAMING,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
106 { USB_AUDIO_TERM_TYPE_MICROPHONE,		AUDIO_MICROPHONE },
107 { USB_AUDIO_TERM_TYPE_DT_MICROPHONE,		AUDIO_MICROPHONE },
108 { USB_AUDIO_TERM_TYPE_PERS_MICROPHONE,		AUDIO_MICROPHONE },
109 { USB_AUDIO_TERM_TYPE_OMNI_DIR_MICROPHONE,	AUDIO_MICROPHONE },
110 { USB_AUDIO_TERM_TYPE_MICROPHONE_ARRAY,		AUDIO_MICROPHONE },
111 { USB_AUDIO_TERM_TYPE_PROCESSING_MIC_ARRAY,	AUDIO_MICROPHONE },
112 { USB_AUDIO_TERM_TYPE_SPEAKER,			AUDIO_SPEAKER },
113 { USB_AUDIO_TERM_TYPE_HEADPHONES,		AUDIO_HEADPHONE },
114 { USB_AUDIO_TERM_TYPE_DISPLAY_AUDIO,		AUDIO_LINE_OUT },
115 { USB_AUDIO_TERM_TYPE_DT_SPEAKER,		AUDIO_SPEAKER },
116 { USB_AUDIO_TERM_TYPE_ROOM_SPEAKER,		AUDIO_SPEAKER },
117 { USB_AUDIO_TERM_TYPE_COMM_SPEAKER,		AUDIO_SPEAKER },
118 { USB_AUDIO_TERM_TYPE_LF_EFFECTS_SPEAKER,	AUDIO_SPEAKER },
119 { USB_AUDIO_TERM_TYPE_HANDSET,		AUDIO_LINE_IN|AUDIO_LINE_OUT },
120 { USB_AUDIO_TERM_TYPE_HEADSET,		AUDIO_LINE_IN|AUDIO_LINE_OUT },
121 { USB_AUDIO_TERM_TYPE_SPEAKERPHONE,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
122 { USB_AUDIO_TERM_TYPE_ECHO_SUPP_SPEAKERPHONE,
123 					AUDIO_LINE_IN|AUDIO_LINE_OUT },
124 { USB_AUDIO_TERM_TYPE_ECHO_CANCEL_SPEAKERPHONE,
125 					AUDIO_LINE_IN|AUDIO_LINE_OUT },
126 { USB_AUDIO_TERM_TYPE_PHONE_LINE,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
127 { USB_AUDIO_TERM_TYPE_TELEPHONE,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
128 { USB_AUDIO_TERM_TYPE_DOWN_LINE_PHONE,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
129 { USB_AUDIO_TERM_TYPE_ANALOG_CONNECTOR,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
130 { USB_AUDIO_TERM_TYPE_DIGITAL_AUDIO_IF,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
131 { USB_AUDIO_TERM_TYPE_LINE_CONNECTOR,	AUDIO_LINE_IN|AUDIO_LINE_OUT },
132 { USB_AUDIO_TERM_TYPE_LEGACY_AUDIO_CONNECTOR,
133 					AUDIO_LINE_IN|AUDIO_LINE_OUT },
134 { USB_AUDIO_TERM_TYPE_SPDIF_IF,		AUDIO_SPDIF_IN },
135 { USB_AUDIO_TERM_TYPE_1394_DA_STREAM,
136 					AUDIO_LINE_IN|AUDIO_LINE_OUT },
137 { USB_AUDIO_TERM_TYPE_1394_DV_STREAM_SNDTRCK,
138 					AUDIO_LINE_IN|AUDIO_LINE_OUT },
139 { 0, 0 }
140 };
141 
142 
143 /*
144  * Module linkage routines for the kernel
145  */
146 static int	usb_ac_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
147 static int	usb_ac_attach(dev_info_t *, ddi_attach_cmd_t);
148 static int	usb_ac_detach(dev_info_t *, ddi_detach_cmd_t);
149 static int	usb_ac_power(dev_info_t *, int, int);
150 
151 /*
152  * STREAMS module entry points
153  */
154 static int	usb_ac_open(queue_t *, dev_t *, int, int, cred_t *);
155 static int	usb_ac_close(queue_t *, int, cred_t *);
156 static int	usb_ac_uwput(queue_t *, mblk_t *);
157 static int	usb_ac_lrput(queue_t *, mblk_t *);
158 
159 /* plumbing */
160 static usb_ac_plumbed_t *usb_ac_get_plumb_info(usb_ac_state_t *, char *,
161 				uchar_t);
162 static usb_ac_plumbed_t *usb_ac_get_plumb_info_from_lrq(usb_ac_state_t *,
163 				queue_t *);
164 static uint_t	usb_ac_get_featureID(usb_ac_state_t *, uchar_t, uint_t,
165 				uint_t);
166 static void	usb_ac_plumb_ioctl(queue_t *, mblk_t *);
167 
168 
169 /* registration */
170 static int	usb_ac_get_curr_n_channels(usb_ac_state_t *, int);
171 static usb_audio_formats_t *usb_ac_get_curr_format(usb_ac_state_t *, int);
172 
173 /* descriptor handling */
174 static int	usb_ac_handle_descriptors(usb_ac_state_t *);
175 static void	usb_ac_add_unit_descriptor(usb_ac_state_t *, uchar_t *, size_t);
176 static void	usb_ac_alloc_unit(usb_ac_state_t *, uint_t);
177 static void	usb_ac_free_all_units(usb_ac_state_t *);
178 static void	usb_ac_setup_connections(usb_ac_state_t *);
179 static void	usb_ac_map_termtype_to_port(usb_ac_state_t *, uint_t);
180 
181 /* power management */
182 static int	usb_ac_pwrlvl0(usb_ac_state_t *);
183 static int	usb_ac_pwrlvl1(usb_ac_state_t *);
184 static int	usb_ac_pwrlvl2(usb_ac_state_t *);
185 static int	usb_ac_pwrlvl3(usb_ac_state_t *);
186 static void	usb_ac_create_pm_components(dev_info_t *, usb_ac_state_t *);
187 static void	usb_ac_pm_busy_component(usb_ac_state_t *);
188 static void	usb_ac_pm_idle_component(usb_ac_state_t *);
189 
190 /* event handling */
191 static int	usb_ac_disconnect_event_cb(dev_info_t *);
192 static int	usb_ac_reconnect_event_cb(dev_info_t *);
193 static int	usb_ac_cpr_suspend(dev_info_t *);
194 static void	usb_ac_cpr_resume(dev_info_t *);
195 
196 static usb_event_t usb_ac_events = {
197 	usb_ac_disconnect_event_cb,
198 	usb_ac_reconnect_event_cb,
199 	NULL, NULL
200 };
201 
202 /* misc. support */
203 static void	usb_ac_restore_device_state(dev_info_t *, usb_ac_state_t *);
204 static int	usb_ac_cleanup(dev_info_t *, usb_ac_state_t *);
205 static void	usb_ac_serialize_access(usb_ac_state_t *);
206 static void	usb_ac_release_access(usb_ac_state_t *);
207 
208 static void	usb_ac_push_unit_id(usb_ac_state_t *, uint_t);
209 static void	usb_ac_pop_unit_id(usb_ac_state_t *, uint_t);
210 static void	usb_ac_show_traverse_path(usb_ac_state_t *);
211 static int	usb_ac_check_path(usb_ac_state_t *, uint_t);
212 
213 static uint_t	usb_ac_traverse_connections(usb_ac_state_t *, uint_t, uint_t,
214 				uint_t, uint_t, uint_t, uint_t,
215 				uint_t *, uint_t, uint_t *,
216 				int (*func)(usb_ac_state_t *, uint_t, uint_t,
217 				uint_t, uint_t, uint_t, uint_t *));
218 static uint_t	usb_ac_set_port(usb_ac_state_t *, uint_t, uint_t);
219 static uint_t	usb_ac_set_control(usb_ac_state_t *, uint_t, uint_t,
220 				uint_t, uint_t, uint_t,
221 				uint_t *, uint_t,
222 				int (*func)(usb_ac_state_t *, uint_t, uint_t,
223 				uint_t, uint_t, uint_t, uint_t *));
224 static uint_t	usb_ac_set_monitor_gain_control(usb_ac_state_t *, uint_t,
225 				uint_t, uint_t, uint_t, uint_t,
226 				uint_t *, uint_t,
227 				int (*func)(usb_ac_state_t *, uint_t, uint_t,
228 				uint_t, uint_t, uint_t, uint_t *));
229 static uint_t	usb_ac_traverse_all_units(usb_ac_state_t *, uint_t, uint_t,
230 				uint_t, uint_t, uint_t, uint_t *,
231 				uint_t, uint_t *,
232 				int (*func)(usb_ac_state_t *, uint_t, uint_t,
233 				uint_t, uint_t, uint_t, uint_t *));
234 static int	usb_ac_update_port(usb_ac_state_t *, uint_t,
235 				uint_t, uint_t, uint_t, uint_t, uint_t *);
236 static int	usb_ac_set_selector(usb_ac_state_t *, uint_t,
237 				uint_t, uint_t, uint_t, uint_t, uint_t *);
238 static int	usb_ac_feature_unit_check(usb_ac_state_t *, uint_t,
239 				uint_t, uint_t, uint_t, uint_t, uint_t *);
240 static int	usb_ac_set_gain(usb_ac_state_t *, uint_t,
241 				uint_t, uint_t, uint_t, uint_t, uint_t *);
242 static int	usb_ac_set_monitor_gain(usb_ac_state_t *, uint_t,
243 				uint_t, uint_t, uint_t, uint_t, uint_t *);
244 static int	usb_ac_set_mute(usb_ac_state_t *, uint_t, uint_t,
245 				uint_t, uint_t, uint_t, uint_t *);
246 static int	usb_ac_set_volume(usb_ac_state_t *, uint_t, short, int dir,
247 				int);
248 static int	usb_ac_get_maxmin_volume(usb_ac_state_t *, uint_t, int, int,
249 				int);
250 static void	usb_ac_free_mblk(mblk_t *);
251 static mblk_t	*usb_ac_allocate_req_mblk(usb_ac_state_t *, int,
252 				void *, uint_t);
253 static int	usb_ac_send_as_cmd(usb_ac_state_t *, usb_ac_plumbed_t *,
254 				int, void *);
255 static int	usb_ac_send_format_cmd(audiohdl_t, int, int, int,
256 				int, int, int);
257 static int	usb_ac_do_setup(audiohdl_t, int, int);
258 static void	usb_ac_do_teardown(audiohdl_t, int, int);
259 static void	usb_ac_do_pause_play(audiohdl_t, int);
260 static void	usb_ac_do_stop_record(audiohdl_t, int);
261 
262 /*  Mixer entry points */
263 static int	usb_ac_setup(audiohdl_t, int, int);
264 static void	usb_ac_teardown(audiohdl_t, int, int);
265 static int	usb_ac_set_config(audiohdl_t, int, int, int, int, int);
266 static int	usb_ac_set_format(audiohdl_t, int, int, int, int, int, int);
267 static int	usb_ac_start_play(audiohdl_t, int);
268 static void	usb_ac_pause_play(audiohdl_t, int);
269 static void	usb_ac_stop_play(audiohdl_t, int);
270 static int	usb_ac_start_record(audiohdl_t, int);
271 static void	usb_ac_stop_record(audiohdl_t, int);
272 static int	usb_ac_restore_audio_state(usb_ac_state_t *, int);
273 
274 /*
275  * External functions
276  */
277 extern int	space_store(char *key, uintptr_t ptr);
278 extern void	space_free(char *);
279 
280 
281 /*
282  * mixer registration data
283  */
284 static am_ad_entry_t usb_ac_entry = {
285 	usb_ac_setup,		/* ad_setup() */
286 	usb_ac_teardown,	/* ad_teardown() */
287 	usb_ac_set_config,	/* ad_set_config() */
288 	usb_ac_set_format,	/* ad_set_format() */
289 	usb_ac_start_play,	/* ad_start_play() */
290 	usb_ac_pause_play,	/* ad_pause_play() */
291 	usb_ac_stop_play,	/* ad_stop_play() */
292 	usb_ac_start_record,	/* ad_start_record() */
293 	usb_ac_stop_record,	/* ad_stop_record() */
294 	NULL,			/* ad_ioctl() */
295 	NULL			/* ad_iocdata() */
296 };
297 
298 /* anchor for soft state structures */
299 static void	*usb_ac_statep;
300 
301 /* for passing soft state etc. to usb_ac_dacf module */
302 static usb_ac_state_space_t ssp;
303 
304 /* STREAMS driver id and limit value structure */
305 static struct module_info usb_ac_modinfo = {
306 	0xffff,				/* module ID number */
307 	"usb_ac",			/* module name */
308 	USB_AUDIO_MIN_PKTSZ,		/* minimum packet size */
309 	USB_AUDIO_MAX_PKTSZ,		/* maximum packet size */
310 	USB_AC_HIWATER,			/* high water mark */
311 	USB_AC_LOWATER			/* low water mark */
312 };
313 
314 /* STREAMS queue processing procedures structures */
315 /* upper read queue */
316 static struct qinit usb_ac_urqueue = {
317 	NULL,			/* put procedure */
318 	NULL,			/* service procedure */
319 	usb_ac_open,		/* open procedure */
320 	usb_ac_close,		/* close procedure */
321 	NULL,			/* unused */
322 	&usb_ac_modinfo,	/* module parameters */
323 	NULL			/* module statistics */
324 };
325 
326 /* upper write queue */
327 static struct qinit usb_ac_uwqueue = {
328 	usb_ac_uwput,		/* put procedure */
329 	audio_sup_wsvc,		/* service procedure */
330 	NULL,			/* open procedure */
331 	NULL,			/* close procedure */
332 	NULL,			/* unused */
333 	&usb_ac_modinfo,	/* module parameters */
334 	NULL			/* module statistics */
335 };
336 
337 /* lower read queue */
338 static struct qinit usb_ac_lrqueue = {
339 	usb_ac_lrput,
340 	NULL,
341 	NULL,
342 	NULL,
343 	NULL,
344 	&usb_ac_modinfo,	/* module parameters */
345 	NULL
346 };
347 
348 /* lower write queue */
349 static struct qinit usb_ac_lwqueue = {
350 	NULL,
351 	NULL,
352 	NULL,
353 	NULL,
354 	NULL,
355 	&usb_ac_modinfo,	/* module parameters */
356 	NULL
357 };
358 
359 /* STREAMS entity declaration structure */
360 static struct streamtab usb_ac_str_info = {
361 	&usb_ac_urqueue,		/* upper read queue */
362 	&usb_ac_uwqueue,		/* upper write queue */
363 	&usb_ac_lrqueue,		/* lower read queue */
364 	&usb_ac_lwqueue,		/* lower write queue */
365 };
366 
367 /*
368  * DDI Structures
369  *
370  * Entry points structure
371  */
372 static struct cb_ops usb_ac_cb_ops = {
373 	nulldev,		/* cb_open */
374 	nulldev,		/* cb_close */
375 	nodev,			/* cb_strategy */
376 	nodev,			/* cb_print */
377 	nodev,			/* cb_dump */
378 	nodev,			/* cb_read */
379 	nodev,			/* cb_write */
380 	nodev,			/* cb_ioctl */
381 	nodev,			/* cb_devmap */
382 	nodev,			/* cb_mmap */
383 	nodev,			/* cb_segmap */
384 	nochpoll,		/* cb_chpoll */
385 	ddi_prop_op,		/* cb_prop_op */
386 	&usb_ac_str_info,	/* cb_str */
387 	D_MP | D_MTPERQ,	/* cb_flag */
388 	CB_REV,			/* cb_rev */
389 	nodev,			/* cb_aread */
390 	nodev,			/* cb_arwite */
391 };
392 
393 /* Device operations structure */
394 static struct dev_ops usb_ac_dev_ops = {
395 	DEVO_REV,		/* devo_rev */
396 	0,			/* devo_refcnt */
397 	usb_ac_getinfo,		/* devo_getinfo */
398 	nulldev,		/* devo_identify - obsolete */
399 	nulldev,		/* devo_probe - not needed */
400 	usb_ac_attach,		/* devo_attach */
401 	usb_ac_detach,		/* devo_detach */
402 	nodev,			/* devo_reset */
403 	&usb_ac_cb_ops,		/* devi_cb_ops */
404 	NULL,			/* devo_busb_ac_ops */
405 	usb_ac_power		/* devo_power */
406 };
407 
408 /* Linkage structure for loadable drivers */
409 static struct modldrv usb_ac_modldrv = {
410 	&mod_driverops,				/* drv_modops */
411 	"USB Audio Control Driver %I%",		/* drv_linkinfo */
412 	&usb_ac_dev_ops				/* drv_dev_ops */
413 };
414 
415 /* Module linkage structure */
416 static struct modlinkage usb_ac_modlinkage = {
417 	MODREV_1,			/* ml_rev */
418 	(void *)&usb_ac_modldrv,	/* ml_linkage */
419 	NULL				/* NULL terminates the list */
420 };
421 
422 /* warlock directives */
423 _NOTE(SCHEME_PROTECTS_DATA("unique per call", iocblk))
424 _NOTE(SCHEME_PROTECTS_DATA("unique per call", datab))
425 _NOTE(SCHEME_PROTECTS_DATA("unique per call", msgb))
426 _NOTE(SCHEME_PROTECTS_DATA("unique per call", queue))
427 _NOTE(SCHEME_PROTECTS_DATA("stable data", usb_pipe_policy_t))
428 
429 /* standard entry points */
430 int
431 _init(void)
432 {
433 	int rval;
434 
435 	/* initialize the soft state */
436 	if ((rval = ddi_soft_state_init(&usb_ac_statep,
437 	    sizeof (usb_ac_state_t), 1)) != DDI_SUCCESS) {
438 		return (rval);
439 	}
440 
441 	if ((rval = mod_install(&usb_ac_modlinkage)) != 0) {
442 		ddi_soft_state_fini(&usb_ac_statep);
443 	}
444 
445 	if (!rval) {
446 		ssp.sp = usb_ac_statep;
447 		ssp.restore_func = usb_ac_restore_audio_state;
448 		ssp.get_featureID_func = usb_ac_get_featureID;
449 		ssp.ac_entryp = &usb_ac_entry;
450 		ssp.pm_busy_component = usb_ac_pm_busy_component;
451 		ssp.pm_idle_component = usb_ac_pm_idle_component;
452 
453 		rval = space_store("usb_ac", (uintptr_t)&ssp);
454 	}
455 
456 	return (rval);
457 }
458 
459 
460 int
461 _fini(void)
462 {
463 	int rval;
464 
465 	if ((rval = mod_remove(&usb_ac_modlinkage)) == 0) {
466 		/* Free the soft state internal structures */
467 		ddi_soft_state_fini(&usb_ac_statep);
468 		space_free("usb_ac");
469 	}
470 
471 	return (rval);
472 }
473 
474 
475 int
476 _info(struct modinfo *modinfop)
477 {
478 	return (mod_info(&usb_ac_modlinkage, modinfop));
479 }
480 
481 /*ARGSUSED*/
482 static int
483 usb_ac_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
484 			void *arg, void **result)
485 {
486 	usb_ac_state_t	   *uacp = NULL;
487 	int error = DDI_FAILURE;
488 	int instance;
489 
490 	switch (infocmd) {
491 	case DDI_INFO_DEVT2DEVINFO:
492 		instance = audio_sup_devt_to_instance((dev_t)arg);
493 		if ((uacp = ddi_get_soft_state(usb_ac_statep,
494 		    instance)) != NULL) {
495 			*result = uacp->usb_ac_dip;
496 			if (*result != NULL) {
497 				error = DDI_SUCCESS;
498 			}
499 		} else {
500 			*result = NULL;
501 		}
502 		break;
503 	case DDI_INFO_DEVT2INSTANCE:
504 		*result = (void *)(uintptr_t)
505 		    audio_sup_devt_to_instance((dev_t)arg);
506 		error = DDI_SUCCESS;
507 		break;
508 	default:
509 		break;
510 	}
511 
512 	return (error);
513 }
514 
515 extern	uint_t		nproc;
516 #define	INIT_PROCESS_CNT 3
517 
518 static int
519 usb_ac_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
520 {
521 	usb_ac_state_t		*uacp = NULL;
522 	audio_sup_reg_data_t	reg_data;
523 	int			instance = ddi_get_instance(dip);
524 	int			minor;
525 	char			*key;
526 	size_t			key_len, len;
527 
528 	switch (cmd) {
529 		case DDI_ATTACH:
530 			break;
531 		case DDI_RESUME:
532 			usb_ac_cpr_resume(dip);
533 
534 			return (DDI_SUCCESS);
535 		default:
536 			return (DDI_FAILURE);
537 	}
538 
539 	/*
540 	 * wait  until all processes are started from main.
541 	 * USB enumerates early in boot (ie. consconfig time).
542 	 * If the plumbing takes place early, the file descriptors
543 	 * are owned by the init process and can never be closed anymore
544 	 * Consequently, hot removal is not possible and the dips
545 	 * never go away. By waiting some time, e.g. INIT_PROCESS_CNT,
546 	 * the problem is avoided.
547 	 */
548 	if (nproc < INIT_PROCESS_CNT) {
549 		USB_DPRINTF_L2(PRINT_MASK_ATTA, NULL,
550 		    "usb_ac%d attach too early", instance);
551 
552 		return (DDI_FAILURE);
553 	}
554 
555 	/*
556 	 * Allocate soft state information.
557 	 */
558 	if (ddi_soft_state_zalloc(usb_ac_statep, instance) != DDI_SUCCESS) {
559 
560 		goto fail;
561 	}
562 
563 	/*
564 	 * get soft state space and initialize
565 	 */
566 	uacp = (usb_ac_state_t *)ddi_get_soft_state(usb_ac_statep, instance);
567 	if (uacp == NULL) {
568 
569 		goto fail;
570 	}
571 
572 
573 	/* get log handle */
574 	uacp->usb_ac_log_handle = usb_alloc_log_hdl(dip, "ac",
575 				&usb_ac_errlevel,
576 				&usb_ac_errmask, &usb_ac_instance_debug,
577 				0);
578 
579 	uacp->usb_ac_instance = instance;
580 	uacp->usb_ac_dip = dip;
581 
582 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
583 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
584 		    "usb_client_attach failed");
585 
586 		usb_free_log_hdl(uacp->usb_ac_log_handle);
587 		ddi_soft_state_free(usb_ac_statep, uacp->usb_ac_instance);
588 
589 		return (DDI_FAILURE);
590 	}
591 
592 	if (usb_get_dev_data(dip, &uacp->usb_ac_dev_data,
593 	    USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
594 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
595 		    "usb_get_dev_data failed");
596 
597 		usb_client_detach(dip, NULL);
598 		usb_free_log_hdl(uacp->usb_ac_log_handle);
599 		ddi_soft_state_free(usb_ac_statep, uacp->usb_ac_instance);
600 
601 		return (DDI_FAILURE);
602 	}
603 
604 	/* initialize mutex & cv */
605 	mutex_init(&uacp->usb_ac_mutex, NULL, MUTEX_DRIVER,
606 			uacp->usb_ac_dev_data->dev_iblock_cookie);
607 
608 	uacp->usb_ac_default_ph = uacp->usb_ac_dev_data->dev_default_ph;
609 
610 	/* register with audiosup */
611 	reg_data.asrd_version	= AUDIOSUP_VERSION;
612 
613 	/*
614 	 * we register with pathname, the mgf, product, and serial number
615 	 * strings, vid.pid, and driver name which should be pretty unique
616 	 */
617 	key_len = 2 * MAXNAMELEN;
618 	if (uacp->usb_ac_dev_data->dev_mfg) {
619 		key_len += strlen(uacp->usb_ac_dev_data->dev_mfg);
620 	}
621 	if (uacp->usb_ac_dev_data->dev_product) {
622 		key_len += strlen(uacp->usb_ac_dev_data->dev_product);
623 	}
624 	if (uacp->usb_ac_dev_data->dev_serial) {
625 		key_len += strlen(uacp->usb_ac_dev_data->dev_serial);
626 	}
627 
628 	key = kmem_alloc(key_len, KM_SLEEP);
629 	(void) ddi_pathname(dip, key);
630 
631 	len = strlen(key);
632 	(void) snprintf(&key[len], key_len - len, ",%s,%s,%s,%x.%x,%s",
633 	    (uacp->usb_ac_dev_data->dev_mfg ?
634 		uacp->usb_ac_dev_data->dev_mfg : "-"),
635 	    (uacp->usb_ac_dev_data->dev_product ?
636 		uacp->usb_ac_dev_data->dev_product : "-"),
637 	    (uacp->usb_ac_dev_data->dev_serial ?
638 		uacp->usb_ac_dev_data->dev_serial : "-"),
639 	    uacp->usb_ac_dev_data->dev_descr->idVendor,
640 	    uacp->usb_ac_dev_data->dev_descr->idProduct,
641 	    ddi_driver_name(dip));
642 
643 	reg_data.asrd_key = key;
644 
645 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
646 	    "registering with key: %s", key);
647 
648 	uacp->usb_ac_audiohdl = audio_sup_register(dip, &reg_data);
649 	kmem_free(key, key_len);
650 
651 	if (uacp->usb_ac_audiohdl == NULL) {
652 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
653 		    "audio_sup_register failed");
654 
655 		goto fail;
656 	}
657 
658 	/* save softstate pointer in audio handle */
659 	audio_sup_set_private(uacp->usb_ac_audiohdl, (void *)uacp);
660 
661 	/* parse all class specific descriptors */
662 	if (usb_ac_handle_descriptors(uacp) != USB_SUCCESS) {
663 
664 		goto fail;
665 	}
666 
667 	/* we no longer need the descr tree */
668 	usb_free_descr_tree(dip, uacp->usb_ac_dev_data);
669 
670 	/* read .conf file properties */
671 	uacp->usb_ac_mixer_mode_enable = ddi_prop_get_int(DDI_DEV_T_ANY,
672 				dip, DDI_PROP_DONTPASS, "mixer-enabled", 1);
673 
674 	uacp->usb_ac_ser_acc = usb_init_serialization(dip,
675 				USB_INIT_SER_CHECK_SAME_THREAD);
676 
677 	/* create minor node */
678 	minor = audio_sup_construct_minor(uacp->usb_ac_audiohdl, USER1);
679 
680 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
681 	    "minor=%d", minor);
682 
683 	if ((ddi_create_minor_node(dip, "mux", S_IFCHR,
684 	    minor, NULL, 0)) != DDI_SUCCESS) {
685 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
686 		    "usb_ac_attach: couldn't create minor node mux");
687 
688 		goto fail;
689 	}
690 	uacp->usb_ac_mux_minor = minor;
691 
692 	mutex_enter(&uacp->usb_ac_mutex);
693 
694 	/* we are online */
695 	uacp->usb_ac_dev_state = USB_DEV_ONLINE;
696 
697 	/*
698 	 * safe guard the postattach to be executed
699 	 * only two states arepossible: plumbed / unplumbed
700 	 */
701 	uacp->usb_ac_plumbing_state = USB_AC_STATE_UNPLUMBED;
702 	uacp->usb_ac_current_plumbed_index = -1;
703 
704 	mutex_exit(&uacp->usb_ac_mutex);
705 
706 	/* create components to power manage this device */
707 	usb_ac_create_pm_components(dip, uacp);
708 
709 	/* Register for events */
710 	if (usb_register_event_cbs(dip, &usb_ac_events, 0) != USB_SUCCESS) {
711 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
712 		    "usb_ac_attach: couldn't register for events");
713 
714 		goto fail;
715 	}
716 
717 	/* report device */
718 	ddi_report_dev(dip);
719 
720 	USB_DPRINTF_L4(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
721 	    "usb_ac_attach: End");
722 
723 	return (DDI_SUCCESS);
724 fail:
725 	if (uacp) {
726 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
727 		    "attach failed");
728 		(void) usb_ac_cleanup(dip, uacp);
729 	}
730 
731 	return (DDI_FAILURE);
732 }
733 
734 
735 static int
736 usb_ac_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
737 {
738 	int instance = ddi_get_instance(dip);
739 	usb_ac_state_t	*uacp;
740 	int rval;
741 
742 	uacp = ddi_get_soft_state(usb_ac_statep, instance);
743 
744 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
745 	    "usb_ac_detach:");
746 
747 	switch (cmd) {
748 	case DDI_DETACH:
749 		rval = usb_ac_cleanup(dip, uacp);
750 
751 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
752 	case DDI_SUSPEND:
753 		rval = usb_ac_cpr_suspend(dip);
754 
755 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
756 	default:
757 
758 		return (DDI_FAILURE);
759 	}
760 }
761 
762 
763 /*
764  * usb_ac_cleanup:
765  *	cleanup on attach failure and detach
766  */
767 static int
768 usb_ac_cleanup(dev_info_t *dip, usb_ac_state_t *uacp)
769 {
770 	usb_ac_power_t	*uacpm;
771 	int	rval = USB_FAILURE;
772 
773 	ASSERT(uacp);
774 
775 	mutex_enter(&uacp->usb_ac_mutex);
776 	uacpm = uacp->usb_ac_pm;
777 
778 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
779 	    "usb_ac_cleanup: uacpm=0x%p", uacpm);
780 
781 	ASSERT(uacp->usb_ac_busy_count == 0);
782 
783 	ASSERT(uacp->usb_ac_plumbing_state == USB_AC_STATE_UNPLUMBED);
784 
785 	/*
786 	 * deregister with audio framework, if it fails we are hosed
787 	 * and we probably don't want to plumb again
788 	 */
789 	if (uacp->usb_ac_audiohdl) {
790 		if (uacp->usb_ac_registered_with_mixer) {
791 			mutex_exit(&uacp->usb_ac_mutex);
792 			if (am_detach(uacp->usb_ac_audiohdl, DDI_DETACH) !=
793 			    AUDIO_SUCCESS) {
794 
795 				return (rval);
796 			}
797 		} else {
798 			mutex_exit(&uacp->usb_ac_mutex);
799 		}
800 		if (audio_sup_unregister(uacp->usb_ac_audiohdl) !=
801 		    AUDIO_SUCCESS) {
802 
803 			return (rval);
804 		}
805 	} else {
806 		mutex_exit(&uacp->usb_ac_mutex);
807 	}
808 
809 	/*
810 	 * Disable the event callbacks, after this point, event
811 	 * callbacks will never get called. Note we shouldn't hold
812 	 * the mutex while unregistering events because there may be a
813 	 * competing event callback thread. Event callbacks are done
814 	 * with ndi mutex held and this can cause a potential deadlock.
815 	 */
816 	usb_unregister_event_cbs(dip, &usb_ac_events);
817 
818 	mutex_enter(&uacp->usb_ac_mutex);
819 
820 	if (uacpm && (uacp->usb_ac_dev_state != USB_DEV_DISCONNECTED)) {
821 		if (uacpm->acpm_wakeup_enabled) {
822 			mutex_exit(&uacp->usb_ac_mutex);
823 			usb_ac_pm_busy_component(uacp);
824 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
825 
826 			rval = usb_handle_remote_wakeup(dip,
827 				USB_REMOTE_WAKEUP_DISABLE);
828 			if (rval != USB_SUCCESS) {
829 				USB_DPRINTF_L2(PRINT_MASK_PM,
830 				    uacp->usb_ac_log_handle,
831 				    "usb_ac_cleanup: disable remote "
832 				    "wakeup failed, rval=%d", rval);
833 			}
834 			usb_ac_pm_idle_component(uacp);
835 		} else {
836 			mutex_exit(&uacp->usb_ac_mutex);
837 		}
838 
839 		(void) pm_lower_power(dip, 0, USB_DEV_OS_PWR_OFF);
840 
841 		mutex_enter(&uacp->usb_ac_mutex);
842 	}
843 
844 	if (uacpm) {
845 		kmem_free(uacpm,  sizeof (usb_ac_power_t));
846 		uacp->usb_ac_pm = NULL;
847 	}
848 
849 	usb_client_detach(dip, uacp->usb_ac_dev_data);
850 
851 	/* free descriptors */
852 	usb_ac_free_all_units(uacp);
853 
854 	mutex_exit(&uacp->usb_ac_mutex);
855 
856 	mutex_destroy(&uacp->usb_ac_mutex);
857 
858 	usb_fini_serialization(uacp->usb_ac_ser_acc);
859 
860 	ddi_remove_minor_node(dip, NULL);
861 
862 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
863 	    "usb_ac_cleanup: Ending");
864 
865 	usb_free_log_hdl(uacp->usb_ac_log_handle);
866 	kmem_free(uacp->usb_ac_connections, uacp->usb_ac_connections_len);
867 	kmem_free(uacp->usb_ac_connections_a, uacp->usb_ac_connections_a_len);
868 	kmem_free(uacp->usb_ac_unit_type, uacp->usb_ac_max_unit);
869 	kmem_free(uacp->usb_ac_traverse_path, uacp->usb_ac_max_unit);
870 
871 	ddi_soft_state_free(usb_ac_statep, uacp->usb_ac_instance);
872 
873 	ddi_prop_remove_all(dip);
874 
875 	return (USB_SUCCESS);
876 }
877 
878 
879 /*
880  * usb_ac_open:
881  *	Open entry point. Called on the plumbing minor node or
882  *	audio or audioctl minor nodes which we pass to audio_sup_open()
883  *	We do not raise power here and wait for the setup callback
884  */
885 /*ARGSUSED*/
886 static int
887 usb_ac_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
888 {
889 	int minor = getminor(*devp);
890 	int instance;
891 	int rval;
892 	usb_ac_state_t	*uacp;
893 
894 	instance = audio_sup_devt_to_instance(*devp);
895 
896 	uacp = ddi_get_soft_state(usb_ac_statep, instance);
897 	if (uacp == NULL) {
898 
899 		return (ENXIO);
900 	}
901 
902 	mutex_enter(&uacp->usb_ac_mutex);
903 	uacp->usb_ac_busy_count++; /* This will prevent unplumbing */
904 
905 	USB_DPRINTF_L4(PRINT_MASK_OPEN, uacp->usb_ac_log_handle,
906 	    "usb_ac_open: Begin q=0x%p, minor=0x%x instance=%d "
907 	    "open cnt=%d", q, minor, instance, uacp->usb_ac_busy_count);
908 
909 	if (sflag) {
910 		USB_DPRINTF_L2(PRINT_MASK_OPEN, uacp->usb_ac_log_handle,
911 		    "usb_ac_open: clone open not supported");
912 
913 		uacp->usb_ac_busy_count--;
914 		mutex_exit(&uacp->usb_ac_mutex);
915 
916 		return (ENXIO);
917 	}
918 
919 	if (minor == uacp->usb_ac_mux_minor) {
920 
921 		USB_DPRINTF_L4(PRINT_MASK_OPEN, uacp->usb_ac_log_handle,
922 		    "usb_ac_open: opening mux");
923 		/*
924 		 * This is the plumbing open, initiated during attach/
925 		 * connect_event_callback/cpr_resume/first user open.
926 		 */
927 		uacp->usb_ac_busy_count--;
928 
929 		/* Save the dev_t value of pluming q to use for lower q's */
930 		uacp->usb_ac_dev = *devp;
931 		audio_sup_set_qptr(q, *devp, (void *)uacp);
932 
933 		/* Initialize the queue pointers */
934 		uacp->usb_ac_rq = q;
935 		uacp->usb_ac_wq = WR(q);
936 
937 		/* release mutex while making streams framework call */
938 		mutex_exit(&uacp->usb_ac_mutex);
939 		qprocson(q);
940 		mutex_enter(&uacp->usb_ac_mutex);
941 
942 	} else if (uacp->usb_ac_plumbing_state != USB_AC_STATE_PLUMBED) {
943 		uacp->usb_ac_busy_count--;
944 		mutex_exit(&uacp->usb_ac_mutex);
945 
946 		return (EIO);
947 	} else {
948 		/* pass the open to audio_sup_open so SADA can do its work */
949 		USB_DPRINTF_L4(PRINT_MASK_OPEN, uacp->usb_ac_log_handle,
950 		    "usb_ac_open: calling audio_sup_open, q=0x%p, open_cnt=%d",
951 		    q, uacp->usb_ac_busy_count);
952 
953 		mutex_exit(&uacp->usb_ac_mutex);
954 
955 		/*
956 		 * go to full power
957 		 */
958 		usb_ac_pm_busy_component(uacp);
959 		(void) pm_raise_power(uacp->usb_ac_dip, 0, USB_DEV_OS_FULL_PWR);
960 
961 		rval = audio_sup_open(q, devp, flag, sflag, credp);
962 
963 		mutex_enter(&uacp->usb_ac_mutex);
964 
965 		if (rval != 0) {
966 			USB_DPRINTF_L4(PRINT_MASK_OPEN,
967 			    uacp->usb_ac_log_handle,
968 			    "audio_sup_open rval=%d", rval);
969 
970 			uacp->usb_ac_busy_count--;
971 
972 			mutex_exit(&uacp->usb_ac_mutex);
973 
974 			usb_ac_pm_idle_component(uacp);
975 
976 			return (rval);
977 		}
978 	}
979 
980 	USB_DPRINTF_L4(PRINT_MASK_OPEN, uacp->usb_ac_log_handle,
981 	    "usb_ac_open: End q=0x%p, open cnt=%d", q, uacp->usb_ac_busy_count);
982 
983 	mutex_exit(&uacp->usb_ac_mutex);
984 
985 	return (0);
986 }
987 
988 
989 /*
990  * usb_ac_close :
991  *	Close entry point
992  */
993 /*ARGSUSED*/
994 static int
995 usb_ac_close(queue_t *q, int flag, cred_t *credp)
996 {
997 	dev_t dev = audio_sup_get_qptr_dev(q);
998 	int minor = getminor(dev);
999 	int instance = audio_sup_get_qptr_instance(q);
1000 	usb_ac_state_t *uacp = ddi_get_soft_state(usb_ac_statep, instance);
1001 	int rval;
1002 
1003 	mutex_enter(&uacp->usb_ac_mutex);
1004 
1005 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, uacp->usb_ac_log_handle,
1006 	    "usb_ac_close: Begin q=0x%p, opencount=%d",
1007 	    q, uacp->usb_ac_busy_count);
1008 
1009 	/* closing the mux? */
1010 	if (minor == uacp->usb_ac_mux_minor) {
1011 		USB_DPRINTF_L4(PRINT_MASK_CLOSE, uacp->usb_ac_log_handle,
1012 		    "usb_ac_close: closing mux plumbing stream");
1013 		mutex_exit(&uacp->usb_ac_mutex);
1014 
1015 		/* Wait till all activity in the default pipe has drained */
1016 		usb_ac_serialize_access(uacp);
1017 		usb_ac_release_access(uacp);
1018 
1019 		audio_sup_free_qptr(q);
1020 		qprocsoff(q);
1021 
1022 		return (0);
1023 	}
1024 
1025 	mutex_exit(&uacp->usb_ac_mutex);
1026 
1027 	rval = audio_sup_close(q, flag, credp);
1028 
1029 	if (rval != 0) {
1030 		USB_DPRINTF_L2(PRINT_MASK_CLOSE, uacp->usb_ac_log_handle,
1031 		    "audio_sup_close fails %d", rval);
1032 
1033 		return (rval);
1034 	}
1035 
1036 	mutex_enter(&uacp->usb_ac_mutex);
1037 
1038 	/* normal streams closing */
1039 	ASSERT(uacp->usb_ac_plumbing_state >= USB_AC_STATE_PLUMBED);
1040 
1041 	uacp->usb_ac_busy_count --;
1042 
1043 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, uacp->usb_ac_log_handle,
1044 	    "usb_ac_close: End rval=%d q=0x%p, opencount=%d",
1045 	    rval, q, uacp->usb_ac_busy_count);
1046 
1047 	mutex_exit(&uacp->usb_ac_mutex);
1048 
1049 	usb_ac_pm_idle_component(uacp);
1050 
1051 	return (0);
1052 }
1053 
1054 
1055 /*
1056  * usb_ac_uwput:
1057  *	write put entry point for the upper mux. Only PLUMB/UNPLUMB ioctls
1058  *	are processed here. All other ioctls are passed to audio_sup routines
1059  *	for further processing.
1060  */
1061 static int
1062 usb_ac_uwput(queue_t *q, mblk_t *mp)
1063 {
1064 	int instance = audio_sup_get_qptr_instance(q);
1065 	usb_ac_state_t	*uacp = ddi_get_soft_state(usb_ac_statep, instance);
1066 	int error = DDI_SUCCESS;
1067 
1068 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1069 	    "usb_ac_uwput: q=0x%p, mp=0x%p", q, mp);
1070 
1071 	ASSERT(mp != NULL);
1072 	ASSERT(mp->b_datap != NULL);
1073 
1074 	mutex_enter(&uacp->usb_ac_mutex);
1075 	if (uacp->usb_ac_wq == q) {
1076 		ASSERT(mp->b_datap->db_type == M_IOCTL);
1077 
1078 		mutex_exit(&uacp->usb_ac_mutex);
1079 
1080 		/* ioctl from plumbing thread (namely P_LINK) */
1081 		usb_ac_plumb_ioctl(q, mp);
1082 
1083 		return (error);
1084 	}
1085 	mutex_exit(&uacp->usb_ac_mutex);
1086 
1087 	/* Pass to audio_sup routine */
1088 	(void) audio_sup_wput(q, mp);
1089 
1090 	return (error);
1091 }
1092 
1093 
1094 /*
1095  * usb_ac_lrput:
1096  *	read put entry point for the lower mux. Get the response from the
1097  *	lower module, signal usb_ac_send_as_cmd(), the thread that is waiting
1098  *	for a response to a message sent earlier anbd pass the response
1099  *	message	block.
1100  */
1101 static int
1102 usb_ac_lrput(queue_t *q, mblk_t *mp)
1103 {
1104 	int instance = audio_sup_get_qptr_instance(q);
1105 	usb_ac_state_t	*uacp;
1106 	int error = DDI_SUCCESS;
1107 	usb_ac_plumbed_t *plumb_infop;
1108 	usb_ac_streams_info_t *streams_infop = NULL;
1109 	int	val;
1110 	char	val1;
1111 	struct iocblk *iocp;
1112 
1113 	uacp = ddi_get_soft_state(usb_ac_statep, instance);
1114 
1115 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1116 	    "usb_ac_lrput: q=0x%p, mp=0x%p, instance=%d", q, mp, instance);
1117 	ASSERT(mp != NULL);
1118 
1119 	mutex_enter(&uacp->usb_ac_mutex);
1120 	plumb_infop = usb_ac_get_plumb_info_from_lrq(uacp, q);
1121 	ASSERT(plumb_infop != NULL);
1122 
1123 	switch (mp->b_datap->db_type) {
1124 	case M_CTL:
1125 	case M_ERROR:
1126 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1127 		    "M_CTL/M_ERROR");
1128 
1129 		switch (plumb_infop->acp_driver) {
1130 		case USB_AS_PLUMBED:
1131 			USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1132 			    "reply from usb_as, lrq=0x%p", q);
1133 			streams_infop = (usb_ac_streams_info_t *)
1134 							plumb_infop->acp_data;
1135 			ASSERT(streams_infop != NULL);
1136 			streams_infop->acs_ac_to_as_req.acr_reply_mp = mp;
1137 			streams_infop->acs_ac_to_as_req.acr_wait_flag = 0;
1138 			cv_signal(&streams_infop->acs_ac_to_as_req.acr_cv);
1139 
1140 			break;
1141 		case USB_AH_PLUMBED:
1142 			USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1143 			    "M_CTL from hid, lrq=0x%p", q);
1144 
1145 			iocp = (struct iocblk *)mp->b_rptr;
1146 			ASSERT(mp->b_cont != NULL);
1147 
1148 			if (uacp->usb_ac_registered_with_mixer) {
1149 
1150 				val1 = *((char *)mp->b_cont->b_rptr);
1151 				val = (int)val1;
1152 
1153 				USB_DPRINTF_L4(PRINT_MASK_ALL,
1154 				    uacp->usb_ac_log_handle, "val1=0x%x(%d),"
1155 				    "val=0x%x(%d)", val1, val1, val, val);
1156 
1157 				switch (iocp->ioc_cmd) {
1158 				/* Handle relative volume change */
1159 				case USB_AUDIO_VOL_CHANGE:
1160 					/* prevent unplumbing */
1161 					uacp->usb_ac_busy_count++;
1162 					if (uacp->usb_ac_plumbing_state ==
1163 					    USB_AC_STATE_PLUMBED) {
1164 						mutex_exit(&uacp->usb_ac_mutex);
1165 						(void) am_hw_state_change(
1166 						    uacp->usb_ac_audiohdl,
1167 						    AM_HWSC_SET_GAIN_DELTA,
1168 						    AUDIO_PLAY, val,
1169 						    AUDIO_NO_SLEEP);
1170 						mutex_enter(&uacp->
1171 								usb_ac_mutex);
1172 					}
1173 					uacp->usb_ac_busy_count--;
1174 					/* FALLTHRU */
1175 				case USB_AUDIO_MUTE:
1176 				default:
1177 					freemsg(mp);
1178 					break;
1179 				}
1180 			} else {
1181 				freemsg(mp);
1182 			}
1183 
1184 			break;
1185 		default:
1186 			USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1187 			    "M_CTL from unknown module(%s)",
1188 			    ddi_driver_name(plumb_infop->acp_dip));
1189 			freemsg(mp);
1190 		}
1191 
1192 		break;
1193 	default:
1194 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1195 		    "Unknown type=%d", mp->b_datap->db_type);
1196 		usb_ac_free_mblk(mp);
1197 	}
1198 	mutex_exit(&uacp->usb_ac_mutex);
1199 
1200 	/*
1201 	 * Nobody is waiting; nothing to send up.
1202 	 */
1203 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1204 	    "usb_ac_lrput: done");
1205 
1206 	return (error);
1207 }
1208 
1209 
1210 /*
1211  * Power Management
1212  * usb_ac_power:
1213  *	power entry point
1214  */
1215 static int
1216 usb_ac_power(dev_info_t *dip, int comp, int level)
1217 {
1218 	int		instance = ddi_get_instance(dip);
1219 	usb_ac_state_t	*uacp;
1220 	usb_ac_power_t	*uacpm;
1221 	int		rval = DDI_FAILURE;
1222 
1223 	uacp = ddi_get_soft_state(usb_ac_statep, instance);
1224 
1225 	USB_DPRINTF_L4(PRINT_MASK_PM, uacp->usb_ac_log_handle,
1226 	    "usb_ac_power: comp=%d level=%d", comp, level);
1227 
1228 	mutex_enter(&uacp->usb_ac_mutex);
1229 	uacpm = uacp->usb_ac_pm;
1230 
1231 	if (USB_DEV_PWRSTATE_OK(uacpm->acpm_pwr_states, level)) {
1232 		USB_DPRINTF_L2(PRINT_MASK_PM, uacp->usb_ac_log_handle,
1233 		    "usb_ac_power: illegal level=%d pwr_states=%d",
1234 		    level, uacpm->acpm_pwr_states);
1235 
1236 		goto done;
1237 	}
1238 
1239 	switch (level) {
1240 	case USB_DEV_OS_PWR_OFF:
1241 		rval = usb_ac_pwrlvl0(uacp);
1242 		break;
1243 	case USB_DEV_OS_PWR_1:
1244 		rval = usb_ac_pwrlvl1(uacp);
1245 		break;
1246 	case USB_DEV_OS_PWR_2:
1247 		rval = usb_ac_pwrlvl2(uacp);
1248 		break;
1249 	case USB_DEV_OS_FULL_PWR:
1250 		rval = usb_ac_pwrlvl3(uacp);
1251 		break;
1252 	}
1253 
1254 done:
1255 	mutex_exit(&uacp->usb_ac_mutex);
1256 
1257 	return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1258 }
1259 
1260 
1261 /*
1262  * functions to handle power transition for various levels
1263  * These functions act as place holders to issue USB commands
1264  * to the devices to change their power levels
1265  * Level 0 = Device is powered off
1266  * Level 3 = Device if full powered
1267  * Level 1,2 = Intermediate power level of the device as implemented
1268  *	by the hardware.
1269  * Note that Level 0 is OS power-off and Level 3 is OS full-power.
1270  */
1271 static int
1272 usb_ac_pwrlvl0(usb_ac_state_t *uacp)
1273 {
1274 	usb_ac_power_t	*uacpm;
1275 	int		rval;
1276 
1277 	uacpm = uacp->usb_ac_pm;
1278 
1279 	switch (uacp->usb_ac_dev_state) {
1280 	case USB_DEV_ONLINE:
1281 		/* Deny the powerdown request if the device is busy */
1282 		if (uacpm->acpm_pm_busy != 0) {
1283 
1284 			return (USB_FAILURE);
1285 		}
1286 
1287 		/* Issue USB D3 command to the device here */
1288 		rval = usb_set_device_pwrlvl3(uacp->usb_ac_dip);
1289 		ASSERT(rval == USB_SUCCESS);
1290 
1291 		uacp->usb_ac_dev_state = USB_DEV_PWRED_DOWN;
1292 		uacpm->acpm_current_power = USB_DEV_OS_PWR_OFF;
1293 
1294 		/* FALLTHRU */
1295 	case USB_DEV_DISCONNECTED:
1296 	case USB_DEV_SUSPENDED:
1297 	case USB_DEV_PWRED_DOWN:
1298 	default:
1299 		return (USB_SUCCESS);
1300 	}
1301 }
1302 
1303 
1304 /* ARGSUSED */
1305 static int
1306 usb_ac_pwrlvl1(usb_ac_state_t *uacp)
1307 {
1308 	int		rval;
1309 
1310 	/* Issue USB D2 command to the device here */
1311 	rval = usb_set_device_pwrlvl2(uacp->usb_ac_dip);
1312 	ASSERT(rval == USB_SUCCESS);
1313 
1314 	return (USB_FAILURE);
1315 }
1316 
1317 
1318 /* ARGSUSED */
1319 static int
1320 usb_ac_pwrlvl2(usb_ac_state_t *uacp)
1321 {
1322 	int		rval;
1323 
1324 	rval = usb_set_device_pwrlvl1(uacp->usb_ac_dip);
1325 	ASSERT(rval == USB_SUCCESS);
1326 
1327 	return (USB_FAILURE);
1328 }
1329 
1330 
1331 static int
1332 usb_ac_pwrlvl3(usb_ac_state_t *uacp)
1333 {
1334 	usb_ac_power_t	*uacpm;
1335 	int		rval;
1336 
1337 	uacpm = uacp->usb_ac_pm;
1338 
1339 	switch (uacp->usb_ac_dev_state) {
1340 	case USB_DEV_PWRED_DOWN:
1341 		/* Issue USB D0 command to the device here */
1342 		rval = usb_set_device_pwrlvl0(uacp->usb_ac_dip);
1343 		ASSERT(rval == USB_SUCCESS);
1344 
1345 		uacp->usb_ac_dev_state = USB_DEV_ONLINE;
1346 		uacpm->acpm_current_power = USB_DEV_OS_FULL_PWR;
1347 		/* FALLTHRU */
1348 	case USB_DEV_ONLINE:
1349 		/* we are already in full power */
1350 
1351 		/* FALLTHRU */
1352 	case USB_DEV_DISCONNECTED:
1353 	case USB_DEV_SUSPENDED:
1354 
1355 		return (USB_SUCCESS);
1356 	default:
1357 		USB_DPRINTF_L2(PRINT_MASK_PM, uacp->usb_ac_log_handle,
1358 		    "usb_ac_pwerlvl3: Illegal dev_state");
1359 
1360 		return (USB_FAILURE);
1361 	}
1362 }
1363 
1364 
1365 static void
1366 usb_ac_create_pm_components(dev_info_t *dip, usb_ac_state_t *uacp)
1367 {
1368 	usb_ac_power_t	*uacpm;
1369 	uint_t		pwr_states;
1370 
1371 	USB_DPRINTF_L4(PRINT_MASK_PM, uacp->usb_ac_log_handle,
1372 	    "usb_ac_create_pm_components: begin");
1373 
1374 	/* Allocate the state structure */
1375 	uacpm = kmem_zalloc(sizeof (usb_ac_power_t), KM_SLEEP);
1376 	uacp->usb_ac_pm = uacpm;
1377 	uacpm->acpm_state = uacp;
1378 	uacpm->acpm_capabilities = 0;
1379 	uacpm->acpm_current_power = USB_DEV_OS_FULL_PWR;
1380 
1381 	if (usb_create_pm_components(dip, &pwr_states) ==
1382 	    USB_SUCCESS) {
1383 		if (usb_handle_remote_wakeup(dip,
1384 		    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
1385 			uacpm->acpm_wakeup_enabled = 1;
1386 
1387 			USB_DPRINTF_L4(PRINT_MASK_PM,
1388 			    uacp->usb_ac_log_handle,
1389 			    "remote Wakeup enabled");
1390 		}
1391 		uacpm->acpm_pwr_states = (uint8_t)pwr_states;
1392 		(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
1393 	} else {
1394 		if (uacpm) {
1395 			kmem_free(uacpm,  sizeof (usb_ac_power_t));
1396 			uacp->usb_ac_pm = NULL;
1397 		}
1398 		USB_DPRINTF_L2(PRINT_MASK_PM, uacp->usb_ac_log_handle,
1399 		    "pm not enabled");
1400 	}
1401 
1402 	USB_DPRINTF_L4(PRINT_MASK_PM, uacp->usb_ac_log_handle,
1403 	    "usb_ac_create_pm_components: end");
1404 }
1405 
1406 
1407 /*
1408  * usb_ac_plumb_ioctl:
1409  *	IOCTL issued from plumbing thread (only P_LINK_LH/P_UNLINK for now
1410  *	caused by ldi_ioctl). Maybe we will need to use this function
1411  *	to issue other IOCTLS to children in future from plumbing thread
1412  */
1413 static void
1414 usb_ac_plumb_ioctl(queue_t *q, mblk_t *mp)
1415 {
1416 	int		instance = audio_sup_get_qptr_instance(q);
1417 	usb_ac_state_t	*uacp = ddi_get_soft_state(usb_ac_statep, instance);
1418 	struct iocblk	*iocp;
1419 	struct linkblk	*linkp;
1420 	int		n;
1421 	usb_ac_streams_info_t *streams_infop;
1422 
1423 	ASSERT(uacp != NULL);
1424 	ASSERT(mp != NULL);
1425 	ASSERT(mp->b_cont != NULL);
1426 
1427 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1428 	    "usb_ac_plumb_ioctl, q=0x%p mp=0x%p instance=%d",
1429 	    q, mp, instance);
1430 
1431 	iocp = (struct iocblk *)mp->b_rptr;
1432 	mutex_enter(&uacp->usb_ac_mutex);
1433 	n = uacp->usb_ac_current_plumbed_index;
1434 
1435 	switch (iocp->ioc_cmd) {
1436 	case I_PLINK:
1437 		linkp = (struct linkblk *)mp->b_cont->b_rptr;
1438 
1439 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1440 		    "LINK ioctl, index=%d linkblk ptr=0x%p", n, linkp);
1441 
1442 		/*
1443 		 * We keep track of the module that is being
1444 		 * currently plumbed through usb_ac_current_plumbed_index
1445 		 * to the plumb structure array. We set the lwq field
1446 		 * of the plumb structure here.
1447 		 */
1448 		ASSERT(uacp->usb_ac_plumbed[n].acp_lwq == NULL);
1449 		uacp->usb_ac_plumbed[n].acp_lwq = linkp->l_qbot;
1450 		uacp->usb_ac_plumbed[n].acp_lrq = RD(linkp->l_qbot);
1451 
1452 		audio_sup_set_qptr(uacp->usb_ac_plumbed[n].acp_lrq,
1453 		    uacp->usb_ac_dev, (void *)uacp);
1454 
1455 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1456 		    "index=%d lwq=0x%p lrq=0x%p", n, linkp->l_qbot,
1457 		    RD(linkp->l_qbot));
1458 		break;
1459 	case I_UNLINK:
1460 	case I_PUNLINK:
1461 		linkp = (struct linkblk *)mp->b_cont->b_rptr;
1462 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1463 		    "UNLINK ioctl, linkblk ptr=0x%p", linkp);
1464 
1465 		audio_sup_free_qptr(RD(linkp->l_qbot));
1466 		uacp->usb_ac_dev = 0;
1467 
1468 		if (uacp->usb_ac_plumbed[n].acp_driver == USB_AS_PLUMBED) {
1469 
1470 			/*
1471 			 * we bzero the streams info and plumbed structure
1472 			 * since there is no guarantee that the next plumbing
1473 			 * will be identical
1474 			 */
1475 			streams_infop = (usb_ac_streams_info_t *)
1476 					uacp->usb_ac_plumbed[n].acp_data;
1477 			cv_destroy(&(streams_infop->acs_ac_to_as_req.acr_cv));
1478 
1479 			/* bzero the relevant plumbing structure */
1480 			bzero(streams_infop, sizeof (usb_ac_streams_info_t));
1481 		}
1482 		bzero(&uacp->usb_ac_plumbed[n], sizeof (usb_ac_plumbed_t));
1483 
1484 		iocp->ioc_count = 0;
1485 		break;
1486 	default:
1487 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1488 		    "Unknown ioctl, cmd=%d", iocp->ioc_cmd);
1489 		iocp->ioc_error = EINVAL;
1490 		mutex_exit(&uacp->usb_ac_mutex);
1491 
1492 		goto iocnak;
1493 	}
1494 
1495 	mutex_exit(&uacp->usb_ac_mutex);
1496 
1497 	/*
1498 	 * Common exit path for calls that return a positive
1499 	 * acknowledgment with a return value of 0.
1500 	 */
1501 	iocp->ioc_rval = 0;
1502 	iocp->ioc_error = 0;
1503 	mp->b_datap->db_type = M_IOCACK;
1504 	qreply(q, mp);
1505 
1506 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1507 	    "usb_ac_plumb_ioctl: End (ACK)");
1508 
1509 	return;
1510 
1511 iocnak:
1512 
1513 	iocp->ioc_rval = 0;
1514 	mp->b_datap->db_type = M_IOCNAK;
1515 	qreply(q, mp);
1516 
1517 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1518 	    "usb_ac_plumb_ioctl: End: (NAK)");
1519 }
1520 
1521 
1522 /*
1523  * usb_ac_get_plumb_info:
1524  *	Get plumb_info pointer that matches module "name"
1525  *	If name = "usb_as", match the direction also (record or play)
1526  */
1527 static usb_ac_plumbed_t *
1528 usb_ac_get_plumb_info(usb_ac_state_t *uacp, char *name, uchar_t reg_play_type)
1529 {
1530 	int			n;
1531 	usb_ac_plumbed_t	*plumb_infop = NULL;
1532 	usb_as_registration_t	*asreg;
1533 	usb_ac_streams_info_t	*asinfo;
1534 
1535 	for (n = 0; n < USB_AC_MAX_PLUMBED; n++) {
1536 		if (uacp->usb_ac_plumbed[n].acp_dip == NULL) {
1537 			continue;
1538 		}
1539 		if (strcmp(ddi_driver_name(uacp->
1540 		    usb_ac_plumbed[n].acp_dip), name) != 0) {
1541 			continue;
1542 		}
1543 		if (uacp->usb_ac_plumbed[n].acp_driver == USB_AS_PLUMBED) {
1544 			asinfo = uacp->usb_ac_plumbed[n].acp_data;
1545 			asreg = asinfo->acs_streams_reg;
1546 			/* Match direction */
1547 			if (asreg->reg_mode & reg_play_type) {
1548 				break;
1549 			}
1550 		} else if (uacp->usb_ac_plumbed[n].acp_driver ==
1551 		    USB_AH_PLUMBED) {
1552 			break;
1553 		}
1554 	}
1555 
1556 	if (n < USB_AC_MAX_PLUMBED) {
1557 		plumb_infop = &uacp->usb_ac_plumbed[n];
1558 	}
1559 
1560 	return (plumb_infop);
1561 }
1562 
1563 
1564 /*
1565  * usb_ac_get_pinfo_from_lrq:
1566  *	Get plumb_info pointer that matches the lrq passed
1567  */
1568 static usb_ac_plumbed_t *
1569 usb_ac_get_plumb_info_from_lrq(usb_ac_state_t *uacp, queue_t *lrq)
1570 {
1571 	int	n;
1572 
1573 	for (n = 0; n < USB_AC_MAX_PLUMBED; n++) {
1574 		if (uacp->usb_ac_plumbed[n].acp_lrq == lrq) {
1575 
1576 			return (&uacp->usb_ac_plumbed[n]);
1577 		}
1578 	}
1579 
1580 	return (NULL);
1581 }
1582 
1583 
1584 /*
1585  * usb_ac_get_featureID:
1586  *	find out if there is at least one feature unit that supports
1587  *	the request controls.
1588  *	Return featureID or USB_AC_ID_NONE.
1589  */
1590 static uint_t
1591 usb_ac_get_featureID(usb_ac_state_t *uacp, uchar_t dir,
1592     uint_t channel, uint_t control)
1593 {
1594 	uint_t count = 0;
1595 
1596 	return (usb_ac_set_control(uacp, dir, USB_AUDIO_FEATURE_UNIT,
1597 		    channel, control, USB_AC_FIND_ONE, &count, 0,
1598 		    usb_ac_feature_unit_check));
1599 }
1600 
1601 
1602 /*
1603  * usb_ac_feature_unit_check:
1604  *	check if a feature unit can support the required channel
1605  *	and control combination. Return USB_SUCCESS or USB_FAILURE.
1606  *	Called for each matching unit from usb_ac_traverse_connections.
1607  */
1608 /*ARGSUSED*/
1609 static int
1610 usb_ac_feature_unit_check(usb_ac_state_t *uacp, uint_t featureID,
1611     uint_t dir, uint_t channel, uint_t control, uint_t arg1, uint_t *depth)
1612 {
1613 	usb_audio_feature_unit_descr1_t *feature_descrp;
1614 	int				n_channel_controls;
1615 
1616 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1617 	    "usb_ac_feature_unit_check: ID=%d ch=%d cntrl=%d",
1618 	    featureID, channel, control);
1619 
1620 	ASSERT((featureID >= 0) && (featureID < uacp->usb_ac_max_unit));
1621 
1622 	/*
1623 	 * check if this control is supported on this channel
1624 	 */
1625 	feature_descrp = (usb_audio_feature_unit_descr1_t *)
1626 		uacp->usb_ac_units[featureID].acu_descriptor;
1627 	ASSERT(feature_descrp->bUnitID == featureID);
1628 
1629 	USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1630 	    "bControlSize=%d", feature_descrp->bControlSize);
1631 
1632 	if (feature_descrp->bControlSize == 0) {
1633 		featureID = USB_AC_ID_NONE;
1634 	} else {
1635 		uint_t index;
1636 
1637 		n_channel_controls = (feature_descrp->bLength -
1638 		    offsetof(usb_audio_feature_unit_descr1_t,
1639 		    bmaControls))/feature_descrp->bControlSize;
1640 
1641 		USB_DPRINTF_L3(PRINT_MASK_ALL,
1642 		    uacp->usb_ac_log_handle,
1643 		    "#controls: %d index=%d", n_channel_controls,
1644 		    feature_descrp->bControlSize * channel);
1645 
1646 		if (channel > n_channel_controls) {
1647 			featureID = USB_AC_ID_NONE;
1648 		} else {
1649 			/*
1650 			 * we only support MUTE and VOLUME
1651 			 * which are in the first byte
1652 			 */
1653 			index = feature_descrp->bControlSize *
1654 						channel;
1655 
1656 			USB_DPRINTF_L3(PRINT_MASK_ALL,
1657 			    uacp->usb_ac_log_handle,
1658 			    "control: 0x%x",
1659 			    feature_descrp->bmaControls[index]);
1660 
1661 			if ((feature_descrp->bmaControls[index] &
1662 			    control) == 0) {
1663 				featureID = USB_AC_ID_NONE;
1664 			}
1665 		}
1666 	}
1667 
1668 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
1669 	    "usb_ac_feature_unit_check: dir=%d featureID=0x%x",
1670 	    dir, featureID);
1671 
1672 	return ((featureID != USB_AC_ID_NONE) ?
1673 		USB_SUCCESS : USB_FAILURE);
1674 }
1675 
1676 
1677 /*
1678  * Descriptor Management
1679  *
1680  * usb_ac_handle_descriptors:
1681  *	extract interesting descriptors from the config cloud
1682  */
1683 static int
1684 usb_ac_handle_descriptors(usb_ac_state_t *uacp)
1685 {
1686 	int			rest, len, index;
1687 	int			rval = USB_FAILURE;
1688 	usb_audio_cs_if_descr_t descr;
1689 	usb_client_dev_data_t	*dev_data = uacp->usb_ac_dev_data;
1690 	usb_alt_if_data_t	*altif_data;
1691 	usb_cvs_data_t		*cvs;
1692 
1693 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1694 	    "config=%d, interface=%d",
1695 	    dev_data->dev_curr_cfg - &dev_data->dev_cfg[0],
1696 	    dev_data->dev_curr_if);
1697 
1698 	altif_data = &dev_data->dev_curr_cfg->
1699 			cfg_if[dev_data->dev_curr_if].if_alt[0];
1700 
1701 	uacp->usb_ac_ifno	= dev_data->dev_curr_if;
1702 	uacp->usb_ac_if_descr	= altif_data->altif_descr;
1703 
1704 	/* find USB_AUDIO_CS_INTERFACE type descriptor */
1705 	for (index = 0; index < altif_data->altif_n_cvs; index++) {
1706 		cvs = &altif_data->altif_cvs[index];
1707 		if (cvs->cvs_buf == NULL) {
1708 			continue;
1709 		}
1710 		if (cvs->cvs_buf[1] == USB_AUDIO_CS_INTERFACE) {
1711 			break;
1712 		}
1713 	}
1714 
1715 	if (index == altif_data->altif_n_cvs) {
1716 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1717 		    "cannot find descriptor type %d", USB_AUDIO_CS_INTERFACE);
1718 
1719 		return (rval);
1720 	}
1721 
1722 	len = usb_parse_data(
1723 		CS_AC_IF_HEADER_FORMAT,
1724 		cvs->cvs_buf, cvs->cvs_buf_len,
1725 		(void *)&descr, sizeof (usb_audio_cs_if_descr_t));
1726 
1727 	/* is this a sane header descriptor */
1728 	if (!((len >= CS_AC_IF_HEADER_SIZE) &&
1729 	    (descr.bDescriptorType == USB_AUDIO_CS_INTERFACE) &&
1730 	    (descr.bDescriptorSubType == USB_AUDIO_HEADER))) {
1731 		USB_DPRINTF_L2(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1732 		    "invalid header");
1733 
1734 		return (rval);
1735 	}
1736 
1737 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1738 	    "header: type=0x%x subtype=0x%x bcdADC=0x%x\n\t"
1739 	    "total=0x%x InCol=0x%x interfaceno: 0x%x 0x%x 0x%x 0x%x",
1740 	    descr.bDescriptorType,
1741 	    descr.bDescriptorSubType,
1742 	    descr.bcdADC,
1743 	    descr.wTotalLength,
1744 	    descr.blnCollection,
1745 	    descr.baInterfaceNr[0], descr.baInterfaceNr[1],
1746 	    descr.baInterfaceNr[2], descr.baInterfaceNr[3]);
1747 
1748 	/*
1749 	 * we read descriptors by index and store them in ID array.
1750 	 * the actual parsing is done in usb_ac_add_unit_descriptor()
1751 	 */
1752 	rest = descr.wTotalLength - descr.bLength;
1753 	for (index++; rest > 0; index++) {
1754 		USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1755 		    "index=%d rest=%d", index, rest);
1756 
1757 		cvs = &altif_data->altif_cvs[index];
1758 		if (cvs->cvs_buf == NULL) {
1759 			continue;
1760 		}
1761 
1762 		/* add to ID array */
1763 		usb_ac_add_unit_descriptor(uacp, cvs->cvs_buf,
1764 						cvs->cvs_buf_len);
1765 		rest -= cvs->cvs_buf[0];
1766 	}
1767 	rval = USB_SUCCESS;
1768 
1769 	usb_ac_setup_connections(uacp);
1770 
1771 	/* determine port types */
1772 	usb_ac_map_termtype_to_port(uacp, AUDIO_PLAY);
1773 	usb_ac_map_termtype_to_port(uacp, AUDIO_RECORD);
1774 
1775 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1776 	    "input port types=0x%x output port types =0x%x",
1777 	    uacp->usb_ac_input_ports, uacp->usb_ac_output_ports);
1778 
1779 
1780 	return (rval);
1781 }
1782 
1783 
1784 /*
1785  * usb_ac_setup_connections:
1786  *	build a matrix reflecting all connections
1787  */
1788 static void
1789 usb_ac_setup_connections(usb_ac_state_t *uacp)
1790 {
1791 	usb_ac_unit_list_t	*units = uacp->usb_ac_units;
1792 	uchar_t			*a, **p, i, unit;
1793 	size_t			a_len, p_len;
1794 
1795 	/* allocate array for unit types for quick reference */
1796 	uacp->usb_ac_unit_type = kmem_zalloc(uacp->usb_ac_max_unit,
1797 							KM_SLEEP);
1798 	/* allocate array for traversal path */
1799 	uacp->usb_ac_traverse_path = kmem_zalloc(uacp->usb_ac_max_unit,
1800 							KM_SLEEP);
1801 
1802 
1803 	/* allocate the connection matrix and set it up */
1804 	a_len = uacp->usb_ac_max_unit * uacp->usb_ac_max_unit;
1805 	p_len = uacp->usb_ac_max_unit * sizeof (uchar_t *);
1806 
1807 	/* trick to create a 2 dimensional array */
1808 	a = kmem_zalloc(a_len, KM_SLEEP);
1809 	p = kmem_zalloc(p_len, KM_SLEEP);
1810 	for (i = 0; i < uacp->usb_ac_max_unit; i++) {
1811 		p[i] = a + i * uacp->usb_ac_max_unit;
1812 	}
1813 	uacp->usb_ac_connections = p;
1814 	uacp->usb_ac_connections_len = p_len;
1815 	uacp->usb_ac_connections_a = a;
1816 	uacp->usb_ac_connections_a_len = a_len;
1817 
1818 	/* traverse all units and set connections */
1819 	for (unit = 0; unit < uacp->usb_ac_max_unit; unit++) {
1820 
1821 		USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1822 		    "traversing unit=0x%x type=0x%x",
1823 		    unit, units[unit].acu_type);
1824 
1825 		/* store type in the first unused column */
1826 		uacp->usb_ac_unit_type[unit] = units[unit].acu_type;
1827 
1828 		/* save the Unit ID in the unit it points to */
1829 		switch (units[unit].acu_type) {
1830 		case USB_AUDIO_FEATURE_UNIT:
1831 		{
1832 			usb_audio_feature_unit_descr1_t *d =
1833 					units[unit].acu_descriptor;
1834 
1835 			USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1836 			    "sourceID=0x%x type=0x%x", d->bSourceID,
1837 			    units[d->bSourceID].acu_type);
1838 
1839 			if (d->bSourceID != 0) {
1840 				ASSERT(p[unit][d->bSourceID] == B_FALSE);
1841 				p[unit][d->bSourceID] = B_TRUE;
1842 			}
1843 
1844 			break;
1845 		}
1846 		case USB_AUDIO_OUTPUT_TERMINAL:
1847 		{
1848 			usb_audio_output_term_descr_t *d =
1849 					units[unit].acu_descriptor;
1850 
1851 			USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1852 			    "sourceID=0x%x type=0x%x", d->bSourceID,
1853 			    units[d->bSourceID].acu_type);
1854 
1855 			if (d->bSourceID != 0) {
1856 				ASSERT(p[unit][d->bSourceID] == B_FALSE);
1857 				p[unit][d->bSourceID] = B_TRUE;
1858 			}
1859 
1860 			break;
1861 		}
1862 		case USB_AUDIO_MIXER_UNIT:
1863 		{
1864 			usb_audio_mixer_unit_descr1_t *d =
1865 					units[unit].acu_descriptor;
1866 			int n_sourceID = d->bNrInPins;
1867 			int id;
1868 
1869 			for (id = 0; id < n_sourceID; id++) {
1870 				USB_DPRINTF_L3(PRINT_MASK_ATTA,
1871 				    uacp->usb_ac_log_handle,
1872 				    "sourceID=0x%x type=0x%x c=%d",
1873 				    d->baSourceID[id],
1874 				    units[d->baSourceID[id]].acu_type,
1875 				    p[unit][d->baSourceID[id]]);
1876 
1877 				if (d->baSourceID[id] != 0) {
1878 					ASSERT(p[unit][d->baSourceID[id]] ==
1879 					    B_FALSE);
1880 					p[unit][d->baSourceID[id]] = B_TRUE;
1881 				}
1882 			}
1883 
1884 			break;
1885 		}
1886 		case USB_AUDIO_SELECTOR_UNIT:
1887 		{
1888 			usb_audio_selector_unit_descr1_t *d =
1889 					units[unit].acu_descriptor;
1890 			int n_sourceID = d->bNrInPins;
1891 			int id;
1892 
1893 			for (id = 0; id < n_sourceID; id++) {
1894 				USB_DPRINTF_L3(PRINT_MASK_ATTA,
1895 				    uacp->usb_ac_log_handle,
1896 				    "sourceID=0x%x type=0x%x",
1897 				    d->baSourceID[id],
1898 				    units[d->baSourceID[id]].acu_type);
1899 
1900 				if (d->baSourceID[id] != 0) {
1901 					ASSERT(p[unit][d->baSourceID[id]] ==
1902 					    B_FALSE);
1903 					p[unit][d->baSourceID[id]] = B_TRUE;
1904 				}
1905 			}
1906 
1907 			break;
1908 		}
1909 		case USB_AUDIO_PROCESSING_UNIT:
1910 		{
1911 			usb_audio_mixer_unit_descr1_t *d =
1912 					units[unit].acu_descriptor;
1913 			int n_sourceID = d->bNrInPins;
1914 			int id;
1915 
1916 			for (id = 0; id < n_sourceID; id++) {
1917 				USB_DPRINTF_L3(PRINT_MASK_ATTA,
1918 				    uacp->usb_ac_log_handle,
1919 				    "sourceID=0x%x type=0x%x",
1920 				    d->baSourceID[id],
1921 				    units[d->baSourceID[id]].acu_type);
1922 
1923 				if (d->baSourceID[id] != 0) {
1924 					ASSERT(p[unit][d->baSourceID[id]] ==
1925 					    B_FALSE);
1926 					p[unit][d->baSourceID[id]] = B_TRUE;
1927 				}
1928 			}
1929 
1930 			break;
1931 		}
1932 		case USB_AUDIO_EXTENSION_UNIT:
1933 		{
1934 			usb_audio_extension_unit_descr1_t *d =
1935 					units[unit].acu_descriptor;
1936 			int n_sourceID = d->bNrInPins;
1937 			int id;
1938 
1939 			for (id = 0; id < n_sourceID; id++) {
1940 				USB_DPRINTF_L3(PRINT_MASK_ATTA,
1941 				    uacp->usb_ac_log_handle,
1942 				    "sourceID=0x%x type=0x%x",
1943 				    d->baSourceID[id],
1944 				    units[d->baSourceID[id]].acu_type);
1945 
1946 				if (d->baSourceID[id] != 0) {
1947 					ASSERT(p[unit][d->baSourceID[id]] ==
1948 					    B_TRUE);
1949 					p[unit][d->baSourceID[id]] = B_FALSE;
1950 				}
1951 			}
1952 
1953 			break;
1954 		}
1955 		case USB_AUDIO_INPUT_TERMINAL:
1956 
1957 			break;
1958 		default:
1959 			/*
1960 			 * Ignore the rest because they are not support yet
1961 			 */
1962 			break;
1963 		}
1964 	}
1965 
1966 #ifdef DEBUG
1967 	/* display topology in log buffer */
1968 {
1969 	uint_t i, j, l;
1970 	char *buf;
1971 
1972 	l = uacp->usb_ac_max_unit * 5;
1973 
1974 	buf = kmem_alloc(l, KM_SLEEP);
1975 
1976 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
1977 	    "unit types:");
1978 
1979 	/* two	strings so they won't be replaced accidentily by tab */
1980 	(void) sprintf(&buf[0], "    ""    ");
1981 	for (i = 1; i < uacp->usb_ac_max_unit; i++) {
1982 		(void) sprintf(&buf[2 + (i*3)], "%02d ", i);
1983 	}
1984 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, buf);
1985 
1986 	(void) sprintf(&buf[0], "  +-------");
1987 	for (i = 1; i < uacp->usb_ac_max_unit; i++) {
1988 		(void) sprintf(&buf[5+((i-1)*3)], "---");
1989 	}
1990 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, buf);
1991 
1992 	(void) sprintf(&buf[0], "    ""    ");
1993 	for (i = 1; i < uacp->usb_ac_max_unit; i++) {
1994 		(void) sprintf(&buf[2 + (i*3)], "%02d ",
1995 			uacp->usb_ac_unit_type[i]);
1996 	}
1997 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, buf);
1998 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, " ");
1999 
2000 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
2001 	    "adjacency matrix:");
2002 	(void) sprintf(&buf[0], "    ""    ");
2003 	for (i = 1; i < uacp->usb_ac_max_unit; i++) {
2004 		(void) sprintf(&buf[2 + (i*3)], "%02d ", i);
2005 	}
2006 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, buf);
2007 
2008 	(void) sprintf(&buf[0], "  +-------");
2009 	for (i = 1; i < uacp->usb_ac_max_unit; i++) {
2010 		(void) sprintf(&buf[5+((i-1)*3)], "---");
2011 	}
2012 	USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, buf);
2013 
2014 	for (i = 1; i < uacp->usb_ac_max_unit; i++) {
2015 		(void) sprintf(&buf[0], "%02d| "" ", i);
2016 		for (j = 1; j < uacp->usb_ac_max_unit; j++) {
2017 			(void) sprintf(&buf[1+(j * 3)], "%2d ", p[i][j]);
2018 		}
2019 		USB_DPRINTF_L3(PRINT_MASK_ATTA, uacp->usb_ac_log_handle, buf);
2020 	}
2021 	kmem_free(buf, l);
2022 }
2023 #endif
2024 }
2025 
2026 
2027 /*
2028  * usb_ac_add_unit_descriptor:
2029  *	take the parsed descriptor in the buffer and store it in the ID unit
2030  *	array. we grow the unit array if the ID exceeds the current max
2031  */
2032 static void
2033 usb_ac_add_unit_descriptor(usb_ac_state_t *uacp, uchar_t *buffer,
2034 	size_t buflen)
2035 {
2036 	void	*descr;
2037 	int	len;
2038 	char	*format;
2039 	size_t	size;
2040 
2041 	USB_DPRINTF_L4(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
2042 	    "usb_ac_add_unit_descriptor: 0x%x 0x%x 0x%x",
2043 	    buffer[0], buffer[1], buffer[2]);
2044 
2045 	/* doubling the length should allow for padding */
2046 	len = 2 * buffer[0];
2047 	descr = kmem_zalloc(len, KM_SLEEP);
2048 
2049 	switch (buffer[2]) {
2050 	case USB_AUDIO_INPUT_TERMINAL:
2051 		format = CS_AC_INPUT_TERM_FORMAT;
2052 		size = CS_AC_INPUT_TERM_SIZE;
2053 
2054 		break;
2055 	case USB_AUDIO_OUTPUT_TERMINAL:
2056 		format = CS_AC_OUTPUT_TERM_FORMAT;
2057 		size = CS_AC_OUTPUT_TERM_SIZE;
2058 
2059 		break;
2060 	case USB_AUDIO_MIXER_UNIT:
2061 		format = CS_AC_MIXER_UNIT_DESCR1_FORMAT "255c";
2062 		size = CS_AC_MIXER_UNIT_DESCR1_SIZE + buffer[4] - 1;
2063 
2064 		break;
2065 	case USB_AUDIO_SELECTOR_UNIT:
2066 		format = CS_AC_SELECTOR_UNIT_DESCR1_FORMAT "255c";
2067 		size = CS_AC_SELECTOR_UNIT_DESCR1_SIZE + buffer[4] - 1;
2068 
2069 		break;
2070 	case USB_AUDIO_FEATURE_UNIT:
2071 		format = CS_AC_FEATURE_UNIT_FORMAT "255c";
2072 		size = CS_AC_FEATURE_UNIT_SIZE;
2073 
2074 		break;
2075 	case USB_AUDIO_PROCESSING_UNIT:
2076 		format = CS_AC_PROCESSING_UNIT_DESCR1_FORMAT "255c";
2077 		size = CS_AC_PROCESSING_UNIT_DESCR1_SIZE + buffer[6] - 1;
2078 
2079 		break;
2080 	case USB_AUDIO_EXTENSION_UNIT:
2081 		format = CS_AC_EXTENSION_UNIT_DESCR1_FORMAT "255c";
2082 		size = CS_AC_EXTENSION_UNIT_DESCR1_SIZE + buffer[6] - 1;
2083 
2084 		break;
2085 	default:
2086 		USB_DPRINTF_L2(PRINT_MASK_ATTA,
2087 		    uacp->usb_ac_log_handle,
2088 		    "unsupported descriptor %d", buffer[2]);
2089 
2090 		/* ignore this descriptor */
2091 		kmem_free(descr, len);
2092 
2093 		return;
2094 	}
2095 
2096 	if (usb_parse_data(format, buffer, buflen, descr, len) < size) {
2097 		/* ignore this descriptor */
2098 		kmem_free(descr, len);
2099 
2100 		return;
2101 	}
2102 
2103 	switch (buffer[2]) {
2104 	case USB_AUDIO_INPUT_TERMINAL:
2105 	{
2106 		usb_audio_input_term_descr_t *d =
2107 		    (usb_audio_input_term_descr_t *)descr;
2108 
2109 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2110 		    uacp->usb_ac_log_handle,
2111 		    "input term: type=0x%x sub=0x%x termid=0x%x\n\t"
2112 		    "termtype=0x%x assoc=0x%x #ch=%d "
2113 		    "chconf=0x%x ich=0x%x iterm=0x%x",
2114 		    d->bDescriptorType, d->bDescriptorSubType,
2115 		    d->bTerminalID, d->wTerminalType,
2116 		    d->bAssocTerminal, d->bNrChannels,
2117 		    d->wChannelConfig, d->iChannelNames,
2118 		    d->iTerminal);
2119 
2120 		usb_ac_alloc_unit(uacp, d->bTerminalID);
2121 		uacp->usb_ac_units[d->bTerminalID].acu_descriptor = descr;
2122 		uacp->usb_ac_units[d->bTerminalID].acu_type = buffer[2];
2123 		uacp->usb_ac_units[d->bTerminalID].acu_descr_length = len;
2124 
2125 		break;
2126 	}
2127 	case USB_AUDIO_OUTPUT_TERMINAL:
2128 	{
2129 		usb_audio_output_term_descr_t *d =
2130 		    (usb_audio_output_term_descr_t *)descr;
2131 
2132 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2133 		    uacp->usb_ac_log_handle,
2134 		    "output term: type=0x%x sub=0x%x termid=0x%x\n\t"
2135 		    "termtype=0x%x assoc=0x%x sourceID=0x%x iterm=0x%x",
2136 		    d->bDescriptorType, d->bDescriptorSubType,
2137 		    d->bTerminalID, d->wTerminalType,
2138 		    d->bAssocTerminal, d->bSourceID,
2139 		    d->iTerminal);
2140 
2141 		usb_ac_alloc_unit(uacp, d->bTerminalID);
2142 		uacp->usb_ac_units[d->bTerminalID].acu_descriptor = descr;
2143 		uacp->usb_ac_units[d->bTerminalID].acu_type = buffer[2];
2144 		uacp->usb_ac_units[d->bTerminalID].acu_descr_length = len;
2145 
2146 		break;
2147 	}
2148 	case USB_AUDIO_MIXER_UNIT:
2149 	{
2150 		usb_audio_mixer_unit_descr1_t *d =
2151 		    (usb_audio_mixer_unit_descr1_t *)descr;
2152 
2153 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2154 		    uacp->usb_ac_log_handle,
2155 		    "mixer unit: type=0x%x sub=0x%x unitid=0x%x\n\t"
2156 		    "#pins=0x%x sourceid[0]=0x%x",
2157 		    d->bDescriptorType, d->bDescriptorSubType,
2158 		    d->bUnitID, d->bNrInPins, d->baSourceID[0]);
2159 		usb_ac_alloc_unit(uacp, d->bUnitID);
2160 		uacp->usb_ac_units[d->bUnitID].acu_descriptor = descr;
2161 		uacp->usb_ac_units[d->bUnitID].acu_type = buffer[2];
2162 		uacp->usb_ac_units[d->bUnitID].acu_descr_length = len;
2163 
2164 		break;
2165 	}
2166 	case USB_AUDIO_SELECTOR_UNIT:
2167 	{
2168 		usb_audio_selector_unit_descr1_t *d =
2169 		    (usb_audio_selector_unit_descr1_t *)descr;
2170 
2171 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2172 		    uacp->usb_ac_log_handle,
2173 		    "selector unit: type=0x%x sub=0x%x unitid=0x%x\n\t"
2174 		    "#pins=0x%x sourceid[0]=0x%x",
2175 		    d->bDescriptorType, d->bDescriptorSubType,
2176 		    d->bUnitID, d->bNrInPins, d->baSourceID[0]);
2177 		usb_ac_alloc_unit(uacp, d->bUnitID);
2178 		uacp->usb_ac_units[d->bUnitID].acu_descriptor = descr;
2179 		uacp->usb_ac_units[d->bUnitID].acu_type = buffer[2];
2180 		uacp->usb_ac_units[d->bUnitID].acu_descr_length = len;
2181 
2182 		break;
2183 	}
2184 	case USB_AUDIO_FEATURE_UNIT:
2185 	{
2186 		usb_audio_feature_unit_descr1_t *d =
2187 		    (usb_audio_feature_unit_descr1_t *)descr;
2188 
2189 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2190 		    uacp->usb_ac_log_handle,
2191 		    "feature unit: type=0x%x sub=0x%x unitid=0x%x\n\t"
2192 		    "sourceid=0x%x size=0x%x "
2193 		    "controls=0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
2194 		    d->bDescriptorType, d->bDescriptorSubType,
2195 		    d->bUnitID, d->bSourceID,
2196 		    d->bControlSize, d->bmaControls[0],
2197 		    d->bmaControls[1], d->bmaControls[2], d->bmaControls[3],
2198 		    d->bmaControls[4], d->bmaControls[5], d->bmaControls[6]);
2199 
2200 		usb_ac_alloc_unit(uacp, d->bUnitID);
2201 		uacp->usb_ac_units[d->bUnitID].acu_descriptor = descr;
2202 		uacp->usb_ac_units[d->bUnitID].acu_type = buffer[2];
2203 		uacp->usb_ac_units[d->bUnitID].acu_descr_length = len;
2204 
2205 		break;
2206 	}
2207 	case USB_AUDIO_PROCESSING_UNIT:
2208 	{
2209 		usb_audio_processing_unit_descr1_t *d =
2210 		    (usb_audio_processing_unit_descr1_t *)descr;
2211 
2212 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2213 		    uacp->usb_ac_log_handle,
2214 		    "processing unit: type=0x%x sub=0x%x unitid=0x%x\n\t"
2215 		    "#pins=0x%x sourceid[0]=0x%x",
2216 		    d->bDescriptorType, d->bDescriptorSubType,
2217 		    d->bUnitID, d->bNrInPins, d->baSourceID[0]);
2218 		usb_ac_alloc_unit(uacp, d->bUnitID);
2219 		uacp->usb_ac_units[d->bUnitID].acu_descriptor = descr;
2220 		uacp->usb_ac_units[d->bUnitID].acu_type = buffer[2];
2221 		uacp->usb_ac_units[d->bUnitID].acu_descr_length = len;
2222 
2223 		break;
2224 	}
2225 	case USB_AUDIO_EXTENSION_UNIT:
2226 	{
2227 		usb_audio_extension_unit_descr1_t *d =
2228 		    (usb_audio_extension_unit_descr1_t *)descr;
2229 
2230 		USB_DPRINTF_L3(PRINT_MASK_ATTA,
2231 		    uacp->usb_ac_log_handle,
2232 		    "mixer unit: type=0x%x sub=0x%x unitid=0x%x\n\t"
2233 		    "#pins=0x%x sourceid[0]=0x%x",
2234 		    d->bDescriptorType, d->bDescriptorSubType,
2235 		    d->bUnitID, d->bNrInPins, d->baSourceID[0]);
2236 		usb_ac_alloc_unit(uacp, d->bUnitID);
2237 		uacp->usb_ac_units[d->bUnitID].acu_descriptor = descr;
2238 		uacp->usb_ac_units[d->bUnitID].acu_type = buffer[2];
2239 		uacp->usb_ac_units[d->bUnitID].acu_descr_length = len;
2240 
2241 		break;
2242 	}
2243 	default:
2244 		break;
2245 	}
2246 }
2247 
2248 
2249 /*
2250  * usb_ac_alloc_unit:
2251  *	check if the unit ID is less than max_unit in which case no
2252  *	extra entries are needed. If more entries are needed, copy over
2253  *	the existing array into a new larger array
2254  */
2255 static void
2256 usb_ac_alloc_unit(usb_ac_state_t *uacp, uint_t unit)
2257 {
2258 	usb_ac_unit_list_t *old = NULL;
2259 	uint_t	max_unit;
2260 
2261 	USB_DPRINTF_L4(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
2262 	    "usb_ac_alloc_unit: unit=%d", unit);
2263 
2264 	if (uacp->usb_ac_units) {
2265 		if (unit < uacp->usb_ac_max_unit) {
2266 			/* existing array is big enough */
2267 
2268 			return;
2269 		}
2270 		old = uacp->usb_ac_units;
2271 		max_unit = uacp->usb_ac_max_unit;
2272 	}
2273 
2274 	/* allocate two extra ones */
2275 	unit += 2;
2276 	uacp->usb_ac_max_unit = unit;
2277 	uacp->usb_ac_units = kmem_zalloc(unit *
2278 				sizeof (usb_ac_unit_list_t), KM_SLEEP);
2279 
2280 	if (old) {
2281 		size_t len = max_unit * sizeof (usb_ac_unit_list_t);
2282 		bcopy(old, uacp->usb_ac_units, len);
2283 
2284 		kmem_free(old, len);
2285 	}
2286 }
2287 
2288 
2289 /*
2290  * usb_ac_free_all_units:
2291  *	free the entire unit list
2292  */
2293 static void
2294 usb_ac_free_all_units(usb_ac_state_t *uacp)
2295 {
2296 	uint_t	unit;
2297 	usb_ac_unit_list_t *unitp;
2298 
2299 	if (uacp->usb_ac_units == NULL) {
2300 
2301 		return;
2302 	}
2303 
2304 	USB_DPRINTF_L4(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
2305 	    "usb_ac_alloc_unit: max_unit=%d", uacp->usb_ac_max_unit);
2306 
2307 	for (unit = 0; unit < uacp->usb_ac_max_unit; unit++) {
2308 		unitp = &uacp->usb_ac_units[unit];
2309 		if (unitp) {
2310 			if (unitp->acu_descriptor) {
2311 				kmem_free(unitp->acu_descriptor,
2312 					unitp->acu_descr_length);
2313 			}
2314 		}
2315 	}
2316 
2317 	kmem_free(uacp->usb_ac_units, uacp->usb_ac_max_unit *
2318 					sizeof (usb_ac_unit_list_t));
2319 }
2320 
2321 
2322 /*
2323  * usb_ac_lookup_port_type:
2324  *	map term type to port type
2325  *	default just return LINE_IN + LINE_OUT
2326  */
2327 static int
2328 usb_ac_lookup_port_type(ushort_t termtype)
2329 {
2330 	uint_t i;
2331 
2332 	for (i = 0; ; i++) {
2333 		if (usb_ac_term_type_map[i].term_type == 0) {
2334 
2335 			break;
2336 		}
2337 
2338 		if (usb_ac_term_type_map[i].term_type == termtype) {
2339 
2340 			return (usb_ac_term_type_map[i].port_type);
2341 		}
2342 	}
2343 
2344 	return (AUDIO_LINE_IN|AUDIO_LINE_OUT);
2345 }
2346 
2347 
2348 /*
2349  * usb_ac_update_port:
2350  *	called for each terminal
2351  */
2352 /*ARGSUSED*/
2353 static int
2354 usb_ac_update_port(usb_ac_state_t *uacp, uint_t id,
2355     uint_t dir, uint_t channel, uint_t control, uint_t arg1, uint_t *depth)
2356 {
2357 	if (dir & AUDIO_PLAY) {
2358 		usb_audio_output_term_descr_t *d =
2359 		    (usb_audio_output_term_descr_t *)
2360 		    uacp->usb_ac_units[id].acu_descriptor;
2361 		uint_t port_type =
2362 			usb_ac_lookup_port_type(d->wTerminalType);
2363 
2364 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2365 		    "usb_ac_update_port: dir=%d type=0x%x port type=%d",
2366 		    dir, d->wTerminalType, port_type);
2367 
2368 		uacp->usb_ac_output_ports |= port_type;
2369 		uacp->usb_ac_output_ports &= ~AUDIO_LINE_IN;
2370 	} else {
2371 		usb_audio_output_term_descr_t *d =
2372 			(usb_audio_output_term_descr_t *)
2373 			uacp->usb_ac_units[id].acu_descriptor;
2374 		uint_t port_type =
2375 			usb_ac_lookup_port_type(d->wTerminalType);
2376 
2377 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2378 		    "usb_ac_update_port: dir=%d type=0x%x port type=%d",
2379 		    dir, d->wTerminalType, port_type);
2380 
2381 		uacp->usb_ac_input_ports |=
2382 			usb_ac_lookup_port_type(d->wTerminalType);
2383 		uacp->usb_ac_input_ports &= ~AUDIO_LINE_OUT;
2384 	}
2385 
2386 	return (USB_SUCCESS);
2387 }
2388 
2389 
2390 /*
2391  * usb_ac_map_termtype_to_port:
2392  *	starting from a streaming termtype find all
2393  *	input or output terminals and OR into uacp->usb_ac_input_ports
2394  *	or uacp->usb_ac_output_ports;
2395  */
2396 static void
2397 usb_ac_map_termtype_to_port(usb_ac_state_t *uacp, uint_t dir)
2398 {
2399 	uint_t count = 0;
2400 	uint_t depth = 0;
2401 	uint_t search_type = (dir & AUDIO_PLAY) ?
2402 	    USB_AUDIO_OUTPUT_TERMINAL : USB_AUDIO_INPUT_TERMINAL;
2403 
2404 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2405 	    "usb_ac_map_term_to_port: dir=%d", dir);
2406 
2407 	(void) usb_ac_traverse_all_units(uacp, dir, search_type, 0,
2408 	    0, USB_AC_FIND_ALL, &count, 0, &depth, usb_ac_update_port);
2409 
2410 	ASSERT(depth == 0);
2411 }
2412 
2413 
2414 /*
2415  * usb_ac_set_port:
2416  *	find a selector port (record side only) and set the
2417  *	input to the matching pin
2418  */
2419 static uint_t
2420 usb_ac_set_port(usb_ac_state_t *uacp, uint_t dir, uint_t port)
2421 {
2422 	uint_t count = 0;
2423 	uint_t id;
2424 	uint_t depth = 0;
2425 
2426 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2427 	    "usb_ac_set_port: dir=%d port=%d", dir, port);
2428 
2429 	/* we only support the selector for the record side */
2430 	if (dir & AUDIO_RECORD) {
2431 		id = usb_ac_traverse_all_units(uacp, dir,
2432 		    USB_AUDIO_SELECTOR_UNIT, 0,
2433 		    0, USB_AC_FIND_ONE, &count, port, &depth,
2434 		    usb_ac_set_selector);
2435 
2436 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2437 		    "usb_ac_set_port: id=%d count=%d port=%d",
2438 		    id, count, port);
2439 
2440 		ASSERT(depth == 0);
2441 	}
2442 
2443 	return (USB_SUCCESS);
2444 }
2445 
2446 
2447 /*
2448  * usb_ac_match_port:
2449  *	given the requested port type, find a correspondig term type
2450  *	Called from usb_ac_traverse_all_units()
2451  */
2452 /*ARGSUSED*/
2453 static int
2454 usb_ac_match_port(usb_ac_state_t *uacp, uint_t id,
2455     uint_t dir, uint_t channel, uint_t control, uint_t arg1, uint_t *depth)
2456 {
2457 	uint_t port_type;
2458 
2459 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2460 	    "usb_ac_match_port: id=%d dir=%d port=%d",
2461 	    id, dir, arg1);
2462 
2463 	if (dir & AUDIO_PLAY) {
2464 		usb_audio_output_term_descr_t *d =
2465 		    (usb_audio_output_term_descr_t *)
2466 		    uacp->usb_ac_units[id].acu_descriptor;
2467 		port_type = usb_ac_lookup_port_type(d->wTerminalType);
2468 
2469 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2470 		    "usb_ac_match_port: "
2471 		    "dir=%d type=0x%x port_type=%d port=%d",
2472 		    dir, d->wTerminalType, port_type, arg1);
2473 	} else {
2474 		usb_audio_output_term_descr_t *d =
2475 			(usb_audio_output_term_descr_t *)
2476 			uacp->usb_ac_units[id].acu_descriptor;
2477 		port_type = usb_ac_lookup_port_type(d->wTerminalType);
2478 
2479 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2480 		    "usb_ac_match_port: "
2481 		    "dir=%d type=0x%x port_type=%d port=%d",
2482 		    dir, d->wTerminalType, port_type, arg1);
2483 	}
2484 
2485 	return ((port_type & arg1) ? USB_SUCCESS : USB_FAILURE);
2486 }
2487 
2488 
2489 /*
2490  * usb_ac_set_selector:
2491  *	Called from usb_ac_traverse_all_units()
2492  *	Find the correct pin and set selector to this pin
2493  */
2494 /*ARGSUSED*/
2495 static int
2496 usb_ac_set_selector(usb_ac_state_t *uacp, uint_t id,
2497     uint_t dir, uint_t channel, uint_t control, uint_t arg1, uint_t *depth)
2498 {
2499 	uint_t count = 0;
2500 	uint_t unit = USB_AC_ID_NONE;
2501 	uint_t pin;
2502 	uint_t search_target =
2503 	    (dir & AUDIO_PLAY) ? USB_AUDIO_OUTPUT_TERMINAL :
2504 				USB_AUDIO_INPUT_TERMINAL;
2505 	usb_audio_selector_unit_descr1_t *d =
2506 		    (usb_audio_selector_unit_descr1_t *)
2507 		    uacp->usb_ac_units[id].acu_descriptor;
2508 	int n_sourceID = d->bNrInPins;
2509 	int rval = USB_FAILURE;
2510 
2511 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2512 	    "usb_ac_set_selector: id=%d dir=%d port=%d",
2513 	    id, dir, arg1);
2514 
2515 	/*
2516 	 * for each pin, find a term type that matches the
2517 	 * requested port type
2518 	 */
2519 	for (pin = 0; pin < n_sourceID; pin++) {
2520 		if (d->baSourceID[pin] == 0) {
2521 
2522 			break;
2523 		}
2524 		unit = d->baSourceID[pin];
2525 
2526 		USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2527 		    "usb_ac_set_selector: pin=%d unit=%d", pin, unit);
2528 
2529 		if (uacp->usb_ac_unit_type[unit] == search_target) {
2530 			if (usb_ac_match_port(uacp, unit, dir, channel,
2531 			    control, arg1, depth) == USB_SUCCESS) {
2532 
2533 				break;
2534 			} else {
2535 				unit = USB_AC_ID_NONE;
2536 
2537 				continue;
2538 			}
2539 		}
2540 
2541 		/* find units connected to this unit */
2542 		unit = usb_ac_traverse_connections(uacp, unit,
2543 				dir, search_target, channel, control,
2544 				USB_AC_FIND_ONE, &count, arg1, depth,
2545 				usb_ac_match_port);
2546 
2547 		if (unit != USB_AC_ID_NONE) {
2548 
2549 			break;
2550 		}
2551 	}
2552 
2553 
2554 	if (unit != USB_AC_ID_NONE) {
2555 		mblk_t		*data;
2556 		usb_cr_t	cr;
2557 		usb_cb_flags_t	cb_flags;
2558 
2559 		USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2560 		    "usb_ac_set_selector: found id=%d at pin %d", unit, pin);
2561 
2562 		mutex_exit(&uacp->usb_ac_mutex);
2563 
2564 		data = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL);
2565 
2566 		/* pins are 1-based */
2567 		*(data->b_rptr) = (char)++pin;
2568 
2569 		if (usb_pipe_sync_ctrl_xfer(
2570 		    uacp->usb_ac_dip,
2571 		    uacp->usb_ac_default_ph,
2572 		    USB_DEV_REQ_HOST_TO_DEV |
2573 		    USB_DEV_REQ_TYPE_CLASS |
2574 		    USB_DEV_REQ_RCPT_IF,	/* bmRequestType */
2575 		    USB_AUDIO_SET_CUR,		/* bRequest */
2576 		    0,				/* wValue */
2577 						/* feature unit and id */
2578 		    (id << 8)| uacp->usb_ac_ifno, /* wIndex */
2579 		    1,				/* wLength */
2580 		    &data,
2581 		    USB_ATTRS_NONE,
2582 		    &cr, &cb_flags,
2583 		    USB_FLAGS_SLEEP) == USB_SUCCESS) {
2584 			USB_DPRINTF_L3(PRINT_MASK_ALL,
2585 			    uacp->usb_ac_log_handle,
2586 			    "set current selection: %d", *data->b_rptr);
2587 
2588 			rval = USB_SUCCESS;
2589 		} else {
2590 			USB_DPRINTF_L2(PRINT_MASK_ALL,
2591 			    uacp->usb_ac_log_handle,
2592 			    "set current pin selection failed");
2593 		}
2594 		freemsg(data);
2595 
2596 		mutex_enter(&uacp->usb_ac_mutex);
2597 	} else {
2598 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2599 		    "usb_ac_set_selector: nothing found");
2600 	}
2601 
2602 	return (rval);
2603 }
2604 
2605 
2606 /*
2607  * usb_ac_set_control:
2608  *	apply func to all units of search_target type for both the
2609  *	requested channel and master channel
2610  */
2611 static uint_t
2612 usb_ac_set_control(usb_ac_state_t *uacp, uint_t dir, uint_t search_target,
2613 	uint_t channel, uint_t control, uint_t all_or_one,
2614 	uint_t *count, uint_t arg1,
2615 	int (*func)(usb_ac_state_t *uacp, uint_t unit, uint_t dir,
2616 		uint_t channel, uint_t control, uint_t arg1, uint_t *depth))
2617 {
2618 	uint_t id;
2619 	uint_t depth = 0;
2620 
2621 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2622 	    "usb_ac_set_control: dir=%d type=%d ch=%d cntl=%d",
2623 	    dir, search_target, channel, control);
2624 
2625 
2626 	id = usb_ac_traverse_all_units(uacp, dir, search_target, channel,
2627 	    control, all_or_one, count, arg1, &depth, func);
2628 
2629 	if ((channel != 0) &&
2630 	    (((id == USB_AC_ID_NONE) && (all_or_one == USB_AC_FIND_ONE)) ||
2631 	    (all_or_one == USB_AC_FIND_ALL)))  {
2632 		/* try master channel */
2633 		channel = 0;
2634 		id = usb_ac_traverse_all_units(uacp, dir, search_target,
2635 			channel, control, all_or_one, count, arg1,
2636 			&depth, func);
2637 	}
2638 
2639 	ASSERT(depth == 0);
2640 
2641 	return (id);
2642 }
2643 
2644 
2645 /*
2646  * usb_ac_traverse_all_units:
2647  *	traverse all units starting with all IT or OT depending on direction.
2648  *	If no unit is found for the particular channel, try master channel
2649  *	If a matching unit is found, apply the function passed by
2650  *	the caller
2651  */
2652 static uint_t
2653 usb_ac_traverse_all_units(usb_ac_state_t *uacp, uint_t dir,
2654 	uint_t search_target, uint_t channel, uint_t control,
2655 	uint_t all_or_one, uint_t *count, uint_t arg1, uint_t *depth,
2656 	int (*func)(usb_ac_state_t *uacp, uint_t unit, uint_t dir,
2657 		uint_t channel, uint_t control, uint_t arg1, uint_t *depth))
2658 {
2659 	uint_t unit, start_type, id;
2660 
2661 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2662 	    "usb_ac_traverse_all_units: "
2663 	    "dir=%d type=%d ch=%d cntl=%d all=%d depth=%d",
2664 	    dir, search_target, channel, control, all_or_one, *depth);
2665 
2666 	start_type = (dir & AUDIO_PLAY) ? USB_AUDIO_INPUT_TERMINAL :
2667 				USB_AUDIO_OUTPUT_TERMINAL;
2668 
2669 	/* keep track of recursion */
2670 	if ((*depth)++ > USB_AC_MAX_DEPTH) {
2671 		USB_DPRINTF_L1(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2672 		    "Unit topology too complex, giving up");
2673 
2674 		return (USB_AC_ID_NONE);
2675 	}
2676 
2677 	for (unit = 1; unit < uacp->usb_ac_max_unit; unit++) {
2678 		/* is this an IT or OT? */
2679 		if (uacp->usb_ac_unit_type[unit] != start_type) {
2680 
2681 			continue;
2682 		}
2683 
2684 		/* start at streaming term types */
2685 		if (dir & AUDIO_PLAY) {
2686 			usb_audio_input_term_descr_t *d =
2687 			    uacp->usb_ac_units[unit].acu_descriptor;
2688 			if (d->wTerminalType !=
2689 			    USB_AUDIO_TERM_TYPE_STREAMING) {
2690 
2691 				continue;
2692 			}
2693 		} else {
2694 			usb_audio_output_term_descr_t *d =
2695 			    uacp->usb_ac_units[unit].acu_descriptor;
2696 			if (d->wTerminalType !=
2697 			    USB_AUDIO_TERM_TYPE_STREAMING) {
2698 
2699 				continue;
2700 			}
2701 		}
2702 
2703 		/* find units connected to this unit */
2704 		id = usb_ac_traverse_connections(uacp, unit, dir,
2705 		    search_target, channel, control, all_or_one, count,
2706 		    arg1, depth, func);
2707 
2708 		if ((all_or_one == USB_AC_FIND_ONE) &&
2709 		    (id != USB_AC_ID_NONE)) {
2710 			unit = id;
2711 
2712 			break;
2713 		}
2714 	}
2715 
2716 	(*depth)--;
2717 
2718 	return	((unit < uacp->usb_ac_max_unit) ? unit : USB_AC_ID_NONE);
2719 }
2720 
2721 
2722 /*
2723  * usb_ac_set_monitor_gain_control:
2724  *	search for a feature unit between output terminal (OT) and
2725  *	input terminal. We are looking for a path between
2726  *	for example a microphone and a speaker through a feature unit
2727  *	and mixer
2728  */
2729 static uint_t
2730 usb_ac_set_monitor_gain_control(usb_ac_state_t *uacp, uint_t dir,
2731 	uint_t search_target, uint_t channel, uint_t control,
2732 	uint_t all_or_one, uint_t *count, uint_t arg1,
2733 	int (*func)(usb_ac_state_t *uacp, uint_t unit, uint_t dir,
2734 		uint_t channel, uint_t control, uint_t arg1, uint_t *depth))
2735 {
2736 	uint_t unit, id;
2737 	uint_t depth = 0;
2738 
2739 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2740 	    "usb_ac_set_monitor_gain_control: dir=%d type=%d ch=%d cntl=%d",
2741 	    dir, search_target, channel, control);
2742 
2743 	for (unit = 1; unit < uacp->usb_ac_max_unit; unit++) {
2744 		usb_audio_output_term_descr_t *d =
2745 			    uacp->usb_ac_units[unit].acu_descriptor;
2746 
2747 		/* is this an OT and not stream type? */
2748 		if ((uacp->usb_ac_unit_type[unit] ==
2749 		    USB_AUDIO_OUTPUT_TERMINAL) &&
2750 		    (d->wTerminalType != USB_AUDIO_TERM_TYPE_STREAMING)) {
2751 
2752 			/* find units connected to this unit */
2753 			id = usb_ac_traverse_connections(uacp, unit, dir,
2754 			    search_target, channel, control, all_or_one, count,
2755 			    arg1, &depth, func);
2756 
2757 			if ((all_or_one == USB_AC_FIND_ONE) &&
2758 			    (id != USB_AC_ID_NONE)) {
2759 
2760 				break;
2761 			}
2762 		}
2763 	}
2764 
2765 	ASSERT(depth == 0);
2766 
2767 	return (id);
2768 }
2769 
2770 
2771 /*
2772  * usb_ac_push/pop_unit
2773  *	add/remove unit ID to the traverse path
2774  */
2775 static void
2776 usb_ac_push_unit_id(usb_ac_state_t *uacp, uint_t unit)
2777 {
2778 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2779 	    "usb_ac_push_unit_id: pushing %d at %d", unit,
2780 	    uacp->usb_ac_traverse_path_index);
2781 
2782 	uacp->usb_ac_traverse_path[uacp->usb_ac_traverse_path_index++] = unit;
2783 	ASSERT(uacp->usb_ac_traverse_path_index < uacp->usb_ac_max_unit);
2784 }
2785 
2786 
2787 static void
2788 usb_ac_pop_unit_id(usb_ac_state_t *uacp, uint_t unit)
2789 {
2790 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2791 	    "usb_ac_push_unit_id: popping %d at %d", unit,
2792 	    uacp->usb_ac_traverse_path_index);
2793 
2794 	uacp->usb_ac_traverse_path[uacp->usb_ac_traverse_path_index--] = 0;
2795 }
2796 
2797 
2798 /*
2799  * usb_ac_show_traverse_path:
2800  *	display entire path, just for debugging
2801  */
2802 static void
2803 usb_ac_show_traverse_path(usb_ac_state_t *uacp)
2804 {
2805 	int i;
2806 
2807 	for (i = 0; i < uacp->usb_ac_traverse_path_index; i++) {
2808 		USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2809 		    "traverse path %d: unit=%d type=%d",
2810 		i, uacp->usb_ac_traverse_path[i],
2811 		uacp->usb_ac_unit_type[uacp->usb_ac_traverse_path[i]]);
2812 	}
2813 }
2814 
2815 
2816 /*
2817  * usb_ac_check_path:
2818  *	check for a specified type in the traverse path
2819  */
2820 static int
2821 usb_ac_check_path(usb_ac_state_t *uacp, uint_t type)
2822 {
2823 	int i;
2824 
2825 	for (i = 0; i < uacp->usb_ac_traverse_path_index; i++) {
2826 		uint_t unit = uacp->usb_ac_traverse_path[i];
2827 
2828 		if (uacp->usb_ac_unit_type[unit] == type) {
2829 
2830 			return (USB_SUCCESS);
2831 		}
2832 	}
2833 
2834 	return (USB_FAILURE);
2835 }
2836 
2837 
2838 /*
2839  * usb_ac_traverse_connections:
2840  *	traverse all units and for each unit with the right type, call
2841  *	func. If the func returns a success and search == USB_AC_FIND_ONE,
2842  *	we are done. If all is set then we continue until we terminate
2843  *	and input or output terminal.
2844  *	For audio play, we traverse columns starting from an input terminal
2845  *	to an output terminal while for record we traverse rows from output
2846  *	terminal to input terminal.
2847  */
2848 static uint_t
2849 usb_ac_traverse_connections(usb_ac_state_t *uacp, uint_t start_unit, uint_t dir,
2850 	uint_t search_target, uint_t channel, uint_t control,
2851 	uint_t all_or_one, uint_t *count, uint_t arg1, uint_t *depth,
2852 	int (*func)(usb_ac_state_t *uacp, uint_t unit, uint_t dir,
2853 		uint_t channel, uint_t control, uint_t arg1, uint_t *depth))
2854 {
2855 	uint_t unit, id;
2856 	uint_t done = (dir & AUDIO_PLAY) ? USB_AUDIO_OUTPUT_TERMINAL :
2857 				USB_AUDIO_INPUT_TERMINAL;
2858 
2859 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2860 	    "usb_ac_traverse_connections: "
2861 	    "start=%d dir=%d type=%d ch=%d cntl=%d all=%d depth=%d",
2862 	    start_unit, dir, search_target, channel, control,
2863 	    all_or_one, *depth);
2864 
2865 	/* keep track of recursion depth */
2866 	if ((*depth)++ > USB_AC_MAX_DEPTH) {
2867 		USB_DPRINTF_L1(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2868 		    "Unit topology too complex, giving up");
2869 
2870 		return (USB_AC_ID_NONE);
2871 	}
2872 
2873 	usb_ac_push_unit_id(uacp, start_unit);
2874 
2875 	for (unit = 1; unit < uacp->usb_ac_max_unit; unit++) {
2876 		uint_t entry = (dir & AUDIO_PLAY) ?
2877 			uacp->usb_ac_connections[unit][start_unit] :
2878 			uacp->usb_ac_connections[start_unit][unit];
2879 
2880 		if (entry) {
2881 			USB_DPRINTF_L3(PRINT_MASK_ALL,
2882 			    uacp->usb_ac_log_handle,
2883 			    "start=%d unit=%d entry=%d type=%d "
2884 			    "done=%d found=%d",
2885 			    start_unit, unit, entry, search_target, done,
2886 			    uacp->usb_ac_unit_type[unit]);
2887 
2888 			/* did we find a matching type? */
2889 			if (uacp->usb_ac_unit_type[unit] == search_target) {
2890 				USB_DPRINTF_L3(PRINT_MASK_ALL,
2891 				    uacp->usb_ac_log_handle,
2892 				    "match: dir=%d unit=%d type=%d",
2893 				    dir, unit, search_target);
2894 
2895 				/* yes, no apply function to this unit */
2896 				if (func(uacp, unit, dir, channel,
2897 				    control, arg1, depth) == USB_SUCCESS) {
2898 					(*count)++;
2899 
2900 					USB_DPRINTF_L3(PRINT_MASK_ALL,
2901 					    uacp->usb_ac_log_handle,
2902 					    "func returned success, "
2903 					    "unit=%d all=%d", unit,
2904 					    all_or_one);
2905 
2906 					/* are we done? */
2907 					if (all_or_one == USB_AC_FIND_ONE) {
2908 
2909 						break;
2910 					}
2911 				}
2912 			}
2913 
2914 			/* did we find the terminating unit */
2915 			if (uacp->usb_ac_unit_type[unit] == done) {
2916 
2917 				continue;
2918 			}
2919 			id = usb_ac_traverse_connections(uacp, unit, dir,
2920 				search_target, channel, control,
2921 				all_or_one, count, arg1, depth, func);
2922 			if ((id != USB_AC_ID_NONE) &&
2923 			    (all_or_one == USB_AC_FIND_ONE)) {
2924 				unit = id;
2925 
2926 				break;
2927 			}
2928 		}
2929 	}
2930 
2931 	(*depth)--;
2932 	usb_ac_pop_unit_id(uacp, start_unit);
2933 
2934 	return	((unit < uacp->usb_ac_max_unit) ? unit : USB_AC_ID_NONE);
2935 }
2936 
2937 
2938 /*
2939  * Event Management
2940  *
2941  * usb_ac_disconnect_event_cb:
2942  *	The device has been disconnected. we either wait for
2943  *	detach or a reconnect event.
2944  */
2945 static int
2946 usb_ac_disconnect_event_cb(dev_info_t *dip)
2947 {
2948 	usb_ac_state_t *uacp = (usb_ac_state_t *)ddi_get_soft_state(
2949 	    usb_ac_statep, ddi_get_instance(dip));
2950 
2951 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, uacp->usb_ac_log_handle,
2952 	    "usb_ac_disconnect_event_cb: dip=0x%p", dip);
2953 
2954 	usb_ac_serialize_access(uacp);
2955 
2956 	/* setting to disconnect state will prevent replumbing */
2957 	mutex_enter(&uacp->usb_ac_mutex);
2958 	uacp->usb_ac_dev_state = USB_DEV_DISCONNECTED;
2959 
2960 	if (uacp->usb_ac_busy_count) {
2961 		USB_DPRINTF_L0(PRINT_MASK_EVENTS, uacp->usb_ac_log_handle,
2962 		    "device was disconnected while busy. "
2963 		    "Data may have been lost");
2964 	}
2965 	mutex_exit(&uacp->usb_ac_mutex);
2966 
2967 	USB_DPRINTF_L3(PRINT_MASK_EVENTS, uacp->usb_ac_log_handle,
2968 	    "usb_ac_disconnect_event_cb: done");
2969 
2970 	usb_ac_release_access(uacp);
2971 
2972 	return (USB_SUCCESS);
2973 }
2974 
2975 
2976 /*
2977  * usb_ac_cpr_suspend:
2978  */
2979 static int
2980 usb_ac_cpr_suspend(dev_info_t *dip)
2981 {
2982 	usb_ac_state_t *uacp = (usb_ac_state_t *)ddi_get_soft_state(
2983 	    usb_ac_statep, ddi_get_instance(dip));
2984 
2985 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2986 	    "usb_ac_cpr_suspend: Begin");
2987 
2988 	mutex_enter(&uacp->usb_ac_mutex);
2989 	uacp->usb_ac_dev_state = USB_DEV_SUSPENDED;
2990 	mutex_exit(&uacp->usb_ac_mutex);
2991 
2992 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
2993 	    "usb_ac_cpr_suspend: End");
2994 
2995 	return (USB_SUCCESS);
2996 }
2997 
2998 
2999 
3000 /*
3001  * usb_ac_reconnect_event_cb:
3002  *	The device was disconnected but this instance not detached, probably
3003  *	because the device was busy.
3004  *	if the same device, continue with restoring state
3005  *	We should either be in the unplumbed state or the plumbed open
3006  *	state.
3007  */
3008 static int
3009 usb_ac_reconnect_event_cb(dev_info_t *dip)
3010 {
3011 	usb_ac_state_t *uacp = (usb_ac_state_t *)ddi_get_soft_state(
3012 	    usb_ac_statep, ddi_get_instance(dip));
3013 
3014 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, uacp->usb_ac_log_handle,
3015 	    "usb_ac_reconnect_event_cb: dip=0x%p", dip);
3016 
3017 	mutex_enter(&uacp->usb_ac_mutex);
3018 	mutex_exit(&uacp->usb_ac_mutex);
3019 
3020 	usb_ac_serialize_access(uacp);
3021 
3022 	/* check the plumbing state */
3023 	mutex_enter(&uacp->usb_ac_mutex);
3024 	uacp->usb_ac_busy_count++;
3025 	if (uacp->usb_ac_plumbing_state ==
3026 	    USB_AC_STATE_PLUMBED) {
3027 		mutex_exit(&uacp->usb_ac_mutex);
3028 		usb_ac_restore_device_state(dip, uacp);
3029 		mutex_enter(&uacp->usb_ac_mutex);
3030 	}
3031 	uacp->usb_ac_busy_count--;
3032 
3033 	if (uacp->usb_ac_busy_count) {
3034 		USB_DPRINTF_L0(PRINT_MASK_EVENTS, uacp->usb_ac_log_handle,
3035 		    "busy device has been reconnected");
3036 	}
3037 	mutex_exit(&uacp->usb_ac_mutex);
3038 
3039 	usb_ac_release_access(uacp);
3040 
3041 	return (USB_SUCCESS);
3042 }
3043 
3044 
3045 /*
3046  * usb_ac_cpr_resume:
3047  *	Restore device state
3048  */
3049 static void
3050 usb_ac_cpr_resume(dev_info_t *dip)
3051 {
3052 	usb_ac_state_t *uacp = (usb_ac_state_t *)ddi_get_soft_state(
3053 	    usb_ac_statep, ddi_get_instance(dip));
3054 
3055 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, uacp->usb_ac_log_handle,
3056 	    "usb_ac_cpr_resume");
3057 
3058 	usb_ac_serialize_access(uacp);
3059 
3060 	usb_ac_restore_device_state(dip, uacp);
3061 
3062 	usb_ac_release_access(uacp);
3063 }
3064 
3065 
3066 /*
3067  * usb_ac_restore_device_state:
3068  *	Set original configuration of the device
3069  *	enable wrq - this starts new transactions on the control pipe
3070  */
3071 static void
3072 usb_ac_restore_device_state(dev_info_t *dip, usb_ac_state_t *uacp)
3073 {
3074 	usb_ac_power_t	*uacpm;
3075 	int	rval;
3076 
3077 	USB_DPRINTF_L4(PRINT_MASK_ATTA, uacp->usb_ac_log_handle,
3078 	    "usb_ac_restore_device_state:");
3079 
3080 	usb_ac_pm_busy_component(uacp);
3081 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
3082 
3083 	/* Check if we are talking to the same device */
3084 	if (usb_check_same_device(dip, uacp->usb_ac_log_handle,
3085 	    USB_LOG_L0, PRINT_MASK_ALL,
3086 	    USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS) {
3087 		usb_ac_pm_idle_component(uacp);
3088 
3089 		/* change the device state from suspended to disconnected */
3090 		mutex_enter(&uacp->usb_ac_mutex);
3091 		uacp->usb_ac_dev_state = USB_DEV_DISCONNECTED;
3092 		mutex_exit(&uacp->usb_ac_mutex);
3093 
3094 		return;
3095 	}
3096 
3097 	mutex_enter(&uacp->usb_ac_mutex);
3098 	uacpm = uacp->usb_ac_pm;
3099 	if (uacpm) {
3100 		if (uacpm->acpm_wakeup_enabled) {
3101 			mutex_exit(&uacp->usb_ac_mutex);
3102 
3103 			if ((rval = usb_handle_remote_wakeup(uacp->usb_ac_dip,
3104 			    USB_REMOTE_WAKEUP_ENABLE)) != USB_SUCCESS) {
3105 
3106 				USB_DPRINTF_L4(PRINT_MASK_ATTA,
3107 				    uacp->usb_ac_log_handle,
3108 				    "usb_ac_restore_device_state: "
3109 				    "remote wakeup "
3110 				    "enable failed, rval=%d", rval);
3111 			}
3112 
3113 			mutex_enter(&uacp->usb_ac_mutex);
3114 		}
3115 	}
3116 
3117 	/* prevent unplumbing */
3118 	uacp->usb_ac_busy_count++;
3119 	uacp->usb_ac_dev_state = USB_DEV_ONLINE;
3120 	if (uacp->usb_ac_plumbing_state == USB_AC_STATE_PLUMBED) {
3121 		(void) usb_ac_restore_audio_state(uacp, 0);
3122 	}
3123 	uacp->usb_ac_busy_count--;
3124 	mutex_exit(&uacp->usb_ac_mutex);
3125 	usb_ac_pm_idle_component(uacp);
3126 }
3127 
3128 
3129 /*
3130  * usb_ac_am_restore_state
3131  */
3132 static void
3133 usb_ac_am_restore_state(void *arg)
3134 {
3135 	usb_ac_state_t *uacp = (usb_ac_state_t *)arg;
3136 
3137 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3138 	    "usb_ac_am_restore_state: Begin");
3139 
3140 	usb_ac_serialize_access(uacp);
3141 
3142 	mutex_enter(&uacp->usb_ac_mutex);
3143 
3144 	if (uacp->usb_ac_plumbing_state ==
3145 	    USB_AC_STATE_PLUMBED_RESTORING) {
3146 		mutex_exit(&uacp->usb_ac_mutex);
3147 
3148 		/*
3149 		 * allow hid and usb_as to restore themselves
3150 		 * (some handshake would have been preferable though)
3151 		 */
3152 		delay(USB_AC_RESTORE_DELAY);
3153 
3154 		(void) audio_sup_restore_state(uacp->usb_ac_audiohdl,
3155 		    AUDIO_ALL_DEVICES, AUDIO_BOTH);
3156 
3157 		mutex_enter(&uacp->usb_ac_mutex);
3158 		uacp->usb_ac_plumbing_state = USB_AC_STATE_PLUMBED;
3159 	}
3160 
3161 	/* allow unplumbing */
3162 	uacp->usb_ac_busy_count--;
3163 	mutex_exit(&uacp->usb_ac_mutex);
3164 
3165 	usb_ac_release_access(uacp);
3166 
3167 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3168 	    "usb_ac_am_restore_state: End");
3169 }
3170 
3171 
3172 /*
3173  * usb_ac_restore_audio_state:
3174  */
3175 static int
3176 usb_ac_restore_audio_state(usb_ac_state_t *uacp, int flag)
3177 {
3178 	ASSERT(mutex_owned(&uacp->usb_ac_mutex));
3179 
3180 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3181 	    "usb_ac_restore_audio_state: flag=%d", flag);
3182 
3183 	switch (uacp->usb_ac_plumbing_state) {
3184 	case USB_AC_STATE_PLUMBED:
3185 		uacp->usb_ac_plumbing_state =
3186 				USB_AC_STATE_PLUMBED_RESTORING;
3187 
3188 		break;
3189 	case USB_AC_STATE_UNPLUMBED:
3190 
3191 		return (USB_SUCCESS);
3192 	case USB_AC_STATE_PLUMBED_RESTORING:
3193 	default:
3194 
3195 		return (USB_FAILURE);
3196 	}
3197 
3198 	/*
3199 	 * increment busy_count again, it will be decremented
3200 	 * in usb_ac_am_restore_state
3201 	 */
3202 	uacp->usb_ac_busy_count++;
3203 
3204 	if (flag & USB_FLAGS_SLEEP) {
3205 		mutex_exit(&uacp->usb_ac_mutex);
3206 		usb_ac_am_restore_state((void *)uacp);
3207 		mutex_enter(&uacp->usb_ac_mutex);
3208 	} else {
3209 		mutex_exit(&uacp->usb_ac_mutex);
3210 		if (usb_async_req(uacp->usb_ac_dip,
3211 		    usb_ac_am_restore_state,
3212 		    (void *)uacp, USB_FLAGS_SLEEP) != USB_SUCCESS) {
3213 
3214 			mutex_enter(&uacp->usb_ac_mutex);
3215 			uacp->usb_ac_busy_count--;
3216 
3217 			return (USB_FAILURE);
3218 		}
3219 		mutex_enter(&uacp->usb_ac_mutex);
3220 	}
3221 
3222 	return (USB_SUCCESS);
3223 }
3224 
3225 
3226 /*
3227  * Mixer Callback Management
3228  * NOTE: all mixer callbacks are serialized. we cannot be closed while
3229  *	we are in the middle of a callback. There needs to be a
3230  *	teardown first. We cannot be unplumbed as long as we are
3231  *	still open.
3232  *
3233  * usb_ac_setup:
3234  *	Send setup to usb_as if the first setup
3235  *	Check power is done in usb_ac_send_as_cmd()
3236  */
3237 static int
3238 usb_ac_setup(audiohdl_t ahdl, int stream, int flag)
3239 {
3240 	int	rval = AUDIO_SUCCESS;
3241 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
3242 
3243 	ASSERT(uacp != NULL);
3244 
3245 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3246 	    "usb_ac_setup: Begin ahdl=0x%p, stream=%d, flag=%d",
3247 	    ahdl, stream, flag);
3248 
3249 	mutex_enter(&uacp->usb_ac_mutex);
3250 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
3251 		mutex_exit(&uacp->usb_ac_mutex);
3252 
3253 		return (AUDIO_FAILURE);
3254 	}
3255 	mutex_exit(&uacp->usb_ac_mutex);
3256 
3257 	usb_ac_serialize_access(uacp);
3258 
3259 	if (flag & AUDIO_PLAY) {
3260 		rval = usb_ac_do_setup(ahdl, stream, AUDIO_PLAY);
3261 	}
3262 
3263 	if ((rval == USB_SUCCESS) && (flag & AUDIO_RECORD)) {
3264 		rval = usb_ac_do_setup(ahdl, stream, AUDIO_RECORD);
3265 	}
3266 
3267 	usb_ac_release_access(uacp);
3268 
3269 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3270 	    "usb_ac_setup: rval=%d", rval);
3271 
3272 	return ((rval == USB_SUCCESS) ? AUDIO_SUCCESS : AUDIO_FAILURE);
3273 }
3274 
3275 
3276 /*
3277  * usb_ac_do_setup:
3278  *	Wrapper function for usb_ac_setup which can be called
3279  *	either from audio framework for usb_ac_set_format
3280  */
3281 static int
3282 usb_ac_do_setup(audiohdl_t ahdl, int stream, int flag)
3283 {
3284 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
3285 	usb_ac_plumbed_t	*plumb_infop = NULL;
3286 	usb_ac_streams_info_t	*streams_infop = NULL;
3287 	int	dir;
3288 
3289 	ASSERT(uacp != NULL);
3290 
3291 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3292 	    "usb_ac_do_setup: Begin ahdl=0x%p, stream=%d, flag=%d",
3293 	    ahdl, stream, flag);
3294 
3295 	mutex_enter(&uacp->usb_ac_mutex);
3296 
3297 	dir = (flag & AUDIO_PLAY) ? AUDIO_PLAY : AUDIO_RECORD;
3298 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", dir);
3299 	ASSERT(plumb_infop != NULL);
3300 
3301 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
3302 	ASSERT(streams_infop != NULL);
3303 
3304 	/*
3305 	 * Handle multiple setup calls. Pass the setup call to usb_as only
3306 	 * the first time so isoc pipe will be opened only once
3307 	 */
3308 	if (streams_infop->acs_setup_teardown_count++) {
3309 		USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3310 		    "usb_ac_do_setup: more than one setup, cnt=%d",
3311 		    streams_infop->acs_setup_teardown_count);
3312 
3313 		mutex_exit(&uacp->usb_ac_mutex);
3314 
3315 		return (USB_SUCCESS);
3316 	}
3317 
3318 	/* Send setup command to usb_as */
3319 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_SETUP, 0) !=
3320 	    USB_SUCCESS) {
3321 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3322 		    "usb_ac_do_setup: failure");
3323 
3324 		streams_infop->acs_setup_teardown_count--;
3325 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
3326 
3327 		mutex_exit(&uacp->usb_ac_mutex);
3328 
3329 		return (USB_FAILURE);
3330 	}
3331 
3332 	streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
3333 
3334 	mutex_exit(&uacp->usb_ac_mutex);
3335 
3336 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3337 	    "usb_ac_do_setup: End");
3338 
3339 	return (USB_SUCCESS);
3340 }
3341 
3342 
3343 /*
3344  * usb_ac_teardown:
3345  *	Send teardown to usb_as if the last teardown
3346  *	Check power is done in usb_ac_send_as_cmd()
3347  *	NOTE: allow teardown when disconnected
3348  */
3349 static void
3350 usb_ac_teardown(audiohdl_t ahdl, int stream, int flag)
3351 {
3352 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
3353 
3354 	ASSERT(uacp != NULL);
3355 
3356 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3357 	    "usb_ac_teardown: Begin ahdl=0x%p, stream=%d",
3358 	    ahdl, stream);
3359 
3360 	usb_ac_serialize_access(uacp);
3361 
3362 	if (flag & AUDIO_PLAY) {
3363 		usb_ac_do_teardown(ahdl, stream, AUDIO_PLAY);
3364 	}
3365 
3366 	if (flag & AUDIO_RECORD) {
3367 		usb_ac_do_teardown(ahdl, stream, AUDIO_RECORD);
3368 	}
3369 
3370 	usb_ac_release_access(uacp);
3371 
3372 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3373 	    "usb_ac_teardown: End");
3374 }
3375 
3376 
3377 /*
3378  * usb_ac_do_teardown()
3379  *	Check power is done in usb_ac_send_as_cmd()
3380  */
3381 static void
3382 usb_ac_do_teardown(audiohdl_t ahdl, int stream, int flag)
3383 {
3384 	usb_ac_state_t		*uacp = audio_sup_get_private(ahdl);
3385 	usb_ac_plumbed_t	*plumb_infop = NULL;
3386 	usb_ac_streams_info_t	*streams_infop = NULL;
3387 	int			dir;
3388 
3389 	ASSERT(uacp != NULL);
3390 
3391 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3392 	    "usb_ac_do_teardown: Begin ahdl=0x%p, stream=%d",
3393 	    ahdl, stream);
3394 
3395 	mutex_enter(&uacp->usb_ac_mutex);
3396 
3397 	dir = (flag & AUDIO_PLAY) ? AUDIO_PLAY : AUDIO_RECORD;
3398 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", dir);
3399 	ASSERT(plumb_infop != NULL);
3400 
3401 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
3402 	ASSERT(streams_infop != NULL);
3403 
3404 	/* There should be at least one matching setup call */
3405 	ASSERT(streams_infop->acs_setup_teardown_count);
3406 
3407 	/*
3408 	 * Handle multiple setup/teardown calls. Pass the call to usb_as
3409 	 * only this is the last teardown so that isoc pipe is closed
3410 	 * only once
3411 	 */
3412 	if (--(streams_infop->acs_setup_teardown_count)) {
3413 		USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3414 		    "usb_ac_do_teardown: more than one setup/teardown, "
3415 		    "cnt=%d",
3416 		    streams_infop->acs_setup_teardown_count);
3417 
3418 		mutex_exit(&uacp->usb_ac_mutex);
3419 
3420 		return;
3421 	}
3422 
3423 	/* Send teardown command to usb_as */
3424 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_TEARDOWN,
3425 	    (void *)NULL) != USB_SUCCESS) {
3426 
3427 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3428 		    "usb_ac_do_teardown: failure");
3429 
3430 		streams_infop->acs_setup_teardown_count++;
3431 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
3432 
3433 		mutex_exit(&uacp->usb_ac_mutex);
3434 
3435 		return;
3436 	}
3437 
3438 	streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
3439 
3440 	mutex_exit(&uacp->usb_ac_mutex);
3441 
3442 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3443 	    "usb_ac_do_teardown: End");
3444 }
3445 
3446 
3447 /*
3448  * usb_ac_set_config:
3449  *	This routine will send control commands to get the max
3450  *	and min gain balance, calculate the gain to be set from the
3451  *	arguments and send another control command to set it.
3452  *	Check power is done here since we will access the default pipe
3453  */
3454 static int
3455 usb_ac_set_config(audiohdl_t ahdl, int stream, int command, int flag,
3456 	int arg1, int arg2)
3457 {
3458 	usb_ac_state_t	*uacp = audio_sup_get_private(ahdl);
3459 	char		*what;
3460 	int		rval = AUDIO_FAILURE;
3461 	uint_t		channel;
3462 	uchar_t 	n_channels = 0;
3463 	uint_t		dir, count;
3464 	short		muteval;
3465 
3466 	ASSERT(uacp != NULL);
3467 
3468 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3469 	    "usb_ac_set_config: Begin ahdl=0x%p\n\t"
3470 	    "stream=%d, cmd=%d, flag=%d, arg1=%d, arg2=%d",
3471 	    ahdl, stream, command, flag, arg1, arg2);
3472 
3473 	mutex_enter(&uacp->usb_ac_mutex);
3474 	if (uacp->usb_ac_plumbing_state < USB_AC_STATE_PLUMBED) {
3475 		mutex_exit(&uacp->usb_ac_mutex);
3476 
3477 		return (AUDIO_FAILURE);
3478 	}
3479 
3480 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
3481 		mutex_exit(&uacp->usb_ac_mutex);
3482 
3483 		return (AUDIO_FAILURE);
3484 	}
3485 	mutex_exit(&uacp->usb_ac_mutex);
3486 	usb_ac_serialize_access(uacp);
3487 	mutex_enter(&uacp->usb_ac_mutex);
3488 
3489 	switch (command) {
3490 	case AM_SET_GAIN:
3491 		/*
3492 		 * Set the gain for a channel. The audio mixer calculates the
3493 		 * impact, if any, on the channel's gain.
3494 		 *
3495 		 *	0 <= gain <= AUDIO_MAX_GAIN
3496 		 *
3497 		 *	arg1 --> gain
3498 		 *	arg2 --> channel #, 0 == left, 1 == right
3499 		 */
3500 		what = "gain";
3501 		channel = ++arg2;
3502 		ASSERT(flag != AUDIO_BOTH);
3503 		dir = (flag & AUDIO_PLAY) ? AUDIO_PLAY : AUDIO_RECORD;
3504 
3505 		/*
3506 		 * We service the set_config command when the device is
3507 		 * plumbed and opened.
3508 		 */
3509 		n_channels = usb_ac_get_curr_n_channels(uacp, dir);
3510 
3511 		if (channel > n_channels) {
3512 			USB_DPRINTF_L2(PRINT_MASK_ALL,
3513 			    uacp->usb_ac_log_handle,
3514 			    "usb_ac_set_config: channel(%d) passed is "
3515 			    " > n_channels(%d)", channel, n_channels);
3516 
3517 			goto done;
3518 		}
3519 		count = 0;
3520 		(void) usb_ac_set_control(uacp, dir,
3521 			    USB_AUDIO_FEATURE_UNIT, channel,
3522 			    USB_AUDIO_VOLUME_CONTROL,
3523 			    USB_AC_FIND_ALL, &count, arg1, usb_ac_set_gain);
3524 
3525 		/*
3526 		 * If feature unit id could not be found, it probably means
3527 		 * volume/gain control is not available for this device.
3528 		 * and we just return success if we haven't completed
3529 		 * the registration with the mixer yet
3530 		 */
3531 		if (count == 0) {
3532 			USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3533 			    "mixer=%d,	no featureID, arg1=%d",
3534 			    uacp->usb_ac_registered_with_mixer, arg1);
3535 			rval = (uacp->usb_ac_registered_with_mixer == 0) ?
3536 					AUDIO_SUCCESS : AUDIO_FAILURE;
3537 		} else {
3538 			rval = AUDIO_SUCCESS;
3539 		}
3540 
3541 		break;
3542 	case AM_SET_PORT:
3543 		what = "port";
3544 		ASSERT(flag != AUDIO_BOTH);
3545 		dir = (flag & AUDIO_PLAY) ? AUDIO_PLAY : AUDIO_RECORD;
3546 
3547 		rval = usb_ac_set_port(uacp, dir, arg1);
3548 		rval = (rval == USB_SUCCESS) ? AUDIO_SUCCESS : AUDIO_FAILURE;
3549 
3550 		break;
3551 	case AM_SET_MONITOR_GAIN:
3552 		what = "monitor gain";
3553 		channel = ++arg2;
3554 		dir = AUDIO_RECORD;
3555 
3556 		/*
3557 		 * We service the set_config command when the device is
3558 		 * plumbed and opened.
3559 		 */
3560 		n_channels = usb_ac_get_curr_n_channels(uacp, dir);
3561 
3562 		if (channel > n_channels) {
3563 			USB_DPRINTF_L2(PRINT_MASK_ALL,
3564 			    uacp->usb_ac_log_handle,
3565 			    "usb_ac_set_config: channel(%d) passed is "
3566 			    " > n_channels(%d)", channel, n_channels);
3567 
3568 			goto done;
3569 		}
3570 		count = 0;
3571 		(void) usb_ac_set_monitor_gain_control(uacp, dir,
3572 			    USB_AUDIO_INPUT_TERMINAL, channel,
3573 			    USB_AUDIO_VOLUME_CONTROL,
3574 			    USB_AC_FIND_ALL, &count, arg1,
3575 			    usb_ac_set_monitor_gain);
3576 
3577 		/*
3578 		 * always return success since we told the mixer
3579 		 * we always support this and sdtaudiocontrol displays
3580 		 * monitor gain regardless.
3581 		 */
3582 		rval = AUDIO_SUCCESS;
3583 
3584 		break;
3585 	case AM_OUTPUT_MUTE:
3586 		what = "mute";
3587 		ASSERT(flag != AUDIO_BOTH);
3588 		dir = (flag & AUDIO_PLAY) ? AUDIO_PLAY : AUDIO_RECORD;
3589 
3590 		/*
3591 		 * arg1 != 0 --> mute
3592 		 * arg1 == 0 --> unmute
3593 		 * arg2 --> not used
3594 		 */
3595 		muteval = (arg1 == 0) ? USB_AUDIO_MUTE_OFF :
3596 					USB_AUDIO_MUTE_ON;
3597 		count = 0;
3598 		(void) usb_ac_set_control(uacp, dir,
3599 			    USB_AUDIO_FEATURE_UNIT, 0,
3600 			    USB_AUDIO_MUTE_CONTROL,
3601 			    USB_AC_FIND_ALL, &count, muteval,
3602 			    usb_ac_set_mute);
3603 
3604 		rval = (count == 0) ? AUDIO_FAILURE : AUDIO_SUCCESS;
3605 
3606 		break;
3607 	case AM_MIC_BOOST:
3608 		what = "mic boost";
3609 		rval = AUDIO_SUCCESS;
3610 		break;
3611 	case AM_SET_GAIN_BAL:
3612 		what = "set gain bal";
3613 		rval = AUDIO_FAILURE;
3614 		break;
3615 	default:
3616 		what = "unknown";
3617 		rval = AUDIO_FAILURE;
3618 	}
3619 
3620 done:
3621 	mutex_exit(&uacp->usb_ac_mutex);
3622 
3623 	/* Now it's safe to release access to other routines */
3624 	usb_ac_release_access(uacp);
3625 
3626 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3627 	    "usb_ac_set_config: %s done, rval=%d", what, rval);
3628 
3629 	return (rval);
3630 }
3631 
3632 
3633 /*
3634  * usb_ac_set_monitor_gain:
3635  *	called for each output terminal which supports
3636  *	from usb_ac_traverse_connections
3637  */
3638 static int
3639 usb_ac_set_monitor_gain(usb_ac_state_t *uacp, uint_t unit,
3640     uint_t dir, uint_t channel, uint_t control, uint_t gain, uint_t *depth)
3641 {
3642 	usb_audio_output_term_descr_t *d =
3643 			    uacp->usb_ac_units[unit].acu_descriptor;
3644 
3645 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3646 	    "usb_ac_set_monitor_gain: ");
3647 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3648 	    "id=%d dir=%d ch=%d cntl=%d gain=%d type=%d term type=0x%x",
3649 	    unit, dir, channel, control, gain,
3650 	    uacp->usb_ac_unit_type[unit], d->wTerminalType);
3651 
3652 	/* log how we got here */
3653 	usb_ac_push_unit_id(uacp, unit);
3654 	usb_ac_show_traverse_path(uacp);
3655 	usb_ac_pop_unit_id(uacp, unit);
3656 
3657 	/* we only care about the ITs connected to real hw inputs */
3658 	switch (d->wTerminalType) {
3659 	case USB_AUDIO_TERM_TYPE_STREAMING:
3660 
3661 		return (USB_FAILURE);
3662 
3663 	case USB_AUDIO_TERM_TYPE_DT_MICROPHONE:
3664 	case USB_AUDIO_TERM_TYPE_PERS_MICROPHONE:
3665 	case USB_AUDIO_TERM_TYPE_OMNI_DIR_MICROPHONE:
3666 	case USB_AUDIO_TERM_TYPE_MICROPHONE_ARRAY:
3667 	case USB_AUDIO_TERM_TYPE_PROCESSING_MIC_ARRAY:
3668 	default:
3669 
3670 		break;
3671 	}
3672 
3673 	/*
3674 	 * we can only do this if the microphone is mixed into the
3675 	 * audio output so look for a mixer first
3676 	 */
3677 	if (usb_ac_check_path(uacp, USB_AUDIO_MIXER_UNIT) ==
3678 	    USB_SUCCESS) {
3679 		int i, id;
3680 
3681 		/* now look for a feature unit */
3682 		for (i = uacp->usb_ac_traverse_path_index - 1; i >= 0;
3683 		    i--) {
3684 			id = uacp->usb_ac_traverse_path[i];
3685 
3686 			switch (uacp->usb_ac_unit_type[id]) {
3687 			case USB_AUDIO_MIXER_UNIT:
3688 
3689 				/* the FU should be before the mixer */
3690 				return (USB_FAILURE);
3691 
3692 			case USB_AUDIO_FEATURE_UNIT:
3693 				/*
3694 				 * now set the volume
3695 				 */
3696 				if (usb_ac_set_gain(uacp, id, dir, channel,
3697 				    control, gain, depth) != USB_SUCCESS) {
3698 
3699 					/* try master channel */
3700 					if (usb_ac_set_gain(uacp, id, dir,
3701 					    0, control, gain, depth) !=
3702 					    USB_SUCCESS) {
3703 
3704 						return (USB_FAILURE);
3705 					}
3706 				}
3707 
3708 				return (USB_SUCCESS);
3709 
3710 			default:
3711 				continue;
3712 			}
3713 		}
3714 	}
3715 
3716 	return (USB_FAILURE);
3717 }
3718 
3719 
3720 /*
3721  * usb_ac_set_gain is called for each feature unit which supports
3722  * the requested controls from usb_ac_traverse_connections
3723  * we still need to check whether this unit supports the requested
3724  * control.
3725  */
3726 static int
3727 usb_ac_set_gain(usb_ac_state_t *uacp, uint_t featureID,
3728     uint_t dir, uint_t channel, uint_t control, uint_t gain, uint_t *depth)
3729 {
3730 	short max, min, current;
3731 
3732 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3733 	    "usb_ac_set_gain: id=%d dir=%d ch=%d cntl=%d gain=%d",
3734 	    featureID, dir, channel, control, gain);
3735 
3736 	if (usb_ac_feature_unit_check(uacp, featureID,
3737 	    dir, channel, control, gain, depth) != USB_SUCCESS) {
3738 
3739 		return (USB_FAILURE);
3740 	}
3741 
3742 	if ((max = usb_ac_get_maxmin_volume(uacp, channel,
3743 	    USB_AUDIO_GET_MAX, dir, featureID)) == USB_FAILURE) {
3744 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3745 		    "usb_ac_set_gain: getting max gain failed");
3746 
3747 		return (USB_FAILURE);
3748 	}
3749 
3750 	USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3751 	    "usb_ac_set_gain: channel %d, max=%d", channel, max);
3752 
3753 	if ((min = usb_ac_get_maxmin_volume(uacp, channel,
3754 	    USB_AUDIO_GET_MIN, dir, featureID)) == USB_FAILURE) {
3755 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3756 		    "usb_ac_set_gain: getting min gain failed");
3757 
3758 		return (USB_FAILURE);
3759 	}
3760 
3761 	USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3762 	    "usb_ac_set_gain: channel=%d, min=%d", channel, min);
3763 
3764 	if ((current = usb_ac_get_maxmin_volume(uacp, channel,
3765 	    USB_AUDIO_GET_CUR, dir, featureID)) == USB_FAILURE) {
3766 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3767 		    "usb_ac_set_gain: getting cur gain failed");
3768 
3769 		return (USB_FAILURE);
3770 	}
3771 
3772 	USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3773 	    "usb_ac_set_gain: channel=%d, cur=%d", channel, current);
3774 
3775 	/*
3776 	 * Set the gain for a channel. The audio mixer calculates the
3777 	 * impact, if any, on the channel's gain.
3778 	 *
3779 	 *	0 <= gain <= AUDIO_MAX_GAIN
3780 	 *
3781 	 *	channel #, 0 == left, 1 == right
3782 	 */
3783 
3784 	if (gain == 0) {
3785 		gain = USB_AUDIO_VOLUME_SILENCE;
3786 	} else {
3787 		gain = max - ((max - min) * (0x100 - gain))/0x100;
3788 	}
3789 
3790 	USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3791 	    "usb_ac_set_gain: ch=%d dir=%d max=%d min=%d gain=%d",
3792 	    channel, dir, max, min, gain);
3793 
3794 	if (usb_ac_set_volume(uacp, channel, gain, dir,
3795 	    featureID) != USB_SUCCESS) {
3796 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3797 		    "usb_ac_set_gain: setting volume failed");
3798 
3799 		return (USB_FAILURE);
3800 	}
3801 
3802 	/* just curious, read it back, device may round up/down */
3803 	if ((current = usb_ac_get_maxmin_volume(uacp, channel,
3804 	    USB_AUDIO_GET_CUR, dir, featureID)) == USB_FAILURE) {
3805 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3806 		    "usb_ac_set_gain: getting cur gain failed");
3807 	}
3808 
3809 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3810 	    "usb_ac_set_gain done: "
3811 	    "id=%d channel=%d, cur=%d gain=%d", featureID, channel,
3812 	    (ushort_t)current, (ushort_t)gain);
3813 
3814 	return (USB_SUCCESS);
3815 }
3816 
3817 
3818 /*
3819  * usb_ac_set_format
3820  *	This mixer callback initiates a command to be sent to
3821  *	usb_as to select an alternate with the passed characteristics
3822  *	and also to set the sample frequency.
3823  *	Note that this may be called when a playing is going on in
3824  *	the streaming interface. To handle that, first stop
3825  *	playing/recording, close the pipe by sending a teardown
3826  *	command, send the set_format command down and then reopen
3827  *	the pipe. Note : (1) audio framework will restart play/record
3828  *	after a set_format command. (2) Check power is done in
3829  *	usb_ac_send_as_cmd().
3830  */
3831 static int
3832 usb_ac_set_format(audiohdl_t ahdl, int stream, int flag,
3833 	int sample, int channels, int precision, int encoding)
3834 {
3835 	usb_ac_state_t		*uacp = audio_sup_get_private(ahdl);
3836 	usb_audio_formats_t	*format;
3837 	usb_audio_formats_t	old_format;
3838 	usb_ac_plumbed_t	*plumb_infop;
3839 	usb_ac_streams_info_t	*streams_infop = NULL;
3840 	int			old_setup_teardown_count;
3841 	int			dir;
3842 	int			rval;
3843 
3844 	ASSERT(uacp != NULL);
3845 
3846 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3847 	    "usb_ac_set_format: Begin ahdl=0x%p, stream=%d, flag=%d, "
3848 	    "sr=%d, chnls=%d, prec=%d, enc=%d", ahdl, stream, flag,
3849 	    sample, channels, precision, encoding);
3850 
3851 	mutex_enter(&uacp->usb_ac_mutex);
3852 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
3853 		mutex_exit(&uacp->usb_ac_mutex);
3854 
3855 		return (AUDIO_FAILURE);
3856 	}
3857 	mutex_exit(&uacp->usb_ac_mutex);
3858 
3859 	usb_ac_serialize_access(uacp);
3860 
3861 	ASSERT(flag != AUDIO_BOTH);
3862 
3863 	mutex_enter(&uacp->usb_ac_mutex);
3864 	dir = (flag & AUDIO_PLAY) ? AUDIO_PLAY : AUDIO_RECORD;
3865 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", dir);
3866 	if (plumb_infop == NULL) {
3867 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3868 		    "usb_ac_set_format: no plumb info");
3869 		mutex_exit(&uacp->usb_ac_mutex);
3870 
3871 		usb_ac_release_access(uacp);
3872 
3873 		return (AUDIO_FAILURE);
3874 	}
3875 
3876 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
3877 	ASSERT(streams_infop != NULL);
3878 
3879 	/* isoc pipe not open and playing is not in progress */
3880 	if (streams_infop->acs_setup_teardown_count == 0) {
3881 
3882 		mutex_exit(&uacp->usb_ac_mutex);
3883 
3884 		rval = usb_ac_send_format_cmd(ahdl, stream, dir, sample,
3885 		    channels, precision, encoding);
3886 
3887 		usb_ac_release_access(uacp);
3888 
3889 		return ((rval == USB_SUCCESS) ?
3890 		    AUDIO_SUCCESS : AUDIO_FAILURE);
3891 	}
3892 
3893 	/* isoc pipe is open and playing might be in progress */
3894 	format = &streams_infop->acs_ac_to_as_req.acr_curr_format;
3895 
3896 	/* Keep a copy of the old format */
3897 	bcopy((void *)format, (void *)&old_format,
3898 	    sizeof (usb_audio_formats_t));
3899 
3900 	ASSERT(streams_infop->acs_setup_teardown_count != 0);
3901 
3902 	old_setup_teardown_count = streams_infop->acs_setup_teardown_count;
3903 	streams_infop->acs_setup_teardown_count = 1;
3904 
3905 	mutex_exit(&uacp->usb_ac_mutex);
3906 
3907 	if (dir == AUDIO_PLAY) {
3908 		usb_ac_do_pause_play(ahdl, stream);
3909 	} else if (dir == AUDIO_RECORD) {
3910 		usb_ac_do_stop_record(ahdl, stream);
3911 	}
3912 
3913 	/* This blocks until the current isoc xfer is over */
3914 	usb_ac_do_teardown(ahdl, stream, dir);
3915 
3916 	if (usb_ac_send_format_cmd(ahdl, stream, dir, sample,
3917 	    channels, precision, encoding) != USB_SUCCESS) {
3918 		/*
3919 		 * Setting new alternate has failed, try restoring
3920 		 * old one.
3921 		 * If there is a bandwidth failure, hang around
3922 		 * till bandwidth is available. Also we know that
3923 		 * there is a matching alternate, so that can't fail.
3924 		 */
3925 		if (usb_ac_send_format_cmd(ahdl, stream, dir,
3926 		    old_format.fmt_sr, old_format.fmt_chns,
3927 		    old_format.fmt_precision, old_format.fmt_encoding) ==
3928 		    USB_FAILURE) {
3929 
3930 			/* We closed the pipe; reopen it */
3931 			(void) usb_ac_do_setup(ahdl, stream, dir);
3932 
3933 			mutex_enter(&uacp->usb_ac_mutex);
3934 			streams_infop->acs_setup_teardown_count =
3935 			    old_setup_teardown_count;
3936 			mutex_exit(&uacp->usb_ac_mutex);
3937 
3938 			usb_ac_release_access(uacp);
3939 
3940 			return (AUDIO_FAILURE);
3941 		}
3942 	}
3943 
3944 	/* This should block until successful */
3945 	(void) usb_ac_do_setup(ahdl, stream, dir);
3946 
3947 	mutex_enter(&uacp->usb_ac_mutex);
3948 	streams_infop->acs_setup_teardown_count = old_setup_teardown_count;
3949 	mutex_exit(&uacp->usb_ac_mutex);
3950 
3951 	usb_ac_release_access(uacp);
3952 
3953 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3954 	    "usb_ac_set_format: End");
3955 
3956 	return (AUDIO_SUCCESS);
3957 }
3958 
3959 
3960 /*
3961  * usb_ac_get_curr_n_channels:
3962  *	Return no. of channels from the current format table
3963  */
3964 static int
3965 usb_ac_get_curr_n_channels(usb_ac_state_t *uacp, int dir)
3966 {
3967 	usb_audio_formats_t *cur_fmt = usb_ac_get_curr_format(uacp, dir);
3968 
3969 	return (cur_fmt->fmt_chns);
3970 }
3971 
3972 
3973 /*
3974  * usb_ac_get_cur_format:
3975  *	Get format for the current alternate
3976  */
3977 static usb_audio_formats_t *
3978 usb_ac_get_curr_format(usb_ac_state_t *uacp, int dir)
3979 {
3980 	usb_ac_plumbed_t *plumb_infop;
3981 	usb_ac_streams_info_t *streams_infop = NULL;
3982 
3983 	ASSERT(mutex_owned(&uacp->usb_ac_mutex));
3984 
3985 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", dir);
3986 	if (plumb_infop == NULL) {
3987 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
3988 		    "usb_ac_get_curr_format: no plumb info");
3989 
3990 		return (NULL);
3991 	}
3992 
3993 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
3994 	ASSERT(streams_infop != NULL);
3995 
3996 	return (&streams_infop->acs_cur_fmt);
3997 }
3998 
3999 
4000 /*
4001  * usb_ac_send_format_cmd
4002  *	Sets format and get alternate setting that matches with
4003  *	the format from the usb_as playing or recording interface
4004  *	Send the set sample freq command down to usb_as.
4005  */
4006 static int
4007 usb_ac_send_format_cmd(audiohdl_t ahdl, int stream, int dir,
4008 	int sample, int channels, int precision, int encoding)
4009 {
4010 	usb_ac_state_t		*uacp = audio_sup_get_private(ahdl);
4011 	usb_audio_formats_t	*format;
4012 	usb_ac_plumbed_t	*plumb_infop = NULL;
4013 	usb_ac_streams_info_t	*streams_infop = NULL;
4014 
4015 	ASSERT(uacp != NULL);
4016 
4017 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4018 	    "usb_ac_send_format_cmd: Begin ahdl=0x%p, stream=%d, dir=%d, "
4019 	    "sr=%d, chnls=%d, prec=%d, enc=%d", ahdl, stream, dir,
4020 	    sample, channels, precision, encoding);
4021 
4022 	mutex_enter(&uacp->usb_ac_mutex);
4023 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
4024 		mutex_exit(&uacp->usb_ac_mutex);
4025 
4026 		return (USB_FAILURE);
4027 	}
4028 
4029 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", dir);
4030 	ASSERT(plumb_infop);
4031 
4032 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
4033 	ASSERT(streams_infop != NULL);
4034 
4035 	ASSERT(dir == AUDIO_PLAY || dir == AUDIO_RECORD);
4036 	streams_infop->acs_ac_to_as_req.acr_curr_dir = dir;
4037 
4038 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4039 	    "usb_ac_send_format_cmd: plumb_infop=0x%p, streams_infop=0x%p",
4040 	    plumb_infop, streams_infop);
4041 
4042 	format = &(streams_infop->acs_ac_to_as_req.acr_curr_format);
4043 	bzero(format, sizeof (usb_audio_formats_t));
4044 
4045 	/* save format info */
4046 	format->fmt_sr		= (uint_t)sample;
4047 	format->fmt_chns	= (uchar_t)channels;
4048 	format->fmt_precision	= (uchar_t)precision;
4049 	format->fmt_encoding	= (uchar_t)encoding;
4050 
4051 	streams_infop->acs_cur_fmt = *format;
4052 
4053 	/*
4054 	 * Set format for the streaming interface with lower write queue
4055 	 * This boils down to set_alternate  interface command in
4056 	 * usb_as and the reply mp contains the currently active
4057 	 * alternate number that is stored in the as_req structure
4058 	 */
4059 	if (usb_ac_send_as_cmd(uacp, plumb_infop,
4060 	    USB_AUDIO_SET_FORMAT, format) != USB_SUCCESS) {
4061 		USB_DPRINTF_L2(PRINT_MASK_ALL,
4062 		    uacp->usb_ac_log_handle,
4063 		    "usb_ac_send_format_cmd: failed");
4064 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4065 		mutex_exit(&uacp->usb_ac_mutex);
4066 
4067 		return (USB_FAILURE);
4068 	} else {
4069 		/* alternate number stored and reply mp freed */
4070 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4071 	}
4072 
4073 	/* Set the sample rate */
4074 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_SET_SAMPLE_FREQ,
4075 	    &sample) != USB_SUCCESS) {
4076 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4077 		    "usb_ac_send_format_cmd: setting format failed");
4078 
4079 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4080 		mutex_exit(&uacp->usb_ac_mutex);
4081 
4082 		return (USB_FAILURE);
4083 	}
4084 
4085 	streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4086 
4087 	mutex_exit(&uacp->usb_ac_mutex);
4088 
4089 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4090 	    "usb_ac_send_format_cmd: End");
4091 
4092 	return (USB_SUCCESS);
4093 }
4094 
4095 
4096 /*
4097  * usb_ac_start_play
4098  *	Send a start_play command down to usb_as
4099  *	Check power is done in usb_ac_send_as_cmd()
4100  */
4101 static int
4102 usb_ac_start_play(audiohdl_t ahdl, int stream)
4103 {
4104 	usb_ac_state_t		*uacp = audio_sup_get_private(ahdl);
4105 	usb_audio_formats_t	*cur_fmt;
4106 	usb_ac_plumbed_t	*plumb_infop = NULL;
4107 	int			dir, samples;
4108 	usb_audio_play_req_t	play_req;
4109 	usb_ac_streams_info_t	*streams_infop = NULL;
4110 
4111 	ASSERT(uacp != NULL);
4112 
4113 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4114 	    "usb_ac_start_play: Begin ahdl=0x%p, stream=%d",
4115 	    ahdl, stream);
4116 
4117 	mutex_enter(&uacp->usb_ac_mutex);
4118 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
4119 		mutex_exit(&uacp->usb_ac_mutex);
4120 
4121 		return (AUDIO_FAILURE);
4122 	}
4123 	mutex_exit(&uacp->usb_ac_mutex);
4124 
4125 	usb_ac_serialize_access(uacp);
4126 
4127 	mutex_enter(&uacp->usb_ac_mutex);
4128 
4129 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", AUDIO_PLAY);
4130 	ASSERT(plumb_infop);
4131 
4132 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
4133 	ASSERT(streams_infop != NULL);
4134 
4135 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4136 	    "usb_ac_start_play: plumb_infop=0x%p, streams_infop=0x%p",
4137 	    plumb_infop, streams_infop);
4138 
4139 	dir = streams_infop->acs_ac_to_as_req.acr_curr_dir;
4140 	ASSERT(dir == AUDIO_PLAY);
4141 
4142 	cur_fmt = &streams_infop->acs_ac_to_as_req.acr_curr_format;
4143 
4144 	/* Check for continuous sample rate done in usb_as */
4145 	samples = cur_fmt->fmt_sr * cur_fmt->fmt_chns /
4146 				uacp->usb_ac_am_ad_info.ad_play.ad_int_rate;
4147 	if (samples & cur_fmt->fmt_chns) {
4148 		samples++;
4149 	}
4150 
4151 	play_req.up_samples = samples;
4152 	play_req.up_handle = ahdl;
4153 
4154 	/* Send setup command to usb_as */
4155 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_START_PLAY,
4156 	    (void *)&play_req) != USB_SUCCESS) {
4157 
4158 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4159 		    "usb_ac_start_play: failure");
4160 
4161 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4162 		mutex_exit(&uacp->usb_ac_mutex);
4163 
4164 		usb_ac_release_access(uacp);
4165 
4166 		return (AUDIO_FAILURE);
4167 	}
4168 
4169 	streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4170 	mutex_exit(&uacp->usb_ac_mutex);
4171 
4172 	usb_ac_release_access(uacp);
4173 
4174 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4175 	    "usb_ac_start_play: End");
4176 
4177 	return (AUDIO_SUCCESS);
4178 }
4179 
4180 
4181 /*
4182  * usb_ac_pause_play:
4183  *	Wrapper function for usb_ac_do_pause_play and gets
4184  *	called from mixer framework.
4185  */
4186 static void
4187 usb_ac_pause_play(audiohdl_t ahdl, int stream)
4188 {
4189 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
4190 
4191 	ASSERT(uacp != NULL);
4192 
4193 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4194 	    "usb_ac_pause_play: Begin ahdl=0x%p, stream=%d",
4195 	    ahdl, stream);
4196 
4197 	mutex_enter(&uacp->usb_ac_mutex);
4198 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
4199 		mutex_exit(&uacp->usb_ac_mutex);
4200 
4201 		return;
4202 	}
4203 	mutex_exit(&uacp->usb_ac_mutex);
4204 
4205 	usb_ac_serialize_access(uacp);
4206 	usb_ac_do_pause_play(ahdl, stream);
4207 	usb_ac_release_access(uacp);
4208 
4209 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4210 	    "usb_ac_pause_play: End");
4211 }
4212 
4213 /*
4214  * usb_ac_do_pause_play:
4215  *	Send a pause_play command to usb_as.
4216  *	Check power is done in usb_ac_send_as_cmd()
4217  */
4218 static void
4219 usb_ac_do_pause_play(audiohdl_t ahdl, int stream)
4220 {
4221 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
4222 	usb_ac_plumbed_t	*plumb_infop = NULL;
4223 	usb_ac_streams_info_t	*streams_infop = NULL;
4224 
4225 	ASSERT(uacp != NULL);
4226 
4227 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4228 	    "usb_ac_do_pause_play: Begin ahdl=0x%p, stream=%d",
4229 	    ahdl, stream);
4230 
4231 	mutex_enter(&uacp->usb_ac_mutex);
4232 
4233 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", AUDIO_PLAY);
4234 	ASSERT(plumb_infop);
4235 
4236 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
4237 	ASSERT(streams_infop != NULL);
4238 
4239 	/* Send setup command to usb_as */
4240 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_PAUSE_PLAY,
4241 	    (void *)NULL) != USB_SUCCESS) {
4242 
4243 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4244 		    "usb_ac_do_pause_play: failure");
4245 
4246 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4247 	}
4248 
4249 	mutex_exit(&uacp->usb_ac_mutex);
4250 
4251 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4252 	    "usb_ac_do_pause_play: End");
4253 }
4254 
4255 
4256 /*
4257  * usb_ac_stop_play:
4258  *	Wrapper function for usb_ac_pause_play	and gets
4259  *	called from mixer framework.
4260  */
4261 static void
4262 usb_ac_stop_play(audiohdl_t ahdl, int stream)
4263 {
4264 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
4265 
4266 	ASSERT(uacp != NULL);
4267 
4268 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4269 	    "usb_ac_stop_play: Begin ahdl=0x%p, stream=%d",
4270 	    ahdl, stream);
4271 
4272 	usb_ac_pause_play(ahdl, stream);
4273 
4274 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4275 	    "usb_ac_stop_play: End");
4276 }
4277 
4278 
4279 /*
4280  * usb_ac_start_record:
4281  *	Sends a start record command down to usb_as.
4282  *	Check power is done in usb_ac_send_as_cmd()
4283  */
4284 static int
4285 usb_ac_start_record(audiohdl_t ahdl, int stream)
4286 {
4287 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
4288 	usb_ac_plumbed_t	*plumb_infop = NULL;
4289 	usb_ac_streams_info_t	*streams_infop = NULL;
4290 
4291 	ASSERT(uacp != NULL);
4292 
4293 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4294 	    "usb_ac_start_record: Begin ahdl=0x%p, stream=%d",
4295 	    ahdl, stream);
4296 
4297 	mutex_enter(&uacp->usb_ac_mutex);
4298 	if (uacp->usb_ac_dev_state != USB_DEV_ONLINE) {
4299 		mutex_exit(&uacp->usb_ac_mutex);
4300 
4301 		return (AUDIO_FAILURE);
4302 	}
4303 	mutex_exit(&uacp->usb_ac_mutex);
4304 
4305 	usb_ac_serialize_access(uacp);
4306 
4307 	mutex_enter(&uacp->usb_ac_mutex);
4308 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", AUDIO_RECORD);
4309 	ASSERT(plumb_infop);
4310 
4311 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
4312 	ASSERT(streams_infop != NULL);
4313 
4314 	/* Send setup command to usb_as */
4315 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_START_RECORD,
4316 	    (void *)&ahdl) != USB_SUCCESS) {
4317 
4318 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4319 		    "usb_ac_start_record: failure");
4320 
4321 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4322 		mutex_exit(&uacp->usb_ac_mutex);
4323 
4324 		usb_ac_release_access(uacp);
4325 
4326 		return (AUDIO_FAILURE);
4327 	}
4328 
4329 	mutex_exit(&uacp->usb_ac_mutex);
4330 
4331 	usb_ac_release_access(uacp);
4332 
4333 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4334 	    "usb_ac_start_record: End");
4335 
4336 	return (AUDIO_SUCCESS);
4337 }
4338 
4339 
4340 /*
4341  * usb_ac_stop_record:
4342  *	Wrapper function for usb_ac_do_stop_record and is
4343  *	called form mixer framework.
4344  */
4345 static void
4346 usb_ac_stop_record(audiohdl_t ahdl, int stream)
4347 {
4348 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
4349 
4350 	ASSERT(uacp != NULL);
4351 
4352 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4353 	    "usb_ac_stop_record: Begin ahdl=0x%p, stream=%d",
4354 	    ahdl, stream);
4355 
4356 	usb_ac_serialize_access(uacp);
4357 	usb_ac_do_stop_record(ahdl, stream);
4358 	usb_ac_release_access(uacp);
4359 
4360 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4361 	    "usb_ac_stop_record: End");
4362 }
4363 
4364 
4365 /*
4366  * usb_ac_do_stop_record:
4367  *	Sends a stop_record command down.
4368  *	Check power is done in usb_ac_send_as_cmd()
4369  */
4370 static void
4371 usb_ac_do_stop_record(audiohdl_t ahdl, int stream)
4372 {
4373 	usb_ac_state_t *uacp = audio_sup_get_private(ahdl);
4374 	usb_ac_plumbed_t	*plumb_infop = NULL;
4375 	usb_ac_streams_info_t	*streams_infop = NULL;
4376 
4377 	ASSERT(uacp != NULL);
4378 
4379 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4380 	    "usb_ac_do_stop_record: Begin ahdl=0x%p, stream=%d",
4381 	    ahdl, stream);
4382 
4383 	mutex_enter(&uacp->usb_ac_mutex);
4384 
4385 	plumb_infop = usb_ac_get_plumb_info(uacp, "usb_as", AUDIO_RECORD);
4386 	ASSERT(plumb_infop != NULL);
4387 
4388 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
4389 	ASSERT(streams_infop != NULL);
4390 
4391 	/* Send setup command to usb_as */
4392 	if (usb_ac_send_as_cmd(uacp, plumb_infop, USB_AUDIO_STOP_RECORD,
4393 	    NULL) != USB_SUCCESS) {
4394 
4395 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4396 		    "usb_ac_do_stop_record: failure");
4397 
4398 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4399 	}
4400 
4401 	mutex_exit(&uacp->usb_ac_mutex);
4402 
4403 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4404 	    "usb_ac_do_stop_record: End");
4405 }
4406 
4407 
4408 /*
4409  * Helper Functions for Mixer callbacks
4410  *
4411  * usb_ac_get_maxmin_volume:
4412  *	Send USBA command down to get the maximum or minimum gain balance
4413  *	Calculate min or max gain balance and return that. Return
4414  *	USB_FAILURE for failure cases
4415  */
4416 static int
4417 usb_ac_get_maxmin_volume(usb_ac_state_t *uacp, uint_t channel, int cmd,
4418     int dir, int feature_unitID)
4419 {
4420 	mblk_t		*data = NULL;
4421 	short		max_or_min;
4422 	usb_cr_t	cr;
4423 	usb_cb_flags_t	cb_flags;
4424 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4425 	    "usb_ac_get_maxmin_volume: channel=%d, cmd=%d dir=%d",
4426 	    channel, cmd, dir);
4427 
4428 	mutex_exit(&uacp->usb_ac_mutex);
4429 
4430 	if (usb_pipe_sync_ctrl_xfer(
4431 	    uacp->usb_ac_dip,
4432 	    uacp->usb_ac_default_ph,
4433 	    USB_DEV_REQ_DEV_TO_HOST |
4434 	    USB_DEV_REQ_TYPE_CLASS |
4435 	    USB_DEV_REQ_RCPT_IF,	/* bmRequestType */
4436 	    cmd,			/* bRequest */
4437 	    (USB_AUDIO_VOLUME_CONTROL << 8) | channel, /* wValue */
4438 					/* feature unit and id */
4439 	    (feature_unitID << 8)| uacp->usb_ac_ifno, /* wIndex */
4440 	    2,				/* wLength */
4441 	    &data,
4442 	    USB_ATTRS_NONE,
4443 	    &cr, &cb_flags,
4444 	    USB_FLAGS_SLEEP) != USB_SUCCESS) {
4445 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4446 		    "usb_ac_get_maxmin_volume: failed, "
4447 		    "cr=%d, cb=0x%x cmd=%d, data=0x%p",
4448 		    cr, cb_flags, cmd, data);
4449 
4450 		freemsg(data);
4451 		mutex_enter(&uacp->usb_ac_mutex);
4452 
4453 		return (USB_FAILURE);
4454 	}
4455 
4456 	mutex_enter(&uacp->usb_ac_mutex);
4457 	ASSERT((data->b_wptr - data->b_rptr) == 2);
4458 
4459 	max_or_min = (*(data->b_rptr+1) << 8) | *data->b_rptr;
4460 
4461 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4462 	    "usb_ac_get_maxmin_volume: max_or_min=0x%x", max_or_min);
4463 
4464 	freemsg(data);
4465 
4466 	return (max_or_min);
4467 }
4468 
4469 
4470 /*
4471  * usb_ac_set_volume:
4472  *	Send USBA command down to set the gain balance
4473  */
4474 static int
4475 usb_ac_set_volume(usb_ac_state_t *uacp, uint_t channel, short gain, int dir,
4476     int feature_unitID)
4477 {
4478 	mblk_t		*data = NULL;
4479 	usb_cr_t	cr;
4480 	usb_cb_flags_t	cb_flags;
4481 	int		rval = USB_FAILURE;
4482 
4483 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4484 	    "usb_ac_set_volume: channel=%d gain=%d dir=%d FU=%d",
4485 	    channel, gain, dir, feature_unitID);
4486 
4487 	mutex_exit(&uacp->usb_ac_mutex);
4488 
4489 	/* Construct the mblk_t from gain for sending to USBA */
4490 	data = allocb_wait(4, BPRI_HI, STR_NOSIG, NULL);
4491 
4492 	*(data->b_wptr++) = (char)gain;
4493 	*(data->b_wptr++) = (char)(gain >> 8);
4494 
4495 	if ((rval = usb_pipe_sync_ctrl_xfer(
4496 	    uacp->usb_ac_dip,
4497 	    uacp->usb_ac_default_ph,
4498 	    USB_DEV_REQ_HOST_TO_DEV |
4499 	    USB_DEV_REQ_TYPE_CLASS |
4500 	    USB_DEV_REQ_RCPT_IF,		/* bmRequestType */
4501 	    USB_AUDIO_SET_CUR,			/* bRequest */
4502 	    (USB_AUDIO_VOLUME_CONTROL << 8) | channel, /* wValue */
4503 						/* feature unit and id */
4504 	    (feature_unitID << 8) | uacp->usb_ac_ifno,	/* wIndex */
4505 	    2,					/* wLength */
4506 	    &data, 0,
4507 	    &cr, &cb_flags, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
4508 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4509 		    "usb_ac_set_volume: failed, cr=%d cb=0x%x",
4510 		    cr, cb_flags);
4511 	}
4512 
4513 	freemsg(data);
4514 	mutex_enter(&uacp->usb_ac_mutex);
4515 
4516 	return (rval);
4517 }
4518 
4519 
4520 /*
4521  * usb_ac_set_mute is called for each unit that supports the
4522  * requested control from usb_ac_traverse_connections
4523  */
4524 static int
4525 usb_ac_set_mute(usb_ac_state_t *uacp, uint_t featureID, uint_t dir,
4526     uint_t channel, uint_t control, uint_t muteval, uint_t *depth)
4527 {
4528 	mblk_t		*data;
4529 	usb_cr_t	cr;
4530 	usb_cb_flags_t	cb_flags;
4531 	int		rval = USB_FAILURE;
4532 
4533 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4534 	    "usb_ac_set_mute: muteval=0x%x, dir=%d", muteval, dir);
4535 
4536 	if (usb_ac_feature_unit_check(uacp, featureID,
4537 	    dir, channel, control, 0, depth) != USB_SUCCESS) {
4538 
4539 		return (USB_FAILURE);
4540 	}
4541 
4542 	mutex_exit(&uacp->usb_ac_mutex);
4543 
4544 	/* Construct the mblk_t for sending to USBA */
4545 	data = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL);
4546 	*(data->b_wptr++) = (char)muteval;
4547 
4548 	if ((rval = usb_pipe_sync_ctrl_xfer(
4549 	    uacp->usb_ac_dip,
4550 	    uacp->usb_ac_default_ph,
4551 	    USB_DEV_REQ_HOST_TO_DEV |
4552 	    USB_DEV_REQ_TYPE_CLASS |
4553 	    USB_DEV_REQ_RCPT_IF,		/* bmRequestType */
4554 	    USB_AUDIO_SET_CUR,			/* bRequest */
4555 	    (USB_AUDIO_MUTE_CONTROL << 8) | channel, /* wValue */
4556 						/* feature unit and id */
4557 	    (featureID << 8) | uacp->usb_ac_ifno, /* wIndex */
4558 	    1,					/* wLength */
4559 	    &data,
4560 	    0,					/* attributes */
4561 	    &cr, &cb_flags, 0)) != USB_SUCCESS) {
4562 
4563 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4564 		    "usb_ac_set_mute: failed, cr=%d cb=0x%x", cr, cb_flags);
4565 	}
4566 
4567 	freemsg(data);
4568 	mutex_enter(&uacp->usb_ac_mutex);
4569 
4570 	return (rval);
4571 }
4572 
4573 
4574 /*
4575  * usb_ac_send_as_cmd:
4576  *	Allocate message blk, send a command down to usb_as,
4577  *	wait for the reply and free the message
4578  *
4579  *	although not really needed to raise power if sending to as
4580  *	it seems better to ensure that both interfaces are at full power
4581  */
4582 static int
4583 usb_ac_send_as_cmd(usb_ac_state_t *uacp, usb_ac_plumbed_t *plumb_infop,
4584     int cmd, void *arg)
4585 {
4586 	mblk_t		*mp = NULL;
4587 	struct iocblk	*iocp;
4588 	queue_t 	*lwq = plumb_infop->acp_lwq;
4589 	usb_ac_streams_info_t *streams_infop;
4590 	int		error = USB_FAILURE;
4591 
4592 	ASSERT(mutex_owned(&uacp->usb_ac_mutex));
4593 	ASSERT(plumb_infop != NULL);
4594 
4595 	streams_infop = (usb_ac_streams_info_t *)plumb_infop->acp_data;
4596 	ASSERT(streams_infop != NULL);
4597 
4598 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4599 	    "usb_ac_send_as_cmd: Begin lwq=0x%p, cmd=0x%x, arg=0x%p",
4600 	    lwq, cmd, arg);
4601 
4602 	if (!canputnext(lwq)) {
4603 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4604 		    "usb_ac_send_as_cmd: canputnext failed");
4605 
4606 		return (error);
4607 	}
4608 
4609 	/*
4610 	 * Allocate mblk for a particular command
4611 	 */
4612 	switch (cmd) {
4613 	case USB_AUDIO_SET_FORMAT:
4614 		mp = usb_ac_allocate_req_mblk(uacp, cmd, (void *)arg,
4615 		    sizeof (usb_audio_formats_t));
4616 		break;
4617 	case USB_AUDIO_TEARDOWN:
4618 	case USB_AUDIO_STOP_RECORD:
4619 	case USB_AUDIO_PAUSE_PLAY:
4620 	case USB_AUDIO_SETUP:
4621 		mp = usb_ac_allocate_req_mblk(uacp, cmd, NULL, 0);
4622 		break;
4623 	case USB_AUDIO_START_RECORD:
4624 		mp = usb_ac_allocate_req_mblk(uacp, cmd, (void *)arg,
4625 		    sizeof (audiohdl_t *));
4626 		break;
4627 	case USB_AUDIO_SET_SAMPLE_FREQ:
4628 		mp = usb_ac_allocate_req_mblk(uacp, cmd, (void *)arg,
4629 		    sizeof (int));
4630 		break;
4631 	case USB_AUDIO_START_PLAY:
4632 		mp = usb_ac_allocate_req_mblk(uacp, cmd, (void *)arg,
4633 		    sizeof (usb_audio_play_req_t));
4634 		break;
4635 	default:
4636 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4637 		    "usb_ac_send_as_cmd: unknown cmd=%d", cmd);
4638 
4639 		return (error);
4640 	}
4641 
4642 	if (mp == NULL) {
4643 		USB_DPRINTF_L2(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4644 		    "usb_ac_send_as_cmd: can't get mblk to send cmd down");
4645 
4646 		return (error);
4647 	}
4648 
4649 	/*
4650 	 * Set wait flag and send message down; we have made sure
4651 	 * before that canputnext succeeds. Note mp will be freed down
4652 	 */
4653 	streams_infop->acs_ac_to_as_req.acr_wait_flag = 1;
4654 
4655 	mutex_exit(&uacp->usb_ac_mutex);
4656 	putnext(lwq, mp);
4657 	mutex_enter(&uacp->usb_ac_mutex);
4658 
4659 	/*
4660 	 * Wait for the response; reply will arrive through rput()
4661 	 * M_CTL and the cv_wait will be signaled there and wait flag
4662 	 * will be reset
4663 	 */
4664 	while (streams_infop->acs_ac_to_as_req.acr_wait_flag) {
4665 #ifndef DEBUG
4666 		cv_wait(&streams_infop->acs_ac_to_as_req.acr_cv,
4667 		    &uacp->usb_ac_mutex);
4668 #else
4669 		clock_t tm = ddi_get_lbolt() +
4670 					drv_usectohz(usb_ac_wait_timeout);
4671 		int rval;
4672 
4673 		rval = cv_timedwait(&streams_infop->acs_ac_to_as_req.acr_cv,
4674 		    &uacp->usb_ac_mutex, tm);
4675 
4676 		if (streams_infop->acs_ac_to_as_req.acr_wait_flag) {
4677 			if (rval == -1) {
4678 				USB_DPRINTF_L3(PRINT_MASK_ALL,
4679 				    uacp->usb_ac_log_handle,
4680 				    "usb_ac_send_as_cmd:"
4681 				    " timeout happen before cmd complete.");
4682 			} else {
4683 				USB_DPRINTF_L3(PRINT_MASK_ALL,
4684 				    uacp->usb_ac_log_handle,
4685 				    "usb_ac_send_as_cmd:"
4686 				    " not signaled by USB_AS_PLUMBED.");
4687 			}
4688 		}
4689 #endif
4690 	}
4691 
4692 	/* Wait is over, get the reply data */
4693 	mp = streams_infop->acs_ac_to_as_req.acr_reply_mp;
4694 	ASSERT(mp != NULL);
4695 
4696 	iocp = (struct iocblk *)mp->b_rptr;
4697 
4698 	USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4699 	    "usb_ac_send_as_cmd: db_type=0x%x cmd=0x%x",
4700 	    mp->b_datap->db_type, iocp->ioc_cmd);
4701 
4702 	switch (mp->b_datap->db_type) {
4703 	case M_CTL:
4704 		switch (iocp->ioc_cmd) {
4705 		case USB_AUDIO_SET_FORMAT:
4706 			/*
4707 			 * This command sets mixer format data
4708 			 * and returns alternate setting that matches
4709 			 */
4710 			ASSERT(mp->b_cont != NULL);
4711 			ASSERT((mp->b_cont->b_wptr - mp->b_cont->b_rptr) ==
4712 			    sizeof (int));
4713 			USB_DPRINTF_L3(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4714 			    "alternate returned %d",
4715 			    *((int *)(mp->b_cont->b_rptr)));
4716 
4717 			streams_infop->acs_ac_to_as_req.acr_curr_format.
4718 			    fmt_alt = *((int *)(mp->b_cont->b_rptr));
4719 
4720 			/*FALLTHROUGH*/
4721 		case USB_AUDIO_SET_SAMPLE_FREQ:
4722 		case USB_AUDIO_SETUP:
4723 		case USB_AUDIO_START_PLAY:
4724 		case USB_AUDIO_PAUSE_PLAY:
4725 		case USB_AUDIO_START_RECORD:
4726 		case USB_AUDIO_STOP_RECORD:
4727 		case USB_AUDIO_TEARDOWN:
4728 			error = USB_SUCCESS;
4729 			break;
4730 		default:
4731 			break;
4732 		}
4733 		break;
4734 	case M_ERROR:
4735 	default:
4736 		error = USB_FAILURE;
4737 	}
4738 
4739 	if (mp) {
4740 		usb_ac_free_mblk(mp);
4741 		streams_infop->acs_ac_to_as_req.acr_reply_mp = NULL;
4742 	}
4743 
4744 	return (error);
4745 }
4746 
4747 
4748 /*
4749  * usb_ac_allocate_req_mblk:
4750  *	Allocate a message block with the specified M_CTL cmd,
4751  *	The 2nd mblk contains the data for the command with a length len
4752  */
4753 static mblk_t *
4754 usb_ac_allocate_req_mblk(usb_ac_state_t *uacp, int cmd, void *buf, uint_t len)
4755 {
4756 	mblk_t	*mp, *mp2;
4757 	struct iocblk *mctlmsg;
4758 
4759 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4760 	    "usb_ac_allocate_req_mblk: cmd=0x%x, buf=0x%p, len=%d",
4761 	    cmd, buf, len);
4762 
4763 	mp = allocb_wait(sizeof (struct iocblk), BPRI_HI, STR_NOSIG, NULL);
4764 	mp->b_datap->db_type = M_CTL;
4765 	mctlmsg = (struct iocblk *)mp->b_datap->db_base;
4766 	mctlmsg->ioc_cmd = cmd;
4767 	mctlmsg->ioc_count = len;
4768 
4769 	mp->b_wptr = mp->b_wptr + sizeof (struct iocblk);
4770 
4771 	if ((len == 0) || (buf == NULL)) {
4772 
4773 		return (mp);
4774 	}
4775 
4776 	mp2 = allocb_wait(len, BPRI_HI, STR_NOSIG, NULL);
4777 	mp->b_cont = mp2;
4778 	bcopy(buf, mp->b_cont->b_datap->db_base, len);
4779 	mp->b_cont->b_wptr = mp->b_cont->b_wptr + len;
4780 
4781 	USB_DPRINTF_L4(PRINT_MASK_ALL, uacp->usb_ac_log_handle,
4782 	    "usb_ac_allocate_req_mblk: mp=0x%p", mp);
4783 
4784 	return (mp);
4785 }
4786 
4787 
4788 /*
4789  * usb_ac_free_mblk:
4790  *	Free the message block
4791  */
4792 static void
4793 usb_ac_free_mblk(mblk_t *mp)
4794 {
4795 	if (mp->b_cont) {
4796 		freemsg(mp->b_cont);
4797 		mp->b_cont = NULL;
4798 	}
4799 	freemsg(mp);
4800 }
4801 
4802 
4803 /*
4804  * usb_ac_serialize/release_access:
4805  */
4806 static void
4807 usb_ac_serialize_access(usb_ac_state_t	*uacp)
4808 {
4809 	(void) usb_serialize_access(uacp->usb_ac_ser_acc, USB_WAIT, 0);
4810 }
4811 
4812 static void
4813 usb_ac_release_access(usb_ac_state_t *uacp)
4814 {
4815 	usb_release_access(uacp->usb_ac_ser_acc);
4816 }
4817 
4818 
4819 static void
4820 usb_ac_pm_busy_component(usb_ac_state_t *usb_ac_statep)
4821 {
4822 	ASSERT(!mutex_owned(&usb_ac_statep->usb_ac_mutex));
4823 
4824 	if (usb_ac_statep->usb_ac_pm != NULL) {
4825 		mutex_enter(&usb_ac_statep->usb_ac_mutex);
4826 		usb_ac_statep->usb_ac_pm->acpm_pm_busy++;
4827 
4828 		USB_DPRINTF_L4(PRINT_MASK_PM,
4829 		    usb_ac_statep->usb_ac_log_handle,
4830 		    "usb_ac_pm_busy_component: %d",
4831 		    usb_ac_statep->usb_ac_pm->acpm_pm_busy);
4832 
4833 		mutex_exit(&usb_ac_statep->usb_ac_mutex);
4834 
4835 		if (pm_busy_component(usb_ac_statep->usb_ac_dip, 0) !=
4836 		    DDI_SUCCESS) {
4837 			mutex_enter(&usb_ac_statep->usb_ac_mutex);
4838 			usb_ac_statep->usb_ac_pm->acpm_pm_busy--;
4839 
4840 			USB_DPRINTF_L2(PRINT_MASK_PM,
4841 			    usb_ac_statep->usb_ac_log_handle,
4842 			    "usb_ac_pm_busy_component failed: %d",
4843 			    usb_ac_statep->usb_ac_pm->acpm_pm_busy);
4844 
4845 			mutex_exit(&usb_ac_statep->usb_ac_mutex);
4846 		}
4847 	}
4848 }
4849 
4850 
4851 static void
4852 usb_ac_pm_idle_component(usb_ac_state_t *usb_ac_statep)
4853 {
4854 	ASSERT(!mutex_owned(&usb_ac_statep->usb_ac_mutex));
4855 
4856 	if (usb_ac_statep->usb_ac_pm != NULL) {
4857 		if (pm_idle_component(usb_ac_statep->usb_ac_dip, 0) ==
4858 		    DDI_SUCCESS) {
4859 			mutex_enter(&usb_ac_statep->usb_ac_mutex);
4860 			ASSERT(usb_ac_statep->usb_ac_pm->acpm_pm_busy > 0);
4861 			usb_ac_statep->usb_ac_pm->acpm_pm_busy--;
4862 
4863 			USB_DPRINTF_L4(PRINT_MASK_PM,
4864 			    usb_ac_statep->usb_ac_log_handle,
4865 			    "usb_ac_pm_idle_component: %d",
4866 			    usb_ac_statep->usb_ac_pm->acpm_pm_busy);
4867 
4868 			mutex_exit(&usb_ac_statep->usb_ac_mutex);
4869 		}
4870 	}
4871 }
4872