xref: /illumos-gate/usr/src/uts/common/io/audio/impl/audio_impl.h (revision 08dcd69c259e91563982b9e7715366f99fee816e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (C) 4Front Technologies 1996-2008.
23  *
24  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #ifndef	_AUDIO_IMPL_H
29 #define	_AUDIO_IMPL_H
30 
31 #include <sys/types.h>
32 #include <sys/list.h>
33 #include <sys/poll.h>
34 
35 #include <sys/audio/audio_driver.h>
36 #include "audio_client.h"
37 
38 #define	AUDIO_MAX_OPENS		256
39 #define	AUDIO_MAX_CHANNELS	16
40 #define	AUDIO_UNIT_EXPAND	1024
41 #define	AUDIO_CHBUFS		2048	/* samples for mixing */
42 #define	AUDIO_VOL_SCALE		256
43 #define	AUDIO_DB_SIZE		50
44 
45 struct audio_parms {
46 	int		p_format;
47 	int		p_rate;
48 	int		p_nchan;
49 };
50 
51 typedef int (*audio_cnv_func_t)(audio_stream_t *, int);
52 
53 struct audio_buffer {
54 	caddr_t			b_data;
55 	uint64_t		b_head;
56 	uint64_t		b_tail;
57 	unsigned		b_hidx;		/* head % nframes */
58 	unsigned		b_tidx;		/* tail % nframes */
59 	unsigned		b_fragfr;	/* frames per frag */
60 	unsigned		b_fragbytes;	/* bytes per frag */
61 	unsigned		b_nframes;	/* total frames */
62 	unsigned		b_nbytes;	/* total bytes */
63 	unsigned		b_nfrags;	/* total frags */
64 	unsigned		b_framesz;	/* bytes per frame  */
65 };
66 
67 /*
68  * struct audio_stream: This structure represents a virtual stream exposed
69  * to a single client.  Each client will have at most two of these (one for
70  * record, one for playback.)
71  */
72 struct audio_stream {
73 	audio_buffer_t		s_buf;
74 #define	s_data			s_buf.b_data
75 #define	s_bufsz			s_buf.b_size
76 #define	s_head			s_buf.b_head
77 #define	s_tail			s_buf.b_tail
78 #define	s_nfrags		s_buf.b_nfrags
79 #define	s_framesz		s_buf.b_framesz
80 #define	s_fragfr		s_buf.b_fragfr
81 #define	s_fragbytes		s_buf.b_fragbytes
82 #define	s_nframes		s_buf.b_nframes
83 #define	s_nbytes		s_buf.b_nbytes
84 #define	s_tidx			s_buf.b_tidx
85 #define	s_hidx			s_buf.b_hidx
86 	ddi_umem_cookie_t	s_cookie;
87 	uint32_t		s_allocsz;
88 	uint32_t		s_hintsz;	/* latency hints */
89 	uint16_t		s_hintfrags;
90 
91 	/*
92 	 * Various counters.
93 	 */
94 	uint64_t		s_samples;
95 	uint64_t		s_errors;	/* underrun or overrun count */
96 
97 	boolean_t		s_running;
98 	boolean_t		s_paused;	/* stream paused */
99 	boolean_t		s_draining;	/* stream draining */
100 
101 	/*
102 	 * Sample rate conversion (SRC) and format conversion details.
103 	 */
104 	struct grc3state	*s_src_state[AUDIO_MAX_CHANNELS];
105 	unsigned		s_src_quality;
106 	int			s_cnv_max;
107 	audio_cnv_func_t	s_converter;
108 	uint32_t		*s_cnv_buf0;
109 	uint32_t		*s_cnv_buf1;
110 	void			*s_cnv_src;
111 	void			*s_cnv_dst;
112 	audio_parms_t		s_cnv_src_parms;
113 #define	s_cnv_src_nchan		s_cnv_src_parms.p_nchan
114 #define	s_cnv_src_rate		s_cnv_src_parms.p_rate
115 #define	s_cnv_src_format	s_cnv_src_parms.p_format
116 
117 	audio_parms_t		s_cnv_dst_parms;
118 #define	s_cnv_dst_nchan		s_cnv_dst_parms.p_nchan
119 #define	s_cnv_dst_rate		s_cnv_dst_parms.p_rate
120 #define	s_cnv_dst_format	s_cnv_dst_parms.p_format
121 
122 	size_t			s_cnv_cnt;
123 	int32_t			*s_cnv_ptr;
124 
125 	audio_parms_t		*s_user_parms;
126 	audio_parms_t		*s_phys_parms;
127 
128 	/*
129 	 * Volume.
130 	 */
131 	uint8_t			s_gain_master;
132 	uint8_t			s_gain_pct;
133 	uint16_t		s_gain_scaled;
134 	uint16_t		s_gain_eff;
135 	boolean_t		s_muted;
136 
137 	/*
138 	 * Callbacks.
139 	 */
140 	uint64_t		s_drain_idx;	/* engine index */
141 
142 	/*
143 	 * Other per stream details, e.g. channel offset, etc.
144 	 */
145 	kmutex_t		s_lock;
146 	kcondvar_t		s_cv;
147 	list_node_t		s_eng_linkage;	/*  place on engine list */
148 	audio_client_t		*s_client;
149 	audio_engine_t		*s_engine;
150 	int			s_choffs;
151 
152 	/*
153 	 * Other bits.
154 	 */
155 	unsigned		s_engcap;	/* ENGINE_xxx_CAP */
156 };
157 
158 /*
159  * struct audio_client: This structure represents a logical port,
160  * associated with an open file, etc.  These are the entities that are
161  * mixed.
162  */
163 struct audio_client {
164 	audio_stream_t		c_istream;
165 	audio_stream_t		c_ostream;
166 	void			*c_private;
167 
168 	/*
169 	 * DDI support.
170 	 */
171 	major_t			c_major;
172 	minor_t			c_minor;
173 	minor_t			c_origminor;
174 	queue_t			*c_rq;
175 	queue_t			*c_wq;
176 
177 	/*
178 	 * Linkage for per-device list of clients.
179 	 */
180 	list_node_t		c_global_linkage;
181 	list_node_t		c_dev_linkage;
182 	int			c_refcnt;
183 
184 	kmutex_t		c_lock;
185 	kcondvar_t		c_cv;
186 	boolean_t		c_is_active;
187 
188 	/*
189 	 * Client wide settings... e.g. ops vector, etc.
190 	 */
191 	unsigned		c_omode;	/* open mode */
192 	pid_t			c_pid;		/* opening process id */
193 	audio_dev_t		*c_dev;
194 	cred_t			*c_cred;
195 	audio_client_ops_t	c_ops;
196 #define	c_open			c_ops.aco_open
197 #define	c_close			c_ops.aco_close
198 #define	c_read			c_ops.aco_read
199 #define	c_write			c_ops.aco_write
200 #define	c_ioctl			c_ops.aco_ioctl
201 #define	c_chpoll		c_ops.aco_chpoll
202 #define	c_output		c_ops.aco_output
203 #define	c_input			c_ops.aco_input
204 #define	c_notify		c_ops.aco_notify
205 #define	c_drain			c_ops.aco_drain
206 #define	c_wput			c_ops.aco_wput
207 #define	c_wsrv			c_ops.aco_wsrv
208 #define	c_rsrv			c_ops.aco_rsrv
209 
210 	struct pollhead		c_pollhead;
211 
212 };
213 
214 struct audio_infostr {
215 	char			i_line[100];
216 	list_node_t		i_linkage;
217 };
218 
219 struct audio_stats {
220 	kstat_named_t		st_head;
221 	kstat_named_t		st_tail;
222 	kstat_named_t		st_flags;
223 	kstat_named_t		st_fragfr;
224 	kstat_named_t		st_nfrags;
225 	kstat_named_t		st_framesz;
226 	kstat_named_t		st_nbytes;
227 	kstat_named_t		st_hidx;
228 	kstat_named_t		st_tidx;
229 	kstat_named_t		st_format;
230 	kstat_named_t		st_nchan;
231 	kstat_named_t		st_rate;
232 	kstat_named_t		st_intrs;
233 	kstat_named_t		st_errors;
234 	kstat_named_t		st_engine_underruns;
235 	kstat_named_t		st_engine_overruns;
236 	kstat_named_t		st_stream_underruns;
237 	kstat_named_t		st_stream_overruns;
238 	kstat_named_t		st_suspended;
239 };
240 
241 /*
242  * An audio engine corresponds to a single DMA transfer channel.  It can
243  * represent either record or playback, but not both at the same time.
244  * A device that supports simultaneous record and playback will register
245  * separate channels.
246  */
247 struct audio_engine {
248 	audio_engine_ops_t	e_ops;
249 	void			*e_private;
250 	unsigned		e_flags;
251 
252 	/*
253 	 * Mixing related fields.
254 	 */
255 	unsigned		e_limiter_state;
256 	int32_t			*e_chbufs[AUDIO_MAX_CHANNELS];
257 	unsigned		e_choffs[AUDIO_MAX_CHANNELS];
258 	unsigned		e_chincr[AUDIO_MAX_CHANNELS];
259 	void			(*e_export)(audio_engine_t *);
260 	void			(*e_import)(audio_engine_t *, audio_stream_t *);
261 
262 	/*
263 	 * Underlying physical buffer shared with device driver.
264 	 */
265 	audio_buffer_t		e_buf;
266 #define	e_head			e_buf.b_head
267 #define	e_tail			e_buf.b_tail
268 #define	e_data			e_buf.b_data
269 #define	e_fragfr		e_buf.b_fragfr
270 #define	e_fragbytes		e_buf.b_fragbytes
271 #define	e_framesz		e_buf.b_framesz
272 #define	e_nbytes		e_buf.b_nbytes
273 #define	e_nframes		e_buf.b_nframes
274 #define	e_nfrags		e_buf.b_nfrags
275 #define	e_hidx			e_buf.b_hidx
276 #define	e_tidx			e_buf.b_tidx
277 
278 	unsigned		e_playahead;
279 
280 	int			e_intrs;
281 	int			e_errors;
282 	int			e_overruns;
283 	int			e_underruns;
284 	int			e_stream_overruns;
285 	int			e_stream_underruns;
286 
287 	audio_parms_t		e_parms;
288 #define	e_format		e_parms.p_format
289 #define	e_nchan			e_parms.p_nchan
290 #define	e_rate			e_parms.p_rate
291 
292 	/*
293 	 * Statistics.
294 	 */
295 	kstat_t			*e_ksp;
296 	struct audio_stats	e_stats;
297 
298 
299 	/*
300 	 * Synchronization.
301 	 */
302 	kmutex_t		e_lock;
303 
304 	/*
305 	 * Linkage for per-device list.
306 	 */
307 	list_node_t		e_dev_linkage;
308 	audio_dev_t		*e_dev;
309 	int			e_num;	/* arbitrary engine number */
310 
311 	/*
312 	 * List of of streams attached to this engine.
313 	 */
314 	list_t			e_streams;
315 	int			e_nrunning;
316 	boolean_t		e_suspended;
317 };
318 
319 struct audio_dev {
320 	dev_info_t		*d_dip;
321 	major_t			d_major;
322 	int			d_instance;
323 
324 	uint32_t		d_flags;
325 #define	DEV_OUTPUT_CAP		(1U << 0)
326 #define	DEV_INPUT_CAP		(1U << 1)
327 #define	DEV_DUPLEX_CAP		(1U << 2)
328 #define	DEV_SNDSTAT_CAP		(1U << 3)
329 #define	DEV_OPAQUE_CAP		(1U << 4)	/* AC3 are not mixable */
330 
331 	char			d_name[128];	/* generic description */
332 	char			d_desc[128];	/* detailed config descr */
333 	char			d_vers[128];	/* detailed version descr */
334 	int			d_number;	/* global /dev/audioXX # */
335 	int			d_index;	/* master device index */
336 	int			d_engno;	/* engine counter */
337 
338 	list_t			d_hwinfo;	/* strings of hw info */
339 
340 	/*
341 	 * Synchronization.
342 	 */
343 	kmutex_t		d_lock;
344 	kcondvar_t		d_cv;
345 	krwlock_t		d_ctrl_lock;	/* leaf lock */
346 	krwlock_t		d_clnt_lock;
347 	unsigned		d_refcnt;
348 
349 	/*
350 	 * Lists of virtual clients, controls and engines.  Protected by
351 	 * the d_lock field above.
352 	 */
353 	list_t			d_clients;
354 	list_t			d_engines;
355 	list_t			d_controls;
356 	audio_ctrl_t		*d_pcmvol_ctrl;
357 	uint64_t		d_pcmvol;
358 
359 	volatile unsigned	d_serial;
360 
361 	/*
362 	 * Linkage onto global list of devices.
363 	 */
364 	list_node_t		d_by_index;
365 	list_node_t		d_by_number;
366 
367 	/*
368 	 * Personality specific data.
369 	 */
370 	void			*d_minor_data[1 << AUDIO_MN_TYPE_NBITS];
371 };
372 
373 /*
374  * Each audio_dev optionally can have controls attached to it.
375  * Controls are separate from audio engines. They are methods of
376  * adjusting pharameters or reading metrics that usually relate to
377  * hardware on devices engine by the driver. They can be things like
378  * master volume for example.
379  *
380  * If the driver does not support controls then it must insure
381  * that any hardware controls are initialized to a usable state.
382  *
383  * For the framework/user-apps to be able to change controls
384  * the driver must create, enable and configure controls with
385  * control API's.
386  *
387  * There are a number of common controls (well-known) that most
388  * hardware supports. These have known names and known ctrl numbers.
389  * In addition a driver can have any number of extention
390  * controls (device-private). These can have any name and any ctrl
391  * number other then the ones, defined as well-knonw ones.
392  *
393  * Only controls created through control API's will be available,
394  * well-known or device-private.
395  */
396 struct	audio_ctrl {
397 	audio_ctrl_desc_t	ctrl_des;
398 #define	ctrl_name		ctrl_des.acd_name
399 #define	ctrl_type		ctrl_des.acd_type
400 #define	ctrl_enum		ctrl_des.acd_enum
401 #define	ctrl_flags		ctrl_des.acd_flags
402 	audio_dev_t		*ctrl_dev;
403 	audio_ctrl_rd_t		ctrl_read_fn;
404 	audio_ctrl_wr_t		ctrl_write_fn;
405 	list_node_t		ctrl_linkage;
406 	kmutex_t		ctrl_lock;
407 	void			*ctrl_arg;
408 };
409 
410 
411 /*
412  * Prototypes.
413  */
414 
415 /* audio_format.c */
416 int auimpl_format_alloc(audio_stream_t *);
417 void auimpl_format_free(audio_stream_t *);
418 int auimpl_format_setup(audio_stream_t *, audio_parms_t *);
419 
420 /* audio_output.c */
421 void auimpl_export_16ne(audio_engine_t *);
422 void auimpl_export_16oe(audio_engine_t *);
423 void auimpl_export_24ne(audio_engine_t *);
424 void auimpl_export_24oe(audio_engine_t *);
425 void auimpl_export_32ne(audio_engine_t *);
426 void auimpl_export_32oe(audio_engine_t *);
427 void auimpl_output_callback(audio_engine_t *);
428 
429 /* audio_input.c */
430 void auimpl_import_16ne(audio_engine_t *, audio_stream_t *);
431 void auimpl_import_16oe(audio_engine_t *, audio_stream_t *);
432 void auimpl_import_24ne(audio_engine_t *, audio_stream_t *);
433 void auimpl_import_24oe(audio_engine_t *, audio_stream_t *);
434 void auimpl_import_32ne(audio_engine_t *, audio_stream_t *);
435 void auimpl_import_32oe(audio_engine_t *, audio_stream_t *);
436 void auimpl_input_callback(audio_engine_t *);
437 int auimpl_input_drain(audio_stream_t *);
438 
439 /* audio_client.c */
440 void auimpl_client_init(void);
441 void auimpl_client_fini(void);
442 audio_client_t *auimpl_client_create(dev_t);
443 void auimpl_client_destroy(audio_client_t *);
444 void auimpl_client_activate(audio_client_t *);
445 void auimpl_client_deactivate(audio_client_t *);
446 int auimpl_create_minors(audio_dev_t *);
447 void auimpl_remove_minors(audio_dev_t *);
448 int auimpl_set_pcmvol(void *, uint64_t);
449 int auimpl_get_pcmvol(void *, uint64_t *);
450 
451 /* audio_engine.c */
452 void auimpl_dev_init(void);
453 void auimpl_dev_fini(void);
454 void auimpl_dev_hold(audio_dev_t *);
455 audio_dev_t *auimpl_dev_hold_by_devt(dev_t);
456 audio_dev_t *auimpl_dev_hold_by_index(int);
457 void auimpl_dev_release(audio_dev_t *);
458 int auimpl_choose_format(int);
459 
460 int auimpl_engine_open(audio_dev_t *, int, int, audio_stream_t *);
461 void auimpl_engine_close(audio_stream_t *);
462 
463 void auimpl_dev_walk_engines(audio_dev_t *,
464     int (*)(audio_engine_t *, void *), void *);
465 
466 void auimpl_dev_vwarn(audio_dev_t *, const char *, va_list);
467 
468 /* engine operations */
469 #define	E_OP(e, entry)		((e)->e_ops.audio_engine_##entry)
470 #define	E_PRV(e)		((e)->e_private)
471 #define	ENG_FORMAT(e)		E_OP(e, format)(E_PRV(e))
472 #define	ENG_RATE(e)		E_OP(e, rate)(E_PRV(e))
473 #define	ENG_CHANNELS(e)		E_OP(e, channels)(E_PRV(e))
474 #define	ENG_SYNC(e, num)	E_OP(e, sync)(E_PRV(e), num)
475 #define	ENG_START(e)		E_OP(e, start)(E_PRV(e))
476 #define	ENG_STOP(e)		E_OP(e, stop)(E_PRV(e))
477 #define	ENG_COUNT(e)		E_OP(e, count)(E_PRV(e))
478 #define	ENG_QLEN(e)		E_OP(e, qlen)(E_PRV(e))
479 #define	ENG_PLAYAHEAD(e)	E_OP(e, playahead)(E_PRV(e))
480 #define	ENG_CLOSE(e)		E_OP(e, close)(E_PRV(e))
481 #define	ENG_OPEN(e, s, nf, d) 	E_OP(e, open)(E_PRV(e), e->e_flags, s, nf, d)
482 #define	ENG_CHINFO(e, c, o, i)	E_OP(e, chinfo(E_PRV(e), c, o, i))
483 
484 /* audio_sun.c */
485 void auimpl_sun_init(void);
486 
487 /* audio_oss.c */
488 void auimpl_oss_init(void);
489 
490 #endif	/* _AUDIO_IMPL_H */
491