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

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

typo

  • Property mode set to 100644
File size: 22.9 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 <devman.h>
36#include <ddf/log.h>
37#include <errno.h>
38#include <str.h>
39#include <as.h>
40#include <sys/mman.h>
41
42#include "audio_pcm_iface.h"
43#include "ddf/driver.h"
44
45typedef enum {
46 IPC_M_AUDIO_PCM_GET_INFO_STR,
47 IPC_M_AUDIO_PCM_QUERY_CAPS,
48 IPC_M_AUDIO_PCM_REGISTER_EVENTS,
49 IPC_M_AUDIO_PCM_UNREGISTER_EVENTS,
50 IPC_M_AUDIO_PCM_TEST_FORMAT,
51 IPC_M_AUDIO_PCM_GET_BUFFER,
52 IPC_M_AUDIO_PCM_RELEASE_BUFFER,
53 IPC_M_AUDIO_PCM_GET_BUFFER_POS,
54 IPC_M_AUDIO_PCM_START_PLAYBACK,
55 IPC_M_AUDIO_PCM_STOP_PLAYBACK,
56 IPC_M_AUDIO_PCM_START_CAPTURE,
57 IPC_M_AUDIO_PCM_STOP_CAPTURE,
58} audio_pcm_iface_funcs_t;
59
60const char *audio_pcm_cap_str(audio_cap_t cap)
61{
62 static const char *caps[] = {
63 [AUDIO_CAP_CAPTURE] = "CAPTURE",
64 [AUDIO_CAP_PLAYBACK] = "PLAYBACK",
65 [AUDIO_CAP_MAX_BUFFER] = "MAXIMUM BUFFER SIZE",
66 [AUDIO_CAP_BUFFER_POS] = "KNOWS BUFFER POSITION",
67 [AUDIO_CAP_INTERRUPT] = "FRAGMENT INTERRUPTS",
68 [AUDIO_CAP_INTERRUPT_MIN_FRAMES] = "MINIMUM FRAGMENT SIZE",
69 [AUDIO_CAP_INTERRUPT_MAX_FRAMES] = "MAXIMUM FRAGMENT SIZE",
70 };
71 if (cap > (sizeof(caps) / sizeof(*caps)))
72 return "UNKNOWN CAP";
73 return caps[cap];
74
75}
76
77const char *audio_pcm_event_str(pcm_event_t event)
78{
79 static const char *events[] = {
80 [PCM_EVENT_PLAYBACK_STARTED] = "PLAYBACK STARTED",
81 [PCM_EVENT_CAPTURE_STARTED] = "CAPTURE STARTED",
82 [PCM_EVENT_FRAMES_PLAYED] = "FRAGMENT PLAYED",
83 [PCM_EVENT_FRAMES_CAPTURED] = "FRAGMENT CAPTURED",
84 [PCM_EVENT_PLAYBACK_TERMINATED] = "PLAYBACK TERMINATED",
85 [PCM_EVENT_CAPTURE_TERMINATED] = "CAPTURE TERMINATED",
86 };
87 if (event > (sizeof(events) / sizeof(*events)))
88 return "UNKNOWN EVENT";
89 return events[event];
90}
91
92/*
93 * CLIENT SIDE
94 */
95
96/**
97 * Open audio session with device identified by location service string.
98 *
99 * @param name Location service string.
100 * @return Pointer to a new audio device session, NULL on failure.
101 */
102audio_pcm_sess_t *audio_pcm_open(const char *name)
103{
104 devman_handle_t device_handle = 0;
105 const int ret = devman_fun_get_handle(name, &device_handle, 0);
106 if (ret != EOK)
107 return NULL;
108 return devman_device_connect(EXCHANGE_SERIALIZE, device_handle,
109 IPC_FLAG_BLOCKING);
110}
111
112/**
113 * Open audio session with device identified by location service id
114 *
115 * @param name Location service id.
116 * @return Pointer to a new audio device session, NULL on failure.
117 */
118audio_pcm_sess_t *audio_pcm_open_service(service_id_t id)
119{
120 return loc_service_connect(EXCHANGE_SERIALIZE, id, IPC_FLAG_BLOCKING);
121}
122
123/**
124 * Close open audio device session.
125 *
126 * @param name Open audio device session.
127 *
128 * @note Calling this function on already closed or invalid session results
129 * in undefined behavior.
130 */
131void audio_pcm_close(audio_pcm_sess_t *sess)
132{
133 if (sess)
134 async_hangup(sess);
135}
136
137/**
138 * Get a short description string.
139 *
140 * @param sess Audio device session.
141 * @param name Place to store newly allocated string.
142 *
143 * @return Error code.
144 *
145 * @note Caller is responsible for freeing newly allocated memory.
146 */
147int audio_pcm_get_info_str(audio_pcm_sess_t *sess, const char **name)
148{
149 if (!name)
150 return EINVAL;
151 async_exch_t *exch = async_exchange_begin(sess);
152 sysarg_t name_size;
153 const int ret = async_req_1_1(exch,
154 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
155 IPC_M_AUDIO_PCM_GET_INFO_STR, &name_size);
156 if (ret == EOK) {
157 char *name_place = calloc(1, name_size);
158 if (!name_place) {
159 /* Make the other side fail
160 * as it waits for read request */
161 async_data_read_start(exch, (void*)-1, 0);
162 async_exchange_end(exch);
163 return ENOMEM;
164 }
165 const int ret =
166 async_data_read_start(exch, name_place, name_size);
167 if (ret != EOK) {
168 free(name_place);
169 async_exchange_end(exch);
170 return ret;
171 }
172 *name = name_place;
173 }
174 async_exchange_end(exch);
175 return ret;
176}
177
178
179/**
180 * Query value of specified capability.
181 *
182 * @param sess Audio device session.
183 * @param cap Audio device capability.
184 * @param val Place to store queried value.
185 *
186 * @return Error code.
187 */
188int audio_pcm_query_cap(audio_pcm_sess_t *sess, audio_cap_t cap)
189{
190 async_exch_t *exch = async_exchange_begin(sess);
191 sysarg_t value = 0;
192 const int ret = async_req_2_1(exch,
193 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_QUERY_CAPS,
194 cap, &value);
195 async_exchange_end(exch);
196 if (ret == EOK)
197 return value;
198 return ret;
199}
200
201/**
202 * Query current position in device buffer.
203 *
204 * @param sess Audio device session.
205 * @param pos Place to store the result.
206 *
207 * @return Error code.
208 *
209 * Works for both playback and capture.
210 */
211int audio_pcm_get_buffer_pos(audio_pcm_sess_t *sess, size_t *pos)
212{
213 if (!pos)
214 return EINVAL;
215 async_exch_t *exch = async_exchange_begin(sess);
216 sysarg_t value = 0;
217 const int ret = async_req_1_1(exch,
218 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
219 IPC_M_AUDIO_PCM_GET_BUFFER_POS, &value);
220 if (ret == EOK)
221 *pos = value;
222 async_exchange_end(exch);
223 return ret;
224}
225
226/**
227 * Test format parameters for device support.
228 *
229 * @param sess Audio device session.
230 * @param channels Number of channels
231 * @param rate Sampling rate.
232 * @format Sample format.
233 *
234 * @return Error code.
235 *
236 * Works for both playback and capture. This function modifies provided
237 * parameters to the nearest values supported by the device.
238 */
239int audio_pcm_test_format(audio_pcm_sess_t *sess, unsigned *channels,
240 unsigned *rate, pcm_sample_format_t *format)
241{
242 async_exch_t *exch = async_exchange_begin(sess);
243 sysarg_t channels_arg = channels ? *channels : 0;
244 sysarg_t rate_arg = rate ? *rate : 0;
245 sysarg_t format_arg = format ? *format : 0;
246 const int ret = async_req_4_3(exch,
247 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
248 IPC_M_AUDIO_PCM_TEST_FORMAT, channels_arg, rate_arg, format_arg,
249 &channels_arg, &rate_arg, &format_arg);
250 async_exchange_end(exch);
251
252 /* All OK or something has changed. Verify that it was not one of the
253 * params we care about */
254 if ((ret == EOK || ret == ELIMIT)
255 && (!channels || *channels == channels_arg)
256 && (!rate || *rate == rate_arg)
257 && (!format || *format == format_arg))
258 return EOK;
259 if (channels)
260 *channels = channels_arg;
261 if (rate)
262 *rate = rate_arg;
263 if (format)
264 *format = format_arg;
265 return ret;
266}
267
268/**
269 * Register callback for device generated events.
270 *
271 * @param sess Audio device session.
272 * @param event_rec Event callback function.
273 * @param arg Event callback custom parameter.
274 *
275 * @return Error code.
276 */
277int audio_pcm_register_event_callback(audio_pcm_sess_t *sess,
278 async_client_conn_t event_callback, void *arg)
279{
280 if (!event_callback)
281 return EINVAL;
282
283 async_exch_t *exch = async_exchange_begin(sess);
284 int ret = async_req_1_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
285 IPC_M_AUDIO_PCM_REGISTER_EVENTS);
286 if (ret == EOK) {
287 ret = async_connect_to_me(exch, 0, 0, 0, event_callback, arg);
288 }
289 async_exchange_end(exch);
290 return ret;
291}
292
293/**
294 * Unregister callback for device generated events.
295 *
296 * @param sess Audio device session.
297 *
298 * @return Error code.
299 */
300int audio_pcm_unregister_event_callback(audio_pcm_sess_t *sess)
301{
302 async_exch_t *exch = async_exchange_begin(sess);
303 const int ret = async_req_1_0(exch,
304 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
305 IPC_M_AUDIO_PCM_UNREGISTER_EVENTS);
306 async_exchange_end(exch);
307 return ret;
308}
309
310/**
311 * Get device accessible playback/capture buffer.
312 *
313 * @param sess Audio device session.
314 * @param buffer Place to store pointer to the buffer.
315 * @param size Place to store buffer size (bytes).
316 *
317 * @return Error code.
318 */
319int audio_pcm_get_buffer(audio_pcm_sess_t *sess, void **buffer, size_t *size)
320{
321 if (!buffer || !size)
322 return EINVAL;
323
324 async_exch_t *exch = async_exchange_begin(sess);
325
326 sysarg_t buffer_size = *size;
327 int ret = async_req_2_1(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
328 IPC_M_AUDIO_PCM_GET_BUFFER, (sysarg_t)buffer_size, &buffer_size);
329 if (ret == EOK) {
330 void *dst = NULL;
331 ret = async_share_in_start_0_0(exch, buffer_size, &dst);
332 if (ret != EOK) {
333 async_exchange_end(exch);
334 return ret;
335 }
336 *buffer = dst;
337 *size = buffer_size;
338 }
339 async_exchange_end(exch);
340 return ret;
341}
342
343/**
344 * Release device accessible playback/capture buffer.
345 *
346 * @param sess Audio device session.
347 *
348 * @return Error code.
349 */
350int audio_pcm_release_buffer(audio_pcm_sess_t *sess)
351{
352 async_exch_t *exch = async_exchange_begin(sess);
353 const int ret = async_req_1_0(exch,
354 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
355 IPC_M_AUDIO_PCM_RELEASE_BUFFER);
356 async_exchange_end(exch);
357 return ret;
358}
359
360/**
361 * Start playback on buffer from position 0.
362 *
363 * @param sess Audio device session.
364 * @param frames Size of fragment (in frames).
365 * @param channels Number of channels.
366 * @param sample_rate Sampling rate (for one channel).
367 * @param format Sample format.
368 *
369 * @return Error code.
370 *
371 * Event will be generated after every fragment. Set fragment size to
372 * 0 to turn off event generation.
373 */
374int audio_pcm_start_playback_fragment(audio_pcm_sess_t *sess, unsigned frames,
375 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
376{
377 if (channels > UINT16_MAX)
378 return EINVAL;
379 assert((format & UINT16_MAX) == format);
380 const sysarg_t packed = (channels << 16) | (format & UINT16_MAX);
381 async_exch_t *exch = async_exchange_begin(sess);
382 const int ret = async_req_4_0(exch,
383 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
384 IPC_M_AUDIO_PCM_START_PLAYBACK,
385 frames, sample_rate, packed);
386 async_exchange_end(exch);
387 return ret;
388}
389/**
390 * Stops playback after current fragment.
391 *
392 * @param sess Audio device session.
393 *
394 * @return Error code.
395 */
396int audio_pcm_last_playback_fragment(audio_pcm_sess_t *sess)
397{
398 async_exch_t *exch = async_exchange_begin(sess);
399 const int ret = async_req_2_0(exch,
400 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
401 IPC_M_AUDIO_PCM_STOP_PLAYBACK, false);
402 async_exchange_end(exch);
403 return ret;
404}
405
406/**
407 * Start playback on buffer from the current position.
408 *
409 * @param sess Audio device session.
410 * @param channels Number of channels.
411 * @param sample_rate Sampling rate (for one channel).
412 * @param format Sample format.
413 *
414 * @return Error code.
415 */
416int audio_pcm_start_playback(audio_pcm_sess_t *sess,
417 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
418{
419 return audio_pcm_start_playback_fragment(
420 sess, 0, channels, sample_rate, format);
421}
422
423/**
424 * Immediately stops current playback.
425 *
426 * @param sess Audio device session.
427 *
428 * @return Error code.
429 */
430int audio_pcm_stop_playback(audio_pcm_sess_t *sess)
431{
432 async_exch_t *exch = async_exchange_begin(sess);
433 const int ret = async_req_2_0(exch,
434 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
435 IPC_M_AUDIO_PCM_STOP_PLAYBACK, true);
436 async_exchange_end(exch);
437 return ret;
438}
439
440/**
441 * Start capture on buffer from the current position.
442 *
443 * @param sess Audio device session.
444 * @param frames Size of fragment (in frames).
445 * @param channels Number of channels.
446 * @param sample_rate Sampling rate (for one channel).
447 * @param format Sample format.
448 *
449 * @return Error code.
450 *
451 * Event will be generated after every fragment. Set fragment size to
452 * 0 to turn off event generation.
453 */
454int audio_pcm_start_capture_fragment(audio_pcm_sess_t *sess, unsigned frames,
455 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
456{
457 if (channels > UINT16_MAX)
458 return EINVAL;
459 assert((format & UINT16_MAX) == format);
460 const sysarg_t packed = (channels << 16) | (format & UINT16_MAX);
461 async_exch_t *exch = async_exchange_begin(sess);
462 const int ret = async_req_4_0(exch,
463 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_START_CAPTURE,
464 frames, sample_rate, packed);
465 async_exchange_end(exch);
466 return ret;
467}
468
469/**
470 * Start capture on buffer from the current position.
471 *
472 * @param sess Audio device session.
473 * @param channels Number of channels.
474 * @param sample_rate Sampling rate (for one channel).
475 * @param format Sample format.
476 *
477 * @return Error code.
478 */
479int audio_pcm_start_capture(audio_pcm_sess_t *sess,
480 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
481{
482 return audio_pcm_start_capture_fragment(
483 sess, 0, channels, sample_rate, format);
484}
485
486/**
487 * Stops capture at the end of current fragment.
488 *
489 * Won't work if capture was started with fragment size 0.
490 * @param sess Audio device session.
491 *
492 * @return Error code.
493 */
494int audio_pcm_last_capture_fragment(audio_pcm_sess_t *sess)
495{
496 async_exch_t *exch = async_exchange_begin(sess);
497 const int ret = async_req_2_0(exch,
498 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
499 IPC_M_AUDIO_PCM_STOP_CAPTURE, false);
500 async_exchange_end(exch);
501 return ret;
502}
503
504/**
505 * Immediately stops current capture.
506 *
507 * @param sess Audio device session.
508 *
509 * @return Error code.
510 */
511int audio_pcm_stop_capture(audio_pcm_sess_t *sess)
512{
513 async_exch_t *exch = async_exchange_begin(sess);
514 const int ret = async_req_2_0(exch,
515 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
516 IPC_M_AUDIO_PCM_STOP_CAPTURE, true);
517 async_exchange_end(exch);
518 return ret;
519}
520
521/*
522 * SERVER SIDE
523 */
524static void remote_audio_pcm_get_info_str(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
525static void remote_audio_pcm_query_caps(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
526static void remote_audio_pcm_events_register(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
527static void remote_audio_pcm_events_unregister(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
528static void remote_audio_pcm_get_buffer_pos(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
529static void remote_audio_pcm_test_format(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
530static void remote_audio_pcm_get_buffer(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
531static void remote_audio_pcm_release_buffer(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
532static void remote_audio_pcm_start_playback(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
533static void remote_audio_pcm_stop_playback(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
534static void remote_audio_pcm_start_capture(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
535static void remote_audio_pcm_stop_capture(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
536
537/** Remote audio pcm buffer interface operations. */
538static remote_iface_func_ptr_t remote_audio_pcm_iface_ops[] = {
539 [IPC_M_AUDIO_PCM_GET_INFO_STR] = remote_audio_pcm_get_info_str,
540 [IPC_M_AUDIO_PCM_QUERY_CAPS] = remote_audio_pcm_query_caps,
541 [IPC_M_AUDIO_PCM_REGISTER_EVENTS] = remote_audio_pcm_events_register,
542 [IPC_M_AUDIO_PCM_UNREGISTER_EVENTS] = remote_audio_pcm_events_unregister,
543 [IPC_M_AUDIO_PCM_GET_BUFFER_POS] = remote_audio_pcm_get_buffer_pos,
544 [IPC_M_AUDIO_PCM_TEST_FORMAT] = remote_audio_pcm_test_format,
545 [IPC_M_AUDIO_PCM_GET_BUFFER] = remote_audio_pcm_get_buffer,
546 [IPC_M_AUDIO_PCM_RELEASE_BUFFER] = remote_audio_pcm_release_buffer,
547 [IPC_M_AUDIO_PCM_START_PLAYBACK] = remote_audio_pcm_start_playback,
548 [IPC_M_AUDIO_PCM_STOP_PLAYBACK] = remote_audio_pcm_stop_playback,
549 [IPC_M_AUDIO_PCM_START_CAPTURE] = remote_audio_pcm_start_capture,
550 [IPC_M_AUDIO_PCM_STOP_CAPTURE] = remote_audio_pcm_stop_capture,
551};
552
553/** Remote audio mixer interface structure. */
554remote_iface_t remote_audio_pcm_iface = {
555 .method_count = sizeof(remote_audio_pcm_iface_ops) /
556 sizeof(remote_audio_pcm_iface_ops[0]),
557 .methods = remote_audio_pcm_iface_ops
558};
559
560void remote_audio_pcm_get_info_str(ddf_fun_t *fun, void *iface,
561 ipc_callid_t callid, ipc_call_t *call)
562{
563 const audio_pcm_iface_t *pcm_iface = iface;
564
565 if (!pcm_iface->get_info_str) {
566 async_answer_0(callid, ENOTSUP);
567 return;
568 }
569 const char *name = NULL;
570 const int ret = pcm_iface->get_info_str(fun, &name);
571 const size_t name_size = name ? str_size(name) + 1 : 0;
572 async_answer_1(callid, ret, name_size);
573 /* Send the string. */
574 if (ret == EOK && name_size > 0) {
575 size_t size;
576 ipc_callid_t name_id;
577 if (!async_data_read_receive(&name_id, &size)) {
578 async_answer_0(name_id, EPARTY);
579 return;
580 }
581 if (size != name_size) {
582 async_answer_0(name_id, ELIMIT);
583 return;
584 }
585 async_data_read_finalize(name_id, name, name_size);
586 }
587}
588
589void remote_audio_pcm_query_caps(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
590{
591 const audio_pcm_iface_t *pcm_iface = iface;
592 const audio_cap_t cap = DEV_IPC_GET_ARG1(*call);
593 if (pcm_iface->query_cap) {
594 const unsigned value = pcm_iface->query_cap(fun, cap);
595 async_answer_1(callid, EOK, value);
596 } else {
597 async_answer_0(callid, ENOTSUP);
598 }
599}
600
601static void remote_audio_pcm_events_register(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
602{
603 const audio_pcm_iface_t *pcm_iface = iface;
604 if (!pcm_iface->get_event_session ||
605 !pcm_iface->set_event_session) {
606 async_answer_0(callid, ENOTSUP);
607 return;
608 }
609
610 async_answer_0(callid, EOK);
611
612 ipc_call_t callback_call;
613 ipc_callid_t callback_id = async_get_call(&callback_call);
614 async_sess_t *sess =
615 async_callback_receive_start(EXCHANGE_ATOMIC, &callback_call);
616 if (sess == NULL) {
617 ddf_msg(LVL_DEBUG, "Failed to create event callback");
618 pcm_iface->release_buffer(fun);
619 async_answer_0(callback_id, EAGAIN);
620 return;
621 }
622 const int ret = pcm_iface->set_event_session(fun, sess);
623 if (ret != EOK) {
624 ddf_msg(LVL_DEBUG, "Failed to set event callback.");
625 pcm_iface->release_buffer(fun);
626 async_hangup(sess);
627 async_answer_0(callback_id, ret);
628 return;
629 }
630 async_answer_0(callback_id, EOK);
631}
632
633static void remote_audio_pcm_events_unregister(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
634{
635 const audio_pcm_iface_t *pcm_iface = iface;
636 if (!pcm_iface->get_event_session ||
637 !pcm_iface->set_event_session) {
638 async_answer_0(callid, ENOTSUP);
639 return;
640 }
641 async_sess_t *sess = pcm_iface->get_event_session(fun);
642 if (sess) {
643 async_hangup(sess);
644 pcm_iface->set_event_session(fun, NULL);
645 }
646 async_answer_0(callid, EOK);
647}
648
649void remote_audio_pcm_get_buffer_pos(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
650{
651 const audio_pcm_iface_t *pcm_iface = iface;
652 size_t pos = 0;
653 const int ret = pcm_iface->get_buffer_pos ?
654 pcm_iface->get_buffer_pos(fun, &pos) : ENOTSUP;
655 async_answer_1(callid, ret, pos);
656}
657
658void remote_audio_pcm_test_format(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
659{
660 const audio_pcm_iface_t *pcm_iface = iface;
661 unsigned channels = DEV_IPC_GET_ARG1(*call);
662 unsigned rate = DEV_IPC_GET_ARG2(*call);
663 pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call);
664 const int ret = pcm_iface->test_format ?
665 pcm_iface->test_format(fun, &channels, &rate, &format) : ENOTSUP;
666 async_answer_3(callid, ret, channels, rate, format);
667}
668
669void remote_audio_pcm_get_buffer(ddf_fun_t *fun, void *iface,
670 ipc_callid_t callid, ipc_call_t *call)
671{
672 const audio_pcm_iface_t *pcm_iface = iface;
673
674 if (!pcm_iface->get_buffer ||
675 !pcm_iface->release_buffer) {
676 async_answer_0(callid, ENOTSUP);
677 return;
678 }
679 void *buffer = NULL;
680 size_t size = DEV_IPC_GET_ARG1(*call);
681 int ret = pcm_iface->get_buffer(fun, &buffer, &size);
682 async_answer_1(callid, ret, size);
683 if (ret != EOK || size == 0)
684 return;
685
686 /* Share the buffer. */
687 size_t share_size = 0;
688 ipc_callid_t share_id = 0;
689
690 ddf_msg(LVL_DEBUG2, "Receiving share request.");
691 if (!async_share_in_receive(&share_id, &share_size)) {
692 ddf_msg(LVL_DEBUG, "Failed to share pcm buffer.");
693 pcm_iface->release_buffer(fun);
694 async_answer_0(share_id, EPARTY);
695 return;
696 }
697
698 ddf_msg(LVL_DEBUG2, "Checking requested share size.");
699 if (share_size != size) {
700 ddf_msg(LVL_DEBUG, "Incorrect pcm buffer size requested.");
701 pcm_iface->release_buffer(fun);
702 async_answer_0(share_id, ELIMIT);
703 return;
704 }
705
706 ddf_msg(LVL_DEBUG2, "Calling share finalize.");
707 ret = async_share_in_finalize(share_id, buffer, AS_AREA_WRITE
708 | AS_AREA_READ);
709 if (ret != EOK) {
710 ddf_msg(LVL_DEBUG, "Failed to share buffer.");
711 pcm_iface->release_buffer(fun);
712 return;
713 }
714
715 ddf_msg(LVL_DEBUG2, "Buffer shared with size %zu.", share_size);
716}
717
718void remote_audio_pcm_release_buffer(ddf_fun_t *fun, void *iface,
719 ipc_callid_t callid, ipc_call_t *call)
720{
721 const audio_pcm_iface_t *pcm_iface = iface;
722
723 const int ret = pcm_iface->release_buffer ?
724 pcm_iface->release_buffer(fun) : ENOTSUP;
725 async_answer_0(callid, ret);
726}
727
728void remote_audio_pcm_start_playback(ddf_fun_t *fun, void *iface,
729 ipc_callid_t callid, ipc_call_t *call)
730{
731 const audio_pcm_iface_t *pcm_iface = iface;
732
733 const unsigned frames = DEV_IPC_GET_ARG1(*call);
734 const unsigned rate = DEV_IPC_GET_ARG2(*call);
735 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
736 const pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
737
738 const int ret = pcm_iface->start_playback
739 ? pcm_iface->start_playback(fun, frames, channels, rate, format)
740 : ENOTSUP;
741 async_answer_0(callid, ret);
742}
743
744void remote_audio_pcm_stop_playback(ddf_fun_t *fun, void *iface,
745 ipc_callid_t callid, ipc_call_t *call)
746{
747 const audio_pcm_iface_t *pcm_iface = iface;
748 const bool immediate = DEV_IPC_GET_ARG1(*call);
749
750 const int ret = pcm_iface->stop_playback ?
751 pcm_iface->stop_playback(fun, immediate) : ENOTSUP;
752 async_answer_0(callid, ret);
753}
754
755void remote_audio_pcm_start_capture(ddf_fun_t *fun, void *iface,
756 ipc_callid_t callid, ipc_call_t *call)
757{
758 const audio_pcm_iface_t *pcm_iface = iface;
759
760 const unsigned frames = DEV_IPC_GET_ARG1(*call);
761 const unsigned rate = DEV_IPC_GET_ARG2(*call);
762 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT16_MAX;
763 const pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
764
765 const int ret = pcm_iface->start_capture
766 ? pcm_iface->start_capture(fun, frames, channels, rate, format)
767 : ENOTSUP;
768 async_answer_0(callid, ret);
769}
770
771void remote_audio_pcm_stop_capture(ddf_fun_t *fun, void *iface,
772 ipc_callid_t callid, ipc_call_t *call)
773{
774 const audio_pcm_iface_t *pcm_iface = iface;
775 const bool immediate = DEV_IPC_GET_ARG1(*call);
776
777 const int ret = pcm_iface->stop_capture ?
778 pcm_iface->stop_capture(fun, immediate) : ENOTSUP;
779 async_answer_0(callid, ret);
780}
781
782/**
783 * @}
784 */
785
Note: See TracBrowser for help on using the repository browser.