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.
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.
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]
22 * Copyright (C) 4Front Technologies 1996-2008.
24 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
30 #include <sys/types.h>
34 #include <sys/audio/audio_driver.h>
35 #include "audio_client.h"
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
44 #define AUDIO_INTRHZ 100
45 #define AUDIO_INTRHZ_MIN 50 /* 20 msec max */
46 #define AUDIO_INTRHZ_MAX 500
54 typedef int (*audio_cnv_func_t
)(audio_stream_t
*, int);
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 */
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.)
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
85 ddi_umem_cookie_t s_cookie
;
87 uint32_t s_hintsz
; /* latency hints */
94 uint64_t s_errors
; /* underrun or overrun count */
97 boolean_t s_paused
; /* stream paused */
98 boolean_t s_draining
; /* stream draining */
101 * Sample rate conversion (SRC) and format conversion details.
103 struct grc3state
*s_src_state
[AUDIO_MAX_CHANNELS
];
104 uint_t s_src_quality
;
106 audio_cnv_func_t s_converter
;
107 uint32_t *s_cnv_buf0
;
108 uint32_t *s_cnv_buf1
;
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
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
124 audio_parms_t
*s_user_parms
;
125 audio_parms_t
*s_phys_parms
;
130 uint8_t s_gain_master
;
132 uint16_t s_gain_scaled
;
139 uint64_t s_drain_idx
; /* engine index */
142 * Other per stream details, e.g. channel offset, etc.
146 list_node_t s_eng_linkage
; /* place on engine list */
147 audio_client_t
*s_client
;
148 audio_engine_t
*s_engine
;
154 uint_t s_engcap
; /* ENGINE_xxx_CAP */
158 * struct audio_client: This structure represents a logical port,
159 * associated with an open file, etc. These are the entities that are
162 struct audio_client
{
163 audio_stream_t c_istream
;
164 audio_stream_t c_ostream
;
168 * We can keep a linked list of clients to "notify" so that
169 * we can do this outside of locked context.
171 audio_client_t
*c_next_input
;
172 audio_client_t
*c_next_output
;
173 audio_client_t
*c_next_drain
;
185 * Linkage for per-device list of clients.
187 list_node_t c_global_linkage
;
188 list_node_t c_dev_linkage
;
190 boolean_t c_serialize
;
194 boolean_t c_is_active
;
197 * Client wide settings... e.g. ops vector, etc.
199 uint_t c_omode
; /* open mode */
200 pid_t c_pid
; /* opening process id */
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
218 struct pollhead c_pollhead
;
222 struct audio_infostr
{
224 list_node_t i_linkage
;
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
;
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
);
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
258 struct audio_engine
{
259 audio_engine_ops_t e_ops
;
264 * Mixing related fields.
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
;
274 * Underlying physical buffer shared with device driver.
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
290 int e_stream_overruns
;
291 int e_stream_underruns
;
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
302 struct audio_stats e_stats
;
310 ddi_periodic_t e_periodic
;
313 * Linkage for per-device list.
315 list_node_t e_dev_linkage
;
317 int e_num
; /* arbitrary engine number */
320 * List of of streams attached to this engine.
323 boolean_t e_suspended
;
326 boolean_t e_need_start
;
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 */
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 */
348 list_t d_hwinfo
; /* strings of hw info */
355 kmutex_t d_ctrl_lock
; /* leaf lock */
356 kcondvar_t d_ctrl_cv
;
357 krwlock_t d_clnt_lock
;
363 * Lists of virtual clients, controls and engines. Protected by
364 * the d_lock field above.
369 audio_ctrl_t
*d_pcmvol_ctrl
;
372 volatile uint_t d_serial
;
375 * Linkage onto global list of devices.
377 list_node_t d_by_index
;
378 list_node_t d_by_number
;
381 * Personality specific data.
383 void *d_minor_data
[1 << AUDIO_MN_TYPE_NBITS
];
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.
393 * If the driver does not support controls then it must insure
394 * that any hardware controls are initialized to a usable state.
396 * For the framework/user-apps to be able to change controls
397 * the driver must create, enable and configure controls with
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.
406 * Only controls created through control API's will be available,
407 * well-known or device-private.
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
;
420 uint64_t ctrl_saved
; /* the saved value */
421 boolean_t ctrl_saved_ok
;
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)
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
*);
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
*);
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 *);
472 int auimpl_save_controls(audio_dev_t
*);
473 int auimpl_restore_controls(audio_dev_t
*);
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);
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
*);
489 void auimpl_dev_walk_engines(audio_dev_t
*,
490 int (*)(audio_engine_t
*, void *), void *);
492 void auimpl_dev_vwarn(audio_dev_t
*, const char *, va_list);
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))
511 void auimpl_sun_init(void);
514 void auimpl_oss_init(void);
516 #endif /* _AUDIO_IMPL_H */