/*
 *  Digital Audio (PCM) - /proc interface
 *  Copyright (c) by Jaroslav Kysela <perex@jcu.cz>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "driver.h"
#include "pcm.h"
#include "info.h"
#include <fcntl.h>
#define SND_PCM_PROC_BUFFER_SIZE	(128*1024)

struct snd_pcm_proc_private {
	unsigned char *buffer;		/* allocated buffer */
	unsigned int size;
	unsigned int used;
	unsigned int head;
	unsigned int tail;
	unsigned int xrun;
	unsigned int sleep:1,
		     packet:1,
		     active:1,		/* write is active */
		     change:1;		/* format was changed */
	snd_pcm_format_t format;	/* active format */
	snd_mutex_define(ptr);
	snd_mutex_define(read);
	snd_sleep_define(read);
	struct snd_pcm_proc_private *next;
};

static void inline snd_pcm_proc_file_reset(struct snd_pcm_proc_private *proc)
{
	proc->head = proc->tail = proc->used = 0;
}

static void snd_pcm_proc_file_write(struct snd_pcm_proc_private *proc,
				    const void *buffer, unsigned int count)
{
	unsigned int tmp, size, size1;

	if (!count)
		return;
	snd_mutex_lock(proc, ptr);
	proc->active = 1;
	tmp = proc->size - proc->used;	/* free */
	if (tmp < count) {
		proc->xrun += count - tmp;
		count = tmp;
	}
	tmp = proc->size - proc->head;
	snd_mutex_unlock(proc, ptr);
	size = count;
	if (tmp < size)
		size = tmp;
	size1 = count - size;
	memcpy(proc->buffer + proc->head, buffer, size);
	if (size1 > 0)
		memcpy(proc->buffer, (char *) buffer + size, size1);
	snd_mutex_lock(proc, ptr);
	proc->head += count;
	proc->head %= proc->size;
	proc->used += count;
	proc->active = 0;
	snd_mutex_unlock(proc, ptr);
	if (proc->sleep)
		snd_wakeup(proc, read);
}

static void snd_pcm_proc_file_format_write(struct snd_pcm_proc_private *proc)
{
	struct {
		snd_pcm_loopback_header_t header;
		snd_pcm_format_t format;
	} data;

	data.header.size = sizeof(snd_pcm_format_t);
	data.header.type = SND_PCM_LB_TYPE_FORMAT;
	memcpy(&data.format, &proc->format, sizeof(snd_pcm_format_t));
	snd_pcm_proc_file_write(proc, &data, sizeof(data));
}

void snd_pcm_proc_format(snd_pcm_channel_t * pchn,
			 snd_pcm_format_t * format)
{
	struct snd_pcm_proc_private *first;

	snd_mutex_lock(pchn, proc);
	first = (struct snd_pcm_proc_private *) pchn->proc_private;
	while (first) {
		memcpy(&first->format, format, sizeof(snd_pcm_format_t));
		first->change = 1;
		first = first->next;
	}
	snd_mutex_unlock(pchn, proc);
}

void snd_pcm_proc_write(snd_pcm_channel_t * pchn,
			const void *buffer, unsigned int count)
{
	struct snd_pcm_proc_private *first;
	snd_pcm_loopback_header_t header;

	snd_mutex_lock(pchn, proc);
	first = (struct snd_pcm_proc_private *) pchn->proc_private;
	while (first) {
		if (first->packet) {
			if (first->change) {
				snd_pcm_proc_file_format_write(first);
				first->change = 0;
			}
			header.size = count;
			header.type = SND_PCM_LB_TYPE_DATA;
			snd_pcm_proc_file_write(first, &header, sizeof(header));
		}
		snd_pcm_proc_file_write(first, buffer, count);
		first = first->next;
	}
	snd_mutex_unlock(pchn, proc);
}

static long snd_pcm_proc_read(void *private_data, void * file_private_data,
			      snd_context_t *ctp, char *buf, long count)
{
	struct snd_pcm_proc_private *proc;
	unsigned int size, size1;
	long count1, result = 0;

	proc = (struct snd_pcm_proc_private *) file_private_data;
	if (count > proc->size)
		count = proc->size;
	if (!count)
		return result;
	while (count > 0) {
		while (!proc->used) {
			if (ctp->nonblock)
				return result;
			snd_mutex_lock(proc, read);
			proc->sleep = 1;
			snd_sleep(proc, read, read, 500);
			proc->sleep = 0;
			snd_mutex_unlock(proc, read);
			if (snd_sleep_abort(proc, read)) {
				return -EINTR;
			}
		}
		count1 = count;
		snd_mutex_lock(proc, ptr);
		if (count1 > proc->used)
			count1 = proc->used;
		size = count1;
		if (proc->size - proc->tail < size)
			size = proc->size - proc->tail;
		size1 = count1 - size;
		snd_mutex_unlock(proc, ptr);
		snd_copy_to_user(ctp, proc->buffer + proc->tail, size);
		buf += size;
		if (size1 > 0) {
			snd_copy_to_user(ctp, proc->buffer, size1);
			buf += size1;
		}
		snd_mutex_lock(proc, ptr);
		proc->tail += count1;
		proc->tail %= proc->size;
		proc->used -= count1;
		snd_mutex_unlock(proc, ptr);
		count -= count1;
		result += count1;
	}
	return result;
}

static int snd_pcm_proc_open(void *private_data, snd_info_entry_t * entry,
			     unsigned short mode, void **file_private_data)
{
	struct snd_pcm_proc_private *proc, *proc1;
	snd_pcm_t *pcm;
	snd_pcm_channel_t *pchn;
	int record;

	if (mode == O_RDWR || mode == O_WRONLY)
		return -EIO;
	proc = (struct snd_pcm_proc_private *)
			snd_calloc(sizeof(struct snd_pcm_proc_private));
	if (!proc)
		return -ENOMEM;
	proc->buffer = snd_malloc(proc->size = SND_PCM_PROC_BUFFER_SIZE);
	if (!proc->buffer) {
		snd_free(proc, sizeof(struct snd_pcm_proc_private));
		return -ENOMEM;
	}
	snd_mutex_prepare(proc, ptr);
	snd_mutex_prepare(proc, read);
	snd_sleep_prepare(proc, read);
	pcm = (snd_pcm_t *) private_data;
	record = entry->name[strlen(entry->name) - 1] == 'r';
	pchn = !record ? &pcm->playback : &pcm->record;
	snd_mutex_lock(pchn, proc);
	proc1 = (struct snd_pcm_proc_private *) pchn->proc_private;
	if (!proc1) {
		pchn->proc_private = proc;
	} else {
		while (proc1->next)
			proc1 = proc1->next;
		proc1->next = proc;
	}
	snd_mutex_unlock(pchn, proc);
	*file_private_data = proc;
	return 0;
}

static int snd_pcm_proc_release(void *private_data, snd_info_entry_t * entry,
			        unsigned short mode, void *file_private_data)
{
	struct snd_pcm_proc_private *proc, *proc1;
	snd_pcm_t *pcm;
	snd_pcm_channel_t *pchn;
	int record;

	proc = (struct snd_pcm_proc_private *) file_private_data;
	if (!proc) {
		return -EIO;
	}
	pcm = (snd_pcm_t *) private_data;
	record = entry->name[strlen(entry->name) - 1] == 'r';
	pchn = !record ? &pcm->playback : &pcm->record;
	snd_mutex_lock(pchn, proc);
	proc1 = (struct snd_pcm_proc_private *) pchn->proc_private;
	if (proc == proc1) {
		pchn->proc_private = proc->next;
	} else {
		while (proc1->next != proc)
			proc1 = proc1->next;
		proc1->next = proc->next;
	}
	snd_mutex_unlock(pchn, proc);
	snd_mutex_destroy(proc, ptr);
	snd_mutex_destroy(proc, read);
	snd_sleep_destroy(proc, read);
	snd_free(proc->buffer, proc->size);
	snd_free(proc, sizeof(struct snd_pcm_proc_private));
	return 0;
}

static int snd_pcm_proc_ioctl(void * private_data,
			      void * file_private_data,
			      snd_ocb_t *ocb,
			      unsigned int cmd,
			      unsigned long arg)
{
	struct snd_pcm_proc_private *proc;

	proc = (struct snd_pcm_proc_private *) file_private_data;
#if 0
	if (((cmd >> 8) & 0xff) != 'L') return -ENXIO;
	switch (cmd) {
	case SND_PCM_LB_IOCTL_PVERSION:
		return snd_ioctl_out((long *) arg, SND_PCM_LB_VERSION);
	case SND_PCM_LB_IOCTL_STREAM_MODE:
		snd_spin_lock(proc, ptr, &flags);
		while (proc->active) {
			snd_spin_unlock(proc, ptr, &flags);
			snd_schedule(proc, active, 1);
			snd_spin_lock(proc, ptr, &flags);
		}
		switch (snd_ioctl_in((long *) arg)) {
			case SND_PCM_LB_STREAM_MODE_PACKET:
				proc->packet=1;
			default:
				proc->packet=0;
		}
		snd_pcm_proc_file_reset(proc);
		snd_spin_unlock(proc, ptr, &flags);
		return 0;
	case SND_PCM_LB_IOCTL_FORMAT:
		if (verify_area(VERIFY_WRITE, (void *) arg, sizeof(snd_pcm_format_t)))
			return -EFAULT;
		copy_to_user((void *) arg, &proc->format, sizeof(snd_pcm_format_t));
		return 0;
	}
#endif
	return -ENXIO;
}

static int snd_pcm_proc_notify(void *private_data, void *file_private_data,
			       snd_ocb_t *ocb, int sel_type)
{
	struct snd_pcm_proc_private *proc;

	proc = (struct snd_pcm_proc_private *) file_private_data;
#if 0
	if (sel_type == SEL_IN) {
		snd_spin_lock(proc, sleep, &flags);
		if (!proc->used) {
			proc->sleep = 1;
			snd_select_wait(proc, read, wait);
			snd_spin_unlock(proc, sleep, &flags);
			return 0;
		}
		proc->sleep = 0;
		snd_spin_unlock(proc, sleep, &flags);
	}
#endif
	return 0;
}

static void snd_pcm_proc_init_direction(snd_pcm_t * pcm, int direction)
{
	char name[16];
	snd_info_entry_t *entry;
	snd_pcm_channel_t *pchn;

	pchn = !direction ? &pcm->playback : &pcm->record;
	sprintf(name, "pcm%d%s", pcm->device, !direction ? "p" : "r");
	entry = snd_info_create_entry(pcm->card, name);
	if (entry) {
		entry->type = SND_INFO_ENTRY_DATA;
		entry->private_data = pcm;
		entry->t.data.open = snd_pcm_proc_open;
		entry->t.data.release = snd_pcm_proc_release;
		entry->t.data.read = snd_pcm_proc_read;
		entry->t.data.ioctl = snd_pcm_proc_ioctl;
		entry->t.data.notify = snd_pcm_proc_notify;
		if (snd_info_register(entry) < 0) {
			snd_info_free_entry(entry);
			entry = NULL;
		}
	}
	pchn->proc_entry = entry;
}


void snd_pcm_proc_init(snd_pcm_t * pcm)
{
	snd_pcm_channel_t *pchn;
	
	pchn = &pcm->playback;
	snd_mutex_prepare(pchn, proc);
	pchn = &pcm->record;
	snd_mutex_prepare(pchn, proc);
	if (pcm->info_flags & SND_PCM_INFO_PLAYBACK) {
		snd_pcm_proc_init_direction(pcm, 0);
	}
	if (pcm->info_flags & SND_PCM_INFO_RECORD) {
		snd_pcm_proc_init_direction(pcm, 1);
	}
}

void snd_pcm_proc_done(snd_pcm_t * pcm)
{
	snd_pcm_channel_t *pchn;

	if ((pcm->info_flags & SND_PCM_INFO_RECORD) && pcm->record.proc_entry) {
		snd_info_unregister((snd_info_entry_t *) pcm->record.proc_entry);
		pcm->record.proc_entry = NULL;
	}
	if ((pcm->info_flags & SND_PCM_INFO_PLAYBACK) && pcm->playback.proc_entry) {
		snd_info_unregister((snd_info_entry_t *) pcm->playback.proc_entry);
		pcm->playback.proc_entry = NULL;
	}
	
	pchn = &pcm->playback;
	snd_mutex_destroy(pchn, proc);
	pchn = &pcm->record;
	snd_mutex_destroy(pchn, proc);
}
