1193640Sariff/*-
2193640Sariff * Copyright (c) 2007-2009 Ariff Abdullah <ariff@FreeBSD.org>
3193640Sariff * All rights reserved.
4193640Sariff *
5193640Sariff * Redistribution and use in source and binary forms, with or without
6193640Sariff * modification, are permitted provided that the following conditions
7193640Sariff * are met:
8193640Sariff * 1. Redistributions of source code must retain the above copyright
9193640Sariff *    notice, this list of conditions and the following disclaimer.
10193640Sariff * 2. Redistributions in binary form must reproduce the above copyright
11193640Sariff *    notice, this list of conditions and the following disclaimer in the
12193640Sariff *    documentation and/or other materials provided with the distribution.
13193640Sariff *
14193640Sariff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15193640Sariff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16193640Sariff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17193640Sariff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18193640Sariff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19193640Sariff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20193640Sariff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21193640Sariff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22193640Sariff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23193640Sariff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24193640Sariff * SUCH DAMAGE.
25193640Sariff *
26193640Sariff * $FreeBSD$
27193640Sariff */
28193640Sariff
29193640Sariff#ifndef _SND_SNDSTAT_H_
30193640Sariff#define _SND_SNDSTAT_H_
31193640Sariff
32193640Sariff#define SNDSTAT_PREPARE_PCM_ARGS					\
33193640Sariff	struct sbuf *s, device_t dev, int verbose
34193640Sariff
35193640Sariff#define SNDSTAT_PREPARE_PCM_BEGIN()	do {				\
36193640Sariff	struct snddev_info *d;						\
37193640Sariff	struct pcm_channel *c;						\
38193640Sariff	struct pcm_feeder *f;						\
39193640Sariff									\
40193640Sariff	d = device_get_softc(dev);					\
41193640Sariff	PCM_BUSYASSERT(d);						\
42193640Sariff									\
43193640Sariff	if (CHN_EMPTY(d, channels.pcm)) {				\
44193640Sariff		sbuf_printf(s, " (mixer only)");			\
45193640Sariff		return (0);						\
46193640Sariff	}								\
47193640Sariff									\
48202267Smav	if (verbose < 1) {						\
49202267Smav		sbuf_printf(s, " (%s%s%s",				\
50202267Smav		    d->playcount ? "play" : "",				\
51202267Smav		    (d->playcount && d->reccount) ? "/" : "",		\
52202267Smav		    d->reccount ? "rec" : "");				\
53202267Smav	} else {							\
54202267Smav		sbuf_printf(s, " (%dp:%dv/%dr:%dv",			\
55202267Smav		    d->playcount, d->pvchancount,			\
56202267Smav		    d->reccount, d->rvchancount);			\
57202267Smav	}								\
58202267Smav	sbuf_printf(s, "%s)%s",						\
59202267Smav	    ((d->playcount != 0 && d->reccount != 0) &&			\
60202267Smav	    (d->flags & SD_F_SIMPLEX)) ? " simplex" : "",		\
61193640Sariff	    (device_get_unit(dev) == snd_unit) ? " default" : "")
62193640Sariff
63193640Sariff
64193640Sariff#define SNDSTAT_PREPARE_PCM_END()					\
65193640Sariff	if (verbose <= 1)						\
66193640Sariff		return (0);						\
67193640Sariff									\
68193640Sariff	sbuf_printf(s, "\n\t");						\
69193640Sariff	sbuf_printf(s, "snddev flags=0x%b", d->flags, SD_F_BITS);	\
70193640Sariff									\
71193640Sariff	CHN_FOREACH(c, d, channels.pcm) {				\
72193640Sariff									\
73193640Sariff		KASSERT(c->bufhard != NULL && c->bufsoft != NULL,	\
74193640Sariff		    ("hosed pcm channel setup"));			\
75193640Sariff									\
76193640Sariff		sbuf_printf(s, "\n\t");					\
77193640Sariff									\
78193640Sariff		sbuf_printf(s, "%s[%s]: ",				\
79193640Sariff		    (c->parentchannel != NULL) ?			\
80193640Sariff		    c->parentchannel->name : "", c->name);		\
81193640Sariff		sbuf_printf(s, "spd %d", c->speed);			\
82193640Sariff		if (c->speed != sndbuf_getspd(c->bufhard))		\
83193640Sariff			sbuf_printf(s, "/%d",				\
84193640Sariff			    sndbuf_getspd(c->bufhard));			\
85193640Sariff		sbuf_printf(s, ", fmt 0x%08x", c->format);		\
86193640Sariff		if (c->format != sndbuf_getfmt(c->bufhard))		\
87193640Sariff			sbuf_printf(s, "/0x%08x",			\
88193640Sariff			    sndbuf_getfmt(c->bufhard));			\
89193640Sariff		sbuf_printf(s, ", flags 0x%08x, 0x%08x",		\
90193640Sariff		    c->flags, c->feederflags);				\
91193640Sariff		if (c->pid != -1)					\
92193640Sariff			sbuf_printf(s, ", pid %d (%s)",			\
93193640Sariff			    c->pid, c->comm);				\
94193640Sariff		sbuf_printf(s, "\n\t");					\
95193640Sariff									\
96193640Sariff		sbuf_printf(s, "interrupts %d, ", c->interrupts);	\
97193640Sariff									\
98193640Sariff		if (c->direction == PCMDIR_REC)				\
99193640Sariff			sbuf_printf(s,					\
100193640Sariff			    "overruns %d, feed %u, hfree %d, "		\
101193640Sariff			    "sfree %d [b:%d/%d/%d|bs:%d/%d/%d]",	\
102193640Sariff				c->xruns, c->feedcount,			\
103193640Sariff				sndbuf_getfree(c->bufhard),		\
104193640Sariff				sndbuf_getfree(c->bufsoft),		\
105193640Sariff				sndbuf_getsize(c->bufhard),		\
106193640Sariff				sndbuf_getblksz(c->bufhard),		\
107193640Sariff				sndbuf_getblkcnt(c->bufhard),		\
108193640Sariff				sndbuf_getsize(c->bufsoft),		\
109193640Sariff				sndbuf_getblksz(c->bufsoft),		\
110193640Sariff				sndbuf_getblkcnt(c->bufsoft));		\
111193640Sariff		else							\
112193640Sariff			sbuf_printf(s,					\
113193640Sariff			    "underruns %d, feed %u, ready %d "		\
114193640Sariff			    "[b:%d/%d/%d|bs:%d/%d/%d]",			\
115193640Sariff				c->xruns, c->feedcount,			\
116193640Sariff				sndbuf_getready(c->bufsoft),		\
117193640Sariff				sndbuf_getsize(c->bufhard),		\
118193640Sariff				sndbuf_getblksz(c->bufhard),		\
119193640Sariff				sndbuf_getblkcnt(c->bufhard),		\
120193640Sariff				sndbuf_getsize(c->bufsoft),		\
121193640Sariff				sndbuf_getblksz(c->bufsoft),		\
122193640Sariff				sndbuf_getblkcnt(c->bufsoft));		\
123193640Sariff		sbuf_printf(s, "\n\t");					\
124193640Sariff									\
125193640Sariff		sbuf_printf(s, "channel flags=0x%b", c->flags,		\
126193640Sariff		    CHN_F_BITS);					\
127193640Sariff		sbuf_printf(s, "\n\t");					\
128193640Sariff									\
129193640Sariff		sbuf_printf(s, "{%s}",					\
130193640Sariff		    (c->direction == PCMDIR_REC) ? "hardware" :		\
131193640Sariff		    "userland");					\
132193640Sariff		sbuf_printf(s, " -> ");					\
133193640Sariff		f = c->feeder;						\
134193640Sariff		while (f->source != NULL)				\
135193640Sariff			f = f->source;					\
136193640Sariff		while (f != NULL) {					\
137193640Sariff			sbuf_printf(s, "%s", f->class->name);		\
138193640Sariff			if (f->desc->type == FEEDER_FORMAT)		\
139193640Sariff				sbuf_printf(s, "(0x%08x -> 0x%08x)",	\
140193640Sariff				    f->desc->in, f->desc->out);		\
141193640Sariff			else if (f->desc->type == FEEDER_MATRIX)	\
142193640Sariff				sbuf_printf(s, "(%d.%d -> %d.%d)",	\
143193640Sariff				    AFMT_CHANNEL(f->desc->in) -		\
144193640Sariff				    AFMT_EXTCHANNEL(f->desc->in),	\
145193640Sariff				    AFMT_EXTCHANNEL(f->desc->in),	\
146193640Sariff				    AFMT_CHANNEL(f->desc->out) -	\
147193640Sariff				    AFMT_EXTCHANNEL(f->desc->out),	\
148193640Sariff				    AFMT_EXTCHANNEL(f->desc->out));	\
149193640Sariff			else if (f->desc->type == FEEDER_RATE)		\
150193640Sariff				sbuf_printf(s,				\
151193640Sariff				    "(0x%08x q:%d %d -> %d)",		\
152193640Sariff				    f->desc->out,			\
153193640Sariff				    FEEDER_GET(f, FEEDRATE_QUALITY),	\
154193640Sariff				    FEEDER_GET(f, FEEDRATE_SRC),	\
155193640Sariff				    FEEDER_GET(f, FEEDRATE_DST));	\
156193640Sariff			else						\
157193640Sariff				sbuf_printf(s, "(0x%08x)",		\
158193640Sariff				    f->desc->out);			\
159193640Sariff			sbuf_printf(s, " -> ");				\
160193640Sariff			f = f->parent;					\
161193640Sariff		}							\
162193640Sariff		sbuf_printf(s, "{%s}",					\
163193640Sariff		    (c->direction == PCMDIR_REC) ? "userland" :		\
164193640Sariff		    "hardware");					\
165193640Sariff	}								\
166193640Sariff									\
167193640Sariff	return (0);							\
168193640Sariff} while (0)
169193640Sariff
170193640Sariff#endif	/* !_SND_SNDSTAT_H_ */
171