source: mainline/uspace/lib/drv/generic/remote_audio_pcm.c@ 346643c

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

audio: Use enum for sample format.

  • Property mode set to 100644
File size: 15.7 KB
Line 
1/*
2 * Copyright (c) 2012 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/** @addtogroup libdrv
29 * @{
30 */
31/** @file
32 */
33
34#include <async.h>
35#include <ddf/log.h>
36#include <errno.h>
37#include <str.h>
38#include <as.h>
39#include <sys/mman.h>
40
41#include "audio_pcm_iface.h"
42#include "ddf/driver.h"
43
44typedef enum {
45 IPC_M_AUDIO_PCM_GET_INFO_STR,
46 IPC_M_AUDIO_PCM_GET_BUFFER,
47 IPC_M_AUDIO_PCM_RELEASE_BUFFER,
48 IPC_M_AUDIO_PCM_START_PLAYBACK,
49 IPC_M_AUDIO_PCM_STOP_PLAYBACK,
50 IPC_M_AUDIO_PCM_START_RECORD,
51 IPC_M_AUDIO_PCM_STOP_RECORD,
52} audio_pcm_iface_funcs_t;
53
54/*
55 * CLIENT SIDE
56 */
57int audio_pcm_get_info_str(async_exch_t *exch, const char **name)
58{
59 if (!exch)
60 return EINVAL;
61 sysarg_t name_size;
62 const int ret = async_req_1_1(exch,
63 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
64 IPC_M_AUDIO_PCM_GET_INFO_STR, &name_size);
65 if (ret == EOK && name) {
66 char *name_place = calloc(1, name_size);
67 if (!name_place) {
68 /* Make the other side fail
69 * as it waits for read request */
70 async_data_read_start(exch, (void*)-1, 0);
71 return ENOMEM;
72 }
73 const int ret =
74 async_data_read_start(exch, name_place, name_size);
75 if (ret != EOK) {
76 free(name_place);
77 return ret;
78 }
79 *name = name_place;
80 }
81 return ret;
82}
83
84int audio_pcm_get_buffer(async_exch_t *exch, void **buffer, size_t *size,
85 unsigned *id, async_client_conn_t event_rec, void* arg)
86{
87 if (!exch || !buffer || !size || !id)
88 return EINVAL;
89
90 sysarg_t buffer_size = *size, buffer_id = 0;
91 const int ret = async_req_2_2(exch,
92 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_GET_BUFFER,
93 (sysarg_t)buffer_size, &buffer_size, &buffer_id);
94 if (ret == EOK) {
95 void *dst = NULL;
96 int ret = async_share_in_start_0_0(exch, buffer_size, &dst);
97 if (ret != EOK) {
98 return ret;
99 }
100 ret = async_connect_to_me(exch, 0, 0, 0, event_rec, arg);
101 if (ret != EOK) {
102 return ret;
103 }
104
105 *buffer = dst;
106 *size = buffer_size;
107 *id = buffer_id;
108 }
109 return ret;
110}
111
112int audio_pcm_release_buffer(async_exch_t *exch, unsigned id)
113{
114 if (!exch)
115 return EINVAL;
116 return async_req_2_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
117 IPC_M_AUDIO_PCM_RELEASE_BUFFER, id);
118}
119
120int audio_pcm_start_playback(async_exch_t *exch, unsigned id, unsigned parts,
121 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
122{
123 if (!exch)
124 return EINVAL;
125 if (parts > UINT8_MAX || channels > UINT8_MAX)
126 return EINVAL;
127 assert((format & UINT16_MAX) == format);
128 const sysarg_t packed =
129 (parts << 24) | (channels << 16) | (format & UINT16_MAX);
130 return async_req_4_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
131 IPC_M_AUDIO_PCM_START_PLAYBACK, id, sample_rate, packed);
132}
133
134int audio_pcm_stop_playback(async_exch_t *exch, unsigned id)
135{
136 if (!exch)
137 return EINVAL;
138 return async_req_2_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
139 IPC_M_AUDIO_PCM_STOP_PLAYBACK, id);
140}
141
142int audio_pcm_start_record(async_exch_t *exch, unsigned id, unsigned parts,
143 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
144{
145 if (!exch)
146 return EINVAL;
147 if (parts > UINT8_MAX || channels > UINT8_MAX)
148 return EINVAL;
149 assert((format & UINT16_MAX) == format);
150 const sysarg_t packed =
151 (parts << 24) | (channels << 16) | (format & UINT16_MAX);
152 return async_req_4_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
153 IPC_M_AUDIO_PCM_START_RECORD, id, sample_rate, packed);
154}
155
156int audio_pcm_stop_record(async_exch_t *exch, unsigned id)
157{
158 if (!exch)
159 return EINVAL;
160 return async_req_2_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
161 IPC_M_AUDIO_PCM_STOP_RECORD, id);
162}
163
164/*
165 * SERVER SIDE
166 */
167static void remote_audio_pcm_get_info_str(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
168static void remote_audio_pcm_get_buffer(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
169static void remote_audio_pcm_release_buffer(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
170static void remote_audio_pcm_start_playback(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
171static void remote_audio_pcm_stop_playback(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
172static void remote_audio_pcm_start_record(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
173static void remote_audio_pcm_stop_record(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
174
175/** Remote audio pcm buffer interface operations. */
176static remote_iface_func_ptr_t remote_audio_pcm_iface_ops[] = {
177 [IPC_M_AUDIO_PCM_GET_INFO_STR] = remote_audio_pcm_get_info_str,
178 [IPC_M_AUDIO_PCM_GET_BUFFER] = remote_audio_pcm_get_buffer,
179 [IPC_M_AUDIO_PCM_RELEASE_BUFFER] = remote_audio_pcm_release_buffer,
180 [IPC_M_AUDIO_PCM_START_PLAYBACK] = remote_audio_pcm_start_playback,
181 [IPC_M_AUDIO_PCM_STOP_PLAYBACK] = remote_audio_pcm_stop_playback,
182 [IPC_M_AUDIO_PCM_START_RECORD] = remote_audio_pcm_start_record,
183 [IPC_M_AUDIO_PCM_STOP_RECORD] = remote_audio_pcm_stop_record,
184};
185
186/** Remote audio mixer interface structure. */
187remote_iface_t remote_audio_pcm_iface = {
188 .method_count = sizeof(remote_audio_pcm_iface_ops) /
189 sizeof(remote_audio_pcm_iface_ops[0]),
190 .methods = remote_audio_pcm_iface_ops
191};
192
193void remote_audio_pcm_get_info_str(ddf_fun_t *fun, void *iface,
194 ipc_callid_t callid, ipc_call_t *call)
195{
196 const audio_pcm_iface_t *pcm_iface = iface;
197
198 if (!pcm_iface->get_info_str) {
199 async_answer_0(callid, ENOTSUP);
200 return;
201 }
202 const char *name = NULL;
203 const int ret = pcm_iface->get_info_str(fun, &name);
204 const size_t name_size = name ? str_size(name) + 1 : 0;
205 async_answer_1(callid, ret, name_size);
206 /* Send the string. */
207 if (ret == EOK && name_size > 0) {
208 size_t size;
209 ipc_callid_t name_id;
210 if (!async_data_read_receive(&name_id, &size)) {
211 async_answer_0(name_id, EPARTY);
212 return;
213 }
214 if (size != name_size) {
215 async_answer_0(name_id, ELIMIT);
216 return;
217 }
218 async_data_read_finalize(name_id, name, name_size);
219 }
220}
221
222void remote_audio_pcm_get_buffer(ddf_fun_t *fun, void *iface,
223 ipc_callid_t callid, ipc_call_t *call)
224{
225 const audio_pcm_iface_t *pcm_iface = iface;
226
227 if (!pcm_iface->get_buffer ||
228 !pcm_iface->release_buffer ||
229 !pcm_iface->set_event_session) {
230 async_answer_0(callid, ENOTSUP);
231 return;
232 }
233 void *buffer = NULL;
234 size_t size = DEV_IPC_GET_ARG1(*call);
235 unsigned id = 0;
236 int ret = pcm_iface->get_buffer(fun, &buffer, &size, &id);
237 async_answer_2(callid, ret, size, id);
238 if (ret != EOK || size == 0)
239 return;
240
241 /* Share the buffer. */
242 size_t share_size = 0;
243 ipc_callid_t share_id = 0;
244
245 ddf_msg(LVL_DEBUG2, "Receiving share request.");
246 if (!async_share_in_receive(&share_id, &share_size)) {
247 ddf_msg(LVL_DEBUG, "Failed to share pcm buffer.");
248 pcm_iface->release_buffer(fun, id);
249 async_answer_0(share_id, EPARTY);
250 return;
251 }
252
253 ddf_msg(LVL_DEBUG2, "Checking requested share size.");
254 if (share_size != size) {
255 ddf_msg(LVL_DEBUG, "Incorrect pcm buffer size requested.");
256 pcm_iface->release_buffer(fun, id);
257 async_answer_0(share_id, ELIMIT);
258 return;
259 }
260
261 ddf_msg(LVL_DEBUG2, "Calling share finalize.");
262 ret = async_share_in_finalize(share_id, buffer, AS_AREA_WRITE
263 | AS_AREA_READ);
264 if (ret != EOK) {
265 ddf_msg(LVL_DEBUG, "Failed to share buffer.");
266 pcm_iface->release_buffer(fun, id);
267 return;
268 }
269
270 ddf_msg(LVL_DEBUG2, "Buffer shared with size %zu, creating callback.",
271 share_size);
272 {
273 ipc_call_t call;
274 ipc_callid_t callid = async_get_call(&call);
275 async_sess_t *sess =
276 async_callback_receive_start(EXCHANGE_ATOMIC, &call);
277 if (sess == NULL) {
278 ddf_msg(LVL_DEBUG, "Failed to create event callback");
279 pcm_iface->release_buffer(fun, id);
280 async_answer_0(callid, EAGAIN);
281 return;
282 }
283 ret = pcm_iface->set_event_session(fun, id, sess);
284 if (ret != EOK) {
285 ddf_msg(LVL_DEBUG, "Failed to set event callback.");
286 pcm_iface->release_buffer(fun, id);
287 async_answer_0(callid, ret);
288 return;
289 }
290 ddf_msg(LVL_DEBUG2, "Buffer and event session setup OK.");
291 async_answer_0(callid, EOK);
292 }
293}
294
295void remote_audio_pcm_release_buffer(ddf_fun_t *fun, void *iface,
296 ipc_callid_t callid, ipc_call_t *call)
297{
298 const audio_pcm_iface_t *pcm_iface = iface;
299
300 const unsigned id = DEV_IPC_GET_ARG1(*call);
301 const int ret = pcm_iface->release_buffer ?
302 pcm_iface->release_buffer(fun, id) : ENOTSUP;
303 async_answer_0(callid, ret);
304}
305
306void remote_audio_pcm_start_playback(ddf_fun_t *fun, void *iface,
307 ipc_callid_t callid, ipc_call_t *call)
308{
309 const audio_pcm_iface_t *pcm_iface = iface;
310
311 const unsigned id = DEV_IPC_GET_ARG1(*call);
312 const unsigned rate = DEV_IPC_GET_ARG2(*call);
313 const unsigned parts = (DEV_IPC_GET_ARG3(*call) >> 24) & UINT8_MAX;
314 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
315 const pcm_sample_format_t format =DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
316
317 const int ret = pcm_iface->start_playback
318 ? pcm_iface->start_playback(fun, id, parts, channels, rate, format)
319 : ENOTSUP;
320 async_answer_0(callid, ret);
321}
322
323void remote_audio_pcm_stop_playback(ddf_fun_t *fun, void *iface,
324 ipc_callid_t callid, ipc_call_t *call)
325{
326 const audio_pcm_iface_t *pcm_iface = iface;
327
328 const unsigned id = DEV_IPC_GET_ARG1(*call);
329 const int ret = pcm_iface->stop_playback ?
330 pcm_iface->stop_playback(fun, id) : ENOTSUP;
331 async_answer_0(callid, ret);
332}
333
334void remote_audio_pcm_start_record(ddf_fun_t *fun, void *iface,
335 ipc_callid_t callid, ipc_call_t *call)
336{
337 const audio_pcm_iface_t *pcm_iface = iface;
338
339 const unsigned id = DEV_IPC_GET_ARG1(*call);
340 const unsigned rate = DEV_IPC_GET_ARG2(*call);
341 const unsigned parts = (DEV_IPC_GET_ARG3(*call) >> 24) & UINT8_MAX;
342 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
343 const pcm_sample_format_t format =DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
344
345 const int ret = pcm_iface->start_record
346 ? pcm_iface->start_record(fun, id, parts, channels, rate, format)
347 : ENOTSUP;
348 async_answer_0(callid, ret);
349}
350
351void remote_audio_pcm_stop_record(ddf_fun_t *fun, void *iface,
352 ipc_callid_t callid, ipc_call_t *call)
353{
354 const audio_pcm_iface_t *pcm_iface = iface;
355
356 const unsigned id = DEV_IPC_GET_ARG1(*call);
357 const int ret = pcm_iface->stop_record ?
358 pcm_iface->stop_record(fun, id) : ENOTSUP;
359 async_answer_0(callid, ret);
360}
361
362#if 0
363
364void remote_audio_mixer_get_info(
365 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
366{
367 audio_mixer_iface_t *mixer_iface = iface;
368
369 if (!mixer_iface->get_info) {
370 async_answer_0(callid, ENOTSUP);
371 return;
372 }
373 const char *name = NULL;
374 unsigned items = 0;
375 const int ret = mixer_iface->get_info(fun, &name, &items);
376 const size_t name_size = name ? str_size(name) + 1 : 0;
377 async_answer_2(callid, ret, name_size, items);
378 /* Send the name. */
379 if (ret == EOK && name_size > 0) {
380 size_t size;
381 ipc_callid_t name_id;
382 if (!async_data_read_receive(&name_id, &size)) {
383 async_answer_0(name_id, EPARTY);
384 return;
385 }
386 if (size != name_size) {
387 async_answer_0(name_id, ELIMIT);
388 return;
389 }
390 async_data_read_finalize(name_id, name, name_size);
391 }
392}
393
394void remote_audio_mixer_get_item_info(
395 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
396{
397 audio_mixer_iface_t *mixer_iface = iface;
398
399 if (!mixer_iface->get_item_info) {
400 async_answer_0(callid, ENOTSUP);
401 return;
402 }
403
404 const unsigned item = DEV_IPC_GET_ARG1(*call);
405 const char *name = NULL;
406 unsigned channels = 0;
407 const int ret = mixer_iface->get_item_info(fun, item, &name, &channels);
408 const size_t name_size = name ? str_size(name) + 1 : 0;
409 async_answer_2(callid, ret, name_size, channels);
410 /* Send the name. */
411 if (ret == EOK && name_size > 0) {
412 size_t size;
413 ipc_callid_t name_id;
414 if (!async_data_read_receive(&name_id, &size)) {
415 async_answer_0(name_id, EPARTY);
416 return;
417 }
418 if (size != name_size) {
419 async_answer_0(name_id, ELIMIT);
420 return;
421 }
422 async_data_read_finalize(name_id, name, name_size);
423 }
424}
425
426void remote_audio_mixer_get_channel_info(
427 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
428{
429 audio_mixer_iface_t *mixer_iface = iface;
430
431 if (!mixer_iface->get_channel_info) {
432 async_answer_0(callid, ENOTSUP);
433 return;
434 }
435
436 const unsigned item = DEV_IPC_GET_ARG1(*call);
437 const unsigned channel = DEV_IPC_GET_ARG2(*call);
438 const char *name = NULL;
439 unsigned levels = 0;
440 const int ret =
441 mixer_iface->get_channel_info(fun, item, channel, &name, &levels);
442 const size_t name_size = name ? str_size(name) + 1 : 0;
443 async_answer_2(callid, ret, name_size, levels);
444 /* Send the name. */
445 if (ret == EOK && name_size > 0) {
446 size_t size;
447 ipc_callid_t name_id;
448 if (!async_data_read_receive(&name_id, &size)) {
449 async_answer_0(name_id, EPARTY);
450 return;
451 }
452 if (size != name_size) {
453 async_answer_0(name_id, ELIMIT);
454 return;
455 }
456 async_data_read_finalize(name_id, name, name_size);
457 }
458}
459
460void remote_audio_mixer_channel_mute_set(
461 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
462{
463 audio_mixer_iface_t *mixer_iface = iface;
464
465 if (!mixer_iface->channel_mute_set) {
466 async_answer_0(callid, ENOTSUP);
467 return;
468 }
469 const unsigned item = DEV_IPC_GET_ARG1(*call);
470 const unsigned channel = DEV_IPC_GET_ARG2(*call);
471 const bool mute = DEV_IPC_GET_ARG3(*call);
472 const int ret = mixer_iface->channel_mute_set(fun, item, channel, mute);
473 async_answer_0(callid, ret);
474}
475
476void remote_audio_mixer_channel_mute_get(
477 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
478{
479 audio_mixer_iface_t *mixer_iface = iface;
480
481 if (!mixer_iface->channel_mute_get) {
482 async_answer_0(callid, ENOTSUP);
483 return;
484 }
485 const unsigned item = DEV_IPC_GET_ARG1(*call);
486 const unsigned channel = DEV_IPC_GET_ARG2(*call);
487 bool mute = false;
488 const int ret =
489 mixer_iface->channel_mute_get(fun, item, channel, &mute);
490 async_answer_1(callid, ret, mute);
491}
492
493void remote_audio_mixer_channel_volume_set(
494 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
495{
496 audio_mixer_iface_t *mixer_iface = iface;
497
498 if (!mixer_iface->channel_volume_set) {
499 async_answer_0(callid, ENOTSUP);
500 return;
501 }
502 const unsigned item = DEV_IPC_GET_ARG1(*call);
503 const unsigned channel = DEV_IPC_GET_ARG2(*call);
504 const unsigned level = DEV_IPC_GET_ARG3(*call);
505 const int ret =
506 mixer_iface->channel_volume_set(fun, item, channel, level);
507 async_answer_0(callid, ret);
508}
509
510void remote_audio_mixer_channel_volume_get(
511 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
512{
513 audio_mixer_iface_t *mixer_iface = iface;
514
515 if (!mixer_iface->channel_volume_get) {
516 async_answer_0(callid, ENOTSUP);
517 return;
518 }
519 const unsigned item = DEV_IPC_GET_ARG1(*call);
520 const unsigned channel = DEV_IPC_GET_ARG2(*call);
521 unsigned current = 0, max = 0;
522 const int ret =
523 mixer_iface->channel_volume_get(fun, item, channel, &current, &max);
524 async_answer_2(callid, ret, current, max);
525}
526#endif
527
528/**
529 * @}
530 */
531
Note: See TracBrowser for help on using the repository browser.