/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
*/

#ident "$Header: fifo_buffer.c 1.6 90/04/24 $"

/*
 * fifo_buffer.c
 *	FIFO support functions for buffered I/O.
 */

/* $Log:	fifo_buffer.c,v $
 */
#include "sys/types.h"
#include "sys/cmn_err.h"
#include "sys/debug.h"
#include "sys/fifo_buffer.h"

#ifdef notyet
/*
 * fifo_c_incr()
 *	Increment the consumer location within the fifo 
 *	descriptor addressed by 'fp' by 'n' bytes. The caller
 *	is responsible for ensuring that the fifo contains 'n' 
 *	bytes of consumable data.
 *	
 *	This is useful when the data is moved out of the fifo
 *	by means other than provided by the fifo operations, i.e.
 *	DMA'ed to a device in a write operation.
 */
void
fifo_c_incr(fp, n)
	fifo_t *fp;
	uint n;
{
	ASSERT(n <= fifo_c_avail(fp), "fifo_c_incr: not enough data in fifo");
	/*
	 *+ fifo_c_incr() has been requested to increment
	 *+ a fifo buffer's consumer location beyond the 
	 *+ producer location, which is not allowed. The 
	 *+ caller has an internal problem.
	 */
	fp->consumer += n;
}

/*
 * fifo_p_incr()
 *	Increment the producer location within the fifo 
 *	descriptor addressed by 'fp' by 'n' bytes. The 
 *	caller is responsible for ensuring that the fifo
 *	has space in which to produce 'n' more bytes of data.
 *	
 *	This is useful when the data is moved into the fifo
 *	by means other than provided by the fifo operations, 
 *	i.e. DMA'ed from a device in a read operation.
 */
void
fifo_p_incr(fp, n)
	fifo_t *fp;
	uint n;
{
	ASSERT(n <= fifo_p_avail(fp), "fifo_p_incr: not enough space in fifo");
	/*
	 *+ fifo_p_incr() has been requested to increment
	 *+ a fifo buffer's producer location beyond the 
	 *+ end of the available fifo buffer space, which
	 *+ is not allowed.  The caller has an internal problem.
	 */
	fp->producer += n;
}

/*
 * fifo_copyin()
 *	Copy 'n' data bytes into the kernel space 
 *	fifo described by 'fp', starting at its
 *	producer location, from the user space buffer 
 *	addressed by 'cp'.  The caller is responsible 
 *	for ensuring that the fifo has space in which to 
 *	produce 'n' more bytes of data.
 *	
 *	Returns the copyin status value. Updates
 *	the fifo producer location if successful.
 */
int
fifo_copyin(fp, cp, n)
	fifo_t *fp;
	caddr_t	cp;
	uint n;
{
	int error;

	ASSERT(n <= fifo_p_avail(fp), "fifo_copyin: not enough space in fifo");
	/*
	 *+ fifo_copyin() has been requested to add more
	 *+ data than the fifo buffer has space 
	 *+ for.  This is not allowed. The caller has 
	 *+ an internal problem.
	 */
	error = copyin(fp->producer, cp, n);
	if (!error)
		fifo_p_incr(fp, n);
	return (error);
}

/*
 * fifo_copyout()
 *	Copy 'n' data bytes from the kernel space 
 *	fifo described by 'fp', starting at its 
 *	consumer location, to the user space buffer 
 *	addressed by 'cp'.  The caller is responsible 
 *	for ensuring that there are 'n' consumable 
 *	bytes of data in the fifo.
 *	
 *	Returns the copyout status value. Updates
 *	the fifo consumer location if successful.
 */
int
fifo_copyout(fp, cp, n)
	fifo_t *fp;
	caddr_t	cp;
	uint n;
{
	int error;

	ASSERT(n <= fifo_c_avail(fp), "fifo_copyout: not enough data in fifo");
	/*
	 *+ fifo_copyout() has been requested to consume
	 *+ more data than the fifo buffer has 
	 *+ available.  This is not allowed.  The caller 
	 *+ has an internal problem.
	 */
	error = copyout(fp->consumer, cp, n);
	if (!error)
		fifo_c_incr(fp, n);
	return (error);
}

/*
 * fifo_bcopyin()
 *	Copy 'n' data bytes to the kernel space 
 *	fifo described by 'fp', starting at its
 *	producer location, from the kernel space 
 *	buffer addressed by 'cp'.  The caller is 
 *	responsible for ensuring that the fifo has 
 *	space in which to produce 'n' more bytes 
 *	of data.
 */
void
fifo_bcopyin(fp, cp, n)
	fifo_t *fp;
	caddr_t	cp;
	uint n;
{
	ASSERT(n <= fifo_p_avail(fp),"fifo_bcopyin: not enough space in fifo");
	/*
	 *+ fifo_bcopyin() has been requested to add more
	 *+ data than the fifo buffer has space 
	 *+ for.  This is not allowed.  The caller has 
	 *+ an internal problem.
	 */
	bcopy(cp, fp->producer, n);
	fifo_p_incr(fp, n);
}

/*
 * fifo_bcopyout()
 *	Copy 'n' data bytes from the kernel space 
 *	fifo described by 'fp', starting at its
 *	consumer location, to the kernel space 
 *	buffer addressed by 'cp'.  The caller is 
 *	responsible for ensuring that there are
 *	'n' consumable bytes of data in the fifo.
 */
void
fifo_bcopyout(fp, cp, n)
	fifo_t *fp;
	caddr_t	cp;
	uint n;
{
	ASSERT(n <= fifo_c_avail(fp),"fifo_bcopyout: not enough data in fifo");
	/*
	 *+ fifo_bcopyout() has been requested to consume
	 *+ more data than the fifo buffer has 
	 *+ available.  This is not allowed.  The caller 
	 *+ has an internal problem.
	 */
	bcopy(fp->consumer, cp, n);
	fifo_c_incr(fp, n);
}

/*
 * fifo_pad()
 *	Zero 'n' data bytes in the fifo described by 
 *	'fp' starting at its producer location.  The 
 *	caller is responsible for ensuring that the 
 *	fifo has space in which to produce 'n' more 
 *	bytes of data.
 */
void
fifo_pad(fp, n)
	fifo_t *fp;
	uint n;
{
	ASSERT(n <= fifo_p_avail(fp),"fifo_pad: not enough space in fifo");
	/*
	 *+ fifo_pad() has been requested to add more NULL
	 *+ data than the fifo buffer has space 
	 *+ for.  This is not allowed.  The caller has 
	 *+ an internal problem.
	 */
	bzero(fp->producer, n);
	fifo_p_incr(fp, n);
}
#endif  /* notyet */
