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