xref: /illumos-gate/usr/src/uts/common/io/audio/impl/audio_impl.h (revision 17f1e64a433a4ca00ffed7539e10c297580a7002)
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 
175 	/*
176 	 * Linkage for per-device list of clients.
177 	 */
178 	list_node_t		c_global_linkage;
179 	list_node_t		c_dev_linkage;
180 	int			c_refcnt;
181 
182 	kmutex_t		c_lock;
183 	kcondvar_t		c_cv;
184 	ddi_taskq_t		*c_tq;
185 	boolean_t		c_do_output;
186 	boolean_t		c_do_input;
187 	boolean_t		c_do_notify;
188 	boolean_t		c_do_drain;
189 	boolean_t		c_closing;
190 	boolean_t		c_is_open;
191 
192 	/*
193 	 * Client wide settings... e.g. ops vector, etc.
194 	 */
195 	unsigned		c_omode;	/* open mode */
196 	pid_t			c_pid;		/* opening process id */
197 	audio_dev_t		*c_dev;
198 	cred_t			*c_cred;
199 	audio_client_ops_t	c_ops;
200 #define	c_open			c_ops.aco_open
201 #define	c_close			c_ops.aco_close
202 #define	c_read			c_ops.aco_read
203 #define	c_write			c_ops.aco_write
204 #define	c_ioctl			c_ops.aco_ioctl
205 #define	c_chpoll		c_ops.aco_chpoll
206 #define	c_output		c_ops.aco_output
207 #define	c_input			c_ops.aco_input
208 #define	c_notify		c_ops.aco_notify
209 #define	c_drain			c_ops.aco_drain
210 
211 	struct pollhead		c_pollhead;
212 
213 };
214 
215 struct audio_infostr {
216 	char			i_line[100];
217 	list_node_t		i_linkage;
218 };
219 
220 struct audio_stats {
221 	kstat_named_t		st_head;
222 	kstat_named_t		st_tail;
223 	kstat_named_t		st_flags;
224 	kstat_named_t		st_fragfr;
225 	kstat_named_t		st_nfrags;
226 	kstat_named_t		st_framesz;
227 	kstat_named_t		st_nbytes;
228 	kstat_named_t		st_hidx;
229 	kstat_named_t		st_tidx;
230 	kstat_named_t		st_format;
231 	kstat_named_t		st_nchan;
232 	kstat_named_t		st_rate;
233 	kstat_named_t		st_intrs;
234 	kstat_named_t		st_errors;
235 	kstat_named_t		st_suspended;
236 };
237 
238 /*
239  * An audio engine corresponds to a single DMA transfer channel.  It can
240  * represent either record or playback, but not both at the same time.
241  * A device that supports simultaneous record and playback will register
242  * separate channels.
243  */
244 struct audio_engine {
245 	audio_engine_ops_t	e_ops;
246 	void			*e_private;
247 	unsigned		e_flags;
248 
249 	/*
250 	 * Mixing related fields.
251 	 */
252 	unsigned		e_limiter_state;
253 	int32_t			*e_chbufs[AUDIO_MAX_CHANNELS];
254 	unsigned		e_choffs[AUDIO_MAX_CHANNELS];
255 	unsigned		e_chincr[AUDIO_MAX_CHANNELS];
256 	void			(*e_export)(audio_engine_t *);
257 	void			(*e_import)(audio_engine_t *, audio_stream_t *);
258 
259 	/*
260 	 * Underlying physical buffer shared with device driver.
261 	 */
262 	audio_buffer_t		e_buf;
263 #define	e_head			e_buf.b_head
264 #define	e_tail			e_buf.b_tail
265 #define	e_data			e_buf.b_data
266 #define	e_fragfr		e_buf.b_fragfr
267 #define	e_fragbytes		e_buf.b_fragbytes
268 #define	e_framesz		e_buf.b_framesz
269 #define	e_nbytes		e_buf.b_nbytes
270 #define	e_nframes		e_buf.b_nframes
271 #define	e_nfrags		e_buf.b_nfrags
272 #define	e_hidx			e_buf.b_hidx
273 #define	e_tidx			e_buf.b_tidx
274 
275 	int			e_intrs;
276 	int			e_errors;
277 
278 	audio_parms_t		e_parms;
279 #define	e_format		e_parms.p_format
280 #define	e_nchan			e_parms.p_nchan
281 #define	e_rate			e_parms.p_rate
282 
283 	/*
284 	 * Statistics.
285 	 */
286 	kstat_t			*e_ksp;
287 	struct audio_stats	e_stats;
288 
289 
290 	/*
291 	 * Synchronization.
292 	 */
293 	kmutex_t		e_lock;
294 
295 	/*
296 	 * Linkage for per-device list.
297 	 */
298 	list_node_t		e_dev_linkage;
299 	audio_dev_t		*e_dev;
300 	int			e_num;	/* arbitrary engine number */
301 
302 	/*
303 	 * List of of streams attached to this engine.
304 	 */
305 	list_t			e_streams;
306 	int			e_nrunning;
307 	boolean_t		e_suspended;
308 };
309 
310 struct audio_dev {
311 	dev_info_t		*d_dip;
312 	major_t			d_major;
313 	int			d_instance;
314 
315 	uint32_t		d_flags;
316 #define	DEV_OUTPUT_CAP		(1U << 0)
317 #define	DEV_INPUT_CAP		(1U << 1)
318 #define	DEV_DUPLEX_CAP		(1U << 2)
319 #define	DEV_SNDSTAT_CAP		(1U << 3)
320 
321 	char			d_name[128];	/* generic description */
322 	char			d_desc[128];	/* detailed config descr */
323 	char			d_vers[128];	/* detailed version descr */
324 	int			d_number;	/* global /dev/audioXX # */
325 	int			d_index;	/* master device index */
326 	int			d_engno;	/* engine counter */
327 
328 	list_t			d_hwinfo;	/* strings of hw info */
329 
330 	/*
331 	 * Synchronization.
332 	 */
333 	kmutex_t		d_lock;
334 	kcondvar_t		d_cv;
335 	krwlock_t		d_ctrl_lock;	/* leaf lock */
336 	krwlock_t		d_clnt_lock;
337 	unsigned		d_refcnt;
338 
339 	/*
340 	 * Lists of virtual clients, controls and engines.  Protected by
341 	 * the d_lock field above.
342 	 */
343 	list_t			d_clients;
344 	list_t			d_engines;
345 	list_t			d_controls;
346 	audio_ctrl_t		*d_pcmvol_ctrl;
347 	uint64_t		d_pcmvol;
348 
349 	/*
350 	 * Linkage onto global list of devices.
351 	 */
352 	list_node_t		d_by_index;
353 	list_node_t		d_by_number;
354 
355 	/*
356 	 * Personality specific data.
357 	 */
358 	void			*d_minor_data[1 << AUDIO_MN_TYPE_NBITS];
359 };
360 
361 /*
362  * Each audio_dev optionally can have controls attached to it.
363  * Controls are separate from audio engines. They are methods of
364  * adjusting pharameters or reading metrics that usually relate to
365  * hardware on devices engine by the driver. They can be things like
366  * master volume for example.
367  *
368  * If the driver does not support controls then it must insure
369  * that any hardware controls are initialized to a usable state.
370  *
371  * For the framework/user-apps to be able to change controls
372  * the driver must create, enable and configure controls with
373  * control API's.
374  *
375  * There are a number of common controls (well-known) that most
376  * hardware supports. These have known names and known ctrl numbers.
377  * In addition a driver can have any number of extention
378  * controls (device-private). These can have any name and any ctrl
379  * number other then the ones, defined as well-knonw ones.
380  *
381  * Only controls created through control API's will be available,
382  * well-known or device-private.
383  */
384 struct	audio_ctrl {
385 	audio_ctrl_desc_t	ctrl_des;
386 #define	ctrl_name		ctrl_des.acd_name
387 #define	ctrl_type		ctrl_des.acd_type
388 #define	ctrl_enum		ctrl_des.acd_enum
389 #define	ctrl_flags		ctrl_des.acd_flags
390 	audio_dev_t		*ctrl_dev;
391 	audio_ctrl_rd_t		ctrl_read_fn;
392 	audio_ctrl_wr_t		ctrl_write_fn;
393 	list_node_t		ctrl_linkage;
394 	kmutex_t		ctrl_lock;
395 	void			*ctrl_arg;
396 };
397 
398 
399 /*
400  * Prototypes.
401  */
402 
403 /* audio_format.c */
404 int auimpl_format_alloc(audio_stream_t *);
405 void auimpl_format_free(audio_stream_t *);
406 int auimpl_format_setup(audio_stream_t *, audio_parms_t *);
407 
408 /* audio_output.c */
409 void auimpl_export_16ne(audio_engine_t *);
410 void auimpl_export_16oe(audio_engine_t *);
411 void auimpl_export_24ne(audio_engine_t *);
412 void auimpl_export_24oe(audio_engine_t *);
413 void auimpl_export_32ne(audio_engine_t *);
414 void auimpl_export_32oe(audio_engine_t *);
415 void auimpl_output_callback(audio_engine_t *);
416 
417 /* audio_input.c */
418 void auimpl_import_16ne(audio_engine_t *, audio_stream_t *);
419 void auimpl_import_16oe(audio_engine_t *, audio_stream_t *);
420 void auimpl_import_24ne(audio_engine_t *, audio_stream_t *);
421 void auimpl_import_24oe(audio_engine_t *, audio_stream_t *);
422 void auimpl_import_32ne(audio_engine_t *, audio_stream_t *);
423 void auimpl_import_32oe(audio_engine_t *, audio_stream_t *);
424 void auimpl_input_callback(audio_engine_t *);
425 int auimpl_input_drain(audio_stream_t *);
426 
427 /* audio_client.c */
428 void auimpl_client_init(void);
429 void auimpl_client_fini(void);
430 audio_client_t *auimpl_client_create(dev_t);
431 void auimpl_client_destroy(audio_client_t *);
432 int auimpl_create_minors(audio_dev_t *);
433 void auimpl_remove_minors(audio_dev_t *);
434 void auimpl_set_gain_master(audio_stream_t *, uint8_t);
435 
436 /* audio_engine.c */
437 void auimpl_dev_init(void);
438 void auimpl_dev_fini(void);
439 void auimpl_dev_hold(audio_dev_t *);
440 audio_dev_t *auimpl_dev_hold_by_devt(dev_t);
441 audio_dev_t *auimpl_dev_hold_by_index(int);
442 void auimpl_dev_release(audio_dev_t *);
443 int auimpl_choose_format(int);
444 
445 int auimpl_engine_open(audio_dev_t *, int, int, audio_stream_t *);
446 void auimpl_engine_close(audio_stream_t *);
447 
448 void auimpl_dev_walk_engines(audio_dev_t *,
449     int (*)(audio_engine_t *, void *), void *);
450 
451 void auimpl_dev_vwarn(audio_dev_t *, const char *, va_list);
452 
453 /* engine operations */
454 #define	E_OP(e, entry)		((e)->e_ops.audio_engine_##entry)
455 #define	E_PRV(e)		((e)->e_private)
456 #define	ENG_FORMAT(e)		E_OP(e, format)(E_PRV(e))
457 #define	ENG_RATE(e)		E_OP(e, rate)(E_PRV(e))
458 #define	ENG_CHANNELS(e)		E_OP(e, channels)(E_PRV(e))
459 #define	ENG_SYNC(e, num)	E_OP(e, sync)(E_PRV(e), num)
460 #define	ENG_START(e)		E_OP(e, start)(E_PRV(e))
461 #define	ENG_STOP(e)		E_OP(e, stop)(E_PRV(e))
462 #define	ENG_COUNT(e)		E_OP(e, count)(E_PRV(e))
463 #define	ENG_QLEN(e)		E_OP(e, qlen)(E_PRV(e))
464 #define	ENG_CLOSE(e)		E_OP(e, close)(E_PRV(e))
465 #define	ENG_OPEN(e, s, nf, d) 	E_OP(e, open)(E_PRV(e), e->e_flags, s, nf, d)
466 #define	ENG_CHINFO(e, c, o, i)	E_OP(e, chinfo(E_PRV(e), c, o, i))
467 
468 /* audio_sun.c */
469 void auimpl_sun_init(void);
470 
471 /* audio_oss.c */
472 void auimpl_oss_init(void);
473 
474 #endif	/* _AUDIO_IMPL_H */
475