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