source: mainline/uspace/lib/drv/generic/remote_audio_mixer.c@ 1912b45

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1912b45 was 1912b45, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

Make pcm control iface generic

  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libdrv
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <errno.h>
37#include <assert.h>
38#include <str.h>
39
40#include "audio_mixer_iface.h"
41#include "ddf/driver.h"
42
43typedef enum {
44 /** Asks for basic mixer info: Mixer name and number of controllable
45 * items.
46 * Answer:
47 * - ENOTSUP - call not supported
48 * - EOK - call successful, info is valid
49 * Answer arguments:
50 * - Mixer name
51 * - Number of controllable items
52 */
53 IPC_M_AUDIO_MIXER_GET_INFO,
54
55 /** Asks for item mixer info: Item name and number of controllable
56 * channels.
57 * Answer:
58 * - ENOTSUP - call not supported
59 * - ENOENT - no such item
60 * - EOK - call successful, info is valid
61 * Answer arguments:
62 * - Item name
63 * - Number of controllable channels
64 */
65 IPC_M_AUDIO_MIXER_GET_ITEM_INFO,
66
67 /** Set new control item setting
68 * Answer:
69 * - ENOTSUP - call not supported
70 * - ENOENT - no such control item
71 * - EOK - call successful, info is valid
72 */
73 IPC_M_AUDIO_MIXER_SET_ITEM_LEVEL,
74
75 /** Get control item setting
76 * Answer:
77 * - ENOTSUP - call not supported
78 * - ENOENT - no such control item
79 * - EOK - call successful, info is valid
80 */
81 IPC_M_AUDIO_MIXER_GET_ITEM_LEVEL,
82} audio_mixer_iface_funcs_t;
83
84/*
85 * CLIENT SIDE
86 */
87
88/**
89 * Query audio mixer for basic info (name and items count).
90 * @param[in] exch IPC exchange connected to the device
91 * @param[out] name Audio mixer string identifier
92 * @param[out] items Number of items controlled by the mixer.
93 * @return Error code.
94 */
95int audio_mixer_get_info(async_exch_t *exch, const char **name, unsigned *items)
96{
97 if (!exch)
98 return EINVAL;
99 sysarg_t name_size, itemc;
100 const int ret = async_req_1_2(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
101 IPC_M_AUDIO_MIXER_GET_INFO, &name_size, &itemc);
102 if (ret == EOK && name) {
103 char *name_place = calloc(1, name_size);
104 if (!name_place) {
105 /* Make the other side fail
106 * as it waits for read request */
107 async_data_read_start(exch, (void*)-1, 0);
108 return ENOMEM;
109 }
110 const int ret =
111 async_data_read_start(exch, name_place, name_size);
112 if (ret != EOK) {
113 free(name_place);
114 return ret;
115 }
116 *name = name_place;
117 }
118 if (ret == EOK && items)
119 *items = itemc;
120 return ret;
121}
122
123/**
124 * Query audio mixer for item specific info (name and channels count).
125 * @param[in] exch IPC exchange connected to the device
126 * @param[in] item The control item
127 * @param[out] name Control item string identifier.
128 * @param[out] channles Number of channels associated with this control item.
129 * @return Error code.
130 */
131int audio_mixer_get_item_info(async_exch_t *exch, unsigned item,
132 const char **name, unsigned *levels)
133{
134 if (!exch)
135 return EINVAL;
136 sysarg_t name_size, lvls;
137 const int ret = async_req_2_2(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
138 IPC_M_AUDIO_MIXER_GET_ITEM_INFO, item, &name_size, &lvls);
139 if (ret == EOK && name) {
140 char *name_place = calloc(1, name_size);
141 if (!name_place) {
142 /* Make the other side fail
143 * as it waits for read request */
144 async_data_read_start(exch, (void*)-1, 0);
145 return ENOMEM;
146 }
147 const int ret =
148 async_data_read_start(exch, name_place, name_size);
149 if (ret != EOK) {
150 free(name_place);
151 return ret;
152 }
153 *name = name_place;
154 }
155 if (ret == EOK && levels)
156 *levels = lvls;
157 return ret;
158}
159
160/**
161 * Set control item to a new level.
162 * @param[in] exch IPC exchange connected to the device.
163 * @param[in] item The control item controlling the channel.
164 * @param[in] level The new value.
165 * @return Error code.
166 */
167int audio_mixer_set_item_level(async_exch_t *exch, unsigned item,
168 unsigned level)
169{
170 if (!exch)
171 return EINVAL;
172 return async_req_3_0(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
173 IPC_M_AUDIO_MIXER_SET_ITEM_LEVEL, item, level);
174}
175
176/**
177 * Get current level of a control item.
178 * @param[in] exch IPC exchange connected to the device.
179 * @param[in] item The control item controlling the channel.
180 * @param[in] channel The channel index.
181 * @param[out] level Currently set value.
182 * @return Error code.
183 */
184int audio_mixer_get_item_level(async_exch_t *exch, unsigned item,
185 unsigned *level)
186{
187 if (!exch)
188 return EINVAL;
189 sysarg_t current;
190 const int ret = async_req_2_1(exch, DEV_IFACE_ID(AUDIO_MIXER_IFACE),
191 IPC_M_AUDIO_MIXER_GET_ITEM_LEVEL, item, &current);
192 if (ret == EOK && level)
193 *level = current;
194 return ret;
195}
196
197/*
198 * SERVER SIDE
199 */
200static void remote_audio_mixer_get_info(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
201static void remote_audio_mixer_get_item_info(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
202static void remote_audio_mixer_get_item_level(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
203static void remote_audio_mixer_set_item_level(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
204
205/** Remote audio mixer interface operations. */
206static remote_iface_func_ptr_t remote_audio_mixer_iface_ops[] = {
207 [IPC_M_AUDIO_MIXER_GET_INFO] = remote_audio_mixer_get_info,
208 [IPC_M_AUDIO_MIXER_GET_ITEM_INFO] = remote_audio_mixer_get_item_info,
209 [IPC_M_AUDIO_MIXER_GET_ITEM_LEVEL] = remote_audio_mixer_get_item_level,
210 [IPC_M_AUDIO_MIXER_SET_ITEM_LEVEL] = remote_audio_mixer_set_item_level,
211};
212
213/** Remote audio mixer interface structure. */
214remote_iface_t remote_audio_mixer_iface = {
215 .method_count = sizeof(remote_audio_mixer_iface_ops) /
216 sizeof(remote_audio_mixer_iface_ops[0]),
217 .methods = remote_audio_mixer_iface_ops
218};
219
220void remote_audio_mixer_get_info(
221 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
222{
223 audio_mixer_iface_t *mixer_iface = iface;
224
225 if (!mixer_iface->get_info) {
226 async_answer_0(callid, ENOTSUP);
227 return;
228 }
229 const char *name = NULL;
230 unsigned items = 0;
231 const int ret = mixer_iface->get_info(fun, &name, &items);
232 const size_t name_size = name ? str_size(name) + 1 : 0;
233 async_answer_2(callid, ret, name_size, items);
234 /* Send the name. */
235 if (ret == EOK && name_size > 0) {
236 size_t size;
237 ipc_callid_t name_id;
238 if (!async_data_read_receive(&name_id, &size)) {
239 async_answer_0(name_id, EPARTY);
240 return;
241 }
242 if (size != name_size) {
243 async_answer_0(name_id, ELIMIT);
244 return;
245 }
246 async_data_read_finalize(name_id, name, name_size);
247 }
248}
249
250void remote_audio_mixer_get_item_info(
251 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
252{
253 audio_mixer_iface_t *mixer_iface = iface;
254
255 if (!mixer_iface->get_item_info) {
256 async_answer_0(callid, ENOTSUP);
257 return;
258 }
259
260 const unsigned item = DEV_IPC_GET_ARG1(*call);
261 const char *name = NULL;
262 unsigned values = 0;
263 const int ret = mixer_iface->get_item_info(fun, item, &name, &values);
264 const size_t name_size = name ? str_size(name) + 1 : 0;
265 async_answer_2(callid, ret, name_size, values);
266 /* Send the name. */
267 if (ret == EOK && name_size > 0) {
268 size_t size;
269 ipc_callid_t name_id;
270 if (!async_data_read_receive(&name_id, &size)) {
271 async_answer_0(name_id, EPARTY);
272 return;
273 }
274 if (size != name_size) {
275 async_answer_0(name_id, ELIMIT);
276 return;
277 }
278 async_data_read_finalize(name_id, name, name_size);
279 }
280}
281
282void remote_audio_mixer_set_item_level(
283 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
284{
285 audio_mixer_iface_t *mixer_iface = iface;
286
287 if (!mixer_iface->set_item_level) {
288 async_answer_0(callid, ENOTSUP);
289 return;
290 }
291 const unsigned item = DEV_IPC_GET_ARG1(*call);
292 const unsigned value = DEV_IPC_GET_ARG2(*call);
293 const int ret = mixer_iface->set_item_level(fun, item, value);
294 async_answer_0(callid, ret);
295}
296
297void remote_audio_mixer_get_item_level(
298 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
299{
300 audio_mixer_iface_t *mixer_iface = iface;
301
302 if (!mixer_iface->get_item_level) {
303 async_answer_0(callid, ENOTSUP);
304 return;
305 }
306 const unsigned item = DEV_IPC_GET_ARG1(*call);
307 unsigned current = 0;
308 const int ret =
309 mixer_iface->get_item_level(fun, item, &current);
310 async_answer_1(callid, ret, current);
311}
312
313/**
314 * @}
315 */
Note: See TracBrowser for help on using the repository browser.