Debian Codegraph @

maemo/dsp-protocol.c

raw symbols SCIP
1 /**
2 * @file dsp-protocol.c
3 * @brief Protocol to communicate with DSP side.
4 * <p>
5 * Copyright (C) 2006 Nokia Corporation
6 * <p>
7 * Contact: Eduardo Bezerra Valentin <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * */
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <poll.h>
27 #include <sys/errno.h>
28 #include <sys/types.h>
29 #include <sys/uio.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/mman.h>
33 #include <math.h>
34 #include <sys/ioctl.h>
35 #include <sys/sem.h>
36
37 #include "dsp-protocol.h"
38 #include "constants.h"
39 #include "debug.h"
40 #include "types.h"
41 #include "reporting.h"
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #ifdef USE_RESOURCE_MANAGER
48 #define DBUS_API_SUBJECT_TO_CHANGE
49 #include <dbus/dbus.h>
50 #define AUDIO_PM_SERVICE "com.nokia.osso_audio_pm"
51 #define AUDIO_PM_PLAYBACK_RESOURCE "/com/nokia/osso/pm/audio/playback"
52 #define AUDIO_PM_RECORD_RESOURCE "/com/nokia/osso/pm/audio/record"
53 #define RESOURCE_INTERFACE "com.nokia.osso_resource_manager"
54 #define RESOURCE_TIMEOUT 200
55 #endif /* USE_RESOURCE_MANAGER */
56
57 #define MAGIC_NUMBER 0x00A3D70A
58 #define PANNING_STEP 0x06
59 #define TASK_IOCTL_LOCK 0x10002
60 #define TASK_IOCTL_UNLOCK 0x10003
61 /* internal datatypes declarations */
62 union semun {
63 int val; /* value for SETVAL */
64 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
65 u_short *array; /* array for GETALL & SETALL */
66 };
67 /* internal features declarations */
68 static int dsp_protocol_flush(dsp_protocol_t * dsp_protocol);
69 static int dsp_protocol_send_command(dsp_protocol_t * dsp_protocol,
70 const short int command);
71 static void dsp_protocol_linear2Q15(const unsigned short int input,
72 unsigned short int *scale,
73 unsigned short int *power2);
74 static void dsp_protocol_Q152linear(const unsigned short int scale,
75 const unsigned short int power2,
76 unsigned short int *output);
77 static int dsp_protocol_update_state(dsp_protocol_t * dsp_protocol);
78 static inline int dsp_protocol_get_sem(dsp_protocol_t * dsp_protocol);
79 static inline int dsp_protocol_lock_dev(dsp_protocol_t * dsp_protocol);
80 static inline int dsp_protocol_unlock_dev(dsp_protocol_t * dsp_protocol);
81 /* Initialisation phase features definitions */
82 /**
83 * @param dsp_protocol DSP protocol reference pointer to be instanciated.
84 *
85 * Creates new dsp_protocol object instance and initializes it
86 * with default parameters. After this the device must be
87 * separately opened with gst_dspaudio_open_node() method.
88 *
89 * @return zero if success, otherwise a negative error code
90 * (-ENOMEM - fail when requesting memory for data structures).
91 */
92 int dsp_protocol_create(dsp_protocol_t ** dsp_protocol)
93 {
94 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
95 int ret = 0;
96 DENTER();
97 *dsp_protocol = (dsp_protocol_t *) calloc(1, sizeof(dsp_protocol_t));
98 if ((*dsp_protocol) == NULL) {
99 DERROR("Could not allocate dsp_protocol instance\n");
100 ret = -ENOMEM;
101 goto out;
102 }
103 (*dsp_protocol)->fd = -1;
104 (*dsp_protocol)->device = NULL;
105 (*dsp_protocol)->state = STATE_UNINITIALISED;
106 (*dsp_protocol)->mute = 0;
107 (*dsp_protocol)->stream_id = 0;
108 (*dsp_protocol)->bridge_buffer_size = 0;
109 (*dsp_protocol)->mmap_buffer_size = 0;
110 (*dsp_protocol)->mmap_buffer = NULL;
111 (*dsp_protocol)->mutex = mutex;
112 (*dsp_protocol)->sem_set_id = -1;
113 #ifdef USE_RESOURCE_MANAGER
114 (*dsp_protocol)->dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
115 #endif
116 out:
117 DLEAVE(ret);
118 return ret;
119 }
120
121 /**
122 * @param dsp_protocol DSP protocol reference pointer to be initialized.
123 * @param device dsp device node name.
124 *
125 * Opens pcm dsp device file and initializes dsp_protocol
126 * with information about stream, state and mmapbuffer.
127 *
128 * @return zero if success, otherwise a negative error code.
129 */
130 int dsp_protocol_open_node(dsp_protocol_t * dsp_protocol, const char *device)
131 {
132 int ret;
133 short int tmp;
134 audio_status_info_t audio_status_info;
135 audio_init_status_t audio_init_status;
136 DENTER();
137 if (dsp_protocol->state != STATE_UNINITIALISED) {
138 report_dsp_protocol
139 ("Trying to send open node from a non-valid state",
140 dsp_protocol);
141 ret = -EIO;
142 goto out;
143 }
144
145 dsp_protocol->fd = open(device, O_RDWR);
146 if (dsp_protocol->fd < 0) {
147 DERROR("Could not open pcm device file %s\n", device);
148 ret = errno;
149 goto out;
150 }
151 dsp_protocol->device = strdup(device);
152 dsp_protocol_get_sem(dsp_protocol);
153 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
154 goto out;
155 if ((ret = dsp_protocol_flush(dsp_protocol)) < 0)
156 goto unlock;
157 tmp = DSP_CMD_STATE;
158 if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
159 ret = -EIO;
160 goto unlock;
161 }
162 if ((ret = read(dsp_protocol->fd, &audio_status_info,
163 sizeof(audio_status_info_t))) < 0) {
164 report_dsp_protocol("Could not read audio_status_info",
165 dsp_protocol);
166 goto unlock;
167 }
168 report_audio_status_info("Received:", audio_status_info);
169 if (audio_status_info.status == STATE_UNINITIALISED) {
170 tmp = DSP_CMD_INIT;
171 if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
172 ret = -EIO;
173 goto unlock;
174 }
175 if ((ret = read(dsp_protocol->fd, &audio_init_status,
176 sizeof(audio_init_status_t))) < 0) {
177 report_dsp_protocol("Error reading INIT status",
178 dsp_protocol);
179 goto unlock;
180 }
181 report_audio_init_status("Received:", audio_init_status);
182 /* receive info from audio_init_status */
183 dsp_protocol->stream_id = audio_init_status.stream_id;
184 dsp_protocol->bridge_buffer_size =
185 audio_init_status.bridge_buffer_size;
186 dsp_protocol->mmap_buffer_size =
187 audio_init_status.mmap_buffer_size;
188 } else {
189 /* This pcm task node is busy. Try to use another one. */
190 ret = -EBUSY;
191 goto unlock;
192 }
193 dsp_protocol->mmap_buffer = (short int *)
194 mmap((void *)0, dsp_protocol->mmap_buffer_size,
195 PROT_READ | PROT_WRITE, MAP_SHARED, dsp_protocol->fd, 0);
196
197 if (dsp_protocol->mmap_buffer == MAP_FAILED) {
198 report_dsp_protocol("Cannot mmap data buffer", dsp_protocol);
199 ret = -ENOMEM;
200 goto unlock;
201 }
202 dsp_protocol->state = STATE_INITIALISED;
203 report_dsp_protocol("connection stablished:", dsp_protocol);
204
205 ret = 0;
206 unlock:
207 dsp_protocol_unlock_dev(dsp_protocol);
208 out:
209 DLEAVE(ret);
210 return ret;
211 }
212
213 /**
214 * @param dsp_protocol DSP protocol reference pointer.
215 * @param audio_params_data audio params to be sent to the dsp.
216 *
217 * Send audio params to pcm task node and checks if
218 * it was sent properly.
219 *
220 * @return zero if success, otherwise a negative error code.
221 */
222 int dsp_protocol_send_audio_params(dsp_protocol_t * dsp_protocol,
223 audio_params_data_t * audio_params_data)
224 {
225 int ret;
226 dsp_cmd_status_t audio_cmd_status;
227 DENTER();
228 if (dsp_protocol->state != STATE_INITIALISED) {
229 report_dsp_protocol
230 ("Trying to send audio parameters from a non-valid state",
231 dsp_protocol);
232 ret = -EIO;
233 goto out;
234 }
235 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
236 goto out;
237 audio_params_data->ds_stream_id = dsp_protocol->stream_id;
238 if (write(dsp_protocol->fd, audio_params_data,
239 sizeof(audio_params_data_t)) < 0) {
240 ret = -1;
241 report_dsp_protocol("Could not send audio_params_data",
242 dsp_protocol);
243 goto unlock;
244 }
245 if (read(dsp_protocol->fd, &audio_cmd_status,
246 sizeof(dsp_cmd_status_t)) < 0) {
247 ret = -1;
248 report_dsp_protocol("Could not receive DSP_CMD_STATUS",
249 dsp_protocol);
250 goto unlock;
251 }
252 if (audio_cmd_status.status != DSP_OK) {
253 ret = -1;
254 report_dsp_protocol("DSP returned a diferent Status of DSP_OK",
255 dsp_protocol);
256 report_return_value("DSP returned", audio_cmd_status.status);
257 report_audio_params("Audio params sent", (*audio_params_data));
258 goto unlock;
259 }
260 report_audio_params("Audio params sent", (*audio_params_data));
261 ret = 0;
262 unlock:
263 dsp_protocol_unlock_dev(dsp_protocol);
264 out:
265 DLEAVE(ret);
266 return ret;
267 }
268
269 /**
270 * @param dsp_protocol DSP protocol reference pointer.
271 * @param speech_params_data audio params to be sent to the dsp.
272 *
273 * Send audio params to pcm_rec task node and checks if
274 * it was sent properly.
275 *
276 * @return zero if success, otherwise a negative error code.
277 */
278 int dsp_protocol_send_speech_params(dsp_protocol_t * dsp_protocol,
279 speech_params_data_t * speech_params_data)
280 {
281 int ret;
282 dsp_cmd_status_t audio_cmd_status;
283 DENTER();
284 if (dsp_protocol->state != STATE_INITIALISED) {
285 report_dsp_protocol
286 ("Trying to send speech parameters from a non-valid state",
287 dsp_protocol);
288 ret = -EIO;
289 goto out;
290 }
291 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
292 goto out;
293 speech_params_data->ds_stream_id = dsp_protocol->stream_id;
294 if (write(dsp_protocol->fd, speech_params_data,
295 sizeof(speech_params_data_t)) < 0) {
296 ret = -1;
297 report_dsp_protocol("Could not send speech_params_data",
298 dsp_protocol);
299 goto unlock;
300 }
301 if (read(dsp_protocol->fd, &audio_cmd_status,
302 sizeof(dsp_cmd_status_t)) < 0) {
303 ret = -1;
304 report_dsp_protocol("Could not receive DSP_CMD_STATUS",
305 dsp_protocol);
306 goto unlock;
307 }
308 if (audio_cmd_status.status != DSP_OK) {
309 ret = -1;
310 report_dsp_protocol("DSP returned a diferent Status of DSP_OK",
311 dsp_protocol);
312 report_return_value("DSP returned", audio_cmd_status.status);
313 report_speech_params("Speech params sent",
314 (*speech_params_data));
315 goto unlock;
316 }
317 report_speech_params("Speech params sent", (*speech_params_data));
318 ret = 0;
319 unlock:
320 dsp_protocol_unlock_dev(dsp_protocol);
321 out:
322 DLEAVE(ret);
323 return ret;
324 }
325
326 /* Execution phase features definitions */
327 /**
328 * @param dsp_protocol dsp_protocol_t structure.
329 *
330 * It starts a playback section sending a DSP_CMD_PLAY. It flushes
331 * all pending message comming from DSP side after DSP_CMD_PLAY.
332 *
333 * @return zero if success, otherwise a negative error code
334 * (-EIO - sending play from a non-valid state).
335 */
336 int dsp_protocol_send_play(dsp_protocol_t * dsp_protocol)
337 {
338 int ret;
339
340 DENTER();
341 if (dsp_protocol->state == STATE_UNINITIALISED) {
342 report_dsp_protocol
343 ("Trying to send play from a non-valid state",
344 dsp_protocol);
345 ret = -EIO;
346 goto out;
347 }
348 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
349 goto out;
350 if (dsp_protocol->state == STATE_PLAYING)
351 ret = 0;
352 else {
353 if ((ret =
354 dsp_protocol_send_command(dsp_protocol,
355 DSP_CMD_PLAY)) == 0)
356 dsp_protocol->state = STATE_PLAYING;
357 dsp_protocol_flush(dsp_protocol);
358 //Both, read and write remain data on the mbx system
359 }
360 dsp_protocol_unlock_dev(dsp_protocol);
361 out:
362 DLEAVE(ret);
363 return ret;
364 }
365
366 /**
367 * @param dsp_protocol dsp_protocol_t structure.
368 * @param data audio data buffer.
369 * @param count amount of data to be copied (in TUint16).
370 *
371 * It copies audio data to the mmap area in the right moment.
372 *
373 * @return if success, it returns the amount of data was sent.
374 * If called in a wrong moment, it returns 0. Otherwise, a negative error code.
375 */
376 int dsp_protocol_send_audio_data(dsp_protocol_t * dsp_protocol,
377 void *data,
378 unsigned short int count /* TUint16 */ )
379 {
380 write_status_t write_status;
381 data_write_t data_write;
382 int ret = 0;
383 DENTER();
384 DPRINT("count %d\n", count);
385 if (dsp_protocol->state != STATE_PLAYING) {
386 report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
387 goto out;
388 }
389 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
390 goto out;
391
392 memcpy(dsp_protocol->mmap_buffer, data, count * 2);
393 data_write.dsp_cmd = DSP_CMD_DATA_WRITE;
394 data_write.data_size = count;
395 if ((ret = write(dsp_protocol->fd, &data_write,
396 sizeof(data_write_t))) < 0)
397 goto unlock;
398
399 if ((ret = read(dsp_protocol->fd, &write_status,
400 sizeof(write_status_t))) < 0)
401 goto unlock;
402 if (write_status.dsp_cmd == DSP_CMD_DATA_WRITE) {
403
404 if (write_status.status == DSP_OK) {
405 ret = count;
406 DPRINT("%d words sent\n", ret);
407 } else {
408 DPRINT("Received a response different of DSP_OK\n");
409 report_return_value("Returned value:",
410 write_status.status);
411 report_dsp_protocol("Current dsp_protocol",
412 dsp_protocol);
413 ret = 0;
414 }
415 } else {
416 report_dsp_protocol("Could not send audio data", dsp_protocol);
417 report_command("Returned cmd", write_status.dsp_cmd);
418 ret = 0;
419 }
420
421 unlock:
422 dsp_protocol_unlock_dev(dsp_protocol);
423 out:
424 DLEAVE(ret);
425 return ret;
426
427 }
428
429 /**
430 * @param dsp_protocol dsp_protocol_t structure.
431 * @param data audio data buffer.
432 * @param count amount of data to be copied (in TUint16).
433 *
434 * It copies the audio data from the mmap area in the right moment.
435 *
436 * @return if success, it returns the amount of data was received.
437 * If called in a wrong moment, it returns 0. Otherwise, a negative error code.
438 */
439 int dsp_protocol_receive_audio_data(dsp_protocol_t * dsp_protocol,
440 void *data, int count /* TU16int */ )
441 {
442 read_status_t read_status;
443 dsp_cmd_status_t audio_cmd_status;
444
445 int ret = 0;
446 DENTER();
447 DPRINT("count %d\n", count);
448 if (dsp_protocol->state != STATE_PLAYING) {
449 report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
450 goto out;
451 }
452 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
453 goto out;
454 memcpy(data, dsp_protocol->mmap_buffer, count * 2);
455 audio_cmd_status.dsp_cmd = DSP_CMD_DATA_READ;
456 audio_cmd_status.status = DSP_OK;
457 if ((ret = write(dsp_protocol->fd, &audio_cmd_status,
458 sizeof(dsp_cmd_status_t))) < 0)
459 goto unlock;
460 if ((ret = read(dsp_protocol->fd, &read_status,
461 sizeof(read_status_t))) < 0)
462 goto unlock;
463 if (read_status.dsp_cmd == DSP_CMD_DATA_READ) {
464 if (read_status.status == DSP_OK) {
465 DPRINT("---------> DSP: ### %d ###\n",
466 read_status.frame_size);
467 ret = count;
468 DPRINT("%d words sent\n", ret);
469 } else {
470 report_dsp_protocol("Receive a status "
471 "different from DSP_OK (skiping block)",
472 dsp_protocol);
473 report_return_value("Returned: ", read_status.status);
474 ret = 0;
475 }
476
477 } else {
478 report_dsp_protocol("Could not receive audio data",
479 dsp_protocol);
480 DPRINT("Returned cmd %d expected %d\n", read_status.dsp_cmd,
481 DSP_CMD_DATA_READ);
482 report_return_value("Returned: ", read_status.status);
483 ret = 0;
484 }
485 unlock:
486 dsp_protocol_unlock_dev(dsp_protocol);
487 out:
488 DLEAVE(ret);
489 return ret;
490 }
491
492 /**
493 * @param dsp_protocol dsp_protocol_t structure.
494 *
495 * It pauses a playback section sending a DSP_CMD_PAUSE.
496 *
497 * @return zero if success, otherwise a negative error code
498 * (-EIO - sending play from a non-valid state).
499 */
500 int dsp_protocol_send_pause(dsp_protocol_t * dsp_protocol)
501 {
502 int ret;
503 DENTER();
504 if (dsp_protocol->state != STATE_PLAYING) {
505 report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
506 ret = -EIO;
507 goto out;
508 }
509 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
510 goto out;
511 if (dsp_protocol->state == STATE_PAUSED)
512 ret = 0;
513 else {
514 if ((ret =
515 dsp_protocol_send_command(dsp_protocol,
516 DSP_CMD_PAUSE)) == 0)
517 dsp_protocol->state = STATE_PAUSED;
518 }
519 dsp_protocol_unlock_dev(dsp_protocol);
520 out:
521 DLEAVE(ret);
522 return ret;
523 }
524
525 /**
526 * @param dsp_protocol dsp_protocol_t structure.
527 *
528 * It stops a playback section sending a DSP_CMD_STOP.
529 *
530 * @return zero if success, otherwise a negative error code
531 * (-EIO - sending play from a non-valid state).
532 */
533 int dsp_protocol_send_stop(dsp_protocol_t * dsp_protocol)
534 {
535 int ret;
536 DENTER();
537 if (dsp_protocol->state != STATE_PLAYING) {
538 report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
539 ret = -EIO;
540 goto out;
541 }
542 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
543 goto out;
544 if (dsp_protocol->state == STATE_STOPPED)
545 ret = 0;
546 else {
547 if ((ret =
548 dsp_protocol_send_command(dsp_protocol,
549 DSP_CMD_STOP)) == 0)
550 dsp_protocol->state = STATE_STOPPED;
551 }
552 dsp_protocol_unlock_dev(dsp_protocol);
553 out:
554 DLEAVE(ret);
555 return ret;
556 }
557
558 /* Deletion phase features definitions */
559 /**
560 * @param dsp_protocol DSP protocol reference pointer to be uninitialized.
561 *
562 * It closes the connection with pcm dsp task node. It flushes all
563 * pending data before that. Closes the mmap area. Initialize the
564 * dsp_protocol structure with unused values.
565 *
566 * @return zero if success, otherwise a negative error code.
567 */
568 int dsp_protocol_close_node(dsp_protocol_t * dsp_protocol)
569 {
570 int ret;
571 DENTER();
572 if (dsp_protocol->state != STATE_UNINITIALISED) {
573 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
574 goto out;
575 if ((ret = dsp_protocol_flush(dsp_protocol)) < 0)
576 goto unlock;
577 if ((ret = dsp_protocol_send_command(dsp_protocol,
578 DSP_CMD_CLOSE)) < 0)
579 goto unlock;
580 }
581
582 if (dsp_protocol->mmap_buffer)
583 munmap(dsp_protocol->mmap_buffer,
584 dsp_protocol->mmap_buffer_size);
585 close(dsp_protocol->fd);
586 dsp_protocol->fd = -1;
587 free(dsp_protocol->device);
588 dsp_protocol->device = NULL;
589 dsp_protocol->state = STATE_UNINITIALISED;
590 dsp_protocol->mute = 0;
591 dsp_protocol->stream_id = 0;
592 dsp_protocol->bridge_buffer_size = 0;
593 dsp_protocol->mmap_buffer_size = 0;
594 dsp_protocol->mmap_buffer = NULL;
595 ret = 0;
596 unlock:
597 dsp_protocol_unlock_dev(dsp_protocol);
598 out:
599 DLEAVE(ret);
600 return ret;
601 }
602
603 /**
604 * @param dsp_protocol DSP protocol reference pointer.
605 *
606 * It frees all the allocated memory. It sets NULL to the pointer.
607 *
608 * @return zero if success, otherwise a negative error code.
609 */
610 int dsp_protocol_destroy(dsp_protocol_t ** dsp_protocol)
611 {
612 int ret = 0;
613 DENTER();
614 #ifdef USE_RESOURCE_MANAGER
615 if ((*dsp_protocol)->dbus_connection)
616 dbus_connection_unref((*dsp_protocol)->dbus_connection);
617 #endif /* USE_RESOURCE_MANAGER */
618 if (*dsp_protocol) {
619 if ((*dsp_protocol)->device)
620 free((*dsp_protocol)->device);
621 free((*dsp_protocol));
622 *dsp_protocol = NULL;
623 }
624 DLEAVE(ret);
625 return ret;
626 }
627
628 /* controls features definitions */
629 /**
630 * dsp_protocol_set_volume:
631 *
632 * @param dsp_protocol dsp_protocol_t structure.
633 * @param left left channel volume value (0 - 100).
634 * @param right right channel volume value (0 - 100).
635 *
636 * It changes volume data for both left and right channels.
637 * It receives both values in a 0 - 100 scale.
638 *
639 * @return zero if success, otherwise a negative error code.
640 */
641 int dsp_protocol_set_volume(dsp_protocol_t * dsp_protocol,
642 unsigned char left, unsigned char right)
643 {
644 int ret;
645 dsp_cmd_status_t audio_cmd_status;
646 volume_data_t volume_data;
647 panning_data_t panning_data;
648
649 DENTER();
650 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
651 goto out;
652 dsp_protocol_linear2Q15(left > right ? left : right, &volume_data.scale,
653 &volume_data.power2);
654 volume_data.dsp_cmd = DSP_CMD_SET_VOLUME;
655 if ((ret =
656 write(dsp_protocol->fd, &volume_data, sizeof(volume_data_t))) < 0)
657 goto unlock;
658 if (read(dsp_protocol->fd, &audio_cmd_status,
659 sizeof(dsp_cmd_status_t)) < 0) {
660 ret = -EINVAL;
661 report_dsp_protocol("Could not receive DSP_CMD_STATUS",
662 dsp_protocol);
663 goto unlock;
664 }
665 report_return_value("Received", audio_cmd_status.status);
666 ret = 0;
667 if (audio_cmd_status.status != DSP_OK)
668 ret = -EIO;
669 if (ret == 0) { /*if sucess till here, update panning info */
670 panning_data.dsp_cmd = DSP_CMD_SET_PANNING;
671 panning_data.steps = PANNING_STEP;
672 if (left != right) {
673 panning_data.left_gain =
674 left >
675 right ? 0x4000 : (1.0 * left) / right * 0x4000;
676 panning_data.right_gain =
677 right >
678 left ? 0x4000 : (1.0 * right) / left * 0x4000;
679 } else {
680 panning_data.left_gain = 0x4000;
681 panning_data.right_gain = 0x4000;
682 }
683 DPRINT("left gain %x right gain %x\n", panning_data.left_gain,
684 panning_data.right_gain);
685 if ((ret =
686 write(dsp_protocol->fd, &panning_data,
687 sizeof(panning_data_t))) < 0)
688 goto unlock;
689 if (read(dsp_protocol->fd, &audio_cmd_status,
690 sizeof(dsp_cmd_status_t)) < 0) {
691 ret = -EINVAL;
692 report_dsp_protocol("Could not receive DSP_CMD_STATUS",
693 dsp_protocol);
694 goto unlock;
695 }
696 ret = 0;
697 if (audio_cmd_status.status != DSP_OK)
698 ret = -EIO;
699
700 }
701 unlock:
702 dsp_protocol_unlock_dev(dsp_protocol);
703 out:
704 DLEAVE(ret);
705 return ret;
706 }
707
708 /**
709 * @param dsp_protocol dsp_protocol_t structure.
710 * @param left left channel volume value output (0 - 100).
711 * @param right right channel volume value output (0 - 100).
712 *
713 * It returns volume data for both left and right channels.
714 * It provides both values in a 0 - 100 scale.
715 *
716 * @return zero if success, otherwise a negative error code.
717 */
718 int dsp_protocol_get_volume(dsp_protocol_t * dsp_protocol,
719 unsigned char *left, unsigned char *right)
720 {
721 unsigned short int tmp;
722 int ret;
723 audio_status_info_t audio_status_info;
724 DENTER();
725 tmp = DSP_CMD_STATE;
726 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
727 goto out;
728
729 if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
730 ret = -EIO;
731 goto unlock;
732 }
733 if ((ret = read(dsp_protocol->fd, &audio_status_info,
734 sizeof(audio_status_info_t))) < 0) {
735 report_dsp_protocol("Could not read audio_status_info",
736 dsp_protocol);
737 goto unlock;
738 }
739 dsp_protocol->state = audio_status_info.status;
740 report_audio_status_info("Received:", audio_status_info);
741 dsp_protocol_Q152linear(audio_status_info.vol_scale,
742 audio_status_info.vol_power2, &tmp);
743 *left = tmp;
744 *right = tmp;
745 if (audio_status_info.number_channels == CHANNELS_2) {
746 if (audio_status_info.left_gain > audio_status_info.right_gain){
747 float result =
748 *right * audio_status_info.right_gain / (0x4000 *
749 1.0);
750 *right = result;
751 if ((result - *right) > 0.5)
752 (*right)++;
753 }
754 if (audio_status_info.left_gain < audio_status_info.right_gain){
755 float result =
756 *left * audio_status_info.left_gain / (0x4000 *
757 1.0);
758 *left = result;
759 if ((result - *left) > 0.5)
760 (*left)++;
761 }
762 }
763 ret = 0;
764 unlock:
765 dsp_protocol_unlock_dev(dsp_protocol);
766 out:
767 DLEAVE(ret);
768 return ret;
769 }
770
771 /**
772 * @param dsp_protocol dsp_protocol_t structure.
773 * @param mute mute value (0 unmuted - 1 muted).
774 *
775 * It changes mute state of DSP task node sending DSP_CMD_[UN]MUTE.
776 * Provide 0 in mute to unmute, and 1 in mute to mute.
777 *
778 * @return zero if success, otherwise a negative error code.
779 */
780 int dsp_protocol_set_mute(dsp_protocol_t * dsp_protocol, unsigned char mute)
781 {
782 int ret = 0;
783 int command;
784 DENTER();
785 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
786 goto out;
787 if (mute == 1)
788 command = DSP_CMD_MUTE;
789 else
790 command = DSP_CMD_UNMUTE;
791 ret = dsp_protocol_send_command(dsp_protocol, command);
792 dsp_protocol->mute = mute;
793 dsp_protocol_unlock_dev(dsp_protocol);
794 out:
795 DLEAVE(ret);
796 return ret;
797 }
798
799 /**
800 * @param dsp_protocol dsp_protocol_t structure.
801 *
802 * It returns mute state of DSP task node.
803 *
804 * @return zero if success, it returns current state of mute.
805 * 0 means not muted. 1 means muted. otherwise a negative error code.
806 */
807 int dsp_protocol_get_mute(dsp_protocol_t * dsp_protocol)
808 {
809 int ret = 0;
810 DENTER();
811 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
812 goto out;
813 ret = dsp_protocol_update_state(dsp_protocol);
814 if (ret >= 0)
815 ret = dsp_protocol->mute;
816 dsp_protocol_unlock_dev(dsp_protocol);
817 out:
818 DLEAVE(ret);
819 return ret;
820 }
821
822 /*miscelaneos features definitions */
823 /**
824 * @param dsp_protocol dsp_protocol_t structure.
825 * @param enable enabled state of mic. 0 disable. 1 enabled.
826 *
827 * It modifies enabled state of mic.
828 *
829 * @return zero if success, otherwise a negative error code.
830 */
831 #ifdef USE_RESOURCE_MANAGER
832 int dsp_protocol_set_mic_enabled(dsp_protocol_t * dsp_protocol, int enabled)
833 {
834 int ret = 0;
835 DBusMessage *message;
836 DBusMessage *reply = NULL;
837 DENTER();
838 if (dsp_protocol->dbus_connection &&
839 (message = dbus_message_new_method_call(AUDIO_PM_SERVICE,
840 AUDIO_PM_RECORD_RESOURCE, RESOURCE_INTERFACE,
841 enabled ? "request" : "release"))) {
842 if (!enabled) {
843 dbus_int32_t handle = 0;
844 /* assuming handle is always 0 for this resource */
845 dbus_message_append_args(message,
846 DBUS_TYPE_INT32, &handle,
847 DBUS_TYPE_INVALID);
848 }
849 reply = dbus_connection_send_with_reply_and_block(
850 dsp_protocol->dbus_connection,
851 message, RESOURCE_TIMEOUT, NULL);
852 /* TODO: check return value(s), append args for request()
853 * These parts of the RM spec may still change so not done yet
854 */
855 dbus_message_unref(message);
856 }
857 if (reply) {
858 dbus_message_unref(reply);
859 } else {
860 DPRINT("Error acquiring PM resource for recording\n");
861 #ifdef ERROR_ON_PM_FAILURE
862 ret = -EIO;
863 #endif
864 }
865 DLEAVE(ret);
866 return ret;
867 }
868
869 #else /* !USE_RESOURCE_MANAGER */
870 int dsp_protocol_set_mic_enabled(dsp_protocol_t * dsp_protocol, int enabled)
871 {
872 int mic_fd;
873 int ret;
874 char mic_control_name[] = "/sys/devices/platform/audio-i2c/mic_enable";
875 DENTER();
876 if ((mic_fd = open(mic_control_name, O_WRONLY)) < 0) {
877 DPRINT("Error opening: %s\n", mic_control_name);
878 #ifdef ERROR_ON_PM_FAILURE
879 ret = -EIO;
880 #else
881 ret = 0;
882 #endif
883 goto out;
884 }
885
886 write(mic_fd, (enabled ? "1" : "0"), sizeof(char));
887 close(mic_fd);
888 ret = 0;
889 out:
890 DLEAVE(ret);
891 return ret;
892 }
893 #endif /* USE_RESOURCE_MANAGER */
894
895 /**
896 * @param dsp_protocol dsp_protocol_t structure.
897 * @param device dsp task node file name.
898 *
899 * It opens a dsp task node and queries for its states.
900 *
901 * @return if success, it returns number of channels whose dsp task is
902 * currently using (1 - mono. 2 stero). otherwise a negative error code.
903 */
904 int dsp_protocol_probe_node(dsp_protocol_t * dsp_protocol, const char *device)
905 {
906 int ret;
907 DENTER();
908 if (dsp_protocol->state != STATE_UNINITIALISED) {
909 report_dsp_protocol
910 ("Trying to send open node from a non-valid state",
911 dsp_protocol);
912 ret = -EIO;
913 goto out;
914 }
915
916 dsp_protocol->fd = open(device, O_RDWR);
917 if (dsp_protocol->fd < 0) {
918 DERROR("Could not open pcm device file %s\n", device);
919 ret = errno;
920 goto out;
921 }
922 dsp_protocol->device = strdup(device);
923 dsp_protocol_get_sem(dsp_protocol);
924
925 if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
926 goto out;
927 dsp_protocol->device = strdup(device);
928 ret = dsp_protocol_update_state(dsp_protocol);
929 if (ret != CHANNELS_1 && ret != CHANNELS_2)
930 ret = CHANNELS_1;
931 dsp_protocol_unlock_dev(dsp_protocol);
932 out:
933 DLEAVE(ret);
934 return ret;
935
936 }
937
938 /**
939 * @param str string with a number.
940 * @param val holder for the conversion result.
941 *
942 * It converts a string to a number (long).
943 *
944 * @return zero if success, otherwise a negative error code.
945 */
946 int safe_strtol(const char *str, long *val)
947 {
948 char *end;
949 long v;
950 int ret;
951 DENTER();
952 if (!*str) {
953 ret = -EINVAL;
954 goto out;
955 }
956 errno = 0;
957 v = strtol(str, &end, 0);
958 if (errno) {
959 ret = -errno;
960 goto out;
961 }
962 if (*end) {
963 ret = -EINVAL;
964 goto out;
965 }
966 *val = v;
967 ret = 0;
968 out:
969 DLEAVE(ret);
970 return ret;
971 }
972 /* internal features definitions */
973 /**
974 * @param dsp_protocol dsp_protocol_t structure.
975 *
976 * Tries to read all the pending data.
977 *
978 * @return zero. success
979 */
980 static int dsp_protocol_flush(dsp_protocol_t * dsp_protocol)
981 {
982 struct pollfd pollf;
983 int ret = 0;
984 int tmp;
985 DENTER();
986 pollf.fd = dsp_protocol->fd;
987 pollf.events = POLLIN;
988 while (poll(&pollf, 1, 0) > 0) {
989 if (read(dsp_protocol->fd, &tmp, sizeof(short int)) == 0)
990 /* Test end of file */
991 break;
992 #ifdef DEBUG
993 fprintf(DEBUG_OUTPUT, ".");
994 #endif
995 }
996 #ifdef DEBUG
997 fprintf(DEBUG_OUTPUT, "\n");
998 #endif
999 DLEAVE(ret);
1000 return ret;
1001 }
1002
1003 /**
1004 * @param dsp_protocol dsp_protocol_t structure.
1005 * @param command command value to be sent to the pcm task node.
1006 *
1007 * Send the command to pcm task node and checks if
1008 * it was sent properly.
1009 *
1010 * @return zero if success, otherwise a negative error code.
1011 */
1012 static int dsp_protocol_send_command(dsp_protocol_t * dsp_protocol,
1013 const short int command)
1014 {
1015 dsp_cmd_status_t audio_cmd_status;
1016 int ret = 0;
1017 short int tmp;
1018 DENTER();
1019 report_command("Sending", command);
1020 tmp = command;
1021 if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
1022 report_dsp_protocol("Could not send", dsp_protocol);
1023 ret = -EIO;
1024 }
1025
1026 if (read(dsp_protocol->fd, &audio_cmd_status,
1027 sizeof(dsp_cmd_status_t)) < 0) {
1028 ret = -1;
1029 report_dsp_protocol("Could not receive DSP_CMD_STATUS",
1030 dsp_protocol);
1031 goto out;
1032 }
1033 /* report_command("Received", audio_cmd_status.dsp_cmd);
1034 report_return_value("Received", audio_cmd_status.status);*/
1035 DPRINT("audio_cmd_status.dsp_cmd: 0x%x\n", audio_cmd_status.dsp_cmd);
1036 DPRINT("audio_cmd_status.status: 0x%x\n", audio_cmd_status.status);
1037 if (audio_cmd_status.status != DSP_OK)
1038 ret = -EIO;
1039 out:
1040 DLEAVE(ret);
1041 return ret;
1042 }
1043
1044 /**
1045 * @param input 0 - 100 value to be converted to Q15.
1046 * @param scale scale value of Q15 format.
1047 * @param power2 power of 2 value of Q15 format.
1048 *
1049 * Converts a 0 - 100 value to a Q15 format value.
1050 *
1051 */
1052 static void dsp_protocol_linear2Q15(const unsigned short int input,
1053 unsigned short int *scale,
1054 unsigned short int *power2)
1055 {
1056 unsigned long int val = MAGIC_NUMBER * input;
1057 DENTER();
1058 if (input == 0) {
1059 *scale = 0;
1060 *power2 = 0;
1061 } else {
1062 *power2 = 1;
1063 while (val < 0x40000000) {
1064 (*power2)--;
1065 val <<= 1;
1066 }
1067 *scale = val >> 16;
1068 }
1069 DPRINT("Resulted scale %d and power2 %d from input %d\n", *scale,
1070 *power2, input);
1071 DLEAVE(0);
1072 }
1073
1074 /**
1075 * @param scale scale value of Q15 format.
1076 * @param power2 power of 2 value of Q15 format.
1077 * @param output 0 - 100 resulted value.
1078 *
1079 * Converts a Q15 format value to a 0 - 100 value.
1080 *
1081 */
1082 static void dsp_protocol_Q152linear(const unsigned short int scale,
1083 const unsigned short int power2, unsigned short int *output)
1084 {
1085 float result = scale * 1.0 / 0x8000 * (1 << power2) * 100.0;
1086 DENTER();
1087 *output = (short int)(result);
1088 if ((result - *output) > 0.5)
1089 (*output)++;
1090 DPRINT("Resulted linear: %d from scale %d and power2 %d\n", *output,
1091 scale, power2);
1092 DLEAVE(0);
1093 }
1094
1095 /**
1096 * @param dsp_protocol dsp_protocol_t structure.
1097 *
1098 * Update dsp_protocol_t structure info. Queries dsp task
1099 * for a audio_status_info structure.
1100 *
1101 * @return if success, it returns number of channels whose dsp task is
1102 * currently using. otherwise a negative error code.
1103 */
1104 static int dsp_protocol_update_state(dsp_protocol_t * dsp_protocol)
1105 {
1106 int ret;
1107 short int tmp;
1108 audio_status_info_t audio_status_info;
1109 DENTER();
1110
1111 if ((ret = dsp_protocol_flush(dsp_protocol)) < 0)
1112 goto out;
1113 tmp = DSP_CMD_STATE;
1114 if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
1115 ret = -EIO;
1116 goto out;
1117 }
1118 if ((ret = read(dsp_protocol->fd, &audio_status_info,
1119 sizeof(audio_status_info_t))) < 0) {
1120 report_dsp_protocol("Could not read audio_status_info",
1121 dsp_protocol);
1122 goto out;
1123 }
1124 report_audio_status_info("Received:", audio_status_info);
1125 dsp_protocol->stream_id = audio_status_info.stream_id;
1126 dsp_protocol->bridge_buffer_size = audio_status_info.bridge_buffer_size;
1127 dsp_protocol->mmap_buffer_size = audio_status_info.mmap_buffer_size;
1128 dsp_protocol->state = audio_status_info.status;
1129 #ifndef NORMAL_DSP_TASK
1130 dsp_protocol->mute = audio_status_info.mute;
1131 #endif
1132 report_dsp_protocol("connection stablished:", dsp_protocol);
1133 ret = audio_status_info.number_channels;
1134 out:
1135 DLEAVE(ret);
1136 return ret;
1137 }
1138
1139 /**
1140 * @param dsp_protocol dsp_protocol_t structure.
1141 *
1142 * It produces the semaphore ID and connects to it.
1143 *
1144 * @return zero if success, otherwise a negative error code.
1145 */
1146 static inline int dsp_protocol_get_sem(dsp_protocol_t * dsp_protocol)
1147 {
1148 union semun sem_val; /* semaphore value, for semctl(). */
1149
1150 int rc;
1151 int ret;
1152 /* identifier returned by ftok() */
1153 key_t set_key;
1154 DENTER();
1155 /* generate a "unique" key for our set, using the */
1156 /* directory "/dev/dsptask/xxxx". */
1157 set_key = ftok(dsp_protocol->device, 0);
1158 if (set_key == -1) {
1159 DPRINT("ftok: %d\n", errno);
1160 ret = -ENODEV;
1161 goto out;
1162 }
1163 DPRINT("key %d\n", set_key);
1164
1165 /* now we can use 'set_key' to generate a set id, for example. */
1166 dsp_protocol->sem_set_id = semget(set_key, 1, 0666);
1167 if (dsp_protocol->sem_set_id == -1) {
1168 DPRINT("semget %d\n", errno);
1169 dsp_protocol->sem_set_id = semget(set_key, 1, IPC_CREAT | 0666);
1170 if (dsp_protocol->sem_set_id == -1) {
1171 DPRINT("semget: IPC_CREAT: %d\n", errno);
1172 ret = -ENODEV;
1173 goto out;
1174 }
1175 DPRINT("Initialising the semaphore\n");
1176 sem_val.val = 1;
1177 rc = semctl(dsp_protocol->sem_set_id, 0, SETVAL, sem_val);
1178 if (rc == -1) {
1179 DPRINT("semctl %d\n", errno);
1180 ret = -ENODEV;
1181 goto out;
1182 }
1183 }
1184 ret = 0;
1185 out:
1186 DLEAVE(ret);
1187 return ret;
1188 }
1189
1190 /**
1191 * @param dsp_protocol dsp_protocol_t structure.
1192 *
1193 * It waits until be able to hold the semaphore to access the dsp task node.
1194 * If the current process already has the semaphore, returns success.
1195 *
1196 * @return zero if success, otherwise a negative error code.
1197 */
1198 static inline int dsp_protocol_lock_dev(dsp_protocol_t * dsp_protocol)
1199 {
1200 int ret;
1201 struct sembuf sem_op;
1202 DENTER();
1203 ret = pthread_mutex_trylock(&(dsp_protocol->mutex));
1204 if (ret != 0) {
1205 DPRINT("No lock %d\n", ret);
1206 if (errno == EBUSY)
1207 ret = 0;
1208 goto out;
1209 }
1210 sem_op.sem_num = 0;
1211 sem_op.sem_op = -1;
1212 sem_op.sem_flg = 0;
1213 DPRINT("requesting semaphore (dev)\n");
1214 if (semop(dsp_protocol->sem_set_id, &sem_op, 1) == -1) {
1215 DPRINT("semop %d\n", errno);
1216 pthread_mutex_unlock(&(dsp_protocol->mutex));
1217 ret = -errno;
1218 }
1219 out:
1220 DLEAVE(ret);
1221 return ret;
1222 }
1223
1224 /**
1225 * @param dsp_protocol dsp_protocol_t structure.
1226 *
1227 * It releases the semaphore.
1228 *
1229 * @return zero if success, otherwise a negative error code.
1230 */
1231 static inline int dsp_protocol_unlock_dev(dsp_protocol_t * dsp_protocol)
1232 {
1233 struct sembuf sem_op;
1234 DENTER();
1235 sem_op.sem_num = 0;
1236 sem_op.sem_op = 1;
1237 sem_op.sem_flg = 0;
1238 DPRINT("Releasing\n");
1239 semop(dsp_protocol->sem_set_id, &sem_op, 1);
1240
1241 pthread_mutex_unlock(&(dsp_protocol->mutex));
1242 DLEAVE(0);
1243 return 0;
1244 }
1245
1246