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