xref: /illumos-gate/usr/src/uts/common/io/audio/impl/audio_impl.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 	ddi_taskq_t		*c_tq;
187 	boolean_t		c_do_output;
188 	boolean_t		c_do_input;
189 	boolean_t		c_do_notify;
190 	boolean_t		c_do_drain;
191 	boolean_t		c_closing;
192 	boolean_t		c_is_open;
193 
194 	/*
195 	 * Client wide settings... e.g. ops vector, etc.
196 	 */
197 	unsigned		c_omode;	/* open mode */
198 	pid_t			c_pid;		/* opening process id */
199 	audio_dev_t		*c_dev;
200 	cred_t			*c_cred;
201 	audio_client_ops_t	c_ops;
202 #define	c_open			c_ops.aco_open
203 #define	c_close			c_ops.aco_close
204 #define	c_read			c_ops.aco_read
205 #define	c_write			c_ops.aco_write
206 #define	c_ioctl			c_ops.aco_ioctl
207 #define	c_chpoll		c_ops.aco_chpoll
208 #define	c_output		c_ops.aco_output
209 #define	c_input			c_ops.aco_input
210 #define	c_notify		c_ops.aco_notify
211 #define	c_drain			c_ops.aco_drain
212 #define	c_wput			c_ops.aco_wput
213 #define	c_wsrv			c_ops.aco_wsrv
214 
215 	struct pollhead		c_pollhead;
216 
217 };
218 
219 struct audio_infostr {
220 	char			i_line[100];
221 	list_node_t		i_linkage;
222 };
223 
224 struct audio_stats {
225 	kstat_named_t		st_head;
226 	kstat_named_t		st_tail;
227 	kstat_named_t		st_flags;
228 	kstat_named_t		st_fragfr;
229 	kstat_named_t		st_nfrags;
230 	kstat_named_t		st_framesz;
231 	kstat_named_t		st_nbytes;
232 	kstat_named_t		st_hidx;
233 	kstat_named_t		st_tidx;
234 	kstat_named_t		st_format;
235 	kstat_named_t		st_nchan;
236 	kstat_named_t		st_rate;
237 	kstat_named_t		st_intrs;
238 	kstat_named_t		st_errors;
239 	kstat_named_t		st_engine_underruns;
240 	kstat_named_t		st_engine_overruns;
241 	kstat_named_t		st_stream_underruns;
242 	kstat_named_t		st_stream_overruns;
243 	kstat_named_t		st_suspended;
244 };
245 
246 /*
247  * An audio engine corresponds to a single DMA transfer channel.  It can
248  * represent either record or playback, but not both at the same time.
249  * A device that supports simultaneous record and playback will register
250  * separate channels.
251  */
252 struct audio_engine {
253 	audio_engine_ops_t	e_ops;
254 	void			*e_private;
255 	unsigned		e_flags;
256 
257 	/*
258 	 * Mixing related fields.
259 	 */
260 	unsigned		e_limiter_state;
261 	int32_t			*e_chbufs[AUDIO_MAX_CHANNELS];
262 	unsigned		e_choffs[AUDIO_MAX_CHANNELS];
263 	unsigned		e_chincr[AUDIO_MAX_CHANNELS];
264 	void			(*e_export)(audio_engine_t *);
265 	void			(*e_import)(audio_engine_t *, audio_stream_t *);
266 
267 	/*
268 	 * Underlying physical buffer shared with device driver.
269 	 */
270 	audio_buffer_t		e_buf;
271 #define	e_head			e_buf.b_head
272 #define	e_tail			e_buf.b_tail
273 #define	e_data			e_buf.b_data
274 #define	e_fragfr		e_buf.b_fragfr
275 #define	e_fragbytes		e_buf.b_fragbytes
276 #define	e_framesz		e_buf.b_framesz
277 #define	e_nbytes		e_buf.b_nbytes
278 #define	e_nframes		e_buf.b_nframes
279 #define	e_nfrags		e_buf.b_nfrags
280 #define	e_hidx			e_buf.b_hidx
281 #define	e_tidx			e_buf.b_tidx
282 
283 	int			e_intrs;
284 	int			e_errors;
285 	int			e_overruns;
286 	int			e_underruns;
287 	int			e_stream_overruns;
288 	int			e_stream_underruns;
289 
290 	audio_parms_t		e_parms;
291 #define	e_format		e_parms.p_format
292 #define	e_nchan			e_parms.p_nchan
293 #define	e_rate			e_parms.p_rate
294 
295 	/*
296 	 * Statistics.
297 	 */
298 	kstat_t			*e_ksp;
299 	struct audio_stats	e_stats;
300 
301 
302 	/*
303 	 * Synchronization.
304 	 */
305 	kmutex_t		e_lock;
306 
307 	/*
308 	 * Linkage for per-device list.
309 	 */
310 	list_node_t		e_dev_linkage;
311 	audio_dev_t		*e_dev;
312 	int			e_num;	/* arbitrary engine number */
313 
314 	/*
315 	 * List of of streams attached to this engine.
316 	 */
317 	list_t			e_streams;
318 	int			e_nrunning;
319 	boolean_t		e_suspended;
320 };
321 
322 struct audio_dev {
323 	dev_info_t		*d_dip;
324 	major_t			d_major;
325 	int			d_instance;
326 
327 	uint32_t		d_flags;
328 #define	DEV_OUTPUT_CAP		(1U << 0)
329 #define	DEV_INPUT_CAP		(1U << 1)
330 #define	DEV_DUPLEX_CAP		(1U << 2)
331 #define	DEV_SNDSTAT_CAP		(1U << 3)
332 
333 	char			d_name[128];	/* generic description */
334 	char			d_desc[128];	/* detailed config descr */
335 	char			d_vers[128];	/* detailed version descr */
336 	int			d_number;	/* global /dev/audioXX # */
337 	int			d_index;	/* master device index */
338 	int			d_engno;	/* engine counter */
339 
340 	list_t			d_hwinfo;	/* strings of hw info */
341 
342 	/*
343 	 * Synchronization.
344 	 */
345 	kmutex_t		d_lock;
346 	kcondvar_t		d_cv;
347 	krwlock_t		d_ctrl_lock;	/* leaf lock */
348 	krwlock_t		d_clnt_lock;
349 	unsigned		d_refcnt;
350 
351 	/*
352 	 * Lists of virtual clients, controls and engines.  Protected by
353 	 * the d_lock field above.
354 	 */
355 	list_t			d_clients;
356 	list_t			d_engines;
357 	list_t			d_controls;
358 	audio_ctrl_t		*d_pcmvol_ctrl;
359 	uint64_t		d_pcmvol;
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 int auimpl_create_minors(audio_dev_t *);
445 void auimpl_remove_minors(audio_dev_t *);
446 void auimpl_set_gain_master(audio_stream_t *, uint8_t);
447 
448 /* audio_engine.c */
449 void auimpl_dev_init(void);
450 void auimpl_dev_fini(void);
451 void auimpl_dev_hold(audio_dev_t *);
452 audio_dev_t *auimpl_dev_hold_by_devt(dev_t);
453 audio_dev_t *auimpl_dev_hold_by_index(int);
454 void auimpl_dev_release(audio_dev_t *);
455 int auimpl_choose_format(int);
456 
457 int auimpl_engine_open(audio_dev_t *, int, int, audio_stream_t *);
458 void auimpl_engine_close(audio_stream_t *);
459 
460 void auimpl_dev_walk_engines(audio_dev_t *,
461     int (*)(audio_engine_t *, void *), void *);
462 
463 void auimpl_dev_vwarn(audio_dev_t *, const char *, va_list);
464 
465 /* engine operations */
466 #define	E_OP(e, entry)		((e)->e_ops.audio_engine_##entry)
467 #define	E_PRV(e)		((e)->e_private)
468 #define	ENG_FORMAT(e)		E_OP(e, format)(E_PRV(e))
469 #define	ENG_RATE(e)		E_OP(e, rate)(E_PRV(e))
470 #define	ENG_CHANNELS(e)		E_OP(e, channels)(E_PRV(e))
471 #define	ENG_SYNC(e, num)	E_OP(e, sync)(E_PRV(e), num)
472 #define	ENG_START(e)		E_OP(e, start)(E_PRV(e))
473 #define	ENG_STOP(e)		E_OP(e, stop)(E_PRV(e))
474 #define	ENG_COUNT(e)		E_OP(e, count)(E_PRV(e))
475 #define	ENG_QLEN(e)		E_OP(e, qlen)(E_PRV(e))
476 #define	ENG_CLOSE(e)		E_OP(e, close)(E_PRV(e))
477 #define	ENG_OPEN(e, s, nf, d) 	E_OP(e, open)(E_PRV(e), e->e_flags, s, nf, d)
478 #define	ENG_CHINFO(e, c, o, i)	E_OP(e, chinfo(E_PRV(e), c, o, i))
479 
480 /* audio_sun.c */
481 void auimpl_sun_init(void);
482 
483 /* audio_oss.c */
484 void auimpl_oss_init(void);
485 
486 #endif	/* _AUDIO_IMPL_H */
487