Blame maemo/dsp-protocol.c

Packit 675970
/**
Packit 675970
 * @file dsp-protocol.c
Packit 675970
 * @brief Protocol to communicate with DSP side.
Packit 675970
 * 

Packit 675970
 * Copyright (C) 2006 Nokia Corporation
Packit 675970
 * 

Packit 675970
 * Contact: Eduardo Bezerra Valentin <eduardo.valentin@indt.org.br>
Packit 675970
 * 
Packit 675970
 * This library is free software; you can redistribute it and/or
Packit 675970
 * modify it under the terms of the GNU Library General Public
Packit 675970
 * License as published by the Free Software Foundation; either
Packit 675970
 * version 2 of the License, or (at your option) any later version.
Packit 675970
 *
Packit 675970
 * This library is distributed in the hope that it will be useful,
Packit 675970
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 675970
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 675970
 * Library General Public License for more details.
Packit 675970
 *
Packit 675970
 * You should have received a copy of the GNU Library General Public License
Packit 675970
 * along with this program; if not, write to the Free Software
Packit 675970
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit 675970
 * */
Packit 675970
#include <stdio.h>
Packit 675970
#include <string.h>
Packit 675970
#include <stdlib.h>
Packit 675970
#include <poll.h>
Packit 675970
#include <sys/errno.h>
Packit 675970
#include <sys/types.h>
Packit 675970
#include <sys/uio.h>
Packit 675970
#include <unistd.h>
Packit 675970
#include <fcntl.h>
Packit 675970
#include <sys/mman.h>
Packit 675970
#include <math.h>
Packit 675970
#include <sys/ioctl.h>
Packit 675970
#include <sys/sem.h>
Packit 675970
Packit 675970
#include "dsp-protocol.h"
Packit 675970
#include "constants.h"
Packit 675970
#include "debug.h"
Packit 675970
#include "types.h"
Packit 675970
#include "reporting.h"
Packit 675970
Packit 675970
#ifdef HAVE_CONFIG_H
Packit 675970
#include <config.h>
Packit 675970
#endif
Packit 675970
Packit 675970
#ifdef USE_RESOURCE_MANAGER
Packit 675970
#define DBUS_API_SUBJECT_TO_CHANGE
Packit 675970
#include <dbus/dbus.h>
Packit 675970
#define AUDIO_PM_SERVICE            "com.nokia.osso_audio_pm"
Packit 675970
#define AUDIO_PM_PLAYBACK_RESOURCE  "/com/nokia/osso/pm/audio/playback"
Packit 675970
#define AUDIO_PM_RECORD_RESOURCE    "/com/nokia/osso/pm/audio/record"
Packit 675970
#define RESOURCE_INTERFACE          "com.nokia.osso_resource_manager"
Packit 675970
#define RESOURCE_TIMEOUT            200
Packit 675970
#endif /* USE_RESOURCE_MANAGER */
Packit 675970
Packit 675970
#define MAGIC_NUMBER		0x00A3D70A
Packit 675970
#define PANNING_STEP		0x06
Packit 675970
#define TASK_IOCTL_LOCK         0x10002
Packit 675970
#define TASK_IOCTL_UNLOCK       0x10003
Packit 675970
/* internal datatypes declarations */
Packit 675970
union semun {
Packit 675970
	int val;		/* value for SETVAL */
Packit 675970
	struct semid_ds *buf;	/* buffer for IPC_STAT & IPC_SET */
Packit 675970
	u_short *array;		/* array for GETALL & SETALL */
Packit 675970
};
Packit 675970
/* internal features declarations */
Packit 675970
static int dsp_protocol_flush(dsp_protocol_t * dsp_protocol);
Packit 675970
static int dsp_protocol_send_command(dsp_protocol_t * dsp_protocol,
Packit 675970
				     const short int command);
Packit 675970
static void dsp_protocol_linear2Q15(const unsigned short int input,
Packit 675970
				    unsigned short int *scale, 
Packit 675970
				    unsigned short int *power2);
Packit 675970
static void dsp_protocol_Q152linear(const unsigned short int scale,
Packit 675970
				    const unsigned short int power2, 
Packit 675970
				    unsigned short int *output);
Packit 675970
static int dsp_protocol_update_state(dsp_protocol_t * dsp_protocol);
Packit 675970
static inline int dsp_protocol_get_sem(dsp_protocol_t * dsp_protocol);
Packit 675970
static inline int dsp_protocol_lock_dev(dsp_protocol_t * dsp_protocol);
Packit 675970
static inline int dsp_protocol_unlock_dev(dsp_protocol_t * dsp_protocol);
Packit 675970
/* Initialisation phase features definitions */
Packit 675970
/**
Packit 675970
 * @param dsp_protocol DSP protocol reference pointer to be instanciated.
Packit 675970
 * 
Packit 675970
 * Creates new dsp_protocol object instance and initializes it
Packit 675970
 * with default parameters. After this the device must be
Packit 675970
 * separately opened with gst_dspaudio_open_node() method.
Packit 675970
 *
Packit 675970
 * @return zero if success, otherwise a negative error code
Packit 675970
 *          (-ENOMEM - fail when requesting memory for data structures).
Packit 675970
 */
Packit 675970
int dsp_protocol_create(dsp_protocol_t ** dsp_protocol)
Packit 675970
{
Packit 675970
	pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
Packit 675970
	int ret = 0;
Packit 675970
	DENTER();
Packit 675970
	*dsp_protocol = (dsp_protocol_t *) calloc(1, sizeof(dsp_protocol_t));
Packit 675970
	if ((*dsp_protocol) == NULL) {
Packit 675970
		DERROR("Could not allocate dsp_protocol instance\n");
Packit 675970
		ret = -ENOMEM;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	(*dsp_protocol)->fd = -1;
Packit 675970
	(*dsp_protocol)->device = NULL;
Packit 675970
	(*dsp_protocol)->state = STATE_UNINITIALISED;
Packit 675970
	(*dsp_protocol)->mute = 0;
Packit 675970
	(*dsp_protocol)->stream_id = 0;
Packit 675970
	(*dsp_protocol)->bridge_buffer_size = 0;
Packit 675970
	(*dsp_protocol)->mmap_buffer_size = 0;
Packit 675970
	(*dsp_protocol)->mmap_buffer = NULL;
Packit 675970
	(*dsp_protocol)->mutex = mutex;
Packit 675970
	(*dsp_protocol)->sem_set_id = -1;
Packit 675970
#ifdef USE_RESOURCE_MANAGER
Packit 675970
        (*dsp_protocol)->dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
Packit 675970
#endif
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol DSP protocol reference pointer to be initialized.
Packit 675970
 * @param device dsp device node name.
Packit 675970
 * 
Packit 675970
 * Opens pcm dsp device file and initializes dsp_protocol
Packit 675970
 * with information about stream, state and mmapbuffer.
Packit 675970
 *
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_open_node(dsp_protocol_t * dsp_protocol, const char *device)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	short int tmp;
Packit 675970
	audio_status_info_t audio_status_info;
Packit 675970
	audio_init_status_t audio_init_status;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_UNINITIALISED) {
Packit 675970
		report_dsp_protocol
Packit 675970
		    ("Trying to send open node from a non-valid state",
Packit 675970
		     dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
Packit 675970
	dsp_protocol->fd = open(device, O_RDWR);
Packit 675970
	if (dsp_protocol->fd < 0) {
Packit 675970
		DERROR("Could not open pcm device file %s\n", device);
Packit 675970
		ret = errno;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	dsp_protocol->device = strdup(device);
Packit 675970
	dsp_protocol_get_sem(dsp_protocol);
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	if ((ret = dsp_protocol_flush(dsp_protocol)) < 0)
Packit 675970
		goto unlock;
Packit 675970
	tmp = DSP_CMD_STATE;
Packit 675970
	if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
Packit 675970
		ret = -EIO;
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	if ((ret = read(dsp_protocol->fd, &audio_status_info,
Packit 675970
			sizeof(audio_status_info_t))) < 0) {
Packit 675970
		report_dsp_protocol("Could not read audio_status_info",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	report_audio_status_info("Received:", audio_status_info);
Packit 675970
	if (audio_status_info.status == STATE_UNINITIALISED) {
Packit 675970
		tmp = DSP_CMD_INIT;
Packit 675970
		if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
Packit 675970
			ret = -EIO;
Packit 675970
			goto unlock;
Packit 675970
		}
Packit 675970
		if ((ret = read(dsp_protocol->fd, &audio_init_status,
Packit 675970
				sizeof(audio_init_status_t))) < 0) {
Packit 675970
			report_dsp_protocol("Error reading INIT status",
Packit 675970
					    dsp_protocol);
Packit 675970
			goto unlock;
Packit 675970
		}
Packit 675970
		report_audio_init_status("Received:", audio_init_status);
Packit 675970
		/* receive info from audio_init_status */
Packit 675970
		dsp_protocol->stream_id = audio_init_status.stream_id;
Packit 675970
		dsp_protocol->bridge_buffer_size =
Packit 675970
		    audio_init_status.bridge_buffer_size;
Packit 675970
		dsp_protocol->mmap_buffer_size =
Packit 675970
		    audio_init_status.mmap_buffer_size;
Packit 675970
	} else {
Packit 675970
		/* This pcm task node is busy. Try to use another one. */
Packit 675970
		ret = -EBUSY;
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	dsp_protocol->mmap_buffer = (short int *)
Packit 675970
	    mmap((void *)0, dsp_protocol->mmap_buffer_size,
Packit 675970
		 PROT_READ | PROT_WRITE, MAP_SHARED, dsp_protocol->fd, 0);
Packit 675970
Packit 675970
	if (dsp_protocol->mmap_buffer == MAP_FAILED) {
Packit 675970
		report_dsp_protocol("Cannot mmap data buffer", dsp_protocol);
Packit 675970
		ret = -ENOMEM;
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	dsp_protocol->state = STATE_INITIALISED;
Packit 675970
	report_dsp_protocol("connection stablished:", dsp_protocol);
Packit 675970
Packit 675970
	ret = 0;
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol DSP protocol reference pointer.
Packit 675970
 * @param audio_params_data audio params to be sent to the dsp.
Packit 675970
 * 
Packit 675970
 * Send audio params to pcm task node and checks if
Packit 675970
 * it was sent properly.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_send_audio_params(dsp_protocol_t * dsp_protocol,
Packit 675970
				   audio_params_data_t * audio_params_data)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	dsp_cmd_status_t audio_cmd_status;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_INITIALISED) {
Packit 675970
		report_dsp_protocol
Packit 675970
		    ("Trying to send audio parameters from a non-valid state",
Packit 675970
		     dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	audio_params_data->ds_stream_id = dsp_protocol->stream_id;
Packit 675970
	if (write(dsp_protocol->fd, audio_params_data,
Packit 675970
		  sizeof(audio_params_data_t)) < 0) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("Could not send audio_params_data",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	if (read(dsp_protocol->fd, &audio_cmd_status, 
Packit 675970
		sizeof(dsp_cmd_status_t)) < 0) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("Could not receive DSP_CMD_STATUS",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	if (audio_cmd_status.status != DSP_OK) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("DSP returned a diferent Status of DSP_OK",
Packit 675970
				    dsp_protocol);
Packit 675970
		report_return_value("DSP returned", audio_cmd_status.status);
Packit 675970
		report_audio_params("Audio params sent", (*audio_params_data));
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	report_audio_params("Audio params sent", (*audio_params_data));
Packit 675970
	ret = 0;
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol DSP protocol reference pointer.
Packit 675970
 * @param speech_params_data audio params to be sent to the dsp.
Packit 675970
 * 
Packit 675970
 * Send audio params to pcm_rec task node and checks if
Packit 675970
 * it was sent properly.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_send_speech_params(dsp_protocol_t * dsp_protocol,
Packit 675970
				    speech_params_data_t * speech_params_data)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	dsp_cmd_status_t audio_cmd_status;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_INITIALISED) {
Packit 675970
		report_dsp_protocol
Packit 675970
		    ("Trying to send speech parameters from a non-valid state",
Packit 675970
		     dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	speech_params_data->ds_stream_id = dsp_protocol->stream_id;
Packit 675970
	if (write(dsp_protocol->fd, speech_params_data,
Packit 675970
		  sizeof(speech_params_data_t)) < 0) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("Could not send speech_params_data",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	if (read(dsp_protocol->fd, &audio_cmd_status, 
Packit 675970
		sizeof(dsp_cmd_status_t)) < 0) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("Could not receive DSP_CMD_STATUS",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	if (audio_cmd_status.status != DSP_OK) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("DSP returned a diferent Status of DSP_OK",
Packit 675970
				    dsp_protocol);
Packit 675970
		report_return_value("DSP returned", audio_cmd_status.status);
Packit 675970
		report_speech_params("Speech params sent",
Packit 675970
				     (*speech_params_data));
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	report_speech_params("Speech params sent", (*speech_params_data));
Packit 675970
	ret = 0;
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/* Execution phase features definitions */
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 *
Packit 675970
 * It starts a playback section sending a DSP_CMD_PLAY. It flushes
Packit 675970
 * all pending message comming from DSP side after DSP_CMD_PLAY.
Packit 675970
 *
Packit 675970
 * @return zero if success, otherwise a negative error code
Packit 675970
 *          (-EIO - sending play from a non-valid state).
Packit 675970
 */
Packit 675970
int dsp_protocol_send_play(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state == STATE_UNINITIALISED) {
Packit 675970
		report_dsp_protocol
Packit 675970
		    ("Trying to send play from a non-valid state",
Packit 675970
		     dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	if (dsp_protocol->state == STATE_PLAYING)
Packit 675970
		ret = 0;
Packit 675970
	else {
Packit 675970
		if ((ret =
Packit 675970
		     dsp_protocol_send_command(dsp_protocol,
Packit 675970
					       DSP_CMD_PLAY)) == 0)
Packit 675970
			dsp_protocol->state = STATE_PLAYING;
Packit 675970
		dsp_protocol_flush(dsp_protocol);	
Packit 675970
		//Both, read and write remain data on the mbx system
Packit 675970
	}
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param data audio data buffer.
Packit 675970
 * @param count amount of data to be copied (in TUint16).
Packit 675970
 *
Packit 675970
 * It copies audio data to the mmap area in the right moment.
Packit 675970
 *
Packit 675970
 * @return if success, it returns the amount of data was sent.
Packit 675970
 * If called in a wrong moment, it returns 0.  Otherwise, a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_send_audio_data(dsp_protocol_t * dsp_protocol,
Packit 675970
				 void *data,
Packit 675970
				 unsigned short int count /* TUint16 */ )
Packit 675970
{
Packit 675970
	write_status_t write_status;
Packit 675970
	data_write_t data_write;
Packit 675970
	int ret = 0;
Packit 675970
	DENTER();
Packit 675970
	DPRINT("count %d\n", count);
Packit 675970
	if (dsp_protocol->state != STATE_PLAYING) {
Packit 675970
		report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
Packit 675970
	memcpy(dsp_protocol->mmap_buffer, data, count * 2);
Packit 675970
	data_write.dsp_cmd = DSP_CMD_DATA_WRITE;
Packit 675970
	data_write.data_size = count;
Packit 675970
	if ((ret = write(dsp_protocol->fd, &data_write,
Packit 675970
			 sizeof(data_write_t))) < 0)
Packit 675970
		goto unlock;
Packit 675970
Packit 675970
	if ((ret = read(dsp_protocol->fd, &write_status,
Packit 675970
			sizeof(write_status_t))) < 0)
Packit 675970
		goto unlock;
Packit 675970
	if (write_status.dsp_cmd == DSP_CMD_DATA_WRITE) {
Packit 675970
Packit 675970
		if (write_status.status == DSP_OK) {
Packit 675970
			ret = count;
Packit 675970
			DPRINT("%d words sent\n", ret);
Packit 675970
		} else {
Packit 675970
			DPRINT("Received a response different of DSP_OK\n");
Packit 675970
			report_return_value("Returned value:",
Packit 675970
					    write_status.status);
Packit 675970
			report_dsp_protocol("Current dsp_protocol",
Packit 675970
					    dsp_protocol);
Packit 675970
			ret = 0;
Packit 675970
		}
Packit 675970
	} else {
Packit 675970
		report_dsp_protocol("Could not send audio data", dsp_protocol);
Packit 675970
		report_command("Returned cmd", write_status.dsp_cmd);
Packit 675970
		ret = 0;
Packit 675970
	}
Packit 675970
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param data audio data buffer.
Packit 675970
 * @param count amount of data to be copied (in TUint16).
Packit 675970
 *
Packit 675970
 * It copies the audio data from the mmap area in the right moment.
Packit 675970
 *
Packit 675970
 * @return if success, it returns the amount of data was received.
Packit 675970
 * If called in a wrong moment, it returns 0.  Otherwise, a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_receive_audio_data(dsp_protocol_t * dsp_protocol,
Packit 675970
				    void *data, int count /* TU16int */ )
Packit 675970
{
Packit 675970
	read_status_t read_status;
Packit 675970
	dsp_cmd_status_t audio_cmd_status;
Packit 675970
Packit 675970
	int ret = 0;
Packit 675970
	DENTER();
Packit 675970
	DPRINT("count %d\n", count);
Packit 675970
	if (dsp_protocol->state != STATE_PLAYING) {
Packit 675970
		report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	memcpy(data, dsp_protocol->mmap_buffer, count * 2);
Packit 675970
	audio_cmd_status.dsp_cmd = DSP_CMD_DATA_READ;
Packit 675970
	audio_cmd_status.status = DSP_OK;
Packit 675970
	if ((ret = write(dsp_protocol->fd, &audio_cmd_status,
Packit 675970
			 sizeof(dsp_cmd_status_t))) < 0)
Packit 675970
		goto unlock;
Packit 675970
	if ((ret = read(dsp_protocol->fd, &read_status,
Packit 675970
			sizeof(read_status_t))) < 0)
Packit 675970
		goto unlock;
Packit 675970
	if (read_status.dsp_cmd == DSP_CMD_DATA_READ) {
Packit 675970
		if (read_status.status == DSP_OK) {
Packit 675970
			DPRINT("---------> DSP: ### %d ###\n",
Packit 675970
			       read_status.frame_size);
Packit 675970
			ret = count;
Packit 675970
			DPRINT("%d words sent\n", ret);
Packit 675970
		} else {
Packit 675970
			report_dsp_protocol("Receive a status "
Packit 675970
					"different from DSP_OK (skiping block)",
Packit 675970
			     dsp_protocol);
Packit 675970
			report_return_value("Returned: ", read_status.status);
Packit 675970
			ret = 0;
Packit 675970
		}
Packit 675970
Packit 675970
	} else {
Packit 675970
		report_dsp_protocol("Could not receive audio data",
Packit 675970
				    dsp_protocol);
Packit 675970
		DPRINT("Returned cmd %d expected %d\n", read_status.dsp_cmd,
Packit 675970
		       DSP_CMD_DATA_READ);
Packit 675970
		report_return_value("Returned: ", read_status.status);
Packit 675970
		ret = 0;
Packit 675970
	}
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 *
Packit 675970
 * It pauses a playback section sending a DSP_CMD_PAUSE.
Packit 675970
 *
Packit 675970
 * @return zero if success, otherwise a negative error code
Packit 675970
 *          (-EIO - sending play from a non-valid state).
Packit 675970
 */
Packit 675970
int dsp_protocol_send_pause(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_PLAYING) {
Packit 675970
		report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	if (dsp_protocol->state == STATE_PAUSED)
Packit 675970
		ret = 0;
Packit 675970
	else {
Packit 675970
		if ((ret =
Packit 675970
		     dsp_protocol_send_command(dsp_protocol,
Packit 675970
					       DSP_CMD_PAUSE)) == 0)
Packit 675970
			dsp_protocol->state = STATE_PAUSED;
Packit 675970
	}
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 *
Packit 675970
 * It stops a playback section sending a DSP_CMD_STOP.
Packit 675970
 *
Packit 675970
 * @return zero if success, otherwise a negative error code
Packit 675970
 *          (-EIO - sending play from a non-valid state).
Packit 675970
 */
Packit 675970
int dsp_protocol_send_stop(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_PLAYING) {
Packit 675970
		report_dsp_protocol("Not in the STATE_PLAYING\n", dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	if (dsp_protocol->state == STATE_STOPPED)
Packit 675970
		ret = 0;
Packit 675970
	else {
Packit 675970
		if ((ret =
Packit 675970
		     dsp_protocol_send_command(dsp_protocol,
Packit 675970
					       DSP_CMD_STOP)) == 0)
Packit 675970
			dsp_protocol->state = STATE_STOPPED;
Packit 675970
	}
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/* Deletion phase features definitions */
Packit 675970
/**
Packit 675970
 * @param dsp_protocol DSP protocol reference pointer to be uninitialized.
Packit 675970
 * 
Packit 675970
 * It closes the connection with pcm dsp task node. It flushes all
Packit 675970
 * pending data before that. Closes the mmap area. Initialize the
Packit 675970
 * dsp_protocol structure with unused values.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_close_node(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_UNINITIALISED) {
Packit 675970
		if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
			goto out;
Packit 675970
		if ((ret = dsp_protocol_flush(dsp_protocol)) < 0)
Packit 675970
			goto unlock;
Packit 675970
		if ((ret = dsp_protocol_send_command(dsp_protocol,
Packit 675970
						     DSP_CMD_CLOSE)) < 0)
Packit 675970
			goto unlock;
Packit 675970
	}
Packit 675970
Packit 675970
	if (dsp_protocol->mmap_buffer)
Packit 675970
		munmap(dsp_protocol->mmap_buffer,
Packit 675970
		       dsp_protocol->mmap_buffer_size);
Packit 675970
	close(dsp_protocol->fd);
Packit 675970
	dsp_protocol->fd = -1;
Packit 675970
	free(dsp_protocol->device);
Packit 675970
	dsp_protocol->device = NULL;
Packit 675970
	dsp_protocol->state = STATE_UNINITIALISED;
Packit 675970
	dsp_protocol->mute = 0;
Packit 675970
	dsp_protocol->stream_id = 0;
Packit 675970
	dsp_protocol->bridge_buffer_size = 0;
Packit 675970
	dsp_protocol->mmap_buffer_size = 0;
Packit 675970
	dsp_protocol->mmap_buffer = NULL;
Packit 675970
	ret = 0;
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol DSP protocol reference pointer.
Packit 675970
 * 
Packit 675970
 * It frees all the allocated memory. It sets NULL to the pointer. 
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_destroy(dsp_protocol_t ** dsp_protocol)
Packit 675970
{
Packit 675970
	int ret = 0;
Packit 675970
	DENTER();
Packit 675970
#ifdef USE_RESOURCE_MANAGER
Packit 675970
        if ((*dsp_protocol)->dbus_connection)
Packit 675970
                dbus_connection_unref((*dsp_protocol)->dbus_connection);
Packit 675970
#endif /* USE_RESOURCE_MANAGER */
Packit 675970
	if (*dsp_protocol) {
Packit 675970
		if ((*dsp_protocol)->device)
Packit 675970
			free((*dsp_protocol)->device);
Packit 675970
		free((*dsp_protocol));
Packit 675970
		*dsp_protocol = NULL;
Packit 675970
	}
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/* controls features definitions */
Packit 675970
/**
Packit 675970
 * dsp_protocol_set_volume:
Packit 675970
 * 
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param left left channel volume value (0 - 100).
Packit 675970
 * @param right right channel volume value (0 - 100).
Packit 675970
 * 
Packit 675970
 * It changes volume data for both left and right channels.
Packit 675970
 * It receives both values in a 0 - 100 scale. 
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_set_volume(dsp_protocol_t * dsp_protocol,
Packit 675970
			    unsigned char left, unsigned char right)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	dsp_cmd_status_t audio_cmd_status;
Packit 675970
	volume_data_t volume_data;
Packit 675970
	panning_data_t panning_data;
Packit 675970
Packit 675970
	DENTER();
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	dsp_protocol_linear2Q15(left > right ? left : right, &volume_data.scale,
Packit 675970
				&volume_data.power2);
Packit 675970
	volume_data.dsp_cmd = DSP_CMD_SET_VOLUME;
Packit 675970
	if ((ret =
Packit 675970
	     write(dsp_protocol->fd, &volume_data, sizeof(volume_data_t))) < 0)
Packit 675970
		goto unlock;
Packit 675970
	if (read(dsp_protocol->fd, &audio_cmd_status, 
Packit 675970
		sizeof(dsp_cmd_status_t)) < 0) {
Packit 675970
		ret = -EINVAL;
Packit 675970
		report_dsp_protocol("Could not receive DSP_CMD_STATUS",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	report_return_value("Received", audio_cmd_status.status);
Packit 675970
	ret = 0;
Packit 675970
	if (audio_cmd_status.status != DSP_OK)
Packit 675970
		ret = -EIO;
Packit 675970
	if (ret == 0) {		/*if sucess till here, update panning info */
Packit 675970
		panning_data.dsp_cmd = DSP_CMD_SET_PANNING;
Packit 675970
		panning_data.steps = PANNING_STEP;
Packit 675970
		if (left != right) {
Packit 675970
			panning_data.left_gain =
Packit 675970
			    left >
Packit 675970
			    right ? 0x4000 : (1.0 * left) / right * 0x4000;
Packit 675970
			panning_data.right_gain =
Packit 675970
			    right >
Packit 675970
			    left ? 0x4000 : (1.0 * right) / left * 0x4000;
Packit 675970
		} else {
Packit 675970
			panning_data.left_gain = 0x4000;
Packit 675970
			panning_data.right_gain = 0x4000;
Packit 675970
		}
Packit 675970
		DPRINT("left gain %x right gain %x\n", panning_data.left_gain,
Packit 675970
		       panning_data.right_gain);
Packit 675970
		if ((ret =
Packit 675970
		     write(dsp_protocol->fd, &panning_data,
Packit 675970
			   sizeof(panning_data_t))) < 0)
Packit 675970
			goto unlock;
Packit 675970
		if (read(dsp_protocol->fd, &audio_cmd_status,
Packit 675970
		         sizeof(dsp_cmd_status_t)) < 0) {
Packit 675970
			ret = -EINVAL;
Packit 675970
			report_dsp_protocol("Could not receive DSP_CMD_STATUS",
Packit 675970
					    dsp_protocol);
Packit 675970
			goto unlock;
Packit 675970
		}
Packit 675970
		ret = 0;
Packit 675970
		if (audio_cmd_status.status != DSP_OK)
Packit 675970
			ret = -EIO;
Packit 675970
Packit 675970
	}
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param left left channel volume value output (0 - 100).
Packit 675970
 * @param right right channel volume value output (0 - 100).
Packit 675970
 * 
Packit 675970
 * It returns volume data for both left and right channels.
Packit 675970
 * It provides both values in a 0 - 100 scale. 
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_get_volume(dsp_protocol_t * dsp_protocol,
Packit 675970
			    unsigned char *left, unsigned char *right)
Packit 675970
{
Packit 675970
	unsigned short int tmp;
Packit 675970
	int ret;
Packit 675970
	audio_status_info_t audio_status_info;
Packit 675970
	DENTER();
Packit 675970
	tmp = DSP_CMD_STATE;
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
Packit 675970
	if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
Packit 675970
		ret = -EIO;
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	if ((ret = read(dsp_protocol->fd, &audio_status_info,
Packit 675970
			sizeof(audio_status_info_t))) < 0) {
Packit 675970
		report_dsp_protocol("Could not read audio_status_info",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto unlock;
Packit 675970
	}
Packit 675970
	dsp_protocol->state = audio_status_info.status;
Packit 675970
	report_audio_status_info("Received:", audio_status_info);
Packit 675970
	dsp_protocol_Q152linear(audio_status_info.vol_scale,
Packit 675970
				audio_status_info.vol_power2, &tmp);
Packit 675970
	*left = tmp;
Packit 675970
	*right = tmp;
Packit 675970
	if (audio_status_info.number_channels == CHANNELS_2) {
Packit 675970
		if (audio_status_info.left_gain > audio_status_info.right_gain){
Packit 675970
			float result =
Packit 675970
			    *right * audio_status_info.right_gain / (0x4000 *
Packit 675970
								     1.0);
Packit 675970
			*right = result;
Packit 675970
			if ((result - *right) > 0.5)
Packit 675970
				(*right)++;
Packit 675970
		}
Packit 675970
		if (audio_status_info.left_gain < audio_status_info.right_gain){
Packit 675970
			float result =
Packit 675970
			    *left * audio_status_info.left_gain / (0x4000 *
Packit 675970
								   1.0);
Packit 675970
			*left = result;
Packit 675970
			if ((result - *left) > 0.5)
Packit 675970
				(*left)++;
Packit 675970
		}
Packit 675970
	}
Packit 675970
	ret = 0;
Packit 675970
      unlock:
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param mute mute value (0 unmuted - 1 muted).
Packit 675970
 * 
Packit 675970
 * It changes mute state of DSP task node sending DSP_CMD_[UN]MUTE.
Packit 675970
 * Provide 0 in mute to unmute, and 1 in mute to mute.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_set_mute(dsp_protocol_t * dsp_protocol, unsigned char mute)
Packit 675970
{
Packit 675970
	int ret = 0;
Packit 675970
	int command;
Packit 675970
	DENTER();
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	if (mute == 1)
Packit 675970
		command = DSP_CMD_MUTE;
Packit 675970
	else
Packit 675970
		command = DSP_CMD_UNMUTE;
Packit 675970
	ret = dsp_protocol_send_command(dsp_protocol, command);
Packit 675970
	dsp_protocol->mute = mute;
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * 
Packit 675970
 * It returns mute state of DSP task node.
Packit 675970
 * 
Packit 675970
 * @return zero if success, it returns current state of mute.
Packit 675970
 * 0 means not muted. 1 means muted. otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_get_mute(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret = 0;
Packit 675970
	DENTER();
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	ret = dsp_protocol_update_state(dsp_protocol);
Packit 675970
	if (ret >= 0)
Packit 675970
		ret = dsp_protocol->mute;
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/*miscelaneos features definitions */
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param enable enabled state of mic. 0 disable. 1 enabled. 
Packit 675970
 * 
Packit 675970
 * It modifies enabled state of mic.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
#ifdef USE_RESOURCE_MANAGER
Packit 675970
int dsp_protocol_set_mic_enabled(dsp_protocol_t * dsp_protocol, int enabled)
Packit 675970
{
Packit 675970
	int ret = 0;
Packit 675970
	DBusMessage *message;
Packit 675970
	DBusMessage *reply = NULL;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->dbus_connection &&
Packit 675970
	    (message = dbus_message_new_method_call(AUDIO_PM_SERVICE,
Packit 675970
				AUDIO_PM_RECORD_RESOURCE, RESOURCE_INTERFACE,
Packit 675970
				enabled ? "request" : "release"))) {
Packit 675970
		if (!enabled) {
Packit 675970
			dbus_int32_t handle = 0;
Packit 675970
			/* assuming handle is always 0 for this resource */
Packit 675970
			dbus_message_append_args(message,
Packit 675970
						 DBUS_TYPE_INT32, &handle,
Packit 675970
						 DBUS_TYPE_INVALID);
Packit 675970
		}
Packit 675970
		reply = dbus_connection_send_with_reply_and_block(
Packit 675970
					dsp_protocol->dbus_connection,
Packit 675970
					message, RESOURCE_TIMEOUT, NULL);
Packit 675970
                /* TODO: check return value(s), append args for request()
Packit 675970
                 * These parts of the RM spec may still change so not done yet
Packit 675970
                 */
Packit 675970
		dbus_message_unref(message);
Packit 675970
	}
Packit 675970
	if (reply) {
Packit 675970
		dbus_message_unref(reply);
Packit 675970
	} else {
Packit 675970
		DPRINT("Error acquiring PM resource for recording\n");
Packit 675970
		#ifdef ERROR_ON_PM_FAILURE
Packit 675970
		ret = -EIO;
Packit 675970
		#endif
Packit 675970
	}
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
#else /* !USE_RESOURCE_MANAGER */
Packit 675970
int dsp_protocol_set_mic_enabled(dsp_protocol_t * dsp_protocol, int enabled)
Packit 675970
{
Packit 675970
	int mic_fd;
Packit 675970
	int ret;
Packit 675970
	char mic_control_name[] = "/sys/devices/platform/audio-i2c/mic_enable";
Packit 675970
	DENTER();
Packit 675970
	if ((mic_fd = open(mic_control_name, O_WRONLY)) < 0) {
Packit 675970
		DPRINT("Error opening: %s\n", mic_control_name);
Packit 675970
#ifdef ERROR_ON_PM_FAILURE
Packit 675970
		ret = -EIO;
Packit 675970
#else
Packit 675970
		ret = 0;
Packit 675970
#endif
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
Packit 675970
	write(mic_fd, (enabled ? "1" : "0"), sizeof(char));
Packit 675970
	close(mic_fd);
Packit 675970
	ret = 0;
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
#endif /* USE_RESOURCE_MANAGER */
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol dsp_protocol_t structure.
Packit 675970
 * @param device dsp task node file name.
Packit 675970
 * 
Packit 675970
 * It opens a dsp task node and queries for its states.
Packit 675970
 * 
Packit 675970
 * @return if success, it returns number of channels whose dsp task is 
Packit 675970
 * currently using (1 - mono. 2 stero). otherwise a negative error code.
Packit 675970
 */
Packit 675970
int dsp_protocol_probe_node(dsp_protocol_t * dsp_protocol, const char *device)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	DENTER();
Packit 675970
	if (dsp_protocol->state != STATE_UNINITIALISED) {
Packit 675970
		report_dsp_protocol
Packit 675970
		    ("Trying to send open node from a non-valid state",
Packit 675970
		     dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
Packit 675970
	dsp_protocol->fd = open(device, O_RDWR);
Packit 675970
	if (dsp_protocol->fd < 0) {
Packit 675970
		DERROR("Could not open pcm device file %s\n", device);
Packit 675970
		ret = errno;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	dsp_protocol->device = strdup(device);
Packit 675970
	dsp_protocol_get_sem(dsp_protocol);
Packit 675970
Packit 675970
	if ((ret = dsp_protocol_lock_dev(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	dsp_protocol->device = strdup(device);
Packit 675970
	ret = dsp_protocol_update_state(dsp_protocol);
Packit 675970
	if (ret != CHANNELS_1 && ret != CHANNELS_2)
Packit 675970
		ret = CHANNELS_1;
Packit 675970
	dsp_protocol_unlock_dev(dsp_protocol);
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param str string with a number.
Packit 675970
 * @param val holder for the conversion result.
Packit 675970
 * 
Packit 675970
 * It converts a string to a number (long).
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
int safe_strtol(const char *str, long *val)
Packit 675970
{
Packit 675970
	char *end;
Packit 675970
	long v;
Packit 675970
	int ret;
Packit 675970
	DENTER();
Packit 675970
	if (!*str) {
Packit 675970
		ret = -EINVAL;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	errno = 0;
Packit 675970
	v = strtol(str, &end, 0);
Packit 675970
	if (errno) {
Packit 675970
		ret = -errno;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if (*end) {
Packit 675970
		ret = -EINVAL;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	*val = v;
Packit 675970
	ret = 0;
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
/* internal features definitions */
Packit 675970
/**
Packit 675970
 * @param dsp_protocol  dsp_protocol_t structure.
Packit 675970
 * 
Packit 675970
 * Tries to read all the pending data.
Packit 675970
 *
Packit 675970
 * @return zero. success
Packit 675970
 */
Packit 675970
static int dsp_protocol_flush(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	struct pollfd pollf;
Packit 675970
	int ret = 0;
Packit 675970
	int tmp;
Packit 675970
	DENTER();
Packit 675970
	pollf.fd = dsp_protocol->fd;
Packit 675970
	pollf.events = POLLIN;
Packit 675970
	while (poll(&pollf, 1, 0) > 0) {
Packit 675970
		if (read(dsp_protocol->fd, &tmp, sizeof(short int)) == 0)
Packit 675970
			/* Test end of file */
Packit 675970
			break;
Packit 675970
#ifdef DEBUG
Packit 675970
		fprintf(DEBUG_OUTPUT, ".");
Packit 675970
#endif
Packit 675970
	}
Packit 675970
#ifdef DEBUG
Packit 675970
	fprintf(DEBUG_OUTPUT, "\n");
Packit 675970
#endif
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol  dsp_protocol_t structure.
Packit 675970
 * @param command command value to be sent to the pcm task node.
Packit 675970
 * 
Packit 675970
 * Send the command to pcm task node and checks if
Packit 675970
 * it was sent properly.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
static int dsp_protocol_send_command(dsp_protocol_t * dsp_protocol,
Packit 675970
				     const short int command)
Packit 675970
{
Packit 675970
	dsp_cmd_status_t audio_cmd_status;
Packit 675970
	int ret = 0;
Packit 675970
	short int tmp;
Packit 675970
	DENTER();
Packit 675970
	report_command("Sending", command);
Packit 675970
	tmp = command;
Packit 675970
	if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
Packit 675970
		report_dsp_protocol("Could not send", dsp_protocol);
Packit 675970
		ret = -EIO;
Packit 675970
	}
Packit 675970
Packit 675970
	if (read(dsp_protocol->fd, &audio_cmd_status, 
Packit 675970
		sizeof(dsp_cmd_status_t)) < 0) {
Packit 675970
		ret = -1;
Packit 675970
		report_dsp_protocol("Could not receive DSP_CMD_STATUS",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
/*	report_command("Received", audio_cmd_status.dsp_cmd);
Packit 675970
	report_return_value("Received",  audio_cmd_status.status);*/
Packit 675970
	DPRINT("audio_cmd_status.dsp_cmd: 0x%x\n", audio_cmd_status.dsp_cmd);
Packit 675970
	DPRINT("audio_cmd_status.status: 0x%x\n", audio_cmd_status.status);
Packit 675970
	if (audio_cmd_status.status != DSP_OK)
Packit 675970
		ret = -EIO;
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param input 0 - 100 value to be converted to Q15. 
Packit 675970
 * @param scale scale value of Q15 format.
Packit 675970
 * @param power2 power of 2 value of Q15 format.
Packit 675970
 * 
Packit 675970
 * Converts a 0 - 100 value to a Q15 format value.
Packit 675970
 * 
Packit 675970
 */
Packit 675970
static void dsp_protocol_linear2Q15(const unsigned short int input,
Packit 675970
				    unsigned short int *scale, 
Packit 675970
				    unsigned short int *power2)
Packit 675970
{
Packit 675970
	unsigned long int val = MAGIC_NUMBER * input;
Packit 675970
	DENTER();
Packit 675970
	if (input == 0) {
Packit 675970
		*scale = 0;
Packit 675970
		*power2 = 0;
Packit 675970
	} else {
Packit 675970
		*power2 = 1;
Packit 675970
		while (val < 0x40000000) {
Packit 675970
			(*power2)--;
Packit 675970
			val <<= 1;
Packit 675970
		}
Packit 675970
		*scale = val >> 16;
Packit 675970
	}
Packit 675970
	DPRINT("Resulted scale %d and power2 %d from input %d\n", *scale,
Packit 675970
	       *power2, input);
Packit 675970
	DLEAVE(0);
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param scale scale value of Q15 format.
Packit 675970
 * @param power2 power of 2 value of Q15 format.
Packit 675970
 * @param output 0 - 100 resulted value.
Packit 675970
 * 
Packit 675970
 * Converts a Q15 format value to a 0 - 100 value.
Packit 675970
 * 
Packit 675970
 */
Packit 675970
static void dsp_protocol_Q152linear(const unsigned short int scale,
Packit 675970
				    const unsigned short int power2, unsigned short int *output)
Packit 675970
{
Packit 675970
	float result = scale * 1.0 / 0x8000 * (1 << power2) * 100.0;
Packit 675970
	DENTER();
Packit 675970
	*output = (short int)(result);
Packit 675970
	if ((result - *output) > 0.5)
Packit 675970
		(*output)++;
Packit 675970
	DPRINT("Resulted linear: %d from scale %d and power2 %d\n", *output,
Packit 675970
	       scale, power2);
Packit 675970
	DLEAVE(0);
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol  dsp_protocol_t structure.
Packit 675970
 * 
Packit 675970
 * Update dsp_protocol_t structure info. Queries dsp task
Packit 675970
 * for a audio_status_info structure.
Packit 675970
 * 
Packit 675970
 * @return if success, it returns number of channels whose dsp task is 
Packit 675970
 * currently using. otherwise a negative error code.
Packit 675970
 */
Packit 675970
static int dsp_protocol_update_state(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	short int tmp;
Packit 675970
	audio_status_info_t audio_status_info;
Packit 675970
	DENTER();
Packit 675970
Packit 675970
	if ((ret = dsp_protocol_flush(dsp_protocol)) < 0)
Packit 675970
		goto out;
Packit 675970
	tmp = DSP_CMD_STATE;
Packit 675970
	if (write(dsp_protocol->fd, &tmp, sizeof(short int)) < 0) {
Packit 675970
		ret = -EIO;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	if ((ret = read(dsp_protocol->fd, &audio_status_info,
Packit 675970
			sizeof(audio_status_info_t))) < 0) {
Packit 675970
		report_dsp_protocol("Could not read audio_status_info",
Packit 675970
				    dsp_protocol);
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	report_audio_status_info("Received:", audio_status_info);
Packit 675970
	dsp_protocol->stream_id = audio_status_info.stream_id;
Packit 675970
	dsp_protocol->bridge_buffer_size = audio_status_info.bridge_buffer_size;
Packit 675970
	dsp_protocol->mmap_buffer_size = audio_status_info.mmap_buffer_size;
Packit 675970
	dsp_protocol->state = audio_status_info.status;
Packit 675970
#ifndef NORMAL_DSP_TASK
Packit 675970
	dsp_protocol->mute = audio_status_info.mute;
Packit 675970
#endif
Packit 675970
	report_dsp_protocol("connection stablished:", dsp_protocol);
Packit 675970
	ret = audio_status_info.number_channels;
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol  dsp_protocol_t structure.
Packit 675970
 * 
Packit 675970
 * It produces the semaphore ID and connects to it.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
static inline int dsp_protocol_get_sem(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	union semun sem_val;	/* semaphore value, for semctl(). */
Packit 675970
Packit 675970
	int rc;
Packit 675970
	int ret;
Packit 675970
	/* identifier returned by ftok() */
Packit 675970
	key_t set_key;
Packit 675970
	DENTER();
Packit 675970
	/* generate a "unique" key for our set, using the */
Packit 675970
	/* directory "/dev/dsptask/xxxx".      */
Packit 675970
	set_key = ftok(dsp_protocol->device, 0);
Packit 675970
	if (set_key == -1) {
Packit 675970
		DPRINT("ftok: %d\n", errno);
Packit 675970
		ret = -ENODEV;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	DPRINT("key %d\n", set_key);
Packit 675970
Packit 675970
	/* now we can use 'set_key' to generate a set id, for example. */
Packit 675970
	dsp_protocol->sem_set_id = semget(set_key, 1, 0666);
Packit 675970
	if (dsp_protocol->sem_set_id == -1) {
Packit 675970
		DPRINT("semget %d\n", errno);
Packit 675970
		dsp_protocol->sem_set_id = semget(set_key, 1, IPC_CREAT | 0666);
Packit 675970
		if (dsp_protocol->sem_set_id == -1) {
Packit 675970
			DPRINT("semget: IPC_CREAT: %d\n", errno);
Packit 675970
			ret = -ENODEV;
Packit 675970
			goto out;
Packit 675970
		}
Packit 675970
		DPRINT("Initialising the semaphore\n");
Packit 675970
		sem_val.val = 1;
Packit 675970
		rc = semctl(dsp_protocol->sem_set_id, 0, SETVAL, sem_val);
Packit 675970
		if (rc == -1) {
Packit 675970
			DPRINT("semctl %d\n", errno);
Packit 675970
			ret = -ENODEV;
Packit 675970
			goto out;
Packit 675970
		}
Packit 675970
	}
Packit 675970
	ret = 0;
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol  dsp_protocol_t structure.
Packit 675970
 * 
Packit 675970
 * It waits until be able to hold the semaphore to access the dsp task node.
Packit 675970
 * If the current process already has the semaphore, returns success.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
static inline int dsp_protocol_lock_dev(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	int ret;
Packit 675970
	struct sembuf sem_op;
Packit 675970
	DENTER();
Packit 675970
	ret = pthread_mutex_trylock(&(dsp_protocol->mutex));
Packit 675970
	if (ret != 0) {
Packit 675970
		DPRINT("No lock %d\n", ret);
Packit 675970
		if (errno == EBUSY)
Packit 675970
			ret = 0;
Packit 675970
		goto out;
Packit 675970
	}
Packit 675970
	sem_op.sem_num = 0;
Packit 675970
	sem_op.sem_op = -1;
Packit 675970
	sem_op.sem_flg = 0;
Packit 675970
	DPRINT("requesting semaphore (dev)\n");
Packit 675970
	if (semop(dsp_protocol->sem_set_id, &sem_op, 1) == -1) {
Packit 675970
		DPRINT("semop %d\n", errno);
Packit 675970
		pthread_mutex_unlock(&(dsp_protocol->mutex));
Packit 675970
		ret = -errno;
Packit 675970
	}
Packit 675970
      out:
Packit 675970
	DLEAVE(ret);
Packit 675970
	return ret;
Packit 675970
}
Packit 675970
Packit 675970
/**
Packit 675970
 * @param dsp_protocol  dsp_protocol_t structure.
Packit 675970
 * 
Packit 675970
 * It releases the semaphore.
Packit 675970
 * 
Packit 675970
 * @return zero if success, otherwise a negative error code.
Packit 675970
 */
Packit 675970
static inline int dsp_protocol_unlock_dev(dsp_protocol_t * dsp_protocol)
Packit 675970
{
Packit 675970
	struct sembuf sem_op;
Packit 675970
	DENTER();
Packit 675970
	sem_op.sem_num = 0;
Packit 675970
	sem_op.sem_op = 1;
Packit 675970
	sem_op.sem_flg = 0;
Packit 675970
	DPRINT("Releasing\n");
Packit 675970
	semop(dsp_protocol->sem_set_id, &sem_op, 1);
Packit 675970
Packit 675970
	pthread_mutex_unlock(&(dsp_protocol->mutex));
Packit 675970
	DLEAVE(0);
Packit 675970
	return 0;
Packit 675970
}
Packit 675970
Packit 675970