xref: /freebsd/sys/dev/sound/pcm/channel.h (revision c824383b269d8abe175ea4751194660716d5600e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7  * All rights reserved.
8  * Copyright (c) 2024-2025 The FreeBSD Foundation
9  *
10  * Portions of this software were developed by Christos Margiolis
11  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 struct pcmchan_caps {
36 	u_int32_t minspeed, maxspeed;
37 	u_int32_t *fmtlist;
38 	u_int32_t caps;
39 };
40 
41 struct pcmchan_matrix {
42 	int id;
43 	uint8_t channels, ext;
44 	struct {
45 		int type;
46 		uint32_t members;
47 	} map[SND_CHN_T_MAX + 1];
48 	uint32_t mask;
49 	int8_t offset[SND_CHN_T_MAX];
50 };
51 
52 /* Forward declarations */
53 struct pcm_channel;
54 struct pcmchan_syncgroup;
55 struct pcmchan_syncmember;
56 
57 extern struct mtx snd_pcm_syncgroups_mtx;
58 extern SLIST_HEAD(pcm_synclist, pcmchan_syncgroup) snd_pcm_syncgroups;
59 
60 #define PCM_SG_LOCK()	    mtx_lock(&snd_pcm_syncgroups_mtx)
61 #define PCM_SG_TRYLOCK()    mtx_trylock(&snd_pcm_syncgroups_mtx)
62 #define PCM_SG_UNLOCK()	    mtx_unlock(&snd_pcm_syncgroups_mtx)
63 #define PCM_SG_LOCKASSERT(arg)	mtx_assert(&snd_pcm_syncgroups_mtx, arg)
64 
65 /**
66  * @brief Specifies an audio device sync group
67  */
68 struct pcmchan_syncgroup {
69 	SLIST_ENTRY(pcmchan_syncgroup) link;
70 	SLIST_HEAD(, pcmchan_syncmember) members;
71 	int id; /**< Group identifier; set to address of group. */
72 };
73 
74 /**
75  * @brief Specifies a container for members of a sync group
76  */
77 struct pcmchan_syncmember {
78 	SLIST_ENTRY(pcmchan_syncmember) link;
79 	struct pcmchan_syncgroup *parent; /**< group head */
80 	struct pcm_channel *ch;
81 };
82 
83 #define	CHN_NAMELEN		32
84 #define	CHN_COMM_UNUSED		"<UNUSED>"
85 #define	CHN_COMM_UNKNOWN	"<UNKNOWN>"
86 
87 struct pcm_channel {
88 	kobj_t methods;
89 
90 	pid_t pid;
91 	struct pcm_feeder *feeder;
92 	u_int32_t align;
93 
94 	int latency;
95 	u_int32_t speed;
96 	u_int32_t format;
97 	u_int32_t flags;
98 	u_int32_t feederflags;
99 	u_int64_t blocks;
100 
101 	int direction;
102 	unsigned int interrupts, xruns, feedcount;
103 	unsigned int timeout;
104 	struct snd_dbuf *bufhard, *bufsoft;
105 	struct snddev_info *parentsnddev;
106 	struct pcm_channel *parentchannel;
107 	void *devinfo;
108 	device_t dev;
109 	int unit;
110 	int type;
111 	char name[CHN_NAMELEN];
112 	char comm[MAXCOMLEN + 1];
113 	struct mtx *lock;
114 	int trigger;
115 	/**
116 	 * For interrupt manipulations.
117 	 */
118 	struct cv intr_cv;
119 	/**
120 	 * Increment,decrement this around operations that temporarily yield
121 	 * lock.
122 	 */
123 	unsigned int inprog;
124 	/* Incrememnt/decrement around cv_timedwait_sig() in chn_sleep(). */
125 	unsigned int sleeping;
126 	/**
127 	 * Special channel operations should examine @c inprog after acquiring
128 	 * lock.  If zero, operations may continue.  Else, thread should
129 	 * wait on this cv for previous operation to finish.
130 	 */
131 	struct cv cv;
132 	/**
133 	 * Low water mark for select()/poll().
134 	 *
135 	 * This is initialized to the channel's fragment size, and will be
136 	 * overwritten if a new fragment size is set.  Users may alter this
137 	 * value directly with the @c SNDCTL_DSP_LOW_WATER ioctl.
138 	 */
139 	unsigned int lw;
140 	/**
141 	 * If part of a sync group, this will point to the syncmember
142 	 * container.
143 	 */
144 	struct pcmchan_syncmember *sm;
145 #ifdef OSSV4_EXPERIMENT
146 	u_int16_t lpeak, rpeak;	/**< Peak value from 0-32767. */
147 #endif
148 
149 	struct {
150 		SLIST_HEAD(, pcm_channel) head;
151 		SLIST_ENTRY(pcm_channel) link;
152 		struct {
153 			SLIST_HEAD(, pcm_channel) head;
154 			SLIST_ENTRY(pcm_channel) link;
155 		} busy;
156 	} children;
157 
158 	struct {
159 		struct {
160 			SLIST_ENTRY(pcm_channel) link;
161 			struct {
162 				SLIST_ENTRY(pcm_channel) link;
163 			} busy;
164 			struct {
165 				SLIST_ENTRY(pcm_channel) link;
166 			} opened;
167 			struct {
168 				SLIST_ENTRY(pcm_channel) link;
169 			} primary;
170 		} pcm;
171 	} channels;
172 
173 	struct pcmchan_matrix matrix;
174   	struct pcmchan_matrix matrix_scratch;
175 
176 	int16_t volume[SND_VOL_C_MAX][SND_CHN_T_VOL_MAX];
177   	int8_t muted[SND_VOL_C_MAX][SND_CHN_T_VOL_MAX];
178 };
179 
180 #define CHN_HEAD(x, y)			&(x)->y.head
181 #define CHN_INIT(x, y)			SLIST_INIT(CHN_HEAD(x, y))
182 #define CHN_LINK(y)			y.link
183 #define CHN_EMPTY(x, y)			SLIST_EMPTY(CHN_HEAD(x, y))
184 #define CHN_FIRST(x, y)			SLIST_FIRST(CHN_HEAD(x, y))
185 #define CHN_NEXT(elm, list)		SLIST_NEXT((elm), CHN_LINK(list))
186 
187 #define CHN_FOREACH(x, y, z)						\
188 	SLIST_FOREACH(x, CHN_HEAD(y, z), CHN_LINK(z))
189 
190 #define CHN_FOREACH_SAFE(w, x, y, z)					\
191 	SLIST_FOREACH_SAFE(w, CHN_HEAD(x, z), CHN_LINK(z), y)
192 
193 #define CHN_INSERT_HEAD(x, y, z)					\
194 	SLIST_INSERT_HEAD(CHN_HEAD(x, z), y, CHN_LINK(z))
195 
196 #define CHN_INSERT_AFTER(x, y, z)					\
197 	SLIST_INSERT_AFTER(x, y, CHN_LINK(z))
198 
199 #define CHN_INSERT_HEAD_SAFE(x, y, z)		do {			\
200 	struct pcm_channel *t = NULL;					\
201 	CHN_FOREACH(t, x, z) {						\
202 		if (t == y)						\
203 			break;						\
204 	} 								\
205 	if (t != y)							\
206 		CHN_INSERT_HEAD(x, y, z);				\
207 } while (0)
208 
209 #define CHN_INSERT_AFTER_SAFE(w, x, y, z)	do {			\
210 	struct pcm_channel *t = NULL;					\
211 	CHN_FOREACH(t, w, z) {						\
212 		if (t == y)						\
213 			break;						\
214 	} 								\
215 	if (t != y)							\
216 		CHN_INSERT_AFTER(x, y, z);				\
217 } while (0)
218 
219 #define CHN_REMOVE(holder, elm, list)		do {			\
220 	if (CHN_FIRST(holder, list) == (elm)) {				\
221 		SLIST_REMOVE_HEAD(CHN_HEAD(holder, list), CHN_LINK(list)); \
222 	} else {							\
223 		struct pcm_channel *t = NULL;				\
224 		CHN_FOREACH(t, holder, list) {				\
225 			if (CHN_NEXT(t, list) == (elm)) {		\
226 				SLIST_REMOVE_AFTER(t, CHN_LINK(list));	\
227 				break;					\
228 			}						\
229 		}							\
230 	}								\
231 } while (0)
232 
233 #define CHN_INSERT_SORT(w, x, y, z)		do {			\
234 	struct pcm_channel *t, *a = NULL;				\
235 	CHN_FOREACH(t, x, z) {						\
236 		if (((y)->type w t->type) ||				\
237 		    (((y)->type == t->type) && ((y)->unit w t->unit)))	\
238 			a = t;						\
239 		else							\
240 			break;						\
241 	}								\
242 	if (a != NULL)							\
243 		CHN_INSERT_AFTER(a, y, z);				\
244 	else								\
245 		CHN_INSERT_HEAD(x, y, z);				\
246 } while (0)
247 
248 #define CHN_INSERT_SORT_ASCEND(x, y, z)		CHN_INSERT_SORT(>, x, y, z)
249 #define CHN_INSERT_SORT_DESCEND(x, y, z)	CHN_INSERT_SORT(<, x, y, z)
250 
251 #define CHN_BUF_PARENT(x, y)						\
252 	(((x) != NULL && (x)->parentchannel != NULL &&			\
253 	(x)->parentchannel->bufhard != NULL) ?				\
254 	(x)->parentchannel->bufhard : (y))
255 
256 #include "channel_if.h"
257 
258 int chn_reinit(struct pcm_channel *c);
259 int chn_write(struct pcm_channel *c, struct uio *buf);
260 int chn_read(struct pcm_channel *c, struct uio *buf);
261 u_int32_t chn_start(struct pcm_channel *c, int force);
262 int chn_sync(struct pcm_channel *c, int threshold);
263 int chn_flush(struct pcm_channel *c);
264 int chn_poll(struct pcm_channel *c, int ev, struct thread *td);
265 
266 char *chn_mkname(char *buf, size_t len, struct pcm_channel *c);
267 struct pcm_channel *chn_init(struct snddev_info *d, struct pcm_channel *parent,
268     kobj_class_t cls, int dir, void *devinfo);
269 void chn_kill(struct pcm_channel *c);
270 void chn_shutdown(struct pcm_channel *c);
271 int chn_release(struct pcm_channel *c);
272 int chn_reset(struct pcm_channel *c, u_int32_t fmt, u_int32_t spd);
273 int chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
274     int center);
275 int chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val);
276 int chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt);
277 int chn_setmute_multi(struct pcm_channel *c, int vc, int mute);
278 int chn_setmute_matrix(struct pcm_channel *c, int vc, int vt, int mute);
279 int chn_getmute_matrix(struct pcm_channel *c, int vc, int vt);
280 void chn_vpc_reset(struct pcm_channel *c, int vc, int force);
281 int chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed);
282 int chn_setspeed(struct pcm_channel *c, uint32_t speed);
283 int chn_setformat(struct pcm_channel *c, uint32_t format);
284 int chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz);
285 int chn_setlatency(struct pcm_channel *c, int latency);
286 void chn_syncstate(struct pcm_channel *c);
287 int chn_trigger(struct pcm_channel *c, int go);
288 int chn_getptr(struct pcm_channel *c);
289 struct pcmchan_caps *chn_getcaps(struct pcm_channel *c);
290 u_int32_t chn_getformats(struct pcm_channel *c);
291 
292 struct pcmchan_matrix *chn_getmatrix(struct pcm_channel *);
293 int chn_setmatrix(struct pcm_channel *, struct pcmchan_matrix *);
294 
295 int chn_oss_getorder(struct pcm_channel *, unsigned long long *);
296 int chn_oss_setorder(struct pcm_channel *, unsigned long long *);
297 int chn_oss_getmask(struct pcm_channel *, uint32_t *);
298 
299 void chn_resetbuf(struct pcm_channel *c);
300 void chn_intr_locked(struct pcm_channel *c);
301 void chn_intr(struct pcm_channel *c);
302 int chn_abort(struct pcm_channel *c);
303 
304 int chn_notify(struct pcm_channel *c, u_int32_t flags);
305 
306 int chn_getrates(struct pcm_channel *c, int **rates);
307 int chn_syncdestroy(struct pcm_channel *c);
308 
309 #define CHN_SETVOLUME(...)		chn_setvolume_matrix(__VA_ARGS__)
310 #if defined(SND_DIAGNOSTIC) || defined(INVARIANTS)
311 #define CHN_GETVOLUME(...)		chn_getvolume_matrix(__VA_ARGS__)
312 #else
313 #define CHN_GETVOLUME(x, y, z)		((x)->volume[y][z])
314 #endif
315 
316 #define CHN_GETMUTE(x, y, z)		((x)->muted[y][z])
317 
318 #ifdef OSSV4_EXPERIMENT
319 int chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak);
320 #endif
321 
322 #define CHN_LOCKOWNED(c)	mtx_owned((c)->lock)
323 #define CHN_LOCK(c)		mtx_lock((c)->lock)
324 #define CHN_UNLOCK(c)		mtx_unlock((c)->lock)
325 #define CHN_TRYLOCK(c)		mtx_trylock((c)->lock)
326 #define CHN_LOCKASSERT(c)	mtx_assert((c)->lock, MA_OWNED)
327 #define CHN_UNLOCKASSERT(c)	mtx_assert((c)->lock, MA_NOTOWNED)
328 
329 #define CHN_BROADCAST(x)	cv_broadcastpri(x, PRIBIO)
330 
331 int snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist);
332 
333 uint32_t snd_str2afmt(const char *);
334 uint32_t snd_afmt2str(uint32_t, char *, size_t);
335 
336 #define AFMTSTR_LEN	16
337 
338 extern int chn_latency;
339 extern int chn_latency_profile;
340 extern int report_soft_formats;
341 extern int report_soft_matrix;
342 
343 enum {
344 	PCMDIR_PLAY = 1,
345 	PCMDIR_PLAY_VIRTUAL,
346 	PCMDIR_REC,
347 	PCMDIR_REC_VIRTUAL,
348 };
349 
350 #define PCMTRIG_START 1
351 #define PCMTRIG_EMLDMAWR 2
352 #define PCMTRIG_EMLDMARD 3
353 #define PCMTRIG_STOP 0
354 #define PCMTRIG_ABORT -1
355 
356 #define PCMTRIG_COMMON(x)	((x) == PCMTRIG_START ||		\
357 				 (x) == PCMTRIG_STOP ||			\
358 				 (x) == PCMTRIG_ABORT)
359 
360 #define CHN_F_CLOSING           0x00000001  /* a pending close */
361 #define CHN_F_ABORTING          0x00000002  /* a pending abort */
362 #define CHN_F_RUNNING		0x00000004  /* dma is running */
363 #define CHN_F_TRIGGERED		0x00000008
364 #define CHN_F_NOTRIGGER		0x00000010
365 /* unused			0x00000020 */
366 
367 #define CHN_F_NBIO              0x00000040  /* do non-blocking i/o */
368 #define CHN_F_MMAP		0x00000080  /* has been mmap()ed */
369 
370 #define CHN_F_BUSY              0x00000100  /* has been opened 	*/
371 #define CHN_F_DIRTY		0x00000200  /* need re-config */
372 #define CHN_F_DEAD		0x00000400  /* too many errors, dead, mdk */
373 /* unused			0x00000800 */
374 
375 #define	CHN_F_HAS_SIZE		0x00001000  /* user set block size */
376 #define CHN_F_HAS_VCHAN		0x00002000  /* vchan master */
377 
378 #define CHN_F_VCHAN_PASSTHROUGH	0x00004000  /* digital ac3/dts passthrough */
379 #define CHN_F_VCHAN_ADAPTIVE	0x00008000  /* adaptive format/rate selection */
380 #define CHN_F_VCHAN_DYNAMIC	(CHN_F_VCHAN_PASSTHROUGH | CHN_F_VCHAN_ADAPTIVE)
381 
382 #define	CHN_F_VIRTUAL		0x10000000  /* not backed by hardware */
383 #define CHN_F_BITPERFECT	0x20000000  /* un-cooked, Heh.. */
384 #define CHN_F_PASSTHROUGH	0x40000000  /* passthrough re-config */
385 #define CHN_F_EXCLUSIVE		0x80000000  /* exclusive access */
386 
387 #define CHN_F_BITS		"\020"					\
388 				"\001CLOSING"				\
389 				"\002ABORTING"				\
390 				"\003RUNNING"				\
391 				"\004TRIGGERED"				\
392 				"\005NOTRIGGER"				\
393 				/* \006 */				\
394 				"\007NBIO"				\
395 				"\010MMAP"				\
396 				"\011BUSY"				\
397 				"\012DIRTY"				\
398 				"\013DEAD"				\
399 				/* \014 */				\
400 				"\015HAS_SIZE"				\
401 				"\016HAS_VCHAN"				\
402 				"\017VCHAN_PASSTHROUGH"			\
403 				"\020VCHAN_ADAPTIVE"			\
404 				"\035VIRTUAL"				\
405 				"\036BITPERFECT"			\
406 				"\037PASSTHROUGH"			\
407 				"\040EXCLUSIVE"
408 
409 #define CHN_F_RESET		(CHN_F_BUSY | CHN_F_DEAD |		\
410 				 CHN_F_VIRTUAL | CHN_F_HAS_VCHAN |	\
411 				 CHN_F_VCHAN_DYNAMIC |			\
412 				 CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE)
413 
414 #define CHN_F_MMAP_INVALID	(CHN_F_DEAD | CHN_F_RUNNING)
415 
416 
417 
418 #define CHN_N_RATE		0x00000001
419 #define CHN_N_FORMAT		0x00000002
420 #define CHN_N_VOLUME		0x00000004
421 #define CHN_N_BLOCKSIZE		0x00000008
422 #define CHN_N_TRIGGER		0x00000010
423 
424 #define CHN_LATENCY_MIN		0
425 #define CHN_LATENCY_MAX		10
426 #define	CHN_LATENCY_DEFAULT	2	/* 21.3ms total buffering */
427 #define CHN_POLICY_MIN		CHN_LATENCY_MIN
428 #define CHN_POLICY_MAX		CHN_LATENCY_MAX
429 #define CHN_POLICY_DEFAULT	CHN_LATENCY_DEFAULT
430 
431 #define CHN_LATENCY_PROFILE_MIN		0
432 #define CHN_LATENCY_PROFILE_MAX		1
433 #define CHN_LATENCY_PROFILE_DEFAULT	CHN_LATENCY_PROFILE_MAX
434 
435 #define CHN_STARTED(c)		((c)->flags & CHN_F_TRIGGERED)
436 #define CHN_STOPPED(c)		(!CHN_STARTED(c))
437 #define CHN_DIRSTR(c)		(((c)->direction == PCMDIR_PLAY) ? \
438 				"PCMDIR_PLAY" : "PCMDIR_REC")
439 #define CHN_BITPERFECT(c)	((c)->flags & CHN_F_BITPERFECT)
440 #define CHN_PASSTHROUGH(c)	((c)->flags & CHN_F_PASSTHROUGH)
441 
442 #define CHN_TIMEOUT		5
443 #define CHN_TIMEOUT_MIN		1
444 #define CHN_TIMEOUT_MAX		10
445 
446 /*
447  * This should be large enough to hold all pcm data between
448  * tsleeps in chn_{read,write} at the highest sample rate.
449  * (which is usually 48kHz * 16bit * stereo = 192000 bytes/sec)
450  */
451 #define CHN_2NDBUFBLKSIZE	(2 * 1024)
452 /* The total number of blocks per secondary bufhard. */
453 #define CHN_2NDBUFBLKNUM	(32)
454 /* The size of a whole secondary bufhard. */
455 #define CHN_2NDBUFMAXSIZE	(131072)
456 
457 #define CHANNEL_DECLARE(name) static DEFINE_CLASS(name, name ## _methods, sizeof(struct kobj))
458