xref: /illumos-gate/usr/src/uts/common/io/audio/impl/audio_impl.h (revision fb2a9bae0030340ad72b9c26ba1ffee2ee3cafec)
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 2010 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 #define	AUDIO_INTRHZ		100
46 #define	AUDIO_INTRHZ_MIN	50	/* 20 msec max */
47 #define	AUDIO_INTRHZ_MAX	500
48 
49 struct audio_parms {
50 	int		p_format;
51 	int		p_rate;
52 	int		p_nchan;
53 };
54 
55 typedef int (*audio_cnv_func_t)(audio_stream_t *, int);
56 
57 struct audio_buffer {
58 	caddr_t			b_data;
59 	uint64_t		b_head;
60 	uint64_t		b_tail;
61 	uint_t			b_hidx;		/* head % nframes */
62 	uint_t			b_tidx;		/* tail % nframes */
63 	uint_t			b_nframes;	/* total frames */
64 	uint_t			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_framesz		s_buf.b_framesz
79 #define	s_nframes		s_buf.b_nframes
80 #define	s_tidx			s_buf.b_tidx
81 #define	s_hidx			s_buf.b_hidx
82 	uint_t			s_nfrags;
83 	uint_t			s_fragfr;
84 	uint_t			s_nbytes;
85 	uint_t			s_fragbytes;
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 	uint_t			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 	uint_t			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 	 * We can keep a linked list of clients to "notify" so that
170 	 * we can do this outside of locked context.
171 	 */
172 	audio_client_t		*c_next_input;
173 	audio_client_t		*c_next_output;
174 	audio_client_t		*c_next_drain;
175 
176 	/*
177 	 * DDI support.
178 	 */
179 	major_t			c_major;
180 	minor_t			c_minor;
181 	minor_t			c_origminor;
182 	queue_t			*c_rq;
183 	queue_t			*c_wq;
184 
185 	/*
186 	 * Linkage for per-device list of clients.
187 	 */
188 	list_node_t		c_global_linkage;
189 	list_node_t		c_dev_linkage;
190 	int			c_refcnt;
191 	boolean_t		c_serialize;
192 
193 	kmutex_t		c_lock;
194 	kcondvar_t		c_cv;
195 	boolean_t		c_is_active;
196 
197 	/*
198 	 * Client wide settings... e.g. ops vector, etc.
199 	 */
200 	uint_t			c_omode;	/* open mode */
201 	pid_t			c_pid;		/* opening process id */
202 	audio_dev_t		*c_dev;
203 	cred_t			*c_cred;
204 	audio_client_ops_t	c_ops;
205 #define	c_open			c_ops.aco_open
206 #define	c_close			c_ops.aco_close
207 #define	c_read			c_ops.aco_read
208 #define	c_write			c_ops.aco_write
209 #define	c_ioctl			c_ops.aco_ioctl
210 #define	c_chpoll		c_ops.aco_chpoll
211 #define	c_output		c_ops.aco_output
212 #define	c_input			c_ops.aco_input
213 #define	c_notify		c_ops.aco_notify
214 #define	c_drain			c_ops.aco_drain
215 #define	c_wput			c_ops.aco_wput
216 #define	c_wsrv			c_ops.aco_wsrv
217 #define	c_rsrv			c_ops.aco_rsrv
218 
219 	struct pollhead		c_pollhead;
220 
221 };
222 
223 struct audio_infostr {
224 	char			i_line[100];
225 	list_node_t		i_linkage;
226 };
227 
228 struct audio_stats {
229 	kstat_named_t		st_head;
230 	kstat_named_t		st_tail;
231 	kstat_named_t		st_flags;
232 	kstat_named_t		st_nfrags;
233 	kstat_named_t		st_framesz;
234 	kstat_named_t		st_nbytes;
235 	kstat_named_t		st_hidx;
236 	kstat_named_t		st_tidx;
237 	kstat_named_t		st_format;
238 	kstat_named_t		st_nchan;
239 	kstat_named_t		st_rate;
240 	kstat_named_t		st_intrs;
241 	kstat_named_t		st_errors;
242 	kstat_named_t		st_engine_underruns;
243 	kstat_named_t		st_engine_overruns;
244 	kstat_named_t		st_stream_underruns;
245 	kstat_named_t		st_stream_overruns;
246 	kstat_named_t		st_playahead;
247 	kstat_named_t		st_suspended;
248 	kstat_named_t		st_failed;
249 };
250 
251 typedef void (*audio_import_fn_t)(audio_engine_t *, uint_t, audio_stream_t *);
252 typedef void (*audio_export_fn_t)(audio_engine_t *, uint_t, uint_t);
253 
254 /*
255  * An audio engine corresponds to a single DMA transfer channel.  It can
256  * represent either record or playback, but not both at the same time.
257  * A device that supports simultaneous record and playback will register
258  * separate channels.
259  */
260 struct audio_engine {
261 	audio_engine_ops_t	e_ops;
262 	void			*e_private;
263 	uint_t			e_flags;
264 
265 	/*
266 	 * Mixing related fields.
267 	 */
268 	uint_t			e_limiter_state;
269 	int32_t			*e_chbufs[AUDIO_MAX_CHANNELS];
270 	uint_t			e_choffs[AUDIO_MAX_CHANNELS];
271 	uint_t			e_chincr[AUDIO_MAX_CHANNELS];
272 	audio_export_fn_t	e_export;
273 	audio_import_fn_t	e_import;
274 
275 	/*
276 	 * Underlying physical buffer shared with device driver.
277 	 */
278 	audio_buffer_t		e_buf;
279 #define	e_head			e_buf.b_head
280 #define	e_tail			e_buf.b_tail
281 #define	e_data			e_buf.b_data
282 #define	e_framesz		e_buf.b_framesz
283 #define	e_nframes		e_buf.b_nframes
284 #define	e_hidx			e_buf.b_hidx
285 #define	e_tidx			e_buf.b_tidx
286 	uint_t			e_fragfr;
287 	uint_t			e_playahead;
288 
289 	int			e_intrs;
290 	int			e_errors;
291 	int			e_overruns;
292 	int			e_underruns;
293 	int			e_stream_overruns;
294 	int			e_stream_underruns;
295 
296 	audio_parms_t		e_parms;
297 #define	e_format		e_parms.p_format
298 #define	e_nchan			e_parms.p_nchan
299 #define	e_rate			e_parms.p_rate
300 
301 	/*
302 	 * Statistics.
303 	 */
304 	kstat_t			*e_ksp;
305 	struct audio_stats	e_stats;
306 
307 
308 	/*
309 	 * Synchronization.
310 	 */
311 	kmutex_t		e_lock;
312 	kcondvar_t		e_cv;
313 	ddi_periodic_t		e_periodic;
314 
315 	/*
316 	 * Linkage for per-device list.
317 	 */
318 	list_node_t		e_dev_linkage;
319 	audio_dev_t		*e_dev;
320 	int			e_num;	/* arbitrary engine number */
321 
322 	/*
323 	 * List of of streams attached to this engine.
324 	 */
325 	list_t			e_streams;
326 	int			e_nrunning;
327 	int			e_suspended;
328 	boolean_t		e_failed;
329 
330 	boolean_t		e_need_start;
331 };
332 
333 struct audio_dev {
334 	dev_info_t		*d_dip;
335 	major_t			d_major;
336 	int			d_instance;
337 
338 	uint32_t		d_flags;
339 #define	DEV_OUTPUT_CAP		(1U << 0)
340 #define	DEV_INPUT_CAP		(1U << 1)
341 #define	DEV_DUPLEX_CAP		(1U << 2)
342 #define	DEV_SNDSTAT_CAP		(1U << 3)
343 #define	DEV_OPAQUE_CAP		(1U << 4)	/* AC3 are not mixable */
344 
345 	char			d_name[128];	/* generic description */
346 	char			d_desc[128];	/* detailed config descr */
347 	char			d_vers[128];	/* detailed version descr */
348 	int			d_number;	/* global /dev/audioXX # */
349 	int			d_index;	/* master device index */
350 	int			d_engno;	/* engine counter */
351 
352 	list_t			d_hwinfo;	/* strings of hw info */
353 
354 	/*
355 	 * Synchronization.
356 	 */
357 	kmutex_t		d_lock;
358 	kcondvar_t		d_cv;
359 	kmutex_t		d_ctrl_lock;	/* leaf lock */
360 	kcondvar_t		d_ctrl_cv;
361 	krwlock_t		d_clnt_lock;
362 	uint_t			d_refcnt;
363 	int			d_suspended;
364 	boolean_t		d_failed;
365 
366 	/*
367 	 * Lists of virtual clients, controls and engines.  Protected by
368 	 * the d_lock field above.
369 	 */
370 	list_t			d_clients;
371 	list_t			d_engines;
372 	list_t			d_controls;
373 	audio_ctrl_t		*d_pcmvol_ctrl;
374 	uint64_t		d_pcmvol;
375 
376 	volatile uint_t		d_serial;
377 
378 	/*
379 	 * Linkage onto global list of devices.
380 	 */
381 	list_node_t		d_by_index;
382 	list_node_t		d_by_number;
383 
384 	/*
385 	 * Personality specific data.
386 	 */
387 	void			*d_minor_data[1 << AUDIO_MN_TYPE_NBITS];
388 };
389 
390 /*
391  * Each audio_dev optionally can have controls attached to it.
392  * Controls are separate from audio engines. They are methods of
393  * adjusting pharameters or reading metrics that usually relate to
394  * hardware on devices engine by the driver. They can be things like
395  * master volume for example.
396  *
397  * If the driver does not support controls then it must insure
398  * that any hardware controls are initialized to a usable state.
399  *
400  * For the framework/user-apps to be able to change controls
401  * the driver must create, enable and configure controls with
402  * control API's.
403  *
404  * There are a number of common controls (well-known) that most
405  * hardware supports. These have known names and known ctrl numbers.
406  * In addition a driver can have any number of extention
407  * controls (device-private). These can have any name and any ctrl
408  * number other then the ones, defined as well-knonw ones.
409  *
410  * Only controls created through control API's will be available,
411  * well-known or device-private.
412  */
413 struct	audio_ctrl {
414 	audio_ctrl_desc_t	ctrl_des;
415 #define	ctrl_name		ctrl_des.acd_name
416 #define	ctrl_type		ctrl_des.acd_type
417 #define	ctrl_enum		ctrl_des.acd_enum
418 #define	ctrl_flags		ctrl_des.acd_flags
419 	audio_dev_t		*ctrl_dev;
420 	audio_ctrl_rd_t		ctrl_read_fn;
421 	audio_ctrl_wr_t		ctrl_write_fn;
422 	list_node_t		ctrl_linkage;
423 	void			*ctrl_arg;
424 	uint64_t		ctrl_saved;	/* the saved value */
425 	boolean_t		ctrl_saved_ok;
426 };
427 
428 
429 /*
430  * Prototypes.
431  */
432 
433 /* audio_format.c */
434 int auimpl_format_alloc(audio_stream_t *);
435 void auimpl_format_free(audio_stream_t *);
436 int auimpl_format_setup(audio_stream_t *, audio_parms_t *);
437 
438 /* audio_output.c */
439 void auimpl_export_16ne(audio_engine_t *, uint_t, uint_t);
440 void auimpl_export_16oe(audio_engine_t *, uint_t, uint_t);
441 void auimpl_export_24ne(audio_engine_t *, uint_t, uint_t);
442 void auimpl_export_24oe(audio_engine_t *, uint_t, uint_t);
443 void auimpl_export_32ne(audio_engine_t *, uint_t, uint_t);
444 void auimpl_export_32oe(audio_engine_t *, uint_t, uint_t);
445 void auimpl_output_callback(void *);
446 void auimpl_output_preload(audio_engine_t *);
447 
448 /* audio_input.c */
449 void auimpl_import_16ne(audio_engine_t *, uint_t, audio_stream_t *);
450 void auimpl_import_16oe(audio_engine_t *, uint_t, audio_stream_t *);
451 void auimpl_import_24ne(audio_engine_t *, uint_t, audio_stream_t *);
452 void auimpl_import_24oe(audio_engine_t *, uint_t, audio_stream_t *);
453 void auimpl_import_32ne(audio_engine_t *, uint_t, audio_stream_t *);
454 void auimpl_import_32oe(audio_engine_t *, uint_t, audio_stream_t *);
455 void auimpl_input_callback(void *);
456 int auimpl_input_drain(audio_stream_t *);
457 
458 /* audio_client.c */
459 void auimpl_client_init(void);
460 void auimpl_client_fini(void);
461 audio_client_t *auimpl_client_create(dev_t);
462 void auimpl_client_destroy(audio_client_t *);
463 void auimpl_client_activate(audio_client_t *);
464 void auimpl_client_deactivate(audio_client_t *);
465 int auimpl_create_minors(audio_dev_t *);
466 void auimpl_remove_minors(audio_dev_t *);
467 int auimpl_set_pcmvol(void *, uint64_t);
468 int auimpl_get_pcmvol(void *, uint64_t *);
469 
470 /* audio_ctrl.c */
471 int auimpl_save_controls(audio_dev_t *);
472 int auimpl_restore_controls(audio_dev_t *);
473 
474 /* audio_engine.c */
475 extern int audio_priority;
476 void auimpl_dev_init(void);
477 void auimpl_dev_fini(void);
478 void auimpl_dev_hold(audio_dev_t *);
479 audio_dev_t *auimpl_dev_hold_by_devt(dev_t);
480 audio_dev_t *auimpl_dev_hold_by_index(int);
481 void auimpl_dev_release(audio_dev_t *);
482 int auimpl_choose_format(int);
483 
484 int auimpl_engine_open(audio_dev_t *, int, int, audio_stream_t *);
485 void auimpl_engine_close(audio_stream_t *);
486 
487 void auimpl_dev_walk_engines(audio_dev_t *,
488     int (*)(audio_engine_t *, void *), void *);
489 
490 void auimpl_dev_vwarn(audio_dev_t *, const char *, va_list);
491 
492 /* engine operations */
493 #define	E_OP(e, entry)		((e)->e_ops.audio_engine_##entry)
494 #define	E_PRV(e)		((e)->e_private)
495 #define	ENG_FORMAT(e)		E_OP(e, format)(E_PRV(e))
496 #define	ENG_RATE(e)		E_OP(e, rate)(E_PRV(e))
497 #define	ENG_CHANNELS(e)		E_OP(e, channels)(E_PRV(e))
498 #define	ENG_SYNC(e, num)	E_OP(e, sync)(E_PRV(e), num)
499 #define	ENG_START(e)		E_OP(e, start)(E_PRV(e))
500 #define	ENG_STOP(e)		E_OP(e, stop)(E_PRV(e))
501 #define	ENG_COUNT(e)		E_OP(e, count)(E_PRV(e))
502 #define	ENG_QLEN(e)		E_OP(e, qlen)(E_PRV(e))
503 #define	ENG_PLAYAHEAD(e)	E_OP(e, playahead)(E_PRV(e))
504 #define	ENG_CLOSE(e)		E_OP(e, close)(E_PRV(e))
505 #define	ENG_OPEN(e, nf, d) 	E_OP(e, open)(E_PRV(e), e->e_flags, nf, d)
506 #define	ENG_CHINFO(e, c, o, i)	E_OP(e, chinfo(E_PRV(e), c, o, i))
507 
508 /* audio_sun.c */
509 void auimpl_sun_init(void);
510 
511 /* audio_oss.c */
512 void auimpl_oss_init(void);
513 
514 #endif	/* _AUDIO_IMPL_H */
515