summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/audio/impl/audio_ctrl.c
blob: f5fd2806397e5d110f8cf09f26315b52663494b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/types.h>
#include <sys/list.h>
#include <sys/sysmacros.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/atomic.h>

#include "audio_impl.h"

/*
 * Audio Control functions.
 */

/*
 * Given a control structure - free all names
 * strings allocated to it.
 *
 * ctrl             - The control who's names that will be free'd.
 */
static void
audio_control_freenames(audio_ctrl_t *ctrl)
{
	int	indx;

	if (ctrl->ctrl_name != NULL)
		strfree((char *)ctrl->ctrl_name);
	ctrl->ctrl_name = NULL;

	for (indx = 0; indx < 64; indx++) {
		if (ctrl->ctrl_enum[indx] != NULL) {
			strfree((char *)ctrl->ctrl_enum[indx]);
			ctrl->ctrl_enum[indx] = NULL;
		}
	}
}

/*
 * This will allocate and register a control for my audio device.
 *
 * d                - The audio device the control will be attached to.
 * desc             - Attributes about this new control
 * read_fn          - Callback function in driver to read control
 * write_fn         - Callback function in driver to write control.
 * arg              - driver private context passed to read_fn/write_fn
 *
 * On success this will return a control structure else NULL.
 *
 * The value passed in for a control number in the audio_ctrl_desc_t
 * has some special meaning. If it is less then AUDIO_CONTROL_EXTBASE
 * then the control is assumed to be a known control. If it is
 * AUDIO_CONTROL_EXTBASE then the framework will allocate a unique
 * control number and replace it in the audio_ctrl_desc_t structure
 * and this control is considered an extended driver private control.
 * The number that is replaced in audio_ctrl_desc_t will be greater
 * then AUDIO_CONTROL_EXTBASE.
 *
 */
audio_ctrl_t *
audio_dev_add_control(audio_dev_t *d, audio_ctrl_desc_t *desc,
    audio_ctrl_rd_t read_fn, audio_ctrl_wr_t write_fn, void *arg)
{
	audio_ctrl_t *ctrl;
	audio_ctrl_desc_t *new_desc;
	char	scratch[16];
	const char	*name;

	/* Verify arguments */
	ASSERT(d);
	ASSERT(desc);

	/* We cannot deal with unnamed controls */
	if ((name = desc->acd_name) == NULL) {
		return (NULL);
	}

	/*
	 * If this was called with a control name that was already
	 * added, then we do some special things. First we reuse the
	 * control audio_ctrl_t and as far as outside users are
	 * concerned the handle is reused. To users this looks like we
	 * are changing the controls attributes. But what we really do
	 * is free every thing allocated to the control and then
	 * reinit everything.  That way the same code can get used for
	 * both.
	 *
	 * We verify anything that could fail before we change the
	 * control or commit to any changes. If there is something bad
	 * return null to indicate an error but the original control
	 * is still usable and untouched.
	 */
	ctrl = auclnt_find_control(d, name);

	if (ctrl == NULL) {
		/* Allocate a new control */
		ctrl = kmem_zalloc(sizeof (*ctrl), KM_SLEEP);
	} else {
		/* Re-configure an existing control */
		switch (desc->acd_type) {
		case AUDIO_CTRL_TYPE_BOOLEAN:
		case AUDIO_CTRL_TYPE_STEREO:
		case AUDIO_CTRL_TYPE_MONO:
		case AUDIO_CTRL_TYPE_METER:
		case AUDIO_CTRL_TYPE_ENUM:
			break;
		default:
			audio_dev_warn(d, "bad control type %d for %s "
			    "not replaced", desc->acd_type, desc->acd_name);
			return (NULL);
		}

		/*
		 * By removing it from the list we prevent the need to lock
		 * and check for locks on the control itself.
		 * Also by doing this we can use the normal add code to do
		 * what it normally does below.
		 */
		mutex_enter(&d->d_ctrl_lock);
		list_remove(&d->d_controls, ctrl);
		mutex_exit(&d->d_ctrl_lock);

		audio_control_freenames(ctrl);
		ctrl->ctrl_read_fn = NULL;
		ctrl->ctrl_write_fn = NULL;
		ctrl->ctrl_arg = NULL;
		ctrl->ctrl_dev = NULL;
	}
	new_desc = &ctrl->ctrl_des;

	/* Fill in new control description */
	new_desc->acd_type = desc->acd_type;
	new_desc->acd_flags = desc->acd_flags;
	new_desc->acd_maxvalue = desc->acd_maxvalue;
	new_desc->acd_minvalue = desc->acd_minvalue;
	new_desc->acd_name = strdup(name);

	/* Process type of control special actions, if any */
	switch (desc->acd_type) {
	case AUDIO_CTRL_TYPE_BOOLEAN:
	case AUDIO_CTRL_TYPE_STEREO:
	case AUDIO_CTRL_TYPE_MONO:
	case AUDIO_CTRL_TYPE_METER:
		break;

	case AUDIO_CTRL_TYPE_ENUM:
		for (int bit = 0; bit < 64; bit++) {
			if (((1U << bit) & desc->acd_maxvalue) == 0)
				continue;
			name = desc->acd_enum[bit];
			if (name == NULL) {
				(void) snprintf(scratch, sizeof (scratch),
				    "bit%d", bit);
				name = scratch;
			}
			new_desc->acd_enum[bit] = strdup(name);
		}
		break;
	default:
		audio_dev_warn(d, "bad control type %d for %s",
		    desc->acd_type, desc->acd_name);
		goto ctrl_fail;
	}

	ctrl->ctrl_dev = d;
	if (new_desc->acd_flags & AUDIO_CTRL_FLAG_READABLE) {
		ASSERT(read_fn);
		ctrl->ctrl_read_fn = read_fn;
		ctrl->ctrl_arg = arg;
	}
	if (new_desc->acd_flags & AUDIO_CTRL_FLAG_WRITEABLE) {
		ASSERT(write_fn);
		ctrl->ctrl_write_fn = write_fn;
		ctrl->ctrl_arg = arg;
	}

	mutex_enter(&d->d_ctrl_lock);
	list_insert_tail(&d->d_controls, ctrl);
	mutex_exit(&d->d_ctrl_lock);

	return (ctrl);


ctrl_fail:
	if (ctrl) {
		audio_control_freenames(ctrl);
		kmem_free(ctrl, sizeof (*ctrl));
	}
	return (NULL);
}

/*
 * This will remove a control from my audio device.
 *
 * ctrl             - The control will be removed.
 */
void
audio_dev_del_control(audio_ctrl_t *ctrl)
{
	audio_dev_t *d;

	/* Verify argument */
	ASSERT(ctrl);
	d = ctrl->ctrl_dev;
	ASSERT(d);

	mutex_enter(&d->d_ctrl_lock);
	list_remove(&d->d_controls, ctrl);
	mutex_exit(&d->d_ctrl_lock);

	audio_control_freenames(ctrl);
	kmem_free(ctrl, sizeof (*ctrl));
}

void
audio_dev_add_soft_volume(audio_dev_t *d)
{
	audio_ctrl_desc_t	desc;

	bzero(&desc, sizeof (desc));
	if (d->d_pcmvol_ctrl == NULL) {
		desc.acd_name = AUDIO_CTRL_ID_VOLUME;
		desc.acd_type = AUDIO_CTRL_TYPE_MONO;
		desc.acd_minvalue = 0;
		desc.acd_maxvalue = 100;
		desc.acd_flags = AUDIO_CTRL_FLAG_RW | AUDIO_CTRL_FLAG_PLAY |
		    AUDIO_CTRL_FLAG_PCMVOL;
		d->d_pcmvol_ctrl = audio_dev_add_control(d, &desc,
		    auimpl_get_pcmvol, auimpl_set_pcmvol, d);
		d->d_pcmvol = 75;
	}
}

/*
 * This will notify clients of need to reread control
 * values since they have changed.
 *
 * There will be a routine that allows a client to register
 * a callback.   For now we just update the serial number.
 *
 * d                - The device that needs updates.
 */
void
audio_dev_update_controls(audio_dev_t *d)
{
	atomic_inc_uint(&d->d_serial);
}


/*
 * This is used to read the current value of a control.
 * Note, this will cause a callback into the driver to get the value.
 *
 * ctrl        - should be the valid control being read.
 * value       - is a pointer to the place that will contain the value read.
 *
 * On return zero is returned on success else errno is returned.
 *
 */
int
audio_control_read(audio_ctrl_t *ctrl, uint64_t *value)
{
	audio_dev_t	*d = ctrl->ctrl_dev;
	uint64_t	my_value;
	int		ret;

	ASSERT(value);

	mutex_enter(&d->d_ctrl_lock);
	while (d->d_suspended) {
		cv_wait(&d->d_ctrl_cv, &d->d_ctrl_lock);
	}

	if (!(ctrl->ctrl_flags & AUDIO_CTRL_FLAG_READABLE)) {
		mutex_exit(&d->d_ctrl_lock);
		return (ENXIO);
	}

	ASSERT(ctrl->ctrl_read_fn);

	ret = ctrl->ctrl_read_fn(ctrl->ctrl_arg, &my_value);
	mutex_exit(&d->d_ctrl_lock);

	if (ret == 0) {
		*value = my_value;
	}

	return (ret);
}

/*
 * This is used to write a value to a control.
 * Note, this will cause a callback into the driver to write the value.
 *
 * ctrl        - should be the valid control being written.
 * value       - is value to set the control to.
 *
 * On return zero is returned on success else errno is returned.
 *
 */
int
audio_control_write(audio_ctrl_t *ctrl, uint64_t value)
{
	int		ret;
	audio_dev_t	*d = ctrl->ctrl_dev;

	mutex_enter(&d->d_ctrl_lock);
	while (d->d_suspended) {
		cv_wait(&d->d_ctrl_cv, &d->d_ctrl_lock);
	}

	if (!(ctrl->ctrl_flags & AUDIO_CTRL_FLAG_WRITEABLE)) {
		mutex_exit(&d->d_ctrl_lock);
		return (ENXIO);
	}

	ASSERT(ctrl->ctrl_write_fn);

	ret = ctrl->ctrl_write_fn(ctrl->ctrl_arg, value);
	if (ret == 0) {
		ctrl->ctrl_saved = value;
		ctrl->ctrl_saved_ok = B_TRUE;
	}
	mutex_exit(&d->d_ctrl_lock);

	if (ret == 0) {
		audio_dev_update_controls(d);
	}

	return (ret);
}

/*
 * This is used to save control values.
 */
int
auimpl_save_controls(audio_dev_t *d)
{
	audio_ctrl_t	*ctrl;
	list_t		*l;
	int		ret;

	ASSERT(mutex_owned(&d->d_ctrl_lock));
	l = &d->d_controls;

	for (ctrl = list_head(l); ctrl; ctrl = list_next(l, ctrl)) {
		if ((!(ctrl->ctrl_flags & AUDIO_CTRL_FLAG_WRITEABLE)) ||
		    (!(ctrl->ctrl_flags & AUDIO_CTRL_FLAG_READABLE))) {
			continue;
		}
		ret = ctrl->ctrl_read_fn(ctrl->ctrl_arg, &ctrl->ctrl_saved);
		if (ret != 0) {
			audio_dev_warn(d,
			    "Unable to save value of control %s",
			    ctrl->ctrl_name);
			return (ret);
		} else {
			ctrl->ctrl_saved_ok = B_TRUE;
		}
	}
	return (0);
}

int
auimpl_restore_controls(audio_dev_t *d)
{
	audio_ctrl_t	*ctrl;
	list_t		*l;
	int		ret;
	int		rv = 0;

	ASSERT(mutex_owned(&d->d_ctrl_lock));
	l = &d->d_controls;

	for (ctrl = list_head(l); ctrl; ctrl = list_next(l, ctrl)) {
		if (!ctrl->ctrl_saved_ok) {
			continue;
		}
		ret = ctrl->ctrl_write_fn(ctrl->ctrl_arg, ctrl->ctrl_saved);
		if (ret != 0) {
			audio_dev_warn(d,
			    "Unable to restore value of control %s",
			    ctrl->ctrl_name);
			rv = ret;
		}
	}
	return (rv);
}