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